1 /*
2  * Copyright 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.example.android.sampletvinput.simple;
18 
19 import android.app.Activity;
20 import android.app.AlertDialog;
21 import android.app.Dialog;
22 import android.app.DialogFragment;
23 import android.content.ContentValues;
24 import android.content.DialogInterface;
25 import android.database.Cursor;
26 import android.media.tv.TvContract;
27 import android.media.tv.TvInputInfo;
28 import android.net.Uri;
29 import android.os.Bundle;
30 
31 import com.example.android.sampletvinput.R;
32 
33 /**
34  * The setup activity for {@link SimpleTvInputService}.
35  */
36 public class SimpleTvInputSetupActivity extends Activity {
37     private static final String CHANNEL_1_NUMBER = "1-1";
38     private static final String CHANNEL_1_NAME = "Bunny - Low Resolution";
39     private static final int CHANNEL_1_ORIG_NETWORK_ID = 0;
40     private static final int CHANNEL_1_TRANSPORT_STREAM_ID = 0;
41     public static final int CHANNEL_1_SERVICE_ID = 1;
42 
43     private static final String CHANNEL_2_NUMBER = "1-2";
44     private static final String CHANNEL_2_NAME = "Bunny - High Resolution";
45     private static final int CHANNEL_2_ORIG_NETWORK_ID = 0;
46     private static final int CHANNEL_2_TRANSPORT_STREAM_ID = 0;
47     public static final int CHANNEL_2_SERVICE_ID = 2;
48 
49     private String mInputId;
50 
51     @Override
onCreate(Bundle savedInstanceState)52     public void onCreate(Bundle savedInstanceState) {
53         super.onCreate(savedInstanceState);
54         mInputId = getIntent().getStringExtra(TvInputInfo.EXTRA_INPUT_ID);
55 
56         DialogFragment newFragment = new MyAlertDialogFragment();
57         newFragment.show(getFragmentManager(), "dialog");
58     }
59 
registerChannels()60     private void registerChannels() {
61         // Check if we already registered channels.
62         Uri uri = TvContract.buildChannelsUriForInput(mInputId);
63         Cursor cursor = null;
64         try {
65             cursor = getContentResolver().query(uri, null, null, null, null);
66             if (cursor != null && cursor.getCount() > 0) {
67                 return;
68             }
69         } finally {
70             if (cursor != null) {
71                 cursor.close();
72             }
73         }
74 
75         ContentValues values = new ContentValues();
76         values.put(TvContract.Channels.COLUMN_INPUT_ID, mInputId);
77 
78         // Register channel 1-1.
79         values.put(TvContract.Channels.COLUMN_DISPLAY_NUMBER, CHANNEL_1_NUMBER);
80         values.put(TvContract.Channels.COLUMN_DISPLAY_NAME, CHANNEL_1_NAME);
81         values.put(TvContract.Channels.COLUMN_ORIGINAL_NETWORK_ID, CHANNEL_1_ORIG_NETWORK_ID);
82         values.put(TvContract.Channels.COLUMN_TRANSPORT_STREAM_ID, CHANNEL_1_TRANSPORT_STREAM_ID);
83         values.put(TvContract.Channels.COLUMN_SERVICE_ID, CHANNEL_1_SERVICE_ID);
84         getContentResolver().insert(TvContract.Channels.CONTENT_URI, values);
85 
86         // Register channel 1-2.
87         values.put(TvContract.Channels.COLUMN_DISPLAY_NUMBER, CHANNEL_2_NUMBER);
88         values.put(TvContract.Channels.COLUMN_DISPLAY_NAME, CHANNEL_2_NAME);
89         values.put(TvContract.Channels.COLUMN_ORIGINAL_NETWORK_ID, CHANNEL_2_ORIG_NETWORK_ID);
90         values.put(TvContract.Channels.COLUMN_TRANSPORT_STREAM_ID, CHANNEL_2_TRANSPORT_STREAM_ID);
91         values.put(TvContract.Channels.COLUMN_SERVICE_ID, CHANNEL_2_SERVICE_ID);
92         getContentResolver().insert(TvContract.Channels.CONTENT_URI, values);
93     }
94 
95     public static class MyAlertDialogFragment extends DialogFragment {
96         @Override
onCreateDialog(Bundle savedInstanceState)97         public Dialog onCreateDialog(Bundle savedInstanceState) {
98             return new AlertDialog.Builder(getActivity())
99                     .setTitle(R.string.simple_setup_title)
100                     .setMessage(R.string.simple_setup_message)
101                     .setPositiveButton(android.R.string.ok,
102                             new DialogInterface.OnClickListener() {
103                                 public void onClick(DialogInterface dialog, int whichButton) {
104                                     ((SimpleTvInputSetupActivity) getActivity()).registerChannels();
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                             }
111                     )
112                     .setNegativeButton(android.R.string.cancel,
113                             new DialogInterface.OnClickListener() {
114                                 public void onClick(DialogInterface dialog, int whichButton) {
115                                     getActivity().finish();
116                                 }
117                             }
118                     )
119                     .create();
120         }
121     }
122 }
123