How you deal with Xsharepreference?? #178

Closed
opened 3 years ago by AndroidX · 10 comments

Actually i am developing a module but Xsharedpreference can't take data from sharepreferences "/data/user/0/packageName/shared_prefs/user_prefs.xml" coz as we knw its coz protection of SDK 24 or Higer i tried this way too...

 if (Build.VERSION.SDK_INT > 23) {
                File Prefile1;
                File Prefile2;
                Prefile1 = new File("/data/user/0/packageName/shared_prefs/user_prefs.xml");
                Prefile2 = new File("/data/user_de/0/packageName/shared_prefs/user_prefs.xml");
                if (Prefile1.exists() && Prefile1.canRead()) {

                    pref = new XSharedPreferences(Prefile1); // For Android 7.0 or Higer

                } else if (!Prefile2.exists() || !Prefile2.canRead()){

                    pref = new XSharedPreferences("packageName", "user_prefs");
                    pref.makeWorldReadable();
                    pref.reload();

                }else {
                    pref = new XSharedPreferences(Prefile2);
                    pref.makeWorldReadable();
                    pref.reload();
                    
                     }

            }else {
                pref = new XSharedPreferences("com.yuvi.mask", "user_prefs");
                pref.makeWorldReadable();
                pref.reload();
            }

            return pref;
        }
        return pref; }

but still can't read user_prefs.xml by Xsharedpreference please help bro

Actually i am developing a module but Xsharedpreference can't take data from sharepreferences "/data/user/0/packageName/shared_prefs/user_prefs.xml" coz as we knw its coz protection of SDK 24 or Higer i tried this way too... ``` if (Build.VERSION.SDK_INT > 23) { File Prefile1; File Prefile2; Prefile1 = new File("/data/user/0/packageName/shared_prefs/user_prefs.xml"); Prefile2 = new File("/data/user_de/0/packageName/shared_prefs/user_prefs.xml"); if (Prefile1.exists() && Prefile1.canRead()) { pref = new XSharedPreferences(Prefile1); // For Android 7.0 or Higer } else if (!Prefile2.exists() || !Prefile2.canRead()){ pref = new XSharedPreferences("packageName", "user_prefs"); pref.makeWorldReadable(); pref.reload(); }else { pref = new XSharedPreferences(Prefile2); pref.makeWorldReadable(); pref.reload(); } }else { pref = new XSharedPreferences("com.yuvi.mask", "user_prefs"); pref.makeWorldReadable(); pref.reload(); } return pref; } return pref; } ``` but still can't read user_prefs.xml by Xsharedpreference please help bro
Owner

It's not SDK 24, it's target SDK 27. If you compile APK with 28+ its data won't be available to other apps even when it's world readable because of SELinux rule. Just compile your module with target SDK 27 and change xml permissions to be available to anyone: https://code.highspec.ru/Mikanoshi/CustoMIUIzer/src/branch/master/app/src/main/java/name/mikanoshi/customiuizer/utils/Helpers.java#L1455

The issue above is only with EdXposed 0.4.x.x, XSharedPreferences were fixed in 0.5.x.x and they now point to a different path in /data/misc/ which is handled by EdXposed itself. You can even use MODE_WORLD_READABLE flag when calling getSharedPreferences(), but it will crash on 0.4.x.x, so it's better to just fix permissions manually yourself. More info abut new prefs here: https://github.com/ElderDrivers/EdXposed/wiki/New-XSharedPreferences

It's not SDK 24, it's target SDK 27. If you compile APK with 28+ its data won't be available to other apps even when it's world readable because of SELinux rule. Just compile your module with target SDK 27 and change xml permissions to be available to anyone: https://code.highspec.ru/Mikanoshi/CustoMIUIzer/src/branch/master/app/src/main/java/name/mikanoshi/customiuizer/utils/Helpers.java#L1455 The issue above is only with EdXposed 0.4.x.x, XSharedPreferences were fixed in 0.5.x.x and they now point to a different path in /data/misc/ which is handled by EdXposed itself. You can even use MODE_WORLD_READABLE flag when calling getSharedPreferences(), but it will crash on 0.4.x.x, so it's better to just fix permissions manually yourself. More info abut new prefs here: https://github.com/ElderDrivers/EdXposed/wiki/New-XSharedPreferences
Poster
@SuppressLint({"SetWorldReadable"})
    static void a(Context context) {
        File file = new File(context.getApplicationInfo().dataDir);
        File file2 = new File(context.getApplicationInfo().dataDir, "shared_prefs");
        if (file.exists()) {
            file.setExecutable(true, false);
            file.setReadable(true, false);
            if (file2.exists()) {
                file2.setExecutable(true, false);
                file2.setReadable(true, false);
                File file3 = new File(file2, "user_prefs.xml");
                if (file3.exists()) {
                    file3.setReadable(true, false);
                }
            }
        }
    }

