1/*
2 * Copyright (C) 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
17package com.android.tv.testing;
18
19import android.content.ComponentName;
20import android.content.ContentResolver;
21import android.content.Context;
22import android.content.pm.PackageManager;
23import android.content.pm.ServiceInfo;
24import android.content.res.Resources;
25import android.media.tv.TvInputInfo;
26import android.media.tv.TvInputManager;
27import android.net.Uri;
28import android.util.Log;
29
30import com.android.tv.common.TvCommonUtils;
31
32import java.io.IOException;
33import java.io.InputStream;
34import java.io.OutputStream;
35import java.text.SimpleDateFormat;
36import java.util.Date;
37import java.util.Locale;
38import java.util.Random;
39
40/**
41 * An utility class for testing.
42 *
43 * <p>This class is also used to check whether TV app is running in tests or not.
44 *
45 * @see TvCommonUtils#isRunningInTest
46 */
47public final class Utils {
48    private static final String TAG ="Utils";
49
50    private static final long DEFAULT_RANDOM_SEED = getSeed();
51
52    public static String getUriStringForResource(Context context, int resId) {
53        if (resId == 0) {
54            return "";
55        }
56        Resources res = context.getResources();
57        return new Uri.Builder()
58            .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
59            .authority(res.getResourcePackageName(resId))
60            .path(res.getResourceTypeName(resId))
61            .appendPath(res.getResourceEntryName(resId)).build().toString();
62    }
63
64    public static void copy(InputStream is, OutputStream os) throws IOException {
65        byte[] buffer = new byte[1024];
66        int len;
67        while ((len = is.read(buffer)) != -1) {
68            os.write(buffer, 0, len);
69        }
70    }
71
72    public static String getServiceNameFromInputId(Context context, String inputId) {
73        TvInputManager tim = (TvInputManager) context.getSystemService(Context.TV_INPUT_SERVICE);
74        for (TvInputInfo info : tim.getTvInputList()) {
75            if (info.getId().equals(inputId)) {
76                return info.getServiceInfo().name;
77            }
78        }
79        return null;
80    }
81
82    public static String getInputIdFromComponentName(Context context, ComponentName name) {
83        TvInputManager tim = (TvInputManager) context.getSystemService(Context.TV_INPUT_SERVICE);
84        for (TvInputInfo info : tim.getTvInputList()) {
85            ServiceInfo si = info.getServiceInfo();
86            if (new ComponentName(si.packageName, si.name).equals(name)) {
87                return info.getId();
88            }
89        }
90        return null;
91    }
92
93    /**
94     * Return the Random class which is needed to make random data for testing.
95     * Default seed of the random is today's date.
96     */
97    public static Random createTestRandom() {
98        return new Random(DEFAULT_RANDOM_SEED);
99    }
100
101    private static long getSeed() {
102        // Set random seed as the date to track failed test data easily.
103        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
104        String today = dateFormat.format(new Date());
105        Log.d(TAG, "Today's random seed is " + today);
106        return Long.valueOf(today);
107    }
108
109    private Utils() {}
110
111    /**
112     * Checks whether TvActivity is enabled or not.
113     */
114    public static boolean isTvActivityEnabled(Context context) {
115        PackageManager pm = context.getPackageManager();
116        ComponentName name = new ComponentName("com.android.tv",
117                "com.android.tv.TvActivity");
118        int enabled = pm.getComponentEnabledSetting(name);
119        return enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED
120                || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
121    }
122}
123