Vachounet | cb0f724 | 2020-10-28 13:26:40 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
micky387 | 9850641 | 2023-10-23 16:33:47 +0200 | [diff] [blame] | 17 | package omnirom.widget; |
Vachounet | cb0f724 | 2020-10-28 13:26:40 +0100 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.TypedArray; |
| 21 | import android.os.UserHandle; |
| 22 | import android.provider.Settings; |
| 23 | import android.util.AttributeSet; |
| 24 | import android.util.Log; |
| 25 | import android.widget.Switch; |
| 26 | |
micky387 | 9850641 | 2023-10-23 16:33:47 +0200 | [diff] [blame] | 27 | import omnirom.preference.R; |
Vachounet | cb0f724 | 2020-10-28 13:26:40 +0100 | [diff] [blame] | 28 | |
| 29 | public class SystemSettingsSwitch extends Switch { |
| 30 | |
| 31 | private boolean mInflated = false; |
| 32 | private int mInitialValue = 0; |
| 33 | |
| 34 | public SystemSettingsSwitch(Context context, AttributeSet attrs, int defStyle) { |
| 35 | super(context, attrs, defStyle); |
| 36 | setInitialValue(context, attrs); |
| 37 | } |
| 38 | |
| 39 | public SystemSettingsSwitch(Context context, AttributeSet attrs) { |
| 40 | super(context, attrs); |
| 41 | setInitialValue(context, attrs); |
| 42 | } |
| 43 | |
| 44 | public SystemSettingsSwitch(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.SystemSettingsSwitch, 0, 0); |
| 52 | mInitialValue = |
| 53 | attributes.getInteger(R.styleable.SystemSettingsSwitch_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.System.getIntForUser(getContext().getContentResolver(), |
| 63 | getResources().getResourceEntryName(getId()), 0, UserHandle.USER_CURRENT) != 0); |
| 64 | break; |
| 65 | case 1: |
| 66 | setChecked(Settings.System.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.System.putIntForUser(getContext().getContentResolver(), |
| 75 | getResources().getResourceEntryName(getId()), checked ? 1 : 0, UserHandle.USER_CURRENT); |
| 76 | super.setChecked(checked); |
| 77 | } |
| 78 | } |