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