Google Invites Selected Devs to Buy Project Tango Development Kit

Just about a month ago, the curious Project Tango development kit was … more

Micromax Yureka: Indian Handset with CyanogenMod

Thesoap opera involving Cyanogen Inc., OnePlus, and Micromax is one of the most talked about … more

Chainfire Turns Your Bootanimation into a Logging Center

Having a nice boot animation certainly adds a little bit of aesthetic polish to your … more

Android TV Launcher Pushed to Google Play

Over the past decade, the tech universe has seen two drastic and widely contrasting changes with … more

Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

[Q] XSharedPreferences - never loaded

OP TheLexus

18th August 2014, 08:47 AM   |  #1  
OP Senior Member
Thanks Meter: 12
 
139 posts
Join Date:Joined: Jun 2011
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 09:02 AM.
18th August 2014, 11:57 AM   |  #2  
theknut's Avatar
Senior Member
Thanks Meter: 3,505
 
917 posts
Join Date:Joined: May 2009
Donate to Me
More
Have you checked what Const.PACKAGE actually is set to?
18th August 2014, 05:08 PM   |  #3  
OP Senior Member
Thanks Meter: 12
 
139 posts
Join Date:Joined: Jun 2011
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, 05:17 PM   |  #4  
theknut's Avatar
Senior Member
Thanks Meter: 3,505
 
917 posts
Join Date:Joined: May 2009
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, 05:30 PM   |  #5  
Senior Member
Thanks Meter: 30
 
156 posts
Join Date:Joined: Mar 2011
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, 06:22 PM   |  #6  
Maxr1998's Avatar
Senior Member
Thanks Meter: 435
 
686 posts
Join Date:Joined: Apr 2013
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:
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 07:18 PM.
24th October 2014, 11:04 PM   |  #7  
rovo89's Avatar
Senior Recognized Developer
Thanks Meter: 15,317
 
2,386 posts
Join Date:Joined: Jan 2012
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?

Post Reply Subscribe to Thread

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

Advanced Search
Display Modes