|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 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.afwsamples.testdpc.policy; |
| 18 | + |
| 19 | +import android.annotation.TargetApi; |
| 20 | +import android.app.Activity; |
| 21 | +import android.app.AlertDialog; |
| 22 | +import android.app.PendingIntent; |
| 23 | +import android.app.admin.DevicePolicyManager; |
| 24 | +import android.content.BroadcastReceiver; |
| 25 | +import android.content.Context; |
| 26 | +import android.content.Intent; |
| 27 | +import android.content.IntentFilter; |
| 28 | +import android.os.Build.VERSION_CODES; |
| 29 | +import android.os.Bundle; |
| 30 | +import android.telephony.euicc.DownloadableSubscription; |
| 31 | +import android.telephony.euicc.EuiccManager; |
| 32 | +import android.util.Log; |
| 33 | +import android.view.View; |
| 34 | +import android.widget.CheckBox; |
| 35 | +import android.widget.EditText; |
| 36 | +import android.widget.Toast; |
| 37 | +import androidx.core.content.ContextCompat; |
| 38 | +import androidx.preference.Preference; |
| 39 | +import com.afwsamples.testdpc.R; |
| 40 | +import com.afwsamples.testdpc.common.BaseSearchablePolicyPreferenceFragment; |
| 41 | +import com.afwsamples.testdpc.common.ReflectionUtil; |
| 42 | +import com.afwsamples.testdpc.common.ReflectionUtil.ReflectionIsTemporaryException; |
| 43 | +import com.afwsamples.testdpc.common.preference.DpcPreference; |
| 44 | +import java.util.Set; |
| 45 | + |
| 46 | +/** Fragment to control eSIMs. */ |
| 47 | +@TargetApi(VERSION_CODES.VANILLA_ICE_CREAM) |
| 48 | +public class EsimControlFragment extends BaseSearchablePolicyPreferenceFragment |
| 49 | + implements Preference.OnPreferenceClickListener, Preference.OnPreferenceChangeListener { |
| 50 | + private static final String TAG = EsimControlFragment.class.getSimpleName(); |
| 51 | + private static final String DOWNLOADE_ESIM = "download_esim"; |
| 52 | + private static final String DELETE_ESIM = "delete_esim"; |
| 53 | + private static final String GET_MANAGED_ESIM = "get_managed_esim"; |
| 54 | + private static final String ACTION_DOWNLOAD_ESIM = "com.afwsamples.testdpc.esim_download"; |
| 55 | + private static final String ACTION_DELETE_ESIM = "com.afwsamples.testdpc.esim_delete"; |
| 56 | + |
| 57 | + private DpcPreference mDownloadEsimPreference; |
| 58 | + private DpcPreference mDeleteEsimPreference; |
| 59 | + private DpcPreference mGetManagedEsimPreference; |
| 60 | + private DevicePolicyManager mDevicePolicyManager; |
| 61 | + private EuiccManager mEuiccManager; |
| 62 | + |
| 63 | + private String getResultText(int resultCode) { |
| 64 | + if (resultCode == EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK) { |
| 65 | + return "EMBEDDED_SUBSCRIPTION_RESULT_OK"; |
| 66 | + } else if (resultCode == EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR) { |
| 67 | + return "EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR"; |
| 68 | + } else if (resultCode == EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR) { |
| 69 | + return "EMBEDDED_SUBSCRIPTION_RESULT_ERROR"; |
| 70 | + } |
| 71 | + return "Uknown: " + resultCode; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void onCreate(Bundle savedInstanceState) { |
| 76 | + mDevicePolicyManager = getActivity().getSystemService(DevicePolicyManager.class); |
| 77 | + mEuiccManager = getActivity().getSystemService(EuiccManager.class); |
| 78 | + getActivity().getActionBar().setTitle(R.string.manage_esim); |
| 79 | + super.onCreate(savedInstanceState); |
| 80 | + } |
| 81 | + |
| 82 | + private BroadcastReceiver mDownloadESIMReceiver = |
| 83 | + new BroadcastReceiver() { |
| 84 | + @Override |
| 85 | + public void onReceive(Context context, Intent intent) { |
| 86 | + |
| 87 | + if (!ACTION_DOWNLOAD_ESIM.equals(intent.getAction())) { |
| 88 | + return; |
| 89 | + } |
| 90 | + int detailedCode = |
| 91 | + intent.getIntExtra(EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE, -1); |
| 92 | + int errorCode = |
| 93 | + intent.getIntExtra(EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_ERROR_CODE, -1); |
| 94 | + |
| 95 | + Log.v( |
| 96 | + TAG, |
| 97 | + "Download result: resultCode: " |
| 98 | + + getResultText(getResultCode()) |
| 99 | + + " detailedCode: " |
| 100 | + + getResultCode() |
| 101 | + + " detailedCode: " |
| 102 | + + detailedCode |
| 103 | + + " errorCode: " |
| 104 | + + errorCode); |
| 105 | + showToast("Download result: " + getResultText(getResultCode()), Toast.LENGTH_LONG); |
| 106 | + getActivity().unregisterReceiver(mDownloadESIMReceiver); |
| 107 | + } |
| 108 | + }; |
| 109 | + |
| 110 | + private BroadcastReceiver mDeleteESIMReceiver = |
| 111 | + new BroadcastReceiver() { |
| 112 | + @Override |
| 113 | + public void onReceive(Context context, Intent intent) { |
| 114 | + if (!ACTION_DELETE_ESIM.equals(intent.getAction())) { |
| 115 | + return; |
| 116 | + } |
| 117 | + int detailedCode = |
| 118 | + intent.getIntExtra(EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE, -1); |
| 119 | + Log.v( |
| 120 | + TAG, |
| 121 | + "Delete result: resultCode: " |
| 122 | + + getResultText(getResultCode()) |
| 123 | + + " detailedCode: " |
| 124 | + + detailedCode); |
| 125 | + |
| 126 | + showToast("Delete result: " + getResultText(getResultCode()), Toast.LENGTH_LONG); |
| 127 | + getActivity().unregisterReceiver(mDeleteESIMReceiver); |
| 128 | + } |
| 129 | + }; |
| 130 | + |
| 131 | + @Override |
| 132 | + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { |
| 133 | + addPreferencesFromResource(R.xml.esim_control_preferences); |
| 134 | + |
| 135 | + mDownloadEsimPreference = (DpcPreference) findPreference(DOWNLOADE_ESIM); |
| 136 | + mDownloadEsimPreference.setOnPreferenceClickListener(this); |
| 137 | + |
| 138 | + mDeleteEsimPreference = (DpcPreference) findPreference(DELETE_ESIM); |
| 139 | + mDeleteEsimPreference.setOnPreferenceClickListener(this); |
| 140 | + |
| 141 | + mGetManagedEsimPreference = (DpcPreference) findPreference(GET_MANAGED_ESIM); |
| 142 | + mGetManagedEsimPreference.setOnPreferenceClickListener(this); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public boolean isAvailable(Context context) { |
| 147 | + return true; |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public boolean onPreferenceChange(Preference preference, Object newValue) { |
| 152 | + return false; |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public boolean onPreferenceClick(Preference preference) { |
| 157 | + String key = preference.getKey(); |
| 158 | + |
| 159 | + switch (key) { |
| 160 | + case DOWNLOADE_ESIM: |
| 161 | + showDownloadEsimUi(); |
| 162 | + return true; |
| 163 | + case DELETE_ESIM: |
| 164 | + showDeleteEsimUi(); |
| 165 | + return true; |
| 166 | + case GET_MANAGED_ESIM: |
| 167 | + showManagedEsimUi(); |
| 168 | + return true; |
| 169 | + } |
| 170 | + return false; |
| 171 | + } |
| 172 | + |
| 173 | + private void showManagedEsimUi() { |
| 174 | + Set<Integer> managedSubIds = getSubscriptionIds(); |
| 175 | + new AlertDialog.Builder(getActivity()) |
| 176 | + .setTitle(R.string.get_managed_esim_dialog_title) |
| 177 | + .setItems(managedSubIds.stream().map(String::valueOf).toArray(String[]::new), null) |
| 178 | + .show(); |
| 179 | + } |
| 180 | + |
| 181 | + private Set<Integer> getSubscriptionIds() { |
| 182 | + try { |
| 183 | + // TODO: remove reflection code and call directly once V is released. |
| 184 | + return ReflectionUtil.invoke(mDevicePolicyManager, "getSubscriptionIds"); |
| 185 | + } catch (ReflectionIsTemporaryException e) { |
| 186 | + Log.e(TAG, "Error invoking getSubscriptionIds", e); |
| 187 | + showToast("Error getting managed esim information.", Toast.LENGTH_LONG); |
| 188 | + } |
| 189 | + return null; |
| 190 | + } |
| 191 | + |
| 192 | + private void showToast(String msg, int duration) { |
| 193 | + Activity activity = getActivity(); |
| 194 | + if (activity == null || activity.isFinishing()) { |
| 195 | + Log.w(TAG, "Not toasting '" + msg + "' as activity is finishing or finished"); |
| 196 | + return; |
| 197 | + } |
| 198 | + Log.d(TAG, "Showing toast: " + msg); |
| 199 | + Toast.makeText(activity, msg, duration).show(); |
| 200 | + } |
| 201 | + |
| 202 | + private void showDownloadEsimUi() { |
| 203 | + if (getActivity() == null || getActivity().isFinishing()) { |
| 204 | + return; |
| 205 | + } |
| 206 | + |
| 207 | + final View dialogView = |
| 208 | + getActivity().getLayoutInflater().inflate(R.layout.esim_dialog_layout, null); |
| 209 | + final EditText activationCodeEditText = |
| 210 | + (EditText) dialogView.findViewById(R.id.activation_code); |
| 211 | + final CheckBox activateAfterDownloadCheckBox = |
| 212 | + (CheckBox) dialogView.findViewById(R.id.activate_esim); |
| 213 | + |
| 214 | + new AlertDialog.Builder(getActivity()) |
| 215 | + .setTitle(R.string.esim_activation_code) |
| 216 | + .setView(dialogView) |
| 217 | + .setPositiveButton( |
| 218 | + android.R.string.ok, |
| 219 | + (dialogInterface, i) -> { |
| 220 | + final String activationCodeString = activationCodeEditText.getText().toString(); |
| 221 | + startEsimDownload(activationCodeString, activateAfterDownloadCheckBox.isChecked()); |
| 222 | + }) |
| 223 | + .setNegativeButton(android.R.string.cancel, null) |
| 224 | + .show(); |
| 225 | + } |
| 226 | + |
| 227 | + private void showDeleteEsimUi() { |
| 228 | + if (getActivity() == null || getActivity().isFinishing()) { |
| 229 | + return; |
| 230 | + } |
| 231 | + |
| 232 | + final View dialogView = |
| 233 | + getActivity().getLayoutInflater().inflate(R.layout.simple_edittext, null); |
| 234 | + final EditText subIdEditText = dialogView.findViewById(R.id.input); |
| 235 | + |
| 236 | + new AlertDialog.Builder(getActivity()) |
| 237 | + .setTitle(R.string.delete_esim_dialog_title) |
| 238 | + .setView(dialogView) |
| 239 | + .setPositiveButton( |
| 240 | + android.R.string.ok, |
| 241 | + (dialogInterface, i) -> { |
| 242 | + final String subId = subIdEditText.getText().toString(); |
| 243 | + deleteEsim(Integer.parseInt(subId)); |
| 244 | + }) |
| 245 | + .setNegativeButton(android.R.string.cancel, null) |
| 246 | + .show(); |
| 247 | + } |
| 248 | + |
| 249 | + private void startEsimDownload(String activationCode, boolean switchAfterDownload) { |
| 250 | + ContextCompat.registerReceiver( |
| 251 | + getActivity(), |
| 252 | + mDownloadESIMReceiver, |
| 253 | + new IntentFilter(ACTION_DOWNLOAD_ESIM), |
| 254 | + ContextCompat.RECEIVER_EXPORTED); |
| 255 | + DownloadableSubscription downloadableSubscription = |
| 256 | + DownloadableSubscription.forActivationCode(activationCode); |
| 257 | + PendingIntent pi = |
| 258 | + PendingIntent.getBroadcast( |
| 259 | + getActivity(), |
| 260 | + 0, |
| 261 | + new Intent(ACTION_DOWNLOAD_ESIM), |
| 262 | + PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT); |
| 263 | + mEuiccManager.downloadSubscription(downloadableSubscription, switchAfterDownload, pi); |
| 264 | + Log.v( |
| 265 | + TAG, |
| 266 | + "started downloading eSIM, " |
| 267 | + + "activationCode : " |
| 268 | + + activationCode |
| 269 | + + ", switchAfterDownload : " |
| 270 | + + switchAfterDownload); |
| 271 | + showToast("started downloading eSIM", Toast.LENGTH_LONG); |
| 272 | + } |
| 273 | + |
| 274 | + private void deleteEsim(int subId) { |
| 275 | + ContextCompat.registerReceiver( |
| 276 | + getActivity(), |
| 277 | + mDeleteESIMReceiver, |
| 278 | + new IntentFilter(ACTION_DELETE_ESIM), |
| 279 | + ContextCompat.RECEIVER_EXPORTED); |
| 280 | + PendingIntent pi = |
| 281 | + PendingIntent.getBroadcast( |
| 282 | + getActivity(), |
| 283 | + 0, |
| 284 | + new Intent(), |
| 285 | + PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); |
| 286 | + mEuiccManager.deleteSubscription(subId, pi); |
| 287 | + |
| 288 | + showToast("started deleting eSIM", Toast.LENGTH_LONG); |
| 289 | + } |
| 290 | +} |
0 commit comments