-
Notifications
You must be signed in to change notification settings - Fork 238
/
RichSetupFragment.java
116 lines (104 loc) · 4.54 KB
/
RichSetupFragment.java
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
/*
* Copyright 2016 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.example.android.sampletvinput.rich;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.SharedPreferences;
import android.media.tv.TvInputInfo;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.android.sampletvinput.R;
import com.google.android.media.tv.companionlibrary.ChannelSetupFragment;
import com.google.android.media.tv.companionlibrary.EpgSyncJobService;
import com.example.android.sampletvinput.SampleJobService;
/**
* Fragment which shows a sample UI for registering channels and setting up SampleJobService to
* provide program information in the background.
*/
public class RichSetupFragment extends ChannelSetupFragment {
public static final long FULL_SYNC_FREQUENCY_MILLIS = 1000 * 60 * 60 * 24; // 24 hour
private static final long FULL_SYNC_WINDOW_SEC = 1000 * 60 * 60 * 24 * 14; // 2 weeks
private String mInputId = null;
private boolean mErrorFound;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mInputId = getActivity().getIntent().getStringExtra(TvInputInfo.EXTRA_INPUT_ID);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragmentView = super.onCreateView(inflater, container, savedInstanceState);
setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.detail_background));
setBadge(getResources().getDrawable(R.drawable.your_company));
setChannelListVisibility(true);
setTitle(getString(R.string.rich_input_label));
setDescription(getString(R.string.company_name));
setButtonText(getString(R.string.rich_setup_add_channel));
return fragmentView;
}
@Override
public void onScanStarted() {
EpgSyncJobService.cancelAllSyncRequests(getActivity());
EpgSyncJobService.requestImmediateSync(getActivity(), mInputId,
new ComponentName(getActivity(), SampleJobService.class));
// Set up SharedPreference to share inputId. If there is not periodic sync job after reboot,
// RichBootReceiver can use the shared inputId to set up periodic sync job.
SharedPreferences sharedPreferences = getActivity().getSharedPreferences(
EpgSyncJobService.PREFERENCE_EPG_SYNC, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(EpgSyncJobService.BUNDLE_KEY_INPUT_ID, mInputId);
editor.apply();
setButtonText(getString(R.string.rich_setup_in_progress));
}
@Override
public String getInputId() {
return mInputId;
}
@Override
public void onScanFinished() {
if (!mErrorFound) {
EpgSyncJobService.cancelAllSyncRequests(getActivity());
EpgSyncJobService.setUpPeriodicSync(getActivity(), mInputId,
new ComponentName(getActivity(), SampleJobService.class),
FULL_SYNC_FREQUENCY_MILLIS, FULL_SYNC_WINDOW_SEC);
getActivity().setResult(Activity.RESULT_OK);
} else {
getActivity().setResult(Activity.RESULT_CANCELED);
}
getActivity().finish();
}
@Override
public void onScanError(int reason) {
mErrorFound = true;
switch (reason) {
case EpgSyncJobService.ERROR_EPG_SYNC_CANCELED:
setDescription(R.string.sync_error_canceled);
break;
case EpgSyncJobService.ERROR_NO_PROGRAMS:
case EpgSyncJobService.ERROR_NO_CHANNELS:
setDescription(R.string.sync_error_no_data);
break;
default:
setDescription(getString(R.string.sync_error_other, reason));
break;
}
}
}