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.testinput;
18
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.app.Dialog;
22import android.app.DialogFragment;
23import android.content.Context;
24import android.content.DialogInterface;
25import android.media.tv.TvContract;
26import android.media.tv.TvInputInfo;
27import android.os.Bundle;
28import android.util.Log;
29
30import com.android.tv.testing.ChannelInfo;
31import com.android.tv.testing.ChannelUtils;
32import com.android.tv.testing.Constants;
33import com.android.tv.testing.ProgramInfo;
34import com.android.tv.testing.ProgramUtils;
35
36import java.util.ArrayList;
37import java.util.List;
38import java.util.Map;
39
40/**
41 * The setup activity for {@link TestTvInputService}.
42 */
43public class TestTvInputSetupActivity extends Activity {
44    private static final String TAG = "TestTvInputSetup";
45    private String mInputId;
46
47    @Override
48    public void onCreate(Bundle savedInstanceState) {
49        super.onCreate(savedInstanceState);
50        mInputId = getIntent().getStringExtra(TvInputInfo.EXTRA_INPUT_ID);
51
52        DialogFragment newFragment = new MyAlertDialogFragment();
53        newFragment.show(getFragmentManager(), "dialog");
54    }
55
56    private void registerChannels(int channelCount) {
57        TestTvInputSetupActivity context = this;
58        registerChannels(context, mInputId, channelCount);
59    }
60
61    public static void registerChannels(Context context, String inputId, int channelCount) {
62        Log.i(TAG, "Registering " + channelCount + " channels");
63        List<ChannelInfo> channels = new ArrayList<>();
64        for (int i = 1; i <= channelCount; i++) {
65            channels.add(ChannelInfo.create(context, i));
66        }
67        ChannelUtils.updateChannels(context, inputId, channels);
68
69        // Reload channels so we have the ids.
70        Map<Long, ChannelInfo> channelIdToInfoMap =
71                ChannelUtils.queryChannelInfoMapForTvInput(context, inputId);
72        for (Long channelId : channelIdToInfoMap.keySet()) {
73            ProgramInfo programInfo = ProgramInfo.create();
74            ProgramUtils.populatePrograms(context, TvContract.buildChannelUri(channelId),
75                    programInfo);
76        }
77    }
78
79    public static class MyAlertDialogFragment extends DialogFragment {
80        @Override
81        public Dialog onCreateDialog(Bundle savedInstanceState) {
82            return new AlertDialog.Builder(getActivity()).setTitle(R.string.simple_setup_title)
83                    .setMessage(R.string.simple_setup_message)
84                    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
85                        @Override
86                        public void onClick(DialogInterface dialog, int whichButton) {
87                            // TODO: add UI to ask how many channels
88                            ((TestTvInputSetupActivity) getActivity())
89                                    .registerChannels(Constants.UNIT_TEST_CHANNEL_COUNT);
90                            // Sets the results so that the application can process the
91                            // registered channels properly.
92                            getActivity().setResult(Activity.RESULT_OK);
93                            getActivity().finish();
94                        }
95                    }).setNegativeButton(android.R.string.cancel,
96                            new DialogInterface.OnClickListener() {
97                                @Override
98                                public void onClick(DialogInterface dialog, int whichButton) {
99                                    getActivity().finish();
100                                }
101                            }).create();
102        }
103    }
104}
105