From e9556a89dbd7c1ab23b431a26e05cbad9e4d3b38 Mon Sep 17 00:00:00 2001 From: "dooyoung.hwang" Date: Fri, 11 Nov 2016 17:23:00 +0900 Subject: [PATCH] Share one receiver for CurrentUserTracker Each CurrentUserTracker has one receiver per one object. This could cause unnecessary transaction as Tracker counts increases. The idea of redcuing receiver is creating one static receiver for CurrentUserTracker and share it with all object. Test: Check if Quicksetting order is changed properly after switching user. Change-Id: I8404829826391f8b315b42bbd0f50e77b783eeab --- .../systemui/settings/CurrentUserTracker.java | 94 +++++++++++++++---- 1 file changed, 74 insertions(+), 20 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/settings/CurrentUserTracker.java b/packages/SystemUI/src/com/android/systemui/settings/CurrentUserTracker.java index dd8075057e176..005206fcd14c5 100644 --- a/packages/SystemUI/src/com/android/systemui/settings/CurrentUserTracker.java +++ b/packages/SystemUI/src/com/android/systemui/settings/CurrentUserTracker.java @@ -22,39 +22,93 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; -public abstract class CurrentUserTracker extends BroadcastReceiver { +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; - private Context mContext; - private int mCurrentUserId; +public abstract class CurrentUserTracker { + private final UserReceiver mUserReceiver; + + private Consumer mCallback = this::onUserSwitched; public CurrentUserTracker(Context context) { - mContext = context; + mUserReceiver = UserReceiver.getInstance(context); } public int getCurrentUserId() { - return mCurrentUserId; - } - - @Override - public void onReceive(Context context, Intent intent) { - if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) { - int oldUserId = mCurrentUserId; - mCurrentUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); - if (oldUserId != mCurrentUserId) { - onUserSwitched(mCurrentUserId); - } - } + return mUserReceiver.getCurrentUserId(); } public void startTracking() { - mCurrentUserId = ActivityManager.getCurrentUser(); - IntentFilter filter = new IntentFilter(Intent.ACTION_USER_SWITCHED); - mContext.registerReceiver(this, filter); + mUserReceiver.addTracker(mCallback); } public void stopTracking() { - mContext.unregisterReceiver(this); + mUserReceiver.removeTracker(mCallback); } public abstract void onUserSwitched(int newUserId); + + private static class UserReceiver extends BroadcastReceiver { + private static UserReceiver sInstance; + + private Context mAppContext; + private boolean mReceiverRegistered; + private int mCurrentUserId; + + private List> mCallbacks = new ArrayList<>(); + + private UserReceiver(Context context) { + mAppContext = context.getApplicationContext(); + } + + static UserReceiver getInstance(Context context) { + if (sInstance == null) { + sInstance = new UserReceiver(context); + } + return sInstance; + } + + @Override + public void onReceive(Context context, Intent intent) { + if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) { + notifyUserSwitched(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0)); + } + } + + public int getCurrentUserId() { + return mCurrentUserId; + } + + private void addTracker(Consumer callback) { + if (!mCallbacks.contains(callback)) { + mCallbacks.add(callback); + } + if (!mReceiverRegistered) { + mCurrentUserId = ActivityManager.getCurrentUser(); + IntentFilter filter = new IntentFilter(Intent.ACTION_USER_SWITCHED); + mAppContext.registerReceiver(this, filter); + mReceiverRegistered = true; + } + } + + private void removeTracker(Consumer callback) { + if (mCallbacks.contains(callback)) { + mCallbacks.remove(callback); + if (mCallbacks.size() == 0 && mReceiverRegistered) { + mAppContext.unregisterReceiver(this); + mReceiverRegistered = false; + } + } + } + + private void notifyUserSwitched(int newUserId) { + if (mCurrentUserId != newUserId) { + mCurrentUserId = newUserId; + for (Consumer consumer : mCallbacks) { + consumer.accept(newUserId); + } + } + } + } }