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