1 /* 2 * Copyright 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.tv.common; 18 19 import android.content.Intent; 20 import android.media.tv.TvInputInfo; 21 22 /** 23 * Util class for common use in TV app and inputs. 24 */ 25 public final class TvCommonUtils { 26 private static Boolean sRunningInTest; 27 TvCommonUtils()28 private TvCommonUtils() { } 29 30 /** 31 * Returns an intent to start the setup activity for the TV input using {@link 32 * TvCommonConstants#INTENT_ACTION_INPUT_SETUP}. 33 */ createSetupIntent(Intent originalSetupIntent, String inputId)34 public static Intent createSetupIntent(Intent originalSetupIntent, String inputId) { 35 if (originalSetupIntent == null) { 36 return null; 37 } 38 Intent setupIntent = new Intent(originalSetupIntent); 39 if (!TvCommonConstants.INTENT_ACTION_INPUT_SETUP.equals(originalSetupIntent.getAction())) { 40 Intent intentContainer = new Intent(TvCommonConstants.INTENT_ACTION_INPUT_SETUP); 41 intentContainer.putExtra(TvCommonConstants.EXTRA_SETUP_INTENT, originalSetupIntent); 42 intentContainer.putExtra(TvCommonConstants.EXTRA_INPUT_ID, inputId); 43 setupIntent = intentContainer; 44 } 45 return setupIntent; 46 } 47 48 /** 49 * Returns an intent to start the setup activity for this TV input using {@link 50 * TvCommonConstants#INTENT_ACTION_INPUT_SETUP}. 51 */ createSetupIntent(TvInputInfo input)52 public static Intent createSetupIntent(TvInputInfo input) { 53 return createSetupIntent(input.createSetupIntent(), input.getId()); 54 } 55 56 /** 57 * Checks if this application is running in tests. 58 * 59 * <p>{@link android.app.ActivityManager#isRunningInTestHarness} doesn't return {@code true} for 60 * the usual devices even the application is running in tests. We need to figure it out by 61 * checking whether the class in tv-tests-common module can be loaded or not. 62 */ isRunningInTest()63 public static synchronized boolean isRunningInTest() { 64 if (sRunningInTest == null) { 65 try { 66 Class.forName("com.android.tv.testing.Utils"); 67 sRunningInTest = true; 68 } catch (ClassNotFoundException e) { 69 sRunningInTest = false; 70 } 71 } 72 return sRunningInTest; 73 } 74 } 75