forked from DigitalCampus/oppia-mobile-android
/
CourseIndexActivity.java
395 lines (349 loc) · 14.7 KB
/
CourseIndexActivity.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
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
/*
* This file is part of OppiaMobile - https://digital-campus.org/
*
* OppiaMobile is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OppiaMobile is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OppiaMobile. If not, see <http://www.gnu.org/licenses/>.
*/
package org.digitalcampus.oppia.activity;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.Callable;
import org.digitalcampus.mobile.learning.R;
import org.digitalcampus.oppia.adapter.SectionListAdapter;
import org.digitalcampus.oppia.exception.InvalidXMLException;
import org.digitalcampus.oppia.model.Activity;
import org.digitalcampus.oppia.model.Course;
import org.digitalcampus.oppia.model.CourseMetaPage;
import org.digitalcampus.oppia.model.Section;
import org.digitalcampus.oppia.service.TrackerService;
import org.digitalcampus.oppia.task.ParseCourseXMLTask;
import org.digitalcampus.oppia.utils.ImageUtils;
import org.digitalcampus.oppia.utils.UIUtils;
import org.digitalcampus.oppia.utils.xmlreaders.CourseXMLReader;
import android.animation.ValueAnimator;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AlertDialog;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.ListView;
public class CourseIndexActivity extends AppActivity implements OnSharedPreferenceChangeListener, ParseCourseXMLTask.OnParseXmlListener {
public static final String TAG = CourseIndexActivity.class.getSimpleName();
public static final String JUMPTO_TAG = "JumpTo";
public static final int RESULT_JUMPTO = 99;
private Course course;
private CourseXMLReader cxr;
private ArrayList<Section> sections;
private SharedPreferences prefs;
private Activity baselineActivity;
private AlertDialog aDialog;
private View loadingCourseView;
private SectionListAdapter sla;
private String digestJumpTo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course_index);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);
loadingCourseView = findViewById(R.id.loading_course);
Bundle bundle = this.getIntent().getExtras();
if (bundle != null) {
course = (Course) bundle.getSerializable(Course.TAG);
String digest = (String) bundle.getSerializable(JUMPTO_TAG);
if (digest != null){
//If there is a digest, we have to parse the course synchronously to avoid showing this activity
try {
cxr = new CourseXMLReader(course.getCourseXMLLocation(), course.getCourseId(), this);
} catch (InvalidXMLException e) {
e.printStackTrace();
showErrorMessage();
return;
}
boolean baselineCompleted = isBaselineCompleted();
if (baselineCompleted) {
course.setMetaPages(cxr.getMetaPages());
sections = cxr.getSections();
startCourseActivityByDigest(digest);
initializeCourseIndex(false);
}
else{
sections = cxr.getSections();
initializeCourseIndex(false);
showBaselineMessage(digest);
}
}
else{
ParseCourseXMLTask task = new ParseCourseXMLTask(this, true);
task.setListener(this);
task.execute(course);
}
}
}
@Override
public void onStart() {
super.onStart();
// set image
if (course.getImageFile() != null) {
BitmapDrawable bm = ImageUtils.LoadBMPsdcard(course.getImageFileFromRoot(), this.getResources(),
R.drawable.dc_logo);
//getSupportActionBar().setIcon(bm);
getSupportActionBar().setHomeAsUpIndicator(bm);
}
}
@Override
public void onResume() {
super.onResume();
if (aDialog != null) {
aDialog.show();
}
if (digestJumpTo != null && isBaselineCompleted()){
startCourseActivityByDigest(digestJumpTo);
digestJumpTo = null;
return;
}
// start a new tracker service
Intent service = new Intent(this, TrackerService.class);
Bundle tb = new Bundle();
tb.putBoolean("backgroundData", true);
service.putExtras(tb);
this.startService(service);
// remove any saved state info from shared prefs in case they interfere with subsequent page views
Editor editor = prefs.edit();
Map<String,?> keys = prefs.getAll();
for(Map.Entry<String,?> entry : keys.entrySet()){
if (entry.getKey().startsWith("widget_")){
editor.remove(entry.getKey());
}
}
editor.commit();
if ((sections != null) && (sections.size()>0)){
cxr.updateCourseActivity();
sla.notifyDataSetChanged();
if (!isBaselineCompleted()){
showBaselineMessage(null);
}
}
}
@Override
public void onPause() {
if (aDialog != null) {
aDialog.dismiss();
aDialog = null;
}
super.onPause();
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
getMenuInflater().inflate(R.menu.activity_course_index, menu);
ArrayList<CourseMetaPage> ammp = course.getMetaPages();
int order = 104;
for (CourseMetaPage mmp : ammp) {
String title = mmp.getLang(
prefs.getString(PrefsActivity.PREF_LANGUAGE, Locale.getDefault().getLanguage()))
.getContent();
menu.add(0, mmp.getId(), order, title).setIcon(android.R.drawable.ic_menu_info_details);
order++;
}
UIUtils.showUserData(menu, this, course);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.menu_language) {
createLanguageDialog();
return true;
} else if (itemId == android.R.id.home) {
this.finish();
return true;
} else {
Intent i;
Bundle tb = new Bundle();
if (itemId == R.id.menu_help) {
i = new Intent(this, AboutActivity.class);
tb.putSerializable(AboutActivity.TAB_ACTIVE, AboutActivity.TAB_HELP);
} else if (itemId == R.id.menu_scorecard) {
i = new Intent(this, ScorecardActivity.class);
tb.putSerializable(Course.TAG, course);
} else {
i = new Intent(this, CourseMetaPageActivity.class);
tb.putSerializable(Course.TAG, course);
tb.putInt(CourseMetaPage.TAG, item.getItemId());
}
i.putExtras(tb);
startActivityForResult(i, 1);
return true;
}
}
private void createLanguageDialog() {
UIUtils.createLanguageDialog(this, course.getLangs(), prefs, new Callable<Boolean>() {
public Boolean call() throws Exception {
CourseIndexActivity.this.onStart();
return true;
}
});
}
private void initializeCourseIndex(boolean animate){
final ListView listView = (ListView) findViewById(R.id.section_list);
sla = new SectionListAdapter(CourseIndexActivity.this, course, sections, new SectionListAdapter.CourseClickListener() {
@Override
public void onActivityClicked(String activityDigest) {
startCourseActivityByDigest(activityDigest);
}
});
if (animate){
AlphaAnimation fadeOutAnimation = new AlphaAnimation(1f, 0f);
fadeOutAnimation.setDuration(700);
fadeOutAnimation.setFillAfter(true);
listView.setAlpha(0f);
ValueAnimator animator = ValueAnimator.ofFloat(1f, 0f);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator valueAnimator){
listView.setTranslationX( (Float) valueAnimator.getAnimatedValue() * 80 );
listView.setAlpha(1f - (Float) valueAnimator.getAnimatedValue());
}
});
animator.setDuration(700);
animator.start();
loadingCourseView.startAnimation(fadeOutAnimation);
}
else{
loadingCourseView.setVisibility(View.GONE);
listView.setVisibility(View.VISIBLE);
}
listView.setAdapter(sla);
}
private boolean isBaselineCompleted() {
ArrayList<Activity> baselineActs = cxr.getBaselineActivities();
// TODO how to handle if more than one baseline activity
for (Activity a : baselineActs) {
if (!a.isAttempted()) {
this.baselineActivity = a;
return false;
}
}
return true;
}
private void showBaselineMessage(final String digest){
aDialog = new AlertDialog.Builder(this, R.style.Oppia_AlertDialogStyle).create();
aDialog.setCancelable(false);
aDialog.setTitle(R.string.alert_pretest);
aDialog.setMessage(this.getString(R.string.alert_pretest_summary));
aDialog.setButton(DialogInterface.BUTTON_NEGATIVE, this.getString(R.string.open),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//We set the digest to be able to jump to this activity after passing the baseline
digestJumpTo = digest;
Intent intent = new Intent(CourseIndexActivity.this, CourseActivity.class);
Bundle tb = new Bundle();
Section section = new Section();
section.addActivity(CourseIndexActivity.this.baselineActivity);
tb.putSerializable(Section.TAG, section);
tb.putSerializable(CourseActivity.BASELINE_TAG, true);
tb.putSerializable(SectionListAdapter.TAG_PLACEHOLDER, 0);
tb.putSerializable(Course.TAG, CourseIndexActivity.this.course);
intent.putExtras(tb);
startActivity(intent);
}
});
aDialog.setButton(DialogInterface.BUTTON_POSITIVE, this.getString(R.string.cancel),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
CourseIndexActivity.this.finish();
}
});
aDialog.show();
}
private void startCourseActivityByDigest(String digest) {
boolean allSectionsCompleted = true;
for (Section section : sections) {
boolean allActivitiesCompleted = true;
for (int i = 0; i < section.getActivities().size(); i++) {
Activity act = section.getActivities().get(i);
if (act.getDigest().equals(digest)) {
if ((course.getSequencingMode().equals(Course.SEQUENCING_MODE_COURSE)) &&
(!allSectionsCompleted || !allActivitiesCompleted)){
UIUtils.showAlert(this, R.string.sequencing_dialog_title, R.string.sequencing_course_message);
}else if ((course.getSequencingMode().equals(Course.SEQUENCING_MODE_SECTION))
&& (!allActivitiesCompleted)){
UIUtils.showAlert(this, R.string.sequencing_dialog_title, R.string.sequencing_section_message);
}else{
Intent intent = new Intent(this, CourseActivity.class);
Bundle tb = new Bundle();
tb.putSerializable(Section.TAG, section);
tb.putSerializable(Course.TAG, course);
tb.putSerializable(SectionListAdapter.TAG_PLACEHOLDER, i);
intent.putExtras(tb);
startActivity(intent);
}
//When we find the activity we are looking for, we can stop the search
return;
}
else{
//If is not the activity we are searching for, we check if it's completed
//(for sequencing purposes)
allActivitiesCompleted = allActivitiesCompleted && act.getCompleted();
}
}
allSectionsCompleted = allSectionsCompleted && allActivitiesCompleted;
}
}
private void showErrorMessage(){
UIUtils.showAlert(CourseIndexActivity.this, R.string.error, R.string.error_reading_xml, new Callable<Boolean>() {
public Boolean call() throws Exception {
CourseIndexActivity.this.finish();
return true;
}
});
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// update the points/badges by invalidating the menu
if(key.equalsIgnoreCase(PrefsActivity.PREF_TRIGGER_POINTS_REFRESH)){
supportInvalidateOptionsMenu();
}
}
//@Override
public void onParseComplete(CourseXMLReader parsed) {
cxr = parsed;
course.setMetaPages(cxr.getMetaPages());
sections = cxr.getSections();
boolean baselineCompleted = isBaselineCompleted();
if (!baselineCompleted){
showBaselineMessage(null);
}
initializeCourseIndex(true);
invalidateOptionsMenu();
}
//@Override
public void onParseError() { showErrorMessage(); }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_JUMPTO){
String digest = data.getStringExtra(JUMPTO_TAG);
startCourseActivityByDigest(digest);
}
}
}
}