• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.keyguard;
18 
19 import android.app.AlertDialog;
20 import android.app.PendingIntent;
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.os.UserHandle;
26 import android.telephony.SubscriptionInfo;
27 import android.telephony.SubscriptionManager;
28 import android.telephony.euicc.EuiccManager;
29 import android.util.AttributeSet;
30 import android.util.Log;
31 import android.view.View;
32 import android.view.WindowManager;
33 import android.widget.Button;
34 
35 import com.android.systemui.R;
36 
37 /***
38  * This button is used by the device with embedded SIM card to disable current carrier to unlock
39  * the device with no cellular service.
40  */
41 class KeyguardEsimArea extends Button implements View.OnClickListener {
42     private static final String ACTION_DISABLE_ESIM = "com.android.keyguard.disable_esim";
43     private static final String TAG = "KeyguardEsimArea";
44     private static final String PERMISSION_SELF = "com.android.systemui.permission.SELF";
45 
46     private int mSubscriptionId;
47     private EuiccManager mEuiccManager;
48 
49     private BroadcastReceiver mReceiver =
50         new BroadcastReceiver() {
51             @Override
52             public void onReceive(Context context, Intent intent) {
53                 if (ACTION_DISABLE_ESIM.equals(intent.getAction())) {
54                     int resultCode = getResultCode();
55                     if (resultCode != EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK) {
56                         Log.e(TAG, "Error disabling esim, result code = " + resultCode);
57                         AlertDialog.Builder builder =
58                                 new AlertDialog.Builder(mContext)
59                                         .setMessage(R.string.error_disable_esim_msg)
60                                         .setTitle(R.string.error_disable_esim_title)
61                                         .setCancelable(false /* cancelable */)
62                                         .setPositiveButton(R.string.ok, null /* listener */);
63                         AlertDialog alertDialog = builder.create();
64                         alertDialog.getWindow().setType(
65                                 WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
66                         alertDialog.show();
67                     }
68                 }
69             }
70         };
71 
KeyguardEsimArea(Context context)72     public KeyguardEsimArea(Context context) {
73         this(context, null);
74     }
75 
KeyguardEsimArea(Context context, AttributeSet attrs)76     public KeyguardEsimArea(Context context, AttributeSet attrs) {
77         this(context, attrs, 0);
78     }
79 
KeyguardEsimArea(Context context, AttributeSet attrs, int defStyleAttr)80     public KeyguardEsimArea(Context context, AttributeSet attrs, int defStyleAttr) {
81         this(context, attrs, defStyleAttr, android.R.style.Widget_Material_Button_Borderless);
82     }
83 
KeyguardEsimArea(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)84     public KeyguardEsimArea(Context context, AttributeSet attrs, int defStyleAttr,
85             int defStyleRes) {
86         super(context, attrs, defStyleAttr, defStyleRes);
87         mEuiccManager = (EuiccManager) context.getSystemService(Context.EUICC_SERVICE);
88         setOnClickListener(this);
89     }
90 
setSubscriptionId(int subscriptionId)91     public void setSubscriptionId(int subscriptionId) {
92         mSubscriptionId = subscriptionId;
93     }
94 
95     @Override
onAttachedToWindow()96     protected void onAttachedToWindow() {
97         super.onAttachedToWindow();
98         mContext.registerReceiver(mReceiver, new IntentFilter(ACTION_DISABLE_ESIM),
99                 PERMISSION_SELF, null /* scheduler */,
100                 Context.RECEIVER_EXPORTED_UNAUDITED);
101     }
102 
isEsimLocked(Context context, int subId)103     public static boolean isEsimLocked(Context context, int subId) {
104         EuiccManager euiccManager =
105                 (EuiccManager) context.getSystemService(Context.EUICC_SERVICE);
106         if (!euiccManager.isEnabled()) {
107             return false;
108         }
109         SubscriptionInfo sub = SubscriptionManager.from(context).getActiveSubscriptionInfo(subId);
110         return  sub != null && sub.isEmbedded();
111     }
112 
113     @Override
onDetachedFromWindow()114     protected void onDetachedFromWindow() {
115         mContext.unregisterReceiver(mReceiver);
116         super.onDetachedFromWindow();
117     }
118 
119     @Override
onClick(View v)120     public void onClick(View v) {
121         SubscriptionInfo sub = SubscriptionManager.from(mContext)
122                 .getActiveSubscriptionInfo(mSubscriptionId);
123         if (sub == null) {
124             Log.e(TAG, "No active subscription with subscriptionId: " + mSubscriptionId);
125             return;
126         }
127         Intent intent = new Intent(ACTION_DISABLE_ESIM);
128         intent.setPackage(mContext.getPackageName());
129         PendingIntent callbackIntent = PendingIntent.getBroadcastAsUser(
130             mContext,
131             0 /* requestCode */,
132             intent,
133             PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE_UNAUDITED, UserHandle.SYSTEM);
134         mEuiccManager.switchToSubscription(
135                 SubscriptionManager.INVALID_SUBSCRIPTION_ID, sub.getPortIndex(), callbackIntent);
136     }
137 }
138