Change storage migration to use quota APIs.

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 fc522c677d)

Bug: 36056324
Test: builds, boots
Merged-In: Icdf774cff520a4b7ca6ec210b34a1c5ff85f8110
Change-Id: Icdf774cff520a4b7ca6ec210b34a1c5ff85f8110
This commit is contained in:
Jeff Sharkey
2017-07-06 11:29:24 -06:00
committed by Andreas Gampe
parent 1e5cd4c3df
commit efe4b48d9b

View File

@@ -16,51 +16,40 @@
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.io.IOException;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static com.android.settings.deviceinfo.StorageSettings.TAG;
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);
}
} catch (InterruptedException | RemoteException e) {
Log.w(TAG, "Failed to measure " + path);
} finally {
mContext.unbindService(this);
}
final UUID emulatedUuid = storage.getUuidForPath(emulatedVol.getPath());
Log.d(TAG, "Measuring size of " + emulatedUuid);
return -1L;
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();
}
}
return size;
} catch (IOException e) {
Log.w(TAG, "Failed to measure", e);
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
}
}