1 package com.android.settings; 2 3 import android.content.ComponentName; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.support.v7.preference.PreferenceViewHolder; 7 import android.util.AttributeSet; 8 import android.view.View; 9 import android.view.ViewGroup; 10 11 /** 12 * An AppListPreference with optional settings button. 13 */ 14 public class AppListPreferenceWithSettings extends AppListPreference { 15 16 private View mSettingsIcon; 17 private ComponentName mSettingsComponent; 18 AppListPreferenceWithSettings(Context context, AttributeSet attrs)19 public AppListPreferenceWithSettings(Context context, AttributeSet attrs) { 20 super(context, attrs); 21 setWidgetLayoutResource(R.layout.preference_widget_settings); 22 } 23 24 @Override onBindViewHolder(PreferenceViewHolder view)25 public void onBindViewHolder(PreferenceViewHolder view) { 26 super.onBindViewHolder(view); 27 28 mSettingsIcon = view.findViewById(R.id.settings_button); 29 mSettingsIcon.setOnClickListener(new View.OnClickListener() { 30 @Override 31 public void onClick(View v) { 32 Intent intent = new Intent(Intent.ACTION_MAIN); 33 intent.setComponent(mSettingsComponent); 34 getContext().startActivity(new Intent(intent)); 35 } 36 }); 37 38 ViewGroup container = (ViewGroup) mSettingsIcon.getParent(); 39 container.setPaddingRelative(0, 0, 0, 0); 40 41 updateSettingsVisibility(); 42 } 43 updateSettingsVisibility()44 private void updateSettingsVisibility() { 45 if (mSettingsIcon == null) { 46 return; 47 } 48 49 if (mSettingsComponent == null) { 50 mSettingsIcon.setVisibility(View.GONE); 51 } else { 52 mSettingsIcon.setVisibility(View.VISIBLE); 53 } 54 } 55 setSettingsComponent(ComponentName settings)56 protected void setSettingsComponent(ComponentName settings) { 57 mSettingsComponent = settings; 58 updateSettingsVisibility(); 59 } 60 } 61