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