Your Method sounds Great!! I understand but i giving support sdk < 24 and sdk > 24 so that why i use this i found someone else github...

can you just explain why we have to use this method for make user_prefs world readable?? any Idea

``` @SuppressLint({"SetWorldReadable"}) static void a(Context context) { File file = new File(context.getApplicationInfo().dataDir); File file2 = new File(context.getApplicationInfo().dataDir, "shared_prefs"); if (file.exists()) { file.setExecutable(true, false); file.setReadable(true, false); if (file2.exists()) { file2.setExecutable(true, false); file2.setReadable(true, false); File file3 = new File(file2, "user_prefs.xml"); if (file3.exists()) { file3.setReadable(true, false); } } } } ``` Your Method sounds Great!! I understand but i giving support sdk < 24 and sdk > 24 so that why i use this i found someone else github... can you just explain why we have to use this method for make user_prefs world readable?? any Idea
Owner

You just need to make shared prefs xml file world readable so any hooked app could read prefs from it. I prefer to open it once in initZygote and then read all the prefs and store them in a Map variable for later use in mods. It is actually only useful if you need to read prefs once on process start. If you need settings to change while process is still running you should implement ContentProvider in module and ContentResolver in mods. This will only work after system is booted though (content provider servcie is started).

Just see how it is done in CustoMIUIzer, it supports all these things :) It detects current path to xml under both EdXposed 0.4 and 0.5, sets required permissions and also loads prefs depending on Xposed version: https://code.highspec.ru/Mikanoshi/CustoMIUIzer/src/branch/master/app/src/main/java/name/mikanoshi/customiuizer/utils/Helpers.java#L1420 https://code.highspec.ru/Mikanoshi/CustoMIUIzer/src/branch/master/app/src/main/java/name/mikanoshi/customiuizer/MainModule.java#L38

You just need to make shared prefs xml file world readable so any hooked app could read prefs from it. I prefer to open it once in initZygote and then read all the prefs and store them in a Map variable for later use in mods. It is actually only useful if you need to read prefs once on process start. If you need settings to change while process is still running you should implement ContentProvider in module and ContentResolver in mods. This will only work after system is booted though (content provider servcie is started). Just see how it is done in CustoMIUIzer, it supports all these things :) It detects current path to xml under both EdXposed 0.4 and 0.5, sets required permissions and also loads prefs depending on Xposed version: https://code.highspec.ru/Mikanoshi/CustoMIUIzer/src/branch/master/app/src/main/java/name/mikanoshi/customiuizer/utils/Helpers.java#L1420 https://code.highspec.ru/Mikanoshi/CustoMIUIzer/src/branch/master/app/src/main/java/name/mikanoshi/customiuizer/MainModule.java#L38
Poster

Yah you right i got root of cause my xposed module working in SDK < 24 without any issue but its not working in SDK > 24 my module change or replace parm with help of IXposedHookLoadPackage and that parm in String and when user put anynumber in my module it will replace or change but for changing parm all String store in user_prefs.xml and here is cause of issue SDK > 24 cant access user_prefs so you have any good idea for accesss user_prefs.xml on above SDK 24 I Appericate....if you could help

root of cause:

if (Build.VERSION.SDK_INT > 24)
            pref = getSharedPreferences("user_prefs", Context.MODE_PRIVATE);
        else {
            pref = getSharedPreferences("user_prefs", Context.MODE_WORLD_READABLE);
        }
Yah you right i got root of cause my xposed module working in SDK < 24 without any issue but its not working in SDK > 24 my module change or replace parm with help of IXposedHookLoadPackage and that parm in String and when user put anynumber in my module it will replace or change but for changing parm all String store in user_prefs.xml and here is cause of issue SDK > 24 cant access user_prefs so you have any good idea for accesss user_prefs.xml on above SDK 24 I Appericate....if you could help **root of cause:** ``` if (Build.VERSION.SDK_INT > 24) pref = getSharedPreferences("user_prefs", Context.MODE_PRIVATE); else { pref = getSharedPreferences("user_prefs", Context.MODE_WORLD_READABLE); } ```
Owner

Just make user_prefs.xml world readable like it is done in my module. You have to fix permissions every time something is changed in your module (there is a listener for shared prefs changes). Don't hardcode a path to this xml though (/data/data/ or /data/user_de/) and don't use getDataDir(), because path is different on EdXposed 0.5.x.x if new XSharedPreferences are used.

Just make user_prefs.xml world readable like it is done in my module. You have to fix permissions every time something is changed in your module (there is a listener for shared prefs changes). Don't hardcode a path to this xml though (/data/data/ or /data/user_de/) and don't use getDataDir(), because path is different on EdXposed 0.5.x.x if new XSharedPreferences are used.
Poster

