package com.zjut.gc502.mobilephonesafeprotector.communicationprotector.receiver;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.android.internal.telephony.ITelephony;
import com.zjut.gc502.mobilephonesafeprotector.communicationprotector.utils.BlackNumberDao;

import java.lang.reflect.Method;

/**
 * Created by ScienceHistory on 16/4/27.
 */
public class InterceptCallReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        SharedPreferences preferences = context.getSharedPreferences("config",Context.MODE_PRIVATE);
        boolean BlackNumStatus = preferences.getBoolean("BlackNumStatus",true);
        if (!BlackNumStatus) {
            //黑名单拦截关闭
            return;
        }

        BlackNumberDao dao = new BlackNumberDao(context);
        if (!intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
            String mIncomingNumber = "";
            //如果是来电
            TelephonyManager manager = (TelephonyManager) context
                    .getSystemService(Service.TELEPHONY_SERVICE);
            switch (manager.getCallState()) {
                case TelephonyManager.CALL_STATE_RINGING:
                    mIncomingNumber = intent.getStringExtra("incoming_number");
                    int mode = dao.getBlackContactMode(mIncomingNumber);
                    if (mode == 1 || mode == 3) {
                        //观察（另外一个应用程序数据库的变化）呼叫记录的变化
                        //如果呼叫记录生成了，就把呼叫记录删除掉
                        Uri uri = Uri.parse("content://call_log/calls");
                        context.getContentResolver().registerContentObserver(
                                uri,true,new CallLogObserver(new Handler(),mIncomingNumber,context)
                        );
                        endCall(context);
                    }
                    break;
            }
        }
    }

    /**
     * 通过内容观察者观察数据库的变化
     */
    private class CallLogObserver extends ContentObserver {
        private String incomingNumber;
        private Context context;
        /**
         * Creates a content observer.
         *
         * @param handler The handler to run {@link #onChange} on, or null if none.
         */
        public CallLogObserver(Handler handler,String incomingNumber,Context context) {
            super(handler);
            this.incomingNumber = incomingNumber;
            this.context = context;
        }

        /**
         * 观察数据库内容变化调用的方法
         * @param selfChange
         */
        @Override
        public void onChange(boolean selfChange) {
            Log.i("CallLogObserver","呼叫记录数据库的内容变化了");
            context.getContentResolver().unregisterContentObserver(this);
            deleteCallLog(incomingNumber,context);
            super.onChange(selfChange);
        }

    }

    /**
     * 清除呼叫记录
     * @param incomingNumber
     * @param context
     */
    private void deleteCallLog(String incomingNumber, Context context) {
        ContentResolver resolver = context.getContentResolver();
        Uri uri = Uri.parse("content://call_log/calls");
        Cursor cursor = resolver.query(uri,new String[]{"_id"},
                "number=?",new String[]{"incomingNumber"},"_id desc limit 1");
        if (cursor.moveToNext()) {
            String id = cursor.getString(0);
            resolver.delete(uri,"_id=?",new String[]{id});
        }
    }

    /**
     * 挂断电话，需要复制两个AIDL
     */
    public void endCall(Context context) {
        try {
            Class clazz = context.getClassLoader()
                    .loadClass("android.os.Service.Manager");
            Method method = clazz.getDeclaredMethod("getService",String.class);
            IBinder iBinder = (IBinder) method.invoke(null,Context.TELECOM_SERVICE);
            ITelephony iTelephony = ITelephony.Stub.asInterface(iBinder);
            iTelephony.endCall();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
