/* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.systemui.statusbar; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.view.accessibility.AccessibilityEvent; import android.widget.ImageView; import android.widget.LinearLayout; import com.android.systemui.R; import com.android.systemui.statusbar.policy.NetworkControllerImpl; import com.android.systemui.statusbar.policy.SecurityController; // Intimately tied to the design of res/layout/signal_cluster_view.xml public class SignalClusterView extends LinearLayout implements NetworkControllerImpl.SignalCluster, SecurityController.SecurityControllerCallback { static final String TAG = "SignalClusterView"; static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); NetworkControllerImpl mNC; SecurityController mSC; private boolean mVpnVisible = false; private boolean mWifiVisible = false; private int mWifiStrengthId = 0; private boolean mMobileVisible = false; private int mMobileStrengthId = 0, mMobileTypeId = 0; private boolean mIsAirplaneMode = false; private int mAirplaneIconId = 0; private String mWifiDescription, mMobileDescription, mMobileTypeDescription; private boolean mRoaming; private boolean mIsMobileTypeIconWide; ViewGroup mWifiGroup, mMobileGroup; ImageView mVpn, mWifi, mMobile, mMobileType, mAirplane; View mWifiAirplaneSpacer; View mWifiSignalSpacer; private int mWideTypeIconStartPadding; public SignalClusterView(Context context) { this(context, null); } public SignalClusterView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public SignalClusterView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public void setNetworkController(NetworkControllerImpl nc) { if (DEBUG) Log.d(TAG, "NetworkController=" + nc); mNC = nc; } public void setSecurityController(SecurityController sc) { if (DEBUG) Log.d(TAG, "SecurityController=" + sc); mSC = sc; mSC.addCallback(this); mVpnVisible = mSC.isVpnEnabled(); } @Override protected void onFinishInflate() { super.onFinishInflate(); mWideTypeIconStartPadding = getContext().getResources().getDimensionPixelSize( R.dimen.wide_type_icon_start_padding); } @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); mVpn = (ImageView) findViewById(R.id.vpn); mWifiGroup = (ViewGroup) findViewById(R.id.wifi_combo); mWifi = (ImageView) findViewById(R.id.wifi_signal); mMobileGroup = (ViewGroup) findViewById(R.id.mobile_combo); mMobile = (ImageView) findViewById(R.id.mobile_signal); mMobileType = (ImageView) findViewById(R.id.mobile_type); mAirplane = (ImageView) findViewById(R.id.airplane); mWifiAirplaneSpacer = findViewById(R.id.wifi_airplane_spacer); mWifiSignalSpacer = findViewById(R.id.wifi_signal_spacer); apply(); } @Override protected void onDetachedFromWindow() { mVpn = null; mWifiGroup = null; mWifi = null; mMobileGroup = null; mMobile = null; mMobileType = null; mAirplane = null; super.onDetachedFromWindow(); } // From SecurityController. @Override public void onStateChanged() { post(new Runnable() { @Override public void run() { mVpnVisible = mSC.isVpnEnabled(); apply(); } }); } @Override public void setWifiIndicators(boolean visible, int strengthIcon, String contentDescription) { mWifiVisible = visible; mWifiStrengthId = strengthIcon; mWifiDescription = contentDescription; apply(); } @Override public void setMobileDataIndicators(boolean visible, int strengthIcon, int typeIcon, String contentDescription, String typeContentDescription, boolean roaming, boolean isTypeIconWide) { mMobileVisible = visible; mMobileStrengthId = strengthIcon; mMobileTypeId = typeIcon; mMobileDescription = contentDescription; mMobileTypeDescription = typeContentDescription; mRoaming = roaming; mIsMobileTypeIconWide = isTypeIconWide; apply(); } @Override public void setIsAirplaneMode(boolean is, int airplaneIconId) { mIsAirplaneMode = is; mAirplaneIconId = airplaneIconId; apply(); } @Override public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { // Standard group layout onPopulateAccessibilityEvent() implementations // ignore content description, so populate manually if (mWifiVisible && mWifiGroup != null && mWifiGroup.getContentDescription() != null) event.getText().add(mWifiGroup.getContentDescription()); if (mMobileVisible && mMobileGroup != null && mMobileGroup.getContentDescription() != null) event.getText().add(mMobileGroup.getContentDescription()); return super.dispatchPopulateAccessibilityEvent(event); } @Override public void onRtlPropertiesChanged(int layoutDirection) { super.onRtlPropertiesChanged(layoutDirection); if (mWifi != null) { mWifi.setImageDrawable(null); } if (mMobile != null) { mMobile.setImageDrawable(null); } if (mMobileType != null) { mMobileType.setImageDrawable(null); } if(mAirplane != null) { mAirplane.setImageDrawable(null); } apply(); } @Override public boolean hasOverlappingRendering() { return false; } // Run after each indicator change. private void apply() { if (mWifiGroup == null) return; mVpn.setVisibility(mVpnVisible ? View.VISIBLE : View.GONE); if (DEBUG) Log.d(TAG, String.format("vpn: %s", mVpnVisible ? "VISIBLE" : "GONE")); if (mWifiVisible) { mWifi.setImageResource(mWifiStrengthId); mWifiGroup.setContentDescription(mWifiDescription); mWifiGroup.setVisibility(View.VISIBLE); } else { mWifiGroup.setVisibility(View.GONE); } if (DEBUG) Log.d(TAG, String.format("wifi: %s sig=%d", (mWifiVisible ? "VISIBLE" : "GONE"), mWifiStrengthId)); if (mMobileVisible && !mIsAirplaneMode) { mMobile.setImageResource(mMobileStrengthId); mMobileType.setImageResource(mMobileTypeId); mMobileGroup.setContentDescription(mMobileTypeDescription + " " + mMobileDescription); mMobileGroup.setVisibility(View.VISIBLE); } else { mMobileGroup.setVisibility(View.GONE); } if (mIsAirplaneMode) { mAirplane.setImageResource(mAirplaneIconId); mAirplane.setVisibility(View.VISIBLE); } else { mAirplane.setVisibility(View.GONE); } if (mIsAirplaneMode && mWifiVisible) { mWifiAirplaneSpacer.setVisibility(View.VISIBLE); } else { mWifiAirplaneSpacer.setVisibility(View.GONE); } if (mRoaming && mMobileVisible && mWifiVisible) { mWifiSignalSpacer.setVisibility(View.VISIBLE); } else { mWifiSignalSpacer.setVisibility(View.GONE); } mMobile.setPaddingRelative(mIsMobileTypeIconWide ? mWideTypeIconStartPadding : 0, 0, 0, 0); if (DEBUG) Log.d(TAG, String.format("mobile: %s sig=%d typ=%d", (mMobileVisible ? "VISIBLE" : "GONE"), mMobileStrengthId, mMobileTypeId)); mMobileType.setVisibility((mRoaming || mMobileTypeId != 0) ? View.VISIBLE : View.GONE); } }