aboutsummaryrefslogtreecommitdiff
path: root/common/src/com/android/tv/common/TvCommonUtils.java
blob: c391ad2410df9c8eb24c3bc202493101acbf4d16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
 * Copyright 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.common;

import android.content.Intent;
import android.media.tv.TvInputInfo;

/**
 * Util class for common use in TV app and inputs.
 */
public final class TvCommonUtils {
    private static Boolean sRunningInTest;

    private TvCommonUtils() { }

    /**
     * Returns an intent to start the setup activity for the TV input using {@link
     * TvCommonConstants#INTENT_ACTION_INPUT_SETUP}.
     */
    public static Intent createSetupIntent(Intent originalSetupIntent, String inputId) {
        if (originalSetupIntent == null) {
            return null;
        }
        Intent setupIntent = new Intent(originalSetupIntent);
        if (!TvCommonConstants.INTENT_ACTION_INPUT_SETUP.equals(originalSetupIntent.getAction())) {
            Intent intentContainer = new Intent(TvCommonConstants.INTENT_ACTION_INPUT_SETUP);
            intentContainer.putExtra(TvCommonConstants.EXTRA_SETUP_INTENT, originalSetupIntent);
            intentContainer.putExtra(TvCommonConstants.EXTRA_INPUT_ID, inputId);
            setupIntent = intentContainer;
        }
        return setupIntent;
    }

    /**
     * Returns an intent to start the setup activity for this TV input using {@link
     * TvCommonConstants#INTENT_ACTION_INPUT_SETUP}.
     */
    public static Intent createSetupIntent(TvInputInfo input) {
        return createSetupIntent(input.createSetupIntent(), input.getId());
    }

    /**
     * Checks if this application is running in tests.
     *
     * <p>{@link android.app.ActivityManager#isRunningInTestHarness} doesn't return {@code true} for
     * the usual devices even the application is running in tests. We need to figure it out by
     * checking whether the class in tv-tests-common module can be loaded or not.
     */
    public static synchronized boolean isRunningInTest() {
        if (sRunningInTest == null) {
            try {
                Class.forName("com.android.tv.testing.Utils");
                sRunningInTest = true;
            } catch (ClassNotFoundException e) {
                sRunningInTest = false;
            }
        }
        return sRunningInTest;
    }
}