1 /* 2 * Copyright (C) 2020 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.settings.network.telephony; 18 19 import android.Manifest; 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.Bundle; 26 import android.os.SystemClock; 27 import android.telephony.euicc.EuiccManager; 28 import android.util.Log; 29 30 import com.android.settings.SidecarFragment; 31 32 import java.util.concurrent.atomic.AtomicInteger; 33 34 /** 35 * The sidecar base class that an Euicc sidecar can extend from. The extended class should implement 36 * getReceiverAction() to return the action string for the broadcast receiver. The extended class 37 * should implement its own get() function to return an instance of that class, and implement the 38 * functional class like run() to actually trigger the function in EuiccManager. 39 */ 40 public abstract class EuiccOperationSidecar extends SidecarFragment { 41 private static final String TAG = "EuiccOperationSidecar"; 42 private static final int REQUEST_CODE = 0; 43 private static final String EXTRA_OP_ID = "op_id"; 44 private static AtomicInteger sCurrentOpId = 45 new AtomicInteger((int) SystemClock.elapsedRealtime()); 46 47 protected EuiccManager mEuiccManager; 48 49 private int mResultCode; 50 private int mDetailedCode; 51 private Intent mResultIntent; 52 private int mOpId; 53 54 protected final BroadcastReceiver mReceiver = 55 new BroadcastReceiver() { 56 @Override 57 public void onReceive(Context context, Intent intent) { 58 if (getReceiverAction().equals(intent.getAction()) 59 && mOpId == intent.getIntExtra(EXTRA_OP_ID, -1)) { 60 mResultCode = getResultCode(); 61 /* TODO: This relies on our LUI and LPA to coexist, should think about how 62 to generalize this further. */ 63 mDetailedCode = 64 intent.getIntExtra( 65 EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE, 66 0 /* defaultValue*/); 67 mResultIntent = intent; 68 Log.i( 69 TAG, 70 String.format( 71 "Result code : %d; detailed code : %d", 72 mResultCode, mDetailedCode)); 73 onActionReceived(); 74 } 75 } 76 }; 77 78 /** 79 * This is called when the broadcast action is received. The subclass may override this to 80 * perform different logic. The broadcast result code may be obtained with {@link 81 * #getResultCode()} and the Intent may be obtained with {@link #getResultIntent()}. 82 */ onActionReceived()83 protected void onActionReceived() { 84 if (mResultCode == EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK) { 85 setState(State.SUCCESS, Substate.UNUSED); 86 } else { 87 setState(State.ERROR, mResultCode); 88 } 89 } 90 91 /** 92 * The extended class should implement it to return a string for the broadcast action. The class 93 * should be unique across all the child classes. 94 */ getReceiverAction()95 protected abstract String getReceiverAction(); 96 createCallbackIntent()97 protected PendingIntent createCallbackIntent() { 98 mOpId = sCurrentOpId.incrementAndGet(); 99 Intent intent = new Intent(getReceiverAction()); 100 intent.putExtra(EXTRA_OP_ID, mOpId); 101 return PendingIntent.getBroadcast( 102 getContext(), REQUEST_CODE, intent, 103 PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE); 104 } 105 106 @Override onCreate(Bundle savedInstanceState)107 public void onCreate(Bundle savedInstanceState) { 108 super.onCreate(savedInstanceState); 109 mEuiccManager = getContext().getSystemService(EuiccManager.class); 110 111 getContext() 112 .getApplicationContext() 113 .registerReceiver( 114 mReceiver, 115 new IntentFilter(getReceiverAction()), 116 Manifest.permission.WRITE_EMBEDDED_SUBSCRIPTIONS, 117 null); 118 } 119 120 @Override onDestroy()121 public void onDestroy() { 122 getContext().getApplicationContext().unregisterReceiver(mReceiver); 123 super.onDestroy(); 124 } 125 getResultCode()126 public int getResultCode() { 127 return mResultCode; 128 } 129 getDetailedCode()130 public int getDetailedCode() { 131 return mDetailedCode; 132 } 133 getResultIntent()134 public Intent getResultIntent() { 135 return mResultIntent; 136 } 137 } 138