Skip to content

Commit

Permalink
Release: works with root
Browse files Browse the repository at this point in the history
  • Loading branch information
amine250 committed Oct 30, 2022
1 parent 1b1b412 commit ef35cc5
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 50 deletions.
42 changes: 13 additions & 29 deletions app/src/main/java/me/zaine/hapticstoggle/HapticsController.kt
Original file line number Diff line number Diff line change
@@ -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
}

}
}
}
11 changes: 2 additions & 9 deletions app/src/main/java/me/zaine/hapticstoggle/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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()
}

}

}
15 changes: 10 additions & 5 deletions app/src/main/java/me/zaine/hapticstoggle/MyQSTileService.kt
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
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()
}
// 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()

}
Expand Down
6 changes: 2 additions & 4 deletions app/src/main/java/me/zaine/hapticstoggle/RootController.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<string name="hello_first_fragment">You can find your new Haptics Toggle tile in the Quick Settings !</string>
<string name="tile_label">Toggle Haptics</string>
<string name="request_perm">Request Permission</string>
<string name="why_need_perm">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.</string>
<string name="why_need_perm">This app needs root access to be able to toggle Haptics settings. Please click the button below and allow it in the next screen.</string>
<string name="toggle_haptics">Toggle Haptics</string>

</resources>
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit ef35cc5

Please sign in to comment.