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 static android.content.Context.MODE_PRIVATE; 20 21 import android.content.SharedPreferences; 22 import android.os.Bundle; 23 import android.telephony.SubscriptionManager; 24 import android.util.Log; 25 26 import androidx.fragment.app.FragmentActivity; 27 28 /** The base class for subscription action dialogs */ 29 public class SubscriptionActionDialogActivity extends FragmentActivity { 30 31 private static final String TAG = "SubscriptionActionDialogActivity"; 32 // Arguments 33 protected static final String ARG_SUB_ID = "sub_id"; 34 protected SubscriptionManager mSubscriptionManager; 35 36 public static final String SIM_ACTION_DIALOG_PREFS = "sim_action_dialog_prefs"; 37 // Shared preference keys 38 public static final String KEY_PROGRESS_STATE = "progress_state"; 39 public static final int PROGRESS_IS_NOT_SHOWING = 0; 40 public static final int PROGRESS_IS_SHOWING = 1; 41 42 @Override onCreate(Bundle savedInstanceState)43 protected void onCreate(Bundle savedInstanceState) { 44 super.onCreate(savedInstanceState); 45 46 mSubscriptionManager = getSystemService(SubscriptionManager.class) 47 .createForAllUserProfiles(); 48 setProgressState(PROGRESS_IS_NOT_SHOWING); 49 } 50 51 52 @Override finish()53 public void finish() { 54 setProgressState(PROGRESS_IS_NOT_SHOWING); 55 super.finish(); 56 } 57 58 /** 59 * Displays a loading dialog. 60 * 61 * @param message The string content should be displayed in the progress dialog. 62 */ showProgressDialog(String message)63 protected void showProgressDialog(String message) { 64 showProgressDialog(message,false); 65 } 66 67 /** 68 * Displays a loading dialog. 69 * 70 * @param message The string content should be displayed in the progress dialog. 71 * @param updateIfNeeded is whether to update the progress state in the SharedPreferences. 72 */ showProgressDialog(String message, boolean updateIfNeeded)73 protected void showProgressDialog(String message, boolean updateIfNeeded) { 74 ProgressDialogFragment.show(getFragmentManager(), message, null); 75 if (updateIfNeeded) { 76 setProgressState(PROGRESS_IS_SHOWING); 77 } 78 } 79 80 /** Dismisses the loading dialog. */ dismissProgressDialog()81 protected void dismissProgressDialog() { 82 ProgressDialogFragment.dismiss(getFragmentManager()); 83 setProgressState(PROGRESS_IS_NOT_SHOWING); 84 } 85 86 /** 87 * Displays an error dialog to indicate the subscription action failure. 88 * 89 * @param title The title of the error dialog. 90 * @param message The body text of the error dialog. 91 */ showErrorDialog(String title, String message)92 protected void showErrorDialog(String title, String message) { 93 AlertDialogFragment.show(this, title, message); 94 } 95 setProgressState(int state)96 protected void setProgressState(int state) { 97 final SharedPreferences prefs = getSharedPreferences(SIM_ACTION_DIALOG_PREFS, MODE_PRIVATE); 98 prefs.edit().putInt(KEY_PROGRESS_STATE, state).apply(); 99 Log.i(TAG, "setProgressState:" + state); 100 } 101 } 102