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