Search in sources :

Example 1 with UserEnvironment

use of android.os.Environment.UserEnvironment in project android_frameworks_base by ParanoidAndroid.

the class MountService method buildObbPath.

@VisibleForTesting
public static String buildObbPath(final String canonicalPath, int userId, boolean forVold) {
    // Only adjust paths when storage is emulated
    if (!Environment.isExternalStorageEmulated()) {
        return canonicalPath;
    }
    String path = canonicalPath.toString();
    // First trim off any external storage prefix
    final UserEnvironment userEnv = new UserEnvironment(userId);
    // /storage/emulated/0
    final String externalPath = userEnv.getExternalStorageDirectory().toString();
    // /storage/emulated_legacy
    final String legacyExternalPath = Environment.getLegacyExternalStorageDirectory().toString();
    if (path.startsWith(externalPath)) {
        path = path.substring(externalPath.length() + 1);
    } else if (path.startsWith(legacyExternalPath)) {
        path = path.substring(legacyExternalPath.length() + 1);
    } else {
        return canonicalPath;
    }
    // Handle special OBB paths on emulated storage
    final String obbPath = "Android/obb";
    if (path.startsWith(obbPath)) {
        path = path.substring(obbPath.length() + 1);
        if (forVold) {
            return new File(Environment.getEmulatedStorageObbSource(), path).toString();
        } else {
            final UserEnvironment ownerEnv = new UserEnvironment(UserHandle.USER_OWNER);
            return new File(ownerEnv.getExternalStorageObbDirectory(), path).toString();
        }
    }
    // Handle normal external storage paths
    if (forVold) {
        return new File(Environment.getEmulatedStorageSource(userId), path).toString();
    } else {
        return new File(userEnv.getExternalStorageDirectory(), path).toString();
    }
}
Also used : UserEnvironment(android.os.Environment.UserEnvironment) File(java.io.File) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 2 with UserEnvironment

use of android.os.Environment.UserEnvironment in project android_frameworks_base by ParanoidAndroid.

the class PackageManagerService method clearExternalStorageDataSync.

private void clearExternalStorageDataSync(String packageName, int userId, boolean allData) {
    final boolean mounted;
    if (Environment.isExternalStorageEmulated()) {
        mounted = true;
    } else {
        final String status = Environment.getExternalStorageState();
        mounted = status.equals(Environment.MEDIA_MOUNTED) || status.equals(Environment.MEDIA_MOUNTED_READ_ONLY);
    }
    if (!mounted) {
        return;
    }
    final Intent containerIntent = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT);
    int[] users;
    if (userId == UserHandle.USER_ALL) {
        users = sUserManager.getUserIds();
    } else {
        users = new int[] { userId };
    }
    final ClearStorageConnection conn = new ClearStorageConnection();
    if (mContext.bindServiceAsUser(containerIntent, conn, Context.BIND_AUTO_CREATE, UserHandle.OWNER)) {
        try {
            for (int curUser : users) {
                long timeout = SystemClock.uptimeMillis() + 5000;
                synchronized (conn) {
                    long now = SystemClock.uptimeMillis();
                    while (conn.mContainerService == null && now < timeout) {
                        try {
                            conn.wait(timeout - now);
                        } catch (InterruptedException e) {
                        }
                    }
                }
                if (conn.mContainerService == null) {
                    return;
                }
                final UserEnvironment userEnv = new UserEnvironment(curUser);
                final File externalCacheDir = userEnv.getExternalStorageAppCacheDirectory(packageName);
                try {
                    conn.mContainerService.clearDirectory(externalCacheDir.toString());
                } catch (RemoteException e) {
                }
                if (allData) {
                    final File externalDataDir = userEnv.getExternalStorageAppDataDirectory(packageName);
                    try {
                        conn.mContainerService.clearDirectory(externalDataDir.toString());
                    } catch (RemoteException e) {
                    }
                    final File externalMediaDir = userEnv.getExternalStorageAppMediaDirectory(packageName);
                    try {
                        conn.mContainerService.clearDirectory(externalMediaDir.toString());
                    } catch (RemoteException e) {
                    }
                }
            }
        } finally {
            mContext.unbindService(conn);
        }
    }
}
Also used : Intent(android.content.Intent) UserEnvironment(android.os.Environment.UserEnvironment) RemoteException(android.os.RemoteException) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 3 with UserEnvironment

use of android.os.Environment.UserEnvironment in project android_frameworks_base by ResurrectionRemix.

the class DefaultContainerService method onHandleIntent.

@Override
protected void onHandleIntent(Intent intent) {
    if (PackageManager.ACTION_CLEAN_EXTERNAL_STORAGE.equals(intent.getAction())) {
        final IPackageManager pm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
        PackageCleanItem item = null;
        try {
            while ((item = pm.nextPackageToClean(item)) != null) {
                final UserEnvironment userEnv = new UserEnvironment(item.userId);
                eraseFiles(userEnv.buildExternalStorageAppDataDirs(item.packageName));
                eraseFiles(userEnv.buildExternalStorageAppMediaDirs(item.packageName));
                if (item.andCode) {
                    eraseFiles(userEnv.buildExternalStorageAppObbDirs(item.packageName));
                }
            }
        } catch (RemoteException e) {
        }
    }
}
Also used : IPackageManager(android.content.pm.IPackageManager) PackageCleanItem(android.content.pm.PackageCleanItem) UserEnvironment(android.os.Environment.UserEnvironment) RemoteException(android.os.RemoteException)

