Skip to content

Commit 183ce02

Browse files
author
Dianne Hackborn
committed
Fix issue #6761130: Clearing app data in settings does not clear app's USB storage
The package manager calls to clear data / clear cache were not also having default container service clear the data on external storage. Now they do. Change-Id: Ib5e5eb6adf2cac5a4cc094cc1a02ac8cfb6a2edf
1 parent 074b54f commit 183ce02

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

core/java/com/android/internal/app/IMediaContainerService.aidl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ interface IMediaContainerService {
3535
long calculateDirectorySize(in String directory);
3636
/** Return file system stats: [0] is total bytes, [1] is available bytes */
3737
long[] getFileSystemStats(in String path);
38+
void clearDirectory(in String directory);
3839
}

packages/DefaultContainerService/src/com/android/defcontainer/DefaultContainerService.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,16 @@ public long[] getFileSystemStats(String path) {
246246
throw new IllegalStateException(e);
247247
}
248248
}
249+
250+
@Override
251+
public void clearDirectory(String path) throws RemoteException {
252+
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
253+
254+
final File directory = new File(path);
255+
if (directory.exists() && directory.isDirectory()) {
256+
eraseFiles(directory);
257+
}
258+
}
249259
};
250260

251261
public DefaultContainerService() {

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7728,6 +7728,80 @@ private boolean deletePackageLI(String packageName,
77287728
return ret;
77297729
}
77307730

7731+
private final class ClearStorageConnection implements ServiceConnection {
7732+
IMediaContainerService mContainerService;
7733+
7734+
@Override
7735+
public void onServiceConnected(ComponentName name, IBinder service) {
7736+
synchronized (this) {
7737+
mContainerService = IMediaContainerService.Stub.asInterface(service);
7738+
notifyAll();
7739+
}
7740+
}
7741+
7742+
@Override
7743+
public void onServiceDisconnected(ComponentName name) {
7744+
}
7745+
}
7746+
7747+
private void clearExternalStorageDataSync(String packageName, boolean allData) {
7748+
final boolean mounted;
7749+
if (Environment.isExternalStorageEmulated()) {
7750+
mounted = true;
7751+
} else {
7752+
final String status = Environment.getExternalStorageState();
7753+
7754+
mounted = status.equals(Environment.MEDIA_MOUNTED)
7755+
|| status.equals(Environment.MEDIA_MOUNTED_READ_ONLY);
7756+
}
7757+
7758+
if (!mounted) {
7759+
return;
7760+
}
7761+
7762+
final Intent containerIntent = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT);
7763+
ClearStorageConnection conn = new ClearStorageConnection();
7764+
if (mContext.bindService(containerIntent, conn, Context.BIND_AUTO_CREATE)) {
7765+
try {
7766+
long timeout = SystemClock.uptimeMillis() + 5000;
7767+
synchronized (conn) {
7768+
long now = SystemClock.uptimeMillis();
7769+
while (conn.mContainerService == null && now < timeout) {
7770+
try {
7771+
conn.wait(timeout - now);
7772+
} catch (InterruptedException e) {
7773+
}
7774+
}
7775+
}
7776+
if (conn.mContainerService == null) {
7777+
return;
7778+
}
7779+
final File externalCacheDir = Environment
7780+
.getExternalStorageAppCacheDirectory(packageName);
7781+
try {
7782+
conn.mContainerService.clearDirectory(externalCacheDir.toString());
7783+
} catch (RemoteException e) {
7784+
}
7785+
if (allData) {
7786+
final File externalDataDir = Environment
7787+
.getExternalStorageAppDataDirectory(packageName);
7788+
try {
7789+
conn.mContainerService.clearDirectory(externalDataDir.toString());
7790+
} catch (RemoteException e) {
7791+
}
7792+
final File externalMediaDir = Environment
7793+
.getExternalStorageAppMediaDirectory(packageName);
7794+
try {
7795+
conn.mContainerService.clearDirectory(externalMediaDir.toString());
7796+
} catch (RemoteException e) {
7797+
}
7798+
}
7799+
} finally {
7800+
mContext.unbindService(conn);
7801+
}
7802+
}
7803+
}
7804+
77317805
@Override
77327806
public void clearApplicationUserData(final String packageName,
77337807
final IPackageDataObserver observer, final int userId) {
@@ -7742,6 +7816,7 @@ public void run() {
77427816
synchronized (mInstallLock) {
77437817
succeeded = clearApplicationUserDataLI(packageName, userId);
77447818
}
7819+
clearExternalStorageDataSync(packageName, true);
77457820
if (succeeded) {
77467821
// invoke DeviceStorageMonitor's update method to clear any notifications
77477822
DeviceStorageMonitorService dsm = (DeviceStorageMonitorService)
@@ -7815,6 +7890,7 @@ public void run() {
78157890
synchronized (mInstallLock) {
78167891
succeded = deleteApplicationCacheFilesLI(packageName, userId);
78177892
}
7893+
clearExternalStorageDataSync(packageName, false);
78187894
if(observer != null) {
78197895
try {
78207896
observer.onRemoveCompleted(packageName, succeded);

0 commit comments

Comments
 (0)