1 /*
2  * Copyright (C) 2017 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.cts.storageapp;
18 
19 import static com.android.cts.storageapp.Utils.TAG;
20 import static com.android.cts.storageapp.Utils.makeUniqueFile;
21 import static com.android.cts.storageapp.Utils.useFallocate;
22 import static com.android.cts.storageapp.Utils.useWrite;
23 
24 import android.app.Activity;
25 import android.content.BroadcastReceiver;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.os.Bundle;
29 import android.os.storage.StorageManager;
30 import android.system.Os;
31 import android.util.Log;
32 
33 import java.io.File;
34 import java.util.Objects;
35 import java.util.UUID;
36 
37 public class UtilsReceiver extends BroadcastReceiver {
38     public static final String EXTRA_FRACTION = "fraction";
39     public static final String EXTRA_BYTES = "bytes";
40     public static final String EXTRA_TIME = "time";
41 
42     @Override
onReceive(Context context, Intent intent)43     public void onReceive(Context context, Intent intent) {
44         final Bundle res = doAllocation(context, intent.getExtras());
45         if (res != null) {
46             setResultCode(Activity.RESULT_OK);
47             setResultExtras(res);
48         } else {
49             setResultCode(Activity.RESULT_CANCELED);
50         }
51     }
52 
doAllocation(Context context, Bundle extras)53     public static Bundle doAllocation(Context context, Bundle extras) {
54         final StorageManager sm = context.getSystemService(StorageManager.class);
55 
56         long allocated = 0;
57         try {
58             // When shared storage is backed by internal, then pivot our cache
59             // files between the two locations to ensure clearing logic works.
60             final File intDir = context.getCacheDir();
61             final File extDir = context.getExternalCacheDir();
62             final UUID intUuid = sm.getUuidForPath(intDir);
63             final UUID extUuid = sm.getUuidForPath(extDir);
64 
65             Log.d(TAG, "Found internal " + intUuid + " and external " + extUuid);
66             final boolean doPivot = Objects.equals(intUuid, extUuid);
67 
68             final double fraction = extras.getDouble(EXTRA_FRACTION, 0);
69             final long quota = sm.getCacheQuotaBytes(intUuid);
70             final long bytes = (long) (quota * fraction);
71             final long time = extras.getLong(EXTRA_TIME, System.currentTimeMillis());
72 
73             int i = 0;
74             while (allocated < bytes) {
75                 final File target;
76                 if (doPivot) {
77                     target = (i++ % 2) == 0 ? intDir : extDir;
78                 } else {
79                     target = intDir;
80                 }
81 
82                 final File f = makeUniqueFile(target);
83                 final long size = 1024 * 1024;
84                 if (target == intDir) {
85                     useFallocate(f, size);
86                 } else {
87                     useWrite(f, size);
88                 }
89                 f.setLastModified(time);
90                 allocated += Os.stat(f.getAbsolutePath()).st_blocks * 512;
91             }
92 
93             Log.d(TAG, "Quota " + quota + ", target " + bytes + ", allocated " + allocated);
94 
95             final Bundle res = new Bundle();
96             res.putLong(EXTRA_BYTES, allocated);
97             return res;
98         } catch (Exception e) {
99             Log.e(TAG, "Failed to allocate cache files", e);
100             return null;
101         }
102     }
103 }
104