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 
17 package com.android.usbtuner.tvinput;
18 
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.media.tv.TvContract;
22 import android.os.Environment;
23 import android.util.Log;
24 
25 import com.android.usbtuner.exoplayer.cache.CacheManager;
26 import com.android.usbtuner.exoplayer.cache.TrickplayStorageManager;
27 import com.android.usbtuner.util.SystemPropertiesProxy;
28 
29 import java.io.File;
30 
31 /**
32  * {@link UsbTunerTvInputService} serves TV channels coming from a usb tuner device.
33  */
34 public class UsbTunerTvInputService extends BaseTunerTvInputService {
35     private static final String TAG = "UsbTunerTvInputService";
36     private static final boolean DEBUG = false;
37 
38 
39     private static final String MAX_CACHE_SIZE_KEY = "usbtuner.cachesize_mbytes";
40     private static final int MAX_CACHE_SIZE_DEF = 2 * 1024;  // 2GB
41     private static final int MIN_CACHE_SIZE_DEF = 256;  // 256MB
42 
43     @Override
createCacheManager()44     protected CacheManager createCacheManager() {
45         int maxCacheSizeMb = SystemPropertiesProxy.getInt(MAX_CACHE_SIZE_KEY, MAX_CACHE_SIZE_DEF);
46         if (maxCacheSizeMb >= MIN_CACHE_SIZE_DEF) {
47             boolean useExternalStorage = Environment.MEDIA_MOUNTED.equals(
48                     Environment.getExternalStorageState()) &&
49                     Environment.isExternalStorageRemovable();
50             if (DEBUG) Log.d(TAG, "useExternalStorage for trickplay: " + useExternalStorage);
51             boolean allowToUseInternalStorage = true;
52             if (useExternalStorage || allowToUseInternalStorage) {
53                 File baseDir = useExternalStorage ? getExternalCacheDir() : getCacheDir();
54                 return new CacheManager(
55                         new TrickplayStorageManager(getApplicationContext(), baseDir,
56                                 1024L * 1024 * maxCacheSizeMb));
57             }
58         }
59         return null;
60     }
61 
getInputId(Context context)62     public static String getInputId(Context context) {
63         return TvContract.buildInputId(new ComponentName(context, UsbTunerTvInputService.class));
64     }
65 }
66