Home | History | Annotate | Download | only in systemui
      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 
     26 import com.android.systemui.statusbar.policy.ConfigurationController;
     27 
     28 import java.util.ArrayList;
     29 import java.util.List;
     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 implements
     36         ConfigurationController.ConfigurationListener {
     37 
     38     private final List<InflateListener> mInflateListeners = new ArrayList<>();
     39     private final int mLayout;
     40 
     41     public AutoReinflateContainer(Context context, @Nullable AttributeSet attrs) {
     42         super(context, attrs);
     43 
     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");
     47         }
     48         mLayout = a.getResourceId(R.styleable.AutoReinflateContainer_android_layout, 0);
     49         a.recycle();
     50         inflateLayout();
     51     }
     52 
     53     @Override
     54     protected void onAttachedToWindow() {
     55         super.onAttachedToWindow();
     56         Dependency.get(ConfigurationController.class).addCallback(this);
     57     }
     58 
     59     @Override
     60     protected void onDetachedFromWindow() {
     61         super.onDetachedFromWindow();
     62         Dependency.get(ConfigurationController.class).removeCallback(this);
     63     }
     64 
     65     protected void inflateLayoutImpl() {
     66         LayoutInflater.from(getContext()).inflate(mLayout, this);
     67     }
     68 
     69     public void inflateLayout() {
     70         removeAllViews();
     71         inflateLayoutImpl();
     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 
     83     @Override
     84     public void onDensityOrFontScaleChanged() {
     85         inflateLayout();
     86     }
     87 
     88     @Override
     89     public void onOverlayChanged() {
     90         inflateLayout();
     91     }
     92 
     93     @Override
     94     public void onUiModeChanged() {
     95         inflateLayout();
     96     }
     97 
     98     @Override
     99     public void onLocaleListChanged() {
    100         inflateLayout();
    101     }
    102 
    103     public interface InflateListener {
    104         /**
    105          * Called whenever a new view is inflated.
    106          */
    107         void onInflated(View v);
    108     }
    109 }
    110