blob: 02cfda783797b62b06bdc1c708d1fa3b4270243d [file] [log] [blame]
Vachounetcb0f7242020-10-28 13:26:40 +01001/*
2 * Copyright (C) 2020 The OmniROM 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
micky38798506412023-10-23 16:33:47 +020017package omnirom.widget;
Vachounetcb0f7242020-10-28 13:26:40 +010018
19import android.content.Context;
20import android.content.res.TypedArray;
21import android.os.UserHandle;
22import android.provider.Settings;
23import android.util.AttributeSet;
24import android.util.Log;
25import android.widget.Switch;
26
micky38798506412023-10-23 16:33:47 +020027import omnirom.preference.R;
Vachounetcb0f7242020-10-28 13:26:40 +010028
29public class SecureSettingsSwitch extends Switch {
30
31 private boolean mInflated = false;
32 private int mInitialValue = 0;
33
34 public SecureSettingsSwitch(Context context, AttributeSet attrs, int defStyle) {
35 super(context, attrs, defStyle);
36 setInitialValue(context, attrs);
37 }
38
39 public SecureSettingsSwitch(Context context, AttributeSet attrs) {
40 super(context, attrs);
41 setInitialValue(context, attrs);
42 }
43
44 public SecureSettingsSwitch(Context context) {
45 super(context, null);
46 }
47
48 private void setInitialValue(Context context, AttributeSet attrs) {
49 if (attrs == null) return;
50 final TypedArray attributes = context.obtainStyledAttributes(attrs,
51 R.styleable.SecureSettingsSwitch, 0, 0);
52 mInitialValue =
53 attributes.getInteger(R.styleable.SecureSettingsSwitch_initialValue, 0);
54 attributes.recycle();
55 }
56
57 @Override
58 protected void onFinishInflate() {
59 mInflated = true;
60 switch (mInitialValue) {
61 case 0:
62 setChecked(Settings.Secure.getIntForUser(getContext().getContentResolver(),
63 getResources().getResourceEntryName(getId()), 0, UserHandle.USER_CURRENT) != 0);
64 break;
65 case 1:
66 setChecked(Settings.Secure.getIntForUser(getContext().getContentResolver(),
67 getResources().getResourceEntryName(getId()), 1, UserHandle.USER_CURRENT) == 1);
68 }
69 }
70
71 @Override
72 public void setChecked(boolean checked) {
73 if (!mInflated) return;
74 Settings.Secure.putIntForUser(getContext().getContentResolver(),
75 getResources().getResourceEntryName(getId()), checked ? 1 : 0, UserHandle.USER_CURRENT);
76 super.setChecked(checked);
77 }
78}