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);//训练语料文件
ArffLoader atf = new ArffLoader();
atf.setFile(inputFile);
System.out.println("+++++loaded train file+++++");
Instances instancesTrain = atf.getDataSet(); // 读入训练文件
inputFile = new File(TARGET_FILE);//测试语料文件
atf.setFile(inputFile);
System.out.println("+++++loaded target file+++++");
Instances instancesTest = atf.getDataSet(); // 读入测试文件
instancesTest.setClassIndex(5); //设置分类属性所在行号(第一行为0号),instancesTest.numAttributes()可以取得属性总数
int sum = instancesTest.numInstances();//测试语料实例数
System.out.println("+++++J48 sum = "+sum);
instancesTrain.setClassIndex(5); // 设置那个属性是类别属性
m_classifier.buildClassifier(instancesTrain); //训练
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("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
// 一次读入一行,直到读入null为文件结束
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
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- org.springframework.beans.factory.UnsatisfiedDependencyEx...
- DisplayBitmapTask.java显示图片的Task,实现了Runnable接口,必须在主线程调用。(1...