Skip to content
This repository has been archived by the owner on Sep 6, 2019. It is now read-only.

Commit

Permalink
Cache settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Bokhorst committed Jun 22, 2013
1 parent 5159081 commit 2dd76ec
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/biz/bokhorst/xprivacy/ActivityApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public RestrictionAdapter(Context context, int resource, XApplicationInfo appInf
mAppInfo = appInfo;
mRestrictions = restrictions;
mExpert = Boolean.parseBoolean(XRestriction.getSetting(null, context, XRestriction.cSettingExpert,
Boolean.FALSE.toString()));
Boolean.FALSE.toString(), false));
}

@Override
Expand Down
8 changes: 4 additions & 4 deletions src/biz/bokhorst/xprivacy/ActivityMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,13 @@ private void optionSettings() {

// Set current values
String sExpert = XRestriction.getSetting(null, ActivityMain.this, XRestriction.cSettingExpert,
Boolean.FALSE.toString());
Boolean.FALSE.toString(), false);
final boolean expert = Boolean.parseBoolean(sExpert);
cbSettings.setChecked(expert);
etLat.setText(XRestriction.getSetting(null, ActivityMain.this, XRestriction.cSettingLatitude, ""));
etLon.setText(XRestriction.getSetting(null, ActivityMain.this, XRestriction.cSettingLongitude, ""));
etLat.setText(XRestriction.getSetting(null, ActivityMain.this, XRestriction.cSettingLatitude, "", false));
etLon.setText(XRestriction.getSetting(null, ActivityMain.this, XRestriction.cSettingLongitude, "", false));
etMac.setText(XRestriction.getSetting(null, ActivityMain.this, XRestriction.cSettingMac,
XRestriction.getDefacedMac()));
XRestriction.getDefacedMac(), false));

// Wait for OK
btnOk.setOnClickListener(new View.OnClickListener() {
Expand Down
2 changes: 1 addition & 1 deletion src/biz/bokhorst/xprivacy/PackageChange.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void onReceive(Context context, Intent intent) {
int uid = intent.getIntExtra(Intent.EXTRA_UID, 0);
boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
boolean expert = Boolean.parseBoolean(XRestriction.getSetting(null, context, XRestriction.cSettingExpert,
Boolean.FALSE.toString()));
Boolean.FALSE.toString(), false));
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);

Expand Down
2 changes: 1 addition & 1 deletion src/biz/bokhorst/xprivacy/XActivityThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected void before(MethodHookParam param) throws Throwable {
if (isRestricted(param, mActionName))
if (Boolean.parseBoolean(XRestriction.getSetting(this,
AndroidAppHelper.currentApplication(), XRestriction.cSettingExpert,
Boolean.FALSE.toString())))
Boolean.FALSE.toString(), true)))
param.setResult(null);
} else if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
// Outgoing call
Expand Down
2 changes: 1 addition & 1 deletion src/biz/bokhorst/xprivacy/XApplicationInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static List<XApplicationInfo> getXApplicationList(Context context) {
// Get references
PackageManager pm = context.getPackageManager();
boolean expert = Boolean.parseBoolean(XRestriction.getSetting(null, context, XRestriction.cSettingExpert,
Boolean.FALSE.toString()));
Boolean.FALSE.toString(), false));

// Get app list
SparseArray<XApplicationInfo> mapApp = new SparseArray<XApplicationInfo>();
Expand Down
4 changes: 2 additions & 2 deletions src/biz/bokhorst/xprivacy/XLocationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ private void replaceLocationListener(MethodHookParam param, int arg) throws Thro
}

