Remove All Ads from XDA

How to save & load module settings after MODE_WORLD_READABLE is gone?

12 posts
Thanks Meter: 29
 
Post Reply Email Thread
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?
 
 
27th July 2017, 01:32 PM |#2  
chengyuhui's Avatar
OP Junior Member
Flag Chengdu
Thanks Meter: 29
 
More
Tried to downgrade targetSdkVersion, the app no longer crashes but XSharedPreferences still can't read the file (the preferences file is already world readable)
7th August 2017, 03:26 PM |#3  
Massi-X's Avatar
Senior Member
Thanks Meter: 289
 
More
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.
9th August 2017, 03:59 PM |#4  
laura almeida's Avatar
Senior Member
Flag Benicarló
Thanks Meter: 2,093
 
More
Quote:
Originally Posted by Massi-X

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.

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
I tested it myself and it is working fine.

Thanks to @PunchUp @pyler
The Following User Says Thank You to laura almeida For This Useful Post: [ View ] Gift laura almeida Ad-Free
9th August 2017, 05:12 PM |#5  
Massi-X's Avatar
Senior Member
Thanks Meter: 289
 
More
Quote:
Originally Posted by laura almeida

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
I tested it myself and it is working fine.

Thanks to @PunchUp @pyler

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

EDIT: I also found in marshmallow my apps was randomly failing reading the preferences (no real reason) and Remotepreferences solved the problem.
9th August 2017, 05:17 PM |#6  
laura almeida's Avatar
Senior Member
Flag Benicarló
Thanks Meter: 2,093
 
More
Quote:
Originally Posted by Massi-X

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

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.
9th August 2017, 05:42 PM |#7  
Massi-X's Avatar
Senior Member
Thanks Meter: 289
 
More
Quote:
Originally Posted by laura almeida

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.

Previous versions: I was speaking about another thing. See edit. Anyway it's another way, 2 is always better than 1.
Post Reply Subscribe to Thread

Guest Quick Reply (no urls or BBcode)
Message:
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes