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.Intent;
21 import android.media.tv.TvContract;
22 import android.media.tv.TvInputInfo;
23 import android.net.Uri;
24 import android.os.Bundle;
25 import android.view.KeyEvent;
26 import com.android.tv.data.ChannelImpl;
27 import com.android.tv.ui.SelectInputView;
28 import com.android.tv.ui.SelectInputView.OnInputSelectedCallback;
29 import com.android.tv.util.Utils;
30 
31 /** An activity to select input. */
32 public class SelectInputActivity extends Activity {
33     private SelectInputView mSelectInputView;
34 
35     @Override
onCreate(Bundle savedInstanceState)36     public void onCreate(Bundle savedInstanceState) {
37         super.onCreate(savedInstanceState);
38         ((TvApplication) getApplicationContext()).setSelectInputActivity(this);
39         setContentView(R.layout.activity_select_input);
40         mSelectInputView = (SelectInputView) findViewById(R.id.scene_transition_common);
41         mSelectInputView.setOnInputSelectedCallback(
42                 new OnInputSelectedCallback() {
43                     @Override
44                     public void onTunerInputSelected() {
45                         startTvWithChannel(TvContract.Channels.CONTENT_URI);
46                     }
47 
48                     @Override
49                     public void onPassthroughInputSelected(TvInputInfo input) {
50                         startTvWithChannel(
51                                 TvContract.buildChannelUriForPassthroughInput(input.getId()));
52                     }
53 
54                     private void startTvWithChannel(Uri channelUri) {
55                         Intent intent =
56                                 new Intent(
57                                         Intent.ACTION_VIEW,
58                                         channelUri,
59                                         SelectInputActivity.this,
60                                         MainActivity.class);
61                         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
62                         startActivity(intent);
63                         finish();
64                     }
65                 });
66         String channelUriString = Utils.getLastWatchedChannelUri(this);
67         if (channelUriString != null) {
68             Uri channelUri = Uri.parse(channelUriString);
69             if (TvContract.isChannelUriForPassthroughInput(channelUri)) {
70                 mSelectInputView.setCurrentChannel(
71                         ChannelImpl.createPassthroughChannel(channelUri));
72             }
73             // No need to set the tuner channel because it's the default selection.
74         }
75     }
76 
77     @Override
onResume()78     protected void onResume() {
79         super.onResume();
80         mSelectInputView.onEnterAction(true);
81     }
82 
83     @Override
onPause()84     protected void onPause() {
85         mSelectInputView.onExitAction();
86         super.onPause();
87     }
88 
89     @Override
onDestroy()90     protected void onDestroy() {
91         ((TvApplication) getApplicationContext()).setSelectInputActivity(null);
92         super.onDestroy();
93     }
94 
95     @Override
onKeyUp(int keyCode, KeyEvent event)96     public boolean onKeyUp(int keyCode, KeyEvent event) {
97         if (keyCode == KeyEvent.KEYCODE_I || keyCode == KeyEvent.KEYCODE_TV_INPUT) {
98             mSelectInputView.onKeyUp(keyCode, event);
99             return true;
100         }
101         return super.onKeyUp(keyCode, event);
102     }
103 }
104