DEV'S

SMSReplier - V1.0 - Service - Intent Share - Phone - WhiteList - ReplyLog Addon - Part2

SMSService.java

package com.dev.cts.smsreplierv10;

import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.ContactsContract;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.telephony.TelephonyManager;

import com.dev.cts.utils.SmsDB;
import com.dev.cts.utils.SmsReplier;

import java.util.ArrayList;

public class SMSService extends Service {

    public static final int NOTIFICATION_ID = 111;
    private int count_id = 1;
    public static final String INCOMING_SMS = "android.provider.Telephony.SMS_RECEIVED";
    public static final String INCOMING_CALL = "android.intent.action.PHONE_STATE";
    private NotificationManagerCompat notificationManagerCompat;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        notificationManagerCompat = NotificationManagerCompat.from(this);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.drawable.icon);
        builder.setContentTitle("SMS Replier Activie");
        builder.setContentText("Your incoming call and SMS will be auto replied");

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, SMSReplierActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(contentIntent);

        // incase of multiple notificaiton incremental values can be used in notification id.

        startForeground(NOTIFICATION_ID, builder.build());
        // Foreground notification // can not be dismissed until service stopped.

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(INCOMING_SMS);
        intentFilter.addAction(INCOMING_CALL);
        registerReceiver(smsAndCallReceiver,intentFilter);

    }

    private void displayNotification(int type, String number){

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.drawable.icon);
        if (type == SmsDB.AR_INCOMING_CALL){
            builder.setContentTitle("Call from "+number);
            builder.setContentText("Your incoming call auto replied");
        }else{
            builder.setContentTitle("SMS from "+number);
            builder.setContentText("Your incoming SMS auto replied");
        }

        Intent intent = new Intent(this, SMSReplierActivity.class);
        intent.setAction(SmsDB.AR_SMS_MSG);
        intent.putExtra(SmsDB.AR_NUM,number);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);

        builder.setContentIntent(contentIntent);
        count_id = count_id+1;
        notificationManagerCompat.notify(count_id,builder.build());

    }

    private void sendSmsReply(Context context,String number,int type){
        SmsDB smsDB = new SmsDB(context);
        smsDB.open();

        Cursor cursor = smsDB.getSelectedSmsReply();
        if (cursor.getCount()>0) {
            cursor.moveToFirst();
            if (!smsDB.isInWhiteList(number)) {
                String message = getName(number)+cursor.getString(cursor.getColumnIndex(SmsDB.SMS_MESSAGE))+"    ";

                SmsManager smsManager = SmsManager.getDefault();
                ArrayList<String> parts = smsManager.divideMessage(message);
                smsManager.sendMultipartTextMessage(number, null, parts, null, null);
                SmsReplier.displayToast(this,number+" SMSReply sent");
                smsDB.insertAutoReplyLog(number,cursor.getString(cursor.getColumnIndex(SmsDB.SMS_TITLE)),message,type);
                displayNotification(type,number);
            }else{
                SmsReplier.displayToast(this,number+" found in white list-DNS");
            }
        }
        cursor.close();
        smsDB.close();

    }

    private String getName(String num){
        Uri contactUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

        String[] projection = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME}; // To return all fields
        Cursor cursor = getContentResolver().query(contactUri, projection, ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE ?", new String[]{"%" + num + "%"}, null);
        cursor.moveToFirst();

        String contactName = "";
        if (cursor.getCount()>0) {
            contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        }

        cursor.close();

        return "Hi "+contactName+", ";

    }

    private BroadcastReceiver smsAndCallReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Bundle bundle = intent.getExtras();

            if (action.equals(INCOMING_SMS)){
                Object[] pdus = (Object[])bundle.get("pdus");
                String incomingNum = null;
                StringBuilder stringBuilder = new StringBuilder();
                for (int i= 0; i<pdus.length; i++){

                    SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
                    incomingNum = smsMessage.getOriginatingAddress();
                    stringBuilder.append(smsMessage.getMessageBody());

                }
                if (incomingNum.length()>6 && !stringBuilder.toString().endsWith("    ")){
                    sendSmsReply(context,incomingNum,SmsDB.AR_INCOMING_SMS);
                }
            }else if (action.equals(INCOMING_CALL)){
                String state = bundle.getString(TelephonyManager.EXTRA_STATE);
                if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
                    String incomingNum = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                    if (incomingNum.length()>6){
                        sendSmsReply(context,incomingNum,SmsDB.AR_INCOMING_CALL);
                    }
                }
            }

        }

    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(smsAndCallReceiver);
    }
}

WhiteListFragment.java

package com.dev.cts.smsreplierv10;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.dev.cts.utils.SmsDB;
import com.dev.cts.utils.SmsReplier;

import java.util.ArrayList;

/**
 * Created by megha on 13/03/15.
 */
public class WhiteListFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener,AdapterView.OnItemLongClickListener {

