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.auto; 18 19 import static com.android.car.ui.core.CarUi.getToolbar; 20 21 import android.os.Bundle; 22 import android.text.TextUtils; 23 import android.view.LayoutInflater; 24 import android.view.View; 25 import android.view.ViewGroup; 26 27 import androidx.annotation.NonNull; 28 import androidx.annotation.Nullable; 29 30 import com.android.car.ui.preference.PreferenceFragment; 31 import com.android.car.ui.toolbar.MenuItem; 32 import com.android.car.ui.toolbar.ToolbarController; 33 34 import java.util.Collections; 35 36 /** Common settings frame for car related settings in permission controller. */ 37 public abstract class AutoSettingsFrameFragment extends PreferenceFragment { 38 39 private ToolbarController mToolbar; 40 41 private CharSequence mLabel; 42 private boolean mIsLoading; 43 private CharSequence mActionLabel; 44 private View.OnClickListener mActionOnClickListener; 45 46 @Override onCreateView(@onNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)47 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, 48 @Nullable Bundle savedInstanceState) { 49 View rootView = super.onCreateView(inflater, container, savedInstanceState); 50 51 mToolbar = getToolbar(requireActivity()); 52 53 updateHeaderLabel(); 54 updateLoading(); 55 updateAction(); 56 57 return rootView; 58 } 59 60 /** Sets the header text of this fragment. */ setHeaderLabel(CharSequence label)61 public void setHeaderLabel(CharSequence label) { 62 mLabel = label; 63 if (getPreferenceScreen() != null) { 64 // Needed because CarUi's preference fragment reads this title 65 getPreferenceScreen().setTitle(mLabel); 66 } 67 updateHeaderLabel(); 68 } 69 70 /** Gets the header text of this fragment. */ getHeaderLabel()71 public CharSequence getHeaderLabel() { 72 return mLabel; 73 } 74 updateHeaderLabel()75 private void updateHeaderLabel() { 76 if (mToolbar != null) { 77 mToolbar.setTitle(mLabel); 78 } 79 } 80 81 /** 82 * Shows a progress view while content is loading. 83 * 84 * @param isLoading {@code true} if the progress view should be visible. 85 */ setLoading(boolean isLoading)86 public void setLoading(boolean isLoading) { 87 mIsLoading = isLoading; 88 updateLoading(); 89 } 90 updateLoading()91 private void updateLoading() { 92 if (mToolbar != null) { 93 mToolbar.getProgressBar().setVisible(mIsLoading); 94 } 95 } 96 97 /** 98 * Shows a button with the given {@code label} that when clicked will call the given {@code 99 * onClickListener}. 100 */ setAction(CharSequence label, View.OnClickListener onClickListener)101 public void setAction(CharSequence label, View.OnClickListener onClickListener) { 102 mActionLabel = label; 103 mActionOnClickListener = onClickListener; 104 updateAction(); 105 } 106 updateAction()107 private void updateAction() { 108 if (mToolbar == null) { 109 return; 110 } 111 if (!TextUtils.isEmpty(mActionLabel) && mActionOnClickListener != null) { 112 mToolbar.setMenuItems(Collections.singletonList(MenuItem.builder(getContext()) 113 .setTitle(mActionLabel) 114 .setOnClickListener(i -> mActionOnClickListener.onClick(null)) 115 .build())); 116 } else { 117 mToolbar.setMenuItems(null); 118 } 119 } 120 } 121