Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static class ReadDbTableAllAsyncTask extends AsyncTask<ArrayList<String>, Void, ArrayList<String>>{
- private static final String TAG = "[MY_DEBUG]";
- private WeakReference<MainActivity> activityWeakReference;
- private SQLiteDatabase db1;
- private long milli_seconds = 0;
- private Calendar calendar = Calendar.getInstance();
- private DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy h:mm a");
- private String printable_date;
- private int i=0;
- private ArrayList<String> messages_list = new ArrayList();
- private Cursor cursor_read_from_table_all;
- private int progress_iterator;
- private boolean cursor_first;
- ReadDbTableAllAsyncTask(MainActivity activity, SQLiteDatabase db2){
- activityWeakReference = new WeakReference<MainActivity>(activity);
- db1 = db2;
- i=0;
- }
- @Override
- protected ArrayList<String> doInBackground(ArrayList<String>... arrayList) {
- db1.beginTransaction();
- String[] projection = {
- BaseColumns._ID,
- SpamBusterContract.TABLE_ALL.COLUMN_CORRES_INBOX_ID,
- SpamBusterContract.TABLE_ALL.COLUMN_SMS_BODY,
- SpamBusterContract.TABLE_ALL.COLUMN_SMS_ADDRESS,
- SpamBusterContract.TABLE_ALL.COLUMN_SMS_EPOCH_DATE
- };
- String selection = null;
- String[] selectionArgs = null;
- String sortOrder =
- SpamBusterContract.TABLE_ALL.COLUMN_SMS_EPOCH_DATE + " desc "; //latest one appears on top of array_adapter
- cursor_read_from_table_all = db1.query(
- SpamBusterContract.TABLE_ALL.TABLE_NAME, // The table to query
- projection, // The array of columns to return (pass null to get all)
- selection, // The columns for the WHERE clause
- selectionArgs, // The values for the WHERE clause
- null, // don't group the rows
- null, // don't filter by row groups
- sortOrder // The sort order
- );
- i = 0;
- cursor_first = cursor_read_from_table_all.moveToFirst();
- if (!cursor_first) {
- Log.d(TAG, "ReadDbTableAllAsyncTask: doInBackground(): TABLE_ALL is empty");
- }
- else {
- Log.d(TAG, "ReadDbTableAllAsyncTask doInBackground: interator i = " + i);
- int index_id = cursor_read_from_table_all.getColumnIndexOrThrow(SpamBusterContract.TABLE_ALL._ID);
- int index_corres_id = cursor_read_from_table_all.getColumnIndexOrThrow(SpamBusterContract.TABLE_ALL.COLUMN_CORRES_INBOX_ID);
- int index_sms_body = cursor_read_from_table_all.getColumnIndexOrThrow(SpamBusterContract.TABLE_ALL.COLUMN_SMS_BODY);
- int index_sms_address = cursor_read_from_table_all.getColumnIndexOrThrow(SpamBusterContract.TABLE_ALL.COLUMN_SMS_ADDRESS);
- int index_sms_epoch_date = cursor_read_from_table_all.getColumnIndexOrThrow(SpamBusterContract.TABLE_ALL.COLUMN_SMS_EPOCH_DATE);
- try {
- messages_list.clear();
- do {
- long itemId = cursor_read_from_table_all.getLong(index_id);
- String corress_inbox_id = cursor_read_from_table_all.getString(index_corres_id);
- String sms_body = cursor_read_from_table_all.getString(index_sms_body);
- String sms_address = cursor_read_from_table_all.getString(index_sms_address);
- String epoch_date = cursor_read_from_table_all.getString(index_sms_epoch_date);
- Log.d(TAG, " doInBackground() : itemId = " + itemId);
- Log.d(TAG, " doInBackground() : corress_inbox_id = " + corress_inbox_id);
- Log.d(TAG, " doInBackground() : sms_body = " + sms_body);
- Log.d(TAG, " doInBackground() : sms_address = " + sms_address);
- milli_seconds = Long.parseLong(epoch_date);
- calendar.setTimeInMillis(milli_seconds);
- printable_date = formatter.format(calendar.getTime());
- Log.d(TAG, " doInBackground() : epoch_date = " + epoch_date + " which is : " + printable_date);
- Log.d(TAG, " doInBackground() : ");
- String str = "ItemID = " + itemId + "\ncorress_inbox_id = " + corress_inbox_id + "\n SMS From: " + getContactName(activityWeakReference.get(), sms_address) + "\n Recieved at: " + printable_date + "\n" + sms_body;
- Log.d(TAG, "doInBackground: progress_iterator = " + progress_iterator);
- Log.d(TAG, "doInBackground: i = " + i);
- Log.d(TAG, "doInBackground: adding into message_list at index + " + progress_iterator);
- Log.d(TAG, "doInBackground: messages_list.add(" + progress_iterator + ", " + str + ")");
- messages_list.add(progress_iterator++, str);
- Log.d(TAG, "ReadDbTableAllAsyncTask doInBackground: incrementing iterator i to " + ++i);
- } while (cursor_read_from_table_all.moveToNext());
- }
- catch(Exception e){
- Log.d(TAG, "doInBackground: exception : " + e);
- }
- }
- cursor_read_from_table_all.close();
- return messages_list;
- }
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- MainActivity activity = activityWeakReference.get();
- if (activity == null || activity.isFinishing()) {
- return;
- }
- messages_list.clear();
- }
- @Override
- protected void onPostExecute(ArrayList<String> msg_list) {
- super.onPostExecute(msg_list);
- MainActivity activity = activityWeakReference.get();
- if (activity == null || activity.isFinishing()) {
- return;
- }
- int j=0;
- try {
- while (j < msg_list.size()) {
- activity.sms_adapter.insert(j, msg_list.get(j).toString());
- activity.sms_adapter.notifyDataSetChanged();
- j++;
- }
- }
- catch (Exception e){
- Log.d(TAG, "onPostExecute: exception : " + e);
- }
- Log.d(TAG, "onPostExecute: Finished reading TABLE_ALL");
- activity.messages.setAdapter(activity.sms_adapter);
- activity.messages.setLayoutManager(new LinearLayoutManager(activity));
- db1.startTransaction();
- db1.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement