1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.systemui.statusbar.tablet;
18
19import java.util.ArrayList;
20
21import android.content.BroadcastReceiver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.IntentFilter;
25import android.content.res.Resources;
26import android.util.DisplayMetrics;
27import android.util.Slog;
28import android.view.Display;
29import android.view.WindowManager;
30import android.view.WindowManagerImpl;
31import android.view.WindowManagerPolicy;
32
33public class HeightReceiver extends BroadcastReceiver {
34    private static final String TAG = "StatusBar.HeightReceiver";
35
36    public interface OnBarHeightChangedListener {
37        public void onBarHeightChanged(int height);
38    }
39
40    Context mContext;
41    ArrayList<OnBarHeightChangedListener> mListeners = new ArrayList<OnBarHeightChangedListener>();
42    WindowManager mWindowManager;
43    int mHeight;
44    boolean mPlugged;
45
46    public HeightReceiver(Context context) {
47        mContext = context;
48        mWindowManager = WindowManagerImpl.getDefault();
49    }
50
51    public void addOnBarHeightChangedListener(OnBarHeightChangedListener l) {
52        mListeners.add(l);
53        l.onBarHeightChanged(mHeight);
54    }
55
56    public void removeOnBarHeightChangedListener(OnBarHeightChangedListener l) {
57        mListeners.remove(l);
58    }
59
60    @Override
61    public void onReceive(Context context, Intent intent) {
62        final boolean plugged
63                = intent.getBooleanExtra(WindowManagerPolicy.EXTRA_HDMI_PLUGGED_STATE, false);
64        setPlugged(plugged);
65    }
66
67    public void registerReceiver() {
68        final IntentFilter filter = new IntentFilter();
69        filter.addAction(WindowManagerPolicy.ACTION_HDMI_PLUGGED);
70        final Intent val = mContext.registerReceiver(this, filter);
71        onReceive(mContext, val);
72    }
73
74    private void setPlugged(boolean plugged) {
75        mPlugged = plugged;
76        updateHeight();
77    }
78
79    public void updateHeight() {
80        final Resources res = mContext.getResources();
81
82        int height = -1;
83        if (mPlugged) {
84            final DisplayMetrics metrics = new DisplayMetrics();
85            Display display = mWindowManager.getDefaultDisplay();
86            display.getRealMetrics(metrics);
87
88            //Slog.i(TAG, "updateHeight: display metrics=" + metrics);
89            final int shortSide = Math.min(metrics.widthPixels, metrics.heightPixels);
90            final int externalShortSide = Math.min(display.getRawExternalWidth(),
91                    display.getRawExternalHeight());
92            height = shortSide - externalShortSide;
93        }
94
95        final int minHeight
96                = res.getDimensionPixelSize(com.android.internal.R.dimen.status_bar_height);
97        if (height < minHeight) {
98            height = minHeight;
99        }
100        Slog.i(TAG, "Resizing status bar plugged=" + mPlugged + " height="
101                + height + " old=" + mHeight);
102        mHeight = height;
103
104        final int N = mListeners.size();
105        for (int i=0; i<N; i++) {
106            mListeners.get(i).onBarHeightChanged(height);
107        }
108    }
109
110    public int getHeight() {
111        return mHeight;
112    }
113}
114
115