1 /* 2 * Copyright (C) 2018 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.app.ActionBar; 20 import android.os.Bundle; 21 import android.view.LayoutInflater; 22 import android.view.Menu; 23 import android.view.MenuInflater; 24 import android.view.MenuItem; 25 import android.view.View; 26 import android.view.ViewGroup; 27 import android.widget.TextView; 28 29 import androidx.annotation.NonNull; 30 import androidx.annotation.Nullable; 31 import androidx.annotation.StringRes; 32 import androidx.preference.PreferenceFragmentCompat; 33 34 import com.android.modules.utils.build.SdkLevel; 35 import com.android.permissioncontroller.R; 36 import com.android.permissioncontroller.permission.utils.Utils; 37 import com.android.permissioncontroller.role.utils.UiUtils; 38 import com.android.settingslib.HelpUtils; 39 import com.android.settingslib.collapsingtoolbar.CollapsingToolbarBaseFragment; 40 41 import com.google.android.material.appbar.CollapsingToolbarLayout; 42 43 /** 44 * Base class for settings fragments. 45 */ 46 // Made public for com.android.permissioncontroller.role.ui.specialappaccess.handheld 47 public abstract class SettingsFragment extends CollapsingToolbarBaseFragment { 48 49 @NonNull 50 private View mLoadingView; 51 @NonNull 52 private TextView mEmptyText; 53 54 @NonNull 55 private PreferenceFragmentCompat mPreferenceFragment; 56 57 @Override onCreate(@ullable Bundle savedInstanceState)58 public void onCreate(@Nullable Bundle savedInstanceState) { 59 super.onCreate(savedInstanceState); 60 61 setHasOptionsMenu(true); 62 } 63 64 @Override onCreateView(@onNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)65 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, 66 @Nullable Bundle savedInstanceState) { 67 View view = super.onCreateView(inflater, container, savedInstanceState); 68 inflater.inflate(R.layout.settings_fragment_include, getContentFrameLayout()); 69 return view; 70 } 71 72 @Override onViewCreated(@onNull View view, @Nullable Bundle savedInstanceState)73 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { 74 super.onViewCreated(view, savedInstanceState); 75 76 mLoadingView = view.findViewById(R.id.loading); 77 mEmptyText = view.findViewById(R.id.empty); 78 } 79 80 @Override onActivityCreated(@ullable Bundle savedInstanceState)81 public void onActivityCreated(@Nullable Bundle savedInstanceState) { 82 super.onActivityCreated(savedInstanceState); 83 84 ActionBar actionBar = requireActivity().getActionBar(); 85 if (actionBar != null) { 86 actionBar.setDisplayHomeAsUpEnabled(true); 87 } 88 89 if (savedInstanceState == null) { 90 mPreferenceFragment = onCreatePreferenceFragment(); 91 getChildFragmentManager().beginTransaction() 92 .add(R.id.preference_fragment_container, mPreferenceFragment) 93 .commit(); 94 } else { 95 mPreferenceFragment = (PreferenceFragmentCompat) getChildFragmentManager() 96 .findFragmentById(R.id.preference_fragment_container); 97 } 98 mEmptyText.setText(getEmptyTextResource()); 99 updateStateViews(); 100 } 101 102 @NonNull onCreatePreferenceFragment()103 protected abstract PreferenceFragmentCompat onCreatePreferenceFragment(); 104 105 @StringRes getEmptyTextResource()106 protected abstract int getEmptyTextResource(); 107 108 @Override onCreateOptionsMenu(@onNull Menu menu, @NonNull MenuInflater inflater)109 public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) { 110 super.onCreateOptionsMenu(menu, inflater); 111 112 if (!SdkLevel.isAtLeastS()) { 113 Utils.prepareSearchMenuItem(menu, requireContext()); 114 int helpUriResource = getHelpUriResource(); 115 if (helpUriResource != 0) { 116 HelpUtils.prepareHelpMenuItem(requireActivity(), menu, helpUriResource, 117 getClass().getName()); 118 } 119 } 120 } 121 122 @StringRes getHelpUriResource()123 protected int getHelpUriResource() { 124 return 0; 125 } 126 127 @Override onOptionsItemSelected(@onNull MenuItem item)128 public boolean onOptionsItemSelected(@NonNull MenuItem item) { 129 switch (item.getItemId()) { 130 case android.R.id.home: 131 requireActivity().finishAfterTransition(); 132 return true; 133 default: 134 return super.onOptionsItemSelected(item); 135 } 136 } 137 138 /** 139 * Set the title of the current settings page. 140 * 141 * @param title the title of the current settings page 142 */ setTitle(@onNull CharSequence title)143 public void setTitle(@NonNull CharSequence title) { 144 requireActivity().setTitle(title); 145 CollapsingToolbarLayout collapsingToolbarLayout = getCollapsingToolbarLayout(); 146 if (collapsingToolbarLayout != null) { 147 collapsingToolbarLayout.setTitle(title); 148 } 149 } 150 151 /** 152 * Callback when changes have been made to the {@link androidx.preference.PreferenceScreen} 153 * of this {@link PreferenceFragmentCompat}. 154 */ onPreferenceScreenChanged()155 public void onPreferenceScreenChanged() { 156 updateStateViews(); 157 } 158 updateStateViews()159 private void updateStateViews() { 160 boolean isLoading = mPreferenceFragment.getPreferenceManager() == null 161 || mPreferenceFragment.getPreferenceScreen() == null; 162 UiUtils.setViewShown(mLoadingView, isLoading); 163 boolean isEmpty = !isLoading 164 && mPreferenceFragment.getPreferenceScreen().getPreferenceCount() == 0; 165 UiUtils.setViewShown(mEmptyText, isEmpty); 166 } 167 } 168