blob: 2a5ccdb0c0a7aa64660822ba7611fba3f30eda5d [file] [log] [blame]
Kensuke Matsui21d1bf12017-03-14 13:27:20 +09001/*
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
15package com.android.systemui.statusbar;
16
17import android.content.Context;
Kensuke Matsui21d1bf12017-03-14 13:27:20 +090018import android.graphics.Rect;
Gus Prevasab336792018-11-14 13:52:20 -050019import android.net.ConnectivityManager;
Kensuke Matsui21d1bf12017-03-14 13:27:20 +090020import android.os.Bundle;
Kensuke Matsui21d1bf12017-03-14 13:27:20 +090021import android.telephony.ServiceState;
22import android.telephony.SubscriptionInfo;
23import android.text.TextUtils;
24import android.util.AttributeSet;
25import android.widget.TextView;
26
27import com.android.internal.telephony.IccCardConstants.State;
28import com.android.keyguard.KeyguardUpdateMonitor;
29import com.android.keyguard.KeyguardUpdateMonitorCallback;
30import com.android.settingslib.WirelessUtils;
31import com.android.systemui.DemoMode;
32import com.android.systemui.Dependency;
Beverly1be62f42018-12-19 17:17:48 -050033import com.android.systemui.plugins.DarkIconDispatcher;
34import com.android.systemui.plugins.DarkIconDispatcher.DarkReceiver;
Kensuke Matsui21d1bf12017-03-14 13:27:20 +090035import com.android.systemui.statusbar.policy.NetworkController;
36import com.android.systemui.statusbar.policy.NetworkController.IconState;
37import com.android.systemui.statusbar.policy.NetworkController.SignalCallback;
38import com.android.systemui.tuner.TunerService;
39import com.android.systemui.tuner.TunerService.Tunable;
40
41import java.util.List;
42
43public 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
58 public OperatorNameView(Context context) {
59 this(context, null);
60 }
61
62 public OperatorNameView(Context context, AttributeSet attrs) {
63 this(context, attrs, 0);
64 }
65
66 public OperatorNameView(Context context, AttributeSet attrs, int defStyle) {
67 super(context, attrs, defStyle);
68 }
69
70 @Override
71 protected void onAttachedToWindow() {
72 super.onAttachedToWindow();
73 mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
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
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
90 public void onDarkChanged(Rect area, float darkIntensity, int tint) {
91 setTextColor(DarkIconDispatcher.getTint(area, this, tint));
92 }
93
94 @Override
95 public void setIsAirplaneMode(IconState icon) {
96 update();
97 }
98
99 @Override
100 public void onTuningChanged(String key, String newValue) {
101 update();
102 }
103
104 @Override
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
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
135 private void updateText() {
136 CharSequence displayText = null;
Malcolm Chen5c63b512019-08-13 13:24:07 -0700137 List<SubscriptionInfo> subs = mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(false);
Kensuke Matsui21d1bf12017-03-14 13:27:20 +0900138 final int N = subs.size();
139 for (int i = 0; i < N; i++) {
140 int subId = subs.get(i).getSubscriptionId();
141 State simState = mKeyguardUpdateMonitor.getSimState(subId);
142 CharSequence carrierName = subs.get(i).getCarrierName();
143 if (!TextUtils.isEmpty(carrierName) && simState == 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}