ok Bro Thanks i am looking your code where you use fixPermissionsAsync method? can you giving link where you use it

ok Bro Thanks i am looking your code where you use fixPermissionsAsync method? can you giving link where you use it
Owner
Every time an xml file change is detected: https://code.highspec.ru/Mikanoshi/CustoMIUIzer/src/commit/722610927814b08274926870bbef450008bc5463/app/src/main/java/name/mikanoshi/customiuizer/MainActivity.java#L82
Mikanoshi closed this issue 3 years ago
Poster

Hey bro i was on break so i stopped working on this module and now i agin started work on it now i tried new XsharedPreference by edxposed and module working but i checked when i install my module in that devices which edxposed not installed then its not even open coz i use sharedPreferences = getSharedPreferences(USER_PREF, Context.MODE_WORLD_READABLE) and i tried one more thing here i was not use new xshared and fixed permission of preference folder and then i see when i change my selinux permission Permassive my module works great on any device and xposed versions so issue is selinux rule in the end after fix permission its not worked and then i changed selinux permission and its working but issue is that how can read preference without use new Xsharedpreference or without change selinux permission i appericate if you could help regarding

Hey bro i was on break so i stopped working on this module and now i agin started work on it now i tried new XsharedPreference by edxposed and module working but i checked when i install my module in that devices which edxposed not installed then its not even open coz i use sharedPreferences = getSharedPreferences(USER_PREF, Context.MODE_WORLD_READABLE) and i tried one more thing here i was not use new xshared and fixed permission of preference folder and then i see when i change my selinux permission Permassive my module works great on any device and xposed versions so issue is selinux rule in the end after fix permission its not worked and then i changed selinux permission and its working but issue is that how can read preference without use new Xsharedpreference or without change selinux permission i appericate if you could help regarding
AndroidX reopened this issue 3 years ago
Owner

Yes, it will crash without EdXposed, that's why you should do this: https://code.highspec.ru/Mikanoshi/CustoMIUIzer/src/branch/master/app/src/main/java/name/mikanoshi/customiuizer/utils/Helpers.java#L1434

If you want to read prefs directly from xml without new XSharedPreferences you must set permissions for that file to be world readable and also compile your module's APK with target SDK <= 27. And even then some processes won't be able to read it, usually happens when app list mode (black\white list) is active in EdXposed Manager.

When using new XSharedPreferences you still have to fix file permissions, but APK can target any SDK.

Yes, it will crash without EdXposed, that's why you should do this: https://code.highspec.ru/Mikanoshi/CustoMIUIzer/src/branch/master/app/src/main/java/name/mikanoshi/customiuizer/utils/Helpers.java#L1434 If you want to read prefs directly from xml without new XSharedPreferences you must set permissions for that file to be world readable and also compile your module's APK with target SDK <= 27. And even then some processes won't be able to read it, usually happens when app list mode (black\white list) is active in EdXposed Manager. When using new XSharedPreferences you still have to fix file permissions, but APK can target any SDK.
Poster

Yes, it will crash without EdXposed, that's why you should do this: https://code.highspec.ru/Mikanoshi/CustoMIUIzer/src/branch/master/app/src/main/java/name/mikanoshi/customiuizer/utils/Helpers.java#L1434

If you want to read prefs directly from xml without new XSharedPreferences you must set permissions for that file to be world readable and also compile your module's APK with target SDK <= 27. And even then some processes won't be able to read it, usually happens when app list mode (black\white list) is active in EdXposed Manager.

When using new XSharedPreferences you still have to fix file permissions, but APK can target any SDK.

Seriouly man you are really genius i make target sdk 23 and its work like magic you really helped me and now i am thinking why i dont get that idea before😂 but thanks again it Fixed!!!

> Yes, it will crash without EdXposed, that's why you should do this: > https://code.highspec.ru/Mikanoshi/CustoMIUIzer/src/branch/master/app/src/main/java/name/mikanoshi/customiuizer/utils/Helpers.java#L1434 > > If you want to read prefs directly from xml without new XSharedPreferences you must set permissions for that file to be world readable and also compile your module's APK with target SDK <= 27. And even then some processes won't be able to read it, usually happens when app list mode (black\white list) is active in EdXposed Manager. > > When using new XSharedPreferences you still have to fix file permissions, but APK can target any SDK. Seriouly man you are really genius i make target sdk 23 and its work like magic you really helped me and now i am thinking why i dont get that idea before😂 but thanks again it Fixed!!!
Mikanoshi closed this issue 3 years ago
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: Mikanoshi/CustoMIUIzer#178
Loading…
There is no content yet.