blob: c2bbfbf73bb6e10a0b00d066f7113c21032b7a6e [file] [log] [blame]
qingxi3d768742017-04-12 11:07:11 -07001/*
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
17package com.android.keyguard;
18
Qingxi Li2febffb2018-01-23 16:16:35 -080019import android.app.AlertDialog;
qingxi7077b912017-05-24 16:04:32 -070020import android.app.PendingIntent;
21import android.content.BroadcastReceiver;
qingxi3d768742017-04-12 11:07:11 -070022import android.content.Context;
qingxi7077b912017-05-24 16:04:32 -070023import android.content.Intent;
24import android.content.IntentFilter;
25import android.os.UserHandle;
Gus Prevasab336792018-11-14 13:52:20 -050026import android.telephony.SubscriptionInfo;
27import android.telephony.SubscriptionManager;
28import android.telephony.euicc.EuiccManager;
qingxi3d768742017-04-12 11:07:11 -070029import android.util.AttributeSet;
Gus Prevasab336792018-11-14 13:52:20 -050030import android.util.Log;
qingxi3d768742017-04-12 11:07:11 -070031import android.view.View;
Qingxi Li2febffb2018-01-23 16:16:35 -080032import android.view.WindowManager;
qingxi3d768742017-04-12 11:07:11 -070033import android.widget.Button;
qingxi3d768742017-04-12 11:07:11 -070034
35/***
36 * This button is used by the device with embedded SIM card to disable current carrier to unlock
37 * the device with no cellular service.
38 */
39class KeyguardEsimArea extends Button implements View.OnClickListener {
qingxi7077b912017-05-24 16:04:32 -070040 private static final String ACTION_DISABLE_ESIM = "com.android.keyguard.disable_esim";
41 private static final String TAG = "KeyguardEsimArea";
42 private static final String PERMISSION_SELF = "com.android.systemui.permission.SELF";
43
qingxi3d768742017-04-12 11:07:11 -070044 private EuiccManager mEuiccManager;
45
qingxi7077b912017-05-24 16:04:32 -070046 private BroadcastReceiver mReceiver =
47 new BroadcastReceiver() {
48 @Override
49 public void onReceive(Context context, Intent intent) {
50 if (ACTION_DISABLE_ESIM.equals(intent.getAction())) {
51 int resultCode = getResultCode();
52 if (resultCode != EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK) {
qingxi7077b912017-05-24 16:04:32 -070053 Log.e(TAG, "Error disabling esim, result code = " + resultCode);
Qingxi Li2febffb2018-01-23 16:16:35 -080054 AlertDialog.Builder builder =
55 new AlertDialog.Builder(mContext)
56 .setMessage(R.string.error_disable_esim_msg)
57 .setTitle(R.string.error_disable_esim_title)
58 .setCancelable(false /* cancelable */)
Qingxi Li3bc25e02018-03-15 10:45:04 -070059 .setPositiveButton(R.string.ok, null /* listener */);
Qingxi Li2febffb2018-01-23 16:16:35 -080060 AlertDialog alertDialog = builder.create();
61 alertDialog.getWindow().setType(
62 WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
63 alertDialog.show();
qingxi7077b912017-05-24 16:04:32 -070064 }
65 }
66 }
67 };
68
qingxi3d768742017-04-12 11:07:11 -070069 public KeyguardEsimArea(Context context) {
70 this(context, null);
71 }
72
73 public KeyguardEsimArea(Context context, AttributeSet attrs) {
74 this(context, attrs, 0);
75 }
76
77 public KeyguardEsimArea(Context context, AttributeSet attrs, int defStyleAttr) {
78 this(context, attrs, defStyleAttr, android.R.style.Widget_Material_Button_Borderless);
79 }
80
81 public KeyguardEsimArea(Context context, AttributeSet attrs, int defStyleAttr,
82 int defStyleRes) {
83 super(context, attrs, defStyleAttr, defStyleRes);
qingxiccae10e2017-08-03 16:24:15 -070084 mEuiccManager = (EuiccManager) context.getSystemService(Context.EUICC_SERVICE);
qingxi3d768742017-04-12 11:07:11 -070085 setOnClickListener(this);
86 }
87
88 @Override
qingxi7077b912017-05-24 16:04:32 -070089 protected void onAttachedToWindow() {
90 super.onAttachedToWindow();
91 mContext.registerReceiver(mReceiver, new IntentFilter(ACTION_DISABLE_ESIM),
92 PERMISSION_SELF, null /* scheduler */);
qingxi3d768742017-04-12 11:07:11 -070093 }
94
qingxi12f2de42017-05-24 15:33:13 -070095 public static boolean isEsimLocked(Context context, int subId) {
96 EuiccManager euiccManager =
97 (EuiccManager) context.getSystemService(Context.EUICC_SERVICE);
qingxidaca6f92017-06-14 11:44:04 -070098 if (!euiccManager.isEnabled()) {
99 return false;
100 }
101 SubscriptionInfo sub = SubscriptionManager.from(context).getActiveSubscriptionInfo(subId);
102 return sub != null && sub.isEmbedded();
qingxi12f2de42017-05-24 15:33:13 -0700103 }
104
qingxi7077b912017-05-24 16:04:32 -0700105 @Override
106 protected void onDetachedFromWindow() {
107 mContext.unregisterReceiver(mReceiver);
108 super.onDetachedFromWindow();
109 }
110
111 @Override
112 public void onClick(View v) {
Qingxi Li2febffb2018-01-23 16:16:35 -0800113 Intent intent = new Intent(ACTION_DISABLE_ESIM);
qingxi7077b912017-05-24 16:04:32 -0700114 intent.setPackage(mContext.getPackageName());
Qingxi Li2febffb2018-01-23 16:16:35 -0800115 PendingIntent callbackIntent = PendingIntent.getBroadcastAsUser(
qingxi7077b912017-05-24 16:04:32 -0700116 mContext,
117 0 /* requestCode */,
118 intent,
Qingxi Li2febffb2018-01-23 16:16:35 -0800119 PendingIntent.FLAG_UPDATE_CURRENT, UserHandle.SYSTEM);
qingxi7077b912017-05-24 16:04:32 -0700120 mEuiccManager
121 .switchToSubscription(SubscriptionManager.INVALID_SUBSCRIPTION_ID, callbackIntent);
122 }
qingxi3d768742017-04-12 11:07:11 -0700123}