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
17package com.android.tv.tuner.util;
18
19import android.annotation.TargetApi;
20import android.content.ComponentName;
21import android.content.Context;
22import android.media.tv.TvInputInfo;
23import android.media.tv.TvInputManager;
24import android.os.AsyncTask;
25import android.os.Build;
26import android.support.annotation.Nullable;
27import android.util.Log;
28import android.util.Pair;
29import com.android.tv.common.BaseApplication;
30import com.android.tv.common.BuildConfig;
31import com.android.tv.common.feature.CommonFeatures;
32import com.android.tv.tuner.R;
33import com.android.tv.tuner.TunerHal;
34
35/** Utility class for providing tuner input info. */
36public class TunerInputInfoUtils {
37    private static final String TAG = "TunerInputInfoUtils";
38    private static final boolean DEBUG = false;
39
40    /** Builds tuner input's info. */
41    @Nullable
42    @TargetApi(Build.VERSION_CODES.N)
43    public static TvInputInfo buildTunerInputInfo(Context context) {
44        Pair<Integer, Integer> tunerTypeAndCount = TunerHal.getTunerTypeAndCount(context);
45        if (tunerTypeAndCount.first == null || tunerTypeAndCount.second == 0) {
46            return null;
47        }
48        int inputLabelId = 0;
49        switch (tunerTypeAndCount.first) {
50            case TunerHal.TUNER_TYPE_BUILT_IN:
51                inputLabelId = R.string.bt_app_name;
52                break;
53            case TunerHal.TUNER_TYPE_USB:
54                inputLabelId = R.string.ut_app_name;
55                break;
56            case TunerHal.TUNER_TYPE_NETWORK:
57                inputLabelId = R.string.nt_app_name;
58                break;
59        }
60        try {
61            String inputId = BaseApplication.getSingletons(context).getEmbeddedTunerInputId();
62            TvInputInfo.Builder builder =
63                    new TvInputInfo.Builder(context, ComponentName.unflattenFromString(inputId));
64            return builder.setLabel(inputLabelId)
65                    .setCanRecord(CommonFeatures.DVR.isEnabled(context))
66                    .setTunerCount(tunerTypeAndCount.second)
67                    .build();
68        } catch (IllegalArgumentException | NullPointerException e) {
69            // BaseTunerTvInputService is not enabled.
70            return null;
71        }
72    }
73
74    /**
75     * Updates tuner input's info.
76     *
77     * @param context {@link Context} instance
78     */
79    public static void updateTunerInputInfo(Context context) {
80        final Context appContext = context.getApplicationContext();
81        if (!BuildConfig.NO_JNI_TEST && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
82            new AsyncTask<Void, Void, TvInputInfo>() {
83                @Override
84                protected TvInputInfo doInBackground(Void... params) {
85                    if (DEBUG) Log.d(TAG, "updateTunerInputInfo()");
86                    return buildTunerInputInfo(appContext);
87                }
88
89                @Override
90                @TargetApi(Build.VERSION_CODES.N)
91                protected void onPostExecute(TvInputInfo info) {
92                    if (info != null) {
93                        ((TvInputManager) appContext.getSystemService(Context.TV_INPUT_SERVICE))
94                                .updateTvInputInfo(info);
95                        if (DEBUG) {
96                            Log.d(
97                                    TAG,
98                                    "TvInputInfo ["
99                                            + info.loadLabel(appContext)
100                                            + "] updated: "
101                                            + info.toString());
102                        }
103                    } else {
104                        if (DEBUG) {
105                            Log.d(TAG, "Updating tuner input info failed. Input is not ready yet.");
106                        }
107                    }
108                }
109            }.execute();
110        }
111    }
112}
113