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