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