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

Commit

Permalink
Always use service cache
Browse files Browse the repository at this point in the history
  • Loading branch information
M66B committed Jun 27, 2014
1 parent f76397c commit 7f3181f
Showing 1 changed file with 49 additions and 67 deletions.
116 changes: 49 additions & 67 deletions src/biz/bokhorst/xprivacy/PrivacyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
public class PrivacyService {
private static int mXUid = -1;
private static boolean mRegistered = false;
private static boolean mUseCache = false;
private static String mSecret = null;
private static Thread mWorker = null;
private static Handler mHandler = null;
Expand Down Expand Up @@ -108,12 +107,6 @@ public static void register(List<String> listError, String secret) {
// Publish semaphore to activity manager service
XActivityManagerService.setSemaphore(mOndemandSemaphore);

// Get memory class to enable/disable caching
// http://stackoverflow.com/questions/2630158/detect-application-heap-size-in-android
int memoryClass = (int) (Runtime.getRuntime().maxMemory() / 1024L / 1024L);
mUseCache = (memoryClass >= 32);
Util.log(null, Log.WARN, "Memory class=" + memoryClass + " cache=" + mUseCache);

// Start a worker thread
mWorker = new Thread(new Runnable() {
@Override
Expand Down Expand Up @@ -381,17 +374,16 @@ private void setRestrictionInternal(PRestriction restriction) throws RemoteExcep
}

// Update cache
if (mUseCache)
synchronized (mRestrictionCache) {
for (CRestriction key : new ArrayList<CRestriction>(mRestrictionCache.keySet()))
if (key.isSameMethod(restriction))
mRestrictionCache.remove(key);

CRestriction key = new CRestriction(restriction, restriction.extra);
if (mRestrictionCache.containsKey(key))
synchronized (mRestrictionCache) {
for (CRestriction key : new ArrayList<CRestriction>(mRestrictionCache.keySet()))
if (key.isSameMethod(restriction))
mRestrictionCache.remove(key);
mRestrictionCache.put(key, key);
}

CRestriction key = new CRestriction(restriction, restriction.extra);
if (mRestrictionCache.containsKey(key))
mRestrictionCache.remove(key);
mRestrictionCache.put(key, key);
}
} catch (Throwable ex) {
Util.bug(null, ex);
throw new RemoteException(ex.toString());
Expand Down Expand Up @@ -463,15 +455,13 @@ else if (hook.getFrom() != null) {
return mresult;

// Check cache
if (mUseCache) {
CRestriction key = new CRestriction(restriction, restriction.extra);
synchronized (mRestrictionCache) {
if (mRestrictionCache.containsKey(key)) {
cached = true;
CRestriction cache = mRestrictionCache.get(key);
mresult.restricted = cache.restricted;
mresult.asked = cache.asked;
}
CRestriction key = new CRestriction(restriction, restriction.extra);
synchronized (mRestrictionCache) {
if (mRestrictionCache.containsKey(key)) {
cached = true;
CRestriction cache = mRestrictionCache.get(key);
mresult.restricted = cache.restricted;
mresult.asked = cache.asked;
}
}

Expand Down Expand Up @@ -574,13 +564,11 @@ else if (hook.getFrom() != null) {
}

// Update cache
if (mUseCache) {
CRestriction key = new CRestriction(mresult, restriction.extra);
synchronized (mRestrictionCache) {
if (mRestrictionCache.containsKey(key))
mRestrictionCache.remove(key);
mRestrictionCache.put(key, key);
}
CRestriction ukey = new CRestriction(mresult, restriction.extra);
synchronized (mRestrictionCache) {
if (mRestrictionCache.containsKey(ukey))
mRestrictionCache.remove(ukey);
mRestrictionCache.put(ukey, ukey);
}
}

Expand All @@ -591,11 +579,11 @@ else if (hook.getFrom() != null) {

// Update cache
if (ondemand) {
CRestriction key = new CRestriction(mresult, restriction.extra);
CRestriction okey = new CRestriction(mresult, restriction.extra);
synchronized (mRestrictionCache) {
if (mRestrictionCache.containsKey(key))
mRestrictionCache.remove(key);
mRestrictionCache.put(key, key);
if (mRestrictionCache.containsKey(okey))
mRestrictionCache.remove(okey);
mRestrictionCache.put(okey, okey);
}
}
}
Expand Down Expand Up @@ -797,10 +785,9 @@ public void deleteRestrictions(int uid, String restrictionName) throws RemoteExc
}

// Clear caches
if (mUseCache)
synchronized (mRestrictionCache) {
mRestrictionCache.clear();
}
synchronized (mRestrictionCache) {
mRestrictionCache.clear();
}
synchronized (mAskedOnceCache) {
mAskedOnceCache.clear();
}
Expand Down Expand Up @@ -1022,15 +1009,13 @@ private void setSettingInternal(PSetting setting) throws RemoteException {
}

// Update cache
if (mUseCache) {
CSetting key = new CSetting(setting.uid, setting.type, setting.name);
key.setValue(setting.value);
synchronized (mSettingCache) {
if (mSettingCache.containsKey(key))
mSettingCache.remove(key);
if (setting.value != null)
mSettingCache.put(key, key);
}
CSetting key = new CSetting(setting.uid, setting.type, setting.name);
key.setValue(setting.value);
synchronized (mSettingCache) {
if (mSettingCache.containsKey(key))
mSettingCache.remove(key);
if (setting.value != null)
mSettingCache.put(key, key);
}

// Clear restrictions for white list
Expand All @@ -1042,10 +1027,10 @@ private void setSettingInternal(PSetting setting) throws RemoteException {
hook.getName());
Util.log(null, Log.WARN, "Clearing cache for " + restriction);
synchronized (mRestrictionCache) {
for (CRestriction key : new ArrayList<CRestriction>(mRestrictionCache.keySet()))
if (key.isSameMethod(restriction)) {
Util.log(null, Log.WARN, "Removing " + key);
mRestrictionCache.remove(key);
for (CRestriction mkey : new ArrayList<CRestriction>(mRestrictionCache.keySet()))
if (mkey.isSameMethod(restriction)) {
Util.log(null, Log.WARN, "Removing " + mkey);
mRestrictionCache.remove(mkey);
}
}
}
Expand Down Expand Up @@ -1085,7 +1070,7 @@ public PSetting getSetting(PSetting setting) throws RemoteException {
// No permissions enforced

// Check cache
if (mUseCache && setting.value != null) {
if (setting.value != null) {
CSetting key = new CSetting(setting.uid, setting.type, setting.name);
synchronized (mSettingCache) {
if (mSettingCache.containsKey(key)) {
Expand Down Expand Up @@ -1145,7 +1130,7 @@ public PSetting getSetting(PSetting setting) throws RemoteException {
}

// Add to cache
if (mUseCache && result.value != null) {
if (result.value != null) {
CSetting key = new CSetting(setting.uid, setting.type, setting.name);
key.setValue(result.value);
synchronized (mSettingCache) {
Expand Down Expand Up @@ -1229,10 +1214,9 @@ public void deleteSettings(int uid) throws RemoteException {
}

// Clear cache
if (mUseCache)
synchronized (mSettingCache) {
mSettingCache.clear();
}
synchronized (mSettingCache) {
mSettingCache.clear();
}
} catch (Throwable ex) {
Util.bug(null, ex);
throw new RemoteException(ex.toString());
Expand Down Expand Up @@ -1273,13 +1257,11 @@ public void clear() throws RemoteException {
}

// Clear caches
if (mUseCache) {
synchronized (mRestrictionCache) {
mRestrictionCache.clear();
}
synchronized (mSettingCache) {
mSettingCache.clear();
}
synchronized (mRestrictionCache) {
mRestrictionCache.clear();
}
synchronized (mSettingCache) {
mSettingCache.clear();
}
synchronized (mAskedOnceCache) {
mAskedOnceCache.clear();
Expand Down

0 comments on commit 7f3181f

Please sign in to comment.