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 
19 import static com.android.settings.deviceinfo.StorageSettings.TAG;
20 
21 import android.app.usage.ExternalStorageStats;
22 import android.app.usage.StorageStatsManager;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.pm.UserInfo;
26 import android.net.TrafficStats;
27 import android.os.AsyncTask;
28 import android.os.UserHandle;
29 import android.os.UserManager;
30 import android.os.storage.StorageManager;
31 import android.os.storage.VolumeInfo;
32 import android.util.Log;
33 import android.text.format.DateUtils;
34 import android.text.format.Formatter;
35 
36 import java.io.IOException;
37 import java.util.UUID;
38 
39 public 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 
MigrateEstimateTask(Context context)51     public MigrateEstimateTask(Context context) {
52         mContext = context;
53     }
54 
copyFrom(Intent intent)55     public void copyFrom(Intent intent) {
56         mSizeBytes = intent.getLongExtra(EXTRA_SIZE_BYTES, -1);
57     }
58 
copyTo(Intent intent)59     public void copyTo(Intent intent) {
60         intent.putExtra(EXTRA_SIZE_BYTES, mSizeBytes);
61     }
62 
63     @Override
doInBackground(Void... params)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
onPostExecute(Long result)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 
onPostExecute(String size, String time)112     public abstract void onPostExecute(String size, String time);
113 }
114