Android Open Source - CallNotifier Calls Receiver






From Project

Back to project page CallNotifier.

License

The source code is released under:

Apache License

If you think the Android project CallNotifier listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.hevsoft.callnotiffier;
//w  w  w  .j av a  2 s  .  c  o m
import android.content.*;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.provider.ContactsContract;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

import java.util.Date;

/**
 * Created by elvis.iulian on 11/7/2014.
 */
public class CallsReceiver extends BroadcastReceiver {

    public static Handler handler ;
    public static String INNR;
    public static String TAG = "CallReceiver";

    @Override
    public void onReceive(final Context context, Intent intent) {
        SharedPreferences pref = context.getSharedPreferences(MainActivity.PREF_NAME, Context.MODE_PRIVATE);
        boolean enabled = pref.getBoolean(MainActivity.ENABLED_KEY,false);
        if(!enabled){
            Log.d(TAG, "receiver is disabled");
            return;
        }
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        if(handler==null){
            Log.d(TAG,"handler is null");
            handler = new Handler();
        }

        if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
            handler.removeCallbacksAndMessages(null);
            TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
            telephony.listen(new PhoneStateListener(){
                @Override
                public void onCallStateChanged(int state, String incomingNumber) {
                    super.onCallStateChanged(state, incomingNumber);
                    ContentResolver resolver =context.getContentResolver();
                    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(incomingNumber));
                    Cursor cursor = resolver.query(uri, new String[] {ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null,
                                          null);
                    String contactName = null;
                    if(cursor.moveToFirst()) {
                        contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
                    }
                    Subject subj = new Subject(contactName,incomingNumber);
                    subj.callDate = new Date(System.currentTimeMillis());
                    final SendSmsRunnable runnable = new SendSmsRunnable(context,subj) ;
                    handler.postDelayed(runnable,new Configuration().getCallTimeOut(context));
                    Log.d(TAG,"post the sms runnable");
                }
            },PhoneStateListener.LISTEN_CALL_STATE);

        }else if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
            handler.removeCallbacksAndMessages(null);
            Log.d(TAG,"sms runnable canceled");
        }else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
            Log.d(TAG,"sms runnable canceled");
            handler.removeCallbacksAndMessages(null);
        }
    }


    class SendSmsRunnable implements Runnable{

        private Context context;
        private Subject subject;

        SendSmsRunnable(Context context,Subject subj){
            this.context =context;
            this.subject = subj;
        }

        @Override
        public void run() {
            ObsController controller = new ObsController(context);
            controller.notifyObservers(subject);
        }
    }


}




Java Source Code List

com.hevsoft.callnotiffier.ApplicationTest.java
com.hevsoft.callnotiffier.CallsReceiver.java
com.hevsoft.callnotiffier.Configuration.java
com.hevsoft.callnotiffier.Contact.java
com.hevsoft.callnotiffier.IObsController.java
com.hevsoft.callnotiffier.MainActivity.java
com.hevsoft.callnotiffier.ObsController.java
com.hevsoft.callnotiffier.SmsWrapper.java
com.hevsoft.callnotiffier.Subject.java