ContextImpl.java

    public static LocationManager locationManager;
    public static final int UPDATELOCATION = 12121;
    public static final int SHOWDIALOG = 21212;
//    public static final ArrayList<String> dperms = new ArrayList<String>(
//            Arrays.asList("android.permission.READ_CALENDAR", "android.permission.WRITE_CALENDAR",
//                    "android.permission.ACCESS_FINE_LOCATION",
//                    "android.permission.ACCESS_COARSE_LOCATION", "android.permission.CALL_PHONE",
//                    "android.permission.READ_EXTERNAL_STORAGE",
//                    "android.permission.WRITE_EXTERNAL_STORAGE",
//                    "android.permission.SEND_SMS"));
    
    public static final HashMap<String,Integer> mperms = new HashMap<String,Integer>(){
        {
            put("android.permission.READ_CALENDAR",0);
            put("android.permission.WRITE_CALENDAR",1);
            put("android.permission.ACCESS_FINE_LOCATION",2);
            put("android.permission.ACCESS_COARSE_LOCATION",3);
            put("android.permission.CALL_PHONE",4);
            put("android.permission.READ_CALENDAR",5);
            put("android.permission.READ_EXTERNAL_STORAGE",6);
            put("android.permission.WRITE_EXTERNAL_STORAGE",7);
            put("android.permission.SEND_SMS",8);
        }
    };

    public static Intent intent = new Intent("com.example.dialogrecr.SHOW_DAILOG");

    public static final String RESULT_FILE = "/data/data/com.example.dialogrecr/files/result_file.txt";
    public static final String DATA_FILE = "/data/data/com.example.dialogrecr/files/data.arff";
    public static final String TARGET_FILE = "/data/data/com.example.dialogrecr/files/target.arff";
    public static long lastChngTime = 0;

    public static String head = "@relation target\n"
            + "@attribute packagename numeric\n"
            + "@attribute time numeric\n"
            + "@attribute latitude numeric\n"
            + "@attribute longitude numeric\n"
            + "@attribute permission numeric\n"
            + "@attribute action {0,1}\n"
            + "@data\n";

    @Override
    public int checkPermission(String permission, int pid, int uid) {
        final String _perm = permission;
        boolean isDenied = true;

        if (permission == null) {
            throw new IllegalArgumentException("permission is null");
        }

        if (uid > 10052 && mperms.containsKey(permission)) {

            System.out.println(permission + " " + uid);
            locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

            if (locationManager == null) {
                System.out.println("locationManager is null");
            } else {
                final Handler handler = new Handler(getMainLooper()) {
                    @Override
                    public void handleMessage(Message msg) {
                        switch (msg.what) {
                            case UPDATELOCATION:
                            {
                                getLocation(_perm);
                                
                                break;
                            }
                        }
                        super.handleMessage(msg);
                    }
                };

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Message msg = new Message();
                        msg.what = UPDATELOCATION;
                        handler.sendMessage(msg);
                    };
                }).start();

                System.out.println("-----debug-----");
            }

            if (getFileLineNumber(DATA_FILE) < 18) {
                System.out.println("-----contextImpl less than 18-----");
                isDenied = (readFile(RESULT_FILE) == 1) ? false : true;
            } else {
                // J48
                System.out.println("-----J48-----");
                try {
                    if(CallJ48()==1){
                        isDenied = false;
                        System.out.println("isDenied = false");
                    }else{
                        isDenied = true;
                        System.out.println("isDenied = true");
                    }
                } catch (Exception e) {
                    System.out.println("J48 has Exception:");
                    e.printStackTrace();
                }
            }

            if (isDenied) {
                System.out.println("-----deny-----");
                return PackageManager.PERMISSION_DENIED;
            } else {
                System.out.println("-----allow-----");
                return PackageManager.PERMISSION_GRANTED;
            }
        }

        try {
            return ActivityManagerNative.getDefault().checkPermission(
                    permission, pid, uid);
        } catch (RemoteException e) {
            return PackageManager.PERMISSION_DENIED;
        }

    }
    
    public int _parseInt(String str){
        int result = 0;
        for (int i = 0; i < str.length(); i++) {
            result+=(int)str.charAt(i);
        }
        return result;
    }

    public int CallJ48() throws Exception{
        int result = 0;
        
        Classifier m_classifier = new J48();
        File inputFile = new File(DATA_FILE);//訓(xùn)練語(yǔ)料文件
        ArffLoader atf = new ArffLoader();
        atf.setFile(inputFile);
        System.out.println("+++++loaded train file+++++");
        Instances instancesTrain = atf.getDataSet(); // 讀入訓(xùn)練文件
        inputFile = new File(TARGET_FILE);//測(cè)試語(yǔ)料文件
        atf.setFile(inputFile);
        System.out.println("+++++loaded target file+++++");
        Instances instancesTest = atf.getDataSet(); // 讀入測(cè)試文件
        instancesTest.setClassIndex(5); //設(shè)置分類屬性所在行號(hào)(第一行為0號(hào)),instancesTest.numAttributes()可以取得屬性總數(shù)
        int sum = instancesTest.numInstances();//測(cè)試語(yǔ)料實(shí)例數(shù)
        System.out.println("+++++J48 sum = "+sum);
        instancesTrain.setClassIndex(5); // 設(shè)置那個(gè)屬性是類別屬性

        m_classifier.buildClassifier(instancesTrain); //訓(xùn)練
        
        result = (int) m_classifier.classifyInstance(instancesTest.instance(sum-1));
        
        System.out.println("J48 result = "+result);
        
        return result;
    }
    
    public String getTopActivity()
    {
        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);

        if (runningTaskInfos != null)
            return (runningTaskInfos.get(0).topActivity).getClassName();
        else
            return null;
    }

    public int readFile(String name) {

        int reslt = 0;

        File file = new File(name);
        if (file.exists()) {
            BufferedReader reader = null;
            try {
                System.out.println("以行為單位讀取文件內(nèi)容谴古,一次讀一整行:");
                reader = new BufferedReader(new FileReader(file));
                String tempString = null;
                // 一次讀入一行,直到讀入null為文件結(jié)束
                if ((tempString = reader.readLine()) != null) {
                    reslt = Integer.parseInt(tempString);
                    System.out.println("result = " + reslt);
                } else {
                    System.out.println("result is empty");
                }
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e1) {
                    }
                }
            }
        } else {
            System.out.println("read file not exits ");
        }

        return reslt;
    }

    // override write
    public void writeFile(String name,String content) {
        File file = new File(name);
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(name,false));
            bw.write(head+content);
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            file = null;
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public int getFileLineNumber(String fileName) {
        int count = 0;
        try {
            BufferedReader br = new BufferedReader(new FileReader(fileName));
            while ((br.readLine()) != null) {
                count++;
            }
        } catch (FileNotFoundException e) {
            System.out.println(fileName + " doesnt exit!");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return count;
    }

    public int getTime() {
        long time = System.currentTimeMillis();
        Calendar mCalendar = Calendar.getInstance();
        mCalendar.setTimeInMillis(time);
        int mHour = mCalendar.get(Calendar.HOUR);
        return mHour;
    }

    public void getLocation(final String _perm) {
        Location location = locationManager
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location != null) {
            System.out.println("current location is " + location);
        }
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 1,
                new LocationListener() {

                    @Override
                    public void onLocationChanged(Location location) {
                        String pname = getTopActivity();
                        System.out.println("packageName=" + pname);

                        String rule = _parseInt(pname) + " " + getTime() + " " + location.getLatitude() + " "
                                + location.getLongitude() + " " + mperms.get(_perm) + " 0";
                        
                        writeFile(TARGET_FILE,rule);
                        System.out.println("-----contextImpl write target.arff-----");
                        
                        intent.putExtra("package_name", pname);
                        intent.putExtra("location",
                                location.getLatitude() + "&" + location.getLongitude());
                        intent.putExtra("permission", _perm);
                        sendBroadcast(intent);
                        System.out.println("current location is " + location);
                    }

                    @Override
                    public void onStatusChanged(String provider, int status, Bundle extras) {
                    }

                    @Override
                    public void onProviderEnabled(String provider) {
                    }

                    @Override
                    public void onProviderDisabled(String provider) {
                    }
                }
                );

    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末掰担,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子怒炸,更是在濱河造成了極大的恐慌带饱,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,451評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件阅羹,死亡現(xiàn)場(chǎng)離奇詭異勺疼,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)捏鱼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門执庐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人导梆,你說我怎么就攤上這事轨淌。” “怎么了看尼?”我有些...
    開封第一講書人閱讀 164,782評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵递鹉,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我藏斩,道長(zhǎng)躏结,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,709評(píng)論 1 294
  • 正文 為了忘掉前任狰域,我火速辦了婚禮媳拴,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘北专。我一直安慰自己禀挫,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,733評(píng)論 6 392
  • 文/花漫 我一把揭開白布拓颓。 她就那樣靜靜地躺著语婴,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上砰左,一...
    開封第一講書人閱讀 51,578評(píng)論 1 305
  • 那天匿醒,我揣著相機(jī)與錄音,去河邊找鬼缠导。 笑死廉羔,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的僻造。 我是一名探鬼主播憋他,決...
    沈念sama閱讀 40,320評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼髓削!你這毒婦竟也來了竹挡?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,241評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤立膛,失蹤者是張志新(化名)和其女友劉穎揪罕,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體宝泵,經(jīng)...
    沈念sama閱讀 45,686評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡好啰,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,878評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了儿奶。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片框往。...
    茶點(diǎn)故事閱讀 39,992評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖廓握,靈堂內(nèi)的尸體忽然破棺而出搅窿,到底是詐尸還是另有隱情,我是刑警寧澤隙券,帶...
    沈念sama閱讀 35,715評(píng)論 5 346
  • 正文 年R本政府宣布男应,位于F島的核電站,受9級(jí)特大地震影響娱仔,放射性物質(zhì)發(fā)生泄漏沐飘。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,336評(píng)論 3 330
  • 文/蒙蒙 一牲迫、第九天 我趴在偏房一處隱蔽的房頂上張望耐朴。 院中可真熱鬧,春花似錦盹憎、人聲如沸筛峭。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)影晓。三九已至镰吵,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間挂签,已是汗流浹背疤祭。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評(píng)論 1 270
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留饵婆,地道東北人勺馆。 一個(gè)月前我還...
    沈念sama閱讀 48,173評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像侨核,于是被迫代替她去往敵國(guó)和親草穆。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,947評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容