1 /* 2 * Copyright (C) 2019 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.permissioncontroller.role.ui; 18 19 import android.app.AlertDialog; 20 import android.app.Dialog; 21 import android.content.Intent; 22 import android.os.Bundle; 23 24 import androidx.annotation.NonNull; 25 import androidx.annotation.Nullable; 26 import androidx.fragment.app.DialogFragment; 27 import androidx.fragment.app.Fragment; 28 29 /** 30 * {@link DialogFragment} for confirmation before setting a default app. 31 */ 32 public class DefaultAppConfirmationDialogFragment extends DialogFragment { 33 34 private String mPackageName; 35 private int mUid; 36 private CharSequence mMessage; 37 38 /** 39 * Create a new instance of this fragment. 40 * 41 * @param packageName the package name of the application 42 * @param uid the UID the specified package is running in 43 * @param message the confirmation message 44 * 45 * @return a new instance of this fragment 46 * 47 * @see #show(String, int, CharSequence, Fragment) 48 */ 49 @NonNull newInstance(@onNull String packageName, int uid, @NonNull CharSequence message)50 public static DefaultAppConfirmationDialogFragment newInstance(@NonNull String packageName, 51 int uid, @NonNull CharSequence message) { 52 DefaultAppConfirmationDialogFragment fragment = new DefaultAppConfirmationDialogFragment(); 53 Bundle arguments = new Bundle(); 54 arguments.putString(Intent.EXTRA_PACKAGE_NAME, packageName); 55 arguments.putInt(Intent.EXTRA_UID, uid); 56 arguments.putCharSequence(Intent.EXTRA_TEXT, message); 57 fragment.setArguments(arguments); 58 return fragment; 59 } 60 61 /** 62 * Show a new instance of this fragment. 63 * 64 * @param packageName the package name of the application 65 * @param uid the UID the specified package is running in 66 * @param message the confirmation message 67 * @param fragment the parent fragment 68 * 69 * @see #newInstance(String, int, CharSequence) 70 */ show(@onNull String packageName, int uid, @NonNull CharSequence message, @NonNull Fragment fragment)71 public static void show(@NonNull String packageName, int uid, 72 @NonNull CharSequence message, @NonNull Fragment fragment) { 73 newInstance(packageName, uid, message).show(fragment.getChildFragmentManager(), null); 74 } 75 76 @Override onCreate(@ullable Bundle savedInstanceState)77 public void onCreate(@Nullable Bundle savedInstanceState) { 78 super.onCreate(savedInstanceState); 79 80 Bundle arguments = getArguments(); 81 mPackageName = arguments.getString(Intent.EXTRA_PACKAGE_NAME); 82 mUid = arguments.getInt(Intent.EXTRA_UID); 83 mMessage = arguments.getCharSequence(Intent.EXTRA_TEXT); 84 } 85 86 @NonNull 87 @Override onCreateDialog(@ullable Bundle savedInstanceState)88 public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { 89 return new AlertDialog.Builder(requireContext(), getTheme()) 90 .setMessage(mMessage) 91 .setPositiveButton(android.R.string.ok, (dialog, which) -> onOk()) 92 .setNegativeButton(android.R.string.cancel, null) 93 .create(); 94 } 95 onOk()96 private void onOk() { 97 Listener listener = (Listener) getParentFragment(); 98 listener.setDefaultApp(mPackageName, mUid); 99 } 100 101 /** 102 * Listener for {@link DefaultAppConfirmationDialogFragment}. 103 */ 104 public interface Listener { 105 106 /** 107 * Set an application as the default app. 108 * 109 * @param packageName the package name of the application 110 */ setDefaultApp(@onNull String packageName, int uid)111 void setDefaultApp(@NonNull String packageName, int uid); 112 } 113 } 114