blob: 2e3cb4977488b92e1f82c680b8557d0e4455e2dd [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
26import java.util.ArrayList;
27import java.util.List;
28
29public class DensityContainer extends FrameLayout {
30
31 private final List<InflateListener> mInflateListeners = new ArrayList<>();
32 private final int mLayout;
33 private int mDensity;
34
35 public DensityContainer(Context context, @Nullable AttributeSet attrs) {
36 super(context, attrs);
37
38 mDensity = context.getResources().getConfiguration().densityDpi;
39
40 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DensityContainer);
41 if (!a.hasValue(R.styleable.DensityContainer_android_layout)) {
42 throw new IllegalArgumentException("DensityContainer must contain a layout");
43 }
44 mLayout = a.getResourceId(R.styleable.DensityContainer_android_layout, 0);
45 inflateLayout();
46 }
47
48 @Override
49 protected void onConfigurationChanged(Configuration newConfig) {
50 super.onConfigurationChanged(newConfig);
51 int density = newConfig.densityDpi;
52 if (density != mDensity) {
53 mDensity = density;
54 inflateLayout();
55 }
56 }
57
58 private void inflateLayout() {
59 removeAllViews();
60 LayoutInflater.from(getContext()).inflate(mLayout, this);
61 final int N = mInflateListeners.size();
62 for (int i = 0; i < N; i++) {
63 mInflateListeners.get(i).onInflated(getChildAt(0));
64 }
65 }
66
67 public void addInflateListener(InflateListener listener) {
68 mInflateListeners.add(listener);
69 listener.onInflated(getChildAt(0));
70 }
71
72 public interface InflateListener {
73 /**
74 * Called whenever a new view is inflated.
75 */
76 void onInflated(View v);
77 }
78}