This source file includes following definitions.
- onInstallFinished
- start
- run
- isInstalled
- cancel
- isRunning
- setTimingForTests
package org.chromium.chrome.browser.banners;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.text.TextUtils;
import java.util.List;
public class InstallerDelegate implements Runnable {
public static interface Observer {
public void onInstallFinished(InstallerDelegate delegate, boolean success);
}
private static final long DEFAULT_MS_BETWEEN_RUNS = 1000;
private static final long DEFAULT_MS_MAXIMUM_WAITING_TIME = 3 * 60 * 1000;
private final Handler mHandler;
private final PackageManager mPackageManager;
private final Observer mObserver;
private final String mPackageName;
private boolean mIsRunning;
private long mMsBetweenRuns;
private long mMsMaximumWaitingTime;
private long mTimestampStarted;
InstallerDelegate(
Looper looper, PackageManager packageManager, Observer observer, String packageName) {
mHandler = new Handler(looper);
mPackageManager = packageManager;
mObserver = observer;
mPackageName = packageName;
mMsBetweenRuns = DEFAULT_MS_BETWEEN_RUNS;
mMsMaximumWaitingTime = DEFAULT_MS_MAXIMUM_WAITING_TIME;
}
public void start() {
mTimestampStarted = SystemClock.elapsedRealtime();
mIsRunning = true;
mHandler.postDelayed(this, mMsBetweenRuns);
}
@Override
public void run() {
boolean isInstalled = isInstalled();
boolean waitedTooLong =
(SystemClock.elapsedRealtime() - mTimestampStarted) > mMsMaximumWaitingTime;
if (isInstalled || !mIsRunning || waitedTooLong) {
mObserver.onInstallFinished(this, isInstalled);
mIsRunning = false;
} else {
mHandler.postDelayed(this, mMsBetweenRuns);
}
}
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;
}
void cancel() {
mIsRunning = false;
}
boolean isRunning() {
return mIsRunning;
}
void setTimingForTests(long msBetween, long msMax) {
mMsBetweenRuns = msBetween;
mMsMaximumWaitingTime = msMax;
}
}