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