| Jeff Sharkey | e77f068 | 2015-04-29 11:24:57 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.settings.deviceinfo; |
| 18 | |
| Jeff Sharkey | efe4b48 | 2017-07-06 11:29:24 -0600 | [diff] [blame] | 19 | import android.app.usage.ExternalStorageStats; |
| 20 | import android.app.usage.StorageStatsManager; |
| Jeff Sharkey | e77f068 | 2015-04-29 11:24:57 -0700 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| Jeff Sharkey | efe4b48 | 2017-07-06 11:29:24 -0600 | [diff] [blame] | 23 | import android.content.pm.UserInfo; |
| Jeff Sharkey | e77f068 | 2015-04-29 11:24:57 -0700 | [diff] [blame] | 24 | import android.os.AsyncTask; |
| Jeff Sharkey | e77f068 | 2015-04-29 11:24:57 -0700 | [diff] [blame] | 25 | import android.os.UserHandle; |
| Jeff Sharkey | efe4b48 | 2017-07-06 11:29:24 -0600 | [diff] [blame] | 26 | import android.os.UserManager; |
| Jeff Sharkey | e77f068 | 2015-04-29 11:24:57 -0700 | [diff] [blame] | 27 | import android.os.storage.StorageManager; |
| 28 | import android.os.storage.VolumeInfo; |
| Jeff Sharkey | e77f068 | 2015-04-29 11:24:57 -0700 | [diff] [blame] | 29 | import android.text.format.DateUtils; |
| 30 | import android.text.format.Formatter; |
| Junyu Lai | 25e26ea | 2022-01-10 12:30:16 +0000 | [diff] [blame] | 31 | import android.util.DataUnit; |
| Bonian Chen | 203f0fe | 2021-03-16 17:39:43 +0800 | [diff] [blame] | 32 | import android.util.Log; |
| Jeff Sharkey | e77f068 | 2015-04-29 11:24:57 -0700 | [diff] [blame] | 33 | |
| Jeff Sharkey | efe4b48 | 2017-07-06 11:29:24 -0600 | [diff] [blame] | 34 | import java.io.IOException; |
| 35 | import java.util.UUID; |
| Jeff Sharkey | e77f068 | 2015-04-29 11:24:57 -0700 | [diff] [blame] | 36 | |
| Jeff Sharkey | efe4b48 | 2017-07-06 11:29:24 -0600 | [diff] [blame] | 37 | public abstract class MigrateEstimateTask extends AsyncTask<Void, Void, Long> { |
| Arc Wang | 192fd24 | 2021-04-14 11:53:46 +0800 | [diff] [blame] | 38 | private static final String TAG = "MigrateEstimateTask"; |
| 39 | |
| Jeff Sharkey | e77f068 | 2015-04-29 11:24:57 -0700 | [diff] [blame] | 40 | private static final String EXTRA_SIZE_BYTES = "size_bytes"; |
| 41 | |
| Jeff Sharkey | e77f068 | 2015-04-29 11:24:57 -0700 | [diff] [blame] | 42 | /** |
| 43 | * Assume roughly a Class 10 card. |
| 44 | */ |
| Junyu Lai | 25e26ea | 2022-01-10 12:30:16 +0000 | [diff] [blame] | 45 | private static final long SPEED_ESTIMATE_BPS = DataUnit.MEBIBYTES.toBytes(10); |
| Jeff Sharkey | e77f068 | 2015-04-29 11:24:57 -0700 | [diff] [blame] | 46 | |
| 47 | private final Context mContext; |
| Jeff Sharkey | e77f068 | 2015-04-29 11:24:57 -0700 | [diff] [blame] | 48 | |
| 49 | private long mSizeBytes = -1; |
| Jeff Sharkey | e77f068 | 2015-04-29 11:24:57 -0700 | [diff] [blame] | 50 | |
| 51 | public MigrateEstimateTask(Context context) { |
| 52 | mContext = context; |
| Jeff Sharkey | e77f068 | 2015-04-29 11:24:57 -0700 | [diff] [blame] | 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 | |
| Jeff Sharkey | efe4b48 | 2017-07-06 11:29:24 -0600 | [diff] [blame] | 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 | |
| Jeff Sharkey | e77f068 | 2015-04-29 11:24:57 -0700 | [diff] [blame] | 73 | final VolumeInfo privateVol = mContext.getPackageManager().getPrimaryStorageCurrentVolume(); |
| Jeff Sharkey | efe4b48 | 2017-07-06 11:29:24 -0600 | [diff] [blame] | 74 | final VolumeInfo emulatedVol = storage.findEmulatedForPrivate(privateVol); |
| Jeff Sharkey | e77f068 | 2015-04-29 11:24:57 -0700 | [diff] [blame] | 75 | |
| Jeff Sharkey | 6a0082b | 2015-07-05 14:17:40 -0700 | [diff] [blame] | 76 | if (emulatedVol == null) { |
| 77 | Log.w(TAG, "Failed to find current primary storage"); |
| 78 | return -1L; |
| 79 | } |
| 80 | |
| Jeff Sharkey | e77f068 | 2015-04-29 11:24:57 -0700 | [diff] [blame] | 81 | try { |
| Jeff Sharkey | efe4b48 | 2017-07-06 11:29:24 -0600 | [diff] [blame] | 82 | final UUID emulatedUuid = storage.getUuidForPath(emulatedVol.getPath()); |
| 83 | Log.d(TAG, "Measuring size of " + emulatedUuid); |
| Jeff Sharkey | e77f068 | 2015-04-29 11:24:57 -0700 | [diff] [blame] | 84 | |
| Jeff Sharkey | efe4b48 | 2017-07-06 11:29:24 -0600 | [diff] [blame] | 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 | } |
| Jeff Sharkey | e77f068 | 2015-04-29 11:24:57 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Override |
| 102 | protected void onPostExecute(Long result) { |
| 103 | mSizeBytes = result; |
| Xiaohui Chen | 538f6de | 2015-09-21 10:12:38 -0700 | [diff] [blame] | 104 | long timeMillis = (mSizeBytes * DateUtils.SECOND_IN_MILLIS) / SPEED_ESTIMATE_BPS; |
| 105 | timeMillis = Math.max(timeMillis, DateUtils.SECOND_IN_MILLIS); |
| Jeff Sharkey | e77f068 | 2015-04-29 11:24:57 -0700 | [diff] [blame] | 106 | |
| 107 | final String size = Formatter.formatFileSize(mContext, mSizeBytes); |
| Xiaohui Chen | 538f6de | 2015-09-21 10:12:38 -0700 | [diff] [blame] | 108 | final String time = DateUtils.formatDuration(timeMillis).toString(); |
| Jeff Sharkey | e77f068 | 2015-04-29 11:24:57 -0700 | [diff] [blame] | 109 | onPostExecute(size, time); |
| 110 | } |
| 111 | |
| 112 | public abstract void onPostExecute(String size, String time); |
| Jeff Sharkey | e77f068 | 2015-04-29 11:24:57 -0700 | [diff] [blame] | 113 | } |