Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1883)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/banners/InstallerDelegate.java

Issue 156013005: Add InstallerDelegate for watching that a package gets installed (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Removing logs Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/banners/InstallerDelegate.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/banners/InstallerDelegate.java b/chrome/android/java/src/org/chromium/chrome/browser/banners/InstallerDelegate.java
new file mode 100644
index 0000000000000000000000000000000000000000..3e6fc6061b46f221532675eb0929763d027d8a9f
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/banners/InstallerDelegate.java
@@ -0,0 +1,87 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.chrome.browser.banners;
+
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.os.Handler;
+import android.text.TextUtils;
+
+import java.util.List;
+
+/**
+ * Monitors the PackageManager to see when an app has been installed.
+ */
+public class InstallerDelegate implements Runnable {
+ /**
+ * Callback for when the app install has completed.
+ */
+ public static interface Observer {
+ /**
+ * Called when the task has finished.
+ * @param delegate Instance of the class that finished.
+ * @param success Whether or not the package was successfully installed.
+ */
+ public void onInstallFinished(InstallerDelegate delegate, boolean success);
+ }
+
+ private final PackageManager mPackageManager;
+ private final Observer mObserver;
+ private final String mPackageName;
+ private boolean mIsCancelled;
+
+ /**
+ * Constructs the InstallerDelegate.
+ * @param packageManager Provides access to the list of installed apps.
+ * @param observer Alerted when the package has been completely installed.
+ * @param packageName Name of the package for the app to monitor.
+ */
+ InstallerDelegate(PackageManager packageManager, Observer observer, String packageName) {
+ mPackageManager = packageManager;
+ mObserver = observer;
+ mPackageName = packageName;
+ }
+
+ /**
+ * Begins monitoring the package manager for the app install.
+ */
+ @Override
+ public void run() {
+ if (isInstalled()) {
+ mObserver.onInstallFinished(this, true);
+ } else if (mIsCancelled) {
+ mObserver.onInstallFinished(this, false);
+ } else {
+ new Handler().postDelayed(this, 1000);
Yaron 2014/02/13 08:45:43 make member variable
gone 2014/02/14 05:16:26 Well, that took a lot more effort than expected.
+ }
+ }
+
+ /**
+ * Checks if the app has been installed on the system.
+ * @return True if the PackageManager reports that the app is installed, false otherwise.
+ */
+ private boolean isInstalled() {
+ List<PackageInfo> packs = mPackageManager.getInstalledPackages(0);
+ for (int i = 0; i < packs.size(); i++) {
+ if (TextUtils.equals(packs.get(i).packageName, mPackageName)) return true;
+ }
+ return false;
+ }
+
+ /**
+ * Prevent rescheduling the Runnable.
+ */
+ void cancel() {
+ mIsCancelled = true;
+ }
+
+ /**
+ * Checks to see if the Runnable will continue scheduling itself.
+ * @return True if the runnable has not been cancelled.
+ */
+ boolean isRunning() {
+ return !mIsCancelled;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698