From e27ec25e44c4dc0d06a850e03fc50ade6e18a89f Mon Sep 17 00:00:00 2001 From: Anay Wadhera Date: Sat, 31 Dec 2022 05:25:11 +0000 Subject: [PATCH] FlagFlipper: rewrite boot receiver in kotlin Change-Id: I428d61113fd2722e3fd0d5ca092385687fcef9ab --- Android.bp | 2 +- .../statix/systemuiflags/BootReceiver.java | 57 ------------------- src/com/statix/systemuiflags/BootReceiver.kt | 57 +++++++++++++++++++ 3 files changed, 58 insertions(+), 58 deletions(-) delete mode 100644 src/com/statix/systemuiflags/BootReceiver.java create mode 100644 src/com/statix/systemuiflags/BootReceiver.kt diff --git a/Android.bp b/Android.bp index acf2670..97be983 100644 --- a/Android.bp +++ b/Android.bp @@ -14,7 +14,7 @@ android_app { name: "SystemUIFlagFlipper", - srcs: ["src/**/*.java"], + srcs: ["src/**/*.kt"], static_libs: ["SystemUIFlagsLib"], resource_dirs: ["res"], platform_apis: true, diff --git a/src/com/statix/systemuiflags/BootReceiver.java b/src/com/statix/systemuiflags/BootReceiver.java deleted file mode 100644 index 9b3bd78..0000000 --- a/src/com/statix/systemuiflags/BootReceiver.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (C) 2022 StatiXOS - * - * 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 com.statix.systemuiflags; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.os.Handler; -import android.os.Looper; -import android.util.Log; - -import com.android.systemui.flags.FlagManager; - -public class BootReceiver extends BroadcastReceiver { - private static final String TAG = "SystemUIFlagFlipper"; - - @Override - public void onReceive(Context context, Intent intent) { - FlagManager manager = new FlagManager(context, new Handler(Looper.getMainLooper())); - new Thread(() -> { - Log.i(TAG, "Updating SystemUIFlags at boot"); - updateFlags(context, manager); - }).start(); - } - - private void updateFlags(Context context, FlagManager flagManager) { - updateConfig(context, R.array.flags, flagManager); - } - - private void updateConfig(Context context, int configArray, FlagManager flagManager) { - // Set current properties - String[] rawProperties = context.getResources().getStringArray(configArray); - for (String property : rawProperties) { - // Format: namespace/key=value - String[] kv = property.split("="); - int id = Integer.parseInt(kv[0]); - boolean enabled = Boolean.parseBoolean(kv[1]); - Log.i(TAG, "Setting flag " + id + " to " + enabled); - - flagManager.setFlagValue(id, enabled); - } - } -} diff --git a/src/com/statix/systemuiflags/BootReceiver.kt b/src/com/statix/systemuiflags/BootReceiver.kt new file mode 100644 index 0000000..af5be9b --- /dev/null +++ b/src/com/statix/systemuiflags/BootReceiver.kt @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2022 StatiXOS + * + * 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 com.statix.systemuiflags + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.os.Handler +import android.os.Looper +import android.util.Log + +import com.android.systemui.flags.FlagManager + +class BootReceiver: BroadcastReceiver() { + companion object { + private const val TAG = "SystemUIFlagFlipper" + } + + override fun onReceive(context: Context, intent: Intent) { + val manager = FlagManager(context, Handler(Looper.getMainLooper())) + Thread({ + Log.i(TAG, "Updating SystemUIFlags at boot") + updateFlags(context, manager) + }).start() + } + + private fun updateFlags(context: Context, flagManager: FlagManager) { + updateConfig(context, R.array.flags, flagManager) + } + + private fun updateConfig(context: Context, configArray: Int, flagManager: FlagManager) { + // Set current properties + val rawProperties = context.resources.getStringArray(configArray) + for (property: String in rawProperties) { + // Format: namespace/key=value + val kv = property.split("=") + val id = kv[0].toInt() + val enabled = kv[1].toBoolean() + Log.i(TAG, "Setting flag ${id} to ${enabled}") + flagManager.setFlagValue(id, enabled) + } + } +}