@@ -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