1 |
package dk.thoerup.smssend; |
2 |
|
3 |
|
4 |
import java.util.ArrayList; |
5 |
import java.util.HashMap; |
6 |
import java.util.Map; |
7 |
|
8 |
import android.app.Activity; |
9 |
import android.app.ProgressDialog; |
10 |
import android.content.Intent; |
11 |
import android.content.SharedPreferences; |
12 |
import android.os.AsyncTask; |
13 |
import android.os.Bundle; |
14 |
import android.provider.ContactsContract; |
15 |
import android.telephony.SmsManager; |
16 |
import android.util.Log; |
17 |
import android.view.Menu; |
18 |
import android.view.MenuItem; |
19 |
import android.view.View; |
20 |
import android.view.View.OnClickListener; |
21 |
import android.widget.AdapterView; |
22 |
import android.widget.ArrayAdapter; |
23 |
import android.widget.Button; |
24 |
import android.widget.EditText; |
25 |
import android.widget.Spinner; |
26 |
import android.widget.Toast; |
27 |
import android.widget.AdapterView.OnItemSelectedListener; |
28 |
|
29 |
public class SmsSend extends Activity { |
30 |
/** Called when the activity is first created. */ |
31 |
|
32 |
final int MAXCOUNT = 25; |
33 |
final String SMSDAEMON = "42407617"; |
34 |
|
35 |
final int OPTIONS_SETTINGS = 1000; |
36 |
|
37 |
final int LOOKUP_PHONENR = 2000; |
38 |
|
39 |
|
40 |
@Override |
41 |
public void onCreate(Bundle savedInstanceState) { |
42 |
super.onCreate(savedInstanceState); |
43 |
setContentView(R.layout.main); |
44 |
|
45 |
Button select = (Button) findViewById(R.id.selectNumber); |
46 |
select.setOnClickListener( selectListener ); |
47 |
|
48 |
Button send = (Button) findViewById(R.id.send); |
49 |
send.setOnClickListener(sendListener); |
50 |
|
51 |
|
52 |
|
53 |
Spinner method = (Spinner) findViewById(R.id.method); |
54 |
ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this, R.array.methods, android.R.layout.simple_spinner_item); |
55 |
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
56 |
method.setOnItemSelectedListener(methodSelected); |
57 |
method.setAdapter(adapter); |
58 |
method.setSelection(0); |
59 |
|
60 |
} |
61 |
|
62 |
@Override |
63 |
public boolean onCreateOptionsMenu(Menu menu) { |
64 |
MenuItem item; |
65 |
|
66 |
item = menu.add(0, OPTIONS_SETTINGS, 0, "Settings"); |
67 |
item.setIcon(android.R.drawable.ic_menu_edit); |
68 |
|
69 |
return true; |
70 |
} |
71 |
|
72 |
@Override |
73 |
public boolean onOptionsItemSelected(MenuItem item) { |
74 |
boolean retval = true; |
75 |
|
76 |
//TODO: Cleanup |
77 |
switch (item.getItemId()) { |
78 |
case OPTIONS_SETTINGS: |
79 |
Intent i = new Intent(this, dk.thoerup.smssend.SmsConfig.class); |
80 |
startActivity(i); |
81 |
break; |
82 |
default: |
83 |
retval = super.onOptionsItemSelected(item); |
84 |
} |
85 |
return retval; |
86 |
|
87 |
} |
88 |
|
89 |
|
90 |
|
91 |
@Override |
92 |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
93 |
super.onActivityResult(requestCode, resultCode, data); |
94 |
|
95 |
switch(requestCode) { |
96 |
case LOOKUP_PHONENR: |
97 |
if (resultCode == Activity.RESULT_OK) { |
98 |
String phone = data.getStringExtra("phone"); |
99 |
EditText dest = (EditText) findViewById(R.id.destination); |
100 |
dest.setText(phone); |
101 |
} |
102 |
break; |
103 |
} |
104 |
} |
105 |
|
106 |
void lookup() { |
107 |
|
108 |
Intent intent = new Intent(Intent.ACTION_PICK); |
109 |
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); |
110 |
|
111 |
startActivityForResult(intent, LOOKUP_PHONENR); |
112 |
|
113 |
|
114 |
|
115 |
/* |
116 |
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, |
117 |
null, null, null, null); |
118 |
while (cursor.moveToNext()) { |
119 |
String contactId = cursor.getString(cursor.getColumnIndex( |
120 |
ContactsContract.Contacts._ID)); |
121 |
String hasPhone = cursor.getString(cursor.getColumnIndex( |
122 |
ContactsContract.Contacts.HAS_PHONE_NUMBER)); |
123 |
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); |
124 |
|
125 |
Log.v("contact", name + " : " + hasPhone); |
126 |
|
127 |
|
128 |
|
129 |
if (Integer.parseInt(hasPhone) > 0) { |
130 |
// You know have the number so now query it like this |
131 |
Cursor phones = getContentResolver().query( |
132 |
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, |
133 |
null, |
134 |
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, |
135 |
null, null); |
136 |
while (phones.moveToNext()) { |
137 |
int phoneIdx = phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER); |
138 |
String phoneNumber = phones.getString( phoneIdx ); |
139 |
Log.v("phone", phoneNumber); |
140 |
|
141 |
} |
142 |
phones.close(); |
143 |
} |
144 |
|
145 |
} |
146 |
cursor.close(); |
147 |
*/ |
148 |
|
149 |
} |
150 |
|
151 |
OnClickListener selectListener = new OnClickListener() { |
152 |
@Override |
153 |
public void onClick(View v) { |
154 |
lookup(); |
155 |
} |
156 |
}; |
157 |
|
158 |
|
159 |
OnClickListener sendListener = new OnClickListener() { |
160 |
@Override |
161 |
public void onClick(View v) { |
162 |
SendWorker worker = new SendWorker(); |
163 |
worker.execute(); |
164 |
} |
165 |
}; |
166 |
|
167 |
int getCount() { |
168 |
EditText countView = (EditText) findViewById(R.id.count); |
169 |
String strCount = countView.getText().toString(); |
170 |
|
171 |
int count = Integer.parseInt(strCount); |
172 |
|
173 |
return Math.min(count, MAXCOUNT); //limit to MAXCOUNT |
174 |
} |
175 |
|
176 |
int getInterval() { |
177 |
EditText intView = (EditText) findViewById(R.id.interval); |
178 |
String strInt = intView.getText().toString(); |
179 |
|
180 |
return Integer.parseInt(strInt); |
181 |
} |
182 |
|
183 |
String getFrom() { |
184 |
EditText origin = (EditText) findViewById(R.id.origin); |
185 |
return origin.getText().toString().trim(); |
186 |
} |
187 |
|
188 |
String getTo() { |
189 |
EditText destination = (EditText) findViewById(R.id.destination); |
190 |
String to = destination.getText().toString().trim(); |
191 |
|
192 |
to = to.replace("+", ""); |
193 |
if (to.length() < 10) { |
194 |
to = "45" + to; |
195 |
} |
196 |
return to; |
197 |
} |
198 |
|
199 |
String getMessage() { |
200 |
EditText message = (EditText) findViewById(R.id.message); |
201 |
return message.getText().toString().trim(); |
202 |
} |
203 |
|
204 |
boolean sendInternal() { |
205 |
int count = getCount(); |
206 |
String to = getTo(); |
207 |
|
208 |
SmsManager sms = SmsManager.getDefault(); |
209 |
ArrayList<String> parts = sms.divideMessage(getMessage() ); |
210 |
|
211 |
for (int i=0; i<count; i++) { |
212 |
sms.sendMultipartTextMessage(to, null, parts, null, null); |
213 |
sleep(5000); //give the phone some time to send the message |
214 |
} |
215 |
|
216 |
return true; |
217 |
} |
218 |
|
219 |
void sleep(int s) { |
220 |
try { |
221 |
Thread.sleep(s); |
222 |
} catch (InterruptedException e) { |
223 |
Log.e("SmsSend", "Sleep interrupted", e); |
224 |
} |
225 |
} |
226 |
|
227 |
boolean isNumeric(String str) { |
228 |
for (int i=0; i<str.length(); i++) { |
229 |
char c = str.charAt(i); |
230 |
if (! Character.isDigit(c) ) |
231 |
return false; |
232 |
} |
233 |
|
234 |
return true; |
235 |
} |
236 |
|
237 |
|
238 |
|
239 |
boolean sendCoolSms() { |
240 |
SharedPreferences prefs = this.getSharedPreferences("SmsSend", MODE_PRIVATE); |
241 |
String user = prefs.getString("cooluser", ""); |
242 |
String pass = prefs.getString("coolpass", ""); |
243 |
|
244 |
if (user.equals("") || pass.equals("")) { |
245 |
Toast.makeText(this, "No username or password set!", Toast.LENGTH_LONG).show(); |
246 |
return false; |
247 |
} |
248 |
|
249 |
if ( isNumeric(getFrom()) ) { |
250 |
Toast.makeText(this, "Can not send when 'from' equals a number\nmust add letters", Toast.LENGTH_LONG).show(); |
251 |
return false; |
252 |
} |
253 |
|
254 |
//"http://sms.coolsmsc.dk:8080/?username=%s&password=%s&to=%s&from=SMS+HTTP+GW&message=hello+world" |
255 |
|
256 |
String url = "http://sms.coolsmsc.dk:8080/"; |
257 |
|
258 |
Map<String,String> params = new HashMap<String,String>(); |
259 |
params.put("username", user); |
260 |
params.put("password", pass); |
261 |
params.put("to", getTo()); |
262 |
params.put("from", getFrom()); |
263 |
params.put("message", getMessage() ); |
264 |
params.put("resulttype", "urlencoded" ); |
265 |
params.put("lang", "lang" ); |
266 |
|
267 |
String paramStr = HttpUtil.encodeParams(params); |
268 |
|
269 |
Log.v("SmsSend", paramStr); |
270 |
|
271 |
|
272 |
boolean success = false; |
273 |
String msg = ""; |
274 |
try { |
275 |
byte bytes[] = HttpUtil.postContent(url, paramStr, 2500 ); |
276 |
String res = new String(bytes); |
277 |
Map<String,String> vals = HttpUtil.decodeParams(res); |
278 |
|
279 |
success = vals.get("status").equals("success"); |
280 |
msg = vals.get("result"); |
281 |
|
282 |
} catch (Exception e) { |
283 |
Log.e("SmsSend", "sendCoolSms", e); |
284 |
success = false; |
285 |
} |
286 |
|
287 |
return success; |
288 |
} |
289 |
|
290 |
boolean sendSmsDaemon() { |
291 |
|
292 |
int count = getCount(); |
293 |
int interval = getInterval(); |
294 |
String to = getTo(); |
295 |
|
296 |
String msg = "spam " + to + " " + count + " " + interval + " " + getMessage(); |
297 |
|
298 |
SmsManager sms = SmsManager.getDefault(); |
299 |
ArrayList<String> parts = sms.divideMessage(msg); |
300 |
|
301 |
|
302 |
sms.sendMultipartTextMessage(SMSDAEMON, null, parts, null, null); |
303 |
|
304 |
return true; |
305 |
} |
306 |
|
307 |
|
308 |
|
309 |
|
310 |
OnItemSelectedListener methodSelected = new OnItemSelectedListener() { |
311 |
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { |
312 |
switch (pos) { |
313 |
case 0: |
314 |
findViewById(R.id.interval).setEnabled(false); |
315 |
findViewById(R.id.origin).setEnabled(false); |
316 |
findViewById(R.id.count).setEnabled(true); |
317 |
break; |
318 |
case 1: |
319 |
findViewById(R.id.interval).setEnabled(false); |
320 |
findViewById(R.id.origin).setEnabled(true); |
321 |
findViewById(R.id.count).setEnabled(false); |
322 |
break; |
323 |
case 2: |
324 |
findViewById(R.id.interval).setEnabled(true); |
325 |
findViewById(R.id.origin).setEnabled(false); |
326 |
findViewById(R.id.count).setEnabled(true); |
327 |
break; |
328 |
} |
329 |
} |
330 |
|
331 |
@Override |
332 |
public void onNothingSelected(AdapterView<?> arg0) { |
333 |
|
334 |
} |
335 |
}; |
336 |
|
337 |
class SendWorker extends AsyncTask<Void, Void, Void> { |
338 |
|
339 |
boolean ret; |
340 |
ProgressDialog dlg; |
341 |
|
342 |
@Override |
343 |
protected void onPreExecute() { |
344 |
super.onPreExecute(); |
345 |
|
346 |
dlg = new ProgressDialog(SmsSend.this); |
347 |
dlg.setMessage( "Sending" ); |
348 |
dlg.setCancelable(true); |
349 |
dlg.show(); |
350 |
|
351 |
ret = false; |
352 |
} |
353 |
|
354 |
@Override |
355 |
protected Void doInBackground(Void... params) { |
356 |
Spinner method = (Spinner) findViewById(R.id.method); |
357 |
int sel = method.getSelectedItemPosition(); |
358 |
|
359 |
|
360 |
switch (sel) { |
361 |
case 0: |
362 |
ret = sendInternal(); |
363 |
break; |
364 |
case 1: |
365 |
ret = sendCoolSms(); |
366 |
break; |
367 |
case 2: |
368 |
ret = sendSmsDaemon(); |
369 |
break; |
370 |
|
371 |
} |
372 |
|
373 |
return null; |
374 |
} |
375 |
|
376 |
@Override |
377 |
protected void onPostExecute(Void result) { |
378 |
dlg.dismiss(); |
379 |
dlg = null; |
380 |
|
381 |
String msg = "Done!"; |
382 |
if (ret == false) |
383 |
msg = "failed!"; |
384 |
|
385 |
Toast.makeText(SmsSend.this, msg, Toast.LENGTH_LONG).show(); |
386 |
} |
387 |
|
388 |
} |
389 |
|
390 |
} |