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.os.Bundle;
20 
21 import androidx.annotation.NonNull;
22 import androidx.annotation.Nullable;
23 import androidx.preference.PreferenceFragmentCompat;
24 
25 import com.android.permissioncontroller.role.ui.DefaultAppListChildFragment;
26 import com.android.permissioncontroller.role.ui.RolePreference;
27 
28 /**
29  * Handheld preference fragment for the list of default apps.
30  * <p>
31  * Must be added as a child fragment and its parent fragment must implement {@link Parent}.
32  */
33 public class HandheldDefaultAppListPreferenceFragment extends PreferenceFragmentCompat
34         implements DefaultAppListChildFragment.Parent {
35 
36     /**
37      * Create a new instance of this fragment.
38      *
39      * @return a new instance of this fragment
40      */
41     @NonNull
newInstance()42     public static HandheldDefaultAppListPreferenceFragment newInstance() {
43         return new HandheldDefaultAppListPreferenceFragment();
44     }
45 
46     @Override
onActivityCreated(@ullable Bundle savedInstanceState)47     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
48         super.onActivityCreated(savedInstanceState);
49 
50         if (savedInstanceState == null) {
51             DefaultAppListChildFragment fragment = DefaultAppListChildFragment.newInstance();
52             getChildFragmentManager().beginTransaction()
53                     .add(fragment, null)
54                     .commit();
55         }
56     }
57 
58     @Override
onCreatePreferences(@ullable Bundle savedInstanceState, @Nullable String rootKey)59     public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
60         // Preferences will be added by the child fragment later.
61     }
62 
63     @NonNull
64     @Override
createPreference()65     public RolePreference createPreference() {
66         return new HandheldRolePreference(requireContext());
67     }
68 
69     @Override
onPreferenceScreenChanged()70     public void onPreferenceScreenChanged() {
71         requireParent().onPreferenceScreenChanged();
72     }
73 
74     @NonNull
requireParent()75     private Parent requireParent() {
76         //noinspection unchecked
77         return (Parent) requireParentFragment();
78     }
79 
80     /**
81      * Interface that the parent fragment must implement.
82      */
83     public interface Parent {
84 
85         /**
86          * Callback when changes have been made to the {@link androidx.preference.PreferenceScreen}
87          * of this {@link PreferenceFragmentCompat}.
88          */
onPreferenceScreenChanged()89         void onPreferenceScreenChanged();
90     }
91 }
92