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 17package com.android.usbtuner.exoplayer.cache; 18 19import android.content.Context; 20import android.media.MediaFormat; 21import android.provider.Settings; 22import android.util.Pair; 23 24import java.io.File; 25import java.util.ArrayList; 26import java.util.SortedMap; 27 28/** 29 * Manages Trickplay storage. 30 */ 31public class TrickplayStorageManager implements CacheManager.StorageManager { 32 private static final String CACHE_DIR = "timeshift"; 33 34 // Copied from android.provider.Settings.Global (hidden fields) 35 private static final String 36 SYS_STORAGE_THRESHOLD_PERCENTAGE = "sys_storage_threshold_percentage"; 37 private static final String 38 SYS_STORAGE_THRESHOLD_MAX_BYTES = "sys_storage_threshold_max_bytes"; 39 40 // Copied from android.os.StorageManager 41 private static final int DEFAULT_THRESHOLD_PERCENTAGE = 10; 42 private static final long DEFAULT_THRESHOLD_MAX_BYTES = 500L * 1024 * 1024; 43 44 private final File mCacheDir; 45 private final long mMaxCacheSize; 46 private final long mStorageBufferBytes; 47 48 private static long getStorageBufferBytes(Context context, File path) { 49 long lowPercentage = Settings.Global.getInt(context.getContentResolver(), 50 SYS_STORAGE_THRESHOLD_PERCENTAGE, DEFAULT_THRESHOLD_PERCENTAGE); 51 long lowBytes = path.getTotalSpace() * lowPercentage / 100; 52 long maxLowBytes = Settings.Global.getLong(context.getContentResolver(), 53 SYS_STORAGE_THRESHOLD_MAX_BYTES, DEFAULT_THRESHOLD_MAX_BYTES); 54 return Math.min(lowBytes, maxLowBytes); 55 } 56 57 public TrickplayStorageManager(Context context, File baseDir, long maxCacheSize) { 58 mCacheDir = new File(baseDir, CACHE_DIR); 59 mCacheDir.mkdirs(); 60 mMaxCacheSize = maxCacheSize; 61 clearStorage(); 62 mStorageBufferBytes = getStorageBufferBytes(context, mCacheDir); 63 } 64 65 @Override 66 public void clearStorage() { 67 for (File file : mCacheDir.listFiles()) { 68 file.delete(); 69 } 70 } 71 72 @Override 73 public File getCacheDir() { 74 return mCacheDir; 75 } 76 77 @Override 78 public boolean isPersistent() { 79 return false; 80 } 81 82 @Override 83 public boolean reachedStorageMax(long cacheSize, long pendingDelete) { 84 return cacheSize - pendingDelete > mMaxCacheSize; 85 } 86 87 @Override 88 public boolean hasEnoughBuffer(long pendingDelete) { 89 return mCacheDir.getUsableSpace() + pendingDelete >= mStorageBufferBytes; 90 } 91 92 @Override 93 public Pair<String, MediaFormat> readTrackInfoFile(boolean isAudio) { 94 return null; 95 } 96 97 @Override 98 public ArrayList<Long> readIndexFile(String trackId) { 99 return null; 100 } 101 102 @Override 103 public void writeTrackInfoFile(String trackId, MediaFormat format, boolean isAudio) { 104 } 105 106 @Override 107 public void writeIndexFile(String trackName, SortedMap<Long, SampleCache> index) { 108 } 109 110} 111