zenfone9: DeviceParts: Add 240Hz touchscreen rate in gaming mode

Change-Id: I052e16bc24d04fc27890164fcd6ba7759b57c7c6
diff --git a/DeviceParts/AndroidManifest.xml b/DeviceParts/AndroidManifest.xml
index f282ebe..c168c05 100644
--- a/DeviceParts/AndroidManifest.xml
+++ b/DeviceParts/AndroidManifest.xml
@@ -98,6 +98,17 @@
             </intent-filter>
         </service>
         <service
+            android:name="org.omnirom.device.GameTileService"
+            android:icon="@drawable/ic_game_rate"
+            android:label="@string/game_mode_title"
+            android:permission="android.permission.BIND_QUICK_SETTINGS_TILE"
+            android:exported="true">
+            <intent-filter>
+                <action
+                    android:name="android.service.quicksettings.action.QS_TILE"/>
+            </intent-filter>
+        </service>
+        <service
             android:name="org.omnirom.device.FrameRateTileService"
             android:icon="@drawable/ic_refresh_rate"
             android:label="@string/refresh_rate_title"
diff --git a/DeviceParts/res/drawable/ic_game_rate.xml b/DeviceParts/res/drawable/ic_game_rate.xml
new file mode 100644
index 0000000..cf48eba
--- /dev/null
+++ b/DeviceParts/res/drawable/ic_game_rate.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>

+<vector xmlns:android="http://schemas.android.com/apk/res/android"

+            android:height="24dp"

+            android:width="24dp"

+            android:viewportWidth="24"

+            android:viewportHeight="24">

+    <path android:fillColor="#ffffffff"

+          android:pathData="M20.47,8c-0.39,-2.3 -2.4,-4 -4.81,-4L8.34,4C5.93,4

+              3.93,5.7 3.53,8 3.53,8 2,16.8 2,16.93 2,18.07 2.93,19 4.07,19c0.57,0

+              1.09,-0.2 1.47,-0.58L9,15h6l3.46,3.42c0.38,0.38 0.89,0.58 1.47,0.58

+              1.15,0 2.07,-0.93 2.07,-2.07C22,16.8

+              20.47,8 20.47,8zM11,10L9,10v2L8,12v-2L6,10L6,9h2L8,7h1v2h2v1zM13.8,10.3c-0.44,0

+              -0.8,-0.36 -0.8,-0.8 0,-0.44 0.36,-0.8 0.8,-0.8 0.44,0 0.8,0.36 0.8,0.8 0,0.44

+              -0.36,0.8 -0.8,0.8zM15.49,12c-0.44,0 -0.8,-0.36 -0.8,-0.8 0,-0.44 0.36,-0.8

+              0.8,-0.8 0.44,0 0.8,0.36 0.8,0.8 0,0.44 -0.36,0.8 -0.8,0.8zM15.49,8.6c-0.44,0

+              -0.8,-0.36 -0.8,-0.8s0.36,-0.8 0.8,-0.8c0.44,0 0.8,0.36 0.8,0.8s-0.36,0.8

+              -0.8,0.8zM17.2,10.3c-0.44,0 -0.8,-0.36 -0.8,-0.8 0,-0.44 0.36,-0.8

+              0.8,-0.8s0.8,0.36 0.8,0.8c0,0.44 -0.36,0.8 -0.8,0.8z"/>

+</vector>

diff --git a/DeviceParts/res/values/strings.xml b/DeviceParts/res/values/strings.xml
index c94aa29..cc68e8f 100644
--- a/DeviceParts/res/values/strings.xml
+++ b/DeviceParts/res/values/strings.xml
@@ -23,6 +23,9 @@
     <string name="advanced_settings">DeviceParts</string>
     <string name="graphics_title">Graphics</string>
     <string name="screen_title">Screen Sensitivity</string>
