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.FocusArea; 31 import com.android.car.ui.R; 32 import com.android.car.ui.baselayout.Insets; 33 import com.android.car.ui.preference.PreferenceFragment; 34 import com.android.car.ui.toolbar.MenuItem; 35 import com.android.car.ui.toolbar.ToolbarController; 36 37 import java.util.Collections; 38 39 /** Common settings frame for car related settings in permission controller. */ 40 public abstract class AutoSettingsFrameFragment extends PreferenceFragment { 41 42 private ToolbarController mToolbar; 43 44 private CharSequence mLabel; 45 private boolean mIsLoading; 46 private CharSequence mActionLabel; 47 private View.OnClickListener mActionOnClickListener; 48 49 @Override onCreateView(@onNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)50 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, 51 @Nullable Bundle savedInstanceState) { 52 View rootView = super.onCreateView(inflater, container, savedInstanceState); 53 54 mToolbar = getToolbar(requireActivity()); 55 56 updateHeaderLabel(); 57 updateLoading(); 58 updateAction(); 59 60 return rootView; 61 } 62 @Override onCarUiInsetsChanged(Insets insets)63 public void onCarUiInsetsChanged(Insets insets) { 64 // don't allow scrolling behind the toolbar to be consistent with the rest of Settings 65 // reference UI. Scrolling behind toolbar also leads to flakier tests due to UI being 66 // visible but clicks are intercepted and dropped by the toolbar. 67 FocusArea focusArea = getView().findViewById(R.id.car_ui_focus_area); 68 focusArea.setHighlightPadding( 69 /* left= */ 0, /* top= */ 0, /* right= */ 0, /* bottom= */ 0); 70 focusArea.setBoundsOffset(/* left= */ 0, /* top= */ 0, /* right= */ 0, /* bottom= */ 0); 71 getView().setPadding( 72 insets.getLeft(), insets.getTop(), insets.getRight(), insets.getBottom()); 73 getCarUiRecyclerView().setPadding( 74 /* left= */ 0, /* top= */ 0, /* right= */ 0, /* bottom= */ 0); 75 } 76 77 /** Sets the header text of this fragment. */ setHeaderLabel(CharSequence label)78 public void setHeaderLabel(CharSequence label) { 79 mLabel = label; 80 if (getPreferenceScreen() != null) { 81 // Needed because CarUi's preference fragment reads this title 82 getPreferenceScreen().setTitle(mLabel); 83 } 84 updateHeaderLabel(); 85 } 86 87 /** Gets the header text of this fragment. */ getHeaderLabel()88 public CharSequence getHeaderLabel() { 89 return mLabel; 90 } 91 updateHeaderLabel()92 private void updateHeaderLabel() { 93 if (mToolbar != null) { 94 mToolbar.setTitle(mLabel); 95 } 96 } 97 98 /** 99 * Shows a progress view while content is loading. 100 * 101 * @param isLoading {@code true} if the progress view should be visible. 102 */ setLoading(boolean isLoading)103 public void setLoading(boolean isLoading) { 104 mIsLoading = isLoading; 105 updateLoading(); 106 } 107 updateLoading()108 private void updateLoading() { 109 if (mToolbar != null) { 110 mToolbar.getProgressBar().setVisible(mIsLoading); 111 } 112 } 113 114 /** 115 * Shows a button with the given {@code label} that when clicked will call the given {@code 116 * onClickListener}. 117 */ setAction(CharSequence label, View.OnClickListener onClickListener)118 public void setAction(CharSequence label, View.OnClickListener onClickListener) { 119 mActionLabel = label; 120 mActionOnClickListener = onClickListener; 121 updateAction(); 122 } 123 updateAction()124 private void updateAction() { 125 if (mToolbar == null) { 126 return; 127 } 128 if (!TextUtils.isEmpty(mActionLabel) && mActionOnClickListener != null) { 129 mToolbar.setMenuItems(Collections.singletonList(MenuItem.builder(getContext()) 130 .setTitle(mActionLabel) 131 .setOnClickListener(i -> mActionOnClickListener.onClick(null)) 132 .build())); 133 } else { 134 mToolbar.setMenuItems(null); 135 } 136 } 137 } 138