1 /* 2 * Copyright (C) 2022 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.sim; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.util.Log; 22 23 import java.lang.ref.WeakReference; 24 import java.util.concurrent.RejectedExecutionException; 25 26 /** 27 * A class for routing dismiss dialog request to SimDialogActivity. 28 */ 29 public class SimDialogProhibitService { 30 31 private static final String TAG = "SimDialogProhibitService"; 32 33 private static WeakReference<SimDialogActivity> sSimDialogActivity; 34 35 /** 36 * Support the dismiss of {@link SimDialogActivity} (singletone.) 37 * 38 * @param activity {@link SimDialogActivity} 39 */ supportDismiss(SimDialogActivity activity)40 public static void supportDismiss(SimDialogActivity activity) { 41 sSimDialogActivity = new WeakReference<SimDialogActivity>(activity); 42 } 43 44 /** 45 * Dismiss SimDialogActivity dialog. 46 * 47 * @param context is a {@link Context} 48 */ dismissDialog(Context context)49 public static void dismissDialog(Context context) { 50 // Dismiss existing dialog. 51 if (!dismissDialogThroughRunnable()) { 52 dismissDialogThroughIntent(context); 53 } 54 } 55 56 /** 57 * Dismiss dialog (if there's any). 58 * 59 * @return {@code true} when success, {@code false} when failure. 60 */ dismissDialogThroughRunnable()61 protected static boolean dismissDialogThroughRunnable() { 62 final SimDialogActivity activity = (sSimDialogActivity == null) ? 63 null : sSimDialogActivity.get(); 64 if (activity == null) { 65 Log.i(TAG, "No SimDialogActivity for dismiss."); 66 return true; 67 } 68 69 try { 70 activity.getMainExecutor().execute(() -> activity.forceClose()); 71 return true; 72 } catch (RejectedExecutionException exception) { 73 Log.w(TAG, "Fail to close SimDialogActivity through executor", exception); 74 } 75 return false; 76 } 77 78 /** 79 * Dismiss dialog through {@link Intent}. 80 * 81 * @param uiContext is {@link Context} for start SimDialogActivity. 82 */ dismissDialogThroughIntent(Context uiContext)83 protected static void dismissDialogThroughIntent(Context uiContext) { 84 Intent newIntent = new Intent(uiContext, SimDialogActivity.class); 85 newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 86 newIntent.putExtra(SimDialogActivity.DIALOG_TYPE_KEY, SimDialogActivity.PICK_DISMISS); 87 uiContext.startActivity(newIntent); 88 } 89 } 90