    public static final int PICK_REQ_CODE = 321;
    private SmsDB smsDB;
    private SimpleCursorAdapter simpleCursorAdapter;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        smsDB = new SmsDB(activity);
        smsDB.open();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_whitelist, container, false);
        Button addNum = (Button) viewGroup.findViewById(R.id.add);
        ListView whiteListView = (ListView) viewGroup.findViewById(R.id.whitelistview);
        whiteListView.setOnItemLongClickListener(this);
        Cursor cursor = smsDB.getAllWhiteListNumbers();
        simpleCursorAdapter = new SimpleCursorAdapter(getActivity(),R.layout.white_list_box,cursor,new String[]{SmsDB.WL_CONTACT_NAME,SmsDB.WL_NUM},new int[]{R.id.display_name,R.id.phone_num});
        whiteListView.setAdapter(simpleCursorAdapter);
        addNum.setOnClickListener(this);

        return viewGroup;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        smsDB.close();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.add:
                Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
                startActivityForResult(intent, PICK_REQ_CODE);
                break;
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_REQ_CODE && resultCode == getActivity().RESULT_OK) {

            Uri contactUri = data.getData();

            updateContact(getActivity(),contactUri);
            simpleCursorAdapter.changeCursor(smsDB.getAllWhiteListNumbers());

        }
    }

    private void updateContact(Context context,Uri contactUri){
        String[] projection = null; // To return all fields
        Cursor cursor = context.getContentResolver().query(contactUri, null, null, null, null);
        cursor.moveToFirst();
        StringBuilder phoneNumber = new StringBuilder();
        String contactName = "";
        //in case if multiple numbers present
        for (int i = 0; i < cursor.getCount(); i++) {
            phoneNumber.append(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
            contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            cursor.moveToNext();
        }
        cursor.close();

        smsDB.insertWhiteListNum(phoneNumber.toString().replace("-","").replaceAll("\\s+",""),contactName);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, final long id) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Delete ?");
        builder.setMessage("Do you want to delete this White List entry ?");
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                smsDB.deleteWhiteListNum(id);
                simpleCursorAdapter.changeCursor(smsDB.getAllWhiteListNumbers());
                SmsReplier.displayToast(getActivity(),"Entry removed successfully");
            }
        });

        builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
        return true;
    }
}

ARLogFragment.java

package com.dev.cts.smsreplierv10;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.dev.cts.utils.SmsDB;
import com.dev.cts.utils.SmsReplier;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

/**
 * Created by megha on 13/03/15.
 */
public class ARLogFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener {

    public static final int PICK_REQ_CODE = 321;
    private SmsDB smsDB;
    private AutoReplyLogAdapter autoReplyLogAdapter;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        smsDB = new SmsDB(activity);
        smsDB.open();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_arloglist, container, false);
        Button clear = (Button) viewGroup.findViewById(R.id.clear);
        ListView arLogListView = (ListView) viewGroup.findViewById(R.id.arloglistview);
        arLogListView.setOnItemClickListener(this);
        Cursor cursor = smsDB.getAllAutoReplyLogs();
        //SmsReplier.displayToast(getActivity(),cursor.getCount()+" All");
        autoReplyLogAdapter = new AutoReplyLogAdapter(getActivity(),cursor);
        arLogListView.setAdapter(autoReplyLogAdapter);
        clear.setOnClickListener(this);

        return viewGroup;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        smsDB.close();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.clear:

                break;
        }

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    private class AutoReplyLogAdapter extends BaseAdapter{

        private LayoutInflater inflater;
        private Cursor cursor;
        public AutoReplyLogAdapter(Context context,Cursor cursor){
            this.cursor = cursor;
            inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return cursor.getCount();
        }

        @Override
        public Object getItem(int position) {
            cursor.moveToPosition(position);
            return cursor.getString(cursor.getColumnIndex(SmsDB.AR_NUM));
        }

        @Override
        public long getItemId(int position) {
            cursor.moveToPosition(position);
            return cursor.getLong(cursor.getColumnIndex(SmsDB.AR_ID));
        }

        private class ViewHolder{
            private TextView displayName,msgType,msgTitle,msgTime;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null){
                viewHolder = new ViewHolder();
                convertView = inflater.inflate(R.layout.arlog_list_box,parent,false);
                viewHolder.displayName = (TextView)convertView.findViewById(R.id.display_name);
                viewHolder.msgType = (TextView)convertView.findViewById(R.id.log_type);
                viewHolder.msgTitle = (TextView)convertView.findViewById(R.id.msg_title);
                viewHolder.msgTime = (TextView)convertView.findViewById(R.id.msg_time);

                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder)convertView.getTag();
            }

            cursor.moveToPosition(position);
            viewHolder.displayName.setText(getName(cursor.getString(cursor.getColumnIndex(SmsDB.AR_NUM))));
            viewHolder.msgTitle.setText(cursor.getString(cursor.getColumnIndex(SmsDB.AR_SMS_TITLE)));
            if (cursor.getInt(cursor.getColumnIndex(SmsDB.AR_TYPE))== SmsDB.AR_INCOMING_CALL){
                viewHolder.msgType.setText("Call");
            }else{
                viewHolder.msgType.setText("SMS");
            }
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(cursor.getLong(cursor.getColumnIndex(SmsDB.AR_TIME)));
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a", Locale.ENGLISH);
            viewHolder.msgTime.setText(simpleDateFormat.format(calendar.getTime()));

            return convertView;
        }

        private String getName(String num){
            Uri contactUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

            String[] projection = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME}; // To return all fields
            Cursor cursor = getActivity().getContentResolver().query(contactUri, projection, ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE ?", new String[]{"%" + num + "%"}, null);
            cursor.moveToFirst();

            String contactName = "";
            if (cursor.getCount()>0) {
                contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            }

            cursor.close();

            if (contactName.equals("")){
                return num;
            }

            return contactName;

        }
    }

}

