Handling configuration changes at runtime instead of killing the process

Change-Id: I0c7f7e58ddb690f371c257b6142a4e918e9acb7f
This commit is contained in:
Sunny Goyal
2018-10-12 11:42:33 -07:00
parent 6e11a2d9f2
commit 87dc48b7bf
7 changed files with 168 additions and 48 deletions

View File

@@ -29,9 +29,12 @@ import android.util.Log;
import android.view.Display;
import android.view.WindowManager;
import com.android.launcher3.MainThreadExecutor;
import com.android.launcher3.Utilities.Consumer;
/**
* {@link BroadcastReceiver} which watches configuration changes and
* restarts the process in case changes which affect the device profile occur.
* notifies the callback in case changes which affect the device profile occur.
*/
public class ConfigMonitor extends BroadcastReceiver implements DisplayListener {
@@ -48,7 +51,9 @@ public class ConfigMonitor extends BroadcastReceiver implements DisplayListener
private final Point mRealSize;
private final Point mSmallestSize, mLargestSize;
public ConfigMonitor(Context context) {
private Consumer<Context> mCallback;
public ConfigMonitor(Context context, Consumer<Context> callback) {
mContext = context;
Configuration config = context.getResources().getConfiguration();
@@ -64,6 +69,12 @@ public class ConfigMonitor extends BroadcastReceiver implements DisplayListener
mSmallestSize = new Point();
mLargestSize = new Point();
display.getCurrentSizeRange(mSmallestSize, mLargestSize);
mCallback = callback;
mContext.registerReceiver(this, new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED));
mContext.getSystemService(DisplayManager.class)
.registerDisplayListener(this, new Handler(UiThreadHelper.getBackgroundLooper()));
}
@Override
@@ -71,16 +82,10 @@ public class ConfigMonitor extends BroadcastReceiver implements DisplayListener
Configuration config = context.getResources().getConfiguration();
if (mFontScale != config.fontScale || mDensity != config.densityDpi) {
Log.d(TAG, "Configuration changed");
killProcess();
notifyChange();
}
}
public void register() {
mContext.registerReceiver(this, new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED));
mContext.getSystemService(DisplayManager.class)
.registerDisplayListener(this, new Handler(UiThreadHelper.getBackgroundLooper()));
}
@Override
public void onDisplayAdded(int displayId) { }
@@ -97,7 +102,7 @@ public class ConfigMonitor extends BroadcastReceiver implements DisplayListener
if (!mRealSize.equals(mTmpPoint1) && !mRealSize.equals(mTmpPoint1.y, mTmpPoint1.x)) {
Log.d(TAG, String.format("Display size changed from %s to %s", mRealSize, mTmpPoint1));
killProcess();
notifyChange();
return;
}
@@ -105,22 +110,28 @@ public class ConfigMonitor extends BroadcastReceiver implements DisplayListener
if (!mSmallestSize.equals(mTmpPoint1) || !mLargestSize.equals(mTmpPoint2)) {
Log.d(TAG, String.format("Available size changed from [%s, %s] to [%s, %s]",
mSmallestSize, mLargestSize, mTmpPoint1, mTmpPoint2));
killProcess();
notifyChange();
}
}
private void killProcess() {
Log.d(TAG, "restarting launcher");
try {
mContext.unregisterReceiver(this);
mContext.getSystemService(DisplayManager.class).unregisterDisplayListener(this);
} catch (Exception e) {
// We are going to die anyway, ignore any error die to race condition in registering.
private synchronized void notifyChange() {
if (mCallback != null) {
Consumer<Context> callback = mCallback;
mCallback = null;
new MainThreadExecutor().execute(() -> callback.accept(mContext));
}
android.os.Process.killProcess(android.os.Process.myPid());
}
private Display getDefaultDisplay(Context context) {
return context.getSystemService(WindowManager.class).getDefaultDisplay();
}
public void unregister() {
try {
mContext.unregisterReceiver(this);
mContext.getSystemService(DisplayManager.class).unregisterDisplayListener(this);
} catch (Exception e) {
Log.e(TAG, "Failed to unregister config monitor", e);
}
}
}