blob: f2c0434a1a957fc40f87fd2381d7c3e21058928e [file] [log] [blame]
Adrian Roos316bf542016-08-23 17:53:07 +02001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.systemui.statusbar.policy;
18
19import android.annotation.Nullable;
Adrian Roos72abcda2016-12-06 14:48:43 -080020import android.content.BroadcastReceiver;
Adrian Roos316bf542016-08-23 17:53:07 +020021import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
24import android.net.ConnectivityManager;
25import android.provider.Settings;
Adrian Roos316bf542016-08-23 17:53:07 +020026import android.telephony.SubscriptionInfo;
27import android.text.TextUtils;
28import android.util.AttributeSet;
Adrian Roos316bf542016-08-23 17:53:07 +020029import android.widget.TextView;
30
31import com.android.internal.telephony.IccCardConstants;
32import com.android.internal.telephony.TelephonyIntents;
33import com.android.keyguard.KeyguardUpdateMonitor;
34import com.android.keyguard.KeyguardUpdateMonitorCallback;
35
36import java.util.List;
37
38public class EmergencyCryptkeeperText extends TextView {
39
40 private KeyguardUpdateMonitor mKeyguardUpdateMonitor;
Adrian Roos72abcda2016-12-06 14:48:43 -080041 private final KeyguardUpdateMonitorCallback mCallback = new KeyguardUpdateMonitorCallback() {
Adrian Roos316bf542016-08-23 17:53:07 +020042 @Override
43 public void onPhoneStateChanged(int phoneState) {
44 update();
45 }
Akira Oshimi76c36c42017-01-31 14:34:59 +090046
47 @Override
48 public void onRefreshCarrierInfo() {
49 update();
50 }
Adrian Roos316bf542016-08-23 17:53:07 +020051 };
Adrian Roos72abcda2016-12-06 14:48:43 -080052 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
53 @Override
54 public void onReceive(Context context, Intent intent) {
55 if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())) {
56 update();
57 }
58 }
59 };
Adrian Roos316bf542016-08-23 17:53:07 +020060
61 public EmergencyCryptkeeperText(Context context, @Nullable AttributeSet attrs) {
62 super(context, attrs);
63 setVisibility(GONE);
64 }
65
66 @Override
67 protected void onAttachedToWindow() {
68 super.onAttachedToWindow();
69 mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
70 mKeyguardUpdateMonitor.registerCallback(mCallback);
Adrian Roos72abcda2016-12-06 14:48:43 -080071 getContext().registerReceiver(mReceiver,
72 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
Adrian Roos316bf542016-08-23 17:53:07 +020073 update();
74 }
75
76 @Override
77 protected void onDetachedFromWindow() {
78 super.onDetachedFromWindow();
79 if (mKeyguardUpdateMonitor != null) {
80 mKeyguardUpdateMonitor.removeCallback(mCallback);
81 }
Adrian Roos72abcda2016-12-06 14:48:43 -080082 getContext().unregisterReceiver(mReceiver);
Adrian Roos316bf542016-08-23 17:53:07 +020083 }
84
85 public void update() {
86 boolean hasMobile = ConnectivityManager.from(mContext)
87 .isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
88 boolean airplaneMode = (Settings.Global.getInt(mContext.getContentResolver(),
89 Settings.Global.AIRPLANE_MODE_ON, 0) == 1);
90
91 if (!hasMobile || airplaneMode) {
92 setText(null);
93 setVisibility(GONE);
94 return;
95 }
96
97 boolean allSimsMissing = true;
98 CharSequence displayText = null;
99
Malcolm Chen5c63b512019-08-13 13:24:07 -0700100 List<SubscriptionInfo> subs = mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(false);
Adrian Roos316bf542016-08-23 17:53:07 +0200101 final int N = subs.size();
102 for (int i = 0; i < N; i++) {
103 int subId = subs.get(i).getSubscriptionId();
104 IccCardConstants.State simState = mKeyguardUpdateMonitor.getSimState(subId);
105 CharSequence carrierName = subs.get(i).getCarrierName();
106 if (simState.iccCardExist() && !TextUtils.isEmpty(carrierName)) {
107 allSimsMissing = false;
108 displayText = carrierName;
109 }
110 }
111 if (allSimsMissing) {
112 if (N != 0) {
113 // Shows "Emergency calls only" on devices that are voice-capable.
114 // This depends on mPlmn containing the text "Emergency calls only" when the radio
115 // has some connectivity. Otherwise it should show "No service"
116 // Grab the first subscription, because they all should contain the emergency text,
117 // described above.
118 displayText = subs.get(0).getCarrierName();
119 } else {
120 // We don't have a SubscriptionInfo to get the emergency calls only from.
121 // Grab it from the old sticky broadcast if possible instead. We can use it
122 // here because no subscriptions are active, so we don't have
123 // to worry about MSIM clashing.
124 displayText = getContext().getText(
125 com.android.internal.R.string.emergency_calls_only);
126 Intent i = getContext().registerReceiver(null,
127 new IntentFilter(TelephonyIntents.SPN_STRINGS_UPDATED_ACTION));
128 if (i != null) {
129 displayText = i.getStringExtra(TelephonyIntents.EXTRA_PLMN);
130 }
131 }
132 }
133
134 setText(displayText);
135 setVisibility(TextUtils.isEmpty(displayText) ? GONE : VISIBLE);
136 }
137}