add menu on save current track 1/2

This commit is contained in:
madwasp79 2019-07-08 12:04:07 +03:00
parent 727b6ba399
commit 14b8df51b5
6 changed files with 195 additions and 13 deletions

View file

@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:osmand="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:background="?attr/bg_color"
android:orientation="vertical">
<TextView
android:id="@+id/saved_track_name_string"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/content_padding"
tools:text="Track 2019-02-26 12-52 saved"
android:textColor="?android:textColorPrimary"
android:textSize="@dimen/default_list_text_size"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="12dp">
<Button
android:id="@+id/open_track_button"
android:background="?attr/dlg_btn_secondary"
android:layout_width="0dp"
android:layout_height="@dimen/bottom_sheet_cancel_button_height"
android:layout_weight="1"
android:text="Open track"
android:textColor="?attr/dlg_btn_secondary_text"
android:textSize="@dimen/default_desc_text_size"
android:layout_marginLeft="16dp"
android:layout_marginRight="8dp"
/>
<Button
android:id="@+id/show_on_map_button"
android:background="?attr/dlg_btn_primary"
android:layout_width="0dp"
android:layout_height="@dimen/bottom_sheet_cancel_button_height"
android:layout_weight="1"
android:text="Show on the map"
android:textAllCaps="true"
android:textColor="?attr/dlg_btn_primary_text"
android:textSize="@dimen/default_desc_text_size"
android:layout_marginLeft="8dp"
android:layout_marginRight="16dp"
/>
</LinearLayout>
</LinearLayout>

View file

@ -11,6 +11,7 @@
Thx - Hardy
-->
<string name="shared_string_saved">saved</string>
<string name="turn_screen_on_router">Wake on turn</string>
<string name="turn_screen_on_time_descr">Set the time for which the screen will turn on.</string>
<string name="turn_screen_on_sensor">Use proximity sensor</string>

View file