private Location getBaseLocation(Context context) {
String sLat = XRestriction.getSetting(this, context, XRestriction.cSettingLatitude, "");
String sLon = XRestriction.getSetting(this, context, XRestriction.cSettingLongitude, "");
String sLat = XRestriction.getSetting(this, context, XRestriction.cSettingLatitude, "", true);
String sLon = XRestriction.getSetting(this, context, XRestriction.cSettingLongitude, "", true);
if (sLat.equals("") || sLon.equals(""))
return null;
Location location = new Location("");
Expand Down
73 changes: 58 additions & 15 deletions src/biz/bokhorst/xprivacy/XRestriction.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public class XRestriction {
private final static int cCacheTimeoutMs = 15 * 1000;
private static Map<String, List<String>> mPermissions = new LinkedHashMap<String, List<String>>();
private static Map<String, List<String>> mMethods = new LinkedHashMap<String, List<String>>();
private static Map<String, CacheEntry> mRestrictionCache = new HashMap<String, CacheEntry>();
private static Map<String, CRestriction> mRestrictionCache = new HashMap<String, CRestriction>();
private static Map<String, CSetting> mSettingsCache = new HashMap<String, CSetting>();
private static Map<UsageData, UsageData> mUsageQueue = new LinkedHashMap<UsageData, UsageData>();

static {
Expand Down Expand Up @@ -238,7 +239,7 @@ public static void registerMethod(String methodName, String restrictionName, Str
public static List<String> getRestrictions() {
List<String> listRestriction = new ArrayList<String>(Arrays.asList(cRestrictionNames));
if (!Boolean.parseBoolean(XRestriction.getSetting(null, null, XRestriction.cSettingExpert,
Boolean.FALSE.toString())))
Boolean.FALSE.toString(), false)))
listRestriction.remove(cBoot);
return listRestriction;
}
Expand Down Expand Up @@ -322,7 +323,7 @@ public static boolean getRestricted(XHook hook, Context context, int uid, String
if (useCache)
synchronized (mRestrictionCache) {
if (mRestrictionCache.containsKey(keyCache)) {
CacheEntry entry = mRestrictionCache.get(keyCache);
CRestriction entry = mRestrictionCache.get(keyCache);
if (entry.isExpired())
mRestrictionCache.remove(keyCache);
else {
Expand Down Expand Up @@ -413,13 +414,13 @@ public static boolean getRestricted(XHook hook, Context context, int uid, String

// Fallback
restricted = XPrivacyProvider.getRestrictedFallback(hook, uid, restrictionName, methodName);
} else {
// Add to cache
synchronized (mRestrictionCache) {
if (mRestrictionCache.containsKey(keyCache))
mRestrictionCache.remove(keyCache);
mRestrictionCache.put(keyCache, new CacheEntry(restricted));
}
}

// Add to cache
synchronized (mRestrictionCache) {
if (mRestrictionCache.containsKey(keyCache))
mRestrictionCache.remove(keyCache);
mRestrictionCache.put(keyCache, new CRestriction(restricted));
}

// Result
Expand Down Expand Up @@ -465,8 +466,24 @@ public static void setRestricted(XHook hook, Context context, int uid, String re
logRestriction(hook, context, uid, "set", restrictionName, methodName, restricted, false);
}

public static String getSetting(XHook hook, Context context, String settingName, String defaultValue) {
// TODO: settings caching
public static String getSetting(XHook hook, Context context, String settingName, String defaultValue,
boolean useCache) {
// Check cache
if (useCache)
synchronized (mSettingsCache) {
if (mSettingsCache.containsKey(settingName)) {
CSetting entry = mSettingsCache.get(settingName);
if (entry.isExpired())
mSettingsCache.remove(settingName);
else {
String value = mSettingsCache.get(settingName).getSettingsValue();
XUtil.log(hook, Log.INFO, String.format("get setting %s=%s *", settingName, value));
return value;
}
}
}

// Get setting
boolean fallback = true;
String value = defaultValue;
if (context != null) {
Expand Down Expand Up @@ -499,9 +516,17 @@ public static String getSetting(XHook hook, Context context, String settingName,
}
}

// Use fallback
if (fallback)
value = XPrivacyProvider.getSettingFallback(settingName, defaultValue);

// Add to cache
synchronized (mSettingsCache) {
if (mSettingsCache.containsKey(settingName))
mSettingsCache.remove(settingName);
mSettingsCache.put(settingName, new CSetting(value));
}

XUtil.log(hook, Log.INFO, String.format("get setting %s=%s%s", settingName, value, (fallback ? " #" : "")));
return value;
}
Expand Down Expand Up @@ -536,7 +561,7 @@ public static long getDefacedHex() {
}

public static String getDefacedMac() {
return getSetting(null, null, cSettingMac, "de:fa:ce:de:fa:ce");
return getSetting(null, null, cSettingMac, "de:fa:ce:de:fa:ce", true);
}

public static byte[] getDefacedBytes() {
Expand Down Expand Up @@ -570,11 +595,11 @@ private static String getPackageName(Context context, int uid) {

// Helper classes

private static class CacheEntry {
private static class CRestriction {
private long mTimestamp;
private boolean mRestricted;

public CacheEntry(boolean restricted) {
public CRestriction(boolean restricted) {
mTimestamp = new Date().getTime();
mRestricted = restricted;
}
Expand All @@ -588,6 +613,24 @@ public boolean isRestricted() {
}
}

private static class CSetting {
private long mTimestamp;
private String mValue;

public CSetting(String settingValue) {
mTimestamp = new Date().getTime();
mValue = settingValue;
}

public boolean isExpired() {
return (mTimestamp + cCacheTimeoutMs < new Date().getTime());
}

public String getSettingsValue() {
return mValue;
}
}

private static class UsageData {
private Integer mUid;
private String mRestriction;
Expand Down

0 comments on commit 2dd76ec

Please sign in to comment.