Skip to content

Commit

Permalink
Advanced troubleshooting in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
andreynovikov committed Feb 1, 2024
1 parent cf87f3f commit ddb338e
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 140 deletions.
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@
<action android:name="mobi.maptrek.action.CENTER_ON_COORDINATES" />
<action android:name="mobi.maptrek.action.NAVIGATE_TO_OBJECT" />
<action android:name="mobi.maptrek.action.NAVIGATE_TO_POINT" />
<action android:name="mobi.maptrek.action.RESET_ADVICES" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
Expand Down
37 changes: 37 additions & 0 deletions app/src/main/java/mobi/maptrek/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,43 @@ public static void setHighlightedType(int type) {
saveInt(PREF_HIGHLIGHTED_TYPE, type);
}

public static void resetMapState() {
remove(PREF_LATITUDE);
remove(PREF_LONGITUDE);
remove(PREF_MAP_SCALE);
remove(PREF_MAP_BEARING);
remove(PREF_MAP_TILT);
remove(PREF_MAP_3D_BUILDINGS);
remove(PREF_MAP_CONTOURS);
remove(PREF_MAP_HILLSHADES);
remove(PREF_MAP_GRID);
remove(PREF_BITMAP_MAP);
remove(PREF_LOCATION_STATE);
remove(PREF_PREVIOUS_LOCATION_STATE);
remove(PREF_TRACKING_STATE);
remove(PREF_ACTION_PANEL_STATE);
remove(PREF_NAVIGATION_WAYPOINT);
remove(PREF_NAVIGATION_LATITUDE);
remove(PREF_NAVIGATION_LONGITUDE);
remove(PREF_NAVIGATION_PROXIMITY);
remove(PREF_NAVIGATION_ROUTE);
remove(PREF_NAVIGATION_ROUTE_POINT);
remove(PREF_NAVIGATION_ROUTE_DIRECTION);
remove(PREF_GAUGES);
remove(PREF_NIGHT_MODE_STATE);
remove(PREF_MAP_STYLE);
remove(PREF_ACTIVITY);
remove(PREF_MAP_FONT_SIZE);
remove(PREF_MAP_USER_SCALE);
remove(PREF_LANGUAGE);
remove(PREF_HIDE_MAP_OBJECTS);
remove(PREF_BITMAP_MAP_TRANSPARENCY);
remove(PREF_REMEMBERED_SCALE);
remove(PREF_AUTO_TILT);
remove(PREF_HIDE_SYSTEM_UI);
remove(PREF_HIGHLIGHTED_TYPE);
}

