[Galaxy Y GT-S5360 General] halnovemila:
One touch Enable/Disable (sort of) widget

Well, time flies guys :silly:

It's since I've done this mod to my SGY that I was missing to have a "one touch" way to enable/disable the Proximity Lock feature as sometimes, especially when having to show some pics to someone, or to let someone else to type something on the phone, the screen lock is unintentionally triggered causing a lot of annoying interruptions.

Since I wasn't interested to all the other features, but just only the proximity lock, I immediately thought I could have decompiled and modified the code of CustomizableParts in a way to have it automatically switch the setting at each time the app is launched

Well... I tried (a lot), I failed, I gave up... temporarily ;)

So far (and so quickly) four months are past... in the meanwhile I've enhance my understanding of the smali code and my reverse engineering ability to the point that I did this:

[GUIDE][MOD][APP] Modify chomp SMS to make it look exactly as SGY SMS app

So, a couple of days ago, I decided that was time to try again to achieve the result that I previously missed.

I already have been able to "clean" the app from all the settings that I didn't care of and to make it display only the "Disable Proximity Lock" setting checkbox, but what I have't been able to do was to automate the inversion of the last value.

In smali\com\tenten\tenten.smali I already found a piece of code that could have been useful to do the job, with some slight modifications.

In .method public onPreferenceTreeClick, that contains the instructions that are executed when the user touch any preference, there are few lines of code that are dedicated for reading the current value of the checkbox and to accordingly set the related "disablelock" value in System Settings.

.method public onPreferenceTreeClick(Landroid/preference/PreferenceScreen;Landroid/preference/Preference;)Z

#get the object related to the Disable Proximity Lock checkbox preference (the object has been previously saved in mDisableLock within the onCreate method)
iget-object v1, p0, Lcom/tenten/tenten;->mDisableLock:Landroid/preference/CheckBoxPreference;

# get the current value of the CheckBox (after it's been touched/clicked by the user)
invoke-virtual {v1}, Landroid/preference/CheckBoxPreference;->isChecked()Z
...
# get the ContentResolver object that is needed to access and change the System Settings
invoke-virtual {p0}, Lcom/tenten/tenten;->getContentResolver()Landroid/content/ContentResolver;
...
# define the name of the setting in System Settings that will be set
const-string v4, "disablelock"

# the following 3 lines are dedicated to invert the current checkbox value as if "Disable Proximity Lock" is checked, so if its value is "true" (same as 1) then a "0" must be set on disablelock System Setting
if-eqz v0, :cond_0
move v3, v2
:cond_0

# here the "disablelock" System Setting is set either to 0 or 1 according to the inverse of the Checkbox value
invoke-static {v1, v4, v3}, Landroid/provider/Settings$System;->putInt(Landroid/content/ContentResolver;Ljava/lang/String;I)Z

What I needed to do is to copy those few lines inside the onCreate method that is executed when the app is launched, and to add the instruction needed to change the status of the CheckBox preference; actually I needed to add this

invoke-virtual {v0, v2}, Landroid/preference/CheckBoxPreference;->setChecked(Z)V

v0 is same as mDisableLock which is actually set inside onCreate method, so it wasn't a problem at all to have it; while v2 is the value that has to be set, either 0 or 1 (unchecked, checked).

The modification seemed quite simple and straightforward but, for the life of me, I wasn't able to get one of the many dozens of versions I built (by changing one small detail at time) that didn't force close once installed and launched on my phone

:eek:

I kept thinking that the problem was the call to get the ContentResolver object within the onCreate method... I was wrong...

After many hours of attempts and failure I then realized the truth...

it was a wrong .locals directive!

Dang! I didn't even know that .locals was a directive that *MUST* match the number of v(0,1,2...) registers used within the method!

Well... now I know... I learnt it the hard way... so I won't forget never ever :p

The fact is that the original value of .locals inside onCreate method was only 2, because in the whole method only registers v0 and v1 were used; but I added few instructions that made use of v2 and v3.

So, well... now it works! :cool:

These are the instructions I've added on the onCreate method soon after the iput-object instruction that saves the "disablelock" CheckBoxPreference object in mDisableLock

iput-object v0, p0, Lcom/tenten/tenten;->mDisableLock:Landroid/preference/CheckBoxPreference;
invoke-virtual {v0}, Landroid/preference/CheckBoxPreference;->isChecked()Z
move-result v3
const/4 v2, 0x1 # v2 is set to 1
if-eqz v3, :cond_0
const/4 v2, 0x0 # if v3 is equal to 1, v2 is set to 0 (otherwise if v3 is 0, v2 will remain 1)
:cond_0
invoke-virtual {v0, v2}, Landroid/preference/CheckBoxPreference;->setChecked(Z)V
invoke-virtual {p0}, Lcom/tenten/tenten;->getContentResolver()Landroid/content/ContentResolver;
move-result-object v1
const-string v2, "disablelock"
invoke-static {v1, v2, v3}, Landroid/provider/Settings$System;->putInt(Landroid/content/ContentResolver;Ljava/lang/String;I)Z
.line 61
return-void
.end method

Now to Enable/Disable the Proximity Lock feature there's no need to change any setting, but just launch the CustomizableParts app

:highfive: :D

Intended to be used as a widget, the CustomizableParts original icon, a black Android bot, didn't look very well if sitting somewhere on the home screen, so I made a new icon for it, that I share here as attachment to this post (as well as the modified CustomizableParts app).

I also tried to automate the closing of the app... so that it is really like having a "on/off" button widget.

I made a shortcut to Terminal Emulator app (already set to automatically execute "su" at the beginning of each terminal session) that execute the following command line

am start -W -n com.tenten/.tenten > /dev/null 2>&1 ; pkill -9 com.tenten

It works, but the execution is quite slow (as it has to load the Terminal Emulator app first, that will then launch the CustomizableParts app) and moreover there's a glitch on the "am start -W" command that makes it "hangs" sometime, in a unpredictable way, so that the Terminal Emulator session "hangs" as well and doesn't close.

Have a look at the video I made