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.util.AttributeSet; 21 import android.view.View; 22 23 import androidx.annotation.AttrRes; 24 import androidx.annotation.NonNull; 25 import androidx.annotation.Nullable; 26 import androidx.annotation.StyleRes; 27 import androidx.preference.Preference; 28 import androidx.preference.PreferenceViewHolder; 29 30 import com.android.permissioncontroller.R; 31 32 /** 33 * {@link Preference} with a settings button. 34 * 35 * @see com.android.settings.widget.GearPreference 36 */ 37 class SettingsButtonPreference extends HandHeldTwoTargetPreference { 38 39 @Nullable 40 private OnSecondTargetClickListener mOnSecondTargetClickListener; 41 SettingsButtonPreference(@onNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes)42 SettingsButtonPreference(@NonNull Context context, @Nullable AttributeSet attrs, 43 @AttrRes int defStyleAttr, @StyleRes int defStyleRes) { 44 super(context, attrs, defStyleAttr, defStyleRes); 45 46 init(); 47 } 48 SettingsButtonPreference(@onNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr)49 SettingsButtonPreference(@NonNull Context context, @Nullable AttributeSet attrs, 50 @AttrRes int defStyleAttr) { 51 super(context, attrs, defStyleAttr); 52 53 init(); 54 } 55 SettingsButtonPreference(@onNull Context context, @Nullable AttributeSet attrs)56 SettingsButtonPreference(@NonNull Context context, @Nullable AttributeSet attrs) { 57 super(context, attrs); 58 59 init(); 60 } 61 SettingsButtonPreference(@onNull Context context)62 SettingsButtonPreference(@NonNull Context context) { 63 super(context); 64 65 init(); 66 } 67 init()68 private void init() { 69 setWidgetLayoutResource(R.layout.settings_button_preference_widget); 70 } 71 72 @Override setOnSecondTargetClickListener(@ullable OnSecondTargetClickListener listener)73 public void setOnSecondTargetClickListener(@Nullable OnSecondTargetClickListener listener) { 74 mOnSecondTargetClickListener = listener; 75 notifyChanged(); 76 } 77 78 @Override onBindViewHolder(@onNull PreferenceViewHolder holder)79 public void onBindViewHolder(@NonNull PreferenceViewHolder holder) { 80 super.onBindViewHolder(holder); 81 82 View widgetFrame = holder.findViewById(android.R.id.widget_frame); 83 widgetFrame.setPadding(0, 0, 0, 0); 84 View settingsButton = holder.findViewById(R.id.settings_button); 85 if (mOnSecondTargetClickListener != null) { 86 widgetFrame.setVisibility(View.VISIBLE); 87 settingsButton.setOnClickListener(view -> 88 mOnSecondTargetClickListener.onSecondTargetClick(this)); 89 } else { 90 widgetFrame.setVisibility(View.GONE); 91 settingsButton.setOnClickListener(null); 92 } 93 // Make the settings button enabled even if the preference itself is disabled. 94 settingsButton.setEnabled(true); 95 } 96 } 97