1 package com.android.launcher3.util; 2 3 /** 4 * Copyright (C) 2015 The Android Open Source Project 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.IntentFilter; 23 import android.content.res.Configuration; 24 import android.util.Log; 25 26 /** 27 * {@link BroadcastReceiver} which watches configuration changes and 28 * restarts the process in case changes which affect the device profile occur. 29 */ 30 public class ConfigMonitor extends BroadcastReceiver { 31 32 private final Context mContext; 33 private final float mFontScale; 34 private final int mDensity; 35 ConfigMonitor(Context context)36 public ConfigMonitor(Context context) { 37 mContext = context; 38 39 Configuration config = context.getResources().getConfiguration(); 40 mFontScale = config.fontScale; 41 mDensity = config.densityDpi; 42 } 43 44 @Override onReceive(Context context, Intent intent)45 public void onReceive(Context context, Intent intent) { 46 Configuration config = context.getResources().getConfiguration(); 47 if (mFontScale != config.fontScale || mDensity != config.densityDpi) { 48 Log.d("ConfigMonitor", "Configuration changed, restarting launcher"); 49 mContext.unregisterReceiver(this); 50 android.os.Process.killProcess(android.os.Process.myPid()); 51 } 52 } 53 register()54 public void register() { 55 mContext.registerReceiver(this, new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED)); 56 } 57 } 58