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 import com.android.launcher3.Utilities; 27 28 /** 29 * {@link BroadcastReceiver} which watches configuration changes and 30 * restarts the process in case changes which affect the device profile occur. 31 */ 32 public class ConfigMonitor extends BroadcastReceiver { 33 34 private final Context mContext; 35 private final float mFontScale; 36 private final int mDensity; 37 38 public ConfigMonitor(Context context) { 39 mContext = context; 40 41 Configuration config = context.getResources().getConfiguration(); 42 mFontScale = config.fontScale; 43 mDensity = getDensity(config); 44 } 45 46 @Override 47 public void onReceive(Context context, Intent intent) { 48 Configuration config = context.getResources().getConfiguration(); 49 if (mFontScale != config.fontScale || mDensity != getDensity(config)) { 50 Log.d("ConfigMonitor", "Configuration changed, restarting launcher"); 51 mContext.unregisterReceiver(this); 52 android.os.Process.killProcess(android.os.Process.myPid()); 53 } 54 } 55 56 public void register() { 57 mContext.registerReceiver(this, new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED)); 58 } 59 60 private static int getDensity(Configuration config) { 61 return Utilities.ATLEAST_JB_MR1 ? config.densityDpi : 0; 62 } 63 } 64