OmniLib: add SecureSettingsSwitch and SystemSettingsSwitch

Update: add TypedArray recycle

Change-Id: I0afb459061da29e4007ad8e76c6e02df192d0de6
diff --git a/res/values/custom_attrs.xml b/res/values/custom_attrs.xml
index c7b38dc..b17034b 100644
--- a/res/values/custom_attrs.xml
+++ b/res/values/custom_attrs.xml
@@ -31,4 +31,13 @@
         <attr name="multiColor" format="boolean" />
         <attr name="withAlpha" format="boolean" />
     </declare-styleable>
-</resources>
\ No newline at end of file
+
+    <declare-styleable name="SecureSettingsSwitch">
+        <attr name="initialValue" format="integer" />
+    </declare-styleable>
+
+    <declare-styleable name="SystemSettingsSwitch">
+        <attr name="initialValue" format="integer" />
+    </declare-styleable>
+
+</resources>
diff --git a/src/org/omnirom/omnilib/widget/SecureSettingsSwitch.java b/src/org/omnirom/omnilib/widget/SecureSettingsSwitch.java
new file mode 100644
index 0000000..621c2b1
--- /dev/null
+++ b/src/org/omnirom/omnilib/widget/SecureSettingsSwitch.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2020 The OmniROM Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.omnirom.omnilib.widget;
+
+import android.content.Context;
+import android.content.res.TypedArray;
+import android.os.UserHandle;
+import android.provider.Settings;
+import android.util.AttributeSet;
+import android.util.Log;
+import android.widget.Switch;
+
+import org.omnirom.omnilib.R;
+
+public class SecureSettingsSwitch extends Switch {
+
+    private boolean mInflated = false;
+    private int mInitialValue = 0;
+
+    public SecureSettingsSwitch(Context context, AttributeSet attrs, int defStyle) {
+        super(context, attrs, defStyle);
+        setInitialValue(context, attrs);
+    }
+
+    public SecureSettingsSwitch(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        setInitialValue(context, attrs);
+    }
+
+    public SecureSettingsSwitch(Context context) {
+        super(context, null);
+    }
+
+    private void setInitialValue(Context context, AttributeSet attrs) {
+        if (attrs == null) return;
+        final TypedArray attributes = context.obtainStyledAttributes(attrs,
+                R.styleable.SecureSettingsSwitch, 0, 0);
+        mInitialValue =
+                attributes.getInteger(R.styleable.SecureSettingsSwitch_initialValue, 0);
+        attributes.recycle();
+    }
+
+    @Override
+    protected void onFinishInflate() {
+        mInflated = true;
+        switch (mInitialValue) {
+            case 0:
+                setChecked(Settings.Secure.getIntForUser(getContext().getContentResolver(),
+                    getResources().getResourceEntryName(getId()), 0, UserHandle.USER_CURRENT) != 0);
+                break;
+            case 1:
+                setChecked(Settings.Secure.getIntForUser(getContext().getContentResolver(),
+                    getResources().getResourceEntryName(getId()), 1, UserHandle.USER_CURRENT) == 1);
+        }
+    }
+
+    @Override
+    public void setChecked(boolean checked) {
+        if (!mInflated) return;
+        Settings.Secure.putIntForUser(getContext().getContentResolver(),
+                getResources().getResourceEntryName(getId()), checked ? 1 : 0, UserHandle.USER_CURRENT);
+        super.setChecked(checked);
+    }
+}
diff --git a/src/org/omnirom/omnilib/widget/SystemSettingsSwitch.java b/src/org/omnirom/omnilib/widget/SystemSettingsSwitch.java
new file mode 100644
index 0000000..0f7c73c
--- /dev/null
+++ b/src/org/omnirom/omnilib/widget/SystemSettingsSwitch.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2020 The OmniROM Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.omnirom.omnilib.widget;
+
+import android.content.Context;
+import android.content.res.TypedArray;
+import android.os.UserHandle;
+import android.provider.Settings;
+import android.util.AttributeSet;
+import android.util.Log;
+import android.widget.Switch;
+
+import org.omnirom.omnilib.R;
+
+public class SystemSettingsSwitch extends Switch {
+
+    private boolean mInflated = false;
+    private int mInitialValue = 0;
+
+    public SystemSettingsSwitch(Context context, AttributeSet attrs, int defStyle) {
+        super(context, attrs, defStyle);
+        setInitialValue(context, attrs);
+    }
+
+    public SystemSettingsSwitch(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        setInitialValue(context, attrs);
+    }
+
+    public SystemSettingsSwitch(Context context) {
+        super(context, null);
+    }
+
+    private void setInitialValue(Context context, AttributeSet attrs) {
+        if (attrs == null) return;
+        final TypedArray attributes = context.obtainStyledAttributes(attrs,
+                R.styleable.SystemSettingsSwitch, 0, 0);
+        mInitialValue =
+                attributes.getInteger(R.styleable.SystemSettingsSwitch_initialValue, 0);
+        attributes.recycle();
+    }
+
+    @Override
+    protected void onFinishInflate() {
+        mInflated = true;
+        switch (mInitialValue) {
+            case 0:
+                setChecked(Settings.System.getIntForUser(getContext().getContentResolver(),
+                    getResources().getResourceEntryName(getId()), 0, UserHandle.USER_CURRENT) != 0);
+                break;
+            case 1:
+                setChecked(Settings.System.getIntForUser(getContext().getContentResolver(),
+                    getResources().getResourceEntryName(getId()), 1, UserHandle.USER_CURRENT) == 1);
+        }
+    }
+
+    @Override
+    public void setChecked(boolean checked) {
+        if (!mInflated) return;
+        Settings.System.putIntForUser(getContext().getContentResolver(),
+                getResources().getResourceEntryName(getId()), checked ? 1 : 0, UserHandle.USER_CURRENT);
+        super.setChecked(checked);
+    }
+}