• 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.handheld;
18 
19 import android.content.Intent;
20 import android.os.Bundle;
21 import android.os.UserHandle;
22 
23 import androidx.annotation.NonNull;
24 import androidx.annotation.Nullable;
25 import androidx.preference.Preference;
26 import androidx.preference.PreferenceFragmentCompat;
27 import androidx.preference.TwoStatePreference;
28 
29 import com.android.permissioncontroller.role.ui.DefaultAppChildFragment;
30 import com.android.settingslib.widget.FooterPreference;
31 import com.android.settingslib.widget.SelectorWithWidgetPreference;
32 
33 /**
34  * Handheld preference fragment for a default app.
35  * <p>
36  * Must be added as a child fragment and its parent fragment must implement {@link Parent}.
37  */
38 public class HandheldDefaultAppPreferenceFragment extends PreferenceFragmentCompat
39         implements DefaultAppChildFragment.Parent {
40 
41     @NonNull
42     private String mRoleName;
43     @NonNull
44     private UserHandle mUser;
45 
46     /**
47      * Create a new instance of this fragment.
48      *
49      * @param roleName the name of the role for the default app
50      * @param user the user for the default app
51      *
52      * @return a new instance of this fragment
53      */
54     @NonNull
newInstance(@onNull String roleName, @NonNull UserHandle user)55     public static HandheldDefaultAppPreferenceFragment newInstance(@NonNull String roleName,
56             @NonNull UserHandle user) {
57         HandheldDefaultAppPreferenceFragment fragment = new HandheldDefaultAppPreferenceFragment();
58         Bundle arguments = new Bundle();
59         arguments.putString(Intent.EXTRA_ROLE_NAME, roleName);
60         arguments.putParcelable(Intent.EXTRA_USER, user);
61         fragment.setArguments(arguments);
62         return fragment;
63     }
64 
65     @Override
onCreate(@ullable Bundle savedInstanceState)66     public void onCreate(@Nullable Bundle savedInstanceState) {
67         super.onCreate(savedInstanceState);
68 
69         Bundle arguments = getArguments();
70         mRoleName = arguments.getString(Intent.EXTRA_ROLE_NAME);
71         mUser = arguments.getParcelable(Intent.EXTRA_USER);
72     }
73 
74     @Override
onActivityCreated(@ullable Bundle savedInstanceState)75     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
76         super.onActivityCreated(savedInstanceState);
77 
78         if (savedInstanceState == null) {
79             DefaultAppChildFragment fragment = DefaultAppChildFragment.newInstance(mRoleName,
80                     mUser);
81             getChildFragmentManager().beginTransaction()
82                     .add(fragment, null)
83                     .commit();
84         }
85     }
86 
87     @Override
onCreatePreferences(@ullable Bundle savedInstanceState, @Nullable String rootKey)88     public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
89         // Preferences will be added by the child fragment later.
90     }
91 
92     @Override
setTitle(@onNull CharSequence title)93     public void setTitle(@NonNull CharSequence title) {
94         requireParent().setTitle(title);
95     }
96 
97     @NonNull
98     @Override
createApplicationPreference()99     public TwoStatePreference createApplicationPreference() {
100         return new SelectorWithWidgetPreference(requireContext());
101     }
102 
103     @NonNull
104     @Override
createFooterPreference()105     public Preference createFooterPreference() {
106         return new FooterPreference(requireContext());
107     }
108 
109     @Override
onPreferenceScreenChanged()110     public void onPreferenceScreenChanged() {
111         requireParent().onPreferenceScreenChanged();
112     }
113 
114     @NonNull
requireParent()115     private Parent requireParent() {
116         //noinspection unchecked
117         return (Parent) requireParentFragment();
118     }
119 
120     /**
121      * Interface that the parent fragment must implement.
122      */
123     public interface Parent {
124 
125         /**
126          * Set the title of the current settings page.
127          *
128          * @param title the title of the current settings page
129          */
setTitle(@onNull CharSequence title)130         void setTitle(@NonNull CharSequence title);
131 
132         /**
133          * Callback when changes have been made to the {@link androidx.preference.PreferenceScreen}
134          * of this {@link PreferenceFragmentCompat}.
135          */
onPreferenceScreenChanged()136         void onPreferenceScreenChanged();
137     }
138 }
139