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 33 private OnGearClickListener mOnGearClickListener; 34 GearPreference(Context context, AttributeSet attrs)35 public GearPreference(Context context, AttributeSet attrs) { 36 super(context, attrs); 37 } 38 setOnGearClickListener(OnGearClickListener l)39 public void setOnGearClickListener(OnGearClickListener l) { 40 mOnGearClickListener = l; 41 notifyChanged(); 42 } 43 44 @Override getSecondTargetResId()45 protected int getSecondTargetResId() { 46 return R.layout.preference_widget_gear; 47 } 48 49 @Override shouldHideSecondTarget()50 protected boolean shouldHideSecondTarget() { 51 return mOnGearClickListener == null; 52 } 53 54 @Override onBindViewHolder(PreferenceViewHolder holder)55 public void onBindViewHolder(PreferenceViewHolder holder) { 56 super.onBindViewHolder(holder); 57 final View gear = holder.findViewById(R.id.settings_button); 58 if (mOnGearClickListener != null) { 59 gear.setVisibility(View.VISIBLE); 60 gear.setOnClickListener(this); 61 } else { 62 gear.setVisibility(View.GONE); 63 gear.setOnClickListener(null); 64 } 65 gear.setEnabled(true); // Make gear available even if the preference itself is disabled. 66 } 67 68 @Override onClick(View v)69 public void onClick(View v) { 70 if (v.getId() == R.id.settings_button) { 71 if (mOnGearClickListener != null) { 72 mOnGearClickListener.onGearClick(this); 73 } 74 } 75 } 76 77 public interface OnGearClickListener { onGearClick(GearPreference p)78 void onGearClick(GearPreference p); 79 } 80 } 81