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.Activity; 20 import android.app.DialogFragment; 21 import android.app.Fragment; 22 import android.os.Bundle; 23 24 import androidx.annotation.Nullable; 25 26 /** 27 * Base dialog fragment class with the functionality to make a fragment or an activity as a listener 28 * which can survive through the activity restarts. 29 */ 30 public abstract class BaseDialogFragment extends DialogFragment { 31 // Tags for the listener which receives event callbacks. 32 private static final String ARG_LISTENER_TAG = "listener_tag"; 33 private static final String ARG_IN_CALLER_TAG = "in_caller_tag"; 34 35 /** 36 * @param activity The caller activity or the activity attached with the fragment. 37 * @param listener The original caller, that is, the listener. The listener can be the fragment 38 * to receive event callbacks. If it is null, will use the activity to handle the event 39 * callback instead. 40 * @param callbackInterfaceClass The interface that the listener should implements. 41 * @param arguments The arguments bundle of the dispatcher fragment used to store the listener's 42 * info. 43 * @param tagInCaller An integer given by the listener to distinguish the action when calling 44 * the listener's callback function. 45 */ setListener( Activity activity, @Nullable Fragment listener, Class<T> callbackInterfaceClass, int tagInCaller, Bundle arguments)46 protected static <T> void setListener( 47 Activity activity, 48 @Nullable Fragment listener, 49 Class<T> callbackInterfaceClass, 50 int tagInCaller, 51 Bundle arguments) { 52 checkValidity(activity, listener, callbackInterfaceClass); 53 54 if (listener != null && listener.getParentFragment() != null) { 55 throw new IllegalArgumentException("The listener must be attached to an activity."); 56 } 57 arguments.putInt(ARG_IN_CALLER_TAG, tagInCaller); 58 if (listener != null) { 59 arguments.putString(ARG_LISTENER_TAG, listener.getTag()); 60 } 61 } 62 63 /** 64 * @param callbackInterfaceClass The interface that the listener should implements. 65 * @param <T> Template type. 66 * @return The listener class. 67 */ 68 @SuppressWarnings("unchecked") getListener(Class<T> callbackInterfaceClass)69 protected <T> T getListener(Class<T> callbackInterfaceClass) { 70 Object listener; 71 String listenerTag = getArguments().getString(ARG_LISTENER_TAG); 72 if (listenerTag == null) { 73 listener = getActivity(); 74 } else { 75 listener = getActivity().getFragmentManager().findFragmentByTag(listenerTag); 76 } 77 if (callbackInterfaceClass.isInstance(listener)) { 78 return (T) listener; 79 } 80 throw new IllegalArgumentException("The caller should implement the callback function."); 81 } 82 83 /** @return The tag set in the listener. */ getTagInCaller()84 protected int getTagInCaller() { 85 return getArguments().getInt(ARG_IN_CALLER_TAG); 86 } 87 checkValidity( Activity activity, @Nullable Fragment listener, Class<T> callbackInterfaceClass)88 private static <T> void checkValidity( 89 Activity activity, @Nullable Fragment listener, Class<T> callbackInterfaceClass) { 90 if (listener != null) { 91 if (!callbackInterfaceClass.isInstance(listener)) { 92 throw new IllegalArgumentException( 93 "The listener fragment should implement the callback function."); 94 } 95 } else { 96 if (!callbackInterfaceClass.isInstance(activity)) { 97 throw new IllegalArgumentException( 98 "The caller activity should implement the callback function."); 99 } 100 } 101 } 102 } 103