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) {
}
}
);
}
ContextImpl.java
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門执庐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人导梆,你說我怎么就攤上這事轨淌。” “怎么了看尼?”我有些...
- 文/不壞的土叔 我叫張陵递鹉,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我藏斩,道長(zhǎng)躏结,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任狰域,我火速辦了婚禮媳拴,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘北专。我一直安慰自己禀挫,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布拓颓。 她就那樣靜靜地躺著语婴,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上砰左,一...
- 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼髓削!你這毒婦竟也來了竹挡?” 一聲冷哼從身側(cè)響起,我...
- 序言:老撾萬(wàn)榮一對(duì)情侶失蹤立膛,失蹤者是張志新(化名)和其女友劉穎揪罕,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體宝泵,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡好啰,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了儿奶。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片框往。...
- 正文 年R本政府宣布男应,位于F島的核電站,受9級(jí)特大地震影響娱仔,放射性物質(zhì)發(fā)生泄漏沐飘。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一牲迫、第九天 我趴在偏房一處隱蔽的房頂上張望耐朴。 院中可真熱鬧,春花似錦盹憎、人聲如沸筛峭。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)影晓。三九已至镰吵,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間挂签,已是汗流浹背疤祭。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像侨核,于是被迫代替她去往敵國(guó)和親草穆。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- org.springframework.beans.factory.UnsatisfiedDependencyEx...
- DisplayBitmapTask.java顯示圖片的Task芹关,實(shí)現(xiàn)了Runnable接口续挟,必須在主線程調(diào)用。(1...
- 到了接近40的年紀(jì)侥衬,是不是總應(yīng)該已經(jīng)到了成熟的階段,但是其實(shí)這個(gè)階段跑芳,或多或少都是那么的迫不得已轴总。 被迫的學(xué)會(huì)沉默...
- 時(shí)光匆匆,宛若白駒過隙博个。轉(zhuǎn)眼間本學(xué)期已經(jīng)過半怀樟,期中考試成績(jī)也全然揭曉。本次期中考試學(xué)校通過成績(jī)對(duì)教師的教和學(xué)生的學(xué)...
- 不管你信不信,趣味數(shù)學(xué)共耍,或者游戲數(shù)學(xué)虑灰,是數(shù)學(xué)的一個(gè)分支,吸引了許多人去研究痹兜。其開山鼻祖可以追溯到兩位穆咐,英國(guó)的杜德尼...