/* * Copyright (C) 2016 mendhak * * This file is part of GPSLogger for Android. * * GPSLogger for Android is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * GPSLogger for Android is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with GPSLogger for Android. If not, see <http://www.gnu.org/licenses/>. */ package com.mendhak.gpslogger.ui.fragments.display; import android.Manifest; import android.content.Intent; import android.os.Bundle; import android.provider.Settings; import android.support.annotation.NonNull; import android.text.InputType; import com.afollestad.materialdialogs.DialogAction; import com.afollestad.materialdialogs.MaterialDialog; import com.canelmas.let.AskPermission; import com.mendhak.gpslogger.R; import com.mendhak.gpslogger.common.EventBusHook; import com.mendhak.gpslogger.common.PreferenceHelper; import com.mendhak.gpslogger.common.Session; import com.mendhak.gpslogger.common.Strings; import com.mendhak.gpslogger.common.events.CommandEvents; import com.mendhak.gpslogger.common.events.ServiceEvents; import com.mendhak.gpslogger.common.slf4j.Logs; import com.mendhak.gpslogger.loggers.Files; import com.mendhak.gpslogger.ui.Dialogs; import de.greenrobot.event.EventBus; import org.slf4j.Logger; /** * Common class for communicating with the parent for the * GpsViewCallbacks */ public abstract class GenericViewFragment extends PermissionedFragment { private static final Logger LOG = Logs.of(GenericViewFragment.class); private PreferenceHelper preferenceHelper = PreferenceHelper.getInstance(); private Session session = Session.getInstance(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); registerEventBus(); } private void registerEventBus() { EventBus.getDefault().register(this); } private void unregisterEventBus() { try { EventBus.getDefault().unregister(this); } catch (Throwable t) { //this may crash if registration did not go through. just be safe } } @Override public void onDestroy() { unregisterEventBus(); super.onDestroy(); } @EventBusHook public void onEventMainThread(ServiceEvents.LocationServicesUnavailable locationServicesUnavailable) { new MaterialDialog.Builder(getActivity()) //.title("Location services unavailable") .content(R.string.gpsprovider_unavailable) .positiveText(R.string.ok) .negativeText(R.string.cancel) .onPositive(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(@NonNull MaterialDialog materialDialog, @NonNull DialogAction dialogAction) { if(getActivity() != null){ Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); settingsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); getActivity().startActivity(settingsIntent); } } }) .show(); } @AskPermission({Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE}) public void requestToggleLogging() { if (session.isStarted()) { toggleLogging(); return; } if(! Files.isAllowedToWriteTo(preferenceHelper.getGpsLoggerFolder())){ Dialogs.alert(getString(R.string.error),getString(R.string.pref_logging_file_no_permissions) + "<br />" + preferenceHelper.getGpsLoggerFolder(), getActivity()); return; } if (preferenceHelper.shouldCreateCustomFile() && preferenceHelper.shouldAskCustomFileNameEachTime()) { new MaterialDialog.Builder(getActivity()) .title(R.string.new_file_custom_title) .content(R.string.new_file_custom_message) .positiveText(R.string.ok) .negativeText(R.string.cancel) .inputType(InputType.TYPE_CLASS_TEXT) .negativeText(R.string.cancel) .input(getString(R.string.letters_numbers), preferenceHelper.getCustomFileName(), new MaterialDialog.InputCallback() { @Override public void onInput(MaterialDialog materialDialog, CharSequence input) { LOG.info("Custom file name chosen : " + input.toString()); String chosenFileName = preferenceHelper.getCustomFileName(); if (!Strings.isNullOrEmpty(input.toString()) && !input.toString().equalsIgnoreCase(chosenFileName)) { preferenceHelper.setCustomFileName(input.toString()); } toggleLogging(); } }) .show(); } else { toggleLogging(); } } public void toggleLogging() { EventBus.getDefault().post(new CommandEvents.RequestToggle()); } }