Home | History | Annotate | Download | only in tvinput
      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.Intent;
     20 import android.content.pm.PackageManager;
     21 import android.content.pm.ResolveInfo;
     22 import android.media.tv.TvInputHardwareInfo;
     23 import android.media.tv.TvInputInfo;
     24 import android.os.Environment;
     25 import android.util.Log;
     26 
     27 import com.android.usbtuner.exoplayer.cache.CacheManager;
     28 import com.android.usbtuner.exoplayer.cache.TrickplayStorageManager;
     29 import com.android.usbtuner.util.SystemPropertiesProxy;
     30 import com.android.usbtuner.util.TisConfiguration;
     31 import org.xmlpull.v1.XmlPullParserException;
     32 
     33 import java.io.File;
     34 import java.io.IOException;
     35 
     36 /**
     37  * {@link InternalTunerTvInputService} serves TV channels coming from a internal tuner device.
     38  */
     39 public class InternalTunerTvInputService extends BaseTunerTvInputService {
     40     private static final String TAG = "InternalTunerTvInputService";
     41     private static final boolean DEBUG = false;
     42 
     43 
     44     private static final String MAX_CACHE_SIZE_KEY = "usbtuner.cachesize_mbytes";
     45     private static final int MAX_CACHE_SIZE_DEF = 2 * 1024;  // 2GB
     46     private static final int MIN_CACHE_SIZE_DEF = 256;  // 256MB
     47 
     48     private ResolveInfo mResolveInfo;
     49     private String mTvInputId;
     50 
     51     @Override
     52     public void onCreate() {
     53         super.onCreate();
     54         mResolveInfo = getPackageManager().resolveService(
     55                 new Intent(SERVICE_INTERFACE).setClass(this, getClass()),
     56                 PackageManager.GET_SERVICES | PackageManager.GET_META_DATA);
     57     }
     58 
     59     @Override
     60     protected CacheManager createCacheManager() {
     61         int maxCacheSizeMb = SystemPropertiesProxy.getInt(MAX_CACHE_SIZE_KEY, MAX_CACHE_SIZE_DEF);
     62         if (maxCacheSizeMb >= MIN_CACHE_SIZE_DEF) {
     63             boolean useExternalStorage = Environment.MEDIA_MOUNTED.equals(
     64                     Environment.getExternalStorageState()) &&
     65                     Environment.isExternalStorageRemovable();
     66             if (DEBUG) Log.d(TAG, "useExternalStorage for trickplay: " + useExternalStorage);
     67             boolean allowToUseInternalStorage = true;
     68             if (useExternalStorage || allowToUseInternalStorage) {
     69                 File baseDir = useExternalStorage ? getExternalCacheDir() : getCacheDir();
     70                 return new CacheManager(
     71                         new TrickplayStorageManager(getApplicationContext(), baseDir,
     72                                 1024L * 1024 * maxCacheSizeMb));
     73             }
     74         }
     75         return null;
     76     }
     77 
     78     @Override
     79     public TvInputInfo onHardwareAdded(TvInputHardwareInfo hardwareInfo) {
     80         if (DEBUG) Log.d(TAG, "onHardwareAdded: " + hardwareInfo.toString());
     81         if (mTvInputId != null) {
     82             return null;
     83         }
     84         TvInputInfo info = null;
     85         if (hardwareInfo.getType() == TvInputHardwareInfo.TV_INPUT_TYPE_TUNER &&
     86                 TisConfiguration.getTunerHwDeviceId(this) == hardwareInfo.getDeviceId()) {
     87             try {
     88                 info = TvInputInfo.createTvInputInfo(this, mResolveInfo, hardwareInfo,
     89                         "Google Tuner", null);
     90                 mTvInputId = info.getId();
     91             } catch (XmlPullParserException e) {
     92                 e.printStackTrace();
     93             } catch (IOException e) {
     94                 e.printStackTrace();
     95             }
     96         }
     97         return info;
     98     }
     99 
    100     @Override
    101     public String onHardwareRemoved(TvInputHardwareInfo hardwareInfo) {
    102         if (DEBUG) Log.d(TAG, "onHardwareRemoved: " + hardwareInfo.toString());
    103         return null;
    104     }
    105 }
    106