1 /*
2  * Copyright (C) 2014 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.cts.verifier.tv;
18 
19 import android.app.Activity;
20 import android.content.ComponentName;
21 import android.content.ContentUris;
22 import android.content.ContentValues;
23 import android.content.Intent;
24 import android.database.Cursor;
25 import android.graphics.Color;
26 import android.media.tv.TvContract;
27 import android.media.tv.TvContract.Programs;
28 import android.media.tv.TvInputInfo;
29 import android.net.Uri;
30 import android.os.Bundle;
31 import android.util.Pair;
32 import android.view.View;
33 
34 import com.android.cts.verifier.R;
35 
36 import java.util.ArrayList;
37 
38 public class MockTvInputSetupActivity extends Activity {
39     private static final String TAG = "MockTvInputSetupActivity";
40 
41     /* package-private */ static final String CHANNEL_NUMBER = "999-0";
42     /* package-private */ static final String CHANNEL_NAME = "Dummy";
43 
44     /* package-private */ static final String PROGRAM_TITLE = "Dummy Program";
45     /* package-private */ static final String PROGRAM_DESCRIPTION = "Dummy Program Description";
46 
47     /* package-private */ static final String APP_LINK_TEST_KEY = "app_link_test_key";
48     /* package-private */ static final String APP_LINK_TEST_VALUE = "app_link_test_value";
49     private static final String APP_LINK_TEXT = "Cts App-Link Text";
50     private static final long PROGRAM_LENGTH_MILLIS = 60 * 60 * 1000;
51     private static final int PROGRAM_COUNT = 24;
52 
53     private static Object sLock = new Object();
54     private static Pair<View, Runnable> sLaunchCallback = null;
55 
expectLaunch(View postTarget, Runnable successCallback)56     static void expectLaunch(View postTarget, Runnable successCallback) {
57         synchronized (sLock) {
58             sLaunchCallback = Pair.create(postTarget, successCallback);
59         }
60     }
61 
62     @Override
onCreate(Bundle savedInstanceState)63     public void onCreate(Bundle savedInstanceState) {
64         try {
65             super.onCreate(savedInstanceState);
66             final String inputId = getIntent().getStringExtra(TvInputInfo.EXTRA_INPUT_ID);
67             final Uri uri = TvContract.buildChannelsUriForInput(inputId);
68             final String[] projection = { TvContract.Channels._ID };
69             try (Cursor cursor = getContentResolver().query(uri, projection, null, null, null)) {
70                 // If we already have channels, just finish without doing anything.
71                 if (cursor != null && cursor.getCount() > 0) {
72                     return;
73                 }
74             }
75 
76             // Add a channel.
77             ContentValues values = new ContentValues();
78             values.put(TvContract.Channels.COLUMN_INPUT_ID, inputId);
79             values.put(TvContract.Channels.COLUMN_DISPLAY_NUMBER, CHANNEL_NUMBER);
80             values.put(TvContract.Channels.COLUMN_DISPLAY_NAME, CHANNEL_NAME);
81             values.put(TvContract.Channels.COLUMN_APP_LINK_TEXT, APP_LINK_TEXT);
82             values.put(TvContract.Channels.COLUMN_APP_LINK_COLOR, Color.RED);
83             values.put(TvContract.Channels.COLUMN_APP_LINK_ICON_URI,
84                     "android.resource://" + getPackageName() + "/" + R.drawable.icon);
85             values.put(TvContract.Channels.COLUMN_APP_LINK_POSTER_ART_URI,
86                     "android.resource://" + getPackageName() + "/" + R.raw.sns_texture);
87             Intent appLinkIntentUri = new Intent(this, AppLinkTestActivity.class);
88             appLinkIntentUri.putExtra(APP_LINK_TEST_KEY, APP_LINK_TEST_VALUE);
89             appLinkIntentUri.setFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
90             values.put(TvContract.Channels.COLUMN_APP_LINK_INTENT_URI,
91                     appLinkIntentUri.toUri(Intent.URI_INTENT_SCHEME));
92 
93             Uri channelUri = getContentResolver().insert(uri, values);
94             // If the channel's ID happens to be zero, we add another and delete the one.
95             if (ContentUris.parseId(channelUri) == 0) {
96                 getContentResolver().delete(channelUri, null, null);
97                 channelUri = getContentResolver().insert(uri, values);
98             }
99 
100             // Add Programs.
101             values = new ContentValues();
102             values.put(Programs.COLUMN_CHANNEL_ID, ContentUris.parseId(channelUri));
103             values.put(Programs.COLUMN_TITLE, PROGRAM_TITLE);
104             values.put(Programs.COLUMN_SHORT_DESCRIPTION, PROGRAM_DESCRIPTION);
105             long nowMs = System.currentTimeMillis();
106             long startTimeMs = nowMs - nowMs % PROGRAM_LENGTH_MILLIS;
107             ArrayList<ContentValues> list = new ArrayList<>();
108             for (int i = 0; i < PROGRAM_COUNT; ++i) {
109                 values.put(Programs.COLUMN_START_TIME_UTC_MILLIS, startTimeMs);
110                 values.put(Programs.COLUMN_END_TIME_UTC_MILLIS,
111                         startTimeMs + PROGRAM_LENGTH_MILLIS);
112                 startTimeMs += PROGRAM_LENGTH_MILLIS;
113                 list.add(new ContentValues(values));
114             }
115             getContentResolver().bulkInsert(Programs.CONTENT_URI, list.toArray(
116                     new ContentValues[0]));
117         } finally {
118             Pair<View, Runnable> launchCallback = null;
119             synchronized (sLock) {
120                 launchCallback = sLaunchCallback;
121                 sLaunchCallback = null;
122             }
123             if (launchCallback != null) {
124                 launchCallback.first.post(launchCallback.second);
125             }
126 
127             setResult(Activity.RESULT_OK);
128             finish();
129         }
130     }
131 }
132