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 setProgressState(PROGRESS_IS_NOT_SHOWING); 48 } 49 50 51 @Override finish()52 public void finish() { 53 setProgressState(PROGRESS_IS_NOT_SHOWING); 54 super.finish(); 55 } 56 57 /** 58 * Displays a loading dialog. 59 * 60 * @param message The string content should be displayed in the progress dialog. 61 */ showProgressDialog(String message)62 protected void showProgressDialog(String message) { 63 showProgressDialog(message,false); 64 } 65 66 /** 67 * Displays a loading dialog. 68 * 69 * @param message The string content should be displayed in the progress dialog. 70 * @param updateIfNeeded is whether to update the progress state in the SharedPreferences. 71 */ showProgressDialog(String message, boolean updateIfNeeded)72 protected void showProgressDialog(String message, boolean updateIfNeeded) { 73 ProgressDialogFragment.show(getFragmentManager(), message, null); 74 if (updateIfNeeded) { 75 setProgressState(PROGRESS_IS_SHOWING); 76 } 77 } 78 79 /** Dismisses the loading dialog. */ dismissProgressDialog()80 protected void dismissProgressDialog() { 81 ProgressDialogFragment.dismiss(getFragmentManager()); 82 setProgressState(PROGRESS_IS_NOT_SHOWING); 83 } 84 85 /** 86 * Displays an error dialog to indicate the subscription action failure. 87 * 88 * @param title The title of the error dialog. 89 * @param message The body text of the error dialog. 90 */ showErrorDialog(String title, String message)91 protected void showErrorDialog(String title, String message) { 92 AlertDialogFragment.show(this, title, message); 93 } 94 setProgressState(int state)95 protected void setProgressState(int state) { 96 final SharedPreferences prefs = getSharedPreferences(SIM_ACTION_DIALOG_PREFS, MODE_PRIVATE); 97 prefs.edit().putInt(KEY_PROGRESS_STATE, state).apply(); 98 Log.i(TAG, "setProgressState:" + state); 99 } 100 } 101