1 /* 2 * Copyright (C) 2022 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.storage; 18 19 import android.content.Context; 20 import android.content.SharedPreferences; 21 22 /** 23 * A utility class to cache and restore the storage size information. 24 */ 25 public class StorageCacheHelper { 26 27 private static final String SHARED_PREFERENCE_NAME = "StorageCache"; 28 private static final String TOTAL_SIZE_KEY = "total_size_key"; 29 private static final String TOTAL_USED_SIZE_KEY = "total_used_size_key"; 30 private static final String IMAGES_SIZE_KEY = "images_size_key"; 31 private static final String VIDEOS_SIZE_KEY = "videos_size_key"; 32 private static final String AUDIO_SIZE_KEY = "audio_size_key"; 33 private static final String APPS_SIZE_KEY = "apps_size_key"; 34 private static final String GAMES_SIZE_KEY = "games_size_key"; 35 private static final String DOCUMENTS_SIZE_KEY = "documents_size_key"; 36 private static final String OTHER_SIZE_KEY = "other_size_key"; 37 private static final String TRASH_SIZE_KEY = "trash_size_key"; 38 private static final String SYSTEM_SIZE_KEY = "system_size_key"; 39 private static final String TEMPORARY_FILES_SIZE_KEY = "temporary_files_size_key"; 40 private static final String USED_SIZE_KEY = "used_size_key"; 41 42 private final SharedPreferences mSharedPreferences; 43 StorageCacheHelper(Context context, int userId)44 public StorageCacheHelper(Context context, int userId) { 45 String sharedPrefName = SHARED_PREFERENCE_NAME + userId; 46 mSharedPreferences = context.getSharedPreferences(sharedPrefName, Context.MODE_PRIVATE); 47 } 48 49 /** 50 * Returns true if there's a cached size info. 51 */ hasCachedSizeInfo()52 public boolean hasCachedSizeInfo() { 53 return mSharedPreferences.getAll().size() > 0; 54 } 55 56 /** 57 * Cache the size info 58 * @param data a data about the file size info. 59 */ cacheSizeInfo(StorageCache data)60 public void cacheSizeInfo(StorageCache data) { 61 mSharedPreferences 62 .edit() 63 .putLong(IMAGES_SIZE_KEY, data.imagesSize) 64 .putLong(VIDEOS_SIZE_KEY, data.videosSize) 65 .putLong(AUDIO_SIZE_KEY, data.audioSize) 66 .putLong(APPS_SIZE_KEY, data.allAppsExceptGamesSize) 67 .putLong(GAMES_SIZE_KEY, data.gamesSize) 68 .putLong(DOCUMENTS_SIZE_KEY, data.documentsSize) 69 .putLong(OTHER_SIZE_KEY, data.otherSize) 70 .putLong(TRASH_SIZE_KEY, data.trashSize) 71 .putLong(SYSTEM_SIZE_KEY, data.systemSize) 72 .putLong(TEMPORARY_FILES_SIZE_KEY, data.temporaryFilesSize) 73 .apply(); 74 } 75 76 /** 77 * Cache total size and total used size 78 */ cacheTotalSizeAndTotalUsedSize(long totalSize, long totalUsedSize)79 public void cacheTotalSizeAndTotalUsedSize(long totalSize, long totalUsedSize) { 80 mSharedPreferences 81 .edit() 82 .putLong(TOTAL_SIZE_KEY, totalSize) 83 .putLong(TOTAL_USED_SIZE_KEY, totalUsedSize) 84 .apply(); 85 } 86 87 /** 88 * Cache used size info when a user is treated as a secondary user. 89 */ cacheUsedSize(long usedSize)90 public void cacheUsedSize(long usedSize) { 91 mSharedPreferences.edit().putLong(USED_SIZE_KEY, usedSize).apply(); 92 } 93 94 /** 95 * Returns used size for secondary user. 96 */ retrieveUsedSize()97 public long retrieveUsedSize() { 98 return mSharedPreferences.getLong(USED_SIZE_KEY, 0); 99 } 100 101 /** 102 * Returns a cached data about all file size information. 103 */ retrieveCachedSize()104 public StorageCache retrieveCachedSize() { 105 StorageCache result = new StorageCache(); 106 result.totalSize = mSharedPreferences.getLong(TOTAL_SIZE_KEY, 0); 107 result.totalUsedSize = mSharedPreferences.getLong(TOTAL_USED_SIZE_KEY, 0); 108 result.imagesSize = mSharedPreferences.getLong(IMAGES_SIZE_KEY, 0); 109 result.videosSize = mSharedPreferences.getLong(VIDEOS_SIZE_KEY, 0); 110 result.audioSize = mSharedPreferences.getLong(AUDIO_SIZE_KEY, 0); 111 result.allAppsExceptGamesSize = mSharedPreferences.getLong(APPS_SIZE_KEY, 0); 112 result.gamesSize = mSharedPreferences.getLong(GAMES_SIZE_KEY, 0); 113 result.documentsSize = mSharedPreferences.getLong(DOCUMENTS_SIZE_KEY, 0); 114 result.otherSize = mSharedPreferences.getLong(OTHER_SIZE_KEY, 0); 115 result.trashSize = mSharedPreferences.getLong(TRASH_SIZE_KEY, 0); 116 result.systemSize = mSharedPreferences.getLong(SYSTEM_SIZE_KEY, 0); 117 result.temporaryFilesSize = mSharedPreferences.getLong(TEMPORARY_FILES_SIZE_KEY, 0); 118 return result; 119 } 120 121 /** 122 * All the cached data about the file size information. 123 */ 124 public static class StorageCache { 125 public long totalSize; 126 public long totalUsedSize; 127 public long gamesSize; 128 public long allAppsExceptGamesSize; 129 public long audioSize; 130 public long imagesSize; 131 public long videosSize; 132 public long documentsSize; 133 public long otherSize; 134 public long trashSize; 135 public long systemSize; 136 public long temporaryFilesSize; 137 } 138 } 139