SmsDB.java

package com.dev.cts.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.Calendar;

/**
 * Created by megha on 28/02/15.
 */
public class SmsDB {

    public static final String SMS_DB_NAME = "sms_reply.db";
    public static final int SMS_VER = 3;

    public static final String SMS_ID = "_id";
    public static final String SMS_TITLE = "_sms_title";
    public static final String SMS_MESSAGE = "_sms_message";
    public static final String SMS_IS_SELECTED = "_sms_is_selected";

    public static final String WL_ID = "_id";
    public static final String WL_NUM = "_wl_num";
    public static final String WL_CONTACT_NAME = "_wl_contact_name";

    public static final String AR_ID = "_id";
    public static final String AR_NUM = "_ar_num";
    public static final String AR_TYPE = "_ar_type";
    public static final String AR_SMS_TITLE = "_ar_sms_title";
    public static final String AR_SMS_MSG = "_ar_sms_msg";
    public static final String AR_TIME = "_ar_time";

    public static final String SMS_TABLE = "auto_rply_tbl";
    public static final String WL_TABLE = "dont_rply_tbl";
    public static final String AR_LOG_TABLE = "auto_rply_log_tbl";

    public static final int SMS_SELECTED = 1;
    public static final int SMS_UNSELECTED = 2;
    public static final int AR_INCOMING_CALL = 4;
    public static final int AR_INCOMING_SMS = 5;

    public static final String CREATE_TABLE_SMS = "CREATE TABLE "+SMS_TABLE+" ("+SMS_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+SMS_TITLE+" TEXT NOT NULL, "+SMS_MESSAGE+" TEXT NOT NULL, "+SMS_IS_SELECTED+" INTEGER NOT NULL)";
    public static final String CREATE_TABLE_WL = "CREATE TABLE "+WL_TABLE+" ("+WL_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+WL_NUM+" TEXT NOT NULL, "+WL_CONTACT_NAME+" INTEGER NOT NULL)";
    public static final String CRATE_TABLE_LOG = "CREATE TABLE "+AR_LOG_TABLE+" ("+AR_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+AR_NUM+" TEXT NOT NULL, "+AR_SMS_TITLE+" TEXT NOT NULL, "+AR_SMS_MSG+" TEXT NOT NULL, "+AR_TYPE+" INTEGER NOT NULL, "+AR_TIME+" INTEGER NOT NULL)";

    public static final String[] SMS_ALL_FIELDS = new String[]{SMS_ID,SMS_TITLE,SMS_MESSAGE,SMS_IS_SELECTED};
    public static final String[] WL_ALL_FIELDS = new String[]{WL_ID,WL_NUM,WL_CONTACT_NAME};
    public static final String[] AR_LOG_ALL_FIELDS = new String[]{AR_ID,AR_NUM,AR_TYPE,AR_SMS_TITLE,AR_SMS_MSG,AR_TIME};

    private SQLiteDatabase db;
    private SmsDBOpenHelper smsDBOpenHelper;

    public SmsDB(Context context){
        smsDBOpenHelper = new SmsDBOpenHelper(context);
    }

    public void open() throws SQLiteException{
        db = smsDBOpenHelper.getWritableDatabase();
    }

    public void close(){
        db.close();
        db = null;
    }

    public Cursor getAllSmsReplies(){
        Cursor cursor = db.query(SMS_TABLE,SMS_ALL_FIELDS,null,null,null,null,SMS_ID+" DESC");
        cursor.moveToFirst();
        return cursor;
    }

    public Cursor getAllWhiteListNumbers(){
        Cursor cursor = db.query(WL_TABLE,WL_ALL_FIELDS,null,null,null,null,WL_ID+" DESC");
        cursor.moveToFirst();
        return cursor;
    }

    public boolean isInWhiteList(String num){
        num = num.replace("-","").replaceAll("\\s+",""); // in case of spl -
        Cursor cursor = db.query(WL_TABLE,WL_ALL_FIELDS,WL_NUM+" LIKE ?",new String[]{"%"+num+"%"},null,null,null);
        cursor.moveToFirst();
        if (cursor.getCount()>0){
            cursor.close();
            return true;
        }
        cursor.close();
        return false;
    }

