package com.allengarvey.annuncified;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;


public class CallReceiver extends BroadcastReceiver{
    public void onReceive(Context context, Intent intent) {

        try {
            TelephonyManager tmgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

            //Create Listner
            MyPhoneStateListener PhoneListener = new MyPhoneStateListener(context);

            // Register listener for LISTEN_CALL_STATE
            tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

        } catch (Exception e) {
            Log.e("Phone Receive Error", " " + e);
        }
    }

    private class MyPhoneStateListener extends PhoneStateListener{
        private Context app;

        public MyPhoneStateListener(Context app){
            super();
            this.app = app;
        }

        public void onCallStateChanged(int state, String incomingNumber) {
            if (state == TelephonyManager.CALL_STATE_RINGING) {
                String msg = "New Phone Call Event. Incoming Number : "+incomingNumber;
                int duration = Toast.LENGTH_LONG;
                Toast toast = Toast.makeText(app, msg, duration);
                toast.show();

                if(NotifyUtil.getIgnoreCallsFromNonContactsSetting(app) && NotifyUtil.getContactIdFromPhoneNumber(app, incomingNumber).equals(NotifyUtil.NOT_FOUND)){
                    NotifyUtil.saveOriginalDefaultRingtonePath(app);
                    silenceCall();
                }
            }
            else{ //hung up, took call, etc.
                NotifyUtil.resetOriginalDefaultRingtonePath(app);
            }
        }

        public void silenceCall(){
            RingtoneManager.setActualDefaultRingtoneUri(app, RingtoneManager.TYPE_RINGTONE, null);
        }

    }
}

@@ -178,32 +178,8 @@
 er;%0A
-import android.net.Uri;%0A
 impo
@@ -291,37 +291,8 @@
 og;%0A
-import android.widget.Toast;%0A
 %0A%0Apu
@@ -551,16 +551,17 @@
 ate List
+e
 ner%0A    
@@ -1224,242 +1224,8 @@
 G) %7B
-%0A                String msg = %22New Phone Call Event. Incoming Number : %22+incomingNumber;%0A                int duration = Toast.LENGTH_LONG;%0A                Toast toast = Toast.makeText(app, msg, duration);%0A                toast.show();
 %0A%0A  

remove toast notification from call listener