@ -743,7 +743,7 @@ public class AppInitializer implements IProgress {
if (System.currentTimeMillis() - timeUpdated >= 1000 * 60 * 30) {
startTask(app.getString(R.string.saving_gpx_tracks), -1);
try {
warnings.addAll(app.savingTrackHelper.saveDataToGpx(app.getAppCustomization().getTracksDir()));
warnings.addAll(app.savingTrackHelper.saveDataToGpx(app.getAppCustomization().getTracksDir()).getWarnings());
} catch (RuntimeException e) {
warnings.add(e.getMessage());
}

View file

@ -188,10 +188,11 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
}
/**
* @return warnings
* @return warnings, filenames
*/
public synchronized List<String> saveDataToGpx(File dir ) {
List<String> warnings = new ArrayList<String>();
public synchronized SaveGpxResult saveDataToGpx(File dir) {
List<String> warnings = new ArrayList<>();
List<String> filenames = new ArrayList<>();
dir.mkdirs();
if (dir.getParentFile().canWrite()) {
if (dir.exists()) {
@ -199,6 +200,7 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
// save file
for (final String f : data.keySet()) {
log.debug("Filename: " + f);
File fout = new File(dir, f + ".gpx"); //$NON-NLS-1$
if (!data.get(f).isEmpty()) {
WptPt pt = data.get(f).findPointToShow();
@ -214,6 +216,7 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
}
String fileName = f + "_" + new SimpleDateFormat("HH-mm_EEE", Locale.US).format(new Date(pt.time)); //$NON-NLS-1$
filenames.add(fileName);
fout = new File(targetDir, fileName + ".gpx"); //$NON-NLS-1$
int ind = 1;
while (fout.exists()) {
@ -224,7 +227,7 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
Exception warn = GPXUtilities.writeGpxFile(fout, data.get(f));
if (warn != null) {
warnings.add(warn.getMessage());
return warnings;
return new SaveGpxResult(warnings, new ArrayList<String>());
}
GPXFile gpx = data.get(f);
@ -257,7 +260,7 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
currentTrack.getModifiablePointsToDisplay().clear();
currentTrack.getModifiableGpxFile().modifiedTime = System.currentTimeMillis();
prepareCurrentTrackForRecording();
return warnings;
return new SaveGpxResult(warnings, filenames);
}
public Map<String, GPXFile> collectRecordedData() {
@ -685,5 +688,24 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
public SelectedGpxFile getCurrentTrack() {
return currentTrack;
}
public class SaveGpxResult {
public SaveGpxResult(List<String> warnings, List<String> filenames) {
this.warnings = warnings;
this.filenames = filenames;
}
List<String> warnings;
List<String> filenames;
public List<String> getWarnings() {
return warnings;
}
public List<String> getFilenames() {
return filenames;
}
}
}

View file

@ -0,0 +1,88 @@
package net.osmand.plus.monitoring;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.StyleSpan;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import java.io.File;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.base.BottomSheetDialogFragment;
import net.osmand.plus.myplaces.AvailableGPXFragment;
public class OnSaveCurrentTrackFragment extends BottomSheetDialogFragment {
public static final String TAG = "OnSaveCurrentTrackBottomSheetFragment";
public static final String SAVED_TRACK_KEY = "saved_track_filename";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final OsmandApplication app = getMyApplication();
Bundle args = getArguments();
String savedGpxName = "";
if (args != null && args.containsKey(SAVED_TRACK_KEY)) {
savedGpxName = args.getString(SAVED_TRACK_KEY, "");
} else {
dismiss();
}
final File f = new File (app.getAppCustomization().getTracksDir() +"/"+ savedGpxName + ".gpx");
final boolean nightMode = !app.getSettings().isLightContent();
final int themeRes = nightMode ? R.style.OsmandDarkTheme : R.style.OsmandLightTheme;
View mainView = View
.inflate(new ContextThemeWrapper(getContext(), themeRes), R.layout.fragment_on_save_current_track, container);
TextView tv = mainView.findViewById(R.id.saved_track_name_string);
Button openTrackBtn = mainView.findViewById(R.id.open_track_button);
Button showOnMapBtn = mainView.findViewById(R.id.show_on_map_button);
SpannableStringBuilder ssb = new SpannableStringBuilder();
ssb.append(app.getResources().getString(R.string.shared_string_gpx_track)).append(" ");
int startIndex = ssb.length();
ssb.append(savedGpxName).append(" ");
ssb.setSpan(new StyleSpan(Typeface.BOLD), startIndex, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
startIndex = ssb.length();
ssb.append(app.getResources().getString(R.string.shared_string_saved));
ssb.setSpan(new StyleSpan(Typeface.NORMAL), startIndex, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(ssb);
openTrackBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AvailableGPXFragment.openTrack(getActivity(), f);
dismiss();
}
});
showOnMapBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//show track on MapActivity
dismiss();
}
});
return mainView;
}
public static void showInstance(FragmentManager fragmentManager, String filename) {
OnSaveCurrentTrackFragment f = new OnSaveCurrentTrackFragment();
Bundle b = new Bundle();
b.putString(SAVED_TRACK_KEY, filename);
f.setArguments(b);
f.show(fragmentManager, TAG);
}
}

View file

@ -31,6 +31,7 @@ import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.SavingTrackHelper;
import net.osmand.plus.activities.SavingTrackHelper.SaveGpxResult;
import net.osmand.plus.dashboard.tools.DashFragmentData;
import net.osmand.plus.views.MapInfoLayer;
import net.osmand.plus.views.OsmandMapLayer.DrawSettings;
@ -40,6 +41,7 @@ import net.osmand.plus.views.mapwidgets.TextInfoWidget;
import java.util.List;
import gnu.trove.list.array.TIntArrayList;
import net.osmand.util.Algorithms;
public class OsmandMonitoringPlugin extends OsmandPlugin {
public static final String ID = "osmand.monitoring";
@ -288,7 +290,7 @@ public class OsmandMonitoringPlugin extends OsmandPlugin {
int which = holder[0];
int item = items.get(which);
if(item == R.string.save_current_track){
saveCurrentTrack();
saveCurrentTrack(map);
} else if(item == R.string.gpx_monitoring_start) {
if (app.getLocationProvider().checkGPSEnabled(map)) {
startGPXMonitoring(map, showTrackSelection);
@ -330,11 +332,20 @@ public class OsmandMonitoringPlugin extends OsmandPlugin {
}
public void saveCurrentTrack() {
saveCurrentTrack(null);
saveCurrentTrack(null, null);
}
public void saveCurrentTrack(@Nullable final Runnable onComplete) {
saveCurrentTrack(onComplete, null);
}
public void saveCurrentTrack(@Nullable final Runnable onComplete) {
app.getTaskManager().runInBackground(new OsmAndTaskRunnable<Void, Void, Void>() {
public void saveCurrentTrack(@Nullable final Activity activity) {
saveCurrentTrack(null, activity);
}
public void saveCurrentTrack(@Nullable final Runnable onComplete, @Nullable final
Activity activity) {
app.getTaskManager().runInBackground(new OsmAndTaskRunnable<Void, Void, SaveGpxResult>() {
@Override
protected void onPreExecute() {
@ -343,11 +354,12 @@ public class OsmandMonitoringPlugin extends OsmandPlugin {
}
@Override
protected Void doInBackground(Void... params) {
protected SaveGpxResult doInBackground(Void... params) {
try {
SavingTrackHelper helper = app.getSavingTrackHelper();
helper.saveDataToGpx(app.getAppCustomization().getTracksDir());
SaveGpxResult result = helper.saveDataToGpx(app.getAppCustomization().getTracksDir());
helper.close();
return result;
} catch (Exception e) {
e.printStackTrace();
}
@ -355,10 +367,14 @@ public class OsmandMonitoringPlugin extends OsmandPlugin {
}
@Override
protected void onPostExecute(Void aVoid) {
protected void onPostExecute(SaveGpxResult result) {
isSaving = false;
app.getNotificationHelper().refreshNotifications();
updateControl();
if (activity instanceof MapActivity && !Algorithms.isEmpty(result.getFilenames())) {
OnSaveCurrentTrackFragment.showInstance(((MapActivity) activity)
.getSupportFragmentManager(), result.getFilenames().get(0));
}
if (onComplete != null) {
onComplete.run();
}