Home | History | Annotate | Download | only in applications
      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.settingslib.applications;
     18 
     19 import android.app.usage.StorageStats;
     20 import android.app.usage.StorageStatsManager;
     21 import android.content.Context;
     22 import android.content.pm.PackageManager;
     23 import android.os.UserHandle;
     24 import android.support.annotation.VisibleForTesting;
     25 
     26 import java.io.IOException;
     27 
     28 /**
     29  * StorageStatsSource wraps the StorageStatsManager for testability purposes.
     30  */
     31 public class StorageStatsSource {
     32     private StorageStatsManager mStorageStatsManager;
     33 
     34     public StorageStatsSource(Context context) {
     35         mStorageStatsManager = context.getSystemService(StorageStatsManager.class);
     36     }
     37 
     38     public StorageStatsSource.ExternalStorageStats getExternalStorageStats(String volumeUuid,
     39             UserHandle user) throws IOException {
     40         return new StorageStatsSource.ExternalStorageStats(
     41                 mStorageStatsManager.queryExternalStatsForUser(volumeUuid, user));
     42     }
     43 
     44     public StorageStatsSource.AppStorageStats getStatsForUid(String volumeUuid, int uid)
     45             throws IOException {
     46         return new StorageStatsSource.AppStorageStatsImpl(
     47                 mStorageStatsManager.queryStatsForUid(volumeUuid, uid));
     48     }
     49 
     50     public StorageStatsSource.AppStorageStats getStatsForPackage(
     51             String volumeUuid, String packageName, UserHandle user)
     52             throws PackageManager.NameNotFoundException, IOException {
     53         return new StorageStatsSource.AppStorageStatsImpl(
     54                 mStorageStatsManager.queryStatsForPackage(volumeUuid, packageName, user));
     55     }
     56 
     57     public long getCacheQuotaBytes(String volumeUuid, int uid) {
     58         return mStorageStatsManager.getCacheQuotaBytes(volumeUuid, uid);
     59     }
     60 
     61     /**
     62      * Static class that provides methods for querying the amount of external storage available as
     63      * well as breaking it up into several media types.
     64      */
     65     public static class ExternalStorageStats {
     66         public long totalBytes;
     67         public long audioBytes;
     68         public long videoBytes;
     69         public long imageBytes;
     70         public long appBytes;
     71 
     72         /** Convenience method for testing. */
     73         @VisibleForTesting
     74         public ExternalStorageStats(
     75                 long totalBytes, long audioBytes, long videoBytes, long imageBytes, long appBytes) {
     76             this.totalBytes = totalBytes;
     77             this.audioBytes = audioBytes;
     78             this.videoBytes = videoBytes;
     79             this.imageBytes = imageBytes;
     80             this.appBytes = appBytes;
     81         }
     82 
     83         /**
     84          * Creates an ExternalStorageStats from the system version of ExternalStorageStats. They are
     85          * identical other than the utility method created for test purposes.
     86          * @param stats The stats to copy to wrap.
     87          */
     88         public ExternalStorageStats(android.app.usage.ExternalStorageStats stats) {
     89             totalBytes = stats.getTotalBytes();
     90             audioBytes = stats.getAudioBytes();
     91             videoBytes = stats.getVideoBytes();
     92             imageBytes = stats.getImageBytes();
     93             appBytes = stats.getAppBytes();
     94         }
     95     }
     96 
     97     /**
     98      * Interface that exists to simplify testing. The platform {@link StorageStats} is too new and
     99      * robolectric cannot see it. It simply wraps a StorageStats object and forwards method calls
    100      * to the real object
    101      */
    102     public interface AppStorageStats {
    103         long getCodeBytes();
    104         long getDataBytes();
    105         long getCacheBytes();
    106         long getTotalBytes();
    107     }
    108 
    109     /**
    110      * Simple implementation of AppStorageStats that will allow you to query the StorageStats object
    111      * passed in for storage information about an app.
    112      */
    113     public static class AppStorageStatsImpl implements
    114             StorageStatsSource.AppStorageStats {
    115         private StorageStats mStats;
    116 
    117         public AppStorageStatsImpl(StorageStats stats) {
    118             mStats = stats;
    119         }
    120 
    121         public long getCodeBytes() {
    122             return mStats.getCodeBytes();
    123         }
    124 
    125         public long getDataBytes() {
    126             return mStats.getDataBytes();
    127         }
    128 
    129         public long getCacheBytes() {
    130             return mStats.getCacheBytes();
    131         }
    132 
    133         public long getTotalBytes() {
    134             return mStats.getAppBytes() + mStats.getDataBytes();
    135         }
    136     }
    137 }