1 |
package dk.thoerup.smssend; |
package dk.thoerup.smssend; |
2 |
|
|
3 |
|
import java.io.ByteArrayOutputStream; |
4 |
|
import java.io.IOException; |
5 |
|
import java.io.InputStream; |
6 |
|
import java.io.OutputStream; |
7 |
|
import java.net.HttpURLConnection; |
8 |
import java.net.URL; |
import java.net.URL; |
9 |
import java.net.URLConnection; |
import java.net.URLConnection; |
10 |
|
import java.net.URLDecoder; |
11 |
|
import java.net.URLEncoder; |
12 |
import java.util.ArrayList; |
import java.util.ArrayList; |
13 |
|
|
|
import android.R.bool; |
|
14 |
import android.app.Activity; |
import android.app.Activity; |
15 |
|
import android.app.ProgressDialog; |
16 |
import android.content.Intent; |
import android.content.Intent; |
17 |
import android.content.SharedPreferences; |
import android.content.SharedPreferences; |
18 |
|
import android.os.AsyncTask; |
19 |
import android.os.Bundle; |
import android.os.Bundle; |
20 |
import android.provider.ContactsContract; |
import android.provider.ContactsContract; |
21 |
import android.telephony.SmsManager; |
import android.telephony.SmsManager; |
39 |
final String SMSDAEMON = "42407617"; |
final String SMSDAEMON = "42407617"; |
40 |
|
|
41 |
final int OPTIONS_SETTINGS = 1000; |
final int OPTIONS_SETTINGS = 1000; |
42 |
|
|
43 |
|
final int LOOKUP_PHONENR = 2000; |
44 |
|
|
45 |
|
|
46 |
@Override |
@Override |
47 |
public void onCreate(Bundle savedInstanceState) { |
public void onCreate(Bundle savedInstanceState) { |
91 |
return retval; |
return retval; |
92 |
|
|
93 |
} |
} |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
void lookup() { |
@Override |
98 |
/* |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
99 |
Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI); |
super.onActivityResult(requestCode, resultCode, data); |
100 |
|
|
101 |
startActivityForResult(intent, 1); |
switch(requestCode) { |
102 |
*/ |
case LOOKUP_PHONENR: |
103 |
|
if (resultCode == Activity.RESULT_OK) { |
104 |
|
String phone = data.getStringExtra("phone"); |
105 |
|
EditText dest = (EditText) findViewById(R.id.destination); |
106 |
|
dest.setText(phone); |
107 |
|
} |
108 |
|
break; |
109 |
|
} |
110 |
|
} |
111 |
|
|
112 |
|
void lookup() { |
113 |
|
|
114 |
Intent intent = new Intent(Intent.ACTION_PICK); |
Intent intent = new Intent(Intent.ACTION_PICK); |
115 |
intent.setType(ContactsContract.Contacts.CONTENT_TYPE); |
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); |
|
|
|
116 |
|
|
117 |
startActivityForResult(intent, 1); |
startActivityForResult(intent, LOOKUP_PHONENR); |
118 |
|
|
119 |
|
|
120 |
|
|
165 |
OnClickListener sendListener = new OnClickListener() { |
OnClickListener sendListener = new OnClickListener() { |
166 |
@Override |
@Override |
167 |
public void onClick(View v) { |
public void onClick(View v) { |
168 |
Spinner method = (Spinner) findViewById(R.id.method); |
SendWorker worker = new SendWorker(); |
169 |
int sel = method.getSelectedItemPosition(); |
worker.execute(); |
|
|
|
|
boolean ret = false; |
|
|
|
|
|
switch (sel) { |
|
|
case 0: |
|
|
ret = sendInternal(); |
|
|
break; |
|
|
case 1: |
|
|
ret = sendCoolSms(); |
|
|
break; |
|
|
case 2: |
|
|
ret = sendSmsDaemon(); |
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
String msg = "Done!"; |
|
|
if (ret == false) |
|
|
msg = "failed!"; |
|
|
|
|
|
Toast.makeText(SmsSend.this, msg, Toast.LENGTH_LONG).show(); |
|
|
|
|
170 |
} |
} |
171 |
}; |
}; |
172 |
|
|
216 |
|
|
217 |
for (int i=0; i<count; i++) { |
for (int i=0; i<count; i++) { |
218 |
sms.sendMultipartTextMessage(to, null, parts, null, null); |
sms.sendMultipartTextMessage(to, null, parts, null, null); |
219 |
|
sleep(5000); //give the phone some time to send the message |
220 |
} |
} |
221 |
|
|
222 |
return true; |
return true; |
223 |
} |
} |
224 |
|
|
225 |
|
void sleep(int s) { |
226 |
|
try { |
227 |
|
Thread.sleep(s); |
228 |
|
} catch (InterruptedException e) { |
229 |
|
Log.e("SmsSend", "Sleep interrupted", e); |
230 |
|
} |
231 |
|
} |
232 |
|
|
233 |
boolean isNumeric(String str) { |
boolean isNumeric(String str) { |
234 |
for (int i=0; i<str.length(); i++) { |
for (int i=0; i<str.length(); i++) { |
235 |
char c = str.charAt(i); |
char c = str.charAt(i); |
239 |
|
|
240 |
return true; |
return true; |
241 |
} |
} |
242 |
|
|
243 |
|
String encode(String url) { |
244 |
|
try { |
245 |
|
return URLEncoder.encode(url, "UTF-8"); |
246 |
|
} catch (Exception e) { |
247 |
|
return url; |
248 |
|
} |
249 |
|
} |
250 |
|
|
251 |
boolean sendCoolSms() { |
boolean sendCoolSms() { |
252 |
SharedPreferences prefs = this.getSharedPreferences("SmsSend", MODE_PRIVATE); |
SharedPreferences prefs = this.getSharedPreferences("SmsSend", MODE_PRIVATE); |
264 |
} |
} |
265 |
|
|
266 |
//"http://sms.coolsmsc.dk:8080/?username=%s&password=%s&to=%s&from=SMS+HTTP+GW&message=hello+world" |
//"http://sms.coolsmsc.dk:8080/?username=%s&password=%s&to=%s&from=SMS+HTTP+GW&message=hello+world" |
267 |
|
|
268 |
|
String url = "http://sms.coolsmsc.dk:8080/"; |
269 |
|
|
270 |
StringBuilder sb = new StringBuilder(); |
StringBuilder sb = new StringBuilder(); |
271 |
sb.append("http://sms.coolsmsc.dk:8080/"); |
sb.append("username=").append(user); |
|
sb.append("?username=").append(user); |
|
272 |
sb.append("&password=").append(pass); |
sb.append("&password=").append(pass); |
273 |
sb.append("&to=").append( getTo() ); |
sb.append("&to=").append( getTo() ); |
274 |
sb.append("&from=").append( getFrom() ); |
sb.append("&from=").append( encode(getFrom()) ); |
275 |
sb.append("&message=").append( getMessage() ); |
sb.append("&message=").append( encode(getMessage()) ); |
276 |
|
sb.append("&resulttype=urlencoded"); |
277 |
|
sb.append("&lang=en"); |
278 |
|
|
279 |
Log.v("SmsSend", sb.toString()); |
Log.v("SmsSend", sb.toString()); |
280 |
|
|
281 |
|
|
282 |
|
boolean success = false; |
283 |
|
String msg = ""; |
284 |
try { |
try { |
285 |
URL u = new URL( sb.toString() ); |
byte bytes[] = postContent(url, sb.toString(), 2500 ); |
286 |
URLConnection conn = u.openConnection(); |
String res = new String(bytes); |
287 |
conn.getInputStream(); |
String parts[] = res.split("&"); |
288 |
|
|
289 |
|
for (String part : parts) { |
290 |
|
String pair[] = part.split("="); |
291 |
|
if (pair[0].equals("status")) { |
292 |
|
if (pair[1].equals("success")) { |
293 |
|
success = true; |
294 |
|
} else { |
295 |
|
success = false; |
296 |
|
} |
297 |
|
} |
298 |
|
|
299 |
|
if (pair[0].equals("result")) { |
300 |
|
msg = URLDecoder.decode(pair[1]); |
301 |
|
} |
302 |
|
} |
303 |
|
|
304 |
|
|
305 |
|
|
306 |
} catch (Exception e) { |
} catch (Exception e) { |
307 |
Log.e("SmsSend", "sendCoolSms", e); |
Log.e("SmsSend", "sendCoolSms", e); |
308 |
return false; |
success = false; |
309 |
} |
} |
310 |
|
|
311 |
return true; |
return success; |
312 |
} |
} |
313 |
|
|
314 |
boolean sendSmsDaemon() { |
boolean sendSmsDaemon() { |
327 |
|
|
328 |
return true; |
return true; |
329 |
} |
} |
330 |
|
|
331 |
|
|
332 |
|
public static byte[] getContent(String uri, int timeout) throws IOException { |
333 |
|
byte buffer[] = new byte[256]; |
334 |
|
|
335 |
|
ByteArrayOutputStream baos = new ByteArrayOutputStream(32768); //start buffer size - instead of default 32bytes |
336 |
|
|
337 |
|
URL url = new URL(uri); |
338 |
|
URLConnection connection = url.openConnection(); |
339 |
|
HttpURLConnection c; |
340 |
|
connection.setConnectTimeout(timeout); |
341 |
|
InputStream is = connection.getInputStream(); |
342 |
|
|
343 |
|
try { |
344 |
|
int count; |
345 |
|
while ( (count = is.read(buffer)) != -1) { |
346 |
|
baos.write(buffer, 0, count); |
347 |
|
} |
348 |
|
} finally { |
349 |
|
try { |
350 |
|
is.close(); |
351 |
|
baos.close(); |
352 |
|
} catch (Exception e) {} //never mind if close throws an exception |
353 |
|
} |
354 |
|
|
355 |
|
return baos.toByteArray(); |
356 |
|
} |
357 |
|
|
358 |
|
public static byte[] postContent(String uri, String data, int timeout) throws IOException { |
359 |
|
byte buffer[] = new byte[256]; |
360 |
|
|
361 |
|
ByteArrayOutputStream baos = new ByteArrayOutputStream(32768); //start buffer size - instead of default 32bytes |
362 |
|
|
363 |
|
URL url = new URL(uri); |
364 |
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
365 |
|
connection.setDoOutput(true); |
366 |
|
connection.setRequestMethod("POST"); |
367 |
|
connection.setConnectTimeout(timeout); |
368 |
|
|
369 |
|
OutputStream os = connection.getOutputStream(); |
370 |
|
os.write( data.getBytes() ); |
371 |
|
|
372 |
|
InputStream is = connection.getInputStream(); |
373 |
|
|
374 |
|
try { |
375 |
|
int count; |
376 |
|
while ( (count = is.read(buffer)) != -1) { |
377 |
|
baos.write(buffer, 0, count); |
378 |
|
} |
379 |
|
} finally { |
380 |
|
try { |
381 |
|
is.close(); |
382 |
|
baos.close(); |
383 |
|
} catch (Exception e) {} //never mind if close throws an exception |
384 |
|
} |
385 |
|
|
386 |
|
return baos.toByteArray(); |
387 |
|
} |
388 |
|
|
389 |
OnItemSelectedListener methodSelected = new OnItemSelectedListener() { |
OnItemSelectedListener methodSelected = new OnItemSelectedListener() { |
390 |
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { |
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { |
412 |
|
|
413 |
} |
} |
414 |
}; |
}; |
415 |
|
|
416 |
|
class SendWorker extends AsyncTask<Void, Void, Void> { |
417 |
|
|
418 |
|
boolean ret; |
419 |
|
ProgressDialog dlg; |
420 |
|
|
421 |
|
@Override |
422 |
|
protected void onPreExecute() { |
423 |
|
super.onPreExecute(); |
424 |
|
|
425 |
|
dlg = new ProgressDialog(SmsSend.this); |
426 |
|
dlg.setMessage( "Sending" ); |
427 |
|
dlg.setCancelable(true); |
428 |
|
dlg.show(); |
429 |
|
|
430 |
|
ret = false; |
431 |
|
} |
432 |
|
|
433 |
|
@Override |
434 |
|
protected Void doInBackground(Void... params) { |
435 |
|
Spinner method = (Spinner) findViewById(R.id.method); |
436 |
|
int sel = method.getSelectedItemPosition(); |
437 |
|
|
438 |
|
|
439 |
|
switch (sel) { |
440 |
|
case 0: |
441 |
|
ret = sendInternal(); |
442 |
|
break; |
443 |
|
case 1: |
444 |
|
ret = sendCoolSms(); |
445 |
|
break; |
446 |
|
case 2: |
447 |
|
ret = sendSmsDaemon(); |
448 |
|
break; |
449 |
|
|
450 |
|
} |
451 |
|
|
452 |
|
return null; |
453 |
|
} |
454 |
|
|
455 |
|
@Override |
456 |
|
protected void onPostExecute(Void result) { |
457 |
|
dlg.dismiss(); |
458 |
|
dlg = null; |
459 |
|
|
460 |
|
String msg = "Done!"; |
461 |
|
if (ret == false) |
462 |
|
msg = "failed!"; |
463 |
|
|
464 |
|
Toast.makeText(SmsSend.this, msg, Toast.LENGTH_LONG).show(); |
465 |
|
} |
466 |
|
|
467 |
|
} |
468 |
|
|
|
} |
|
469 |
|
} |