Live updates work in progress

This commit is contained in:
GaidamakUA 2015-12-14 18:51:52 +02:00
parent b0e33667d5
commit e190a7a285
4 changed files with 113 additions and 7 deletions

View file

@ -288,5 +288,7 @@
<action android:name="android.app.action.DEVICE_ADMIN_DISABLED" />
</intent-filter>
</receiver>
<activity android:name=".liveupdates.LiveUpdatesActivity"/>
<receiver android:name="net.osmand.plus.liveupdates.LiveUpdatesAlarmReceiver"/>
</application>
</manifest>

View file

@ -68,6 +68,16 @@
android:layout_column="1"
android:layout_gravity="right"
android:entries="@array/update_frequencies_array"/>
<Spinner
android:id="@+id/updateTimesOfDaySpinner"
android:layout_width="wrap_content"
android:layout_column="1"
android:layout_row="4"
android:layout_gravity="right"
android:visibility="gone"
android:entries="@array/update_times_of_day"/>
</GridLayout>
<View

View file

@ -0,0 +1,17 @@
package net.osmand.plus.liveupdates;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import net.osmand.PlatformUtil;
import org.apache.commons.logging.Log;
public class LiveUpdatesAlarmReceiver extends BroadcastReceiver {
private static final Log LOG = PlatformUtil.getLog(LiveUpdatesAlarmReceiver.class);
@Override
public void onReceive(Context context, Intent intent) {
LOG.debug("onReceive");
}
}

View file

@ -1,7 +1,11 @@
package net.osmand.plus.liveupdates;
import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
@ -9,6 +13,8 @@ import android.support.v7.app.AlertDialog;
import android.support.v7.widget.SwitchCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
@ -18,6 +24,10 @@ import net.osmand.plus.activities.OsmandActionBarActivity;
public class SettingsDialogFragment extends DialogFragment {
public static final String LOCAL_INDEX = "local_index";
public static final int UPDATE_HOURLY = 0;
public static final int UPDATE_DAILY = 1;
public static final int UPDATE_WEEKLY = 2;
public static final String UPDATE_TIMES = "_update_times";
@NonNull
@Override
@ -28,15 +38,38 @@ public class SettingsDialogFragment extends DialogFragment {
View view = LayoutInflater.from(getActivity())
.inflate(R.layout.dialog_live_updates_item_settings, null);
final SwitchCompat liveUpdatesSwitch = (SwitchCompat) view.findViewById(R.id.liveUpdatesSwitch);
final Spinner updateFrequencySpinner = (Spinner) view.findViewById(R.id.updateFrequencySpinner);
final Spinner updateTimesOfDaySpinner = (Spinner) view.findViewById(R.id.updateTimesOfDaySpinner);
final OsmandSettings.CommonPreference<Boolean> liveUpdatePreference =
preferenceForLocalIndex(LiveUpdatesFragment.LIVE_UPDATES_ON_POSTFIX, localIndexInfo);
preferenceForLocalIndex(localIndexInfo);
final OsmandSettings.CommonPreference<Integer> 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<Boolean> 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<Boolean> preferenceForLocalIndex(LocalIndexInfo item) {
final String settingId = item.getFileName() + LiveUpdatesFragment.LIVE_UPDATES_ON_POSTFIX;
return getSettings().registerBooleanPreference(settingId, false);
}
private OsmandSettings.CommonPreference<Integer> 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
}
}