aboutsummaryrefslogtreecommitdiff
path: root/tuner/SampleDvbTuner/src/com/android/tv/tuner/sample/dvb/setup/SampleDvbTunerSetupActivity.java
blob: 54b3a9e7ef0528156f37d13cf9f4974c0e9e6f38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.tuner.sample.dvb.setup;

import android.app.FragmentManager;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.media.tv.TvInputInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;
import android.view.KeyEvent;
import com.android.tv.common.BaseApplication;
import com.android.tv.common.feature.CommonFeatures;
import com.android.tv.common.ui.setup.SetupFragment;
import com.android.tv.common.ui.setup.SetupMultiPaneFragment;
import com.android.tv.common.util.PostalCodeUtils;
import com.android.tv.tuner.TunerHal;
import com.android.tv.tuner.sample.dvb.R;
import com.android.tv.tuner.setup.BaseTunerSetupActivity;
import com.android.tv.tuner.setup.ConnectionTypeFragment;
import com.android.tv.tuner.setup.LineupFragment;
import com.android.tv.tuner.setup.PostalCodeFragment;
import com.android.tv.tuner.setup.ScanFragment;
import com.android.tv.tuner.setup.ScanResultFragment;
import com.android.tv.tuner.setup.WelcomeFragment;
import com.google.android.tv.partner.support.EpgContract;
import com.google.android.tv.partner.support.EpgInput;
import com.google.android.tv.partner.support.EpgInputs;
import com.google.android.tv.partner.support.Lineup;
import com.google.android.tv.partner.support.Lineups;
import com.google.android.tv.partner.support.TunerSetupUtils;
import java.util.ArrayList;
import java.util.List;

/** An activity that serves Live TV tuner setup process. */
public class SampleDvbTunerSetupActivity extends BaseTunerSetupActivity {
    private static final String TAG = "SampleDvbTunerSetupActivity";
    private static final boolean DEBUG = false;

    private static final int FETCH_LINEUP_TIMEOUT_MS = 10000; // 10 seconds
    private static final int FETCH_LINEUP_RETRY_TIMEOUT_MS = 20000; // 20 seconds
    private static final String OTAD_PREFIX = "OTAD";
    private static final String STRING_BROADCAST_DIGITAL = "Broadcast Digital";

    private LineupFragment currentLineupFragment;

