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

Commit

Permalink
Simplify on demand dialog locking
Browse files Browse the repository at this point in the history
Thanks for the idea @Tungstwenty
  • Loading branch information
M66B committed Feb 3, 2014
1 parent 2312526 commit d59e2e2
Showing 1 changed file with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions src/biz/bokhorst/xprivacy/PrivacyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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;

Expand Down Expand Up @@ -1086,20 +1086,15 @@ private Boolean onDemandDialog(final Hook hook, final PRestriction restriction)
}

final AlertDialogHolder holder = new AlertDialogHolder();
final ReentrantLock dialogLock = new ReentrantLock();
final Semaphore semaphore = new Semaphore(1);
semaphore.acquireUninterruptibly();
final CountDownLatch latch = new CountDownLatch(1);

// Run dialog in looper
mHandler.post(new Runnable() {
@Override
public void run() {
try {
dialogLock.lock();
semaphore.release();

AlertDialog.Builder builder = getOnDemandDialogBuilder(restriction, hook, appInfo,
result, context, dialogLock);
result, context, latch);
AlertDialog alertDialog = builder.create();
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.setCancelable(false);
Expand All @@ -1112,22 +1107,20 @@ public void run() {
mListError.add(ex.toString());
mListError.add(Log.getStackTraceString(ex));
}
dialogLock.unlock();
latch.countDown();
}
}
});

// Wait for dialog to display
semaphore.acquireUninterruptibly();

// Wait for dialog to complete
if (!dialogLock.tryLock(cMaxOnDemandDialog, TimeUnit.SECONDS)) {
if (!latch.await(cMaxOnDemandDialog, TimeUnit.SECONDS)) {
Util.log(null, Log.WARN, "On demand dialog timeout " + restriction);
mHandler.post(new Runnable() {
@Override
public void run() {
if (holder.dialog != null)
holder.dialog.cancel();
AlertDialog dialog = holder.dialog;
if (dialog != null)
dialog.cancel();
result.restricted = true;
}
});
Expand All @@ -1150,7 +1143,7 @@ final class AlertDialogHolder {
}

private AlertDialog.Builder getOnDemandDialogBuilder(final PRestriction restriction, Hook hook,
ApplicationInfoEx appInfo, final PRestriction result, Context context, final ReentrantLock dialogLock)
ApplicationInfoEx appInfo, final PRestriction result, Context context, final CountDownLatch latch)
throws NameNotFoundException {
// Get resources
String self = PrivacyService.class.getPackage().getName();
Expand Down Expand Up @@ -1220,7 +1213,7 @@ public void onClick(DialogInterface dialog, int which) {
// Deny
result.restricted = true;
onDemandChoice(restriction, cbCategory.isChecked(), true);
dialogLock.unlock();
latch.countDown();
}
});
alertDialogBuilder.setNeutralButton(resources.getString(R.string.title_denyonce),
Expand All @@ -1229,7 +1222,7 @@ public void onClick(DialogInterface dialog, int which) {
public void onClick(DialogInterface dialog, int which) {
// Deny
result.restricted = true;
dialogLock.unlock();
latch.countDown();
}
});
alertDialogBuilder.setNegativeButton(resources.getString(R.string.title_allow),
Expand All @@ -1239,7 +1232,7 @@ public void onClick(DialogInterface dialog, int which) {
// Allow
result.restricted = false;
onDemandChoice(restriction, cbCategory.isChecked(), false);
dialogLock.unlock();
latch.countDown();
}
});
return alertDialogBuilder;
Expand Down

0 comments on commit d59e2e2

Please sign in to comment.