Example 4 with UserEnvironment

use of android.os.Environment.UserEnvironment in project android_frameworks_base by DirtyUnicorns.

the class PackageManagerService method clearExternalStorageDataSync.

private void clearExternalStorageDataSync(String packageName, int userId, boolean allData) {
    if (DEFAULT_CONTAINER_PACKAGE.equals(packageName))
        return;
    final boolean mounted;
    if (Environment.isExternalStorageEmulated()) {
        mounted = true;
    } else {
        final String status = Environment.getExternalStorageState();
        mounted = status.equals(Environment.MEDIA_MOUNTED) || status.equals(Environment.MEDIA_MOUNTED_READ_ONLY);
    }
    if (!mounted) {
        return;
    }
    final Intent containerIntent = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT);
    int[] users;
    if (userId == UserHandle.USER_ALL) {
        users = sUserManager.getUserIds();
    } else {
        users = new int[] { userId };
    }
    final ClearStorageConnection conn = new ClearStorageConnection();
    if (mContext.bindServiceAsUser(containerIntent, conn, Context.BIND_AUTO_CREATE, UserHandle.SYSTEM)) {
        try {
            for (int curUser : users) {
                long timeout = SystemClock.uptimeMillis() + 5000;
                synchronized (conn) {
                    long now;
                    while (conn.mContainerService == null && (now = SystemClock.uptimeMillis()) < timeout) {
                        try {
                            conn.wait(timeout - now);
                        } catch (InterruptedException e) {
                        }
                    }
                }
                if (conn.mContainerService == null) {
                    return;
                }
                final UserEnvironment userEnv = new UserEnvironment(curUser);
                clearDirectory(conn.mContainerService, userEnv.buildExternalStorageAppCacheDirs(packageName));
                if (allData) {
                    clearDirectory(conn.mContainerService, userEnv.buildExternalStorageAppDataDirs(packageName));
                    clearDirectory(conn.mContainerService, userEnv.buildExternalStorageAppMediaDirs(packageName));
                }
            }
        } finally {
            mContext.unbindService(conn);
        }
    }
}
Also used : Intent(android.content.Intent) UserEnvironment(android.os.Environment.UserEnvironment)

Example 5 with UserEnvironment

use of android.os.Environment.UserEnvironment in project android_frameworks_base by DirtyUnicorns.

the class MountService method mkdirs.

@Override
public int mkdirs(String callingPkg, String appPath) {
    final int userId = UserHandle.getUserId(Binder.getCallingUid());
    final UserEnvironment userEnv = new UserEnvironment(userId);
    // Validate that reported package name belongs to caller
    final AppOpsManager appOps = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
    appOps.checkPackage(Binder.getCallingUid(), callingPkg);
    File appFile = null;
    try {
        appFile = new File(appPath).getCanonicalFile();
    } catch (IOException e) {
        Slog.e(TAG, "Failed to resolve " + appPath + ": " + e);
        return -1;
    }
    // belong to the calling package.
    if (FileUtils.contains(userEnv.buildExternalStorageAppDataDirs(callingPkg), appFile) || FileUtils.contains(userEnv.buildExternalStorageAppObbDirs(callingPkg), appFile) || FileUtils.contains(userEnv.buildExternalStorageAppMediaDirs(callingPkg), appFile)) {
        appPath = appFile.getAbsolutePath();
        if (!appPath.endsWith("/")) {
            appPath = appPath + "/";
        }
        try {
            mConnector.execute("volume", "mkdirs", appPath);
            return 0;
        } catch (NativeDaemonConnectorException e) {
            return e.getCode();
        }
    }
    throw new SecurityException("Invalid mkdirs path: " + appFile);
}
Also used : AppOpsManager(android.app.AppOpsManager) UserEnvironment(android.os.Environment.UserEnvironment) IOException(java.io.IOException) File(java.io.File) AtomicFile(android.util.AtomicFile)

Aggregations

UserEnvironment (android.os.Environment.UserEnvironment)19 File (java.io.File)11 RemoteException (android.os.RemoteException)7 IPackageManager (android.content.pm.IPackageManager)6 PackageCleanItem (android.content.pm.PackageCleanItem)6 AppOpsManager (android.app.AppOpsManager)5 AtomicFile (android.util.AtomicFile)5 IOException (java.io.IOException)5 Intent (android.content.Intent)3 VolumeInfo (android.os.storage.VolumeInfo)3 StorageVolume (android.os.storage.StorageVolume)1 VisibleForTesting (com.android.internal.annotations.VisibleForTesting)1 ZipFile (java.util.zip.ZipFile)1