Skip to content

Commit 61f5737

Browse files
author
Amith Yamasani
committed
Centralize the creation of the user system directory
Environment.getUserSystemDirectory(int userId) Use it all relevant places that was hardcoding it. Also, wipe out the user's system directory when user is removed, otherwise old state might be transferred to a new user. Change-Id: I788ce9c4cf9624229e65efa7047bc0c019ccef0a
1 parent 00453e7 commit 61f5737

File tree

9 files changed

+58
-22
lines changed

9 files changed

+58
-22
lines changed

core/java/android/accounts/AccountManagerService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,7 +1879,7 @@ public void handleMessage(Message msg) {
18791879

18801880
private static String getDatabaseName(int userId) {
18811881
File systemDir = Environment.getSystemSecureDirectory();
1882-
File databaseFile = new File(systemDir, "users/" + userId + "/" + DATABASE_NAME);
1882+
File databaseFile = new File(Environment.getUserSystemDirectory(userId), DATABASE_NAME);
18831883
if (userId == 0) {
18841884
// Migrate old file, if it exists, to the new location.
18851885
// Make sure the new file doesn't already exist. A dummy file could have been
@@ -1888,7 +1888,7 @@ private static String getDatabaseName(int userId) {
18881888
File oldFile = new File(systemDir, DATABASE_NAME);
18891889
if (oldFile.exists() && !databaseFile.exists()) {
18901890
// Check for use directory; create if it doesn't exist, else renameTo will fail
1891-
File userDir = new File(systemDir, "users/" + userId);
1891+
File userDir = Environment.getUserSystemDirectory(userId);
18921892
if (!userDir.exists()) {
18931893
if (!userDir.mkdirs()) {
18941894
throw new IllegalStateException("User dir cannot be created: " + userDir);

core/java/android/app/backup/WallpaperBackupHelper.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import android.content.Context;
2121
import android.graphics.BitmapFactory;
2222
import android.graphics.Point;
23+
import android.os.Environment;
2324
import android.os.ParcelFileDescriptor;
25+
import android.os.UserHandle;
2426
import android.util.Slog;
2527
import android.view.Display;
2628
import android.view.WindowManager;
@@ -39,8 +41,12 @@ public class WallpaperBackupHelper extends FileBackupHelperBase implements Backu
3941

4042
// This path must match what the WallpaperManagerService uses
4143
// TODO: Will need to change if backing up non-primary user's wallpaper
42-
public static final String WALLPAPER_IMAGE = "/data/system/users/0/wallpaper";
43-
public static final String WALLPAPER_INFO = "/data/system/users/0/wallpaper_info.xml";
44+
public static final String WALLPAPER_IMAGE =
45+
new File(Environment.getUserSystemDirectory(UserHandle.USER_OWNER),
46+
"wallpaper").getAbsolutePath();
47+
public static final String WALLPAPER_INFO =
48+
new File(Environment.getUserSystemDirectory(UserHandle.USER_OWNER),
49+
"wallpaper_info.xml").getAbsolutePath();
4450
// Use old keys to keep legacy data compatibility and avoid writing two wallpapers
4551
public static final String WALLPAPER_IMAGE_KEY =
4652
"/data/data/com.android.settings/files/wallpaper";
@@ -50,7 +56,9 @@ public class WallpaperBackupHelper extends FileBackupHelperBase implements Backu
5056
// will be saved to this file from the restore stream, then renamed to the proper
5157
// location if it's deemed suitable.
5258
// TODO: Will need to change if backing up non-primary user's wallpaper
53-
private static final String STAGE_FILE = "/data/system/users/0/wallpaper-tmp";
59+
private static final String STAGE_FILE =
60+
new File(Environment.getUserSystemDirectory(UserHandle.USER_OWNER),
61+
"wallpaper-tmp").getAbsolutePath();
5462

5563
Context mContext;
5664
String[] mFiles;

core/java/android/os/Environment.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@ public static File getMediaStorageDirectory() {
103103
return MEDIA_STORAGE_DIRECTORY;
104104
}
105105

106+
/**
107+
* Return the system directory for a user. This is for use by system services to store
108+
* files relating to the user. This directory will be automatically deleted when the user
109+
* is removed.
110+
*
111+
* @hide
112+
*/
113+
public static File getUserSystemDirectory(int userId) {
114+
return new File(new File(getSystemSecureDirectory(), "users"), Integer.toString(userId));
115+
}
116+
106117
/**
107118
* Returns whether the Encrypted File System feature is enabled on the device or not.
108119
* @return <code>true</code> if Encrypted File System feature is enabled, <code>false</code>

core/java/com/android/internal/widget/LockSettingsService.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import android.database.sqlite.SQLiteDatabase;
2424
import android.database.sqlite.SQLiteOpenHelper;
2525
import android.os.Binder;
26+
import android.os.Environment;
2627
import android.os.RemoteException;
2728
import android.os.SystemProperties;
2829
import android.os.UserHandle;
@@ -173,7 +174,8 @@ private String getLockPatternFilename(int userId) {
173174
// Leave it in the same place for user 0
174175
return dataSystemDirectory + LOCK_PATTERN_FILE;
175176
} else {
176-
return dataSystemDirectory + "users/" + userId + "/" + LOCK_PATTERN_FILE;
177+
return new File(Environment.getUserSystemDirectory(userId), LOCK_PATTERN_FILE)
178+
.getAbsolutePath();
177179
}
178180
}
179181

@@ -185,7 +187,8 @@ private String getLockPasswordFilename(int userId) {
185187
// Leave it in the same place for user 0
186188
return dataSystemDirectory + LOCK_PASSWORD_FILE;
187189
} else {
188-
return dataSystemDirectory + "users/" + userId + "/" + LOCK_PASSWORD_FILE;
190+
return new File(Environment.getUserSystemDirectory(userId), LOCK_PASSWORD_FILE)
191+
.getAbsolutePath();
189192
}
190193
}
191194

services/java/com/android/server/AppWidgetServiceImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import android.net.Uri;
4141
import android.os.Binder;
4242
import android.os.Bundle;
43+
import android.os.Environment;
4344
import android.os.IBinder;
4445
import android.os.RemoteException;
4546
import android.os.SystemClock;
@@ -1634,11 +1635,11 @@ void readStateFromFileLocked(FileInputStream stream) {
16341635
}
16351636

16361637
static File getSettingsFile(int userId) {
1637-
return new File("/data/system/users/" + userId + "/" + SETTINGS_FILENAME);
1638+
return new File(Environment.getUserSystemDirectory(userId), SETTINGS_FILENAME);
16381639
}
16391640

16401641
AtomicFile savedStateFile() {
1641-
File dir = new File("/data/system/users/" + mUserId);
1642+
File dir = Environment.getUserSystemDirectory(mUserId);
16421643
File settingsFile = getSettingsFile(mUserId);
16431644
if (!settingsFile.exists() && mUserId == 0) {
16441645
if (!dir.exists()) {

services/java/com/android/server/SystemBackupAgent.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
import android.app.backup.FullBackupDataOutput;
2525
import android.app.backup.WallpaperBackupHelper;
2626
import android.content.Context;
27+
import android.os.Environment;
2728
import android.os.ParcelFileDescriptor;
2829
import android.os.ServiceManager;
30+
import android.os.UserHandle;
2931
import android.util.Slog;
3032

3133

@@ -45,11 +47,13 @@ public class SystemBackupAgent extends BackupAgentHelper {
4547
private static final String WALLPAPER_INFO_FILENAME = "wallpaper_info.xml";
4648

4749
// TODO: Will need to change if backing up non-primary user's wallpaper
48-
private static final String WALLPAPER_IMAGE_DIR = "/data/system/users/0";
50+
private static final String WALLPAPER_IMAGE_DIR =
51+
Environment.getUserSystemDirectory(UserHandle.USER_OWNER).getAbsolutePath();
4952
private static final String WALLPAPER_IMAGE = WallpaperBackupHelper.WALLPAPER_IMAGE;
5053

5154
// TODO: Will need to change if backing up non-primary user's wallpaper
52-
private static final String WALLPAPER_INFO_DIR = "/data/system/users/0";
55+
private static final String WALLPAPER_INFO_DIR =
56+
Environment.getUserSystemDirectory(UserHandle.USER_OWNER).getAbsolutePath();
5357
private static final String WALLPAPER_INFO = WallpaperBackupHelper.WALLPAPER_INFO;
5458
// Use old keys to keep legacy data compatibility and avoid writing two wallpapers
5559
private static final String WALLPAPER_IMAGE_KEY = WallpaperBackupHelper.WALLPAPER_IMAGE_KEY;

services/java/com/android/server/WallpaperManagerService.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ class WallpaperManagerService extends IWallpaperManager.Stub {
9292
* restarting it vs. just reverting to the static wallpaper.
9393
*/
9494
static final long MIN_WALLPAPER_CRASH_TIME = 10000;
95-
96-
static final File WALLPAPER_BASE_DIR = new File("/data/system/users");
9795
static final String WALLPAPER = "wallpaper";
9896
static final String WALLPAPER_INFO = "wallpaper_info.xml";
9997

@@ -395,12 +393,12 @@ public WallpaperManagerService(Context context) {
395393
mIPackageManager = AppGlobals.getPackageManager();
396394
mMonitor = new MyPackageMonitor();
397395
mMonitor.register(context, null, true);
398-
WALLPAPER_BASE_DIR.mkdirs();
399-
loadSettingsLocked(0);
396+
getWallpaperDir(UserHandle.USER_OWNER).mkdirs();
397+
loadSettingsLocked(UserHandle.USER_OWNER);
400398
}
401399

402400
private static File getWallpaperDir(int userId) {
403-
return new File(WALLPAPER_BASE_DIR + "/" + userId);
401+
return Environment.getUserSystemDirectory(userId);
404402
}
405403

406404
@Override
@@ -414,7 +412,7 @@ protected void finalize() throws Throwable {
414412

415413
public void systemReady() {
416414
if (DEBUG) Slog.v(TAG, "systemReady");
417-
WallpaperData wallpaper = mWallpaperMap.get(0);
415+
WallpaperData wallpaper = mWallpaperMap.get(UserHandle.USER_OWNER);
418416
switchWallpaper(wallpaper);
419417
wallpaper.wallpaperObserver = new WallpaperObserver(wallpaper);
420418
wallpaper.wallpaperObserver.startWatching();
@@ -880,7 +878,7 @@ private void checkPermission(String permission) {
880878
}
881879

882880
private static JournaledFile makeJournaledFile(int userId) {
883-
final String base = getWallpaperDir(userId) + "/" + WALLPAPER_INFO;
881+
final String base = new File(getWallpaperDir(userId), WALLPAPER_INFO).getAbsolutePath();
884882
return new JournaledFile(new File(base), new File(base + ".tmp"));
885883
}
886884

services/java/com/android/server/pm/Settings.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -745,13 +745,12 @@ private void replaceUserIdLPw(int uid, Object obj) {
745745
}
746746

747747
private File getUserPackagesStateFile(int userId) {
748-
return new File(mSystemDir,
749-
"users/" + userId + "/package-restrictions.xml");
748+
return new File(Environment.getUserSystemDirectory(userId), "package-restrictions.xml");
750749
}
751750

752751
private File getUserPackagesStateBackupFile(int userId) {
753-
return new File(mSystemDir,
754-
"users/" + userId + "/package-restrictions-backup.xml");
752+
return new File(Environment.getUserSystemDirectory(userId),
753+
"package-restrictions-backup.xml");
755754
}
756755

757756
void writeAllUsersPackageRestrictionsLPr() {

services/java/com/android/server/pm/UserManagerService.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ void finishRemoveUser(int userHandle) {
594594
// Update the user list
595595
writeUserListLocked();
596596
updateUserIdsLocked();
597+
removeDirectoryRecursive(Environment.getUserSystemDirectory(userHandle));
597598
}
598599
}
599600

@@ -603,6 +604,17 @@ void finishRemoveUser(int userHandle) {
603604
mContext.sendBroadcast(addedIntent, android.Manifest.permission.MANAGE_USERS);
604605
}
605606

607+
private void removeDirectoryRecursive(File parent) {
608+
if (parent.isDirectory()) {
609+
String[] files = parent.list();
610+
for (String filename : files) {
611+
File child = new File(parent, filename);
612+
removeDirectoryRecursive(child);
613+
}
614+
}
615+
parent.delete();
616+
}
617+
606618
@Override
607619
public int getUserSerialNumber(int userHandle) {
608620
synchronized (mPackagesLock) {

0 commit comments

Comments
 (0)