-
-
Notifications
You must be signed in to change notification settings - Fork 982
/
LiveUpdatesFragment.java
666 lines (595 loc) · 23.8 KB
/
LiveUpdatesFragment.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
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
package net.osmand.plus.liveupdates;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.SwitchCompat;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import net.osmand.map.WorldRegion;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.Version;
import net.osmand.plus.activities.LocalIndexHelper;
import net.osmand.plus.activities.LocalIndexInfo;
import net.osmand.plus.activities.OsmandBaseExpandableListAdapter;
import net.osmand.plus.base.BaseOsmAndFragment;
import net.osmand.plus.download.ui.AbstractLoadLocalIndexTask;
import net.osmand.plus.inapp.InAppHelper;
import net.osmand.plus.inapp.InAppHelper.InAppListener;
import net.osmand.plus.resources.IncrementalChangesManager;
import net.osmand.util.Algorithms;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.DEFAULT_LAST_CHECK;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.TimeOfDay;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.UpdateFrequency;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.formatDateTime;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.getNameToDisplay;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.getPendingIntent;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceLastCheck;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceLiveUpdatesOn;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceTimeOfDayToUpdate;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceUpdateFrequency;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.setAlarmForPendingIntent;
public class LiveUpdatesFragment extends BaseOsmAndFragment implements InAppListener {
public static final int TITLE = R.string.live_updates;
private static final int SUBSCRIPTION_SETTINGS = 5;
public static final Comparator<LocalIndexInfo> LOCAL_INDEX_INFO_COMPARATOR = new Comparator<LocalIndexInfo>() {
@Override
public int compare(LocalIndexInfo lhs, LocalIndexInfo rhs) {
return lhs.getName().compareTo(rhs.getName());
}
};
private View subscriptionHeader;
private ExpandableListView listView;
private LocalIndexesAdapter adapter;
private AsyncTask<Void, LocalIndexInfo, List<LocalIndexInfo>> loadLocalIndexesTask;
private ProgressBar progressBar;
private boolean processing;
public InAppHelper getInAppHelper() {
Activity activity = getActivity();
if (activity instanceof OsmLiveActivity) {
return ((OsmLiveActivity) activity).getInAppHelper();
} else {
return null;
}
}
private boolean isProcessing() {
return processing;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_live_updates, container, false);
listView = (ExpandableListView) view.findViewById(android.R.id.list);
View bottomShadowView = inflater.inflate(R.layout.card_bottom_divider, listView, false);
listView.addFooterView(bottomShadowView);
adapter = new LocalIndexesAdapter(this);
listView.setAdapter(adapter);
listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
if (!processing && InAppHelper.isSubscribedToLiveUpdates()) {
final FragmentManager fragmentManager = getChildFragmentManager();
LiveUpdatesSettingsDialogFragment
.createInstance(adapter.getChild(groupPosition, childPosition).getFileName())
.show(fragmentManager, "settings");
return true;
} else {
return false;
}
}
});
progressBar = (ProgressBar) view.findViewById(R.id.progress);
if (!Version.isDeveloperVersion(getMyApplication())) {
subscriptionHeader = inflater.inflate(R.layout.live_updates_header, listView, false);
updateSubscriptionHeader();
listView.addHeaderView(subscriptionHeader);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0 && !processing && InAppHelper.isSubscribedToLiveUpdates()) {
SubscriptionFragment subscriptionFragment = new SubscriptionFragment();
subscriptionFragment.setEditMode(true);
subscriptionFragment.show(getChildFragmentManager(), SubscriptionFragment.TAG);
}
}
});
}
loadLocalIndexesTask = new LoadLocalIndexTask(adapter, this).execute();
return view;
}
public void updateSubscriptionHeader() {
View subscriptionBanner = subscriptionHeader.findViewById(R.id.subscription_banner);
View subscriptionInfo = subscriptionHeader.findViewById(R.id.subscription_info);
if (getSettings().LIVE_UPDATES_PURCHASED.get()) {
ImageView statusIcon = (ImageView) subscriptionHeader.findViewById(R.id.statusIcon);
TextView statusTextView = (TextView) subscriptionHeader.findViewById(R.id.statusTextView);
TextView regionNameTextView = (TextView) subscriptionHeader.findViewById(R.id.regionTextView);
statusTextView.setText(getString(R.string.osm_live_active));
statusIcon.setImageDrawable(getMyApplication().getIconsCache().getThemedIcon(R.drawable.ic_action_done));
String countryName = getSettings().BILLING_USER_COUNTRY.get();
if (Algorithms.isEmpty(countryName)) {
WorldRegion world = getMyApplication().getRegions().getWorldRegion();
countryName = world.getLocaleName();
}
regionNameTextView.setText(countryName);
subscriptionBanner.setVisibility(View.GONE);
subscriptionInfo.setVisibility(View.VISIBLE);
} else {
Button readMoreBtn = (Button) subscriptionHeader.findViewById(R.id.read_more_button);
readMoreBtn.setEnabled(!processing);
readMoreBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("http://osmand.net/osm_live.php");
Intent goToOsmLive = new Intent(Intent.ACTION_VIEW, uri);
startActivity(goToOsmLive);
}
});
Button subscriptionButton = (Button) subscriptionHeader.findViewById(R.id.subscription_button);
subscriptionButton.setEnabled(!processing);
subscriptionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SubscriptionFragment subscriptionFragment = new SubscriptionFragment();
subscriptionFragment.show(getChildFragmentManager(), SubscriptionFragment.TAG);
}
});
subscriptionBanner.setVisibility(View.VISIBLE);
subscriptionInfo.setVisibility(View.GONE);
}
}
@Override
public void onResume() {
super.onResume();
InAppHelper helper = getInAppHelper();
if (helper != null) {
enableProgress();
helper.addListener(this);
helper.start(false);
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
loadLocalIndexesTask.cancel(true);
}
@Override
public void onDestroy() {
super.onDestroy();
InAppHelper helper = getInAppHelper();
if (helper != null) {
helper.removeListener(this);
}
}
public void notifyLiveUpdatesChanged() {
if (adapter != null) {
adapter.notifyLiveUpdatesChanged();
}
}
@SuppressWarnings("deprecation")
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if (getSettings().LIVE_UPDATES_PURCHASED.get() && !Version.isDeveloperVersion(getMyApplication())) {
ActionBar actionBar = getMyActivity().getSupportActionBar();
if (actionBar != null) {
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
}
SubMenu split = menu.addSubMenu(R.string.shared_string_more_actions);
split.setIcon(R.drawable.ic_overflow_menu_white);
MenuItemCompat.setShowAsAction(split.getItem(), MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
MenuItem item = split.add(0, SUBSCRIPTION_SETTINGS, 0, R.string.osm_live_subscription_settings);
MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == SUBSCRIPTION_SETTINGS && !processing) {
SubscriptionFragment subscriptionFragment = new SubscriptionFragment();
subscriptionFragment.setEditMode(true);
subscriptionFragment.show(getChildFragmentManager(), SubscriptionFragment.TAG);
return true;
}
return super.onOptionsItemSelected(item);
}
protected class LocalIndexesAdapter extends OsmandBaseExpandableListAdapter {
public static final int SHOULD_UPDATE_GROUP_POSITION = 0;
public static final int SHOULD_NOT_UPDATE_GROUP_POSITION = 1;
final ArrayList<LocalIndexInfo> dataShouldUpdate = new ArrayList<>();
final ArrayList<LocalIndexInfo> dataShouldNotUpdate = new ArrayList<>();
final LiveUpdatesFragment fragment;
final Context ctx;
public LocalIndexesAdapter(LiveUpdatesFragment fragment) {
this.fragment = fragment;
ctx = fragment.getActivity();
}
public void add(LocalIndexInfo info) {
OsmandSettings.CommonPreference<Boolean> preference = preferenceLiveUpdatesOn(
info.getFileName(), getSettings());
if (preference.get()) {
dataShouldUpdate.add(info);
} else {
dataShouldNotUpdate.add(info);
}
}
public void notifyLiveUpdatesChanged() {
Set<LocalIndexInfo> changedSet = new HashSet<>();
for (LocalIndexInfo localIndexInfo : dataShouldUpdate) {
OsmandSettings.CommonPreference<Boolean> preference =
preferenceLiveUpdatesOn(localIndexInfo.getFileName(), getSettings());
if (!preference.get()) {
changedSet.add(localIndexInfo);
}
}
dataShouldUpdate.removeAll(changedSet);
dataShouldNotUpdate.addAll(changedSet);
changedSet.clear();
for (LocalIndexInfo localIndexInfo : dataShouldNotUpdate) {
OsmandSettings.CommonPreference<Boolean> preference =
preferenceLiveUpdatesOn(localIndexInfo.getFileName(), getSettings());
if (preference.get()) {
changedSet.add(localIndexInfo);
}
}
dataShouldUpdate.addAll(changedSet);
dataShouldNotUpdate.removeAll(changedSet);
notifyDataSetChanged();
expandAllGroups();
}
public void sort() {
Collections.sort(dataShouldUpdate, LOCAL_INDEX_INFO_COMPARATOR);
Collections.sort(dataShouldNotUpdate, LOCAL_INDEX_INFO_COMPARATOR);
}
@Override
public LocalIndexInfo getChild(int groupPosition, int childPosition) {
if (groupPosition == 0) {
return dataShouldUpdate.get(childPosition);
} else if (groupPosition == 1) {
return dataShouldNotUpdate.get(childPosition);
} else {
throw new IllegalArgumentException("unexpected group position:" + groupPosition);
}
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// it would be unusable to have 10000 local indexes
return groupPosition * 10000 + childPosition;
}
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
LocalFullMapsViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(ctx);
convertView = inflater.inflate(R.layout.local_index_live_updates_list_item, parent, false);
viewHolder = new LocalFullMapsViewHolder(convertView, fragment);
convertView.setTag(viewHolder);
} else {
viewHolder = (LocalFullMapsViewHolder) convertView.getTag();
}
viewHolder.bindLocalIndexInfo(getChild(groupPosition, childPosition).getFileName(), isLastChild);
return convertView;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View view = convertView;
String group = getGroup(groupPosition);
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(ctx);
view = inflater.inflate(R.layout.list_group_title_with_switch, parent, false);
}
TextView nameView = ((TextView) view.findViewById(R.id.title));
nameView.setText(group);
view.setOnClickListener(null);
final SwitchCompat liveUpdatesSwitch = (SwitchCompat) view.findViewById(R.id.toggle_item);
View topShadowView = view.findViewById(R.id.bottomShadowView);
if (groupPosition == SHOULD_UPDATE_GROUP_POSITION) {
topShadowView.setVisibility(View.GONE);
liveUpdatesSwitch.setVisibility(View.VISIBLE);
OsmandApplication application = (OsmandApplication) ctx.getApplicationContext();
final OsmandSettings settings = application.getSettings();
liveUpdatesSwitch.setChecked(settings.IS_LIVE_UPDATES_ON.get());
liveUpdatesSwitch.setEnabled(!processing);
liveUpdatesSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
if (InAppHelper.isSubscribedToLiveUpdates()) {
settings.IS_LIVE_UPDATES_ON.set(true);
enableLiveUpdates(true);
} else {
liveUpdatesSwitch.setChecked(false);
getMyApplication().showToastMessage(getString(R.string.osm_live_ask_for_purchase));
}
} else {
settings.IS_LIVE_UPDATES_ON.set(false);
enableLiveUpdates(false);
}
}
});
} else {
topShadowView.setVisibility(View.VISIBLE);
liveUpdatesSwitch.setVisibility(View.GONE);
}
View divider = view.findViewById(R.id.divider);
if (getChildrenCount(groupPosition) == 0) {
divider.setVisibility(View.GONE);
} else {
divider.setVisibility(View.VISIBLE);
}
return view;
}
private void enableLiveUpdates(boolean enable) {
AlarmManager alarmMgr = (AlarmManager) getActivity()
.getSystemService(Context.ALARM_SERVICE);
for (LocalIndexInfo li : dataShouldUpdate) {
String fileName = li.getFileName();
PendingIntent alarmIntent = getPendingIntent(getActivity(),
fileName);
if (enable) {
final OsmandSettings.CommonPreference<Integer> updateFrequencyPreference =
preferenceUpdateFrequency(fileName, getSettings());
final OsmandSettings.CommonPreference<Integer> timeOfDayPreference =
preferenceTimeOfDayToUpdate(fileName, getSettings());
UpdateFrequency updateFrequency = UpdateFrequency.values()[updateFrequencyPreference.get()];
TimeOfDay timeOfDayToUpdate = TimeOfDay.values()[timeOfDayPreference.get()];
setAlarmForPendingIntent(alarmIntent, alarmMgr, updateFrequency, timeOfDayToUpdate);
} else {
alarmMgr.cancel(alarmIntent);
}
}
}
@Override
public int getChildrenCount(int groupPosition) {
if (groupPosition == SHOULD_UPDATE_GROUP_POSITION) {
return dataShouldUpdate.size();
} else if (groupPosition == SHOULD_NOT_UPDATE_GROUP_POSITION) {
return dataShouldNotUpdate.size();
} else {
throw new IllegalArgumentException("unexpected group position:" + groupPosition);
}
}
@Override
public String getGroup(int groupPosition) {
if (groupPosition == SHOULD_UPDATE_GROUP_POSITION) {
return getString(R.string.download_live_updates);
} else if (groupPosition == SHOULD_NOT_UPDATE_GROUP_POSITION) {
return getString(R.string.available_maps);
} else {
throw new IllegalArgumentException("unexpected group position:" + groupPosition);
}
}
@Override
public int getGroupCount() {
return dataShouldNotUpdate.size() == 0 ? 1 : 2;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
private void expandAllGroups() {
for (int i = 0; i < adapter.getGroupCount(); i++) {
listView.expandGroup(i);
}
}
private static class LocalFullMapsViewHolder {
public static final int UPDATES_ENABLED_ITEM_HEIGHT = 72;
public static final int UPDATES_DISABLED_ITEM_HEIGHT = 50;
private final ImageView icon;
private final TextView nameTextView;
private final TextView subheaderTextView;
private final TextView descriptionTextView;
private final ImageButton options;
private final LiveUpdatesFragment fragment;
private final View view;
private final int secondaryColor;
private final View divider;
private LocalFullMapsViewHolder(View view, LiveUpdatesFragment context) {
icon = (ImageView) view.findViewById(R.id.icon);
nameTextView = (TextView) view.findViewById(R.id.nameTextView);
subheaderTextView = (TextView) view.findViewById(R.id.subheaderTextView);
descriptionTextView = (TextView) view.findViewById(R.id.descriptionTextView);
options = (ImageButton) view.findViewById(R.id.options);
this.view = view;
this.fragment = context;
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getActivity().getTheme();
theme.resolveAttribute(android.R.attr.textColorSecondary, typedValue, true);
secondaryColor = typedValue.data;
divider = view.findViewById(R.id.divider);
}
public void bindLocalIndexInfo(@NonNull final String item, boolean isLastChild) {
OsmandApplication context = fragment.getMyActivity().getMyApplication();
final OsmandSettings.CommonPreference<Boolean> shouldUpdatePreference =
preferenceLiveUpdatesOn(item, fragment.getSettings());
IncrementalChangesManager changesManager = context.getResourceManager().getChangesManager();
nameTextView.setText(getNameToDisplay(item, fragment.getMyActivity()));
AbsListView.LayoutParams layoutParams = (AbsListView.LayoutParams) view.getLayoutParams();
if (shouldUpdatePreference.get()) {
final Integer frequencyId = preferenceUpdateFrequency(item, fragment.getSettings()).get();
final Integer timeOfDateToUpdateId = preferenceTimeOfDayToUpdate(item, fragment.getSettings()).get();
final UpdateFrequency frequency = UpdateFrequency.values()[frequencyId];
final TimeOfDay timeOfDay = TimeOfDay.values()[timeOfDateToUpdateId];
subheaderTextView.setVisibility(View.VISIBLE);
String subheaderText = fragment.getString(frequency.getLocalizedId());
if (frequency != UpdateFrequency.HOURLY) {
subheaderText += " • " + fragment.getString(timeOfDay.getLocalizedId());
}
subheaderTextView.setText(subheaderText);
subheaderTextView.setTextColor(fragment.getActivity().getResources()
.getColor(R.color.osmand_orange));
icon.setImageDrawable(fragment.getIcon(R.drawable.ic_map, R.color.osmand_orange));
options.setImageDrawable(getSecondaryColorPaintedIcon(R.drawable.ic_overflow_menu_white));
layoutParams.height = (int) dpToPx(view.getContext(), UPDATES_ENABLED_ITEM_HEIGHT);
} else {
subheaderTextView.setVisibility(View.GONE);
icon.setImageDrawable(getSecondaryColorPaintedIcon(R.drawable.ic_map));
options.setImageDrawable(getSecondaryColorPaintedIcon(R.drawable.ic_action_plus));
layoutParams.height = (int) dpToPx(view.getContext(), UPDATES_DISABLED_ITEM_HEIGHT);
}
view.setLayoutParams(layoutParams);
final String fileNameWithoutExtension =
Algorithms.getFileNameWithoutExtension(new File(item));
final long timestamp = changesManager.getTimestamp(fileNameWithoutExtension);
final long lastCheck = preferenceLastCheck(item, fragment.getSettings()).get();
OsmandSettings.CommonPreference<Boolean> liveUpdateOn = preferenceLiveUpdatesOn(item, fragment.getSettings());
if(liveUpdateOn.get() && lastCheck != DEFAULT_LAST_CHECK) {
String lastCheckString = formatDateTime(fragment.getActivity(), lastCheck );
descriptionTextView.setText(context.getString(R.string.last_update, lastCheckString));
} else {
String lastCheckString = formatDateTime(fragment.getActivity(), timestamp );
descriptionTextView.setText(context.getString(R.string.last_map_change, lastCheckString));
}
if (!fragment.isProcessing() && InAppHelper.isSubscribedToLiveUpdates()) {
final View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
final FragmentManager fragmentManager = fragment.getChildFragmentManager();
LiveUpdatesSettingsDialogFragment.createInstance(item).show(fragmentManager, "settings");
}
};
options.setOnClickListener(clickListener);
options.setEnabled(true);
} else {
options.setEnabled(false);
}
if (isLastChild) {
divider.setVisibility(View.GONE);
} else {
divider.setVisibility(View.VISIBLE);
}
}
private Drawable getSecondaryColorPaintedIcon(@DrawableRes int drawable) {
return fragment.getMyActivity().getMyApplication().getIconsCache()
.getPaintedIcon(drawable, secondaryColor);
}
}
public static class LoadLocalIndexTask
extends AsyncTask<Void, LocalIndexInfo, List<LocalIndexInfo>>
implements AbstractLoadLocalIndexTask {
//private List<LocalIndexInfo> result;
private LocalIndexesAdapter adapter;
private LiveUpdatesFragment fragment;
private LocalIndexHelper helper;
public LoadLocalIndexTask(LocalIndexesAdapter adapter,
LiveUpdatesFragment fragment) {
this.adapter = adapter;
this.fragment = fragment;
helper = new LocalIndexHelper(fragment.getMyActivity().getMyApplication());
}
@Override
protected List<LocalIndexInfo> doInBackground(Void... params) {
return helper.getLocalFullMaps(this);
}
@Override
public void loadFile(LocalIndexInfo... loaded) {
publishProgress(loaded);
}
@Override
protected void onProgressUpdate(LocalIndexInfo... values) {
for (LocalIndexInfo localIndexInfo : values) {
if (localIndexInfo.getType() == LocalIndexHelper.LocalIndexType.MAP_DATA
&& !(localIndexInfo.getFileName().toLowerCase().contains("world"))) {
adapter.add(localIndexInfo);
}
}
adapter.notifyDataSetChanged();
fragment.expandAllGroups();
}
@Override
protected void onPostExecute(List<LocalIndexInfo> result) {
//this.result = result;
adapter.sort();
}
}
private void enableProgress() {
processing = true;
progressBar.setVisibility(View.VISIBLE);
updateSubscriptionHeader();
adapter.notifyDataSetChanged();
}
private void disableProgress() {
processing = false;
progressBar.setVisibility(View.INVISIBLE);
updateSubscriptionHeader();
adapter.notifyDataSetChanged();
}
public static float dpToPx(final Context context, final float dp) {
return dp * context.getResources().getDisplayMetrics().density;
}
@Override
public void onError(String error) {
disableProgress();
}
@Override
public void onGetItems() {
getSettings().LIVE_UPDATES_PURCHASED.set(InAppHelper.isSubscribedToLiveUpdates());
if (!InAppHelper.isSubscribedToLiveUpdates()) {
getSettings().IS_LIVE_UPDATES_ON.set(false);
adapter.enableLiveUpdates(false);
}
disableProgress();
}
@Override
public void onItemPurchased(String sku) {
if (InAppHelper.getSkuLiveUpdates().equals(sku)) {
updateSubscriptionHeader();
}
}
@Override
public void showProgress() {
enableProgress();
}
@Override
public void dismissProgress() {
disableProgress();
}
}