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