Kensuke Matsui | 21d1bf1 | 2017-03-14 13:27:20 +0900 | [diff] [blame] | 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; |
Kensuke Matsui | 21d1bf1 | 2017-03-14 13:27:20 +0900 | [diff] [blame] | 18 | import android.graphics.Rect; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 19 | import android.net.ConnectivityManager; |
Kensuke Matsui | 21d1bf1 | 2017-03-14 13:27:20 +0900 | [diff] [blame] | 20 | import android.os.Bundle; |
Kensuke Matsui | 21d1bf1 | 2017-03-14 13:27:20 +0900 | [diff] [blame] | 21 | import android.telephony.ServiceState; |
| 22 | import android.telephony.SubscriptionInfo; |
| 23 | import android.text.TextUtils; |
| 24 | import android.util.AttributeSet; |
| 25 | import android.widget.TextView; |
| 26 | |
| 27 | import com.android.internal.telephony.IccCardConstants.State; |
| 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; |
Beverly | 1be62f4 | 2018-12-19 17:17:48 -0500 | [diff] [blame] | 33 | import com.android.systemui.plugins.DarkIconDispatcher; |
| 34 | import com.android.systemui.plugins.DarkIconDispatcher.DarkReceiver; |
Kensuke Matsui | 21d1bf1 | 2017-03-14 13:27:20 +0900 | [diff] [blame] | 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 | |
| 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 Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame^] | 137 | List<SubscriptionInfo> subs = mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(false); |
Kensuke Matsui | 21d1bf1 | 2017-03-14 13:27:20 +0900 | [diff] [blame] | 138 | 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 | } |