1 /*
2  * Copyright (C) 2020 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.accessibility;
18 
19 import android.content.Context;
20 import android.view.View;
21 
22 import androidx.preference.PreferenceViewHolder;
23 import androidx.preference.SwitchPreference;
24 
25 /**
26  * A switch preference that has a divider below and above. Used for Accessibility Settings use
27  * service.
28  */
29 public final class DividerSwitchPreference extends SwitchPreference {
30 
31     private Boolean mDividerAllowedAbove;
32     private Boolean mDividerAllowBelow;
33     private int mSwitchVisibility;
34 
DividerSwitchPreference(Context context)35     public DividerSwitchPreference(Context context) {
36         super(context);
37         mDividerAllowedAbove = true;
38         mDividerAllowBelow = true;
39         mSwitchVisibility = View.VISIBLE;
40     }
41 
42     @Override
onBindViewHolder(PreferenceViewHolder holder)43     public void onBindViewHolder(PreferenceViewHolder holder) {
44         super.onBindViewHolder(holder);
45         holder.setDividerAllowedAbove(mDividerAllowedAbove);
46         holder.setDividerAllowedBelow(mDividerAllowBelow);
47 
48         final View switchView = holder.itemView.findViewById(android.R.id.widget_frame);
49         if (switchView != null) {
50             switchView.setVisibility(mSwitchVisibility);
51         }
52     }
53 
54     /**
55      * Sets divider whether to show in preference above.
56      *
57      * @param allowed true will be drawn on above this item
58      */
setDividerAllowedAbove(boolean allowed)59     public void setDividerAllowedAbove(boolean allowed) {
60         if (mDividerAllowedAbove != allowed) {
61             mDividerAllowedAbove = allowed;
62             notifyChanged();
63         }
64     }
65 
66     /**
67      * Sets divider whether to show in preference below.
68      *
69      * @param allowed true will be drawn on below this item
70      */
setDividerAllowedBelow(boolean allowed)71     public void setDividerAllowedBelow(boolean allowed) {
72         if (mDividerAllowedAbove != allowed) {
73             mDividerAllowBelow = allowed;
74             notifyChanged();
75         }
76     }
77 
78     /**
79      * Sets the visibility state of Settings view.
80      *
81      * @param visibility one of {@link View#VISIBLE}, {@link View#INVISIBLE}, or {@link View#GONE}.
82      */
setSwitchVisibility(@iew.Visibility int visibility)83     public void setSwitchVisibility(@View.Visibility int visibility) {
84         if (mSwitchVisibility != visibility) {
85             mSwitchVisibility = visibility;
86             notifyChanged();
87         }
88     }
89 }
90