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

Commit

Permalink
Fixed not restricting, multiple on demand dialogs
Browse files Browse the repository at this point in the history
Refs #1242
Refs #1243
  • Loading branch information
M66B committed Feb 3, 2014
1 parent d59e2e2 commit a9e4d31
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Test and beta releases will have experimental functions enabled by default.

**Next release**

* Fixed not restricting if not using on demand ([issue](https://github.com/M66B/XPrivacy/issues/1242))
* Fixed multiple on demand dialogs ([issue](https://github.com/M66B/XPrivacy/issues/1243))
* Broken on demand asking for same restriction

[Open issues](https://github.com/M66B/XPrivacy/issues?state=open)

**Version 1.99.29 TEST**
Expand Down
4 changes: 2 additions & 2 deletions src/biz/bokhorst/xprivacy/CRestriction.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ public class CRestriction {

public CRestriction(PRestriction restriction) {
mTimestamp = new Date().getTime();
mRestriction = restriction;
mRestriction = new PRestriction(restriction);
}

public PRestriction getRestriction() {
return mRestriction;
}

public void setRestriction(PRestriction restriction) {
mRestriction = restriction;
mRestriction = new PRestriction(restriction);
}

public boolean isExpired() {
Expand Down
8 changes: 8 additions & 0 deletions src/biz/bokhorst/xprivacy/PRestriction.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ public class PRestriction implements Parcelable {
public PRestriction() {
}

public PRestriction(PRestriction other) {
uid = other.uid;
restrictionName = other.restrictionName;
methodName = other.methodName;
restricted = other.restricted;
time = other.time;
}

public PRestriction(int _uid, String category, String method) {
uid = _uid;
restrictionName = category;
Expand Down
6 changes: 6 additions & 0 deletions src/biz/bokhorst/xprivacy/PSetting.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ public class PSetting implements Parcelable {
public PSetting() {
}

public PSetting(PSetting other) {
uid = other.uid;
name = other.name;
value = other.value;
}

public PSetting(int _uid, String _name, String _value) {
uid = _uid;
name = _name;
Expand Down
36 changes: 12 additions & 24 deletions src/biz/bokhorst/xprivacy/PrivacyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
Expand Down Expand Up @@ -60,7 +60,7 @@ public class PrivacyService {
private static Handler mHandler = null;
private static boolean mBootCompleted = false;

private static ReentrantLock mOndemandLock = new ReentrantLock(true);
private static Semaphore mOndemandSemaphore = new Semaphore(1, true);

private static SQLiteStatement stmtGetRestriction = null;
private static SQLiteStatement stmtGetSetting = null;
Expand All @@ -81,7 +81,7 @@ public class PrivacyService {
private static final int cMaxOnDemandDialog = 10; // seconds

// TODO: define column names
// sqlite3 /data/data/biz.bokhorst.xprivacy/xprivacy.db
// sqlite3 /data/xprivacy/xprivacy.db

public static void register(List<String> listError, String secret) {
// Store secret and errors
Expand Down Expand Up @@ -129,9 +129,10 @@ public void run() {
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
try {
// Delay ANR while on demand dialog open
boolean ondemanding = (mOndemandSemaphore.availablePermits() < 1);
Util.log(null, Log.WARN, "Foreground ANR uid=" + getUidANR(param) + " ondemand="
+ mOndemandLock.isLocked());
if (mOndemandLock.isLocked())
+ ondemanding);
if (ondemanding)
param.setResult(5 * 1000);
} catch (Throwable ex) {
Util.bug(null, ex);
Expand All @@ -153,9 +154,10 @@ protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
try {
// Ignore ANR while on demand dialog open
boolean ondemanding = (mOndemandSemaphore.availablePermits() < 1);
Util.log(null, Log.WARN, "Background ANR uid=" + getUidANR(param) + " ondemand="
+ mOndemandLock.isLocked());
if (mOndemandLock.isLocked())
+ ondemanding);
if (ondemanding)
param.setResult(null);
} catch (Throwable ex) {
Util.bug(null, ex);
Expand Down Expand Up @@ -509,7 +511,7 @@ else if (PrivacyManager.cView.equals(restriction.restrictionName))
notifyRestricted(restriction);

// Ask to restrict
if (!result.asked && usage && PrivacyManager.isApplication(restriction.uid))
if (!result.restricted && !result.asked && usage && PrivacyManager.isApplication(restriction.uid))
result.restricted = onDemandDialog(hook, restriction);

// Log usage
Expand Down Expand Up @@ -1067,24 +1069,10 @@ private Boolean onDemandDialog(final Hook hook, final PRestriction restriction)

// Go ask
Util.log(null, Log.WARN, "On demand " + restriction);
mOndemandLock.lock();
mOndemandSemaphore.acquireUninterruptibly();
try {
Util.log(null, Log.WARN, "On demanding " + restriction);

// Check if not asked before
CRestriction key = new CRestriction(restriction);
synchronized (mRestrictionCache) {
if (mRestrictionCache.containsKey(key)) {
PRestriction cache = mRestrictionCache.get(key).getRestriction();
result.restricted = cache.restricted;
result.asked = cache.asked;
}
}
if (result.asked) {
Util.log(null, Log.WARN, "Already asked " + restriction);
return result.restricted;
}

final AlertDialogHolder holder = new AlertDialogHolder();
final CountDownLatch latch = new CountDownLatch(1);

Expand Down Expand Up @@ -1126,7 +1114,7 @@ public void run() {
});
}
} finally {
mOndemandLock.unlock();
mOndemandSemaphore.release();
}
} catch (Throwable ex) {
Util.bug(null, ex);
Expand Down

0 comments on commit a9e4d31

Please sign in to comment.