1 /* 2 * Copyright (C) 2017 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.settings.widget; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import android.view.View; 22 23 import androidx.preference.PreferenceViewHolder; 24 25 import com.android.settings.R; 26 import com.android.settingslib.RestrictedPreference; 27 28 /** 29 * A preference with a Gear on the side 30 */ 31 public class GearPreference extends RestrictedPreference implements View.OnClickListener { 32 // Default true for gear available even if the preference itself is disabled. 33 protected boolean mGearState = true; 34 private OnGearClickListener mOnGearClickListener; 35 GearPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)36 public GearPreference(Context context, AttributeSet attrs, 37 int defStyleAttr, int defStyleRes) { 38 super(context, attrs, defStyleAttr, defStyleRes); 39 } 40 GearPreference(Context context, AttributeSet attrs)41 public GearPreference(Context context, AttributeSet attrs) { 42 super(context, attrs); 43 } 44 setOnGearClickListener(OnGearClickListener l)45 public void setOnGearClickListener(OnGearClickListener l) { 46 mOnGearClickListener = l; 47 notifyChanged(); 48 } 49 50 /** Sets state of gear button. */ setGearEnabled(boolean enabled)51 public void setGearEnabled(boolean enabled) { 52 mGearState = enabled; 53 } 54 55 /** Gets state of gear button. */ isGearEnabled()56 public boolean isGearEnabled() { 57 return mGearState; 58 } 59 60 @Override getSecondTargetResId()61 protected int getSecondTargetResId() { 62 return R.layout.preference_widget_gear; 63 } 64 65 @Override shouldHideSecondTarget()66 protected boolean shouldHideSecondTarget() { 67 return mOnGearClickListener == null; 68 } 69 70 @Override onBindViewHolder(PreferenceViewHolder holder)71 public void onBindViewHolder(PreferenceViewHolder holder) { 72 super.onBindViewHolder(holder); 73 final View gear = holder.findViewById(R.id.settings_button); 74 if (mOnGearClickListener != null) { 75 gear.setVisibility(View.VISIBLE); 76 gear.setOnClickListener(this); 77 } else { 78 gear.setVisibility(View.GONE); 79 gear.setOnClickListener(null); 80 } 81 // Make gear available even if the preference itself is disabled. 82 gear.setEnabled(mGearState); 83 } 84 85 @Override onClick(View v)86 public void onClick(View v) { 87 if (v.getId() == R.id.settings_button) { 88 if (mOnGearClickListener != null) { 89 mOnGearClickListener.onGearClick(this); 90 } 91 } 92 } 93 94 public interface OnGearClickListener { onGearClick(GearPreference p)95 void onGearClick(GearPreference p); 96 } 97 } 98