1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.systemui; 16 17 import android.annotation.Nullable; 18 import android.content.Context; 19 import android.content.pm.ActivityInfo; 20 import android.content.res.Configuration; 21 import android.content.res.TypedArray; 22 import android.util.AttributeSet; 23 import android.view.LayoutInflater; 24 import android.view.View; 25 import android.widget.FrameLayout; 26 27 import java.util.ArrayList; 28 import java.util.List; 29 import java.util.Set; 30 31 /** 32 * Custom {@link FrameLayout} that re-inflates when changes to {@link Configuration} happen. 33 * Currently supports changes to density, asset path, and locale. 34 */ 35 public class AutoReinflateContainer extends FrameLayout { 36 37 private static final Set<Integer> SUPPORTED_CHANGES = Set.of( 38 ActivityInfo.CONFIG_LOCALE, 39 ActivityInfo.CONFIG_UI_MODE, 40 ActivityInfo.CONFIG_ASSETS_PATHS, 41 ActivityInfo.CONFIG_DENSITY, 42 ActivityInfo.CONFIG_FONT_SCALE 43 ); 44 45 private final List<InflateListener> mInflateListeners = new ArrayList<>(); 46 private final int mLayout; 47 48 private final Configuration mLastConfig = new Configuration(); 49 AutoReinflateContainer(Context context, @Nullable AttributeSet attrs)50 public AutoReinflateContainer(Context context, @Nullable AttributeSet attrs) { 51 super(context, attrs); 52 53 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AutoReinflateContainer); 54 if (!a.hasValue(R.styleable.AutoReinflateContainer_android_layout)) { 55 throw new IllegalArgumentException("AutoReinflateContainer must contain a layout"); 56 } 57 mLayout = a.getResourceId(R.styleable.AutoReinflateContainer_android_layout, 0); 58 a.recycle(); 59 inflateLayout(); 60 } 61 62 @Override onConfigurationChanged(Configuration newConfig)63 protected void onConfigurationChanged(Configuration newConfig) { 64 int diff = mLastConfig.updateFrom(newConfig); 65 for (int change: SUPPORTED_CHANGES) { 66 if ((diff & change) != 0) { 67 inflateLayout(); 68 return; 69 } 70 } 71 } 72 inflateLayoutImpl()73 protected void inflateLayoutImpl() { 74 LayoutInflater.from(getContext()).inflate(mLayout, this); 75 } 76 inflateLayout()77 public void inflateLayout() { 78 removeAllViews(); 79 inflateLayoutImpl(); 80 final int N = mInflateListeners.size(); 81 for (int i = 0; i < N; i++) { 82 mInflateListeners.get(i).onInflated(getChildAt(0)); 83 } 84 } 85 addInflateListener(InflateListener listener)86 public void addInflateListener(InflateListener listener) { 87 mInflateListeners.add(listener); 88 listener.onInflated(getChildAt(0)); 89 } 90 91 public interface InflateListener { 92 /** 93 * Called whenever a new view is inflated. 94 */ onInflated(View v)95 void onInflated(View v); 96 } 97 } 98