• 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;
18 
19 import android.app.Activity;
20 import android.content.ActivityNotFoundException;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.media.tv.TvInputInfo;
24 import android.os.Bundle;
25 import android.os.Handler;
26 import android.os.Looper;
27 import android.support.annotation.MainThread;
28 import android.util.Log;
29 import com.android.tv.common.SoftPreconditions;
30 import com.android.tv.common.actions.InputSetupActionUtils;
31 import com.android.tv.data.ChannelDataManager;
32 import com.android.tv.data.ChannelDataManager.Listener;
33 import com.android.tv.data.epg.EpgFetcher;
34 import com.android.tv.data.epg.EpgInputWhiteList;
35 import com.android.tv.features.TvFeatures;
36 import com.android.tv.util.SetupUtils;
37 import com.android.tv.util.TvInputManagerHelper;
38 import com.android.tv.util.Utils;
39 import com.google.android.tv.partner.support.EpgContract;
40 import java.util.concurrent.TimeUnit;
41 
42 /**
43  * An activity to launch a TV input setup activity.
44  *
45  * <p>After setup activity is finished, all channels will be browsable.
46  */
47 public class SetupPassthroughActivity extends Activity {
48     private static final String TAG = "SetupPassthroughAct";
49     private static final boolean DEBUG = false;
50 
51     private static final int REQUEST_START_SETUP_ACTIVITY = 200;
52 
53     private static ScanTimeoutMonitor sScanTimeoutMonitor;
54 
55     private TvInputInfo mTvInputInfo;
56     private Intent mActivityAfterCompletion;
57     private boolean mEpgFetcherDuringScan;
58     private EpgInputWhiteList mEpgInputWhiteList;
59 
60     @Override
onCreate(Bundle savedInstanceState)61     public void onCreate(Bundle savedInstanceState) {
62         if (DEBUG) Log.d(TAG, "onCreate");
63         super.onCreate(savedInstanceState);
64         TvSingletons tvSingletons = TvSingletons.getSingletons(this);
65         TvInputManagerHelper inputManager = tvSingletons.getTvInputManagerHelper();
66         Intent intent = getIntent();
67         String inputId = intent.getStringExtra(InputSetupActionUtils.EXTRA_INPUT_ID);
68         mTvInputInfo = inputManager.getTvInputInfo(inputId);
69         mEpgInputWhiteList = new EpgInputWhiteList(tvSingletons.getCloudEpgFlags());
70         mActivityAfterCompletion = InputSetupActionUtils.getExtraActivityAfter(intent);
71         boolean needToFetchEpg =
72                 mTvInputInfo != null && Utils.isInternalTvInput(this, mTvInputInfo.getId());
73         if (needToFetchEpg) {
74             // In case when the activity is restored, this flag should be restored as well.
75             mEpgFetcherDuringScan = true;
76         }
77         if (savedInstanceState == null) {
78             SoftPreconditions.checkArgument(
79                     InputSetupActionUtils.hasInputSetupAction(intent),
80                     TAG,
81                     "Unsupported action %s",
82                     intent.getAction());
83             if (DEBUG) Log.d(TAG, "TvInputId " + inputId + " / TvInputInfo " + mTvInputInfo);
84             if (mTvInputInfo == null) {
85                 Log.w(TAG, "There is no input with the ID " + inputId + ".");
86                 finish();
87                 return;
88             }
89             if (intent.getExtras() == null) {
90                 Log.w(TAG, "There is no extra info in the intent");
91                 finish();
92                 return;
93             }
94             Intent setupIntent = InputSetupActionUtils.getExtraSetupIntent(intent);
95             if (DEBUG) Log.d(TAG, "Setup activity launch intent: " + setupIntent);
96             if (setupIntent == null) {
97                 Log.w(TAG, "The input (" + mTvInputInfo.getId() + ") doesn't have setup.");
98                 finish();
99                 return;
100             }
101             SetupUtils.grantEpgPermission(this, mTvInputInfo.getServiceInfo().packageName);
102             if (DEBUG) Log.d(TAG, "Activity after completion " + mActivityAfterCompletion);
103             // If EXTRA_SETUP_INTENT is not removed, an infinite recursion happens during
104             // setupIntent.putExtras(intent.getExtras()).
105             Bundle extras = intent.getExtras();
106             InputSetupActionUtils.removeSetupIntent(extras);
107             setupIntent.putExtras(extras);
108             try {
109                 startActivityForResult(setupIntent, REQUEST_START_SETUP_ACTIVITY);
110             } catch (ActivityNotFoundException e) {
111                 Log.e(TAG, "Can't find activity: " + setupIntent.getComponent());
112                 finish();
113                 return;
114             }
115             if (needToFetchEpg) {
116                 if (sScanTimeoutMonitor == null) {
117                     sScanTimeoutMonitor = new ScanTimeoutMonitor(this);
118                 }
119                 sScanTimeoutMonitor.startMonitoring();
120                 TvSingletons.getSingletons(this).getEpgFetcher().onChannelScanStarted();
121             }
122         }
123     }
124 
125     @Override
onActivityResult(int requestCode, final int resultCode, final Intent data)126     public void onActivityResult(int requestCode, final int resultCode, final Intent data) {
127         if (DEBUG)
128             Log.d(TAG, "onActivityResult(" + requestCode + ",  " + resultCode + ",  " + data + ")");
129         if (sScanTimeoutMonitor != null) {
130             sScanTimeoutMonitor.stopMonitoring();
131         }
132         // Note: It's not guaranteed that this method is always called after scanning.
133         boolean setupComplete =
134                 requestCode == REQUEST_START_SETUP_ACTIVITY && resultCode == Activity.RESULT_OK;
135         // Tells EpgFetcher that channel source setup is finished.
136         EpgFetcher epgFetcher = TvSingletons.getSingletons(this).getEpgFetcher();
137         if (mEpgFetcherDuringScan) {
138             epgFetcher.onChannelScanFinished();
139         }
140         if (!setupComplete) {
141             setResult(resultCode, data);
142             finish();
143             return;
144         }
145         if (mTvInputInfo == null) {
146             Log.w(
147                     TAG,
148                     "There is no input with ID "
149                             + getIntent().getStringExtra(InputSetupActionUtils.EXTRA_INPUT_ID)
150                             + ".");
151             setResult(resultCode, data);
152             finish();
153             return;
154         }
155         TvSingletons.getSingletons(this)
156                 .getSetupUtils()
157                 .onTvInputSetupFinished(
158                         mTvInputInfo.getId(),
159                         () -> {
160                             if (mActivityAfterCompletion != null) {
161                                 try {
162                                     startActivity(mActivityAfterCompletion);
163                                 } catch (ActivityNotFoundException e) {
164                                     Log.w(TAG, "Activity launch failed", e);
165                                 }
166                             }
167                             setResult(resultCode, data);
168                             finish();
169                         });
170     }
171 
172     /**
173      * Monitors the scan progress and notifies the timeout of the scanning. The purpose of this
174      * monitor is to call EpgFetcher.onChannelScanFinished() in case when
175      * SetupPassthroughActivity.onActivityResult() is not called properly. b/36008534
176      */
177     @MainThread
178     private static class ScanTimeoutMonitor {
179         // Set timeout long enough. The message in Sony TV says the scanning takes about 30 minutes.
180         private static final long SCAN_TIMEOUT_MS = TimeUnit.MINUTES.toMillis(30);
181 
182         private final Context mContext;
183         private final ChannelDataManager mChannelDataManager;
184         private final Handler mHandler = new Handler(Looper.getMainLooper());
185         private final Runnable mScanTimeoutRunnable =
186                 () -> {
187                     Log.w(
188                             TAG,
189                             "No channels has been added for a while."
190                                     + " The scan might have finished unexpectedly.");
191                     onScanTimedOut();
192                 };
193         private final Listener mChannelDataManagerListener =
194                 new Listener() {
195                     @Override
196                     public void onLoadFinished() {
197                         setupTimer();
198                     }
199 
200                     @Override
201                     public void onChannelListUpdated() {
202                         setupTimer();
203                     }
204 
205                     @Override
206                     public void onChannelBrowsableChanged() {}
207                 };
208         private boolean mStarted;
209 
ScanTimeoutMonitor(Context context)210         private ScanTimeoutMonitor(Context context) {
211             mContext = context.getApplicationContext();
212             mChannelDataManager = TvSingletons.getSingletons(context).getChannelDataManager();
213         }
214 
startMonitoring()215         private void startMonitoring() {
216             if (!mStarted) {
217                 mStarted = true;
218                 mChannelDataManager.addListener(mChannelDataManagerListener);
219             }
220             if (mChannelDataManager.isDbLoadFinished()) {
221                 setupTimer();
222             }
223         }
224 
stopMonitoring()225         private void stopMonitoring() {
226             if (mStarted) {
227                 mStarted = false;
228                 mHandler.removeCallbacks(mScanTimeoutRunnable);
229                 mChannelDataManager.removeListener(mChannelDataManagerListener);
230             }
231         }
232 
setupTimer()233         private void setupTimer() {
234             mHandler.removeCallbacks(mScanTimeoutRunnable);
235             mHandler.postDelayed(mScanTimeoutRunnable, SCAN_TIMEOUT_MS);
236         }
237 
onScanTimedOut()238         private void onScanTimedOut() {
239             stopMonitoring();
240             TvSingletons.getSingletons(mContext).getEpgFetcher().onChannelScanFinished();
241         }
242     }
243 }
244