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; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.IntentFilter; 23 import android.net.ConnectivityManager; 24 import android.provider.Settings; 25 import android.telephony.ServiceState; 26 import android.telephony.SubscriptionInfo; 27 import android.text.TextUtils; 28 import android.util.AttributeSet; 29 import android.util.Log; 30 import android.view.ViewGroup; 31 import android.view.ViewParent; 32 import android.widget.TextView; 33 34 import com.android.internal.telephony.IccCardConstants; 35 import com.android.internal.telephony.TelephonyIntents; 36 import com.android.keyguard.KeyguardUpdateMonitor; 37 import com.android.keyguard.KeyguardUpdateMonitorCallback; 38 39 import java.util.List; 40 41 public class EmergencyCryptkeeperText extends TextView { 42 43 private KeyguardUpdateMonitor mKeyguardUpdateMonitor; 44 private KeyguardUpdateMonitorCallback mCallback = new KeyguardUpdateMonitorCallback() { 45 @Override 46 public void onPhoneStateChanged(int phoneState) { 47 update(); 48 } 49 }; 50 51 public EmergencyCryptkeeperText(Context context, @Nullable AttributeSet attrs) { 52 super(context, attrs); 53 setVisibility(GONE); 54 } 55 56 @Override 57 protected void onAttachedToWindow() { 58 super.onAttachedToWindow(); 59 mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(mContext); 60 mKeyguardUpdateMonitor.registerCallback(mCallback); 61 update(); 62 } 63 64 @Override 65 protected void onDetachedFromWindow() { 66 super.onDetachedFromWindow(); 67 if (mKeyguardUpdateMonitor != null) { 68 mKeyguardUpdateMonitor.removeCallback(mCallback); 69 } 70 } 71 72 public void update() { 73 boolean hasMobile = ConnectivityManager.from(mContext) 74 .isNetworkSupported(ConnectivityManager.TYPE_MOBILE); 75 boolean airplaneMode = (Settings.Global.getInt(mContext.getContentResolver(), 76 Settings.Global.AIRPLANE_MODE_ON, 0) == 1); 77 78 if (!hasMobile || airplaneMode) { 79 setText(null); 80 setVisibility(GONE); 81 return; 82 } 83 84 boolean allSimsMissing = true; 85 CharSequence displayText = null; 86 87 List<SubscriptionInfo> subs = mKeyguardUpdateMonitor.getSubscriptionInfo(false); 88 final int N = subs.size(); 89 for (int i = 0; i < N; i++) { 90 int subId = subs.get(i).getSubscriptionId(); 91 IccCardConstants.State simState = mKeyguardUpdateMonitor.getSimState(subId); 92 CharSequence carrierName = subs.get(i).getCarrierName(); 93 if (simState.iccCardExist() && !TextUtils.isEmpty(carrierName)) { 94 allSimsMissing = false; 95 displayText = carrierName; 96 } 97 } 98 if (allSimsMissing) { 99 if (N != 0) { 100 // Shows "Emergency calls only" on devices that are voice-capable. 101 // This depends on mPlmn containing the text "Emergency calls only" when the radio 102 // has some connectivity. Otherwise it should show "No service" 103 // Grab the first subscription, because they all should contain the emergency text, 104 // described above. 105 displayText = subs.get(0).getCarrierName(); 106 } else { 107 // We don't have a SubscriptionInfo to get the emergency calls only from. 108 // Grab it from the old sticky broadcast if possible instead. We can use it 109 // here because no subscriptions are active, so we don't have 110 // to worry about MSIM clashing. 111 displayText = getContext().getText( 112 com.android.internal.R.string.emergency_calls_only); 113 Intent i = getContext().registerReceiver(null, 114 new IntentFilter(TelephonyIntents.SPN_STRINGS_UPDATED_ACTION)); 115 if (i != null) { 116 displayText = i.getStringExtra(TelephonyIntents.EXTRA_PLMN); 117 } 118 } 119 } 120 121 setText(displayText); 122 setVisibility(TextUtils.isEmpty(displayText) ? GONE : VISIBLE); 123 } 124 } 125