62 lines
2.1 KiB
Java
Raw Normal View History

2017-09-30 03:04:23 +08:00
package com.topjohnwu.magisk.container;
import android.content.ContentValues;
2017-06-01 00:19:52 +08:00
import android.content.pm.ApplicationInfo;
2017-01-25 01:23:41 +08:00
import android.content.pm.PackageManager;
2018-09-10 02:27:45 -04:00
2018-10-17 19:44:48 -04:00
import com.topjohnwu.magisk.utils.Utils;
2018-09-10 02:27:45 -04:00
import androidx.annotation.NonNull;
2017-06-01 00:19:52 +08:00
public class Policy implements Comparable<Policy>{
2017-01-26 01:13:23 +08:00
public static final int INTERACTIVE = 0;
public static final int DENY = 1;
public static final int ALLOW = 2;
public int uid, policy = INTERACTIVE;
public long until;
2017-01-30 19:27:00 +08:00
public boolean logging = true, notification = true;
public String packageName, appName;
2017-06-01 00:19:52 +08:00
public ApplicationInfo info;
2017-01-30 19:27:00 +08:00
public Policy(int uid, PackageManager pm) throws PackageManager.NameNotFoundException {
2017-01-25 01:23:41 +08:00
String[] pkgs = pm.getPackagesForUid(uid);
if (pkgs == null || pkgs.length == 0)
throw new PackageManager.NameNotFoundException();
2017-12-12 02:35:00 +08:00
this.uid = uid;
2017-10-28 15:12:29 +08:00
packageName = pkgs[0];
info = pm.getApplicationInfo(packageName, 0);
2018-10-17 19:44:48 -04:00
appName = Utils.getAppLabel(info, pm);
2017-01-25 01:23:41 +08:00
}
public Policy(ContentValues values, PackageManager pm) throws PackageManager.NameNotFoundException {
uid = values.getAsInteger("uid");
packageName = values.getAsString("package_name");
policy = values.getAsInteger("policy");
until = values.getAsInteger("until");
logging = values.getAsInteger("logging") != 0;
notification = values.getAsInteger("notification") != 0;
2017-06-01 00:19:52 +08:00
info = pm.getApplicationInfo(packageName, 0);
if (info.uid != uid)
throw new PackageManager.NameNotFoundException();
2017-06-01 00:19:52 +08:00
appName = info.loadLabel(pm).toString();
}
public ContentValues getContentValues() {
ContentValues values = new ContentValues();
values.put("uid", uid);
values.put("package_name", packageName);
2017-01-27 01:02:40 +08:00
values.put("policy", policy);
values.put("until", until);
values.put("logging", logging ? 1 : 0);
values.put("notification", notification ? 1 : 0);
return values;
}
2017-06-01 00:19:52 +08:00
@Override
public int compareTo(@NonNull Policy policy) {
return appName.toLowerCase().compareTo(policy.appName.toLowerCase());
}
}