From ef35cc511f632eb9f1a0913790fb4b8a409b7211 Mon Sep 17 00:00:00 2001 From: Amine Zaine Date: Sun, 30 Oct 2022 12:15:17 +0100 Subject: [PATCH] Release: works with root --- .../zaine/hapticstoggle/HapticsController.kt | 42 ++++++------------- .../me/zaine/hapticstoggle/MainActivity.kt | 11 +---- .../me/zaine/hapticstoggle/MyQSTileService.kt | 15 ++++--- .../me/zaine/hapticstoggle/RootController.kt | 6 +-- app/src/main/res/values/strings.xml | 2 +- build.gradle | 4 +- 6 files changed, 30 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/me/zaine/hapticstoggle/HapticsController.kt b/app/src/main/java/me/zaine/hapticstoggle/HapticsController.kt index e459b94..e841dda 100644 --- a/app/src/main/java/me/zaine/hapticstoggle/HapticsController.kt +++ b/app/src/main/java/me/zaine/hapticstoggle/HapticsController.kt @@ -1,45 +1,29 @@ package me.zaine.hapticstoggle -import android.content.Context; -import android.os.VibrationAttributes; -import android.os.Vibrator; -import android.provider.Settings -import android.util.Log +import com.topjohnwu.superuser.Shell -class HapticsController { +class HapticsController { companion object { //Gets current haptics state : Active or Inactive fun getHapticsState(): Boolean{ - var state:Boolean = true + var state = Shell.cmd("cat /sys/class/leds/vibrator/level").exec().out[0].toInt(); + println("Haptics current state is $state"); - return state + return (state != 0) } //Toggles haptics state - fun toggleHapticsState(): Int { - - - val hapticFeedbackEnabled: Int = VibrationAttributes.USAGE_TOUCH - Log.i("Haptics", "are "+ hapticFeedbackEnabled) - - - - //Checking root access - if(RootController.checkRootAccess()){ - Log.i("Haptics","haptics state is "+ hapticFeedbackEnabled) - //Getting current haptics state - if (Settings.System.HAPTIC_FEEDBACK_ENABLED == "18") { - Log.i("Haptics", "toggled OFF!!!") - return 0 - }else{ - Log.i("Haptics", "toggled ON!!!") - return 1 - } + fun toggleHapticsState(): Boolean { + return if (getHapticsState()){ + //disabling + Shell.cmd("echo 0 > /sys/class/leds/vibrator/level").exec(); + false }else{ - return -1 + //enabling + Shell.cmd("echo 3 > /sys/class/leds/vibrator/level").exec(); + true } - } } } \ No newline at end of file diff --git a/app/src/main/java/me/zaine/hapticstoggle/MainActivity.kt b/app/src/main/java/me/zaine/hapticstoggle/MainActivity.kt index 17466a5..8f593a9 100644 --- a/app/src/main/java/me/zaine/hapticstoggle/MainActivity.kt +++ b/app/src/main/java/me/zaine/hapticstoggle/MainActivity.kt @@ -1,8 +1,6 @@ package me.zaine.hapticstoggle import android.os.Bundle -import android.os.VibrationAttributes -import android.provider.Settings import android.util.Log import android.view.View import android.widget.Toast @@ -43,8 +41,6 @@ class MainActivity : AppCompatActivity() { val navController = findNavController(R.id.nav_host_fragment_content_main) appBarConfiguration = AppBarConfiguration(navController.graph) setupActionBarWithNavController(navController, appBarConfiguration) - - } override fun onSupportNavigateUp(): Boolean { @@ -60,11 +56,8 @@ class MainActivity : AppCompatActivity() { fun toggleHaptics(view: View){ when (HapticsController.toggleHapticsState()) { - -1 -> Toast.makeText(applicationContext, "Root access is needed!", Toast.LENGTH_SHORT).show() - 0 -> Toast.makeText(applicationContext, "Haptics are off!", Toast.LENGTH_SHORT).show() - 1 -> Toast.makeText(applicationContext, "Haptics are on!", Toast.LENGTH_SHORT).show() + false -> Toast.makeText(applicationContext, "Haptics are off!", Toast.LENGTH_SHORT).show() + true -> Toast.makeText(applicationContext, "Haptics are on!", Toast.LENGTH_SHORT).show() } - } - } \ No newline at end of file diff --git a/app/src/main/java/me/zaine/hapticstoggle/MyQSTileService.kt b/app/src/main/java/me/zaine/hapticstoggle/MyQSTileService.kt index 357e8f2..4d8258c 100644 --- a/app/src/main/java/me/zaine/hapticstoggle/MyQSTileService.kt +++ b/app/src/main/java/me/zaine/hapticstoggle/MyQSTileService.kt @@ -1,10 +1,13 @@ package me.zaine.hapticstoggle +import android.graphics.drawable.Icon import android.service.quicksettings.Tile import android.service.quicksettings.TileService +import me.zaine.hapticstoggle.HapticsController.Companion.getHapticsState class MyQSTileService : TileService() { + data class StateModel(val enabled: Boolean, val label: String, val icon: Icon) // Called when the user adds your tile. override fun onTileAdded() { super.onTileAdded() @@ -12,22 +15,24 @@ class MyQSTileService : TileService() { // Called when your app can update your tile. override fun onStartListening() { super.onStartListening() - val tile:Tile = getQsTile(); - tile.updateTile() + val state = getHapticsState() + qsTile.state = if (state) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE + qsTile.updateTile() } // Called when your app can no longer update your tile. override fun onStopListening() { super.onStopListening() - val tile:Tile = getQsTile(); + val tile:Tile = qsTile; } // Called when the user taps on your tile in an active or inactive state. override fun onClick() { super.onClick() - val tile:Tile = getQsTile(); - HapticsController.toggleHapticsState() + val tile:Tile = qsTile; + val state = HapticsController.toggleHapticsState() + qsTile.state = if (state) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE tile.updateTile() } diff --git a/app/src/main/java/me/zaine/hapticstoggle/RootController.kt b/app/src/main/java/me/zaine/hapticstoggle/RootController.kt index 9006526..7e03afe 100644 --- a/app/src/main/java/me/zaine/hapticstoggle/RootController.kt +++ b/app/src/main/java/me/zaine/hapticstoggle/RootController.kt @@ -1,13 +1,11 @@ package me.zaine.hapticstoggle -import com.topjohnwu.superuser.Shell; +import com.topjohnwu.superuser.Shell.getShell class RootController { companion object{ - fun checkRootAccess(): Boolean { - var result: Shell.Result = Shell.cmd("find /dev/block -iname boot").exec(); - return result.isSuccess + return getShell().isRoot } } } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 39ba231..0d950da 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -6,7 +6,7 @@ You can find your new Haptics Toggle tile in the Quick Settings ! Toggle Haptics Request Permission - This app needs the permission \'android.permission.WRITE_SETTINGS\' to be able to toggle Haptics settings. Please click the button below and allow it in the next screen. + This app needs root access to be able to toggle Haptics settings. Please click the button below and allow it in the next screen. Toggle Haptics \ No newline at end of file diff --git a/build.gradle b/build.gradle index c518bc1..498eb4d 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { - id 'com.android.application' version '7.3.0' apply false - id 'com.android.library' version '7.3.0' apply false + id 'com.android.application' version '7.3.1' apply false + id 'com.android.library' version '7.3.1' apply false id 'org.jetbrains.kotlin.android' version '1.7.10' apply false } \ No newline at end of file