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;
18
19import android.content.Context;
20import android.util.AttributeSet;
21import android.util.Log;
22import android.view.View;
23import android.view.ViewGroup;
24import android.view.accessibility.AccessibilityEvent;
25import android.widget.ImageView;
26import android.widget.LinearLayout;
27
28import com.android.systemui.R;
29import com.android.systemui.statusbar.policy.NetworkController;
30
31// Intimately tied to the design of res/layout/signal_cluster_view.xml
32public class SignalClusterView
33        extends LinearLayout
34        implements NetworkController.SignalCluster {
35
36    static final boolean DEBUG = false;
37    static final String TAG = "SignalClusterView";
38
39    NetworkController mNC;
40
41    private boolean mWifiVisible = false;
42    private int mWifiStrengthId = 0;
43    private boolean mMobileVisible = false;
44    private int mMobileStrengthId = 0, mMobileTypeId = 0;
45    private boolean mIsAirplaneMode = false;
46    private int mAirplaneIconId = 0;
47    private String mWifiDescription, mMobileDescription, mMobileTypeDescription;
48
49    ViewGroup mWifiGroup, mMobileGroup;
50    ImageView mWifi, mMobile, mMobileType, mAirplane;
51    View mSpacer;
52
53    public SignalClusterView(Context context) {
54        this(context, null);
55    }
56
57    public SignalClusterView(Context context, AttributeSet attrs) {
58        this(context, attrs, 0);
59    }
60
61    public SignalClusterView(Context context, AttributeSet attrs, int defStyle) {
62        super(context, attrs, defStyle);
63    }
64
65    public void setNetworkController(NetworkController nc) {
66        if (DEBUG) Log.d(TAG, "NetworkController=" + nc);
67        mNC = nc;
68    }
69
70    @Override
71    protected void onAttachedToWindow() {
72        super.onAttachedToWindow();
73
74        mWifiGroup      = (ViewGroup) findViewById(R.id.wifi_combo);
75        mWifi           = (ImageView) findViewById(R.id.wifi_signal);
76        mMobileGroup    = (ViewGroup) findViewById(R.id.mobile_combo);
77        mMobile         = (ImageView) findViewById(R.id.mobile_signal);
78        mMobileType     = (ImageView) findViewById(R.id.mobile_type);
79        mSpacer         =             findViewById(R.id.spacer);
80        mAirplane       = (ImageView) findViewById(R.id.airplane);
81
82        apply();
83    }
84
85    @Override
86    protected void onDetachedFromWindow() {
87        mWifiGroup      = null;
88        mWifi           = null;
89        mMobileGroup    = null;
90        mMobile         = null;
91        mMobileType     = null;
92        mSpacer         = null;
93        mAirplane       = null;
94
95        super.onDetachedFromWindow();
96    }
97
98    @Override
99    public void setWifiIndicators(boolean visible, int strengthIcon, String contentDescription) {
100        mWifiVisible = visible;
101        mWifiStrengthId = strengthIcon;
102        mWifiDescription = contentDescription;
103
104        apply();
105    }
106
107    @Override
108    public void setMobileDataIndicators(boolean visible, int strengthIcon,
109            int typeIcon, String contentDescription, String typeContentDescription) {
110        mMobileVisible = visible;
111        mMobileStrengthId = strengthIcon;
112        mMobileTypeId = typeIcon;
113        mMobileDescription = contentDescription;
114        mMobileTypeDescription = typeContentDescription;
115
116        apply();
117    }
118
119    @Override
120    public void setIsAirplaneMode(boolean is, int airplaneIconId) {
121        mIsAirplaneMode = is;
122        mAirplaneIconId = airplaneIconId;
123
124        apply();
125    }
126
127    @Override
128    public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
129        // Standard group layout onPopulateAccessibilityEvent() implementations
130        // ignore content description, so populate manually
131        if (mWifiVisible && mWifiGroup != null && mWifiGroup.getContentDescription() != null)
132            event.getText().add(mWifiGroup.getContentDescription());
133        if (mMobileVisible && mMobileGroup != null && mMobileGroup.getContentDescription() != null)
134            event.getText().add(mMobileGroup.getContentDescription());
135        return super.dispatchPopulateAccessibilityEvent(event);
136    }
137
138    @Override
139    public void onRtlPropertiesChanged(int layoutDirection) {
140        super.onRtlPropertiesChanged(layoutDirection);
141
142        if (mWifi != null) {
143            mWifi.setImageDrawable(null);
144        }
145
146        if (mMobile != null) {
147            mMobile.setImageDrawable(null);
148        }
149
150        if (mMobileType != null) {
151            mMobileType.setImageDrawable(null);
152        }
153
154        if(mAirplane != null) {
155            mAirplane.setImageDrawable(null);
156        }
157
158        apply();
159    }
160
161    // Run after each indicator change.
162    private void apply() {
163        if (mWifiGroup == null) return;
164
165        if (mWifiVisible) {
166            mWifi.setImageResource(mWifiStrengthId);
167
168            mWifiGroup.setContentDescription(mWifiDescription);
169            mWifiGroup.setVisibility(View.VISIBLE);
170        } else {
171            mWifiGroup.setVisibility(View.GONE);
172        }
173
174        if (DEBUG) Log.d(TAG,
175                String.format("wifi: %s sig=%d",
176                    (mWifiVisible ? "VISIBLE" : "GONE"),
177                    mWifiStrengthId));
178
179        if (mMobileVisible && !mIsAirplaneMode) {
180            mMobile.setImageResource(mMobileStrengthId);
181            mMobileType.setImageResource(mMobileTypeId);
182
183            mMobileGroup.setContentDescription(mMobileTypeDescription + " " + mMobileDescription);
184            mMobileGroup.setVisibility(View.VISIBLE);
185        } else {
186            mMobileGroup.setVisibility(View.GONE);
187        }
188
189        if (mIsAirplaneMode) {
190            mAirplane.setImageResource(mAirplaneIconId);
191            mAirplane.setVisibility(View.VISIBLE);
192        } else {
193            mAirplane.setVisibility(View.GONE);
194        }
195
196        if (mMobileVisible && mWifiVisible && mIsAirplaneMode) {
197            mSpacer.setVisibility(View.INVISIBLE);
198        } else {
199            mSpacer.setVisibility(View.GONE);
200        }
201
202        if (DEBUG) Log.d(TAG,
203                String.format("mobile: %s sig=%d typ=%d",
204                    (mMobileVisible ? "VISIBLE" : "GONE"),
205                    mMobileStrengthId, mMobileTypeId));
206
207        mMobileType.setVisibility(
208                !mWifiVisible ? View.VISIBLE : View.GONE);
209    }
210}
211
212