Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a0afdb0c authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Change storage migration to use quota APIs."

parents 08217cbd efe4b48d
Loading
Loading
Loading
Loading
+29 −47
Original line number Diff line number Diff line
@@ -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.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,32 +66,37 @@ 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;
        }
    }

    @Override
    protected void onPostExecute(Long result) {
@@ -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
    }
}