OnePlus Addresses Reservation List Cheating

Jake Cooper figured out how to bump his place up on OnePlus’ reservation list and … more

OnePlus 2 Benchmark Scores Show Its Muscle

This video by FoneArena contains all the popular benchmarks being tested on the OnePlus 2, … more

Sony’s Emergence in The Middle: Is The Price Right?

Sony’s Electronics Division is not in its best days, and its smartphone … more

HTC Delivering Ads Straight to Sense Home

HTC’s 2015 has been a year marked by a desperate search for revenue.The HTC One M9 … more

[Q] XSharedPreferences - never loaded

141 posts
Thanks Meter: 12
 
By TheLexus, Senior Member on 18th August 2014, 07:47 AM
Post Reply Subscribe to Thread Email Thread
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
Last edited by TheLexus; 18th August 2014 at 08:02 AM.
 
 
18th August 2014, 10:57 AM |#2  
theknut's Avatar
Senior Member
Thanks Meter: 3,834
 
Donate to Me
More
Have you checked what Const.PACKAGE actually is set to?
18th August 2014, 04:08 PM |#3  
OP Senior Member
Thanks Meter: 12
 
More
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.
18th August 2014, 04:17 PM |#4  
theknut's Avatar
Senior Member
Thanks Meter: 3,834
 
Donate to Me
More
Quote:
Originally Posted by TheLexus

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.

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
Quote:

getSharedPreferences(Common.PREFERENCES_NAME, Context.MODE_WORLD_READABLE)

and in the module I open the settings via
Quote:

new XSharedPreferences(Common.PACKAGE_NAME);

Please remove your current preferences file and the MODE_MULTI_PROCESS and try the code from above.
13th October 2014, 04:30 PM |#5  
Senior Member
Thanks Meter: 30
 
More
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(Com mons.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.
The Following User Says Thank You to caioketo For This Useful Post: [ View ]
21st October 2014, 05:22 PM |#6  
Maxr1998's Avatar
Senior Member
Flag Near Munich
Thanks Meter: 916
 
More
Quote:
Originally Posted by caioketo

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(Com mons.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.

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.
Last edited by Maxr1998; 21st October 2014 at 06:18 PM.
24th October 2014, 10:04 PM |#7  
rovo89's Avatar
Senior Recognized Developer
Thanks Meter: 37,479
 
Donate to Me
More
Quote:
Originally Posted by caioketo

So you found any way to make it work??
I ran into the same problem, I set the preferences by the code:

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.

Quote:
Originally Posted by Maxr1998

I have a similar problem, but only for one preference:

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?
The Following User Says Thank You to rovo89 For This Useful Post: [ View ]
19th December 2014, 09:43 PM |#8  
Maxr1998's Avatar
Senior Member
Flag Near Munich
Thanks Meter: 916
 
More
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
Post Reply Subscribe to Thread

Tags
xsharedpreferences xposed
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes