add menu on save current track 2/2

add utility method to insert styled substring.
This commit is contained in:
madwasp79 2019-07-08 16:48:56 +03:00
parent 14b8df51b5
commit 2be7fe1c77
4 changed files with 58 additions and 27 deletions

View file

@ -21,19 +21,20 @@
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="12dp">
android:paddingBottom="@dimen/content_padding_small">
<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_height="@dimen/bottom_sheet_cancel_button_height_small"
android:layout_weight="1"
android:text="Open track"
android:text="@string/shared_string_open_track"
android:textColor="?attr/dlg_btn_secondary_text"
android:textSize="@dimen/default_desc_text_size"
android:layout_marginLeft="16dp"
android:layout_marginRight="8dp"
android:textAllCaps="false"
android:layout_marginLeft="@dimen/bottom_sheet_content_margin"
android:layout_marginRight="@dimen/bottom_sheet_content_margin_small"
/>
@ -41,14 +42,14 @@
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_height="@dimen/bottom_sheet_cancel_button_height_small"
android:layout_weight="1"
android:text="Show on the map"
android:textAllCaps="true"
android:text="@string/shared_string_show_on_map"
android:textAllCaps="false"
android:textColor="?attr/dlg_btn_primary_text"
android:textSize="@dimen/default_desc_text_size"
android:layout_marginLeft="8dp"
android:layout_marginRight="16dp"
android:layout_marginLeft="@dimen/bottom_sheet_content_margin_small"
android:layout_marginRight="@dimen/bottom_sheet_content_margin"
/>
</LinearLayout>

View file

@ -11,7 +11,8 @@
Thx - Hardy
-->
<string name="shared_string_saved">saved</string>
<string name="shared_string_open_track">Open track</string>
<string name="shared_string_track_is_saved">Track %s is 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

@ -13,6 +13,7 @@ import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Typeface;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
@ -31,10 +32,13 @@ import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.text.style.ImageSpan;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.util.DisplayMetrics;
import android.util.TypedValue;
@ -551,4 +555,24 @@ public class AndroidUtils {
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
return keyguardManager.inKeyguardRestrictedInputMode();
}
public static CharSequence insertStyledSubstring(CharSequence preFormattedString, CharSequence textToInsert, int typefaceStyle) {
final String placeholder = "%s";
if (typefaceStyle < 0 || typefaceStyle > 3) {
return preFormattedString;
}
if (!preFormattedString.toString().contains(placeholder)) {
return preFormattedString;
}
int indexOfPlaceholder = preFormattedString.toString().indexOf(placeholder);
SpannableStringBuilder ssb = new SpannableStringBuilder();
ssb.append(preFormattedString.subSequence(0, indexOfPlaceholder));
int startIndex = ssb.length();
ssb.append(textToInsert).append(" ");
ssb.setSpan(new StyleSpan(typefaceStyle), startIndex, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
startIndex = ssb.length();
ssb.append(preFormattedString.subSequence(indexOfPlaceholder+2, preFormattedString.length()));
ssb.setSpan(new StyleSpan(Typeface.NORMAL), startIndex, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return ssb;
}
}

View file

@ -5,9 +5,6 @@ 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;
@ -16,10 +13,15 @@ import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import java.io.File;
import net.osmand.AndroidUtils;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.WptPt;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.base.BottomSheetDialogFragment;
import net.osmand.plus.myplaces.AvailableGPXFragment;
import net.osmand.plus.myplaces.AvailableGPXFragment.GpxInfo;
public class OnSaveCurrentTrackFragment extends BottomSheetDialogFragment {
@ -49,15 +51,7 @@ public class OnSaveCurrentTrackFragment extends BottomSheetDialogFragment {
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);
tv.setText(AndroidUtils.insertStyledSubstring(app.getString(R.string.shared_string_track_is_saved), savedGpxName, Typeface.BOLD));
openTrackBtn.setOnClickListener(new OnClickListener() {
@Override
@ -71,6 +65,18 @@ public class OnSaveCurrentTrackFragment extends BottomSheetDialogFragment {
@Override
public void onClick(View v) {
//show track on MapActivity
GpxInfo gpxInfo = new GpxInfo();
gpxInfo.setGpx(GPXUtilities.loadGPXFile(f));
boolean e = true;
if (gpxInfo.gpx != null) {
OsmandSettings settings = app.getSettings();
WptPt loc = gpxInfo.gpx.findPointToShow();
if (loc != null) {
settings.setMapLocationToShow(loc.lat, loc.lon, settings.getLastKnownMapZoom());
e = false;
app.getSelectedGpxHelper().setGpxFileToDisplay(gpxInfo.gpx);
}
}
dismiss();
}
});
@ -84,5 +90,4 @@ public class OnSaveCurrentTrackFragment extends BottomSheetDialogFragment {
f.setArguments(b);
f.show(fragmentManager, TAG);
}
}