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