Search in sources :

Example 1 with UpdateFrequency

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();
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) DialogInterface(android.content.DialogInterface) Spinner(android.widget.Spinner) TextView(android.widget.TextView) TimeOfDay(net.osmand.plus.liveupdates.LiveUpdatesHelper.TimeOfDay) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) OsmandSettings(net.osmand.plus.OsmandSettings) IncrementalChangesManager(net.osmand.plus.resources.IncrementalChangesManager) UpdateFrequency(net.osmand.plus.liveupdates.LiveUpdatesHelper.UpdateFrequency) LiveUpdatesHelper.preferenceUpdateFrequency(net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceUpdateFrequency) CheckBox(android.widget.CheckBox) AlarmManager(android.app.AlarmManager) AdapterView(android.widget.AdapterView) PendingIntent(android.app.PendingIntent) LiveUpdatesHelper.getPendingIntent(net.osmand.plus.liveupdates.LiveUpdatesHelper.getPendingIntent) LiveUpdatesHelper.setAlarmForPendingIntent(net.osmand.plus.liveupdates.LiveUpdatesHelper.setAlarmForPendingIntent) File(java.io.File) SwitchCompat(android.support.v7.widget.SwitchCompat) NonNull(android.support.annotation.NonNull)

Aggregations

AlarmManager (android.app.AlarmManager)1 PendingIntent (android.app.PendingIntent)1 DialogInterface (android.content.DialogInterface)1 NonNull (android.support.annotation.NonNull)1 AlertDialog (android.support.v7.app.AlertDialog)1 SwitchCompat (android.support.v7.widget.SwitchCompat)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 CheckBox (android.widget.CheckBox)1 Spinner (android.widget.Spinner)1 TextView (android.widget.TextView)1 File (java.io.File)1 OsmandSettings (net.osmand.plus.OsmandSettings)1 TimeOfDay (net.osmand.plus.liveupdates.LiveUpdatesHelper.TimeOfDay)1 UpdateFrequency (net.osmand.plus.liveupdates.LiveUpdatesHelper.UpdateFrequency)1 LiveUpdatesHelper.getPendingIntent (net.osmand.plus.liveupdates.LiveUpdatesHelper.getPendingIntent)1 LiveUpdatesHelper.preferenceUpdateFrequency (net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceUpdateFrequency)1 LiveUpdatesHelper.setAlarmForPendingIntent (net.osmand.plus.liveupdates.LiveUpdatesHelper.setAlarmForPendingIntent)1 IncrementalChangesManager (net.osmand.plus.resources.IncrementalChangesManager)1