blob: 5ed9eaad0a00be0e13442a5981a91a6e2f968537 [file] [log] [blame]
Jason Monk46dbfb42016-02-25 14:59:20 -05001/*
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
15package com.android.systemui;
16
17import android.annotation.Nullable;
18import android.content.Context;
19import android.content.res.Configuration;
20import android.content.res.TypedArray;
21import android.util.AttributeSet;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.widget.FrameLayout;
25
Lucas Dupind2ddb442017-07-11 12:10:56 -070026import com.android.systemui.statusbar.policy.ConfigurationController;
27
Jason Monk46dbfb42016-02-25 14:59:20 -050028import java.util.ArrayList;
29import java.util.List;
30
Clara Bayarri9b1fdff2016-04-21 14:28:47 +010031/**
32 * Custom {@link FrameLayout} that re-inflates when changes to {@link Configuration} happen.
Lucas Dupind2ddb442017-07-11 12:10:56 -070033 * Currently supports changes to density, asset path, and locale.
Clara Bayarri9b1fdff2016-04-21 14:28:47 +010034 */
Lucas Dupind2ddb442017-07-11 12:10:56 -070035public class AutoReinflateContainer extends FrameLayout implements
36 ConfigurationController.ConfigurationListener {
Jason Monk46dbfb42016-02-25 14:59:20 -050037
38 private final List<InflateListener> mInflateListeners = new ArrayList<>();
39 private final int mLayout;
Jason Monk46dbfb42016-02-25 14:59:20 -050040
Clara Bayarri9b1fdff2016-04-21 14:28:47 +010041 public AutoReinflateContainer(Context context, @Nullable AttributeSet attrs) {
Jason Monk46dbfb42016-02-25 14:59:20 -050042 super(context, attrs);
43
Clara Bayarri9b1fdff2016-04-21 14:28:47 +010044 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 Monk46dbfb42016-02-25 14:59:20 -050047 }
Clara Bayarri9b1fdff2016-04-21 14:28:47 +010048 mLayout = a.getResourceId(R.styleable.AutoReinflateContainer_android_layout, 0);
Lucas Dupind2ddb442017-07-11 12:10:56 -070049 a.recycle();
Jason Monk46dbfb42016-02-25 14:59:20 -050050 inflateLayout();
51 }
52
53 @Override
Lucas Dupind2ddb442017-07-11 12:10:56 -070054 protected void onAttachedToWindow() {
55 super.onAttachedToWindow();
56 Dependency.get(ConfigurationController.class).addCallback(this);
57 }
Clara Bayarri9b1fdff2016-04-21 14:28:47 +010058
Lucas Dupind2ddb442017-07-11 12:10:56 -070059 @Override
60 protected void onDetachedFromWindow() {
61 super.onDetachedFromWindow();
62 Dependency.get(ConfigurationController.class).removeCallback(this);
Jason Monk46dbfb42016-02-25 14:59:20 -050063 }
64
Jason Monkbeda2dd2016-08-18 10:41:17 -040065 protected void inflateLayoutImpl() {
Jason Monk46dbfb42016-02-25 14:59:20 -050066 LayoutInflater.from(getContext()).inflate(mLayout, this);
Jason Monkbeda2dd2016-08-18 10:41:17 -040067 }
68
Lucas Dupind2ddb442017-07-11 12:10:56 -070069 public void inflateLayout() {
Jason Monkbeda2dd2016-08-18 10:41:17 -040070 removeAllViews();
71 inflateLayoutImpl();
Jason Monk46dbfb42016-02-25 14:59:20 -050072 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 Dupind2ddb442017-07-11 12:10:56 -070083 @Override
84 public void onDensityOrFontScaleChanged() {
85 inflateLayout();
86 }
87
88 @Override
89 public void onOverlayChanged() {
90 inflateLayout();
91 }
92
93 @Override
Lucas Dupin40ec6b782018-06-05 19:07:16 -070094 public void onUiModeChanged() {
95 inflateLayout();
96 }
97
98 @Override
Lucas Dupind2ddb442017-07-11 12:10:56 -070099 public void onLocaleListChanged() {
100 inflateLayout();
101 }
102
Jason Monk46dbfb42016-02-25 14:59:20 -0500103 public interface InflateListener {
104 /**
105 * Called whenever a new view is inflated.
106 */
107 void onInflated(View v);
108 }
109}