From e190a7a2850879142ae970316a24933532814140 Mon Sep 17 00:00:00 2001 From: GaidamakUA Date: Mon, 14 Dec 2015 18:51:52 +0200 Subject: [PATCH] Live updates work in progress --- OsmAnd/AndroidManifest.xml | 2 + .../dialog_live_updates_item_settings.xml | 10 ++ .../liveupdates/LiveUpdatesAlarmReceiver.java | 17 ++++ .../liveupdates/SettingsDialogFragment.java | 91 +++++++++++++++++-- 4 files changed, 113 insertions(+), 7 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesAlarmReceiver.java diff --git a/OsmAnd/AndroidManifest.xml b/OsmAnd/AndroidManifest.xml index 50beb09926..a11f634a7a 100644 --- a/OsmAnd/AndroidManifest.xml +++ b/OsmAnd/AndroidManifest.xml @@ -288,5 +288,7 @@ + + \ No newline at end of file diff --git a/OsmAnd/res/layout/dialog_live_updates_item_settings.xml b/OsmAnd/res/layout/dialog_live_updates_item_settings.xml index 834a31c466..766763f84c 100644 --- a/OsmAnd/res/layout/dialog_live_updates_item_settings.xml +++ b/OsmAnd/res/layout/dialog_live_updates_item_settings.xml @@ -68,6 +68,16 @@ android:layout_column="1" android:layout_gravity="right" android:entries="@array/update_frequencies_array"/> + + + liveUpdatePreference = - preferenceForLocalIndex(LiveUpdatesFragment.LIVE_UPDATES_ON_POSTFIX, localIndexInfo); + preferenceForLocalIndex(localIndexInfo); + final OsmandSettings.CommonPreference updateFrequencies = + preferenceUpdateTimes(localIndexInfo); liveUpdatesSwitch.setChecked(liveUpdatePreference.get()); builder.setView(view) .setPositiveButton(R.string.shared_string_save, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { + final int updateFrequencyInt = updateFrequencySpinner.getSelectedItemPosition(); + updateFrequencies.set(updateFrequencyInt); + AlarmManager alarmMgr = (AlarmManager) getActivity() + .getSystemService(Context.ALARM_SERVICE); + + Intent intent = new Intent(getActivity(), LiveUpdatesAlarmReceiver.class); + PendingIntent alarmIntent = PendingIntent.getBroadcast(getActivity(), 0, intent, 0); + + UpdateFrequencies updateFrequency = UpdateFrequencies.values()[updateFrequencyInt]; + switch (updateFrequency) { + case HOURLY: + alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, + 1000, 60 * 60 * 1000, alarmIntent); + break; + case DAILY: + case WEEKLY: + updateTimesOfDaySpinner.setVisibility(View.VISIBLE); + break; + } liveUpdatePreference.set(liveUpdatesSwitch.isChecked()); getLiveUpdatesFragment().notifyLiveUpdatesChanged(); } @@ -48,6 +81,29 @@ public class SettingsDialogFragment extends DialogFragment { getLiveUpdatesFragment().runLiveUpdate(localIndexInfo); } }); + + updateFrequencySpinner.setSelection(updateFrequencies.get()); + updateFrequencySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + UpdateFrequencies updateFrequency = UpdateFrequencies.values()[position]; + switch (updateFrequency) { + case HOURLY: + updateTimesOfDaySpinner.setVisibility(View.GONE); + break; + case DAILY: + case WEEKLY: + updateTimesOfDaySpinner.setVisibility(View.VISIBLE); + break; + } + } + + @Override + public void onNothingSelected(AdapterView parent) { + + } + }); + return builder.create(); } @@ -55,12 +111,22 @@ public class SettingsDialogFragment extends DialogFragment { return (LiveUpdatesFragment) getParentFragment(); } - private OsmandSettings.CommonPreference preferenceForLocalIndex(String idPostfix, - LocalIndexInfo item) { - final OsmandApplication myApplication = ((OsmandActionBarActivity) this.getActivity()).getMyApplication(); - final OsmandSettings settings = myApplication.getSettings(); - final String settingId = item.getFileName() + idPostfix; - return settings.registerBooleanPreference(settingId, false); + private OsmandSettings.CommonPreference preferenceForLocalIndex(LocalIndexInfo item) { + final String settingId = item.getFileName() + LiveUpdatesFragment.LIVE_UPDATES_ON_POSTFIX; + return getSettings().registerBooleanPreference(settingId, false); + } + + private OsmandSettings.CommonPreference preferenceUpdateTimes(LocalIndexInfo item) { + final String settingId = item.getFileName() + UPDATE_TIMES; + return getSettings().registerIntPreference(settingId, UpdateFrequencies.HOURLY.ordinal()); + } + + private OsmandSettings getSettings() { + return getMyApplication().getSettings(); + } + + private OsmandApplication getMyApplication() { + return ((OsmandActionBarActivity) this.getActivity()).getMyApplication(); } public static SettingsDialogFragment createInstance(LocalIndexInfo localIndexInfo) { @@ -70,4 +136,15 @@ public class SettingsDialogFragment extends DialogFragment { fragment.setArguments(args); return fragment; } + + public static enum UpdateFrequencies { + HOURLY, + DAILY, + WEEKLY + } + + public static enum TimesOfDay { + MORNING, + NIGHT + } }