aboutsummaryrefslogtreecommitdiff
path: root/common/src/com/android/tv/common/util/CommonUtils.java
blob: 305431d3411446a98bea5444c047fa40826e73c3 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * 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.util;

import android.content.Context;
import android.content.Intent;
import android.media.tv.TvInputInfo;
import android.os.Build;
import android.util.ArraySet;
import android.util.Log;
import com.android.tv.common.BuildConfig;
import com.android.tv.common.CommonConstants;
import com.android.tv.common.actions.InputSetupActionUtils;
import com.android.tv.common.experiments.Experiments;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Set;

/** Util class for common use in TV app and inputs. */
@SuppressWarnings("AndroidApiChecker") // TODO(b/32513850) remove when error prone is updated
public final class CommonUtils {
    private static final String TAG = "CommonUtils";
    private static final ThreadLocal<SimpleDateFormat> ISO_8601 =
            new ThreadLocal() {
                private final SimpleDateFormat value =
                        new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US);

                @Override
                protected SimpleDateFormat initialValue() {
                    return value;
                }
            };
    // Hardcoded list for known bundled inputs not written by OEM/SOCs.
    // Bundled (system) inputs not in the list will get the high priority
    // so they and their channels come first in the UI.
    private static final Set<String> BUNDLED_PACKAGE_SET = new ArraySet<>();

    static {
        BUNDLED_PACKAGE_SET.add("com.android.tv");
    }

    private static Boolean sRunningInTest;

    private CommonUtils() {}

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

    /**
     * Returns an intent to start the setup activity for this TV input using {@link
     * InputSetupActionUtils#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.Utils");
                Log.i(
                        TAG,
                        "Assumed to be running in a test because"
                                + " com.android.tv.testing.utils.Utils is found");
                sRunningInTest = true;
            } catch (ClassNotFoundException e) {
                sRunningInTest = false;
            }
        }
        return sRunningInTest;
    }

    /** Checks whether a given package is in our bundled package set. */
    public static boolean isInBundledPackageSet(String packageName) {
        return BUNDLED_PACKAGE_SET.contains(packageName);
    }

    /** Checks whether a given input is a bundled input. */
    public static boolean isBundledInput(String inputId) {
        for (String prefix : BUNDLED_PACKAGE_SET) {
            if (inputId.startsWith(prefix + "/")) {
                return true;
            }
        }
        return false;
    }

    /** Returns true if the application is packaged with Live TV. */
    public static boolean isPackagedWithLiveChannels(Context context) {
        return (CommonConstants.BASE_PACKAGE.equals(context.getPackageName()));
    }

    /** Returns true if the current user is a developer. */
    public static boolean isDeveloper() {
        return BuildConfig.ENG || Experiments.ENABLE_DEVELOPER_FEATURES.get();
    }

    /** Converts time in milliseconds to a ISO 8061 string. */
    public static String toIsoDateTimeString(long timeMillis) {
        return ISO_8601.get().format(new Date(timeMillis));
    }

    /** Deletes a file or a directory. */
    public static void deleteDirOrFile(File fileOrDirectory) {
        if (fileOrDirectory.isDirectory()) {
            for (File child : fileOrDirectory.listFiles()) {
                deleteDirOrFile(child);
            }
        }
        fileOrDirectory.delete();
    }

    public static boolean isRoboTest() {
        return "robolectric".equals(Build.FINGERPRINT);
    }
}