1 /* 2 * Copyright (C) 2024 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.bluetooth; 18 19 import android.app.Dialog; 20 import android.os.Bundle; 21 import android.util.Log; 22 import android.view.LayoutInflater; 23 import android.view.View; 24 import android.widget.TextView; 25 26 import androidx.annotation.NonNull; 27 import androidx.annotation.Nullable; 28 import androidx.annotation.VisibleForTesting; 29 import androidx.appcompat.app.AlertDialog; 30 import androidx.fragment.app.Fragment; 31 import androidx.fragment.app.FragmentManager; 32 import androidx.lifecycle.Lifecycle; 33 34 import com.android.settings.R; 35 import com.android.settings.core.instrumentation.InstrumentedDialogFragment; 36 37 import com.google.common.base.Strings; 38 39 public class ProgressDialogFragment extends InstrumentedDialogFragment { 40 private static final String TAG = "BTProgressDialog"; 41 42 private static final String BUNDLE_KEY_MESSAGE = "bundle_key_message"; 43 44 @Nullable private static FragmentManager sManager; 45 @Nullable private static Lifecycle sLifecycle; 46 private String mMessage = ""; 47 @Nullable private AlertDialog mAlertDialog; 48 49 @Override getMetricsCategory()50 public int getMetricsCategory() { 51 // TODO: add metrics 52 return 0; 53 } 54 55 /** 56 * Returns a new instance of {@link ProgressDialogFragment} dialog. 57 * 58 * @param host The Fragment this dialog will be hosted. 59 */ 60 @Nullable newInstance(@ullable Fragment host)61 public static ProgressDialogFragment newInstance(@Nullable Fragment host) { 62 if (host == null) return null; 63 try { 64 sManager = host.getChildFragmentManager(); 65 sLifecycle = host.getLifecycle(); 66 } catch (IllegalStateException e) { 67 Log.d(TAG, "Fail to create new instance: " + e.getMessage()); 68 return null; 69 } 70 return new ProgressDialogFragment(); 71 } 72 73 /** 74 * Display {@link ProgressDialogFragment} dialog. 75 * 76 * @param message The message to be shown on the dialog 77 */ show(@onNull String message)78 public void show(@NonNull String message) { 79 if (sManager == null) return; 80 Lifecycle.State currentState = sLifecycle == null ? null : sLifecycle.getCurrentState(); 81 if (currentState == null || !currentState.isAtLeast(Lifecycle.State.STARTED)) { 82 Log.d(TAG, "Fail to show dialog with state: " + currentState); 83 return; 84 } 85 if (mAlertDialog != null && mAlertDialog.isShowing()) { 86 if (!mMessage.equals(message)) { 87 Log.d(TAG, "Update dialog message."); 88 TextView messageView = mAlertDialog.findViewById(R.id.message); 89 if (messageView != null) { 90 messageView.setText(message); 91 } 92 mMessage = message; 93 } 94 Log.d(TAG, "Dialog is showing, return."); 95 return; 96 } 97 mMessage = message; 98 Log.d(TAG, "Show up the progress dialog."); 99 Bundle args = new Bundle(); 100 args.putString(BUNDLE_KEY_MESSAGE, message); 101 setArguments(args); 102 show(sManager, TAG); 103 } 104 105 /** Returns the current message on the dialog. */ 106 @VisibleForTesting 107 @NonNull getMessage()108 public String getMessage() { 109 return mMessage; 110 } 111 ProgressDialogFragment()112 private ProgressDialogFragment() { 113 } 114 115 @Override 116 @NonNull onCreateDialog(@ullable Bundle savedInstanceState)117 public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { 118 Bundle args = requireArguments(); 119 String message = args.getString(BUNDLE_KEY_MESSAGE, ""); 120 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 121 LayoutInflater inflater = LayoutInflater.from(builder.getContext()); 122 View customView = inflater.inflate( 123 R.layout.dialog_audio_sharing_progress, /* root= */ null); 124 TextView textView = customView.findViewById(R.id.message); 125 if (textView != null && !Strings.isNullOrEmpty(message)) { 126 textView.setText(message); 127 } 128 AlertDialog dialog = builder.setView(customView).setCancelable(false).create(); 129 dialog.setCanceledOnTouchOutside(false); 130 mAlertDialog = dialog; 131 return dialog; 132 } 133 } 134