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