· 8 years ago · Aug 01, 2018, 03:00 PM
1// HttpConnector.java
2
3import java.io.BufferedReader;
4import java.io.IOException;
5import java.io.InputStream;
6import java.io.InputStreamReader;
7import java.io.OutputStreamWriter;
8import java.net.HttpURLConnection;
9import java.net.URL;
10import java.net.UnknownHostException;
11
12import android.util.Log;
13
14public class HttpConnector {
15
16 private static final String tag = "Whozzat";
17
18 private String response ;
19 private HttpURLConnection connection;
20
21 public HttpConnector()
22 {
23 super();
24 connection =null;
25 }
26
27 // Request to web server using get method
28
29 public void HttpGetRequest(String url) throws UnknownHostException, IOException
30 {
31 connection = (HttpURLConnection) (new URL(url)).openConnection();
32 connection.setRequestMethod("GET");
33 if(connection.getResponseCode() == HttpURLConnection.HTTP_OK)
34 {
35 this.setResponse(getResponseString(connection.getInputStream()));
36 }
37 else
38 {
39 this.setResponse(connection.getResponseMessage());
40 }
41 }
42
43 // Request to web server using post method
44
45 public void HttpPostRequest(String url, String params) throws UnknownHostException, IOException
46 {
47 OutputStreamWriter request = null;
48
49 connection = (HttpURLConnection)(new URL(url)).openConnection();
50 connection.setDoOutput(true);
51 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
52 connection.setRequestMethod("POST");
53
54 request = new OutputStreamWriter(connection.getOutputStream());
55 request.write(params);
56 request.flush();
57 request.close();
58
59 if(connection.getResponseCode() == HttpURLConnection.HTTP_OK)
60 {
61 this.setResponse(getResponseString(connection.getInputStream()));
62 }
63 else
64 {
65 this.setResponse(connection.getResponseMessage());
66 }
67 }
68
69 // Converts the InputStream in to a simple readable string.
70
71 private String getResponseString(InputStream is)
72 {
73 String line = "";
74 BufferedReader br = null;
75 InputStreamReader isr = null;
76 StringBuilder sb = new StringBuilder();
77
78 try
79 {
80 isr = new InputStreamReader(is);
81 br = new BufferedReader(isr);
82 while ((line = br.readLine()) != null)
83 {
84 sb.append(line + "\n");
85 }
86 }
87 catch (IOException e)
88 {
89 Log.e(tag, e.getMessage());
90 }
91 return sb.toString().trim();
92 }
93
94 // Getters and Setters methods for server response
95
96 public String getResponse() {
97 return response;
98 }
99
100 public void setResponse(String response) {
101 this.response = response;
102 }
103}
104
105//DatabaseAdapter
106
107import android.content.ContentValues;
108import android.content.Context;
109import android.database.Cursor;
110import android.database.SQLException;
111import android.database.sqlite.SQLiteDatabase;
112import android.database.sqlite.SQLiteOpenHelper;
113import android.util.Log;
114
115public class DatabaseAdapter
116{
117 /*
118 * This class is used for the database operations related
119 * to the Groups of the user Contacts.
120 */
121
122 public static final String TAG = "Whozzat";
123
124 //private final Context context;
125
126 public static final String DB_NAME = "whozzat";
127 public static final String GROUPS_TABLE = "groups";
128 public static final String PERSONS_TABLE = "persons";
129 public static final int DB_VERSION = 1;
130
131
132 public static final String DB_TABLE_1 = "CREATE TABLE IF NOT EXISTS groups"
133 + "(_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,"
134 + "group_name TEXT NOT NULL);";
135
136 public static final String DB_TABLE_2 = "CREATE TABLE IF NOT EXISTS persons"
137 + "(_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,"
138 + "person_name TEXT NOT NULL ,"
139 + "mobile_number TEXT NOT NULL,"
140 + "person_group_id INTEGER NOT NULL CONSTRAINT person_group_id REFERENCES groups(_id) ON DELETE CASCADE);";
141
142 private static class DatabaseHelper extends SQLiteOpenHelper
143 {
144 public DatabaseHelper(Context context)
145 {
146 super(context, DB_NAME, null, DB_VERSION);
147 }
148
149 @Override
150 public void onCreate(SQLiteDatabase db)
151 {
152 db.execSQL(DB_TABLE_1); // Creates the group table in database.
153 db.execSQL(DB_TABLE_2); // Creates the persons table in database.
154 }
155
156 @Override
157 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
158 {
159 db.execSQL("DROP TABLE IF EXISTS groups, persons");
160 onCreate(db);
161 }
162
163 @Override
164 public void onOpen(SQLiteDatabase db) {
165 super.onOpen(db);
166
167 if(db.isReadOnly())
168 {
169 db.execSQL("PRAGMA foreign_keys=ON;");
170 }
171 }
172 }
173
174 private DatabaseHelper dbh;
175 private SQLiteDatabase db;
176
177 DatabaseAdapter(Context context)
178 {
179 dbh = new DatabaseHelper(context) ;
180 }
181
182 public DatabaseAdapter open() throws SQLException
183 {
184 db = dbh.getWritableDatabase();
185 return this;
186 }
187
188 public void close()
189 {
190 dbh.close();
191 }
192
193 public long addGroup(String groupName) {
194 ContentValues initialValues = new ContentValues();
195 initialValues.put("group_name", groupName);
196 return db.insert(GROUPS_TABLE, null, initialValues);
197 }
198
199 public Cursor getGroupNames() {
200 return db.query(GROUPS_TABLE, new String[] {"_id", "group_name"}, null,
201 null, null, null, null);
202 // Query will be like this : "SELECT group_name from whozzat.groups";
203 }
204
205 public boolean getDuplicateGroup(String group_name)
206 {
207 String columns [] =new String [] {"group_name"};
208 String selection = "group_name=?";
209 String selectedArgs [] = new String [] {group_name};
210 Cursor cur = db.query(GROUPS_TABLE, columns, selection, selectedArgs,null,null,null);
211 return cur.getCount() > 0;
212 }
213
214 // This will add the person details to the selected group by the user.
215 public long addPersonsToGroup(String groupName, String[] names,
216 String[] numbers) {
217
218 /*
219 * SQL query will be like this :
220 * SELECT _id from whozzat.groups WHERE group_name=?
221 */
222
223 Cursor cursor = db.query(GROUPS_TABLE, new String[] { "_id" },
224 "group_name=?", new String[] { groupName }, null, null, null);
225
226 cursor.moveToFirst();
227
228 String person_group_id = cursor.getString(cursor.getColumnIndexOrThrow("_id"));
229
230 ContentValues values = new ContentValues();
231 long result = -1;
232
233 for(int i = 0; i< names.length; i++)
234 {
235 values.put("person_name", names[i]);
236 values.put("mobile_number", numbers[i]);
237 values.put("person_group_id", person_group_id);
238 result = db.insert(PERSONS_TABLE, null, values);
239 }
240 return result;
241 }
242
243 public int deletePersonFromGroup(String[] personDetails)
244 {
245 /*
246 * SQL query will be like this :
247 * DELETE FROM whozzat.persons WHERE person_name=? AND mobile_number=?
248 */
249
250 String whereClause = "person_name=? AND mobile_number=?";
251 String [] whereArgs = personDetails;
252 return db.delete(PERSONS_TABLE, whereClause, whereArgs);
253 }
254
255 public int deleteGroup(String[] groupName)
256 {
257 /*
258 * SQL query will be like this :
259 * DELETE FROM whozzat.groups WHERE group_name=?
260 */
261
262 int result = 0;
263 Cursor cursor = db.query(GROUPS_TABLE, new String[] {"_id"}, "group_name=?", groupName,null, null, null);
264 cursor.moveToFirst();
265 String [] group_id = new String[] {cursor.getString(0)};
266
267 db.beginTransaction();
268 try {
269 result = db.delete(PERSONS_TABLE, "person_group_id=?", group_id);
270 result = db.delete(GROUPS_TABLE, "_id=?", group_id);
271 db.setTransactionSuccessful();
272 }
273 catch(Exception e) {
274 Log.e(TAG, e.getMessage());
275 }
276 finally {
277 db.endTransaction();
278 cursor.close();
279 }
280 return result;
281 }
282
283 public Cursor getGroupDetails(String group_name)
284 {
285 String sql = "SELECT person_name, mobile_number FROM persons WHERE persons.person_group_id IN (SELECT _id FROM groups WHERE group_name=?)";
286 return db.rawQuery(sql, new String [] {group_name});
287 }
288
289 public Cursor getGroupContacts(String group_id)
290 {
291 String sql = "SELECT person_name, mobile_number FROM persons WHERE person_group_id =?";
292 return db.rawQuery(sql, new String [] {group_id});
293 }
294}
295
296// XML Handler
297
298import org.xml.sax.Attributes;
299import org.xml.sax.SAXException;
300import org.xml.sax.helpers.DefaultHandler;
301
302import com.whozzat.util.DeliveryReportsList;
303
304import android.util.Log;
305
306
307@SuppressWarnings("unused")
308public class DeliveryReportsHandler extends DefaultHandler{
309
310 StringBuffer currentValue = new StringBuffer("");
311 private static final String tag="Whozzat";
312
313 /*
314 <whozzat>
315 <deliveryreport>
316 <name>Recipient Name</name>
317 <number>Recipient Number</number>
318 <senttime>Time Stamp</senttime>
319 <message>Message Text</message>
320 <status>Delivery Status</status>
321 <deliverytime>Time Stamp</deliverytime>
322 </deliveryreport>
323 </whozzat>
324 */
325
326 public static DeliveryReportsList deliveryReportsList = null;
327
328 public static DeliveryReportsList getDeliveryReportsList() {
329 return deliveryReportsList;
330 }
331
332 public static void setDeliveryReportsList(DeliveryReportsList deliveryReportsList) {
333 DeliveryReportsHandler.deliveryReportsList = deliveryReportsList;
334 }
335
336 @Override
337 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
338
339 super.startElement(uri, localName, qName, attributes);
340 currentValue.setLength(0);
341 }
342
343 @Override
344 public void startDocument() throws SAXException {
345 super.startDocument();
346
347 deliveryReportsList = new DeliveryReportsList();
348 }
349
350 @Override
351 public void endElement(String uri, String localName, String qName) throws SAXException {
352
353 super.endElement(uri, localName, qName);
354
355 if(localName.equalsIgnoreCase("name"))
356 {
357 deliveryReportsList.setName(currentValue.toString());
358 }
359 else if(localName.equalsIgnoreCase("number"))
360 {
361 deliveryReportsList.setNumber(currentValue.toString());
362 }
363 else if(localName.equalsIgnoreCase("senttime"))
364 {
365 deliveryReportsList.setSentTime(currentValue.toString());
366 }
367 else if(localName.equalsIgnoreCase("message"))
368 {
369 deliveryReportsList.setMessage(currentValue.toString());
370 }
371 else if(localName.equalsIgnoreCase("status"))
372 {
373 deliveryReportsList.setStatus(currentValue.toString());
374 }
375 else if(localName.equalsIgnoreCase("deliverytime"))
376 {
377 deliveryReportsList.setDeliveryTime(currentValue.toString());
378 }
379 }
380
381 @Override
382 public void characters(char[] ch, int start, int length) throws SAXException
383 {
384 super.characters(ch, start, length);
385 currentValue.append(new String(ch, start, length));
386 }
387}
388
389// Contacts Picker
390
391package com.whozzat;
392
393import java.util.ArrayList;
394
395import com.whozzat.util.AlertUtils;
396import com.whozzat.util.ContactsManager;
397
398import android.app.Activity;
399import android.app.ListActivity;
400import android.content.Context;
401import android.content.Intent;
402import android.database.Cursor;
403import android.graphics.PorterDuff.Mode;
404import android.os.Bundle;
405import android.provider.ContactsContract.CommonDataKinds.Phone;
406import android.util.Log;
407import android.view.LayoutInflater;
408import android.view.View;
409import android.view.View.OnClickListener;
410import android.view.ViewGroup;
411import android.widget.Button;
412import android.widget.CheckBox;
413import android.widget.EditText;
414import android.widget.SimpleCursorAdapter;
415import android.widget.TextView;
416
417@SuppressWarnings("unused")
418public class PickContacts extends ListActivity
419{
420 private static final String tag = "Whozzat";
421 private static final String MobileNumberList = "MobileNumberList";
422
423 private ArrayList<Boolean>itemChecked = new ArrayList<Boolean>();
424 private EditText mPersonName;
425
426 @Override
427 public void onCreate(Bundle savedInstanceState) {
428 super.onCreate(savedInstanceState);
429
430 this.setTitle("Select Numbers");
431
432 this.setContentView(R.layout.contacts_list);
433
434 String[] projection = new String[] { Phone._ID, Phone.DISPLAY_NAME,
435 Phone.TYPE, Phone.NUMBER };
436
437 String[] displayFields = new String[] { Phone.DISPLAY_NAME, Phone.TYPE,
438 Phone.NUMBER };
439 int[] displayViews = new int[] { R.id.name, R.id.contact_type,
440 R.id.number, R.id.bcheck };
441
442 String sortOrder = Phone.DISPLAY_NAME + " ASC";
443
444 Cursor cur = managedQuery(Phone.CONTENT_URI, projection, null, null,
445 sortOrder);
446 final ContactsListAdapter adapter = new ContactsListAdapter(this,
447 R.layout.contacts_list_items, cur, displayFields, displayViews);
448
449 this.setListAdapter(adapter);
450 this.getListView().setTextFilterEnabled(true);
451 Button addSelected = (Button) findViewById(R.id.btn_add_selected);
452 Button cancel = (Button) findViewById(R.id.btn_cancel);
453
454 addSelected.getBackground().setColorFilter(0xff79bf34, Mode.MULTIPLY);
455 cancel.getBackground().setColorFilter(0xff79bf34, Mode.MULTIPLY);
456 addSelected.setOnClickListener(new OnClickListener() {
457 public void onClick(View v) {
458 if(itemChecked.contains((Object) true))
459 {
460 Intent intent = new Intent();
461 Bundle bundle = new Bundle();
462 bundle.putStringArrayList(MobileNumberList, ContactsManager.getContactNumbers());
463 intent.putExtras(bundle);
464 setResult(Activity.RESULT_OK, intent);
465 finish();
466 }
467 else
468 {
469 AlertUtils.displayAlert(PickContacts.this, "Oops ! you haven't selected any contact.",0);
470 }
471 }
472 });
473 cancel.setOnClickListener(new OnClickListener() {
474 public void onClick(View v) {
475 finish();
476 }
477 });
478 }
479
480 public class ContactsListAdapter extends SimpleCursorAdapter {
481 private Cursor c;
482 private Context context;
483 private ArrayList<String> list = new ArrayList<String>();
484 private int[] colors = new int[] { 0xff3e3e3e, 0xff3e3e3e };
485
486 public ContactsListAdapter(Context context, int layout, Cursor c,
487 String[] from, int[] to) {
488 super(context, layout, c, from, to);
489 this.c = c;
490 this.context = context;
491
492 for(int i = 0; i< this.getCount(); i++)
493 {
494 itemChecked.add(i, false);
495 }
496 }
497
498 public View getView(final int pos, View inView, ViewGroup parent)
499 {
500 if (inView == null) {
501 LayoutInflater inflater = (LayoutInflater) context
502 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
503 inView = inflater.inflate(R.layout.contacts_list_items, null);
504 }
505
506 this.c.moveToPosition(pos);
507
508 int row = pos % colors.length;
509 inView.setBackgroundColor(colors[row]);
510
511 final CheckBox cBox = (CheckBox) inView.findViewById(R.id.bcheck);
512 TextView name = (TextView) inView.findViewById(R.id.name);
513 TextView number = (TextView) inView.findViewById(R.id.number);
514 TextView number_type = (TextView) inView.findViewById(R.id.contact_type);
515
516 String mName = this.c.getString(this.c .getColumnIndex(Phone.DISPLAY_NAME));
517 String mNumber = this.c.getString(this.c.getColumnIndex(Phone.NUMBER));
518
519 name.setText(mName);
520 number.setText(mNumber);
521
522 String type = this.c.getString(this.c.getColumnIndex(Phone.TYPE));
523 if (type.equals("1")) {
524 type = "Home";
525 } else if (type.equals("2")) {
526 type = "Mobile";
527 } else if (type.equals("3")) {
528 type = "Work";
529 } else {
530 type = "Other";
531 }
532 number_type.setText(type);
533
534 // Remove ,(Comma) Characters from Contacts Names.
535 if(mName.contains(","))
536 {
537 mName = mName.replace(",", "");
538 }
539
540 // Remove +(Plus) Character from Contacts Numbers.
541 if (mNumber.contains("+")) {
542 mNumber = mNumber.replace("+", "");
543 }
544
545 // Remove -(Dash) Character from Contacts Numbers.
546 if (mNumber.contains("-")) {
547 mNumber = mNumber.replace("-", "");
548 }
549 cBox.setTag(mName+",<"+mNumber+">");
550 cBox.setOnClickListener(new OnClickListener() {
551 public void onClick(View v) {
552
553 CheckBox cb = (CheckBox) v.findViewById(R.id.bcheck);
554
555 String number = cb.getTag().toString();
556 if(cb.isChecked())
557 {
558 itemChecked.set(pos, true);
559 list.add(number);
560 }
561 else
562 if(!cb.isChecked())
563 {
564 itemChecked.set(pos, false);
565 list.remove(number);
566 }
567 }
568 });
569 cBox.setChecked(itemChecked.get(pos));
570 ContactsManager.setContactNumbers(list);
571 return inView;
572 }
573 }
574}