// Path: app/src/main/java/com/yan/mobilesafe/DataBase/AddressDB.java
// public class AddressDB {
//     private static final String PATH = "data/data/com.yan.mobilesafe/files/address.db";
// 
//     public static String getAddress(String number){
//         String address = "未知号码";
// 
//         //获取数据库对象
//         SQLiteDatabase database = SQLiteDatabase.openDatabase(PATH,null,SQLiteDatabase.OPEN_READONLY);
// 
//         //手机号正则表达式匹配
//         //1 + （3.4.5.6.7.8)+(9位数字)
//         //^1[3-8]\d{9}$
// 
//         if (number.matches("^1[3-8]\\d{9}$")){
//             Cursor cursor = database.rawQuery("select location from data2 where id = (select outkey from data1 where id = ?)"
//             ,new String[]{number.substring(0,7)});
//             if (cursor.moveToNext()){
//                 address = cursor.getString(0);
//             }
//             cursor.close();
//         }else if (number.matches("^\\d+$")){ //匹配数字
//             switch (number.length()){
//                 case 3:
//                     address = "报警电话";
//                     break;
//                 case 4:
//                     address = "模拟器电话";
//                     break;
//                 case 5:
//                     address = "客服电话";
//                     break;
//                 case 7:
//                 case 8:
//                     address = "本地电话";
//                     break;
//                 default:
//                     //03154011111
//                     //01055225525
//                     if (number.startsWith("0") && number.length()>10){  //可能是长途电话
//                         //先查询3位区号
//                         Cursor cursor = database.rawQuery("select location from data2 where area = ?",
//                                 new String[]{number.substring(1,3)});
//                         if (cursor.moveToNext()){
//                             address = cursor.getString(0);
//                             address = address.substring(0,address.length()-2);
//                         }else {
//                             cursor.close();
//                             //查询4位区号
//                             cursor = database.rawQuery("select location from data2 where area = ?",
//                                     new String[]{number.substring(1,4)});
//                             if (cursor.moveToNext()){
//                                 address = cursor.getString(0);
//                                 address = address.substring(0,address.length()-2);
//                             }
//                             cursor.close();
//                         }
//                     }
//                     break;
//             }
//         }
// 
//         database.close();
//         return address;
//     }
// }
// 
// Path: app/src/main/java/com/yan/mobilesafe/utils/ToastUtils.java
// public class ToastUtils {
//     public static void showToast(final Activity context, final String string){
//         if ("main".equals(Thread.currentThread().getName())){ //判断是否为主线程
//             Toast.makeText(context,string,Toast.LENGTH_SHORT).show();
//         }else {
//             context.runOnUiThread(new Runnable() { //如果不是，就用该方法使其在ui线程中运行
//                 @Override
//                 public void run() {
//                     Toast.makeText(context, string, Toast.LENGTH_SHORT).show();
//                 }
//             });
//         }
// 
//     }
// }


import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import com.yan.mobilesafe.DataBase.AddressDB;
import com.yan.mobilesafe.R;
import com.yan.mobilesafe.utils.ToastUtils;
    }
    
    class MyListener extends PhoneStateListener {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING: //电话响了
                    Log.e("AddressService", "来电话了");
                    String address = AddressDB.getAddress(incomingNumber);
                    //ToastUtils.showToast(getApplicationContext(), address);
                    showToast(address);
                    break;
                case TelephonyManager.CALL_STATE_IDLE: //电话闲置状态
                    if (windowManager != null) {
                        windowManager.removeView(view);  //移除view
                    }

            }
            super.onCallStateChanged(state, incomingNumber);
        }
    }

    /***
     * 监听去电广播接收者
     */
    class OutCallReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String number = getResultData();
// Path: app/src/main/java/com/yan/mobilesafe/DataBase/AddressDB.java
// public class AddressDB {
//     private static final String PATH = "data/data/com.yan.mobilesafe/files/address.db";
// 
//     public static String getAddress(String number){
//         String address = "未知号码";
// 
//         //获取数据库对象
//         SQLiteDatabase database = SQLiteDatabase.openDatabase(PATH,null,SQLiteDatabase.OPEN_READONLY);
// 
//         //手机号正则表达式匹配
//         //1 + （3.4.5.6.7.8)+(9位数字)
//         //^1[3-8]\d{9}$
// 
//         if (number.matches("^1[3-8]\\d{9}$")){
//             Cursor cursor = database.rawQuery("select location from data2 where id = (select outkey from data1 where id = ?)"
//             ,new String[]{number.substring(0,7)});
//             if (cursor.moveToNext()){
//                 address = cursor.getString(0);
//             }
//             cursor.close();
//         }else if (number.matches("^\\d+$")){ //匹配数字
//             switch (number.length()){
//                 case 3:
//                     address = "报警电话";
//                     break;
//                 case 4:
//                     address = "模拟器电话";
//                     break;
//                 case 5:
//                     address = "客服电话";
//                     break;
//                 case 7:
//                 case 8:
//                     address = "本地电话";
//                     break;
//                 default:
//                     //03154011111
//                     //01055225525
//                     if (number.startsWith("0") && number.length()>10){  //可能是长途电话
//                         //先查询3位区号
//                         Cursor cursor = database.rawQuery("select location from data2 where area = ?",
//                                 new String[]{number.substring(1,3)});
//                         if (cursor.moveToNext()){
//                             address = cursor.getString(0);
//                             address = address.substring(0,address.length()-2);
//                         }else {
//                             cursor.close();
//                             //查询4位区号
//                             cursor = database.rawQuery("select location from data2 where area = ?",
//                                     new String[]{number.substring(1,4)});
//                             if (cursor.moveToNext()){
//                                 address = cursor.getString(0);
//                                 address = address.substring(0,address.length()-2);
//                             }
//                             cursor.close();
//                         }
//                     }
//                     break;
//             }
//         }
// 
//         database.close();
//         return address;
//     }
// }
// 
// Path: app/src/main/java/com/yan/mobilesafe/utils/ToastUtils.java
// public class ToastUtils {
//     public static void showToast(final Activity context, final String string){
//         if ("main".equals(Thread.currentThread().getName())){ //判断是否为主线程
//             Toast.makeText(context,string,Toast.LENGTH_SHORT).show();
//         }else {
//             context.runOnUiThread(new Runnable() { //如果不是，就用该方法使其在ui线程中运行
//                 @Override
//                 public void run() {
//                     Toast.makeText(context, string, Toast.LENGTH_SHORT).show();
//                 }
//             });
//         }
// 
//     }
// }


// Path: app/src/main/java/com/yan/mobilesafe/Service/AddressService.java
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import com.yan.mobilesafe.DataBase.AddressDB;
import com.yan.mobilesafe.R;
import com.yan.mobilesafe.utils.ToastUtils;

    }
    
    class MyListener extends PhoneStateListener {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING: //电话响了
                    Log.e("AddressService", "来电话了");
                    String address = AddressDB.getAddress(incomingNumber);
                    //ToastUtils.showToast(getApplicationContext(), address);
                    showToast(address);
                    break;
                case TelephonyManager.CALL_STATE_IDLE: //电话闲置状态
                    if (windowManager != null) {
                        windowManager.removeView(view);  //移除view
                    }

            }
            super.onCallStateChanged(state, incomingNumber);
        }
    }

    /***
     * 监听去电广播接收者
     */
    class OutCallReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String number = getResultData();
            String address = AddressDB.getAddress(number);