1 |
package dk.thoerup.smssend; |
2 |
|
3 |
import java.net.URL; |
4 |
import java.net.URLConnection; |
5 |
import java.net.URLEncoder; |
6 |
import java.util.ArrayList; |
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 |
String encode(String url) { |
238 |
try { |
239 |
return URLEncoder.encode(url, "UTF-8"); |
240 |
} catch (Exception e) { |
241 |
return url; |
242 |
} |
243 |
} |
244 |
|
245 |
boolean sendCoolSms() { |
246 |
SharedPreferences prefs = this.getSharedPreferences("SmsSend", MODE_PRIVATE); |
247 |
String user = prefs.getString("cooluser", ""); |
248 |
String pass = prefs.getString("coolpass", ""); |
249 |
|
250 |
if (user.equals("") || pass.equals("")) { |
251 |
Toast.makeText(this, "No username or password set!", Toast.LENGTH_LONG).show(); |
252 |
return false; |
253 |
} |
254 |
|
255 |
if ( isNumeric(getFrom()) ) { |
256 |
Toast.makeText(this, "Can not send when 'from' equals a number\nmust add letters", Toast.LENGTH_LONG).show(); |
257 |
return false; |
258 |
} |
259 |
|
260 |
//"http://sms.coolsmsc.dk:8080/?username=%s&password=%s&to=%s&from=SMS+HTTP+GW&message=hello+world" |
261 |
StringBuilder sb = new StringBuilder(); |
262 |
sb.append("http://sms.coolsmsc.dk:8080/"); |
263 |
sb.append("?username=").append(user); |
264 |
sb.append("&password=").append(pass); |
265 |
sb.append("&to=").append( getTo() ); |
266 |
sb.append("&from=").append( encode(getFrom()) ); |
267 |
sb.append("&message=").append( encode(getMessage()) ); |
268 |
|
269 |
Log.v("SmsSend", sb.toString()); |
270 |
|
271 |
try { |
272 |
URL u = new URL( sb.toString() ); |
273 |
URLConnection conn = u.openConnection(); |
274 |
conn.getInputStream(); |
275 |
|
276 |
|
277 |
|
278 |
} catch (Exception e) { |
279 |
Log.e("SmsSend", "sendCoolSms", e); |
280 |
return false; |
281 |
} |
282 |
|
283 |
return true; |
284 |
} |
285 |
|
286 |
boolean sendSmsDaemon() { |
287 |
|
288 |
int count = getCount(); |
289 |
int interval = getInterval(); |
290 |
String to = getTo(); |
291 |
|
292 |
String msg = "spam " + to + " " + count + " " + interval + " " + getMessage(); |
293 |
|
294 |
SmsManager sms = SmsManager.getDefault(); |
295 |
ArrayList<String> parts = sms.divideMessage(msg); |
296 |
|
297 |
|
298 |
sms.sendMultipartTextMessage(SMSDAEMON, null, parts, null, null); |
299 |
|
300 |
return true; |
301 |
} |
302 |
|
303 |
OnItemSelectedListener methodSelected = new OnItemSelectedListener() { |
304 |
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { |
305 |
switch (pos) { |
306 |
case 0: |
307 |
findViewById(R.id.interval).setEnabled(false); |
308 |
findViewById(R.id.origin).setEnabled(false); |
309 |
findViewById(R.id.count).setEnabled(true); |
310 |
break; |
311 |
case 1: |
312 |
findViewById(R.id.interval).setEnabled(false); |
313 |
findViewById(R.id.origin).setEnabled(true); |
314 |
findViewById(R.id.count).setEnabled(false); |
315 |
break; |
316 |
case 2: |
317 |
findViewById(R.id.interval).setEnabled(true); |
318 |
findViewById(R.id.origin).setEnabled(false); |
319 |
findViewById(R.id.count).setEnabled(true); |
320 |
break; |
321 |
} |
322 |
} |
323 |
|
324 |
@Override |
325 |
public void onNothingSelected(AdapterView<?> arg0) { |
326 |
|
327 |
} |
328 |
}; |
329 |
|
330 |
class SendWorker extends AsyncTask<Void, Void, Void> { |
331 |
|
332 |
boolean ret; |
333 |
ProgressDialog dlg; |
334 |
|
335 |
@Override |
336 |
protected void onPreExecute() { |
337 |
super.onPreExecute(); |
338 |
|
339 |
dlg = new ProgressDialog(SmsSend.this); |
340 |
dlg.setMessage( "Sending" ); |
341 |
dlg.setCancelable(true); |
342 |
dlg.show(); |
343 |
|
344 |
ret = false; |
345 |
} |
346 |
|
347 |
@Override |
348 |
protected Void doInBackground(Void... params) { |
349 |
Spinner method = (Spinner) findViewById(R.id.method); |
350 |
int sel = method.getSelectedItemPosition(); |
351 |
|
352 |
|
353 |
switch (sel) { |
354 |
case 0: |
355 |
ret = sendInternal(); |
356 |
break; |
357 |
case 1: |
358 |
ret = sendCoolSms(); |
359 |
break; |
360 |
case 2: |
361 |
ret = sendSmsDaemon(); |
362 |
break; |
363 |
|
364 |
} |
365 |
|
366 |
return null; |
367 |
} |
368 |
|
369 |
@Override |
370 |
protected void onPostExecute(Void result) { |
371 |
dlg.dismiss(); |
372 |
dlg = null; |
373 |
|
374 |
String msg = "Done!"; |
375 |
if (ret == false) |
376 |
msg = "failed!"; |
377 |
|
378 |
Toast.makeText(SmsSend.this, msg, Toast.LENGTH_LONG).show(); |
379 |
} |
380 |
|
381 |
} |
382 |
|
383 |
} |