• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.tv.tuner.util;
18 
19 import android.annotation.TargetApi;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.media.tv.TvInputInfo;
23 import android.media.tv.TvInputManager;
24 import android.os.Build;
25 import android.support.annotation.Nullable;
26 import android.support.v4.os.BuildCompat;
27 import android.util.Log;
28 
29 import com.android.tv.common.feature.CommonFeatures;
30 import com.android.tv.tuner.R;
31 import com.android.tv.tuner.TunerHal;
32 import com.android.tv.tuner.tvinput.TunerTvInputService;
33 
34 /**
35  * Utility class for providing tuner input info.
36  */
37 public class TunerInputInfoUtils {
38     private static final String TAG = "TunerInputInfoUtils";
39     private static final boolean DEBUG = false;
40 
41     /**
42      * Builds tuner input's info.
43      */
44     @Nullable
45     @TargetApi(Build.VERSION_CODES.N)
buildTunerInputInfo(Context context, boolean fromBuiltInTuner)46     public static TvInputInfo buildTunerInputInfo(Context context, boolean fromBuiltInTuner) {
47         int numOfDevices = TunerHal.getTunerCount(context);
48         if (numOfDevices == 0) {
49             return null;
50         }
51         TvInputInfo.Builder builder = new TvInputInfo.Builder(context, new ComponentName(context,
52                 TunerTvInputService.class));
53         if (fromBuiltInTuner) {
54             builder.setLabel(R.string.bt_app_name);
55         } else {
56             builder.setLabel(R.string.ut_app_name);
57         }
58         try {
59             return builder.setCanRecord(CommonFeatures.DVR.isEnabled(context))
60                     .setTunerCount(numOfDevices)
61                     .build();
62         } catch (NullPointerException e) {
63             // TunerTvInputService is not enabled.
64             return null;
65         }
66     }
67 
68     /**
69      * Updates tuner input's info.
70      *
71      * @param context {@link Context} instance
72      */
updateTunerInputInfo(Context context)73     public static void updateTunerInputInfo(Context context) {
74         if (BuildCompat.isAtLeastN()) {
75             if (DEBUG) Log.d(TAG, "updateTunerInputInfo()");
76             TvInputInfo info = buildTunerInputInfo(context, isBuiltInTuner(context));
77             if (info != null) {
78                 ((TvInputManager) context.getSystemService(Context.TV_INPUT_SERVICE))
79                         .updateTvInputInfo(info);
80                 if (DEBUG) {
81                     Log.d(TAG, "TvInputInfo [" + info.loadLabel(context)
82                             + "] updated: " + info.toString());
83                 }
84             } else {
85                 if (DEBUG) {
86                     Log.d(TAG, "Updating tuner input's info failed. Input is not ready yet.");
87                 }
88             }
89         }
90     }
91 
92     /**
93      * Returns if the current tuner service is for a built-in tuner.
94      *
95      * @param context {@link Context} instance
96      */
isBuiltInTuner(Context context)97     public static boolean isBuiltInTuner(Context context) {
98         return TunerHal.getTunerType(context) == TunerHal.TUNER_TYPE_BUILT_IN;
99     }
100 }
101