1 /*
2  * Copyright (C) 2017 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 
15 package com.android.systemui.statusbar;
16 
17 import android.content.Context;
18 import android.graphics.Rect;
19 import android.net.ConnectivityManager;
20 import android.os.Bundle;
21 import android.telephony.ServiceState;
22 import android.telephony.SubscriptionInfo;
23 import android.telephony.TelephonyManager;
24 import android.text.TextUtils;
25 import android.util.AttributeSet;
26 import android.widget.TextView;
27 
28 import com.android.keyguard.KeyguardUpdateMonitor;
29 import com.android.keyguard.KeyguardUpdateMonitorCallback;
30 import com.android.settingslib.WirelessUtils;
31 import com.android.systemui.DemoMode;
32 import com.android.systemui.Dependency;
33 import com.android.systemui.plugins.DarkIconDispatcher;
34 import com.android.systemui.plugins.DarkIconDispatcher.DarkReceiver;
35 import com.android.systemui.statusbar.policy.NetworkController;
36 import com.android.systemui.statusbar.policy.NetworkController.IconState;
37 import com.android.systemui.statusbar.policy.NetworkController.SignalCallback;
38 import com.android.systemui.tuner.TunerService;
39 import com.android.systemui.tuner.TunerService.Tunable;
40 
41 import java.util.List;
42 
43 public class OperatorNameView extends TextView implements DemoMode, DarkReceiver,
44         SignalCallback, Tunable {
45 
46     private static final String KEY_SHOW_OPERATOR_NAME = "show_operator_name";
47 
48     private KeyguardUpdateMonitor mKeyguardUpdateMonitor;
49     private boolean mDemoMode;
50 
51     private final KeyguardUpdateMonitorCallback mCallback = new KeyguardUpdateMonitorCallback() {
52         @Override
53         public void onRefreshCarrierInfo() {
54             updateText();
55         }
56     };
57 
OperatorNameView(Context context)58     public OperatorNameView(Context context) {
59         this(context, null);
60     }
61 
OperatorNameView(Context context, AttributeSet attrs)62     public OperatorNameView(Context context, AttributeSet attrs) {
63         this(context, attrs, 0);
64     }
65 
OperatorNameView(Context context, AttributeSet attrs, int defStyle)66     public OperatorNameView(Context context, AttributeSet attrs, int defStyle) {
67         super(context, attrs, defStyle);
68     }
69 
70     @Override
onAttachedToWindow()71     protected void onAttachedToWindow() {
72         super.onAttachedToWindow();
73         mKeyguardUpdateMonitor = Dependency.get(KeyguardUpdateMonitor.class);
74         mKeyguardUpdateMonitor.registerCallback(mCallback);
75         Dependency.get(DarkIconDispatcher.class).addDarkReceiver(this);
76         Dependency.get(NetworkController.class).addCallback(this);
77         Dependency.get(TunerService.class).addTunable(this, KEY_SHOW_OPERATOR_NAME);
78     }
79 
80     @Override
onDetachedFromWindow()81     protected void onDetachedFromWindow() {
82         super.onDetachedFromWindow();
83         mKeyguardUpdateMonitor.removeCallback(mCallback);
84         Dependency.get(DarkIconDispatcher.class).removeDarkReceiver(this);
85         Dependency.get(NetworkController.class).removeCallback(this);
86         Dependency.get(TunerService.class).removeTunable(this);
87     }
88 
89     @Override
onDarkChanged(Rect area, float darkIntensity, int tint)90     public void onDarkChanged(Rect area, float darkIntensity, int tint) {
91         setTextColor(DarkIconDispatcher.getTint(area, this, tint));
92     }
93 
94     @Override
setIsAirplaneMode(IconState icon)95     public void setIsAirplaneMode(IconState icon) {
96         update();
97     }
98 
99     @Override
onTuningChanged(String key, String newValue)100     public void onTuningChanged(String key, String newValue) {
101         update();
102     }
103 
104     @Override
dispatchDemoCommand(String command, Bundle args)105     public void dispatchDemoCommand(String command, Bundle args) {
106         if (!mDemoMode && command.equals(COMMAND_ENTER)) {
107             mDemoMode = true;
108         } else if (mDemoMode && command.equals(COMMAND_EXIT)) {
109             mDemoMode = false;
110             update();
111         } else if (mDemoMode && command.equals(COMMAND_OPERATOR)) {
112             setText(args.getString("name"));
113         }
114     }
115 
update()116     private void update() {
117         boolean showOperatorName = Dependency.get(TunerService.class)
118                 .getValue(KEY_SHOW_OPERATOR_NAME, 1) != 0;
119         setVisibility(showOperatorName ? VISIBLE : GONE);
120 
121         boolean hasMobile = ConnectivityManager.from(mContext)
122                 .isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
123         boolean airplaneMode = WirelessUtils.isAirplaneModeOn(mContext);
124         if (!hasMobile || airplaneMode) {
125             setText(null);
126             setVisibility(GONE);
127             return;
128         }
129 
130         if (!mDemoMode) {
131             updateText();
132         }
133     }
134 
updateText()135     private void updateText() {
136         CharSequence displayText = null;
137         List<SubscriptionInfo> subs = mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(false);
138         final int N = subs.size();
139         for (int i = 0; i < N; i++) {
140             int subId = subs.get(i).getSubscriptionId();
141             int simState = mKeyguardUpdateMonitor.getSimState(subId);
142             CharSequence carrierName = subs.get(i).getCarrierName();
143             if (!TextUtils.isEmpty(carrierName) && simState == TelephonyManager.SIM_STATE_READY) {
144                 ServiceState ss = mKeyguardUpdateMonitor.getServiceState(subId);
145                 if (ss != null && ss.getState() == ServiceState.STATE_IN_SERVICE) {
146                     displayText = carrierName;
147                     break;
148                 }
149             }
150         }
151 
152         setText(displayText);
153     }
154 }
155