Skip to content

Commit c53183a

Browse files
committed
Fix config can't read
1 parent 44cd96a commit c53183a

File tree

6 files changed

+53
-24
lines changed

6 files changed

+53
-24
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ android {
1515
minSdkVersion 24
1616
//noinspection OldTargetApi
1717
targetSdkVersion 27
18-
versionCode 1
19-
versionName "1.0"
18+
versionCode 2
19+
versionName "1.1"
2020
}
2121

2222
buildTypes {

app/src/main/java/org/meowcat/xposed/mipush/Enhancement.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) {
5353
@Override
5454
protected void afterHookedMethod(MethodHookParam param) {
5555
final Context context = (Context) param.args[0];
56+
final ApplicationInfo applicationInfo = context.getApplicationInfo();
57+
if (applicationInfo == null) {
58+
// null application info, exit
59+
return;
60+
}
61+
final int userId = UserHandle.getUserHandleForUid(applicationInfo.uid).hashCode();
5662
final boolean availability = Utils.getParamAvailability(param, Binder.getCallingPid());
5763

5864
if (packageName.equals(BuildConfig.APPLICATION_ID)) {
@@ -66,7 +72,7 @@ protected void afterHookedMethod(MethodHookParam param) {
6672
}
6773

6874
try {
69-
final IniConf conf = new IniConf(context);
75+
final IniConf conf = new IniConf(userId);
7076

7177
switch (conf.get(MODULE_WORKING_MODE, "blacklist")) {
7278
case MODE_BLACK:

app/src/main/java/top/trumeet/mipush/settings/PreferenceFragment.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.os.Build;
77
import android.os.Bundle;
88
import android.os.SystemProperties;
9+
import android.os.UserHandle;
910
import android.text.Html;
1011
import android.util.Pair;
1112
import android.view.Menu;
@@ -49,7 +50,7 @@ public void onCreate(Bundle savedInstanceState) {
4950
setHasOptionsMenu(true);
5051
if (savedInstanceState == null) {
5152
try {
52-
mConf = new IniConf(requireContext());
53+
mConf = new IniConf(UserHandle.getUserHandleForUid(requireContext().getApplicationInfo().uid).hashCode());
5354
} catch (IOException e) {
5455
// We have encountered an unrecoverable error.
5556
throw new RuntimeException(e);

app/src/main/java/top/trumeet/mipush/settings/ini/IniConf.java

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package top.trumeet.mipush.settings.ini;
22

3+
import android.annotation.SuppressLint;
34
import android.content.Context;
5+
import android.os.FileUtils;
46
import android.util.Log;
57

68
import androidx.annotation.NonNull;
@@ -18,6 +20,8 @@
1820
import java.util.Map;
1921
import java.util.Objects;
2022

23+
import static top.trumeet.mipush.settings.ini.IniConstants.CONF_FILE;
24+
import static top.trumeet.mipush.settings.ini.IniConstants.CONF_PATH;
2125
import static top.trumeet.mipush.settings.ini.IniConstants.INI_DEFAULT;
2226
import static top.trumeet.mipush.settings.ini.IniConstants.TAG;
2327
import static top.trumeet.mipush.settings.ini.IniConstants.rwxrwxrwx;
@@ -52,19 +56,28 @@ public class IniConf {
5256
* If the file is absent, it will be created.
5357
* If the file is null, write() will do nothing.
5458
*/
55-
public IniConf(@Nullable final File file) throws IOException {
56-
if (file != null) {
57-
setFilePermissionsFromMode(file, rwxrwxrwx);
59+
@SuppressWarnings({"ResultOfMethodCallIgnored"})
60+
public IniConf(final File file) throws IOException {
61+
final File conf_path = new File(file, CONF_PATH);
62+
final File conf_file = new File(conf_path, CONF_FILE);
63+
64+
FileUtils.setPermissions(file.getAbsolutePath() + "/", rwxrwxrwx, -1, -1);
65+
66+
if (!conf_path.exists()) {
67+
conf_path.mkdir();
5868
}
59-
mIni = createDefaultConfig(file);
69+
70+
FileUtils.setPermissions(conf_path.getAbsolutePath() + "/", rwxrwxrwx, -1, -1);
71+
72+
mIni = createDefaultConfig(conf_file);
6073
// TODO: File locking?
6174
}
6275

6376
/**
6477
* Construct using the default path
6578
*/
66-
public IniConf(Context context) throws IOException {
67-
this(IniUtils.getConfigPath(context));
79+
public IniConf(int userId) throws IOException {
80+
this(IniUtils.getConfigPath(userId));
6881
}
6982

7083
/**
@@ -74,15 +87,13 @@ public IniConf(Context context) throws IOException {
7487
* @return The upgraded ini. It will not be stored in file system.
7588
* @throws IOException In case there's errors.
7689
*/
77-
private Ini createDefaultConfig(@Nullable final File file) throws IOException {
90+
private Ini createDefaultConfig(final File file) throws IOException {
7891
final Ini defaultIni = new Ini();
7992
defaultIni.load(new ByteArrayInputStream(INI_DEFAULT.getBytes(StandardCharsets.UTF_8)));
8093
final Ini currentIni = new Ini();
81-
if (file != null) {
82-
currentIni.setFile(file);
83-
if (file.exists()) {
84-
currentIni.load(file);
85-
}
94+
currentIni.setFile(file);
95+
if (file.exists()) {
96+
currentIni.load(file);
8697
}
8798
// Instead of just copying, we compare and proceed the diffs.
8899
// Check for absent options to be added.
@@ -139,19 +150,21 @@ public Map<IniKey, Object> getAll() {
139150
/**
140151
* Write all changes to the file system. If the file is absent, it will be created.
141152
*/
142-
@SuppressWarnings("ResultOfMethodCallIgnored")
153+
@SuppressLint("WorldReadableFiles")
154+
@SuppressWarnings({"ResultOfMethodCallIgnored", "deprecation"})
143155
public void write() throws IOException {
144156
// Log.d(TAG, "Saving configuration");
145157
if (mIni.getFile() != null) {
146158
// Create the parent if needed.
147159
final File parent = mIni.getFile().getParentFile();
160+
FileUtils.setPermissions(parent.getParent() + "/", 511, -1, -1);
148161
if (!Objects.requireNonNull(parent).exists()) {
149162
parent.mkdir();
150163
}
151164
// Write, or create.
152165
mIni.store();
153166

154-
setFilePermissionsFromMode(mIni.getFile(), rwxrwxrwx);
167+
setFilePermissionsFromMode(mIni.getFile().getAbsolutePath(), Context.MODE_WORLD_READABLE);
155168
} else {
156169
Log.w(TAG, "File is null. Ignoring.");
157170
}

app/src/main/java/top/trumeet/mipush/settings/ini/IniConstants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ public final class IniConstants {
66
// linux permission rwx rwx rwx, OCT is 00777, DEX is 511
77
public static final int rwxrwxrwx = 511;
88

9+
public static final String CONF_PATH = "etc";
10+
public static final String CONF_FILE = "module.conf";
11+
912
public static final IniKey MODULE_WORKING_MODE = new IniKey("module", "working_mode");
1013
public static final IniKey MODULE_BLACKLIST = new IniKey("module", "blacklist");
1114
public static final IniKey MODULE_WHITELIST = new IniKey("module", "whitelist");

app/src/main/java/top/trumeet/mipush/settings/ini/IniUtils.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import androidx.annotation.NonNull;
88

9+
import org.meowcat.xposed.mipush.BuildConfig;
10+
911
import java.io.File;
1012

1113
public class IniUtils {
@@ -31,12 +33,12 @@ public static String convertKeyToSP(@NonNull final IniKey key) {
3133
/**
3234
* Change file permission with given permission mode
3335
*
34-
* @param file file to change permissions
36+
* @param name file to change permissions
3537
* @param mode file permission
3638
*/
3739
@SuppressWarnings("deprecation")
3840
@SuppressLint({"WorldReadableFiles", "WorldWriteableFiles"})
39-
public static void setFilePermissionsFromMode(File file, int mode) {
41+
public static void setFilePermissionsFromMode(String name, int mode) {
4042
int perms = FileUtils.S_IRUSR | FileUtils.S_IWUSR
4143
| FileUtils.S_IRGRP | FileUtils.S_IWGRP;
4244
if ((mode & Context.MODE_WORLD_READABLE) != 0) {
@@ -45,18 +47,22 @@ public static void setFilePermissionsFromMode(File file, int mode) {
4547
if ((mode & Context.MODE_WORLD_WRITEABLE) != 0) {
4648
perms |= FileUtils.S_IWOTH;
4749
}
48-
FileUtils.setPermissions(file.getAbsolutePath(), perms, -1, -1);
50+
FileUtils.setPermissions(name, perms, -1, -1);
4951
}
5052

5153
/**
5254
* Get the configuration path
5355
* The configuration may be deleted, but it can be simply recreated. In such cases,
5456
* it will return a File with exists() = false.
5557
*
56-
* @param context Application context
58+
* @param userId userId
5759
* @return Path of the configuration
5860
*/
59-
public static File getConfigPath(Context context) {
60-
return new File(context.getApplicationInfo().deviceProtectedDataDir + "/etc/module.conf");
61+
public static File getConfigPath(int userId) {
62+
if (userId < 0) {
63+
// system user, try to load main user's conf
64+
userId = 0;
65+
}
66+
return new File(String.format("/data/user_de/%s/%s/", userId, BuildConfig.APPLICATION_ID));
6167
}
6268
}

0 commit comments

Comments
 (0)