+    <string name="game_title">Touch Screen Sensitivity</string>
+    <string name="game_mode_title">Game mode</string>
+    <string name="game_mode_summary">Increase the touchscreen rate to 240Hz</string>
     <string name="glove_mode_summary">Increase touch sensitivity when wearing gloves</string>
     <string name="glove_mode_title">Glove mode</string>
     <string name="proxi_summary_head">Check proximity sensor for handling fingerprint sensor</string>
diff --git a/DeviceParts/res/xml/main.xml b/DeviceParts/res/xml/main.xml
index 6d37bd0..ab6c078 100644
--- a/DeviceParts/res/xml/main.xml
+++ b/DeviceParts/res/xml/main.xml
@@ -71,6 +71,16 @@
     </PreferenceCategory>
 
     <PreferenceCategory
+        android:key="game_mode"
+        android:title="@string/game_title">
+        <SwitchPreference
+            android:key="game_refresh_rate"
+            android:summary="@string/game_mode_summary"
+            android:title="@string/game_mode_title"
+            android:persistent="false" />
+    </PreferenceCategory>
+
+    <PreferenceCategory
         android:key="screen"
         android:title="@string/screen_title">
         <SwitchPreference
diff --git a/DeviceParts/src/org/omnirom/device/DeviceSettings.java b/DeviceParts/src/org/omnirom/device/DeviceSettings.java
index a4d37ba..4418134 100644
--- a/DeviceParts/src/org/omnirom/device/DeviceSettings.java
+++ b/DeviceParts/src/org/omnirom/device/DeviceSettings.java
@@ -49,6 +49,7 @@
 
     protected static final String DEFAULT_FPS_VALUE = "60";
     private static final String KEY_CATEGORY_SCREEN = "screen";
+    public static final String KEY_GAME_SWITCH = "game_refresh_rate";
     public static final String KEY_GLOVE_SWITCH = "glove";
     private static final String KEY_FRAME_MODE = "frame_mode_key";
     private static final String KEY_FRAME_CATEGORY = "frame_mode_main";
@@ -61,6 +62,7 @@
 
     private static ListPreference mFrameModeRate;
     private static Preference mWizard;
+    private static TwoStatePreference mGameModeSwitch;
     private static TwoStatePreference mGloveModeSwitch;
 
     private static final String SURFACE_FLINGER_SERVICE_KEY = "SurfaceFlinger";
@@ -81,6 +83,11 @@
         mFrameModeRate.setSummary(mFrameModeRate.getEntry());
         mFrameModeRate.setOnPreferenceChangeListener(this);
 
+        mGameModeSwitch = (TwoStatePreference) findPreference(KEY_GAME_SWITCH);
+        mGameModeSwitch.setEnabled(GameModeSwitch.isSupported());
+        mGameModeSwitch.setChecked(GameModeSwitch.isCurrentlyEnabled(this.getContext()));
+        mGameModeSwitch.setOnPreferenceChangeListener(new GameModeSwitch(getContext()));
+
         mGloveModeSwitch = (TwoStatePreference) findPreference(KEY_GLOVE_SWITCH);
         mGloveModeSwitch.setEnabled(GloveModeSwitch.isSupported());
         mGloveModeSwitch.setChecked(GloveModeSwitch.isCurrentlyEnabled(this.getContext()));
diff --git a/DeviceParts/src/org/omnirom/device/GameModeSwitch.java b/DeviceParts/src/org/omnirom/device/GameModeSwitch.java
new file mode 100644
index 0000000..1de3ab4
--- /dev/null
+++ b/DeviceParts/src/org/omnirom/device/GameModeSwitch.java
@@ -0,0 +1,61 @@
+/*
+* Copyright (C) 2024 The OmniROM Project
+*
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 2 of the License, or
+* (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program. If not, see <http://www.gnu.org/licenses/>.
+*
+*/
+package org.omnirom.device;
+
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.provider.Settings;
+import androidx.preference.Preference;
+import androidx.preference.Preference.OnPreferenceChangeListener;
+import androidx.preference.PreferenceManager;
+
+public class GameModeSwitch implements OnPreferenceChangeListener {
+
+    private static final String FILE = "/proc/driver/gaming_mode";
+
+    public static final String SETTINGS_KEY = DeviceSettings.KEY_SETTINGS_PREFIX + DeviceSettings.KEY_GAME_SWITCH;
+
+    private Context mContext;
+
+    public GameModeSwitch(Context context) {
+        mContext = context;
+    }
+
+    public static String getFile() {
+        if (Utils.fileWritable(FILE)) {
+            return FILE;
+        }
+        return null;
+    }
+
+    public static boolean isSupported() {
+        return Utils.fileWritable(getFile());
+    }
+
+    public static boolean isCurrentlyEnabled(Context context) {
+        return Utils.getFileValueAsBoolean(getFile(), false);
+    }
+
+    @Override
+    public boolean onPreferenceChange(Preference preference, Object newValue) {
+        Boolean enabled = (Boolean) newValue;
+        Settings.System.putInt(mContext.getContentResolver(), SETTINGS_KEY, enabled ? 1 : 0);
+        Utils.writeValue(getFile(), enabled ? "1" : "0");
+        return true;
+    }
+}
\ No newline at end of file
diff --git a/DeviceParts/src/org/omnirom/device/GameTileService.java b/DeviceParts/src/org/omnirom/device/GameTileService.java
new file mode 100644
index 0000000..2a8904f
--- /dev/null
+++ b/DeviceParts/src/org/omnirom/device/GameTileService.java
@@ -0,0 +1,60 @@
+/*
+* Copyright (C) 2024 The OmniROM Project
+*
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 2 of the License, or
+* (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program. If not, see <http://www.gnu.org/licenses/>.
+*
+*/
+package org.omnirom.device;
+
+import android.annotation.TargetApi;
+import android.provider.Settings;
+import android.service.quicksettings.Tile;
+import android.service.quicksettings.TileService;
+
+@TargetApi(24)
+public class GameTileService extends TileService {
+
+    @Override
+    public void onDestroy() {
+    }
+
+    @Override
+    public void onTileAdded() {
+        getQsTile().updateTile();
+    }
+
+    @Override
+    public void onTileRemoved() {
+    }
+
+    @Override
+    public void onStartListening() {
+        boolean enabled = Settings.System.getInt(this.getContentResolver(), GameModeSwitch.SETTINGS_KEY, 0) != 0;
+        getQsTile().setState(enabled ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE);
+        getQsTile().updateTile();
+    }
+
+    @Override
+    public void onStopListening() {
+    }
+
+    @Override
+    public void onClick() {
+        boolean enabled = Settings.System.getInt(this.getContentResolver(), GameModeSwitch.SETTINGS_KEY, 0) != 0;
+        Settings.System.putInt(this.getContentResolver(), GameModeSwitch.SETTINGS_KEY, enabled ? 0 : 1);
+        Utils.writeValue(GameModeSwitch.getFile(), enabled ? "0" : "1");
+        getQsTile().setState(enabled ? Tile.STATE_INACTIVE : Tile.STATE_ACTIVE);
+        getQsTile().updateTile();
+    }
+}
diff --git a/DeviceParts/src/org/omnirom/device/Startup.java b/DeviceParts/src/org/omnirom/device/Startup.java
index 1959f2e..e2efe92 100644
--- a/DeviceParts/src/org/omnirom/device/Startup.java
+++ b/DeviceParts/src/org/omnirom/device/Startup.java
@@ -48,6 +48,8 @@
         boolean imported = Settings.System.getInt(context.getContentResolver(), "omni_device_setting_imported", 0) != 0;
         if (!imported) {
             SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
+            boolean gameEnabled = sharedPrefs.getBoolean(DeviceSettings.KEY_GAME_SWITCH, false);
+            Settings.System.putInt(context.getContentResolver(), GameModeSwitch.SETTINGS_KEY, gameEnabled ? 1 : 0);
             boolean enabled = sharedPrefs.getBoolean(DeviceSettings.KEY_GLOVE_SWITCH, false);
             Settings.System.putInt(context.getContentResolver(), GloveModeSwitch.SETTINGS_KEY, enabled ? 1 : 0);
 
@@ -139,6 +141,11 @@
         restore(getGestureFile(GestureSettings.OFFSCREEN_PATH), valueExtra);
         }
 
