11package cn .benstone .android .xposed .xprivatesd ;
22
33import android .text .TextUtils ;
4+ import android .util .StringBuilderPrinter ;
45
56import java .io .File ;
67import java .io .IOException ;
@@ -26,14 +27,11 @@ public class XposedModMain implements IXposedHookZygoteInit, IXposedHookLoadPack
2627 private String userSd ;
2728 private int userSdLength ;
2829
29- private String userAppSd ;
30-
31- private String perAppBase ;
3230 private int perAppBaseLength ;
3331 private String perAppBase2 ;
3432
35- private String pkgPath ;
36- private int pkgPathLength ;
33+ private String appPath ;
34+ private int appPathLength ;
3735
3836 private String perAppPath ;
3937 private String noMediaFile ;
@@ -56,44 +54,69 @@ public void initZygote(StartupParam startupParam) {
5654 prefs .makeWorldReadable ();
5755 }
5856 userSd = Common .getInternalStoragePath ();
59- userSdLength = userSd .length ();
60- // log("userSD: " + userSd);
6157 }
6258
6359 @ Override
6460 public void handleLoadPackage (LoadPackageParam lpparam ) {
6561 prefs .reload ();
6662 log_debug = prefs .getBoolean (Common .LOG_DEBUG , false );
6763
68- if (!isEnabledApp (lpparam )) {
64+ if (log_debug ) {
65+ if (userSd != null ) {
66+ log ("userSD: " + userSd );
67+ } else {
68+ log ("userSD: N/A" );
69+ }
70+
71+ if (lpparam .packageName != null ) {
72+ log ("packageName: " + lpparam .packageName );
73+ } else {
74+ log ("packageName: N/A" );
75+ }
76+
77+ if (lpparam .processName != null ) {
78+ log ("processName: " + lpparam .processName );
79+ } else {
80+ log ("processName: N/A" );
81+ }
82+
83+ if (lpparam .isFirstApplication ) {
84+ log ("isFirstApplication: true" );
85+ } else {
86+ log ("isFirstApplication: false" );
87+ }
88+
89+ if (lpparam .appInfo != null ) {
90+ StringBuilder builder = new StringBuilder ();
91+ lpparam .appInfo .dump (new StringBuilderPrinter (builder ), "" );
92+ log ("appInfo: " + builder .toString ());
93+ } else {
94+ log ("appInfo: N/A" );
95+ }
96+ }
97+
98+ if (userSd == null ) {
99+ // system services
69100 return ;
70101 }
71102
72- userAppSd = Common .getInternalStoragePath ();
73- if (log_debug && !userAppSd .equals (userSd )) {
74- log ("userAppSd: " + userAppSd );
103+ String appName = lpparam .isFirstApplication ? lpparam .packageName : lpparam .processName ;
104+ int appFlags = lpparam .appInfo != null ? lpparam .appInfo .flags : 0 ;
105+
106+ if (!isEnabledApp (appName , appFlags )) {
107+ return ;
75108 }
76109
77- perAppBase = File .separator + prefs .getString (Common .PER_APP_PATH , Common .DEFAULT_PER_APP_PATH );
110+ userSdLength = userSd .length ();
111+
112+ String perAppBase = File .separator + prefs .getString (Common .PER_APP_PATH , Common .DEFAULT_PER_APP_PATH );
78113 perAppBaseLength = perAppBase .length ();
79114 perAppBase2 = perAppBase .toLowerCase ();
80115
81- pkgPath = File .separator + lpparam .packageName .toLowerCase ();
82- pkgPathLength = pkgPath .length ();
83-
84- File perAppPathFile ;
85- if (userAppSd .equals (userSd )) {
86- perAppPathFile = new File (userSd + perAppBase + pkgPath );
87- } else {
88- if (userAppSd .startsWith (perAppBase , userSdLength )) {
89- pkgPath = userAppSd .substring (userSdLength + perAppBaseLength );
90- perAppPathFile = new File (userAppSd );
91- } else {
92- log ("WARN: strange path of sdcard: " + userAppSd );
93- perAppPathFile = new File (userAppSd + perAppBase + pkgPath );
94- }
95- }
116+ appPath = File .separator + appName .toLowerCase ();
117+ appPathLength = appPath .length ();
96118
119+ File perAppPathFile = new File (userSd + perAppBase + appPath );
97120 perAppPath = perAppPathFile .getAbsolutePath ();
98121 if (log_debug ) {
99122 log ("sandbox: " + perAppPath );
@@ -191,18 +214,13 @@ protected void afterHookedMethod(MethodHookParam param) {
191214 });
192215 }
193216
194- private boolean isEnabledApp (LoadPackageParam lpparam ) {
195- if (log_debug ) {
196- log ("app: " + lpparam .packageName );
197- }
198-
199- int appFlags = lpparam .appInfo == null ? 0 : lpparam .appInfo .flags ;
200- if (!Common .isAppHookAllow (prefs , lpparam .packageName , appFlags )) {
217+ private boolean isEnabledApp (String appName , int appFlags ) {
218+ if (!Common .isAppHookAllow (prefs , appName , appFlags )) {
201219 return false ;
202220 }
203221
204222 Set <String > enabledApps = prefs .getStringSet (Common .ENABLED_APPS , new HashSet <String >());
205- return (!enabledApps .isEmpty ()) && enabledApps .contains (lpparam . packageName );
223+ return (!enabledApps .isEmpty ()) && enabledApps .contains (appName );
206224 }
207225
208226 private static boolean isExcludePath (String path , String [] excludeList , boolean log_debug ) {
@@ -243,13 +261,12 @@ private String getPatchedPath(File oldFile) {
243261 if (inSdPath2 .endsWith (File .separator + Common .FILE_NOMEDIA )) {
244262 // redirect all ".no_media" to one
245263 newPath = noMediaFile ;
246- } else if (inSdPath2 .startsWith (perAppBase2 )) {
247- // within the home of sandbox
248- int inSdPathLength = inSdPath2 .length ();
249- int appPkgPathLength = pkgPathLength ;
264+ }
250265
251- if (!inSdPath2 .startsWith (pkgPath , perAppBaseLength )) {
252- if (inSdPathLength > perAppBaseLength ) {
266+ else if (inSdPath2 .startsWith (perAppBase2 )) {
267+ // within the home of sandbox
268+ if (!inSdPath2 .startsWith (appPath , perAppBaseLength )) {
269+ if (inSdPath2 .length () > perAppBaseLength ) {
253270 if (inSdPath2 .charAt (perAppBaseLength ) == File .separatorChar ) {
254271 newPath = perAppPath + inSdPath .substring (perAppBaseLength );
255272 } else {
@@ -260,16 +277,20 @@ private String getPatchedPath(File oldFile) {
260277 }
261278 } else {
262279 // check redundant app path
263- int subPathLength = perAppBaseLength + appPkgPathLength ;
280+ int subPathLength = perAppBaseLength + appPathLength ;
264281 int offset = subPathLength ;
265- while (inSdPath2 .startsWith (perAppBase2 + pkgPath , offset )) {
282+
283+ while (inSdPath2 .startsWith (perAppBase2 + appPath , offset )) {
266284 offset += subPathLength ;
267285 }
286+
268287 if (offset > subPathLength ) {
269288 newPath = perAppPath + inSdPath .substring (offset );
270289 }
271290 }
272- } else {
291+ }
292+
293+ else {
273294 // not in sandbox
274295 if ((excludePaths == null ) || (!isExcludePath (inSdPath2 , excludePaths , log_debug ))) {
275296 // make File object within sandbox
@@ -278,7 +299,7 @@ private String getPatchedPath(File oldFile) {
278299 }
279300 } else {
280301 // check path like /[0-9a-f]+/storage/emulated/0
281- if (( oldPath .length () > userSdLength ) && ( oldPath . charAt ( 0 ) == File . separatorChar ) ) {
302+ if (oldPath .length () > userSdLength ) {
282303 int off = oldPath .indexOf (File .separatorChar , 1 );
283304 if ((off > 0 ) && (oldPath .substring (off ).startsWith (userSd ))) {
284305 newPath = perAppPath + oldPath .substring (off + userSdLength );
0 commit comments