    public Cursor getAllAutoReplyLogs(){
        Cursor cursor = db.query(AR_LOG_TABLE,AR_LOG_ALL_FIELDS,null,null,null,null,AR_TIME+" DESC");
        cursor.moveToFirst();
        return cursor;
    }

    public Cursor getSmsReplyById(long smsId){
        Cursor cursor = db.query(SMS_TABLE,SMS_ALL_FIELDS,SMS_ID+" = ?",new String[]{Long.toString(smsId)},null,null,SMS_ID+" DESC");
        cursor.moveToFirst();
        return cursor;
    }

    public Cursor getSelectedSmsReply(){
        Cursor cursor = db.query(SMS_TABLE,SMS_ALL_FIELDS,SMS_IS_SELECTED+" = ?",new String[]{Integer.toString(SMS_SELECTED)},null,null,SMS_ID+" DESC");
        cursor.moveToFirst();
        return cursor;
    }

    public int setAllUnselected(){
        //To make sure we set all messages unselected
        ContentValues contentValues = new ContentValues();
        contentValues.put(SMS_IS_SELECTED,SMS_UNSELECTED);
        return db.update(SMS_TABLE,contentValues,null,null);
    }

    public int setSelectedSmsReply(long smsId){
        setAllUnselected();
        ContentValues contentValues = new ContentValues();
        contentValues.put(SMS_IS_SELECTED,SMS_SELECTED);
        return db.update(SMS_TABLE,contentValues,SMS_ID+" = ?",new String[]{Long.toString(smsId)});
    }

    public int updateSmsReply(long smsId,String smsTitle,String smsMessage){
        ContentValues contentValues = new ContentValues();
        contentValues.put(SMS_TITLE,smsTitle);
        contentValues.put(SMS_MESSAGE,smsMessage);
        return db.update(SMS_TABLE,contentValues,SMS_ID+" = ?",new String[]{Long.toString(smsId)});
    }

    public long insertSmsReply(String smsTitle,String smsMessage){
        ContentValues contentValues = new ContentValues();
        contentValues.put(SMS_TITLE,smsTitle);
        contentValues.put(SMS_MESSAGE,smsMessage);
        contentValues.put(SMS_IS_SELECTED,SMS_UNSELECTED);
        return db.insert(SMS_TABLE,null,contentValues);
    }

    public long insertWhiteListNum(String num,String name){

        ContentValues contentValues = new ContentValues();
        contentValues.put(WL_NUM,num);
        contentValues.put(WL_CONTACT_NAME,name);
        return db.insert(WL_TABLE,null,contentValues);
    }

    public long insertAutoReplyLog(String num,String title, String msg,int type){
        ContentValues contentValues = new ContentValues();
        contentValues.put(AR_NUM,num);
        contentValues.put(AR_TYPE,type);
        contentValues.put(AR_SMS_TITLE,title);
        contentValues.put(AR_SMS_MSG,msg);
        Calendar calendar = Calendar.getInstance();
        contentValues.put(AR_TIME,calendar.getTimeInMillis()); // System.currentTimeMillis() can also be used
        return db.insert(AR_LOG_TABLE,null,contentValues);
    }

    private boolean isSmsSelected(long smsId){
        Cursor cursor = db.query(SMS_TABLE,SMS_ALL_FIELDS,SMS_IS_SELECTED+" = ? AND "+SMS_ID+" = ?",new String[]{Integer.toString(SMS_SELECTED),Long.toString(smsId)},null,null,SMS_ID+" DESC");
        cursor.moveToFirst();
        if (cursor.getCount() > 0){
            return true;
        }

        return false;
    }

    public boolean deleteSmsReply(long smsId){
        if (isSmsSelected(smsId)){
            return false;
        }
        db.delete(SMS_TABLE,SMS_ID+" = ?",new String[]{Long.toString(smsId)});
        return true;

    }

    public boolean deleteWhiteListNum(long wlId){
        db.delete(WL_TABLE,WL_ID+" = ?",new String[]{Long.toString(wlId)});
        return true;

    }

    public boolean deleteAllAutoReplyLogs(){
        db.delete(AR_LOG_TABLE,null,null);
        return true;
    }

    private class SmsDBOpenHelper extends SQLiteOpenHelper{

        public SmsDBOpenHelper(Context context) {
            super(context, SMS_DB_NAME, null, SMS_VER);

        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(CREATE_TABLE_SMS);
            db.execSQL(CREATE_TABLE_WL);
            db.execSQL(CRATE_TABLE_LOG);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            if (newVersion > oldVersion){
                db.execSQL("DROP TABLE IF EXISTS "+SMS_TABLE);
                db.execSQL("DROP TABLE IF EXISTS "+AR_LOG_TABLE);
                db.execSQL("DROP TABLE IF EXISTS "+WL_TABLE);
                onCreate(db);
            }
        }
    }
}

