1 /* 2 * Copyright (C) 2016 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.packageinstaller.permission.ui.handheld; 18 19 import android.content.Context; 20 import android.view.View; 21 import android.widget.Switch; 22 23 import androidx.preference.PreferenceViewHolder; 24 import androidx.preference.SwitchPreference; 25 26 class MultiTargetSwitchPreference extends SwitchPreference { 27 private View.OnClickListener mSwitchOnClickLister; 28 MultiTargetSwitchPreference(Context context)29 public MultiTargetSwitchPreference(Context context) { 30 super(context); 31 } 32 setCheckedOverride(boolean checked)33 public void setCheckedOverride(boolean checked) { 34 super.setChecked(checked); 35 } 36 37 @Override setChecked(boolean checked)38 public void setChecked(boolean checked) { 39 // If double target behavior is enabled do nothing 40 if (mSwitchOnClickLister == null) { 41 super.setChecked(checked); 42 } 43 } 44 setSwitchOnClickListener(View.OnClickListener listener)45 public void setSwitchOnClickListener(View.OnClickListener listener) { 46 mSwitchOnClickLister = listener; 47 } 48 49 @Override onBindViewHolder(PreferenceViewHolder holder)50 public void onBindViewHolder(PreferenceViewHolder holder) { 51 super.onBindViewHolder(holder); 52 Switch switchView = holder.itemView.findViewById(android.R.id.switch_widget); 53 if (switchView != null) { 54 switchView.setOnClickListener(mSwitchOnClickLister); 55 56 if (mSwitchOnClickLister != null) { 57 final int padding = (int) ((holder.itemView.getMeasuredHeight() 58 - switchView.getMeasuredHeight()) / 2 + 0.5f); 59 switchView.setPaddingRelative(padding, padding, 0, padding); 60 } 61 } 62 } 63 } 64