• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 CharSequence mMessage;
36 
37     /**
38      * Create a new instance of this fragment.
39      *
40      * @param packageName the package name of the application
41      * @param message the confirmation message
42      *
43      * @return a new instance of this fragment
44      *
45      * @see #show(String, CharSequence, Fragment)
46      */
47     @NonNull
newInstance(@onNull String packageName, @NonNull CharSequence message)48     public static DefaultAppConfirmationDialogFragment newInstance(@NonNull String packageName,
49             @NonNull CharSequence message) {
50         DefaultAppConfirmationDialogFragment fragment = new DefaultAppConfirmationDialogFragment();
51         Bundle arguments = new Bundle();
52         arguments.putString(Intent.EXTRA_PACKAGE_NAME, packageName);
53         arguments.putCharSequence(Intent.EXTRA_TEXT, message);
54         fragment.setArguments(arguments);
55         return fragment;
56     }
57 
58     /**
59      * Show a new instance of this fragment.
60      *
61      * @param packageName the package name of the application
62      * @param message the confirmation message
63      * @param fragment the parent fragment
64      *
65      * @see #newInstance(String, CharSequence)
66      */
show(@onNull String packageName, @NonNull CharSequence message, @NonNull Fragment fragment)67     public static void show(@NonNull String packageName, @NonNull CharSequence message,
68             @NonNull Fragment fragment) {
69         newInstance(packageName, message).show(fragment.getChildFragmentManager(), null);
70     }
71 
72     @Override
onCreate(@ullable Bundle savedInstanceState)73     public void onCreate(@Nullable Bundle savedInstanceState) {
74         super.onCreate(savedInstanceState);
75 
76         Bundle arguments = getArguments();
77         mPackageName = arguments.getString(Intent.EXTRA_PACKAGE_NAME);
78         mMessage = arguments.getCharSequence(Intent.EXTRA_TEXT);
79     }
80 
81     @NonNull
82     @Override
onCreateDialog(@ullable Bundle savedInstanceState)83     public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
84         return new AlertDialog.Builder(requireContext(), getTheme())
85                 .setMessage(mMessage)
86                 .setPositiveButton(android.R.string.ok, (dialog, which) -> onOk())
87                 .setNegativeButton(android.R.string.cancel, null)
88                 .create();
89     }
90 
onOk()91     private void onOk() {
92         Listener listener = (Listener) getParentFragment();
93         listener.setDefaultApp(mPackageName);
94     }
95 
96     /**
97      * Listener for {@link DefaultAppConfirmationDialogFragment}.
98      */
99     public interface Listener {
100 
101         /**
102          * Set an application as the default app.
103          *
104          * @param packageName the package name of the application
105          */
setDefaultApp(@onNull String packageName)106         void setDefaultApp(@NonNull String packageName);
107     }
108 }
109