/**
Returns total running time in minutes
*/
Expand Down
5 changes: 0 additions & 5 deletions app/src/main/java/mobi/maptrek/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -828,11 +828,6 @@ protected void onNewIntent(Intent intent) {
setMapLocation(position.getGeoPoint());
} else if ("mobi.maptrek.action.NAVIGATE_TO_OBJECT".equals(action)) {
startNavigation(intent.getLongExtra(NavigationService.EXTRA_ID, 0L));
} else if ("mobi.maptrek.action.RESET_ADVICES".equals(action)) {
mBackgroundHandler.postDelayed(Configuration::resetAdviceState, 10000); // Delay reset so that advices are not shown immediately after reset
Snackbar.make(mViews.coordinatorLayout, R.string.msgAdvicesReset, Snackbar.LENGTH_LONG)
.setAnchorView(mViews.actionPanel)
.show();
} else if ("geo".equals(scheme)) {
Uri uri = intent.getData();
if (uri == null)
Expand Down
46 changes: 30 additions & 16 deletions app/src/main/java/mobi/maptrek/MapTrek.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Andrey Novikov
* Copyright 2024 Andrey Novikov
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
Expand All @@ -21,7 +21,6 @@
import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
Expand Down Expand Up @@ -134,6 +133,7 @@ public class MapTrek extends Application {
@Override
public void onCreate() {
super.onCreate();
logger.info("Trekarta application starting");
mSelf = this;

PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
Expand Down Expand Up @@ -211,22 +211,17 @@ public static MapTrek getApplication() {
}

/** @noinspection unused*/
public void restart(@NonNull Context context, Class<?> cls) {
Intent intent = new Intent(context, cls);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
/*
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE);
AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (mgr != null)
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, pendingIntent);
*/
if (context instanceof Activity)
((Activity) context).finish();
startActivity(intent);
/*
public void restart() {
logger.info("Application restart initiated");
Configuration.commit();
//Context ctx = getApplicationContext();
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage(getPackageName());
if (intent != null) {
Intent mainIntent = Intent.makeRestartActivityTask(intent.getComponent());
startActivity(mainIntent);
}
Runtime.getRuntime().exit(0);
*/
}

@TargetApi(26)
Expand Down Expand Up @@ -437,6 +432,7 @@ public void setBitmapLayerMaps(@NonNull List<MapFile> bitmapLayerMaps) {

@SuppressWarnings("SameParameterValue")
private void copyAsset(String asset, File outFile) throws IOException {
logger.debug("copyAsset({})", asset);
InputStream in = getAssets().open(asset);
//noinspection IOStreamConstructor
OutputStream out = new FileOutputStream(outFile);
Expand Down Expand Up @@ -470,6 +466,24 @@ public String getUserNotification() {
return notification;
}

synchronized public void removeMapDatabase() {
// close databases
if (mHillshadeHelper != null) {
mHillshadeHelper.close();
mHillshadeHelper = null;
mHillshadeDatabase = null;
}
if (mDetailedMapHelper != null) {
mDetailedMapHelper.close();
mDetailedMapHelper = null;
mDetailedMapDatabase = null;
}
File dbFile = new File(getExternalFilesDir("native"), Index.WORLDMAP_FILENAME);
logger.info("Detailed map database deleted: {}", dbFile.delete());
File hsFile = new File(getExternalFilesDir("native"), Index.HILLSHADE_FILENAME);
logger.info("Hillshade database deleted: {}", hsFile.delete());
}

synchronized public void optionallyCloseMapDatabase(UUID id) {
WorkManager workManager = WorkManager.getInstance(this);
ListenableFuture<List<WorkInfo>> workInfos = workManager.getWorkInfosByTag(MapWorker.TAG);
Expand Down
115 changes: 0 additions & 115 deletions app/src/main/java/mobi/maptrek/fragments/Settings.java

This file was deleted.

99 changes: 99 additions & 0 deletions app/src/main/java/mobi/maptrek/fragments/preferences/Advanced.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright 2024 Andrey Novikov
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

package mobi.maptrek.fragments.preferences;

import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.preference.Preference;

import com.google.android.material.snackbar.BaseTransientBottomBar;
import com.google.android.material.snackbar.Snackbar;

import mobi.maptrek.Configuration;
import mobi.maptrek.MainActivity;
import mobi.maptrek.MapTrek;
import mobi.maptrek.R;

public class Advanced extends BasePreferences {
@Override
public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
addPreferencesFromResource(R.xml.preferences_advanced);

Preference purgeMaps = findPreference("purge_maps");
if (purgeMaps != null)
purgeMaps.setOnPreferenceClickListener(preference -> {
CoordinatorLayout coordinatorLayout = ((MainActivity) requireActivity()).getCoordinatorLayout();
Snackbar snackbar = Snackbar.make(coordinatorLayout, R.string.msgPurgeMaps, Snackbar.LENGTH_LONG)
.setAnchorView(getView())
.addCallback(new BaseTransientBottomBar.BaseCallback<Snackbar>() {
public void onDismissed(Snackbar snackbar, @DismissEvent int event) {
super.onDismissed(snackbar, event);
if (event == DISMISS_EVENT_ACTION)
return;
requireActivity().finish();
MapTrek.getApplication().removeMapDatabase();
MapTrek.getApplication().restart();
}
})
.setAction(R.string.actionUndo, view -> {
// do nothing, we just do not precede with destructive operation
});
TextView snackbarTextView = snackbar.getView().findViewById(com.google.android.material.R.id.snackbar_text);
snackbarTextView.setMaxLines(99);
snackbar.show();
return true;
});

Preference resetMap = findPreference("reset_map");
if (resetMap != null)
resetMap.setOnPreferenceClickListener(preference -> {
CoordinatorLayout coordinatorLayout = ((MainActivity) requireActivity()).getCoordinatorLayout();
Snackbar snackbar = Snackbar.make(coordinatorLayout, R.string.msgMapReset, Snackbar.LENGTH_LONG)
.setAnchorView(getView())
.addCallback(new BaseTransientBottomBar.BaseCallback<Snackbar>() {
public void onDismissed(Snackbar snackbar, @DismissEvent int event) {
super.onDismissed(snackbar, event);
if (event == DISMISS_EVENT_ACTION)
return;
Configuration.resetMapState();
MapTrek.getApplication().restart();
}
})
.setAction(R.string.actionUndo, view -> {
// do nothing, we just do not precede with destructive operation
});
TextView snackbarTextView = snackbar.getView().findViewById(com.google.android.material.R.id.snackbar_text);
snackbarTextView.setMaxLines(99);
snackbar.show();
return true;
});

Preference resetAdvices = findPreference("reset_advices");
if (resetAdvices != null)
resetAdvices.setOnPreferenceClickListener(preference -> {
Configuration.resetAdviceState();
CoordinatorLayout coordinatorLayout = ((MainActivity) requireActivity()).getCoordinatorLayout();
Snackbar.make(coordinatorLayout, R.string.msgAdvicesReset, Snackbar.LENGTH_LONG)
.setAnchorView(getView())
.show();
return true;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
addPreferencesFromResource(R.xml.preferences);

Preference rate = (Preference) findPreference("preferences_rate");
Preference rate = (Preference) findPreference("rate");
if (rate != null) {
if (Configuration.ratingActionPerformed() || (Configuration.getRunningTime() < 240)) {
getPreferenceScreen().removePreference(rate);
Expand Down
Loading

0 comments on commit ddb338e

Please sign in to comment.