MediaRouter: don't scanPackages if unnecessary
scanPackages was called whenever it receives a broadcast, which could be duplicated. This CL adds a boolean variable (mScanPackagesScheduled) not to scan packages if it is scheduled. Bug: 153527463 Test: cts test, atest mediarouertest and check MediaRouteProvider#onBind, #onUnbind are called when a new provider is installed. Confirm # of calls is reduced (3 -> 1). Change-Id: Iaa423b7d1b2b2a97aebed0001b4077bdf95b1d54
This commit is contained in:
@@ -169,6 +169,8 @@ final class MediaRoute2ProviderServiceProxy extends MediaRoute2Provider
|
||||
}
|
||||
|
||||
public void rebindIfDisconnected() {
|
||||
//TODO: When we are connecting to the service, calling this will unbind and bind again.
|
||||
// We'd better not unbind if we are connecting.
|
||||
if (mActiveConnection == null && shouldBind()) {
|
||||
unbind();
|
||||
bind();
|
||||
|
||||
@@ -83,7 +83,7 @@ final class MediaRoute2ProviderWatcher {
|
||||
|
||||
// Scan packages.
|
||||
// Also has the side-effect of restarting providers if needed.
|
||||
mHandler.post(mScanPackagesRunnable);
|
||||
postScanPackagesIfNeeded();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,7 +92,7 @@ final class MediaRoute2ProviderWatcher {
|
||||
mRunning = false;
|
||||
|
||||
mContext.unregisterReceiver(mScanPackagesReceiver);
|
||||
mHandler.removeCallbacks(mScanPackagesRunnable);
|
||||
mHandler.removeCallbacks(this::scanPackages);
|
||||
|
||||
// Stop all providers.
|
||||
for (int i = mProxies.size() - 1; i >= 0; i--) {
|
||||
@@ -154,20 +154,19 @@ final class MediaRoute2ProviderWatcher {
|
||||
return -1;
|
||||
}
|
||||
|
||||
private void postScanPackagesIfNeeded() {
|
||||
if (!mHandler.hasCallbacks(this::scanPackages)) {
|
||||
mHandler.post(this::scanPackages);
|
||||
}
|
||||
}
|
||||
|
||||
private final BroadcastReceiver mScanPackagesReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (DEBUG) {
|
||||
Slog.d(TAG, "Received package manager broadcast: " + intent);
|
||||
}
|
||||
scanPackages();
|
||||
}
|
||||
};
|
||||
|
||||
private final Runnable mScanPackagesRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
scanPackages();
|
||||
postScanPackagesIfNeeded();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user