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.Button; |
23 |
import android.widget.EditText; |
24 |
import android.widget.Spinner; |
25 |
import android.widget.Toast; |
26 |
import android.widget.AdapterView.OnItemSelectedListener; |
27 |
import dk.thoerup.androidutils.HttpUtil; |
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 |
String respMessage; |
40 |
|
41 |
|
42 |
@Override |
43 |
public void onCreate(Bundle savedInstanceState) { |
44 |
super.onCreate(savedInstanceState); |
45 |
setContentView(R.layout.main); |
46 |
|
47 |
Button select = (Button) findViewById(R.id.selectNumber); |
48 |
select.setOnClickListener( selectListener ); |
49 |
|
50 |
Button send = (Button) findViewById(R.id.send); |
51 |
send.setOnClickListener(sendListener); |
52 |
|
53 |
|
54 |
|
55 |
Spinner method = (Spinner) findViewById(R.id.method); |
56 |
method.setOnItemSelectedListener(methodSelected); |
57 |
method.setSelection(0); |
58 |
|
59 |
} |
60 |
|
61 |
@Override |
62 |
public boolean onCreateOptionsMenu(Menu menu) { |
63 |
MenuItem item; |
64 |
|
65 |
item = menu.add(0, OPTIONS_SETTINGS, 0, "Settings"); |
66 |
item.setIcon(android.R.drawable.ic_menu_edit); |
67 |
|
68 |
return true; |
69 |
} |
70 |
|
71 |
@Override |
72 |
public boolean onOptionsItemSelected(MenuItem item) { |
73 |
boolean retval = true; |
74 |
|
75 |
//TODO: Cleanup |
76 |
switch (item.getItemId()) { |
77 |
case OPTIONS_SETTINGS: |
78 |
Intent i = new Intent(this, dk.thoerup.smssend.SmsConfig.class); |
79 |
startActivity(i); |
80 |
break; |
81 |
default: |
82 |
retval = super.onOptionsItemSelected(item); |
83 |
} |
84 |
return retval; |
85 |
|
86 |
} |
87 |
|
88 |
|
89 |
|
90 |
@Override |
91 |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
92 |
super.onActivityResult(requestCode, resultCode, data); |
93 |
|
94 |
switch(requestCode) { |
95 |
case LOOKUP_PHONENR: |
96 |
if (resultCode == Activity.RESULT_OK) { |
97 |
String phone = data.getStringExtra("phone"); |
98 |
EditText dest = (EditText) findViewById(R.id.destination); |
99 |
dest.setText(phone); |
100 |
} |
101 |
break; |
102 |
} |
103 |
} |
104 |
|
105 |
void lookup() { |
106 |
|
107 |
Intent intent = new Intent(Intent.ACTION_PICK); |
108 |
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); |
109 |
|
110 |
startActivityForResult(intent, LOOKUP_PHONENR); |
111 |
|
112 |
|
113 |
|
114 |
/* |
115 |
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, |
116 |
null, null, null, null); |
117 |
while (cursor.moveToNext()) { |
118 |
String contactId = cursor.getString(cursor.getColumnIndex( |
119 |
ContactsContract.Contacts._ID)); |
120 |
String hasPhone = cursor.getString(cursor.getColumnIndex( |
121 |
ContactsContract.Contacts.HAS_PHONE_NUMBER)); |
122 |
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); |
123 |
|
124 |
Log.v("contact", name + " : " + hasPhone); |
125 |
|
126 |
|
127 |
|
128 |
if (Integer.parseInt(hasPhone) > 0) { |
129 |
// You know have the number so now query it like this |
130 |
Cursor phones = getContentResolver().query( |
131 |
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, |
132 |
null, |
133 |
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, |
134 |
null, null); |
135 |
while (phones.moveToNext()) { |
136 |
int phoneIdx = phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER); |
137 |
String phoneNumber = phones.getString( phoneIdx ); |
138 |
Log.v("phone", phoneNumber); |
139 |
|
140 |
} |
141 |
phones.close(); |
142 |
} |
143 |
|
144 |
} |
145 |
cursor.close(); |
146 |
*/ |
147 |
|
148 |
} |
149 |
|
150 |
OnClickListener selectListener = new OnClickListener() { |
151 |
@Override |
152 |
public void onClick(View v) { |
153 |
lookup(); |
154 |
} |
155 |
}; |
156 |
|
157 |
|
158 |
OnClickListener sendListener = new OnClickListener() { |
159 |
@Override |
160 |
public void onClick(View v) { |
161 |
SendWorker worker = new SendWorker(); |
162 |
worker.execute(); |
163 |
} |
164 |
}; |
165 |
|
166 |
int getCount() { |
167 |
EditText countView = (EditText) findViewById(R.id.count); |
168 |
String strCount = countView.getText().toString(); |
169 |
|
170 |
int count = Integer.parseInt(strCount); |
171 |
|
172 |
return Math.min(count, MAXCOUNT); //limit to MAXCOUNT |
173 |
} |
174 |
|
175 |
int getInterval() { |
176 |
EditText intView = (EditText) findViewById(R.id.interval); |
177 |
String strInt = intView.getText().toString(); |
178 |
|
179 |
return Integer.parseInt(strInt); |
180 |
} |
181 |
|
182 |
String getFrom() { |
183 |
EditText origin = (EditText) findViewById(R.id.origin); |
184 |
return origin.getText().toString().trim(); |
185 |
} |
186 |
|
187 |
String getTo() { |
188 |
EditText destination = (EditText) findViewById(R.id.destination); |
189 |
String to = destination.getText().toString().trim(); |
190 |
|
191 |
to = to.replace("+", ""); |
192 |
if (to.length() < 10) { |
193 |
to = "45" + to; |
194 |
} |
195 |
return to; |
196 |
} |
197 |
|
198 |
String getMessage() { |
199 |
EditText message = (EditText) findViewById(R.id.message); |
200 |
return message.getText().toString().trim(); |
201 |
} |
202 |
|
203 |
boolean sendInternal() { |
204 |
int count = getCount(); |
205 |
String to = getTo(); |
206 |
|
207 |
SmsManager sms = SmsManager.getDefault(); |
208 |
ArrayList<String> parts = sms.divideMessage(getMessage() ); |
209 |
|
210 |
for (int i=0; i<count; i++) { |
211 |
sms.sendMultipartTextMessage(to, null, parts, null, null); |
212 |
sleep(5000); //give the phone some time to send the message |
213 |
} |
214 |
|
215 |
return true; |
216 |
} |
217 |
|
218 |
void sleep(int s) { |
219 |
try { |
220 |
Thread.sleep(s); |
221 |
} catch (InterruptedException e) { |
222 |
Log.e("SmsSend", "Sleep interrupted", e); |
223 |
} |
224 |
} |
225 |
|
226 |
boolean isNumeric(String str) { |
227 |
for (int i=0; i<str.length(); i++) { |
228 |
char c = str.charAt(i); |
229 |
if (! Character.isDigit(c) ) |
230 |
return false; |
231 |
} |
232 |
|
233 |
return true; |
234 |
} |
235 |
|
236 |
|
237 |
|
238 |
boolean sendCoolSms() { |
239 |
SharedPreferences prefs = this.getSharedPreferences("SmsSend", MODE_PRIVATE); |
240 |
String user = prefs.getString("cooluser", ""); |
241 |
String pass = prefs.getString("coolpass", ""); |
242 |
|
243 |
if (user.equals("") || pass.equals("")) { |
244 |
respMessage = "No username or password set!"; |
245 |
return false; |
246 |
} |
247 |
|
248 |
if ( isNumeric(getFrom()) ) { |
249 |
respMessage = "Can not send when 'from' equals a number\nmust add letters"; |
250 |
return false; |
251 |
} |
252 |
|
253 |
//"http://sms.coolsmsc.dk:8080/?username=%s&password=%s&to=%s&from=SMS+HTTP+GW&message=hello+world" |
254 |
|
255 |
String url = "http://sms.coolsmsc.dk:8080/"; |
256 |
|
257 |
Map<String,String> params = new HashMap<String,String>(); |
258 |
params.put("username", user); |
259 |
params.put("password", pass); |
260 |
params.put("to", getTo()); |
261 |
params.put("from", getFrom()); |
262 |
params.put("message", getMessage() ); |
263 |
params.put("resulttype", "urlencoded" ); |
264 |
params.put("lang", "lang" ); |
265 |
|
266 |
String paramStr = HttpUtil.encodeParams(params); |
267 |
|
268 |
Log.v("SmsSend", paramStr); |
269 |
|
270 |
|
271 |
boolean success = false; |
272 |
|
273 |
try { |
274 |
byte bytes[] = HttpUtil.postContent(url, paramStr, 2500 ); |
275 |
String res = new String(bytes); |
276 |
Map<String,String> vals = HttpUtil.decodeParams(res); |
277 |
|
278 |
success = vals.get("status").equals("success"); |
279 |
respMessage = vals.get("result"); |
280 |
|
281 |
} catch (Exception e) { |
282 |
Log.e("SmsSend", "sendCoolSms", e); |
283 |
respMessage = "" + e.getMessage(); |
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 = respMessage; |
384 |
|
385 |
Toast.makeText(SmsSend.this, msg, Toast.LENGTH_LONG).show(); |
386 |
} |
387 |
|
388 |
} |
389 |
|
390 |
} |