qingxi | 3d76874 | 2017-04-12 11:07:11 -0700 | [diff] [blame] | 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 | |
Qingxi Li | 2febffb | 2018-01-23 16:16:35 -0800 | [diff] [blame] | 19 | import android.app.AlertDialog; |
qingxi | 7077b91 | 2017-05-24 16:04:32 -0700 | [diff] [blame] | 20 | import android.app.PendingIntent; |
| 21 | import android.content.BroadcastReceiver; |
qingxi | 3d76874 | 2017-04-12 11:07:11 -0700 | [diff] [blame] | 22 | import android.content.Context; |
qingxi | 7077b91 | 2017-05-24 16:04:32 -0700 | [diff] [blame] | 23 | import android.content.Intent; |
| 24 | import android.content.IntentFilter; |
| 25 | import android.os.UserHandle; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 26 | import android.telephony.SubscriptionInfo; |
| 27 | import android.telephony.SubscriptionManager; |
| 28 | import android.telephony.euicc.EuiccManager; |
qingxi | 3d76874 | 2017-04-12 11:07:11 -0700 | [diff] [blame] | 29 | import android.util.AttributeSet; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 30 | import android.util.Log; |
qingxi | 3d76874 | 2017-04-12 11:07:11 -0700 | [diff] [blame] | 31 | import android.view.View; |
Qingxi Li | 2febffb | 2018-01-23 16:16:35 -0800 | [diff] [blame] | 32 | import android.view.WindowManager; |
qingxi | 3d76874 | 2017-04-12 11:07:11 -0700 | [diff] [blame] | 33 | import android.widget.Button; |
qingxi | 3d76874 | 2017-04-12 11:07:11 -0700 | [diff] [blame] | 34 | |
Sunny Goyal | 87fccf0 | 2019-08-13 17:39:10 -0700 | [diff] [blame] | 35 | import com.android.systemui.R; |
| 36 | |
qingxi | 3d76874 | 2017-04-12 11:07:11 -0700 | [diff] [blame] | 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 { |
qingxi | 7077b91 | 2017-05-24 16:04:32 -0700 | [diff] [blame] | 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 | |
qingxi | 3d76874 | 2017-04-12 11:07:11 -0700 | [diff] [blame] | 46 | private EuiccManager mEuiccManager; |
| 47 | |
qingxi | 7077b91 | 2017-05-24 16:04:32 -0700 | [diff] [blame] | 48 | private BroadcastReceiver mReceiver = |
| 49 | new BroadcastReceiver() { |
| 50 | @Override |
| 51 | public void onReceive(Context context, Intent intent) { |
| 52 | if (ACTION_DISABLE_ESIM.equals(intent.getAction())) { |
| 53 | int resultCode = getResultCode(); |
| 54 | if (resultCode != EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK) { |
qingxi | 7077b91 | 2017-05-24 16:04:32 -0700 | [diff] [blame] | 55 | Log.e(TAG, "Error disabling esim, result code = " + resultCode); |
Qingxi Li | 2febffb | 2018-01-23 16:16:35 -0800 | [diff] [blame] | 56 | AlertDialog.Builder builder = |
| 57 | new AlertDialog.Builder(mContext) |
| 58 | .setMessage(R.string.error_disable_esim_msg) |
| 59 | .setTitle(R.string.error_disable_esim_title) |
| 60 | .setCancelable(false /* cancelable */) |
Qingxi Li | 3bc25e0 | 2018-03-15 10:45:04 -0700 | [diff] [blame] | 61 | .setPositiveButton(R.string.ok, null /* listener */); |
Qingxi Li | 2febffb | 2018-01-23 16:16:35 -0800 | [diff] [blame] | 62 | AlertDialog alertDialog = builder.create(); |
| 63 | alertDialog.getWindow().setType( |
| 64 | WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG); |
| 65 | alertDialog.show(); |
qingxi | 7077b91 | 2017-05-24 16:04:32 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | } |
| 69 | }; |
| 70 | |
qingxi | 3d76874 | 2017-04-12 11:07:11 -0700 | [diff] [blame] | 71 | public KeyguardEsimArea(Context context) { |
| 72 | this(context, null); |
| 73 | } |
| 74 | |
| 75 | public KeyguardEsimArea(Context context, AttributeSet attrs) { |
| 76 | this(context, attrs, 0); |
| 77 | } |
| 78 | |
| 79 | public KeyguardEsimArea(Context context, AttributeSet attrs, int defStyleAttr) { |
| 80 | this(context, attrs, defStyleAttr, android.R.style.Widget_Material_Button_Borderless); |
| 81 | } |
| 82 | |
| 83 | public KeyguardEsimArea(Context context, AttributeSet attrs, int defStyleAttr, |
| 84 | int defStyleRes) { |
| 85 | super(context, attrs, defStyleAttr, defStyleRes); |
qingxi | ccae10e | 2017-08-03 16:24:15 -0700 | [diff] [blame] | 86 | mEuiccManager = (EuiccManager) context.getSystemService(Context.EUICC_SERVICE); |
qingxi | 3d76874 | 2017-04-12 11:07:11 -0700 | [diff] [blame] | 87 | setOnClickListener(this); |
| 88 | } |
| 89 | |
| 90 | @Override |
qingxi | 7077b91 | 2017-05-24 16:04:32 -0700 | [diff] [blame] | 91 | protected void onAttachedToWindow() { |
| 92 | super.onAttachedToWindow(); |
| 93 | mContext.registerReceiver(mReceiver, new IntentFilter(ACTION_DISABLE_ESIM), |
| 94 | PERMISSION_SELF, null /* scheduler */); |
qingxi | 3d76874 | 2017-04-12 11:07:11 -0700 | [diff] [blame] | 95 | } |
| 96 | |
qingxi | 12f2de4 | 2017-05-24 15:33:13 -0700 | [diff] [blame] | 97 | public static boolean isEsimLocked(Context context, int subId) { |
| 98 | EuiccManager euiccManager = |
| 99 | (EuiccManager) context.getSystemService(Context.EUICC_SERVICE); |
qingxi | daca6f9 | 2017-06-14 11:44:04 -0700 | [diff] [blame] | 100 | if (!euiccManager.isEnabled()) { |
| 101 | return false; |
| 102 | } |
| 103 | SubscriptionInfo sub = SubscriptionManager.from(context).getActiveSubscriptionInfo(subId); |
| 104 | return sub != null && sub.isEmbedded(); |
qingxi | 12f2de4 | 2017-05-24 15:33:13 -0700 | [diff] [blame] | 105 | } |
| 106 | |
qingxi | 7077b91 | 2017-05-24 16:04:32 -0700 | [diff] [blame] | 107 | @Override |
| 108 | protected void onDetachedFromWindow() { |
| 109 | mContext.unregisterReceiver(mReceiver); |
| 110 | super.onDetachedFromWindow(); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void onClick(View v) { |
Qingxi Li | 2febffb | 2018-01-23 16:16:35 -0800 | [diff] [blame] | 115 | Intent intent = new Intent(ACTION_DISABLE_ESIM); |
qingxi | 7077b91 | 2017-05-24 16:04:32 -0700 | [diff] [blame] | 116 | intent.setPackage(mContext.getPackageName()); |
Qingxi Li | 2febffb | 2018-01-23 16:16:35 -0800 | [diff] [blame] | 117 | PendingIntent callbackIntent = PendingIntent.getBroadcastAsUser( |
qingxi | 7077b91 | 2017-05-24 16:04:32 -0700 | [diff] [blame] | 118 | mContext, |
| 119 | 0 /* requestCode */, |
| 120 | intent, |
Qingxi Li | 2febffb | 2018-01-23 16:16:35 -0800 | [diff] [blame] | 121 | PendingIntent.FLAG_UPDATE_CURRENT, UserHandle.SYSTEM); |
qingxi | 7077b91 | 2017-05-24 16:04:32 -0700 | [diff] [blame] | 122 | mEuiccManager |
| 123 | .switchToSubscription(SubscriptionManager.INVALID_SUBSCRIPTION_ID, callbackIntent); |
| 124 | } |
qingxi | 3d76874 | 2017-04-12 11:07:11 -0700 | [diff] [blame] | 125 | } |