Jason Monk | 46dbfb4 | 2016-02-25 14:59:20 -0500 | [diff] [blame] | 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.res.Configuration; |
| 20 | import android.content.res.TypedArray; |
| 21 | import android.util.AttributeSet; |
| 22 | import android.view.LayoutInflater; |
| 23 | import android.view.View; |
| 24 | import android.widget.FrameLayout; |
| 25 | |
Lucas Dupin | d2ddb44 | 2017-07-11 12:10:56 -0700 | [diff] [blame] | 26 | import com.android.systemui.statusbar.policy.ConfigurationController; |
| 27 | |
Jason Monk | 46dbfb4 | 2016-02-25 14:59:20 -0500 | [diff] [blame] | 28 | import java.util.ArrayList; |
| 29 | import java.util.List; |
| 30 | |
Clara Bayarri | 9b1fdff | 2016-04-21 14:28:47 +0100 | [diff] [blame] | 31 | /** |
| 32 | * Custom {@link FrameLayout} that re-inflates when changes to {@link Configuration} happen. |
Lucas Dupin | d2ddb44 | 2017-07-11 12:10:56 -0700 | [diff] [blame] | 33 | * Currently supports changes to density, asset path, and locale. |
Clara Bayarri | 9b1fdff | 2016-04-21 14:28:47 +0100 | [diff] [blame] | 34 | */ |
Lucas Dupin | d2ddb44 | 2017-07-11 12:10:56 -0700 | [diff] [blame] | 35 | public class AutoReinflateContainer extends FrameLayout implements |
| 36 | ConfigurationController.ConfigurationListener { |
Jason Monk | 46dbfb4 | 2016-02-25 14:59:20 -0500 | [diff] [blame] | 37 | |
| 38 | private final List<InflateListener> mInflateListeners = new ArrayList<>(); |
| 39 | private final int mLayout; |
Jason Monk | 46dbfb4 | 2016-02-25 14:59:20 -0500 | [diff] [blame] | 40 | |
Clara Bayarri | 9b1fdff | 2016-04-21 14:28:47 +0100 | [diff] [blame] | 41 | public AutoReinflateContainer(Context context, @Nullable AttributeSet attrs) { |
Jason Monk | 46dbfb4 | 2016-02-25 14:59:20 -0500 | [diff] [blame] | 42 | super(context, attrs); |
| 43 | |
Clara Bayarri | 9b1fdff | 2016-04-21 14:28:47 +0100 | [diff] [blame] | 44 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AutoReinflateContainer); |
| 45 | if (!a.hasValue(R.styleable.AutoReinflateContainer_android_layout)) { |
| 46 | throw new IllegalArgumentException("AutoReinflateContainer must contain a layout"); |
Jason Monk | 46dbfb4 | 2016-02-25 14:59:20 -0500 | [diff] [blame] | 47 | } |
Clara Bayarri | 9b1fdff | 2016-04-21 14:28:47 +0100 | [diff] [blame] | 48 | mLayout = a.getResourceId(R.styleable.AutoReinflateContainer_android_layout, 0); |
Lucas Dupin | d2ddb44 | 2017-07-11 12:10:56 -0700 | [diff] [blame] | 49 | a.recycle(); |
Jason Monk | 46dbfb4 | 2016-02-25 14:59:20 -0500 | [diff] [blame] | 50 | inflateLayout(); |
| 51 | } |
| 52 | |
| 53 | @Override |
Lucas Dupin | d2ddb44 | 2017-07-11 12:10:56 -0700 | [diff] [blame] | 54 | protected void onAttachedToWindow() { |
| 55 | super.onAttachedToWindow(); |
| 56 | Dependency.get(ConfigurationController.class).addCallback(this); |
| 57 | } |
Clara Bayarri | 9b1fdff | 2016-04-21 14:28:47 +0100 | [diff] [blame] | 58 | |
Lucas Dupin | d2ddb44 | 2017-07-11 12:10:56 -0700 | [diff] [blame] | 59 | @Override |
| 60 | protected void onDetachedFromWindow() { |
| 61 | super.onDetachedFromWindow(); |
| 62 | Dependency.get(ConfigurationController.class).removeCallback(this); |
Jason Monk | 46dbfb4 | 2016-02-25 14:59:20 -0500 | [diff] [blame] | 63 | } |
| 64 | |
Jason Monk | beda2dd | 2016-08-18 10:41:17 -0400 | [diff] [blame] | 65 | protected void inflateLayoutImpl() { |
Jason Monk | 46dbfb4 | 2016-02-25 14:59:20 -0500 | [diff] [blame] | 66 | LayoutInflater.from(getContext()).inflate(mLayout, this); |
Jason Monk | beda2dd | 2016-08-18 10:41:17 -0400 | [diff] [blame] | 67 | } |
| 68 | |
Lucas Dupin | d2ddb44 | 2017-07-11 12:10:56 -0700 | [diff] [blame] | 69 | public void inflateLayout() { |
Jason Monk | beda2dd | 2016-08-18 10:41:17 -0400 | [diff] [blame] | 70 | removeAllViews(); |
| 71 | inflateLayoutImpl(); |
Jason Monk | 46dbfb4 | 2016-02-25 14:59:20 -0500 | [diff] [blame] | 72 | final int N = mInflateListeners.size(); |
| 73 | for (int i = 0; i < N; i++) { |
| 74 | mInflateListeners.get(i).onInflated(getChildAt(0)); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | public void addInflateListener(InflateListener listener) { |
| 79 | mInflateListeners.add(listener); |
| 80 | listener.onInflated(getChildAt(0)); |
| 81 | } |
| 82 | |
Lucas Dupin | d2ddb44 | 2017-07-11 12:10:56 -0700 | [diff] [blame] | 83 | @Override |
| 84 | public void onDensityOrFontScaleChanged() { |
| 85 | inflateLayout(); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void onOverlayChanged() { |
| 90 | inflateLayout(); |
| 91 | } |
| 92 | |
| 93 | @Override |
Lucas Dupin | 40ec6b78 | 2018-06-05 19:07:16 -0700 | [diff] [blame] | 94 | public void onUiModeChanged() { |
| 95 | inflateLayout(); |
| 96 | } |
| 97 | |
| 98 | @Override |
Lucas Dupin | d2ddb44 | 2017-07-11 12:10:56 -0700 | [diff] [blame] | 99 | public void onLocaleListChanged() { |
| 100 | inflateLayout(); |
| 101 | } |
| 102 | |
Jason Monk | 46dbfb4 | 2016-02-25 14:59:20 -0500 | [diff] [blame] | 103 | public interface InflateListener { |
| 104 | /** |
| 105 | * Called whenever a new view is inflated. |
| 106 | */ |
| 107 | void onInflated(View v); |
| 108 | } |
| 109 | } |