    private List<String> channelNumbers;
    private List<Lineup> lineups;
    private Lineup selectedLineup;
    private List<Pair<Lineup, Integer>> lineupMatchCountPair;
    private FetchLineupTask fetchLineupTask;
    private EpgInput epgInput;
    private String postalCode;
    private final Handler handler = new Handler();
    private final Runnable cancelFetchLineupTaskRunnable =
            new Runnable() {
                @Override
                public void run() {
                    cancelFetchLineup();
                }
            };
    private String embeddedInputId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (DEBUG) {
            Log.d(TAG, "onCreate");
        }
        embeddedInputId = BaseApplication.getSingletons(this).getEmbeddedTunerInputId();
        new QueryEpgInputTask(embeddedInputId).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void executeGetTunerTypeAndCountAsyncTask() {
        new AsyncTask<Void, Void, Integer>() {
            @Override
            protected Integer doInBackground(Void... arg0) {
                return TunerHal.getTunerTypeAndCount(SampleDvbTunerSetupActivity.this).first;
            }

            @Override
            protected void onPostExecute(Integer result) {
                if (!SampleDvbTunerSetupActivity.this.isDestroyed()) {
                    mTunerType = result;
                    if (result == null) {
                        finish();
                    } else if (!mActivityStopped) {
                        showInitialFragment();
                    } else {
                        mPendingShowInitialFragment = true;
                    }
                }
            }
        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }

    @Override
    protected boolean executeAction(String category, int actionId, Bundle params) {
        switch (category) {
            case WelcomeFragment.ACTION_CATEGORY:
                switch (actionId) {
                    case SetupMultiPaneFragment.ACTION_DONE:
                        super.executeAction(category, actionId, params);
                        break;
                    default:
                        String postalCode = PostalCodeUtils.getLastPostalCode(this);
                        if (mNeedToShowPostalCodeFragment
                                || (CommonFeatures.ENABLE_CLOUD_EPG_REGION.isEnabled(
                                                getApplicationContext())
                                        && TextUtils.isEmpty(postalCode))) {
                            // We cannot get postal code automatically. Postal code input fragment
                            // should always be shown even if users have input some valid postal
                            // code in this activity before.
                            mNeedToShowPostalCodeFragment = true;
                            showPostalCodeFragment();
                        } else {
                            lineups = null;
                            selectedLineup = null;
                            this.postalCode = postalCode;
                            restartFetchLineupTask();
                            showConnectionTypeFragment();
                        }
                        break;
                }
                return true;
            case PostalCodeFragment.ACTION_CATEGORY:
                lineups = null;
                selectedLineup = null;
                switch (actionId) {
                    case SetupMultiPaneFragment.ACTION_DONE:
                        String postalCode = params.getString(PostalCodeFragment.KEY_POSTAL_CODE);
                        if (postalCode != null) {
                            this.postalCode = postalCode;
                            restartFetchLineupTask();
                        }
                        // fall through
                    case SetupMultiPaneFragment.ACTION_SKIP:
                        showConnectionTypeFragment();
                        break;
                    default: // fall out
                }
                return true;
            case ConnectionTypeFragment.ACTION_CATEGORY:
                channelNumbers = null;
                lineupMatchCountPair = null;
                return super.executeAction(category, actionId, params);
            case ScanFragment.ACTION_CATEGORY:
                switch (actionId) {
                    case ScanFragment.ACTION_CANCEL:
                        getFragmentManager().popBackStack();
                        return true;
                    case ScanFragment.ACTION_FINISH:
                        clearTunerHal();
                        channelNumbers =
                                params.getStringArrayList(ScanFragment.KEY_CHANNEL_NUMBERS);
                        selectedLineup = null;
                        if (CommonFeatures.ENABLE_CLOUD_EPG_REGION.isEnabled(
                                        getApplicationContext())
                                && channelNumbers != null
                                && !channelNumbers.isEmpty()
                                && !TextUtils.isEmpty(this.postalCode)) {
                            showLineupFragment();
                        } else {
                            showScanResultFragment();
                        }
                        return true;
                    default: // fall out
                }
                break;
            case LineupFragment.ACTION_CATEGORY:
                switch (actionId) {
                    case LineupFragment.ACTION_SKIP:
                        selectedLineup = null;
                        currentLineupFragment = null;
                        showScanResultFragment();
                        break;
                    case LineupFragment.ACTION_ID_RETRY:
                        currentLineupFragment.onRetry();
                        restartFetchLineupTask();
                        handler.postDelayed(
                                cancelFetchLineupTaskRunnable, FETCH_LINEUP_RETRY_TIMEOUT_MS);
                        break;
                    default:
                        if (actionId >= 0 && actionId < lineupMatchCountPair.size()) {
                            if (DEBUG) {
                                if (selectedLineup != null) {
                                    Log.d(
                                            TAG,
                                            "Lineup " + selectedLineup.getName() + " is selected.");
                                }
                            }
                            selectedLineup = lineupMatchCountPair.get(actionId).first;
                        }
                        currentLineupFragment = null;
                        showScanResultFragment();
                        break;
                }
                return true;
            case ScanResultFragment.ACTION_CATEGORY:
                switch (actionId) {
                    case SetupMultiPaneFragment.ACTION_DONE:
                        new SampleDvbTunerSetupActivity.InsertOrModifyEpgInputTask(
                                        selectedLineup, embeddedInputId)
                                .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                        break;
                    default:
                        // scan again
                        if (lineups == null || lineups.isEmpty()) {
                            lineups = null;
                            restartFetchLineupTask();
                        }
                        super.executeAction(category, actionId, params);
                        break;
                }
                return true;
            default: // fall out
        }
        return false;
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            FragmentManager manager = getFragmentManager();
            int count = manager.getBackStackEntryCount();
            if (count > 0) {
                String lastTag = manager.getBackStackEntryAt(count - 1).getName();
                if (LineupFragment.class.getCanonicalName().equals(lastTag) && count >= 2) {
                    // Pops fragment including ScanFragment.
                    manager.popBackStack(
                            manager.getBackStackEntryAt(count - 2).getName(),
                            FragmentManager.POP_BACK_STACK_INCLUSIVE);
                    return true;
                }
                if (ScanResultFragment.class.getCanonicalName().equals(lastTag) && count >= 2) {
                    String secondLastTag = manager.getBackStackEntryAt(count - 2).getName();
                    if (ScanFragment.class.getCanonicalName().equals(secondLastTag)) {
                        // Pops fragment including ScanFragment.
                        manager.popBackStack(
                                secondLastTag, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                        return true;
                    }
                    if (LineupFragment.class.getCanonicalName().equals(secondLastTag)) {
                        currentLineupFragment =
                                (LineupFragment) manager.findFragmentByTag(secondLastTag);
                        if (lineups == null || lineups.isEmpty()) {
                            lineups = null;
                            restartFetchLineupTask();
                        }
                    }
                } else if (ScanFragment.class.getCanonicalName().equals(lastTag)) {
                    mLastScanFragment.finishScan(true);
                    return true;
                }
            }
        }
        return super.onKeyUp(keyCode, event);
    }

    private void showLineupFragment() {
        if (lineupMatchCountPair == null && lineups != null) {
            lineupMatchCountPair = TunerSetupUtils.lineupChannelMatchCount(lineups, channelNumbers);
        }
        currentLineupFragment = new LineupFragment();
        currentLineupFragment.setArguments(getArgsForLineupFragment());
        currentLineupFragment.setShortDistance(
                SetupFragment.FRAGMENT_ENTER_TRANSITION | SetupFragment.FRAGMENT_RETURN_TRANSITION);
        handler.removeCallbacksAndMessages(null);
        showFragment(currentLineupFragment, true);
        handler.postDelayed(cancelFetchLineupTaskRunnable, FETCH_LINEUP_TIMEOUT_MS);
    }

    private Bundle getArgsForLineupFragment() {
        Bundle args = new Bundle();
        if (lineupMatchCountPair == null) {
            return args;
        }
        ArrayList<String> lineupNames = new ArrayList<>(lineupMatchCountPair.size());
        ArrayList<Integer> matchNumbers = new ArrayList<>(lineupMatchCountPair.size());
        int defaultLineupIndex = 0;
        for (Pair<Lineup, Integer> pair : lineupMatchCountPair) {
            Lineup lineup = pair.first;
            String name;
            if (!TextUtils.isEmpty(lineup.getName())) {
                name = lineup.getName();
            } else {
                name = lineup.getId();
            }
            if (name.equals(OTAD_PREFIX + postalCode) || name.equals(STRING_BROADCAST_DIGITAL)) {
                // rename OTA / antenna lineups
                name = getString(R.string.ut_lineup_name_antenna);
            }
            lineupNames.add(name);
            matchNumbers.add(pair.second);
            if (epgInput != null && TextUtils.equals(lineup.getId(), epgInput.getLineupId())) {
                // The last index is the current one.
                defaultLineupIndex = lineupNames.size() - 1;
            }
        }
        args.putStringArrayList(LineupFragment.KEY_LINEUP_NAMES, lineupNames);
        args.putIntegerArrayList(LineupFragment.KEY_MATCH_NUMBERS, matchNumbers);
        args.putInt(LineupFragment.KEY_DEFAULT_LINEUP, defaultLineupIndex);
        return args;
    }

    private void cancelFetchLineup() {
        if (fetchLineupTask == null) {
            return;
        }
        AsyncTask.Status status = fetchLineupTask.getStatus();
        if (status == AsyncTask.Status.RUNNING || status == AsyncTask.Status.PENDING) {
            fetchLineupTask.cancel(true);
            fetchLineupTask = null;
            if (currentLineupFragment != null) {
                currentLineupFragment.onLineupNotFound();
            }
        }
    }

    private void restartFetchLineupTask() {
        if (!CommonFeatures.ENABLE_CLOUD_EPG_REGION.isEnabled(getApplicationContext())
                || TextUtils.isEmpty(postalCode)) {
            return;
        }
        if (fetchLineupTask != null) {
            fetchLineupTask.cancel(true);
        }
        handler.removeCallbacksAndMessages(null);
        fetchLineupTask = new FetchLineupTask(getContentResolver(), postalCode);
        fetchLineupTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }

    private class FetchLineupTask extends AsyncTask<Void, Void, List<Lineup>> {
        private final ContentResolver contentResolver;
        private final String postalCode;

        FetchLineupTask(ContentResolver contentResolver, String postalCode) {
            this.contentResolver = contentResolver;
            this.postalCode = postalCode;
        }

        @Override
        protected List<Lineup> doInBackground(Void... args) {
            if (contentResolver == null || TextUtils.isEmpty(postalCode)) {
                return new ArrayList<>();
            }
            return new ArrayList<>(Lineups.query(contentResolver, postalCode));
        }

        @Override
        protected void onPostExecute(List<Lineup> lineups) {
            if (DEBUG) {
                if (lineups != null) {
                    Log.d(TAG, "FetchLineupTask fetched " + lineups.size() + " lineups");
                } else {
                    Log.d(TAG, "FetchLineupTask returned null");
                }
            }
            SampleDvbTunerSetupActivity.this.lineups = lineups;
            if (currentLineupFragment != null) {
                if (lineups == null || lineups.isEmpty()) {
                    currentLineupFragment.onLineupNotFound();
                } else {
                    lineupMatchCountPair =
                            TunerSetupUtils.lineupChannelMatchCount(
                                    SampleDvbTunerSetupActivity.this.lineups, channelNumbers);
                    currentLineupFragment.onLineupFound(getArgsForLineupFragment());
                }
            }
        }
    }

    private class InsertOrModifyEpgInputTask extends AsyncTask<Void, Void, Void> {
        private final Lineup lineup;
        private final String inputId;

        InsertOrModifyEpgInputTask(@Nullable Lineup lineup, String inputId) {
            this.lineup = lineup;
            this.inputId = inputId;
        }

        @Override
        protected Void doInBackground(Void... args) {
            if (lineup == null
                    || (SampleDvbTunerSetupActivity.this.epgInput != null
                            && TextUtils.equals(
                                    lineup.getId(),
                                    SampleDvbTunerSetupActivity.this.epgInput.getLineupId()))) {
                return null;
            }
            ContentValues values = new ContentValues();
            values.put(EpgContract.EpgInputs.COLUMN_INPUT_ID, inputId);
            values.put(EpgContract.EpgInputs.COLUMN_LINEUP_ID, lineup.getId());

            ContentResolver contentResolver = getContentResolver();
            if (SampleDvbTunerSetupActivity.this.epgInput != null) {
                values.put(
                        EpgContract.EpgInputs.COLUMN_ID,
                        SampleDvbTunerSetupActivity.this.epgInput.getId());
                EpgInputs.update(contentResolver, EpgInput.createEpgChannel(values));
                return null;
            }
            EpgInput epgInput = EpgInputs.queryEpgInput(contentResolver, inputId);
            if (epgInput == null) {
                contentResolver.insert(EpgContract.EpgInputs.CONTENT_URI, values);
            } else {
                values.put(EpgContract.EpgInputs.COLUMN_ID, epgInput.getId());
                EpgInputs.update(contentResolver, EpgInput.createEpgChannel(values));
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            Intent data = new Intent();
            data.putExtra(TvInputInfo.EXTRA_INPUT_ID, inputId);
            data.putExtra(EpgContract.EXTRA_USE_CLOUD_EPG, true);
            setResult(RESULT_OK, data);
            finish();
        }
    }

    private class QueryEpgInputTask extends AsyncTask<Void, Void, EpgInput> {
        private final String inputId;

        QueryEpgInputTask(String inputId) {
            this.inputId = inputId;
        }

        @Override
        protected EpgInput doInBackground(Void... args) {
            ContentResolver contentResolver = getContentResolver();
            return EpgInputs.queryEpgInput(contentResolver, inputId);
        }

        @Override
        protected void onPostExecute(EpgInput result) {
            epgInput = result;
        }
    }
}