1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings.deviceinfo;
18
19import static com.android.settings.deviceinfo.StorageSettings.TAG;
20
21import android.app.usage.ExternalStorageStats;
22import android.app.usage.StorageStatsManager;
23import android.content.Context;
24import android.content.Intent;
25import android.content.pm.UserInfo;
26import android.net.TrafficStats;
27import android.os.AsyncTask;
28import android.os.UserHandle;
29import android.os.UserManager;
30import android.os.storage.StorageManager;
31import android.os.storage.VolumeInfo;
32import android.telecom.Log;
33import android.text.format.DateUtils;
34import android.text.format.Formatter;
35
36import java.io.IOException;
37import java.util.UUID;
38
39public abstract class MigrateEstimateTask extends AsyncTask<Void, Void, Long> {
40    private static final String EXTRA_SIZE_BYTES = "size_bytes";
41
42    /**
43     * Assume roughly a Class 10 card.
44     */
45    private static final long SPEED_ESTIMATE_BPS = 10 * TrafficStats.MB_IN_BYTES;
46
47    private final Context mContext;
48
49    private long mSizeBytes = -1;
50
51    public MigrateEstimateTask(Context context) {
52        mContext = context;
53    }
54
55    public void copyFrom(Intent intent) {
56        mSizeBytes = intent.getLongExtra(EXTRA_SIZE_BYTES, -1);
57    }
58
59    public void copyTo(Intent intent) {
60        intent.putExtra(EXTRA_SIZE_BYTES, mSizeBytes);
61    }
62
63    @Override
64    protected Long doInBackground(Void... params) {
65        if (mSizeBytes != -1) {
66            return mSizeBytes;
67        }
68
69        final UserManager user = mContext.getSystemService(UserManager.class);
70        final StorageManager storage = mContext.getSystemService(StorageManager.class);
71        final StorageStatsManager stats = mContext.getSystemService(StorageStatsManager.class);
72
73        final VolumeInfo privateVol = mContext.getPackageManager().getPrimaryStorageCurrentVolume();
74        final VolumeInfo emulatedVol = storage.findEmulatedForPrivate(privateVol);
75
76        if (emulatedVol == null) {
77            Log.w(TAG, "Failed to find current primary storage");
78            return -1L;
79        }
80
81        try {
82            final UUID emulatedUuid = storage.getUuidForPath(emulatedVol.getPath());
83            Log.d(TAG, "Measuring size of " + emulatedUuid);
84
85            long size = 0;
86            for (UserInfo u : user.getUsers()) {
87                final ExternalStorageStats s = stats.queryExternalStatsForUser(emulatedUuid,
88                        UserHandle.of(u.id));
89                size += s.getTotalBytes();
90                if (u.id == UserHandle.USER_SYSTEM) {
91                    size += s.getObbBytes();
92                }
93            }
94            return size;
95        } catch (IOException e) {
96            Log.w(TAG, "Failed to measure", e);
97            return -1L;
98        }
99    }
100
101    @Override
102    protected void onPostExecute(Long result) {
103        mSizeBytes = result;
104        long timeMillis = (mSizeBytes * DateUtils.SECOND_IN_MILLIS) / SPEED_ESTIMATE_BPS;
105        timeMillis = Math.max(timeMillis, DateUtils.SECOND_IN_MILLIS);
106
107        final String size = Formatter.formatFileSize(mContext, mSizeBytes);
108        final String time = DateUtils.formatDuration(timeMillis).toString();
109        onPostExecute(size, time);
110    }
111
112    public abstract void onPostExecute(String size, String time);
113}
114