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.Intent;
22 import android.media.tv.TvInputInfo;
23 import android.os.Bundle;
24 import android.util.Log;
25 
26 import com.android.tv.common.SoftPreconditions;
27 import com.android.tv.common.TvCommonConstants;
28 import com.android.tv.util.SetupUtils;
29 import com.android.tv.util.TvInputManagerHelper;
30 
31 /**
32  * An activity to launch a TV input setup activity.
33  *
34  * <p> After setup activity is finished, all channels will be browsable.
35  */
36 public class SetupPassthroughActivity extends Activity {
37     private static final String TAG = "SetupPassthroughAct";
38     private static final boolean DEBUG = false;
39 
40     private static final int REQUEST_START_SETUP_ACTIVITY = 200;
41 
42     private TvInputInfo mTvInputInfo;
43     private Intent mActivityAfterCompletion;
44 
45     @Override
onCreate(Bundle savedInstanceState)46     public void onCreate(Bundle savedInstanceState) {
47         super.onCreate(savedInstanceState);
48         Intent intent = getIntent();
49         SoftPreconditions.checkState(
50                 intent.getAction().equals(TvCommonConstants.INTENT_ACTION_INPUT_SETUP));
51         ApplicationSingletons appSingletons = TvApplication.getSingletons(this);
52         TvInputManagerHelper inputManager = appSingletons.getTvInputManagerHelper();
53         String inputId = intent.getStringExtra(TvCommonConstants.EXTRA_INPUT_ID);
54         mTvInputInfo = inputManager.getTvInputInfo(inputId);
55         if (DEBUG) Log.d(TAG, "TvInputId " + inputId + " / TvInputInfo " + mTvInputInfo);
56         if (mTvInputInfo == null) {
57             Log.w(TAG, "There is no input with the ID " + inputId + ".");
58             finish();
59             return;
60         }
61         Intent setupIntent = intent.getExtras().getParcelable(TvCommonConstants.EXTRA_SETUP_INTENT);
62         if (DEBUG) Log.d(TAG, "Setup activity launch intent: " + setupIntent);
63         if (setupIntent == null) {
64             Log.w(TAG, "The input (" + mTvInputInfo.getId() + ") doesn't have setup.");
65             finish();
66             return;
67         }
68         SetupUtils.grantEpgPermission(this, mTvInputInfo.getServiceInfo().packageName);
69         mActivityAfterCompletion = intent.getParcelableExtra(
70                 TvCommonConstants.EXTRA_ACTIVITY_AFTER_COMPLETION);
71         if (DEBUG) Log.d(TAG, "Activity after completion " + mActivityAfterCompletion);
72         // If EXTRA_SETUP_INTENT is not removed, an infinite recursion happens during
73         // setupIntent.putExtras(intent.getExtras()).
74         Bundle extras = intent.getExtras();
75         extras.remove(TvCommonConstants.EXTRA_SETUP_INTENT);
76         setupIntent.putExtras(extras);
77         startActivityForResult(setupIntent, REQUEST_START_SETUP_ACTIVITY);
78     }
79 
80     @Override
onActivityResult(int requestCode, final int resultCode, final Intent data)81     public void onActivityResult(int requestCode, final int resultCode, final Intent data) {
82         boolean setupComplete = requestCode == REQUEST_START_SETUP_ACTIVITY
83                 && resultCode == Activity.RESULT_OK;
84         if (!setupComplete) {
85             setResult(resultCode, data);
86             finish();
87             return;
88         }
89         SetupUtils.getInstance(this).onTvInputSetupFinished(mTvInputInfo.getId(), new Runnable() {
90             @Override
91             public void run() {
92                 if (mActivityAfterCompletion != null) {
93                     try {
94                         startActivity(mActivityAfterCompletion);
95                     } catch (ActivityNotFoundException e) {
96                         Log.w(TAG, "Activity launch failed", e);
97                     }
98                 }
99                 setResult(resultCode, data);
100                 finish();
101             }
102         });
103     }
104 }
105