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