blob: 4b2241294021695a43c753902a4b4188307b86de [file] [log] [blame]
Jeff Sharkeye77f0682015-04-29 11:24:57 -07001/*
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
Jeff Sharkeyefe4b482017-07-06 11:29:24 -060019import android.app.usage.ExternalStorageStats;
20import android.app.usage.StorageStatsManager;
Jeff Sharkeye77f0682015-04-29 11:24:57 -070021import android.content.Context;
22import android.content.Intent;
Jeff Sharkeyefe4b482017-07-06 11:29:24 -060023import android.content.pm.UserInfo;
Jeff Sharkeye77f0682015-04-29 11:24:57 -070024import android.os.AsyncTask;
Jeff Sharkeye77f0682015-04-29 11:24:57 -070025import android.os.UserHandle;
Jeff Sharkeyefe4b482017-07-06 11:29:24 -060026import android.os.UserManager;
Jeff Sharkeye77f0682015-04-29 11:24:57 -070027import android.os.storage.StorageManager;
28import android.os.storage.VolumeInfo;
Jeff Sharkeye77f0682015-04-29 11:24:57 -070029import android.text.format.DateUtils;
30import android.text.format.Formatter;
Junyu Lai25e26ea2022-01-10 12:30:16 +000031import android.util.DataUnit;
Bonian Chen203f0fe2021-03-16 17:39:43 +080032import android.util.Log;
Jeff Sharkeye77f0682015-04-29 11:24:57 -070033
Jeff Sharkeyefe4b482017-07-06 11:29:24 -060034import java.io.IOException;
35import java.util.UUID;
Jeff Sharkeye77f0682015-04-29 11:24:57 -070036
Jeff Sharkeyefe4b482017-07-06 11:29:24 -060037public abstract class MigrateEstimateTask extends AsyncTask<Void, Void, Long> {
Arc Wang192fd242021-04-14 11:53:46 +080038 private static final String TAG = "MigrateEstimateTask";
39
Jeff Sharkeye77f0682015-04-29 11:24:57 -070040 private static final String EXTRA_SIZE_BYTES = "size_bytes";
41
Jeff Sharkeye77f0682015-04-29 11:24:57 -070042 /**
43 * Assume roughly a Class 10 card.
44 */
Junyu Lai25e26ea2022-01-10 12:30:16 +000045 private static final long SPEED_ESTIMATE_BPS = DataUnit.MEBIBYTES.toBytes(10);
Jeff Sharkeye77f0682015-04-29 11:24:57 -070046
47 private final Context mContext;
Jeff Sharkeye77f0682015-04-29 11:24:57 -070048
49 private long mSizeBytes = -1;
Jeff Sharkeye77f0682015-04-29 11:24:57 -070050
51 public MigrateEstimateTask(Context context) {
52 mContext = context;
Jeff Sharkeye77f0682015-04-29 11:24:57 -070053 }
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
Jeff Sharkeyefe4b482017-07-06 11:29:24 -060069 final UserManager user = mContext.getSystemService(UserManager.class);
70 final StorageManager storage = mContext.getSystemService(StorageManager.class);
71 final StorageStatsManager stats = mContext.getSystemService(StorageStatsManager.class);
72
Jeff Sharkeye77f0682015-04-29 11:24:57 -070073 final VolumeInfo privateVol = mContext.getPackageManager().getPrimaryStorageCurrentVolume();
Jeff Sharkeyefe4b482017-07-06 11:29:24 -060074 final VolumeInfo emulatedVol = storage.findEmulatedForPrivate(privateVol);
Jeff Sharkeye77f0682015-04-29 11:24:57 -070075
Jeff Sharkey6a0082b2015-07-05 14:17:40 -070076 if (emulatedVol == null) {
77 Log.w(TAG, "Failed to find current primary storage");
78 return -1L;
79 }
80
Jeff Sharkeye77f0682015-04-29 11:24:57 -070081 try {
Jeff Sharkeyefe4b482017-07-06 11:29:24 -060082 final UUID emulatedUuid = storage.getUuidForPath(emulatedVol.getPath());
83 Log.d(TAG, "Measuring size of " + emulatedUuid);
Jeff Sharkeye77f0682015-04-29 11:24:57 -070084
Jeff Sharkeyefe4b482017-07-06 11:29:24 -060085 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 }
Jeff Sharkeye77f0682015-04-29 11:24:57 -070099 }
100
101 @Override
102 protected void onPostExecute(Long result) {
103 mSizeBytes = result;
Xiaohui Chen538f6de2015-09-21 10:12:38 -0700104 long timeMillis = (mSizeBytes * DateUtils.SECOND_IN_MILLIS) / SPEED_ESTIMATE_BPS;
105 timeMillis = Math.max(timeMillis, DateUtils.SECOND_IN_MILLIS);
Jeff Sharkeye77f0682015-04-29 11:24:57 -0700106
107 final String size = Formatter.formatFileSize(mContext, mSizeBytes);
Xiaohui Chen538f6de2015-09-21 10:12:38 -0700108 final String time = DateUtils.formatDuration(timeMillis).toString();
Jeff Sharkeye77f0682015-04-29 11:24:57 -0700109 onPostExecute(size, time);
110 }
111
112 public abstract void onPostExecute(String size, String time);
Jeff Sharkeye77f0682015-04-29 11:24:57 -0700113}