• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.app.Dialog;
20 import android.app.DialogFragment;
21 import android.app.FragmentManager;
22 import android.app.FragmentTransaction;
23 import android.app.ProgressDialog;
24 import android.content.DialogInterface;
25 import android.os.Bundle;
26 import android.text.TextUtils;
27 import android.view.KeyEvent;
28 
29 import com.android.settings.R;
30 
31 /** Fragment to show a progress dialog. */
32 public class ProgressDialogFragment extends DialogFragment {
33     private static final String ARG_TITLE = "title";
34 
35     private static final String TAG = "ProgressDialogFragment";
36 
37     private OnDismissCallback mDismissCallback;
38 
39     // Host fragment is optional to implement this interface.
40     public interface OnDismissCallback {
41         // Action performed when the progress dialog is dismissed.
onProgressDialogDismiss()42         void onProgressDialogDismiss();
43     }
44 
45     /**
46      * Check whether there is already a showing progress dialog. If yes and the title of the showing
47      * one is the same with the new coming one, just return and do nothing. If the title of the
48      * showing one is different from the new one, remove the showing one and create a new dialog for
49      * the new one. If there is no progress dialog right now, just create a new one.
50      */
show(FragmentManager fm, String title, OnDismissCallback dismissCallback)51     public static void show(FragmentManager fm, String title, OnDismissCallback dismissCallback) {
52         ProgressDialogFragment fragment = (ProgressDialogFragment) fm.findFragmentByTag(TAG);
53         if (fragment != null
54                 && TextUtils.equals(fragment.getArguments().getString(ARG_TITLE), title)) {
55             return;
56         }
57 
58         FragmentTransaction ft = fm.beginTransaction();
59         if (fragment != null) {
60             ft.remove(fragment);
61         }
62 
63         fragment = new ProgressDialogFragment();
64         fragment.setDismissCallback(dismissCallback);
65 
66         Bundle arguments = new Bundle();
67         arguments.putString(ARG_TITLE, title);
68         fragment.setArguments(arguments);
69         fragment.show(ft, TAG);
70     }
71 
72     /**
73      * Called by the caller activity or fragment when the progress is finished.
74      *
75      * @param fm The fragment manager.
76      */
dismiss(FragmentManager fm)77     public static void dismiss(FragmentManager fm) {
78         DialogFragment fragment = (DialogFragment) fm.findFragmentByTag(TAG);
79         if (fragment != null) {
80             fragment.dismiss();
81         }
82     }
83 
84     @Override
85     @SuppressWarnings("deprecation") // ProgressDialog is deprecated but is intended UX for now
onCreateDialog(Bundle savedInstanceState)86     public Dialog onCreateDialog(Bundle savedInstanceState) {
87         ProgressDialog dialog = new ProgressDialog(getActivity());
88         dialog.getWindow().setBackgroundDrawableResource(R.drawable.sim_progress_dialog_rounded_bg);
89         dialog.setCancelable(false);
90         dialog.setCanceledOnTouchOutside(false);
91         dialog.setMessage(getArguments().getString(ARG_TITLE));
92         dialog.setOnKeyListener(
93                 (progressDialog, keyCode, event) -> KeyEvent.KEYCODE_BACK == keyCode);
94 
95         return dialog;
96     }
97 
98     @Override
onDismiss(DialogInterface dialog)99     public void onDismiss(DialogInterface dialog) {
100         super.onDismiss(dialog);
101         if (mDismissCallback != null) {
102             mDismissCallback.onProgressDialogDismiss();
103         }
104     }
105 
setDismissCallback(OnDismissCallback dismissCallback)106     private void setDismissCallback(OnDismissCallback dismissCallback) {
107         mDismissCallback = dismissCallback;
108     }
109 }
110