Home | History | Annotate | Download | only in keyguard
      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.PendingIntent;
     20 import android.content.BroadcastReceiver;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.IntentFilter;
     24 import android.os.UserHandle;
     25 import android.util.AttributeSet;
     26 import android.view.View;
     27 import android.widget.Button;
     28 import android.telephony.SubscriptionManager;
     29 import android.telephony.SubscriptionInfo;
     30 import android.telephony.euicc.EuiccManager;
     31 import android.util.Log;
     32 
     33 import java.lang.ref.WeakReference;
     34 
     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  */
     39 class KeyguardEsimArea extends Button implements View.OnClickListener {
     40     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 
     44     private EuiccManager mEuiccManager;
     45 
     46     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) {
     53                         // TODO (b/62680294): Surface more info. to the end users for this failure.
     54                         Log.e(TAG, "Error disabling esim, result code = " + resultCode);
     55                     }
     56                 }
     57             }
     58         };
     59 
     60     public KeyguardEsimArea(Context context) {
     61         this(context, null);
     62     }
     63 
     64     public KeyguardEsimArea(Context context, AttributeSet attrs) {
     65         this(context, attrs, 0);
     66     }
     67 
     68     public KeyguardEsimArea(Context context, AttributeSet attrs, int defStyleAttr) {
     69         this(context, attrs, defStyleAttr, android.R.style.Widget_Material_Button_Borderless);
     70     }
     71 
     72     public KeyguardEsimArea(Context context, AttributeSet attrs, int defStyleAttr,
     73             int defStyleRes) {
     74         super(context, attrs, defStyleAttr, defStyleRes);
     75         mEuiccManager = (EuiccManager) context.getSystemService(Context.EUICC_SERVICE);
     76         setOnClickListener(this);
     77     }
     78 
     79     @Override
     80     protected void onAttachedToWindow() {
     81         super.onAttachedToWindow();
     82         mContext.registerReceiver(mReceiver, new IntentFilter(ACTION_DISABLE_ESIM),
     83                 PERMISSION_SELF, null /* scheduler */);
     84     }
     85 
     86     public static boolean isEsimLocked(Context context, int subId) {
     87         EuiccManager euiccManager =
     88                 (EuiccManager) context.getSystemService(Context.EUICC_SERVICE);
     89         if (!euiccManager.isEnabled()) {
     90             return false;
     91         }
     92         SubscriptionInfo sub = SubscriptionManager.from(context).getActiveSubscriptionInfo(subId);
     93         return  sub != null && sub.isEmbedded();
     94     }
     95 
     96     @Override
     97     protected void onDetachedFromWindow() {
     98         mContext.unregisterReceiver(mReceiver);
     99         super.onDetachedFromWindow();
    100     }
    101 
    102     @Override
    103     public void onClick(View v) {
    104         Intent intent = new Intent(mContext, KeyguardEsimArea.class);
    105         intent.setAction(ACTION_DISABLE_ESIM);
    106         intent.setPackage(mContext.getPackageName());
    107         PendingIntent callbackIntent = PendingIntent.getBroadcast(
    108             mContext,
    109             0 /* requestCode */,
    110             intent,
    111             PendingIntent.FLAG_UPDATE_CURRENT);
    112         mEuiccManager
    113                 .switchToSubscription(SubscriptionManager.INVALID_SUBSCRIPTION_ID, callbackIntent);
    114     }
    115 }
    116