Nick0703

SignalClusterView.java

Oct 24th, 2013
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.03 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2011 The Android Open Source Project
  3.  * This code has been modified. Portions copyright (C) 2012, ParanoidAndroid Project.
  4.  * Copyright (c) 2012-2013 The Linux Foundation. All rights reserved.
  5.  *
  6.  * Not a Contribution.
  7.  *
  8.  * Licensed under the Apache License, Version 2.0 (the "License");
  9.  * you may not use this file except in compliance with the License.
  10.  * You may obtain a copy of the License at
  11.  *
  12.  *      http://www.apache.org/licenses/LICENSE-2.0
  13.  *
  14.  * Unless required by applicable law or agreed to in writing, software
  15.  * distributed under the License is distributed on an "AS IS" BASIS,
  16.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17.  * See the License for the specific language governing permissions and
  18.  * limitations under the License.
  19.  */
  20.  
  21. package com.android.systemui.statusbar;
  22.  
  23. import android.content.ContentResolver;
  24. import android.content.Context;
  25. import android.database.ContentObserver;
  26. import android.graphics.drawable.Drawable;
  27. import android.graphics.PorterDuff;
  28. import android.os.Handler;
  29. import android.provider.Settings;
  30. import android.util.AttributeSet;
  31. import android.util.ColorUtils;
  32. import android.util.Slog;
  33. import android.view.View;
  34. import android.view.ViewGroup;
  35. import android.view.accessibility.AccessibilityEvent;
  36. import android.widget.ImageView;
  37. import android.widget.LinearLayout;
  38. import android.widget.TextView;
  39.  
  40. import com.android.systemui.R;
  41. import com.android.systemui.statusbar.policy.NetworkController;
  42.  
  43. // Intimately tied to the design of res/layout/signal_cluster_view.xml
  44. public class SignalClusterView
  45.         extends LinearLayout
  46.         implements NetworkController.SignalCluster {
  47.  
  48.     static final boolean DEBUG = false;
  49.     static final String TAG = "SignalClusterView";
  50.  
  51.     private static final int EVENT_SIGNAL_STRENGTH_CHANGED = 200;
  52.  
  53.     NetworkController mNC;
  54.  
  55.     private boolean mWifiVisible = false;
  56.     private int mWifiStrengthId = 0, mWifiActivityId = 0;
  57.     private boolean mMobileVisible = false;
  58.     private int mMobileStrengthId = 0, mMobileActivityId = 0;
  59.     private int mMobileTypeId = 0, mNoSimIconId = 0;
  60.     private boolean mIsAirplaneMode = false;
  61.     private int mAirplaneIconId = 0;
  62.     private String mWifiDescription, mMobileDescription, mMobileTypeDescription;
  63.     private static final int STYLE_HIDE = 0;
  64.     private static final int STYLE_SHOW = 1;
  65.     private static final int STYLE_SHOW_DBM = 2;
  66.  
  67.     private boolean showingSignalText = false;
  68.     private boolean showingWiFiText = false;
  69.     private boolean showingAltCluster = false;
  70.  
  71.     ViewGroup mWifiGroup, mMobileGroup;
  72.     ImageView mWifi, mMobile, mWifiActivity, mMobileActivity, mMobileType, mAirplane, mEther, mNoSimSlot;
  73.     TextView mMobileText,mWiFiText;
  74.     View mSpacer;
  75.  
  76.     private ColorUtils.ColorSettingInfo mColorInfo;
  77.  
  78.     Handler mHandler;
  79.  
  80.     private SettingsObserver mSettingsObserver;
  81.  
  82.     public SignalClusterView(Context context) {
  83.         this(context, null);
  84.     }
  85.  
  86.     public SignalClusterView(Context context, AttributeSet attrs) {
  87.         this(context, attrs, 0);
  88.     }
  89.  
  90.     public SignalClusterView(Context context, AttributeSet attrs, int defStyle) {
  91.         super(context, attrs, defStyle);
  92.         mColorInfo = ColorUtils.getColorSettingInfo(context, Settings.System.STATUS_ICON_COLOR);
  93.         mSettingsObserver = new SettingsObserver(mHandler);
  94.     }
  95.  
  96.     public void setNetworkController(NetworkController nc) {
  97.         if (DEBUG) Slog.d(TAG, "NetworkController=" + nc);
  98.         mNC = nc;
  99.     }
  100.  
  101.     @Override
  102.     protected void onAttachedToWindow() {
  103.         super.onAttachedToWindow();
  104.  
  105.         mWifiGroup      = (ViewGroup) findViewById(R.id.wifi_combo);
  106.         mWifi           = (ImageView) findViewById(R.id.wifi_signal);
  107.         mWifiActivity   = (ImageView) findViewById(R.id.wifi_inout);
  108.         mMobileGroup    = (ViewGroup) findViewById(R.id.mobile_combo);
  109.         mMobile         = (ImageView) findViewById(R.id.mobile_signal);
  110.         mMobileActivity = (ImageView) findViewById(R.id.mobile_inout);
  111.         mMobileType     = (ImageView) findViewById(R.id.mobile_type);
  112.         mMobileText     = (TextView)  findViewById(R.id.signal_text);
  113.         mNoSimSlot      = (ImageView) findViewById(R.id.no_sim);
  114.         mWiFiText       = (TextView)  findViewById(R.id.wifi_signal_text);
  115.         mSpacer         =             findViewById(R.id.spacer);
  116.         mAirplane       = (ImageView) findViewById(R.id.airplane);
  117.  
  118.         mHandler = new Handler();
  119.  
  120.         mSettingsObserver.observe();
  121.  
  122.         apply();
  123.     }
  124.  
  125.     @Override
  126.     protected void onDetachedFromWindow() {
  127.         mWifiGroup      = null;
  128.         mWifi           = null;
  129.         mWifiActivity   = null;
  130.         mMobileGroup    = null;
  131.         mMobile         = null;
  132.         mMobileActivity = null;
  133.         mMobileType     = null;
  134.         mMobileText     = null;
  135.         mNoSimSlot      = null;
  136.         mWiFiText       = null;
  137.         mSpacer         = null;
  138.         mAirplane       = null;
  139.  
  140.         mContext.getContentResolver().unregisterContentObserver(mSettingsObserver);
  141.  
  142.         super.onDetachedFromWindow();
  143.     }
  144.  
  145.     @Override
  146.     public void setWifiIndicators(boolean visible, int strengthIcon, int activityIcon,
  147.             String contentDescription) {
  148.         mWifiVisible = visible;
  149.         mWifiStrengthId = strengthIcon;
  150.         mWifiActivityId = activityIcon;
  151.         mWifiDescription = contentDescription;
  152.  
  153.         apply();
  154.     }
  155.  
  156.     @Override
  157.     public void setMobileDataIndicators(boolean visible, int strengthIcon, int activityIcon,
  158.             int typeIcon, String contentDescription, String typeContentDescription,
  159.             int noSimIcon) {
  160.         mMobileVisible = visible;
  161.         mMobileStrengthId = strengthIcon;
  162.         mMobileActivityId = activityIcon;
  163.         mMobileTypeId = typeIcon;
  164.         mMobileDescription = contentDescription;
  165.         mMobileTypeDescription = typeContentDescription;
  166.         mNoSimIconId = noSimIcon;
  167.  
  168.         apply();
  169.     }
  170.  
  171.     @Override
  172.     public void setIsAirplaneMode(boolean is, int airplaneIconId) {
  173.         mIsAirplaneMode = is;
  174.         mAirplaneIconId = airplaneIconId;
  175.  
  176.         apply();
  177.     }
  178.  
  179.     @Override
  180.     public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
  181.         // Standard group layout onPopulateAccessibilityEvent() implementations
  182.         // ignore content description, so populate manually
  183.         if (mWifiVisible && mWifiGroup.getContentDescription() != null) {
  184.             event.getText().add(mWifiGroup.getContentDescription());
  185.         }
  186.         if (mMobileVisible && mMobileGroup.getContentDescription() != null) {
  187.             event.getText().add(mMobileGroup.getContentDescription());
  188.         }
  189.         return super.dispatchPopulateAccessibilityEvent(event);
  190.     }
  191.  
  192.     public void setColor(ColorUtils.ColorSettingInfo colorInfo) {
  193.         mColorInfo = colorInfo;
  194.         apply();
  195.     }
  196.  
  197.     @Override
  198.     public void onRtlPropertiesChanged(int layoutDirection) {
  199.         super.onRtlPropertiesChanged(layoutDirection);
  200.  
  201.         if (mWifi != null) {
  202.             mWifi.setImageDrawable(null);
  203.         }
  204.         if (mWifiActivity != null) {
  205.             mWifiActivity.setImageDrawable(null);
  206.         }
  207.  
  208.         if (mMobile != null) {
  209.             mMobile.setImageDrawable(null);
  210.         }
  211.         if (mMobileActivity != null) {
  212.             mMobileActivity.setImageDrawable(null);
  213.         }
  214.         if (mMobileType != null) {
  215.             mMobileType.setImageDrawable(null);
  216.         }
  217.  
  218.         if(mAirplane != null) {
  219.             mAirplane.setImageDrawable(null);
  220.         }
  221.     }
  222.  
  223.     // Run after each indicator change.
  224.     public void apply() {
  225.         if (mWifiGroup == null) return;
  226.  
  227.         if (mWifiVisible) {
  228.             mWifiGroup.setVisibility(View.VISIBLE);
  229.             Drawable wifiBitmap = mContext.getResources().getDrawable(mWifiStrengthId);
  230.             if (mColorInfo.isLastColorNull) {
  231.                 wifiBitmap.clearColorFilter();
  232.             } else {
  233.                 wifiBitmap.setColorFilter(mColorInfo.lastColor, PorterDuff.Mode.SRC_IN);
  234.             }
  235.             mWifi.setImageDrawable(wifiBitmap);
  236.             mWifiActivity.setImageResource(mWifiActivityId);
  237.  
  238.             mWifiGroup.setContentDescription(mWifiDescription);
  239.             if (showingWiFiText){
  240.                 mWifi.setVisibility(View.GONE);
  241.                 mWifiActivity.setVisibility(View.GONE);
  242.                 mWiFiText.setVisibility(View.VISIBLE);
  243.             } else {
  244.                 mWifi.setVisibility(View.VISIBLE);
  245.                 mWifiActivity.setVisibility(View.VISIBLE);
  246.                 mWiFiText.setVisibility(View.GONE);
  247.             }
  248.         } else {
  249.             mWifiGroup.setVisibility(View.GONE);
  250.         }
  251.  
  252.         if (DEBUG) Slog.d(TAG,
  253.                 String.format("wifi: %s sig=%d act=%d",
  254.                     (mWifiVisible ? "VISIBLE" : "GONE"),
  255.                     mWifiStrengthId, mWifiActivityId));
  256.  
  257.         if (mMobileVisible && !mIsAirplaneMode) {
  258.             mMobileGroup.setVisibility(View.VISIBLE);
  259.             if(mMobileStrengthId != 0) {
  260.                 Drawable mobileBitmap = mContext.getResources().getDrawable(mMobileStrengthId);
  261.                 if (mColorInfo.isLastColorNull) {
  262.                     mobileBitmap.clearColorFilter();
  263.                 } else {
  264.                     mobileBitmap.setColorFilter(mColorInfo.lastColor, PorterDuff.Mode.SRC_IN);
  265.                 }
  266.                 mMobile.setImageDrawable(mobileBitmap);
  267.             }
  268.             if(mMobileActivityId != 0 && showingAltCluster) {
  269.                 Drawable mobileActivityBitmap = mContext.getResources().getDrawable(mMobileActivityId);
  270.                 if (mColorInfo.isLastColorNull) {
  271.                     mobileActivityBitmap.clearColorFilter();
  272.                 } else {
  273.                     mobileActivityBitmap.setColorFilter(mColorInfo.lastColor, PorterDuff.Mode.SRC_IN);
  274.                 }
  275.                 mMobileActivity.setImageDrawable(mobileActivityBitmap);
  276.             }
  277.             if(mMobileTypeId != 0) {
  278.                 Drawable mobileTypeBitmap = mContext.getResources().getDrawable(mMobileTypeId);
  279.                 if (mColorInfo.isLastColorNull) {
  280.                     mobileTypeBitmap.clearColorFilter();
  281.                 } else {
  282.                     mobileTypeBitmap.setColorFilter(mColorInfo.lastColor, PorterDuff.Mode.SRC_IN);
  283.                 }
  284.                 mMobileType.setImageDrawable(mobileTypeBitmap);
  285.             }
  286.             mMobile.setImageResource(mMobileStrengthId);
  287.             mMobileActivity.setImageResource(mMobileActivityId);
  288.             mMobileType.setImageResource(mMobileTypeId);
  289.  
  290.             mMobileGroup.setContentDescription(mMobileTypeDescription + " " + mMobileDescription);
  291.             if (showingSignalText && !mIsAirplaneMode) {
  292.                 mMobile.setVisibility(View.GONE);
  293.                 mMobileText.setVisibility(View.VISIBLE);
  294.             } else {
  295.                 mMobileGroup.setVisibility(View.GONE);
  296.         }
  297.  
  298.         if (mIsAirplaneMode) {
  299.             mAirplane.setVisibility(View.VISIBLE);
  300.             if(mAirplaneIconId != 0) {
  301.                 Drawable AirplaneBitmap = mContext.getResources().getDrawable(mAirplaneIconId);
  302.                 if (mColorInfo.isLastColorNull) {
  303.                     mAirplane.clearColorFilter();
  304.                 } else {
  305.                     mAirplane.setColorFilter(mColorInfo.lastColor, PorterDuff.Mode.SRC_IN);
  306.                 }
  307.                 mAirplane.setImageDrawable(AirplaneBitmap);
  308.             }
  309.             mAirplane.setImageResource(mAirplaneIconId);
  310.             mAirplane.setVisibility(View.VISIBLE);
  311.         } else {
  312.             mAirplane.setVisibility(View.GONE);
  313.         }
  314.  
  315.         if (mMobileVisible && mWifiVisible &&
  316.                 ((mIsAirplaneMode) || (mNoSimIconId != 0))) {
  317.             mSpacer.setVisibility(View.INVISIBLE);
  318.         } else {
  319.             mSpacer.setVisibility(View.GONE);
  320.         }
  321.  
  322.         if (DEBUG) Slog.d(TAG,
  323.                 String.format("mobile: %s sig=%d act=%d typ=%d",
  324.                     (mMobileVisible ? "VISIBLE" : "GONE"),
  325.                     mMobileStrengthId, mMobileActivityId, mMobileTypeId));
  326.  
  327.         mMobileType.setVisibility(
  328.                 !mWifiVisible ? View.VISIBLE : View.GONE);
  329.         if (showingAltCluster) {
  330.             this.setVisibility((this.getId() == R.id.signal_cluster) ? View.GONE : View.VISIBLE);
  331.         } else {
  332.             this.setVisibility((this.getId() == R.id.signal_cluster) ? View.VISIBLE : View.GONE);
  333.         }
  334.     }
  335.  
  336.     class SettingsObserver extends ContentObserver {
  337.         SettingsObserver(Handler handler) {
  338.             super(handler);
  339.         }
  340.  
  341.         void observe() {
  342.             ContentResolver resolver = mContext.getContentResolver();
  343.             resolver.registerContentObserver(
  344.                     Settings.System.getUriFor(Settings.System.STATUSBAR_SIGNAL_TEXT), false,
  345.                     this);
  346.             resolver.registerContentObserver(
  347.                     Settings.System.getUriFor(Settings.System.STATUSBAR_WIFI_SIGNAL_TEXT), false,
  348.                     this);
  349.             resolver.registerContentObserver(
  350.                     Settings.System.getUriFor(Settings.System.STATUSBAR_SIGNAL_CLUSTER_ALT), false,
  351.                     this);
  352.             updateSettings();
  353.         }
  354.  
  355.         @Override
  356.         public void onChange(boolean selfChange) {
  357.             updateSettings();
  358.         }
  359.     }
  360.    
  361.     protected void updateSettings() {
  362.         ContentResolver resolver = mContext.getContentResolver();
  363.  
  364.         showingSignalText = Settings.System.getInt(resolver,
  365.                 Settings.System.STATUSBAR_SIGNAL_TEXT, 0) != 0;
  366.         showingWiFiText = Settings.System.getInt(resolver,
  367.                 Settings.System.STATUSBAR_WIFI_SIGNAL_TEXT, 0) != 0;
  368.         boolean clustdefault = getResources().getBoolean(R.bool.statusbar_alt_signal_layout);
  369.         showingAltCluster = Settings.System.getBoolean(resolver,
  370.                 Settings.System.STATUSBAR_SIGNAL_CLUSTER_ALT, clustdefault);
  371.         apply();
  372.     }
  373.   }
  374. }
Advertisement
Add Comment
Please, Sign In to add comment