/[projects]/android/SmsSend/src/dk/thoerup/smssend/SmsSend.java
ViewVC logotype

Contents of /android/SmsSend/src/dk/thoerup/smssend/SmsSend.java

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20