1316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos/*
2316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos * Copyright (C) 2016 The Android Open Source Project
3316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos *
4316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos * Licensed under the Apache License, Version 2.0 (the "License");
5316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos * you may not use this file except in compliance with the License.
6316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos * You may obtain a copy of the License at
7316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos *
8316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos *      http://www.apache.org/licenses/LICENSE-2.0
9316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos *
10316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos * Unless required by applicable law or agreed to in writing, software
11316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos * distributed under the License is distributed on an "AS IS" BASIS,
12316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos * See the License for the specific language governing permissions and
14316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos * limitations under the License
15316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos */
16316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos
17316bf544843ae581336fa157355c76774e0e0cbcAdrian Roospackage com.android.systemui.statusbar.policy;
18316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos
19316bf544843ae581336fa157355c76774e0e0cbcAdrian Roosimport android.annotation.Nullable;
2072abcdaec706c682c122af0f196286a9f96c18e2Adrian Roosimport android.content.BroadcastReceiver;
21316bf544843ae581336fa157355c76774e0e0cbcAdrian Roosimport android.content.Context;
22316bf544843ae581336fa157355c76774e0e0cbcAdrian Roosimport android.content.Intent;
23316bf544843ae581336fa157355c76774e0e0cbcAdrian Roosimport android.content.IntentFilter;
24316bf544843ae581336fa157355c76774e0e0cbcAdrian Roosimport android.net.ConnectivityManager;
25316bf544843ae581336fa157355c76774e0e0cbcAdrian Roosimport android.provider.Settings;
26316bf544843ae581336fa157355c76774e0e0cbcAdrian Roosimport android.telephony.ServiceState;
27316bf544843ae581336fa157355c76774e0e0cbcAdrian Roosimport android.telephony.SubscriptionInfo;
28316bf544843ae581336fa157355c76774e0e0cbcAdrian Roosimport android.text.TextUtils;
29316bf544843ae581336fa157355c76774e0e0cbcAdrian Roosimport android.util.AttributeSet;
30316bf544843ae581336fa157355c76774e0e0cbcAdrian Roosimport android.util.Log;
31316bf544843ae581336fa157355c76774e0e0cbcAdrian Roosimport android.view.ViewGroup;
32316bf544843ae581336fa157355c76774e0e0cbcAdrian Roosimport android.view.ViewParent;
33316bf544843ae581336fa157355c76774e0e0cbcAdrian Roosimport android.widget.TextView;
34316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos
35316bf544843ae581336fa157355c76774e0e0cbcAdrian Roosimport com.android.internal.telephony.IccCardConstants;
36316bf544843ae581336fa157355c76774e0e0cbcAdrian Roosimport com.android.internal.telephony.TelephonyIntents;
37316bf544843ae581336fa157355c76774e0e0cbcAdrian Roosimport com.android.keyguard.KeyguardUpdateMonitor;
38316bf544843ae581336fa157355c76774e0e0cbcAdrian Roosimport com.android.keyguard.KeyguardUpdateMonitorCallback;
39316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos
40316bf544843ae581336fa157355c76774e0e0cbcAdrian Roosimport java.util.List;
41316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos
42316bf544843ae581336fa157355c76774e0e0cbcAdrian Roospublic class EmergencyCryptkeeperText extends TextView {
43316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos
44316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos    private KeyguardUpdateMonitor mKeyguardUpdateMonitor;
4572abcdaec706c682c122af0f196286a9f96c18e2Adrian Roos    private final KeyguardUpdateMonitorCallback mCallback = new KeyguardUpdateMonitorCallback() {
46316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos        @Override
47316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos        public void onPhoneStateChanged(int phoneState) {
48316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos            update();
49316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos        }
50231bcf7b9f6be47848160dbd2ae2c78a031f0ef8Akira Oshimi
51231bcf7b9f6be47848160dbd2ae2c78a031f0ef8Akira Oshimi        @Override
52231bcf7b9f6be47848160dbd2ae2c78a031f0ef8Akira Oshimi        public void onRefreshCarrierInfo() {
53231bcf7b9f6be47848160dbd2ae2c78a031f0ef8Akira Oshimi            update();
54231bcf7b9f6be47848160dbd2ae2c78a031f0ef8Akira Oshimi        }
55316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos    };
5672abcdaec706c682c122af0f196286a9f96c18e2Adrian Roos    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
5772abcdaec706c682c122af0f196286a9f96c18e2Adrian Roos        @Override
5872abcdaec706c682c122af0f196286a9f96c18e2Adrian Roos        public void onReceive(Context context, Intent intent) {
5972abcdaec706c682c122af0f196286a9f96c18e2Adrian Roos            if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())) {
6072abcdaec706c682c122af0f196286a9f96c18e2Adrian Roos                update();
6172abcdaec706c682c122af0f196286a9f96c18e2Adrian Roos            }
6272abcdaec706c682c122af0f196286a9f96c18e2Adrian Roos        }
6372abcdaec706c682c122af0f196286a9f96c18e2Adrian Roos    };
64316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos
65316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos    public EmergencyCryptkeeperText(Context context, @Nullable AttributeSet attrs) {
66316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos        super(context, attrs);
67316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos        setVisibility(GONE);
68316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos    }
69316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos
70316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos    @Override
71316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos    protected void onAttachedToWindow() {
72316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos        super.onAttachedToWindow();
73316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos        mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
74316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos        mKeyguardUpdateMonitor.registerCallback(mCallback);
7572abcdaec706c682c122af0f196286a9f96c18e2Adrian Roos        getContext().registerReceiver(mReceiver,
7672abcdaec706c682c122af0f196286a9f96c18e2Adrian Roos                new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
77316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos        update();
78316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos    }
79316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos
80316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos    @Override
81316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos    protected void onDetachedFromWindow() {
82316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos        super.onDetachedFromWindow();
83316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos        if (mKeyguardUpdateMonitor != null) {
84316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos            mKeyguardUpdateMonitor.removeCallback(mCallback);
85316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos        }
8672abcdaec706c682c122af0f196286a9f96c18e2Adrian Roos        getContext().unregisterReceiver(mReceiver);
87316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos    }
88316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos
89316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos    public void update() {
90316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos        boolean hasMobile = ConnectivityManager.from(mContext)
91316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos                .isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
92316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos        boolean airplaneMode = (Settings.Global.getInt(mContext.getContentResolver(),
93316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos                Settings.Global.AIRPLANE_MODE_ON, 0) == 1);
94316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos
95316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos        if (!hasMobile || airplaneMode) {
96316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos            setText(null);
97316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos            setVisibility(GONE);
98316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos            return;
99316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos        }
100316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos
101316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos        boolean allSimsMissing = true;
102316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos        CharSequence displayText = null;
103316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos
104316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos        List<SubscriptionInfo> subs = mKeyguardUpdateMonitor.getSubscriptionInfo(false);
105316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos        final int N = subs.size();
106316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos        for (int i = 0; i < N; i++) {
107316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos            int subId = subs.get(i).getSubscriptionId();
108316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos            IccCardConstants.State simState = mKeyguardUpdateMonitor.getSimState(subId);
109316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos            CharSequence carrierName = subs.get(i).getCarrierName();
110316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos            if (simState.iccCardExist() && !TextUtils.isEmpty(carrierName)) {
111316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos                allSimsMissing = false;
112316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos                displayText = carrierName;
113316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos            }
114316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos        }
115316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos        if (allSimsMissing) {
116316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos            if (N != 0) {
117316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos                // Shows "Emergency calls only" on devices that are voice-capable.
118316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos                // This depends on mPlmn containing the text "Emergency calls only" when the radio
119316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos                // has some connectivity. Otherwise it should show "No service"
120316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos                // Grab the first subscription, because they all should contain the emergency text,
121316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos                // described above.
122316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos                displayText = subs.get(0).getCarrierName();
123316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos            } else {
124316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos                // We don't have a SubscriptionInfo to get the emergency calls only from.
125316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos                // Grab it from the old sticky broadcast if possible instead. We can use it
126316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos                // here because no subscriptions are active, so we don't have
127316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos                // to worry about MSIM clashing.
128316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos                displayText = getContext().getText(
129316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos                        com.android.internal.R.string.emergency_calls_only);
130316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos                Intent i = getContext().registerReceiver(null,
131316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos                        new IntentFilter(TelephonyIntents.SPN_STRINGS_UPDATED_ACTION));
132316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos                if (i != null) {
133316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos                    displayText = i.getStringExtra(TelephonyIntents.EXTRA_PLMN);
134316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos                }
135316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos            }
136316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos        }
137316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos
138316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos        setText(displayText);
139316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos        setVisibility(TextUtils.isEmpty(displayText) ? GONE : VISIBLE);
140316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos    }
141316bf544843ae581336fa157355c76774e0e0cbcAdrian Roos}
142