-
Notifications
You must be signed in to change notification settings - Fork 43
/
CurrentServiceFragment.java
285 lines (253 loc) · 8.9 KB
/
CurrentServiceFragment.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
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
/* © 2010 Stephan Reichholf <stephan at reichholf dot net>
*
* Licensed under the Create-Commons Attribution-Noncommercial-Share Alike 3.0 Unported
* http://creativecommons.org/licenses/by-nc-sa/3.0/
*/
package net.reichholf.dreamdroid.fragment;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.loader.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.evernote.android.state.State;
import net.reichholf.dreamdroid.R;
import net.reichholf.dreamdroid.activities.abs.MultiPaneHandler;
import net.reichholf.dreamdroid.fragment.abs.BaseHttpFragment;
import net.reichholf.dreamdroid.fragment.dialogs.EpgDetailBottomSheet;
import net.reichholf.dreamdroid.helpers.ExtendedHashMap;
import net.reichholf.dreamdroid.helpers.Statics;
import net.reichholf.dreamdroid.helpers.enigma2.CurrentService;
import net.reichholf.dreamdroid.helpers.enigma2.Event;
import net.reichholf.dreamdroid.helpers.enigma2.Picon;
import net.reichholf.dreamdroid.helpers.enigma2.Service;
import net.reichholf.dreamdroid.helpers.enigma2.Timer;
import net.reichholf.dreamdroid.helpers.enigma2.requesthandler.CurrentServiceRequestHandler;
import net.reichholf.dreamdroid.helpers.enigma2.requesthandler.TimerAddByEventIdRequestHandler;
import net.reichholf.dreamdroid.intents.IntentFactory;
import net.reichholf.dreamdroid.loader.AsyncSimpleLoader;
import net.reichholf.dreamdroid.loader.LoaderResult;
import java.util.ArrayList;
/**
* Shows some information about the service currently running on TV
*
* @author sreichholf
*
*/
public class CurrentServiceFragment extends BaseHttpFragment {
@SuppressWarnings("unused")
private static final String LOG_TAG = "CurrentServiceFragment";
private TextView mServiceName;
private TextView mProvider;
private TextView mNowStart;
private TextView mNowTitle;
private TextView mNowDesc;
private TextView mNowDuration;
private TextView mNextStart;
private TextView mNextTitle;
private TextView mNextDesc;
private TextView mNextDuration;
private Button mStream;
private LinearLayout mNowLayout;
private LinearLayout mNextLayout;
protected ProgressDialog mProgress;
@Nullable
private ExtendedHashMap mService;
private ExtendedHashMap mNow;
private ExtendedHashMap mNext;
private boolean mCurrentServiceReady;
@Nullable
@State public ExtendedHashMap mCurrent;
@Nullable
@State public ExtendedHashMap mCurrentItem;
@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initTitles(getString(R.string.current_service));
mCurrentServiceReady = false;
}
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.current_service, container, false);
mServiceName = view.findViewById(R.id.service_name);
mProvider = view.findViewById(R.id.provider);
mNowStart = view.findViewById(R.id.event_now_start);
mNowTitle = view.findViewById(R.id.event_now_title);
mNowDesc = view.findViewById(R.id.event_now_desc);
mNowDuration = view.findViewById(R.id.event_now_duration);
mNextStart = view.findViewById(R.id.event_next_start);
mNextTitle = view.findViewById(R.id.event_next_title);
mNextDesc = view.findViewById(R.id.event_next_desc);
mNextDuration = view.findViewById(R.id.event_next_duration);
mStream = view.findViewById(R.id.ButtonStream);
mNowLayout = view.findViewById(R.id.layout_now);
mNextLayout = view.findViewById(R.id.layout_next);
registerOnClickListener(mNowLayout, Statics.ITEM_NOW);
registerOnClickListener(mNextLayout, Statics.ITEM_NEXT);
registerOnClickListener(mStream, Statics.ITEM_STREAM);
return view;
}
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
if (mCurrent == null || mCurrent.isEmpty())
mReload = true;
super.onViewCreated(view, savedInstanceState);
if(!mReload)
applyData(0, mCurrent);
}
/**
* Register an <code>OnClickListener</code> for a view and a specific item
* ID (<code>ITEM_*</code> statics)
*
* @param v
* The view an OnClickListener should be registered for
* @param id
* The id used to identify the item clicked (<code>ITEM_*</code>
* statics)
*/
protected void registerOnClickListener(@NonNull View v, final int id) {
v.setOnClickListener(v1 -> onItemSelected(id));
}
/**
* @param id
*/
@Override
protected boolean onItemSelected(int id) {
if(!mCurrentServiceReady){
showToast(getText(R.string.not_available));
return true;
}
String ref;
switch (id) {
case Statics.ITEM_NOW:
showEpgDetail(mNow);
return true;
case Statics.ITEM_NEXT:
showEpgDetail(mNext);
return true;
case Statics.ITEM_STREAM:
ref = mService.getString(Service.KEY_REFERENCE);
String name = mService.getString(Service.KEY_NAME);
if (!"".equals(ref) && ref != null) {
streamService(ref, name);
} else {
showToast(getText(R.string.not_available));
}
return true;
default:
return super.onItemSelected(id);
}
}
private void showEpgDetail(@Nullable ExtendedHashMap event) {
if (event != null) {
mCurrentItem = event;
Bundle args = new Bundle();
args.putSerializable("currentItem", mCurrentItem);
((MultiPaneHandler) getAppCompatActivity()).showDialogFragment(EpgDetailBottomSheet.class, args,
"current_epg_detail_dialog");
}
}
/**
* Called after loading the current service has finished to update the
* GUI-Content
*/
@Override
public void applyData(int loaderId, @Nullable ExtendedHashMap content) {
if (content != null && !content.isEmpty()) {
mCurrent = content;
mCurrentServiceReady = true;
mService = (ExtendedHashMap) mCurrent.get(CurrentService.KEY_SERVICE);
@SuppressWarnings("unchecked")
ArrayList<ExtendedHashMap> events = (ArrayList<ExtendedHashMap>) mCurrent.get(CurrentService.KEY_EVENTS);
mNow = events.get(0);
mNext = events.get(1);
mServiceName.setText(mService.getString(CurrentService.KEY_SERVICE_NAME));
mProvider.setText(mService.getString(CurrentService.KEY_SERVICE_PROVIDER));
// Now
mNowStart.setText(mNow.getString(Event.KEY_EVENT_START_READABLE));
mNowTitle.setText(mNow.getString(Event.KEY_EVENT_TITLE));
mNowDesc.setText(mNow.getString(Event.KEY_EVENT_DESCRIPTION_EXTENDED, ""));
mNowDuration.setText(mNow.getString(Event.KEY_EVENT_DURATION_READABLE));
// Next
mNextStart.setText(mNext.getString(Event.KEY_EVENT_START_READABLE));
mNextTitle.setText(mNext.getString(Event.KEY_EVENT_TITLE));
mNextDesc.setText(mNext.getString(Event.KEY_EVENT_DESCRIPTION_EXTENDED, ""));
mNextDuration.setText(mNext.getString(Event.KEY_EVENT_DURATION_READABLE));
ImageView piconView = getView().findViewById(R.id.picon);
Picon.setPiconForView(getAppCompatActivity(), piconView, mService, Statics.TAG_PICON);
} else {
showToast(getText(R.string.not_available));
}
}
/**
* @param event
*/
protected void setTimerByEventData(@NonNull ExtendedHashMap event) {
Timer.editUsingEvent((MultiPaneHandler) getAppCompatActivity(), event, this);
}
/**
* @param event
*/
protected void setTimerById(@NonNull ExtendedHashMap event) {
if (mProgress != null) {
if (mProgress.isShowing()) {
mProgress.dismiss();
}
}
mProgress = ProgressDialog.show(getAppCompatActivity(), "", getText(R.string.saving), true);
execSimpleResultTask(new TimerAddByEventIdRequestHandler(), Timer.getEventIdParams(event));
}
/*
* (non-Javadoc)
*
* @see
* net.reichholf.dreamdroid.abstivities.AbstractHttpListActivity#onSimpleResult
* (boolean, net.reichholf.dreamdroid.helpers.ExtendedHashMap)
*/
public void onSimpleResult(boolean success, ExtendedHashMap result) {
if (mProgress != null) {
if (mProgress.isShowing()) {
mProgress.dismiss();
}
}
super.onSimpleResult(success, result);
}
/**
* @param ref
* A ServiceReference
*/
private void streamService(String ref, String name) {
Intent intent = IntentFactory.getStreamServiceIntent(getAppCompatActivity(), ref, name);
startActivity(intent);
}
@Override
public void onDialogAction(int action, Object details, String dialogTag) {
switch (action) {
case Statics.ACTION_SET_TIMER:
setTimerById(mCurrentItem);
break;
case Statics.ACTION_EDIT_TIMER:
setTimerByEventData(mCurrentItem);
break;
case Statics.ACTION_FIND_SIMILAR:
findSimilarEvents(mCurrentItem);
break;
case Statics.ACTION_IMDB:
IntentFactory.queryIMDb(getAppCompatActivity(), mCurrentItem);
break;
}
}
@NonNull
@Override
public Loader<LoaderResult<ExtendedHashMap>> onCreateLoader(int id, Bundle args) {
return new AsyncSimpleLoader(getAppCompatActivity(), new CurrentServiceRequestHandler(),
args);
}
}