use of net.osmand.plus.liveupdates.LiveUpdatesHelper.UpdateFrequency in project Osmand by osmandapp.
the class LiveUpdatesSettingsDialogFragment method onCreateDialog.
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final String fileName = getArguments().getString(LOCAL_INDEX_FILE_NAME);
assert fileName != null;
View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_live_updates_item_settings, null);
final TextView regionNameTextView = (TextView) view.findViewById(R.id.regionNameTextView);
final TextView lastMapChangeTextView = (TextView) view.findViewById(R.id.lastMapChangeTextView);
final TextView lastUpdateTextView = (TextView) view.findViewById(R.id.lastUpdateTextView);
final SwitchCompat liveUpdatesSwitch = (SwitchCompat) view.findViewById(R.id.toggle_item);
final CheckBox downloadOverWiFiCheckBox = (CheckBox) view.findViewById(R.id.downloadOverWiFiSwitch);
final Spinner updateFrequencySpinner = (Spinner) view.findViewById(R.id.updateFrequencySpinner);
final Spinner updateTimesOfDaySpinner = (Spinner) view.findViewById(R.id.updateTimesOfDaySpinner);
final View updateTimesOfDayLayout = view.findViewById(R.id.updateTimesOfDayLayout);
final TextView sizeTextView = (TextView) view.findViewById(R.id.sizeTextView);
regionNameTextView.setText(getNameToDisplay(fileName, getMyActivity()));
final String fileNameWithoutExtension = Algorithms.getFileNameWithoutExtension(new File(fileName));
final IncrementalChangesManager changesManager = getMyApplication().getResourceManager().getChangesManager();
final long timestamp = changesManager.getTimestamp(fileNameWithoutExtension);
String lastUpdateDate = formatDateTime(getActivity(), timestamp);
lastMapChangeTextView.setText(getString(R.string.last_map_change, lastUpdateDate));
final long lastCheck = preferenceLastCheck(fileName, getSettings()).get();
OsmandSettings.CommonPreference<Boolean> preference = preferenceLiveUpdatesOn(fileName, getSettings());
if (preference.get() && lastCheck != DEFAULT_LAST_CHECK) {
String lastCheckString = formatDateTime(getActivity(), lastCheck);
lastUpdateTextView.setText(getString(R.string.last_update, lastCheckString));
} else {
lastUpdateTextView.setVisibility(View.GONE);
}
final OsmandSettings.CommonPreference<Boolean> liveUpdatePreference = preferenceForLocalIndex(fileName, getSettings());
final OsmandSettings.CommonPreference<Boolean> downloadViaWiFiPreference = preferenceDownloadViaWiFi(fileName, getSettings());
final OsmandSettings.CommonPreference<Integer> updateFrequencyPreference = preferenceUpdateFrequency(fileName, getSettings());
final OsmandSettings.CommonPreference<Integer> timeOfDayPreference = preferenceTimeOfDayToUpdate(fileName, getSettings());
downloadOverWiFiCheckBox.setChecked(!liveUpdatePreference.get() || downloadViaWiFiPreference.get());
sizeTextView.setText(getUpdatesSize(fileNameWithoutExtension, changesManager));
TimeOfDay[] timeOfDays = TimeOfDay.values();
String[] timeOfDaysStrings = new String[timeOfDays.length];
for (int i = 0; i < timeOfDays.length; i++) {
timeOfDaysStrings[i] = getString(timeOfDays[i].getLocalizedId());
}
updateTimesOfDaySpinner.setAdapter(new ArrayAdapter<>(getActivity(), R.layout.action_spinner_item, timeOfDaysStrings));
updateTimesOfDaySpinner.setSelection(timeOfDayPreference.get());
UpdateFrequency[] updateFrequencies = UpdateFrequency.values();
String[] updateFrequenciesStrings = new String[updateFrequencies.length];
for (int i = 0; i < updateFrequencies.length; i++) {
updateFrequenciesStrings[i] = getString(updateFrequencies[i].getLocalizedId());
}
refreshTimeOfDayLayout(UpdateFrequency.values()[updateFrequencyPreference.get()], updateTimesOfDayLayout);
updateFrequencySpinner.setAdapter(new ArrayAdapter<>(getActivity(), R.layout.action_spinner_item, updateFrequenciesStrings));
updateFrequencySpinner.setSelection(updateFrequencyPreference.get());
updateFrequencySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
refreshTimeOfDayLayout(UpdateFrequency.values()[position], updateTimesOfDayLayout);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
builder.setView(view).setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (liveUpdatePreference.get() != liveUpdatesSwitch.isChecked()) {
liveUpdatePreference.set(liveUpdatesSwitch.isChecked());
if (!liveUpdatesSwitch.isChecked()) {
long updatesSize = changesManager.getUpdatesSize(fileNameWithoutExtension);
if (updatesSize != 0) {
ClearUpdatesDialogFragment.createInstance(fileName).show(getParentFragment().getChildFragmentManager(), null);
}
}
}
downloadViaWiFiPreference.set(downloadOverWiFiCheckBox.isChecked());
final int updateFrequencyInt = updateFrequencySpinner.getSelectedItemPosition();
updateFrequencyPreference.set(updateFrequencyInt);
AlarmManager alarmMgr = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
PendingIntent alarmIntent = getPendingIntent(getActivity(), fileName);
final int timeOfDayInt = updateTimesOfDaySpinner.getSelectedItemPosition();
timeOfDayPreference.set(timeOfDayInt);
if (liveUpdatesSwitch.isChecked() && getSettings().IS_LIVE_UPDATES_ON.get()) {
runLiveUpdate(getActivity(), fileName, false);
UpdateFrequency updateFrequency = UpdateFrequency.values()[updateFrequencyInt];
TimeOfDay timeOfDayToUpdate = TimeOfDay.values()[timeOfDayInt];
setAlarmForPendingIntent(alarmIntent, alarmMgr, updateFrequency, timeOfDayToUpdate);
} else {
alarmMgr.cancel(alarmIntent);
}
getLiveUpdatesFragment().notifyLiveUpdatesChanged();
}
}).setNegativeButton(R.string.shared_string_cancel, null).setNeutralButton(R.string.update_now, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
runLiveUpdate(getActivity(), fileName, true);
sizeTextView.setText(getUpdatesSize(fileNameWithoutExtension, changesManager));
}
});
return builder.create();
}
Aggregations