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