+        boolean enabledGame = Settings.System.getInt(context.getContentResolver(), GameModeSwitch.SETTINGS_KEY, 0) != 0;
+        if (enabledGame) {
+            restore(GameModeSwitch.getFile(), enabledGame);
+        }
+
         boolean enabledGlove = Settings.System.getInt(context.getContentResolver(), GloveModeSwitch.SETTINGS_KEY, 0) != 0;
         if (enabledGlove) {
             restore(GloveModeSwitch.getFile(), enabledGlove);
diff --git a/DeviceParts/src/org/omnirom/device/TileActivity.java b/DeviceParts/src/org/omnirom/device/TileActivity.java
index 3de9bdd..cf5defd 100644
--- a/DeviceParts/src/org/omnirom/device/TileActivity.java
+++ b/DeviceParts/src/org/omnirom/device/TileActivity.java
@@ -28,6 +28,7 @@
         Intent intent = new Intent();
         String className = ((ComponentName) getIntent().getParcelableExtra("android.intent.extra.COMPONENT_NAME")).getClassName();
         if (className.equals("org.omnirom.device.FrameRateTileService") ||
+                className.equals("org.omnirom.device.GameTileService") ||
                 className.equals("org.omnirom.device.GloveModeTileService")) {
             intent.setPackage("org.omnirom.device");
             intent.setAction("org.omnirom.device.DEVICE_SETTING_PAGE");
diff --git a/prebuilt/vendor/etc/init/hw/init.asus.rc b/prebuilt/vendor/etc/init/hw/init.asus.rc
index 3cc439c..800b354 100644
--- a/prebuilt/vendor/etc/init/hw/init.asus.rc
+++ b/prebuilt/vendor/etc/init/hw/init.asus.rc
@@ -449,11 +449,6 @@
 on property:vendor.audio.in_call=0
    write /sys/devices/platform/soc/990000.i2c/i2c-0/0-0038/fts_phone_state 0
 
-on property:vendor.asus.gamingtype=1
-   write /sys/devices/platform/soc/990000.i2c/i2c-0/0-0038/fts_game_mode 1
-on property:vendor.asus.gamingtype=0
-   write /sys/devices/platform/soc/990000.i2c/i2c-0/0-0038/fts_game_mode 0
-
 on property:vendor.screen.rotation=0
    write /sys/devices/platform/soc/990000.i2c/i2c-0/0-0038/fts_rotation_mode 0
 on property:vendor.screen.rotation=90
diff --git a/sepolicy/vendor/genfs_contexts b/sepolicy/vendor/genfs_contexts
index 082b838..6e44efd 100644
--- a/sepolicy/vendor/genfs_contexts
+++ b/sepolicy/vendor/genfs_contexts
@@ -11,6 +11,7 @@
 
 # Display
 genfscon proc /driver/swipeup                                                                   u:object_r:vendor_proc_graphics:s0
+genfscon proc /driver/gaming_mode                                                               u:object_r:vendor_proc_graphics:s0
 genfscon proc /driver/gesture_type                                                              u:object_r:vendor_proc_graphics:s0
 genfscon proc /driver/glove                                                                     u:object_r:vendor_proc_graphics:s0
 genfscon proc /driver/dclick                                                                    u:object_r:vendor_proc_graphics:s0