Hi guys,
I'm currently a little bit confused because i try to create my first XPosed module for Xposed 2.6.1 on a Android 4.0.3 unit but i have trouble with my preferences. I tried multiple things but it was never working.
Here is the preferences code of my Settings activity. That one works and it creates a the preference file with rw-rw-r permissions and the preferences are correctly saved.
Code:
getPreferenceManager().setSharedPreferencesMode(MODE_WORLD_READABLE | MODE_MULTI_PROCESS);
getPreferenceManager().setSharedPreferencesName(Const.PACKAGE + "_preferences");
addPreferencesFromResource(R.xml.preferences);
That one is the xposed side snippet:
Code:
preferences = new XSharedPreferences(Const.PACKAGE, Const.PACKAGE+"_preferences");
if (preferences.getAll().size()==0)
{
debug("Preferences seems not to be initialized!");
}
It never loads any of my preferences. I tried the constructor XSharedPreferences(Const.PACKAGE) instead, but without any luck...
I checked the directory permissions of the settings directory, and that one seems to be correct (rwxrwxr-x).
Thanks for every help!
Bye
Have you checked what Const.PACKAGE actually is set to?
It is set to the hardcoded package Name. No class.getPackage... I added a file.isreadable and it returns false. I dont know, im just out oft any ideas.
TheLexus said:
It is set to the hardcoded package Name. No class.getPackage... I added a file.isreadable and it returns false. I dont know, im just out oft any ideas.
Click to expand...
Click to collapse
Is there any reason why you set MODE_MULTI_PROCESS? Can you please try removing it, delete your preference file and try again?
In my UI I open SharedPreferences from <packagename>_preferences.xml
getSharedPreferences(Common.PREFERENCES_NAME, Context.MODE_WORLD_READABLE)
Click to expand...
Click to collapse
and in the module I open the settings via
new XSharedPreferences(Common.PACKAGE_NAME);
Click to expand...
Click to collapse
Please remove your current preferences file and the MODE_MULTI_PROCESS and try the code from above.
So you found any way to make it work??
I ran into the same problem, I set the preferences by the code:
SharedPreferences.Editor editor = LogExtras.getAppContext().getSharedPreferences(Commons.PACKAGE_NAME, Context.MODE_WORLD_READABLE).edit();
editor.putString("package_to_look", _packageToLook);
And when I try to get it from the module it always returns size 0
private static final XSharedPreferences preferences = new XSharedPreferences(Commons.PACKAGE_NAME, Commons.PACKAGE_NAME+"_preferences");
public static void refreshPreferences() {
Log.d("Utils", "reading prefs");
preferences.reload();
Log.d("Utils", Integer.toString(preferences.getAll().size()));
Commons.PACKAGE_TO_LOOK = preferences.getString("package_to_look", Commons.PACKAGE_TO_LOOK);
log("Package to Look: " + Commons.PACKAGE_TO_LOOK);
}
---------- Post added at 04:30 PM ---------- Previous post was at 03:34 PM ----------
I tested the prefs in the context, and it works, only in xposed it dont load.
So I got the app saving the pref and reading the pref working, but when I try to read the pref with XSharedPreference, the size is always 0.
Already tried everything, but nothing works.
caioketo said:
So you found any way to make it work??
I ran into the same problem, I set the preferences by the code:
SharedPreferences.Editor editor = LogExtras.getAppContext().getSharedPreferences(Commons.PACKAGE_NAME, Context.MODE_WORLD_READABLE).edit();
editor.putString("package_to_look", _packageToLook);
And when I try to get it from the module it always returns size 0
private static final XSharedPreferences preferences = new XSharedPreferences(Commons.PACKAGE_NAME, Commons.PACKAGE_NAME+"_preferences");
public static void refreshPreferences() {
Log.d("Utils", "reading prefs");
preferences.reload();
Log.d("Utils", Integer.toString(preferences.getAll().size()));
Commons.PACKAGE_TO_LOOK = preferences.getString("package_to_look", Commons.PACKAGE_TO_LOOK);
log("Package to Look: " + Commons.PACKAGE_TO_LOOK);
}
---------- Post added at 04:30 PM ---------- Previous post was at 03:34 PM ----------
I tested the prefs in the context, and it works, only in xposed it dont load.
So I got the app saving the pref and reading the pref working, but when I try to read the pref with XSharedPreference, the size is always 0.
Already tried everything, but nothing works.
Click to expand...
Click to collapse
I have a similar problem, but only for one preference:
Code:
public class Main implements IXposedHookZygoteInit, IXposedHookLoadPackage {
public static final String MY_PACKAGE_NAME = Main.class.getPackage().getName();
private static XSharedPreferences pref, packagePref;
@Override
public void initZygote(StartupParam startupParam) throws Throwable {
pref = new XSharedPreferences(MY_PACKAGE_NAME, Common.PREF);
packagePref = new XSharedPreferences(MY_PACKAGE_NAME, Common.PREF_PACKAGE);
}
@Override
public void handleLoadPackage(LoadPackageParam lpparam) throws Throwable {
pref.reload();
packagePref.reload();
final String packageName = lpparam.packageName;
boolean masterSwitchOn = pref.getBoolean(Common.MASTER_SWITCH, true);
XposedBridge.log("Master Switch " + (masterSwitchOn ? "on" : "off"));
if (!masterSwitchOn) {
return;
}
if (!packagePref.getBoolean(packageName, false)) {
return;
}
Long timestamp = System.currentTimeMillis();
Long permitTimestamp = packagePref.getLong(packageName + "_tmp", 0);
if (permitTimestamp != 0 && timestamp - permitTimestamp <= 4000) {
return;
}
.
.
.
}
So, the package boolean works, the master switch doesn't…
It only seems to work after opening and closing the app multiple times.
caioketo said:
So you found any way to make it work??
I ran into the same problem, I set the preferences by the code:
Click to expand...
Click to collapse
Are you sure that your app saves the file to shared_prefs/<packagename>_preferences.xml? Better verify it using a file explorer. It's the default for preference activities, but it might not be for the methods you use.
Maxr1998 said:
I have a similar problem, but only for one preference:
Click to expand...
Click to collapse
Similar stuff here. Does your application really save the preferences in two different files, named shared_prefs/<Value of Common.PREF>.xml and shared_prefs/<Value of Common.PREF_PACKAGE>.xml? If yes, are both of these files world-readable?
You just saved my day. Making the preference files world readable solves it , preferences.makeWorldReadable() is the way to go. And yes, I'm using two preference files.
Thank you thank you thank you
Related
I am trying to hook this method, I want to filter out the "Google Play services" from there...(is under settings app)
Code:
/**
* Fills a list of applications which queried location recently within
* specified time.
*/
public List<Preference> getAppList() {
// Retrieve a location usage list from AppOps
AppOpsManager aoManager =
(AppOpsManager) mActivity.getSystemService(Context.APP_OPS_SERVICE);
List<AppOpsManager.PackageOps> appOps = aoManager.getPackagesForOps(
new int[] {
AppOpsManager.OP_MONITOR_LOCATION,
AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION,
});
// Process the AppOps list and generate a preference list.
ArrayList<Preference> prefs = new ArrayList<Preference>();
long now = System.currentTimeMillis();
for (AppOpsManager.PackageOps ops : appOps) {
// Don't show the Android System in the list - it's not actionable for the user.
// Also don't show apps belonging to background users.
int uid = ops.getUid();
boolean isAndroidOs = (uid == Process.SYSTEM_UID)
&& ANDROID_SYSTEM_PACKAGE_NAME.equals(ops.getPackageName());
if (!isAndroidOs && ActivityManager.getCurrentUser() == UserHandle.getUserId(uid)) {
Preference pref = getPreferenceFromOps(now, ops);
if (pref != null) {
prefs.add(pref);
}
}
}
return prefs;
}
I can't hook this method, I'v tried something like :
Code:
if (lpparam.packageName.equals("com.android.settings")) {
findAndHookMethod("com.android.settings.location.LocationSettings", lpparam.classLoader, "getAppList", new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
XposedBridge.log("Called");
}
what is the best approach to achieve this?
devtrop said:
I can't hook this method
Click to expand...
Click to collapse
Why not? Are there any errors in the Xposed log?
If not, I'd make sure the package name is correct and that the method is actually called when you expect it to be.
Code:
09-29 11:06:53.600 5277 5277 I Xposed : java.lang.NoSuchMethodError: com.android.settings.location.RecentLocationApps#getAppList(java.util.List)#exact
running on Samsumg Note 3.
devtrop said:
Code:
09-29 11:06:53.600 5277 5277 I Xposed : java.lang.NoSuchMethodError: com.android.settings.location.RecentLocationApps#getAppList(java.util.List)#exact
running on Samsumg Note 3.
Click to expand...
Click to collapse
That error doesn't correspond to the code you've posted in the first post. Post the modified code and the error.
Purely guessing, though: Samsung modified the code you're trying to hook. You're hooking the methods you're seeing in the AOSP code, but that's not the case on your device.
You are correct, sorry. I'm trying a lot of different stuff.
this is the code:
Code:
try
{
findAndHookMethod("com.android.settings.location.RecentLocationApps", lpparam.classLoader,
"getAppList", List.class, new XC_MethodHook() {
@SuppressWarnings("unchecked")
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable
{
XposedBridge.log("RecentLocationApps.getAppList()");
}
});
}
catch(Throwable t)
{
XposedBridge.log(t);
}
the throwable is :
Code:
09-29 11:56:08.559 5282 5282 I Xposed : java.lang.NoSuchMethodError: com.android.settings.location.RecentLocationApps#getAppList(java.util.List)#exact
if tried to do that for every package, not just com.android.settings or com.android.settings.location and it doesn't seem to work.
do you know what's the correct package? do I have to start dedexing Samsung's packages to get the answer?
devtrop said:
do you know what's the correct package?
Click to expand...
Click to collapse
com.android.settings should be the correct package, if I'm not mistaken.
devtrop said:
do I have to start dedexing Samsung's packages to get the answer?
Click to expand...
Click to collapse
That'd be the best thing to do as it looks like Samsung has changed some things.
Hey there,
at the moment I'm trying to create my first Xposed Module. My wish to give the VolumePanel a new look. I found the layout and the code for the VolumePanel in AOSP here:
Code
Layout
Now I want to modify this layout but I can't get it work. I'm not sure in which package the VolumePanel is after the compilation of Android. I tried wit this code here:
Code:
resparam.res.hookLayout("android", "layout", "volume_adjust", new XC_LayoutInflated()
{
@Override
public void handleLayoutInflated(LayoutInflatedParam liparam) throws Throwable
{
Log.d("ME", "Layout infaleted");
LinearLayout ll = new LinearLayout(liparam.view.getContext());
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(liparam.view);
liparam.view = ll;
}
});
If I try it with this code the function is never called. As you can see here I'm trying to wrap the whole layout with a LinearLayout (for the beginning).
I'm also not realy sure if I can call the last line of code and overwrite the variable view. Is that okay or do I have to do it in another way? (but I know that the xml is in the framework-res.apk)
Would be great if you can help me with this: Can I overwrite the view in the way I do and where can I get the packageName of the volumeView?
Kind regards
Cilenco
Volley is an HTTP library that makes networking for Android apps easier and faster.
We need to set the context before sending requests, according to nameless-technology.blogspot.com /2013/11/custom-system-service-using-xposedbridge.html, my testing code was:
Code:
public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
...
protected void beforeHookedMethod(MethodHookParam param) throws
Throwable {
...
Context context = (Context) param.getResult();
RequestQueue queue = Volley.newRequestQueue(context);
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
msgs_g = response;
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
msgs_g = "That didn't work!";
}
});
queue.add(stringRequest);
However, when the module was loading, there's an exception:
Code:
java.lang.NullPointerException
at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:45)
at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:105)
at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:115)
at xptest.tk.xpvolley.Sendoh$1.beforeHookedMethod(Sendoh.java:61)
at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:611)
From code you provided it's not clear what method exactly you are hooking. But some remarks, anyway:
- are you sure original method returns context?
- I'm not sure whether using getResult() in before hook is a good practice, as you are basically asking for a return value when method was not executed, yet.
You should try after hook instead
C3C076 said:
From code you provided it's not clear what method exactly you are hooking. But some remarks, anyway:
- are you sure original method returns context?
- I'm not sure whether using getResult() in before hook is a good practice, as you are basically asking for a return value when method was not executed, yet.
You should try after hook instead
Click to expand...
Click to collapse
@C3C076 Thanks for your reply.
I just modified the code from `beforeHookedMethod` to `afterHookedMethod`, at this time, I got nothing in my log.
What I want to hook is the function `dispatchSensorEvent` in the `android.hardware.SystemSensorManager$SensorEventQueue` class. Actually I don't whether this method returns context, thanks for reminding me.
Finally I used `AsyncTask` with `DefaultHttpClient` to get the data from the web site, I still use `beforeHookedMethod`.
Hey.
I have a little problem with my module. I can't dynamicly change settings, changes are applied only after reboot
Here code (in this case is code responding to cahnge color )
ColorPickerSample.java
Code:
String mColor = ([COLOR=#008000][B]"#" [/B][/COLOR]+ [COLOR=#660e7a][B]editText[/B][/COLOR].getText().toString());
[COLOR=#808000]@SuppressWarnings[/COLOR]([COLOR=#008000][B]"deprecation"[/B][/COLOR])
SharedPreferences pref = getSharedPreferences([COLOR=#008000][B]"pref"[/B][/COLOR], Context.[COLOR=#660e7a][B][I]MODE_WORLD_READABLE[/I][/B][/COLOR]);
Editor editor = pref.edit();
[COLOR=#808080][I]//write prefs
[/I][/COLOR]editor.putString(PrefKeys.UI_COLOR, mColor);
[COLOR=#808080][I]//apply
[/I][/COLOR]editor.commit();
Module.java
Code:
XSharedPreferences [COLOR=#660e7a][B]pref [/B][/COLOR]= [COLOR=#000080][B]new [/B][/COLOR]XSharedPreferences(ModuleTest.[COLOR=#000080][B]class[/B][/COLOR].getPackage().getName(), [COLOR=#008000][B]"pref"[/B][/COLOR]);
[COLOR=#000080][B]final [/B][/COLOR]String [COLOR=#660e7a][B]uicolor [/B][/COLOR]= [COLOR=#660e7a][B]pref[/B][/COLOR].getString(PrefKeys.[COLOR=#660e7a][I]UI_COLOR[/I][/COLOR], [COLOR=#008000][B]"#ff008080"[/B][/COLOR]);
[COLOR=#000080][B]final int [/B][/COLOR][COLOR=#660e7a][B]colorint [/B][/COLOR]= Color.[I]parseColor[/I]([COLOR=#660e7a][B]uicolor[/B][/COLOR]);
[COLOR=#808000]@Override
[/COLOR][COLOR=#000080][B]public void [/B][/COLOR]initZygote(StartupParam startupParam) [COLOR=#000080][B]throws [/B][/COLOR]Throwable {
XResources.[I]setSystemWideReplacement[/I]([COLOR=#008000][B]"android"[/B][/COLOR], [COLOR=#008000][B]"color"[/B][/COLOR], [COLOR=#008000][B]"holo_blue_light"[/B][/COLOR], Color.[I]parseColor[/I](uicolor));
}
Thank you so much for any help!
KuaQ said:
Hey.
I have a little problem with my module. I can't dynamicly change settings, changes are applied only after reboot
Here code (in this case is code responding to cahnge color )
ColorPickerSample.java
Code:
String mColor = ([COLOR=#008000][B]"#" [/B][/COLOR]+ [COLOR=#660e7a][B]editText[/B][/COLOR].getText().toString());
[COLOR=#808000]@SuppressWarnings[/COLOR]([COLOR=#008000][B]"deprecation"[/B][/COLOR])
SharedPreferences pref = getSharedPreferences([COLOR=#008000][B]"pref"[/B][/COLOR], Context.[COLOR=#660e7a][B][I]MODE_WORLD_READABLE[/I][/B][/COLOR]);
Editor editor = pref.edit();
[COLOR=#808080][I]//write prefs
[/I][/COLOR]editor.putString(PrefKeys.UI_COLOR, mColor);
[COLOR=#808080][I]//apply
[/I][/COLOR]editor.commit();
Module.java
Code:
XSharedPreferences [COLOR=#660e7a][B]pref [/B][/COLOR]= [COLOR=#000080][B]new [/B][/COLOR]XSharedPreferences(ModuleTest.[COLOR=#000080][B]class[/B][/COLOR].getPackage().getName(), [COLOR=#008000][B]"pref"[/B][/COLOR]);
[COLOR=#000080][B]final [/B][/COLOR]String [COLOR=#660e7a][B]uicolor [/B][/COLOR]= [COLOR=#660e7a][B]pref[/B][/COLOR].getString(PrefKeys.[COLOR=#660e7a][I]UI_COLOR[/I][/COLOR], [COLOR=#008000][B]"#ff008080"[/B][/COLOR]);
[COLOR=#000080][B]final int [/B][/COLOR][COLOR=#660e7a][B]colorint [/B][/COLOR]= Color.[I]parseColor[/I]([COLOR=#660e7a][B]uicolor[/B][/COLOR]);
[COLOR=#808000]@Override
[/COLOR][COLOR=#000080][B]public void [/B][/COLOR]initZygote(StartupParam startupParam) [COLOR=#000080][B]throws [/B][/COLOR]Throwable {
XResources.[I]setSystemWideReplacement[/I]([COLOR=#008000][B]"android"[/B][/COLOR], [COLOR=#008000][B]"color"[/B][/COLOR], [COLOR=#008000][B]"holo_blue_light"[/B][/COLOR], Color.[I]parseColor[/I](uicolor));
}
Thank you so much for any help!
Click to expand...
Click to collapse
Could you make broadcast receiver to get the color and set it
Sent from my SM-G530H using XDA Free mobile app
Problem solved. Thank you
initZygote's code runs only once, when system boots. So even if you change pref, nothing will happen.
According to document, trying to use MODE_WORLD_READABLE will crash the module UI with a SecurityException.
I've tried to use a ContentProvider, but unable to get the Context object in handleLoadPackage (Illegal state).
Is there any better way?
Tried to downgrade targetSdkVersion, the app no longer crashes but XSharedPreferences still can't read the file (the preferences file is already world readable)
You can use Remotepreferences (awesome library!). You will need a context however, so it is not suitable for methods in initzigote or where you can't access the context. I think is the only way as of now.
Massi-X said:
You can use Remotepreferences (awesome library!). You will need a context however, so it is not suitable for methods in initzigote or where you can't access the context. I think is the only way as of now.
Click to expand...
Click to collapse
Remotepreferences will complicate things.
The easiest way to do is to remove the below line
Code:
getPreferenceManager().setSharedPreferencesMode(MODE_WORLD_READABLE);
And add the following on class level.
Code:
@Override
public void onPause() {
super.onPause();
// Set preferences permissions to be world readable
// Workaround for Android N and above since MODE_WORLD_READABLE will cause security exception and FC.
final File dataDir = new File(getActivity().getApplicationInfo().dataDir);
final File prefsDir = new File(dataDir, "shared_prefs");
final File prefsFile = new File(prefsDir, getPreferenceManager().getSharedPreferencesName() + ".xml");
if (prefsFile.exists()) {
dataDir.setReadable(true, false);
dataDir.setExecutable(true, false);
prefsDir.setReadable(true, false);
prefsDir.setExecutable(true, false);
prefsFile.setReadable(true, false);
prefsFile.setExecutable(true, false);
}
}
This method will work on any sdk :good:
I tested it myself and it is working fine.
Thanks to @PunchUp @pyler
laura almeida said:
Remotepreferences will complicate things.
The easiest way to do is to remove the below line
Code:
getPreferenceManager().setSharedPreferencesMode(MODE_WORLD_READABLE);
And add the following on class level.
Code:
@Override
public void onPause() {
super.onPause();
// Set preferences permissions to be world readable
// Workaround for Android N and above since MODE_WORLD_READABLE will cause security exception and FC.
final File dataDir = new File(getActivity().getApplicationInfo().dataDir);
final File prefsDir = new File(dataDir, "shared_prefs");
final File prefsFile = new File(prefsDir, getPreferenceManager().getSharedPreferencesName() + ".xml");
if (prefsFile.exists()) {
dataDir.setReadable(true, false);
dataDir.setExecutable(true, false);
prefsDir.setReadable(true, false);
prefsDir.setExecutable(true, false);
prefsFile.setReadable(true, false);
prefsFile.setExecutable(true, false);
}
}
This method will work on any sdk :good:
I tested it myself and it is working fine.
Thanks to @PunchUp @pyler
Click to expand...
Click to collapse
Yeah but it seems an hacky thing. It will work with any Android? With selinux enabled? On custom ROMs? Surely Remotepreferences complicate things but is the best way (also for previous Android versions) of reading the preferences file. Anyway this can be another solution :good:
EDIT: I also found in marshmallow my apps was randomly failing reading the preferences (no real reason) and Remotepreferences solved the problem.
Massi-X said:
Remotepreferences will complicate things.
The easiest way to do is to remove the below line
And add the following on class level.
Yeah but it seems an hacky thing. It will work with any Android? With selinux enabled? On custom ROMs? Surely Remotepreferences complicate things but is the best way (also for previous Android versions) of reading the preferences file. Anyway this can be another solution :highfive:
Click to expand...
Click to collapse
With any android version - Yes
Will it work with previous android version - Yes
On custom ROMs - Yes
With selinux enabled ? - not tested yet. But I think it works.
laura almeida said:
With any android version - Yes
Will it work with previous android version - Yes
On custom ROMs - Yes
With selinux enabled ? - not tested yet. But I think it works.
Click to expand...
Click to collapse
Previous versions: I was speaking about another thing. See edit. Anyway it's another way, 2 is always better than 1.