OSDN Git Service

Change storage migration to use quota APIs.
authorJeff Sharkey <jsharkey@android.com>
Thu, 6 Jul 2017 17:29:24 +0000 (11:29 -0600)
committerAndreas Gampe <agampe@google.com>
Tue, 31 Oct 2017 16:48:11 +0000 (09:48 -0700)
New quota APIs are much faster than trying to measure manually, and
removing this last user of calculateDirectorySize() means we can
remove it once and for all.

(cherry picked from commit fc522c677d2bcb4835f799b39993c69f4c222e55)

Bug: 36056324
Test: builds, boots
Merged-In: Icdf774cff520a4b7ca6ec210b34a1c5ff85f8110
Change-Id: Icdf774cff520a4b7ca6ec210b34a1c5ff85f8110

src/com/android/settings/deviceinfo/MigrateEstimateTask.java

index 395b6a2..c41ebe0 100644 (file)
 
 package com.android.settings.deviceinfo;
 
-import android.content.ComponentName;
+import static com.android.settings.deviceinfo.StorageSettings.TAG;
+
+import android.app.usage.ExternalStorageStats;
+import android.app.usage.StorageStatsManager;
 import android.content.Context;
 import android.content.Intent;
-import android.content.ServiceConnection;
+import android.content.pm.UserInfo;
 import android.net.TrafficStats;
 import android.os.AsyncTask;
-import android.os.IBinder;
-import android.os.RemoteException;
 import android.os.UserHandle;
+import android.os.UserManager;
 import android.os.storage.StorageManager;
 import android.os.storage.VolumeInfo;
 import android.telecom.Log;
 import android.text.format.DateUtils;
 import android.text.format.Formatter;
 
-import com.android.internal.app.IMediaContainerService;
-
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.TimeUnit;
-
-import static com.android.settings.deviceinfo.StorageSettings.TAG;
+import java.io.IOException;
+import java.util.UUID;
 
-public abstract class MigrateEstimateTask extends AsyncTask<Void, Void, Long> implements
-        ServiceConnection {
+public abstract class MigrateEstimateTask extends AsyncTask<Void, Void, Long> {
     private static final String EXTRA_SIZE_BYTES = "size_bytes";
 
-    private static final ComponentName DEFAULT_CONTAINER_COMPONENT = new ComponentName(
-            "com.android.defcontainer", "com.android.defcontainer.DefaultContainerService");
-
     /**
      * Assume roughly a Class 10 card.
      */
     private static final long SPEED_ESTIMATE_BPS = 10 * TrafficStats.MB_IN_BYTES;
 
     private final Context mContext;
-    private final StorageManager mStorage;
-
-    private final CountDownLatch mConnected = new CountDownLatch(1);
-    private IMediaContainerService mService;
 
     private long mSizeBytes = -1;
 
     public MigrateEstimateTask(Context context) {
         mContext = context;
-        mStorage = context.getSystemService(StorageManager.class);
     }
 
     public void copyFrom(Intent intent) {
@@ -77,31 +66,36 @@ public abstract class MigrateEstimateTask extends AsyncTask<Void, Void, Long> im
             return mSizeBytes;
         }
 
+        final UserManager user = mContext.getSystemService(UserManager.class);
+        final StorageManager storage = mContext.getSystemService(StorageManager.class);
+        final StorageStatsManager stats = mContext.getSystemService(StorageStatsManager.class);
+
         final VolumeInfo privateVol = mContext.getPackageManager().getPrimaryStorageCurrentVolume();
-        final VolumeInfo emulatedVol = mStorage.findEmulatedForPrivate(privateVol);
+        final VolumeInfo emulatedVol = storage.findEmulatedForPrivate(privateVol);
 
         if (emulatedVol == null) {
             Log.w(TAG, "Failed to find current primary storage");
             return -1L;
         }
 
-        final String path = emulatedVol.getPath().getAbsolutePath();
-        Log.d(TAG, "Estimating for current path " + path);
-
-        final Intent intent = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT);
-        mContext.bindServiceAsUser(intent, this, Context.BIND_AUTO_CREATE, UserHandle.SYSTEM);
-
         try {
-            if (mConnected.await(15, TimeUnit.SECONDS)) {
-                return mService.calculateDirectorySize(path);
+            final UUID emulatedUuid = storage.getUuidForPath(emulatedVol.getPath());
+            Log.d(TAG, "Measuring size of " + emulatedUuid);
+
+            long size = 0;
+            for (UserInfo u : user.getUsers()) {
+                final ExternalStorageStats s = stats.queryExternalStatsForUser(emulatedUuid,
+                        UserHandle.of(u.id));
+                size += s.getTotalBytes();
+                if (u.id == UserHandle.USER_SYSTEM) {
+                    size += s.getObbBytes();
+                }
             }
-        } catch (InterruptedException | RemoteException e) {
-            Log.w(TAG, "Failed to measure " + path);
-        } finally {
-            mContext.unbindService(this);
+            return size;
+        } catch (IOException e) {
+            Log.w(TAG, "Failed to measure", e);
+            return -1L;
         }
-
-        return -1L;
     }
 
     @Override
@@ -116,16 +110,4 @@ public abstract class MigrateEstimateTask extends AsyncTask<Void, Void, Long> im
     }
 
     public abstract void onPostExecute(String size, String time);
-
-    @Override
-    public void onServiceConnected(ComponentName name, IBinder service) {
-        mService = IMediaContainerService.Stub.asInterface(service);
-        mConnected.countDown();
-    }
-
-    @Override
-    public void onServiceDisconnected(ComponentName name) {
-        // Ignored; we leave service in place for the background thread to
-        // run into DeadObjectException
-    }
 }