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_AND_OTHER_SIZE_KEY = "documents_and_other_size_key"; 36 private static final String TRASH_SIZE_KEY = "trash_size_key"; 37 private static final String SYSTEM_SIZE_KEY = "system_size_key"; 38 private static final String USED_SIZE_KEY = "used_size_key"; 39 40 private final SharedPreferences mSharedPreferences; 41 StorageCacheHelper(Context context, int userId)42 public StorageCacheHelper(Context context, int userId) { 43 String sharedPrefName = SHARED_PREFERENCE_NAME + userId; 44 mSharedPreferences = context.getSharedPreferences(sharedPrefName, Context.MODE_PRIVATE); 45 } 46 47 /** 48 * Returns true if there's a cached size info. 49 */ hasCachedSizeInfo()50 public boolean hasCachedSizeInfo() { 51 return mSharedPreferences.getAll().size() > 0; 52 } 53 54 /** 55 * Cache the size info 56 * @param data a data about the file size info. 57 */ cacheSizeInfo(StorageCache data)58 public void cacheSizeInfo(StorageCache data) { 59 mSharedPreferences 60 .edit() 61 .putLong(IMAGES_SIZE_KEY, data.imagesSize) 62 .putLong(VIDEOS_SIZE_KEY, data.videosSize) 63 .putLong(AUDIO_SIZE_KEY, data.audioSize) 64 .putLong(APPS_SIZE_KEY, data.allAppsExceptGamesSize) 65 .putLong(GAMES_SIZE_KEY, data.gamesSize) 66 .putLong(DOCUMENTS_AND_OTHER_SIZE_KEY, data.documentsAndOtherSize) 67 .putLong(TRASH_SIZE_KEY, data.trashSize) 68 .putLong(SYSTEM_SIZE_KEY, data.systemSize) 69 .apply(); 70 } 71 72 /** 73 * Cache total size and total used size 74 */ cacheTotalSizeAndTotalUsedSize(long totalSize, long totalUsedSize)75 public void cacheTotalSizeAndTotalUsedSize(long totalSize, long totalUsedSize) { 76 mSharedPreferences 77 .edit() 78 .putLong(TOTAL_SIZE_KEY, totalSize) 79 .putLong(TOTAL_USED_SIZE_KEY, totalUsedSize) 80 .apply(); 81 } 82 83 /** 84 * Cache used size info when a user is treated as a secondary user. 85 */ cacheUsedSize(long usedSize)86 public void cacheUsedSize(long usedSize) { 87 mSharedPreferences.edit().putLong(USED_SIZE_KEY, usedSize).apply(); 88 } 89 90 /** 91 * Returns used size for secondary user. 92 */ retrieveUsedSize()93 public long retrieveUsedSize() { 94 return mSharedPreferences.getLong(USED_SIZE_KEY, 0); 95 } 96 97 /** 98 * Returns a cached data about all file size information. 99 */ retrieveCachedSize()100 public StorageCache retrieveCachedSize() { 101 StorageCache result = new StorageCache(); 102 result.totalSize = mSharedPreferences.getLong(TOTAL_SIZE_KEY, 0); 103 result.totalUsedSize = mSharedPreferences.getLong(TOTAL_USED_SIZE_KEY, 0); 104 result.imagesSize = mSharedPreferences.getLong(IMAGES_SIZE_KEY, 0); 105 result.videosSize = mSharedPreferences.getLong(VIDEOS_SIZE_KEY, 0); 106 result.audioSize = mSharedPreferences.getLong(AUDIO_SIZE_KEY, 0); 107 result.allAppsExceptGamesSize = mSharedPreferences.getLong(APPS_SIZE_KEY, 0); 108 result.gamesSize = mSharedPreferences.getLong(GAMES_SIZE_KEY, 0); 109 result.documentsAndOtherSize = mSharedPreferences.getLong(DOCUMENTS_AND_OTHER_SIZE_KEY, 0); 110 result.trashSize = mSharedPreferences.getLong(TRASH_SIZE_KEY, 0); 111 result.systemSize = mSharedPreferences.getLong(SYSTEM_SIZE_KEY, 0); 112 return result; 113 } 114 115 /** 116 * All the cached data about the file size information. 117 */ 118 public static class StorageCache { 119 public long totalSize; 120 public long totalUsedSize; 121 public long gamesSize; 122 public long allAppsExceptGamesSize; 123 public long audioSize; 124 public long imagesSize; 125 public long videosSize; 126 public long documentsAndOtherSize; 127 public long trashSize; 128 public long systemSize; 129 } 130 } 131