1 /*
2  * Copyright (C) 2021 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.util;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.pm.PackageManager;
22 import android.util.Log;
23 
24 /** A utility class for Google TV */
25 public class GtvUtils {
26     private static final String TAG = "GtvUtils";
27     private static final String AMATI_FEATURE = "com.google.android.feature.AMATI_EXPERIENCE";
28     private static final String PERMISSION_WRITE_EPG_DATA =
29             "com.android.providers.tv.permission.WRITE_EPG_DATA";
30     private static final String ACTION_INPUT_SELECTED = "android.apps.tv.launcherx.INPUT_SELECTED";
31     private static final String EXTRA_INPUT_ID = "extra_input_id";
32     private static final String LAUNCHERX_PACKAGE_NAME = "com.google.android.apps.tv.launcherx";
33     private static Boolean mEnabled = null;
34 
isEnabled(Context context)35     private static boolean isEnabled(Context context) {
36         if (mEnabled == null) {
37             PackageManager pm = context.getPackageManager();
38             mEnabled = pm.hasSystemFeature(AMATI_FEATURE);
39         }
40         return mEnabled;
41     }
42 
43     /** Broadcasts the intent with inputId to the Launcher */
broadcastInputId(Context context, String inputId)44     public static void broadcastInputId(Context context, String inputId) {
45         if (isEnabled(context)) {
46             if (inputId == null) {
47                 Log.e(TAG, "Will not broadcast inputId because it is null");
48             } else {
49                 Intent intent = new Intent(ACTION_INPUT_SELECTED);
50                 intent.putExtra(EXTRA_INPUT_ID, inputId);
51                 intent.setPackage(LAUNCHERX_PACKAGE_NAME);
52                 context.sendBroadcast(intent, PERMISSION_WRITE_EPG_DATA);
53             }
54         }
55     }
56 }
57