Adrian Roos | 316bf54 | 2016-08-23 17:53:07 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.statusbar.policy; |
| 18 | |
| 19 | import android.annotation.Nullable; |
Adrian Roos | 72abcda | 2016-12-06 14:48:43 -0800 | [diff] [blame] | 20 | import android.content.BroadcastReceiver; |
Adrian Roos | 316bf54 | 2016-08-23 17:53:07 +0200 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
| 24 | import android.net.ConnectivityManager; |
| 25 | import android.provider.Settings; |
Adrian Roos | 316bf54 | 2016-08-23 17:53:07 +0200 | [diff] [blame] | 26 | import android.telephony.SubscriptionInfo; |
| 27 | import android.text.TextUtils; |
| 28 | import android.util.AttributeSet; |
Adrian Roos | 316bf54 | 2016-08-23 17:53:07 +0200 | [diff] [blame] | 29 | import android.widget.TextView; |
| 30 | |
| 31 | import com.android.internal.telephony.IccCardConstants; |
| 32 | import com.android.internal.telephony.TelephonyIntents; |
| 33 | import com.android.keyguard.KeyguardUpdateMonitor; |
| 34 | import com.android.keyguard.KeyguardUpdateMonitorCallback; |
| 35 | |
| 36 | import java.util.List; |
| 37 | |
| 38 | public class EmergencyCryptkeeperText extends TextView { |
| 39 | |
| 40 | private KeyguardUpdateMonitor mKeyguardUpdateMonitor; |
Adrian Roos | 72abcda | 2016-12-06 14:48:43 -0800 | [diff] [blame] | 41 | private final KeyguardUpdateMonitorCallback mCallback = new KeyguardUpdateMonitorCallback() { |
Adrian Roos | 316bf54 | 2016-08-23 17:53:07 +0200 | [diff] [blame] | 42 | @Override |
| 43 | public void onPhoneStateChanged(int phoneState) { |
| 44 | update(); |
| 45 | } |
Akira Oshimi | 76c36c4 | 2017-01-31 14:34:59 +0900 | [diff] [blame] | 46 | |
| 47 | @Override |
| 48 | public void onRefreshCarrierInfo() { |
| 49 | update(); |
| 50 | } |
Adrian Roos | 316bf54 | 2016-08-23 17:53:07 +0200 | [diff] [blame] | 51 | }; |
Adrian Roos | 72abcda | 2016-12-06 14:48:43 -0800 | [diff] [blame] | 52 | 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 Roos | 316bf54 | 2016-08-23 17:53:07 +0200 | [diff] [blame] | 60 | |
| 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 Roos | 72abcda | 2016-12-06 14:48:43 -0800 | [diff] [blame] | 71 | getContext().registerReceiver(mReceiver, |
| 72 | new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED)); |
Adrian Roos | 316bf54 | 2016-08-23 17:53:07 +0200 | [diff] [blame] | 73 | update(); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | protected void onDetachedFromWindow() { |
| 78 | super.onDetachedFromWindow(); |
| 79 | if (mKeyguardUpdateMonitor != null) { |
| 80 | mKeyguardUpdateMonitor.removeCallback(mCallback); |
| 81 | } |
Adrian Roos | 72abcda | 2016-12-06 14:48:43 -0800 | [diff] [blame] | 82 | getContext().unregisterReceiver(mReceiver); |
Adrian Roos | 316bf54 | 2016-08-23 17:53:07 +0200 | [diff] [blame] | 83 | } |
| 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 Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame^] | 100 | List<SubscriptionInfo> subs = mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(false); |
Adrian Roos | 316bf54 | 2016-08-23 17:53:07 +0200 | [diff] [blame] | 101 | 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 | } |