SmsReplier.java

package com.dev.cts.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.widget.Toast;

/**
 * Created by megha on 27/02/15.
 */
public class SmsReplier {

    public static final String PIN = "_pin";
    public static final String FIRST_NAME = "_first_name";
    public static final String LAST_NAME = "_last_name";
    public static final String EMAIL = "_email";
    public static final String PHONE = "_phone";
    public static final String GENDER = "_gender";
    public static final String PHOTO = "_photo";
    public static final int MALE = 2;
    public static final int FEMALE = 1;
    public static final String DEFAULT_PIN = "1234";

    public static final String DEFAULT_VALUE = "";

    public static final String ICON = "icon";
    public static final String TEXT = "text";
    public static final String REG_ACTION = "reg_action";
    public static final int EDIT = 1;

    public static final String SMSLIST_FRAGMENT = "smslistfragment";
    public static final String SMSCOMPOSE_FRAGMENT = "smscomposefragment";
    public static final String WL_FRAGMENT = "wl_fragment";
    public static final String AR_LOG_FRAGMENT = "ar_log_fragment";
    public static final String REG_FRAGMENT = "reg_fragment";

    public static void setPin(Context context,String pin){
        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
        editor.putString(PIN,pin);
        editor.commit();
    }

    public static String getPin(Context context){
       SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
       return sharedPreferences.getString(PIN,DEFAULT_PIN);
    }

    public static void setFirstName(Context context,String firstName){
        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
        editor.putString(FIRST_NAME,firstName);
        editor.commit();
    }

    public static String getFirstName(Context context){
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        return sharedPreferences.getString(FIRST_NAME,DEFAULT_VALUE);
    }

    public static void setLastName(Context context,String lastName){
        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
        editor.putString(LAST_NAME,lastName);
        editor.commit();
    }

    public static String getLastName(Context context){
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        return sharedPreferences.getString(LAST_NAME,DEFAULT_VALUE);
    }

    public static void setEmail(Context context,String email){
        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
        editor.putString(EMAIL,email);
        editor.commit();
    }

    public static String getEmail(Context context){
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        return sharedPreferences.getString(EMAIL,DEFAULT_VALUE);
    }

    public static void setPhone(Context context,String phone){
        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
        editor.putString(PHONE,phone);
        editor.commit();
    }

    public static String getPhone(Context context){
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        return sharedPreferences.getString(PHONE,DEFAULT_VALUE);
    }

    public static void setGender(Context context,int gender){
        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
        editor.putInt(GENDER, gender);
        editor.commit();
    }

    public static int getGender(Context context){
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        return sharedPreferences.getInt(GENDER, FEMALE);
    }

    public static String getPhoto(Context context){
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        return sharedPreferences.getString(PHOTO,DEFAULT_VALUE);
    }

    public static void setPhoto(Context context,String photoPath){
        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
        editor.putString(PHOTO, photoPath);
        editor.commit();
    }

    public static void displayToast(Context context, String text){
        Toast.makeText(context,text,Toast.LENGTH_LONG).show();
    }

}

activity_main.xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/pager"
    >

    <android.support.v4.view.PagerTabStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="top"
        android:background="#0B614B"
        android:textColor="#fff"
        android:paddingTop="4dp"
        android:paddingBottom="4dp"
        android:textSize="20sp"
        />

</android.support.v4.view.ViewPager>

activity_smsreplier.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawerlayout"
    >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragmentholder"
        >

    </FrameLayout>

    <include layout="@layout/drawer_view"
        android:layout_gravity="start"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
     />

</android.support.v4.widget.DrawerLayout>

drawer_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#0B614B"
    android:id="@+id/navlist"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:elevation="2dp"

        >

        <ImageView
            android:layout_width="0dp"
            android:layout_height="75dp"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher"
            android:id="@+id/profile_pic"
            android:padding="5dp"
            />
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:text="@string/app_name"
            android:gravity="center"
            android:id="@+id/profile_name"
            android:textColor="#ffffffff"
            />

    </LinearLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/drawer_list"
        android:background="#ffffffff"
        android:divider="@android:color/transparent"
        />

</LinearLayout>

drawer_list_box.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:orientation="horizontal"
    >

    <ImageView
        android:layout_width="25dp"
        android:layout_height="wrap_content"
        android:src="@drawable/time36"
        android:id="@+id/menu_icon"
        android:layout_marginLeft="10dp"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/app_name"
        android:id="@+id/menu_name"
        android:gravity="center_vertical"
        android:layout_marginLeft="10dp"
        android:textSize="16sp"
        android:textStyle="bold"
        />

</LinearLayout>

Android

