So, I am working on a "custom settings" app for my android phone, and I am in need of a little help here....
Here's what I'd like to do...
I'd like to be able to set a Preference in the /res/xml/file.xml that on tap fires off a shell script based on some value in the xml
So, say my Preference is:
<ListPreference android:key="dp_cleaner" android:title="My Cleaner" android:entries="@array/dp_cleaner_entries" android:entryValues="@array/dp_cleaner_entries_values" />
And the .smali method to fire off the shell scripts is
.method public static execRootCmdSilent(Ljava/lang/String;)I
# Contains all my code to fire it off...
.end method
Usage
:try_start_0
const-string v2, "THE_COMMAND"
invoke-static {v2}, Lcom/android/settings/ExecSH;->execRootCmdSilent(Ljava/lang/String;)I
:try_end_0
.catch Ljava/lang/Exception; {:try_start_0 .. :try_end_0} :catch_0
How could I populate my const-string from the tapped value in the list? I'm assuming that I would need to findPreference somehow via the key, but I don't know how I can pass the tapped value to the method....
Could it be something as simple as:
.class public Lcom/android/settings/ExecSH;
.super Lcom/android/settings/SettingsPreferenceFragment;
.source "ExecSH.java"
.implements Landroid/preference/Preference$OnPreferenceChangeListener;
.implements Landroid/preference/Preference$OnPreferenceClickListener;
# direct methods
.method public onPreferenceTreeClick(Landroid/preference/PreferenceScreen;Landroid/preference/Preference;)Z
:try_start_0
const-string v2, Landroid/preference/Preference;->findPreference(Ljava/lang/CharSequence;)Landroid/preference/Preference;
invoke-static {v2}, Lcom/android/settings/ExecSH;->execRootCmdSilent(Ljava/lang/String;)I
:try_end_0
.catch Ljava/lang/Exception; {:try_start_0 .. :try_end_0} :catch_0
.end method
.method public static execRootCmdSilent(Ljava/lang/String;)I
#MY CODE
.end method
Your class needs to implement the Preference.OnPreferenceChangedListener interface and be set as your ListPreference's change listener. Your listener method will be passed the new value as a parameter, at which point you can pass that in to your execRootCmdSilent method.
The critical smali section is toward the bottom of the below files:
.method public onPreferenceChange(Landroid/preference/Preference;Ljava/lang/Object;)Z
move-object v2, p2
// there are some more moves here and a test, which is why we're shuffling registers
move-object v3, v2
invoke-virtual {v3}, Ljava/lang/Object;->toString()Ljava/lang/String;
move-result-object v3
invoke-static {v3}, Lsarbs/com/tinker/SettingsActivity;->execRootCmdSilent(Ljava/lang/String;)I
Basically, the p2 value is the second parameter being passed to the onPreferenceChange method (newValue). We move that into a local register, and then invoke-virtual the toString method on it. Move the result of that call into the v3 register, and then you can invoke-static your execRootCmdSilent method using that v3 register as the parameter.
The Java code I used to implement this:
package sarbs.com.tinker;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
public class SettingsActivity extends PreferenceActivity implements Preference.OnPreferenceChangeListener {
ListPreference mListPreference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
mListPreference = (ListPreference) findPreference("dp_cleaner");
mListPreference.setOnPreferenceChangeListener(this);
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference == mListPreference) {
execRootCmdSilent(newValue.toString());
}
return true;
}
public static int execRootCmdSilent(String str) {
return 0;
}
}
And the smali code it produced:
.class public Lsarbs/com/tinker/SettingsActivity;
.super Landroid/preference/PreferenceActivity;
.source "SettingsActivity.java"
# interfaces
.implements Landroid/preference/Preference$OnPreferenceChangeListener;
# instance fields
.field mListPreference:Landroid/preference/ListPreference;
# direct methods
.method public constructor <init>()V
.registers 3
.prologue
.line 8
move-object v0, p0
.local v0, "this":Lsarbs/com/tinker/SettingsActivity;
move-object v1, v0
invoke-direct {v1}, Landroid/preference/PreferenceActivity;-><init>()V
return-void
.end method
.method public static execRootCmdSilent(Ljava/lang/String;)I
.registers 3
.prologue
.line 30
move-object v0, p0
.local v0, "str":Ljava/lang/String;
const/4 v1, 0x0
move v0, v1
.end local v0 # "str":Ljava/lang/String;
return v0
.end method
# virtual methods
.method protected onCreate(Landroid/os/Bundle;)V
.registers 7
.prologue
.line 13
move-object v0, p0
.local v0, "this":Lsarbs/com/tinker/SettingsActivity;
move-object v1, p1
.local v1, "savedInstanceState":Landroid/os/Bundle;
move-object v2, v0
move-object v3, v1
invoke-super {v2, v3}, Landroid/preference/PreferenceActivity;->onCreate(Landroid/os/Bundle;)V
.line 15
move-object v2, v0
const v3, 0x7f030001
invoke-virtual {v2, v3}, Lsarbs/com/tinker/SettingsActivity;->addPreferencesFromResource(I)V
.line 17
move-object v2, v0
move-object v3, v0
const-string v4, "dp_cleaner"
invoke-virtual {v3, v4}, Lsarbs/com/tinker/SettingsActivity;->findPreference(Ljava/lang/CharSequence;)Landroid/preference/Preference;
move-result-object v3
check-cast v3, Landroid/preference/ListPreference;
iput-object v3, v2, Lsarbs/com/tinker/SettingsActivity;->mListPreference:Landroid/preference/ListPreference;
.line 18
move-object v2, v0
iget-object v2, v2, Lsarbs/com/tinker/SettingsActivity;->mListPreference:Landroid/preference/ListPreference;
move-object v3, v0
invoke-virtual {v2, v3}, Landroid/preference/ListPreference;->setOnPreferenceChangeListener(Landroid/preference/Preference$OnPreferenceChangeListener;)V
.line 19
return-void
.end method
.method public onPreferenceChange(Landroid/preference/Preference;Ljava/lang/Object;)Z
.registers 8
.prologue
.line 23
move-object v0, p0
.local v0, "this":Lsarbs/com/tinker/SettingsActivity;
move-object v1, p1
.local v1, "preference":Landroid/preference/Preference;
move-object v2, p2
.local v2, "newValue":Ljava/lang/Object;
move-object v3, v1
move-object v4, v0
iget-object v4, v4, Lsarbs/com/tinker/SettingsActivity;->mListPreference:Landroid/preference/ListPreference;
if-ne v3, v4, :cond_12
.line 24
move-object v3, v2
invoke-virtual {v3}, Ljava/lang/Object;->toString()Ljava/lang/String;
move-result-object v3
invoke-static {v3}, Lsarbs/com/tinker/SettingsActivity;->execRootCmdSilent(Ljava/lang/String;)I
move-result v3
.line 26
:cond_12
const/4 v3, 0x1
move v0, v3
.end local v0 # "this":Lsarbs/com/tinker/SettingsActivity;
return v0
.end method
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With