2018-11-27 21:26:06
1) Android Videos
2018-07-09 06:40:27
2) 09 Jul - Classroom Videos
2018-07-06 21:15:16
3) Recommended Videos
2018-07-06 21:00:45
4) Classroom Videos
2018-06-21 23:15:31
5) ViewPager TabStrip - TabLayout
2018-06-21 22:19:12
6) AsyncTask Filedownload
2018-06-21 19:54:27
7) Service Connection - Bind Service
2018-06-21 03:08:05
8) Instrumentation Test
2018-06-20 22:10:24
9) ApiDoc - Shopping List App
2018-06-20 22:08:56
10) AsyncTask - PreferenceManager - Example
2018-06-20 20:02:07
11) Fragments - RecyclerView Examples UnitTesting- Part 4
2018-06-20 19:59:57
12) Fragments - RecyclerView Examples -Database - Part 3
2018-06-20 19:59:00
13) Fragments - RecyclerView Examples - Part 2
2018-06-20 19:52:33
14) Fragments - RecyclerView Examples - Part 1
2018-06-19 23:52:14
15) Customize Adapter Example
2018-06-19 22:58:07
16) SimpleAdapter - HashMap Data Type
2018-06-19 03:39:01
17) ListView Add-Edit Example
2018-06-19 02:25:02
18) OptionMenu - Background - Shape
2018-06-18 23:53:27
19) SetActivityForResult Example
2018-06-18 21:33:01
20) Android Button Event
2018-06-18 03:19:58
21) Android Calculator View - Activity LifeCycle
2018-06-15 02:59:14
22) ViewPager - TabLayout Example
2018-06-15 00:14:08
23) Broadcast Receiver SMSManager
2018-06-14 22:58:22
24) Android Location Aware Apps - OnScreen Permission
2018-06-14 05:13:06
25) Sqlite Example - SimpleCursor Adapter
2018-06-14 03:03:36
26) Fragment - NavigationDrawer Example
2018-06-13 22:51:12
27) WebService POST
2018-06-13 05:07:48
28) WebService GET Example
2018-06-13 03:26:20
29) Service - Broadcast Receiver - SimpleAdapter
2018-06-12 23:00:54
30) ListView Add Edit Delete
2018-06-12 04:40:31
31) Background - SharedPreferences
2018-06-12 03:15:15
32) ActionBar - Option Menu
2018-06-12 01:05:30
33) RelativeLayout - ScrollView - Intent -Bundle
2018-06-11 03:38:21
34) Calculator App - Part 2
2018-06-11 00:19:59
35) Calculator Layout - Part 1
2018-05-15 03:07:50
36) Kotlin - Functional Programming Best Practices
2018-05-15 01:10:02
37) Shopping App - Kotlin - Companion Object
2018-05-14 23:51:41
38) Shopping App - Kotlin - Java SAM Interface To Lambda
2018-05-14 20:58:14
39) ShoppingList App - Api Doc
2018-05-14 01:03:11
40) Kotlin - Activities - UI Building - WebService - AsyncTask
2018-05-13 23:53:26
41) Kotlin Classes - Inheritance
2018-01-24 01:18:09
42) IOS - Performance Tuning - Best Practices
2018-01-24 00:07:33
43) Google API Pricing Plan
2018-01-24 00:03:21
44) Firebase Integration - App Analytics
2018-01-23 03:39:46
45) Profiling - Best Practices - Service - Broadcast Receiver - Document -PDF
2018-01-23 03:24:36
46) Broadcast Receiver - Profiling - Unregister - Reduce Memory Leak
2018-01-23 00:20:06
47) Background Service - Use Of LocationListener - Run Time Permission Check - Best Practice
2018-01-22 22:39:07
48) Android Memory Profiling - Performance Improvement - Handler - Runnable
2018-01-22 21:24:46
49) Android Performance Improvement Techniques - PDF Document
2018-01-22 04:50:44
50) Best Practice To Add Smooth View Pan And Communicate Between Activities
2018-01-22 02:50:35
51) Layout Technique - Activity LifeCycle
2017-12-11 05:23:14
52) ShoppingList App - Fragment - Part 5
2017-12-11 05:08:05
53) Feedback
2017-12-11 03:25:52
54) ShoppingList App - RecyclerView - Part 4
2017-12-11 00:27:38
55) ShoppingList App - DrawerLayout - Part 3 - Drawables - Menus - Gradle Configs
2017-12-11 00:23:30
56) ShoppingList App - DrawerLayout - Part 2 - Layouts
2017-12-11 00:20:50
57) ShoppingList App - DrawerLayout - Part 1
2017-12-10 21:17:56
58) ShoppingList App - Api Calls
2017-12-08 03:32:57
59) App - AsyncTast Part 2
2017-12-08 01:32:57
60) App - AsyncTast Part 1
2017-12-07 23:06:12
61) WebService GET - JSON Parsing
2017-12-07 04:51:25
62) Service And Broadcast Receiver
2017-12-06 22:49:06
63) Sqlite DB Example - ListView - Complex Form
2017-12-06 00:23:16
64) ListView - Adapters
2017-12-05 22:44:14
65) ActionBar Option Menu
2017-12-05 03:22:37
66) Multiple Activities
2015-11-19 02:45:01
67) Generate APK
2015-11-18 09:47:41
68) ShoppingList - Cloud App - WebService POST - Final
2015-11-17 13:29:54
69) Creating Swipe Views With Tabs Using ViewPager And TabLayout
2015-11-17 08:00:24
70) TabHost Using Fragment
2015-11-17 03:41:10
71) ShoppingList - Cloud App - WebService POST Example
2015-11-16 03:02:07
72) Camera - Gallery - Intent Calling - Customised Adapter
2015-11-13 08:36:20
73) Android Broadcast Receiver - SMS And Call
2015-11-12 13:17:25
74) WebService Example - HttpURLConnection Example
2015-11-12 13:11:57
75) Android Location Services - Foreground And Background Example
2015-11-11 23:04:58
76) ShoppingList App - Sqlite
2015-11-09 23:23:00
77) ShoppingList App - Continued
2015-11-09 04:09:50
78) ListView - ShoppingList App
2015-11-08 22:23:23
79) Android Layouts - Revisit
2015-03-25 04:05:46
80) Overriding Theme - ActionBar And Tab
2015-03-14 02:45:58
81) SMSReplier - V1.0 - Service - Intent Share - Phone - WhiteList - ReplyLog Addon - Part4
2015-03-14 02:44:07
82) SMSReplier - V1.0 - Service - Intent Share - Phone - WhiteList - ReplyLog Addon - Part3
2015-03-14 02:34:48
83) SMSReplier - V1.0 - Service - Intent Share - Phone - WhiteList - ReplyLog Addon - Part2
2015-03-14 02:30:01
84) SMSReplier - V1.0 - Service - Intent Share - Phone - WhiteList - ReplyLog Addon - Part1
2015-03-12 02:04:35
85) SMSReplier - V1.0 - Broadcast Receiver - Foreground Notification - Persistency Add-on - Part 2
2015-03-12 01:59:08
86) SMSReplier - V1.0 - Broadcast Receiver - Foreground Notification - Persistency Add-on - Part 1
2015-03-11 01:24:43
87) WebService -HTTP GET - AsyncTask - XML Parsing
2015-03-10 01:22:20
88) GoogleMap V2 - LocationListener - GPS
2015-03-09 01:51:33
89) SMS And Call Broadcast Receiver
2015-03-06 01:25:52
90) WebService - HTTP POST - AsyncTask - JSON Parsing
2015-03-05 01:18:38
91) WebService Introduction - GET - AsyncTask - JSON Parsing
2015-03-04 00:39:34
92) AsyncTask Example - Loading Image From Server
2015-03-03 00:15:21
93) SqliteDB Integration - SMSReplier V1.0 - Continued
2015-03-02 01:39:20
94) SMSList - Compose Fragment - FragmentBackStack - SMSReplier V1.0 - Continued...
2015-02-28 00:45:24
95) Post Login - DrawerLayout - Menu System - SMSReplier V1.0 Continued
2015-02-27 01:26:30
96) ViewPager - Tab - PreferenceManager Add On - Moving Everything To Fragment -SMSReplier V1.0 Continued
2015-02-25 02:56:09
97) Camera - Bitmap Add On - SMSReplier V1.0 - Continued
2015-02-25 01:29:05
98) SMSReplier - V1.0 - Registration Form
2015-02-24 03:56:38
99) Introduction To Service And BroadcastReceiver - IntentFilter
2015-02-23 00:57:08
100) Communicating Fragments - Bundle - Interface
2015-02-20 02:01:03
101) Navigation Drawer - Fragment Indroduction
2015-02-18 00:40:37
102) ListView - CustomAdapter With ViewHolder
2015-02-18 00:37:09
103) ListView - SimpleCursorAdapter Example
2015-02-16 04:13:55
104) ListView With SqliteDB - Add -Edit - Delete - Retrive
2015-02-13 23:07:36
105) OptionsMenu And ContextMenu With ListView
2015-02-13 01:40:46
106) ArrayAdapter And SimpleAdapter - ListView Example
2015-02-11 04:22:29
107) Forward And Reverse Bundling Objects Between Activities
2015-02-11 00:16:14
108) Bundle - Parceling Objects Between Activities
2015-02-10 00:34:05
109) ScrollView - PreferenceManager
2015-02-09 03:49:48
110) MultiActivity - AndroidManifest - Intent Introduction
2015-02-09 00:33:52
111) ClickListeners - Object Initialisation - Toast
2015-02-08 23:35:56
112) Bitmap From SD Card
2015-02-07 03:26:37
113) Android Layout Tricks - RelativeLayout
2015-02-06 23:04:29
114) Android Layout Tricks - LinearLayout
2015-02-05 20:47:58
115) UI Stack And Activity LifeCycle
2015-02-04 21:04:55
116) Working With Android Studio
2015-02-04 20:52:49
117) Java - OOP Concept - A Revisit
2014-12-08 22:11:38
118) Maintaining Login State - A Simple Approach
2014-12-07 21:03:53
119) 9 Patch Image Background
2014-12-05 04:25:48
120) WebService - JSON Example From 10.0.2.2 - Localhost
2014-12-03 03:30:10
121) VideoView - Media Controller
2014-12-03 02:52:15
122) Google Map V2 - Location Listener - Display Current Location - Check GPS Status
2014-12-02 23:12:16
123) Google Map V2 Integration
2014-12-02 04:22:51
124) Implicit Intent MIME Type - Camera - Gallery
2014-12-01 23:41:14
125) Camera - Implicit Intent - External Storage Example
2014-12-01 03:01:45
126) WebService - Google Map API WebService Example
2014-11-27 04:22:52
127) Google API Key, KeyStore, Certificate SHA1 Fingerprints
2014-11-27 03:59:46
128) Navigation Drawer - ActionBarDrawerToggle Example
2014-11-26 03:39:20
129) Notification From Service
2014-11-25 03:33:52
130) WebView Example
2014-11-24 00:37:52
131) Saving Item State - Fragment Master Detail Flow Final
2014-11-23 11:43:21
132) Package - Apk Suppress Lint Error
2014-11-21 22:49:09
133) AsyncTask Introduction - Load Image from URL
2014-11-21 05:15:40
134) GridView Example
2014-11-21 03:44:34
135) Location Aware Apps - GPS_PROVIDER - NETWORK_PROVIDER - Introduction
2014-11-20 03:20:22
136) SMSReplier Ver1.3 With Broadcast Receiver
2014-11-19 03:25:18
137) SMSReplier Ver1.2 Using Fragments And SQLite
2014-11-18 05:10:34
138) SMSReplier Ver1.1 Using Fragments - A Modular Approach
2014-11-17 05:21:15
139) ActionBar Tab - Fragment - ViewPager - Lisview - Compose Message
2014-11-15 04:43:26
140) APK - Android Application Package Process
2014-11-15 03:43:55
141) View Fragment And Pager - An Image Gallery Example
2014-11-13 04:07:42
142) Button,Checkbox With Image Background
2014-11-12 04:49:54
143) SMSReplier Ver1.0
2014-11-12 03:10:49
144) BroadcastReceiver With Permission
2014-11-11 03:06:20
145) BroadcastReceiver - Service - Example
2014-11-10 05:21:10
146) Background Service Example
2014-11-10 03:47:35
147) Runtime UI View Generation
2014-11-10 03:30:14
148) Shopping List App Version 3.1 - Shopping Object - Shopping Adapter - Custom Data View Binding
2014-11-10 00:01:53
149) Spinner Example - Dropdown - ArrayAdapter
2014-11-08 01:20:13
150) Shopping List App Version 3.0 - SQlite - CRUD
2014-11-07 03:16:33
151) Shopping List App Version 2.9 - SQlite - SimpleCursorAdapter
2014-11-06 03:08:08
152) Shopping List App Version 2.2 - JSONAdapter (custom) Addon
2014-11-05 04:12:28
153) Customising Adapter From BaseAdapter
2014-11-04 04:46:19
154) Shopping List App Version 2.1 - Alert Dialog Addon
2014-11-04 01:25:04
155) Shopping List App Version 2.1 - View-Edit-Delete Addon
2014-11-03 03:16:51
156) Json Revisit
2014-11-02 23:07:09
157) PreferenceManager Revisit
2014-10-31 03:19:16
158) Shopping List App Version 2.0 - PreferenceManager Add On - Save List
2014-10-30 03:42:19
159) Shopping List App Version 1.0 - Menu-onActivityResult-ListActivity
2014-10-30 02:18:46
160) SharedPreference Example
2014-10-29 22:17:27
161) Option Menu And Context Menu Example
2014-10-29 02:57:14
162) MultiActivity With OnActivityResult Example
2014-10-28 03:09:56
163) Bundle Example
2014-10-27 03:06:53
164) ListView - SimpleAdapter Example
2014-10-27 01:35:15
165) CoreJava Questions & Answers
2014-10-26 22:37:58
166) ListView - ArrayAdapter Example - Continued
2014-10-25 03:32:33
167) ListView - ArrayAdapter Example
2014-10-25 00:18:15
168) Calculator App
2014-10-23 03:14:37
169) Calculator Layout
2014-10-23 00:01:20
170) Event-listeners Example
2014-10-21 03:45:26
171) Android MultiActivity Example
2014-10-20 23:05:09
172) Activity Life Cycle Example
2014-10-20 23:03:17
173) Inheritance Scenario Wrt To Activity
2014-10-20 05:17:13
174) Android HelloWorld From Base
2014-10-20 01:49:59
175) Java Array And ArrayList
2014-10-19 23:06:25
176) Java Basics 1
2014-10-18 05:03:46
177) Hello World Java

© Vasudev.H / Email: tech3in@live.com