Added dialog for group name/color editing (waypoints)

This commit is contained in:
Alexey Kulish 2018-05-16 14:27:32 +03:00
parent 5e49d95b18
commit 4aeba9b449
5 changed files with 399 additions and 60 deletions

View file

@ -1,5 +1,6 @@
package net.osmand.plus.activities;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
@ -13,10 +14,13 @@ import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import net.osmand.AndroidUtils;
@ -66,26 +70,35 @@ public class EditFavoriteGroupDialogFragment extends MenuBottomSheetDialogFragme
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder b = new AlertDialog.Builder(getContext());
b.setTitle(R.string.favorite_group_name);
final EditText nameEditText = new EditText(getContext());
nameEditText.setText(group.name);
b.setView(nameEditText);
b.setNegativeButton(R.string.shared_string_cancel, null);
b.setPositiveButton(R.string.shared_string_save, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String name = nameEditText.getText().toString();
boolean nameChanged = !Algorithms.objectEquals(group.name, name);
if (nameChanged) {
app.getFavorites()
.editFavouriteGroup(group, name, group.color, group.visible);
updateParentFragment();
Activity activity = getActivity();
if (activity != null) {
AlertDialog.Builder b = new AlertDialog.Builder(activity);
b.setTitle(R.string.favorite_group_name);
final EditText nameEditText = new EditText(activity);
nameEditText.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
nameEditText.setText(group.name);
LinearLayout container = new LinearLayout(activity);
int sidePadding = AndroidUtils.dpToPx(activity, 24f);
int topPadding = AndroidUtils.dpToPx(activity, 4f);
container.setPadding(sidePadding, topPadding, sidePadding, topPadding);
container.addView(nameEditText);
b.setView(container);
b.setNegativeButton(R.string.shared_string_cancel, null);
b.setPositiveButton(R.string.shared_string_save, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String name = nameEditText.getText().toString();
boolean nameChanged = !Algorithms.objectEquals(group.name, name);
if (nameChanged) {
app.getFavorites()
.editFavouriteGroup(group, name, group.color, group.visible);
updateParentFragment();
}
dismiss();
}
dismiss();
}
});
b.show();
});
b.show();
}
}
})
.create();
@ -102,35 +115,38 @@ public class EditFavoriteGroupDialogFragment extends MenuBottomSheetDialogFragme
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ListPopupWindow popup = new ListPopupWindow(getActivity());
popup.setAnchorView(changeColorView);
popup.setContentWidth(AndroidUtils.dpToPx(app, 200f));
popup.setModal(true);
popup.setDropDownGravity(Gravity.END | Gravity.TOP);
if (AndroidUiHelper.isOrientationPortrait(getActivity())) {
popup.setVerticalOffset(AndroidUtils.dpToPx(app, 48f));
} else {
popup.setVerticalOffset(AndroidUtils.dpToPx(app, -48f));
}
popup.setHorizontalOffset(AndroidUtils.dpToPx(app, -6f));
final FavoriteColorAdapter colorAdapter = new FavoriteColorAdapter(getActivity());
popup.setAdapter(colorAdapter);
popup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Integer color = colorAdapter.getItem(position);
if (color != null) {
if (color != group.color) {
app.getFavorites()
.editFavouriteGroup(group, group.name, color, group.visible);
updateParentFragment();
}
}
popup.dismiss();
dismiss();
Activity activity = getActivity();
if (activity != null) {
final ListPopupWindow popup = new ListPopupWindow(activity);
popup.setAnchorView(v);
popup.setContentWidth(AndroidUtils.dpToPx(app, 200f));
popup.setModal(true);
popup.setDropDownGravity(Gravity.END | Gravity.TOP);
if (AndroidUiHelper.isOrientationPortrait(activity)) {
popup.setVerticalOffset(AndroidUtils.dpToPx(app, 48f));
} else {
popup.setVerticalOffset(AndroidUtils.dpToPx(app, -48f));
}
});
popup.show();
popup.setHorizontalOffset(AndroidUtils.dpToPx(app, -6f));
final FavoriteColorAdapter colorAdapter = new FavoriteColorAdapter(activity);
popup.setAdapter(colorAdapter);
popup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Integer color = colorAdapter.getItem(position);
if (color != null) {
if (color != group.color) {
app.getFavorites()
.editFavouriteGroup(group, group.name, color, group.visible);
updateParentFragment();
}
}
popup.dismiss();
dismiss();
}
});
popup.show();
}
}
})
.create();
@ -244,7 +260,7 @@ public class EditFavoriteGroupDialogFragment extends MenuBottomSheetDialogFragme
private final OsmandApplication app;
FavoriteColorAdapter(Context context) {
public FavoriteColorAdapter(Context context) {
super(context, R.layout.rendering_prop_menu_item);
this.app = (OsmandApplication) getContext().getApplicationContext();
init();

View file

@ -258,7 +258,8 @@ public class TrackActivity extends TabActivity {
viewPager = (LockableViewPager) findViewById(R.id.pager);
viewPager.setSwipeLocked(true);
setViewPagerAdapter(viewPager, new ArrayList<TabActivity.TabItem>());
new GPXFileLoaderTask(this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
loadGpx();
}
@Override
@ -269,6 +270,10 @@ public class TrackActivity extends TabActivity {
}
}
public void loadGpx() {
new GPXFileLoaderTask(this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
}
public OsmandApplication getMyApplication() {
return (OsmandApplication) getApplication();
}
@ -276,6 +281,9 @@ public class TrackActivity extends TabActivity {
@Override
protected void onPause() {
super.onPause();
if (viewPager.getCurrentItem() == 1) {
openPointsTab = true;
}
}
@Override
@ -387,7 +395,7 @@ public class TrackActivity extends TabActivity {
}
}
OsmandFragmentPagerAdapter pagerAdapter = (OsmandFragmentPagerAdapter) viewPager.getAdapter();
if (pagerAdapter != null) {
if (pagerAdapter != null && pagerAdapter.getCount() == 0) {
pagerAdapter.addTab(getTabIndicator(R.string.gpx_track, TrackSegmentFragment.class));
if (hasWayPoints() || hasRoutePoints()) {
pagerAdapter.addTab(getTabIndicator(R.string.points, TrackPointFragment.class));

View file

@ -4,6 +4,7 @@ import net.osmand.PlatformUtil;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Surface;
@ -14,7 +15,7 @@ import android.view.View;
*/
public class AndroidUiHelper {
public static int getScreenOrientation(Activity activity) {
public static int getScreenOrientation(@NonNull Activity activity) {
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
@ -90,12 +91,12 @@ public class AndroidUiHelper {
return false;
}
public static boolean isXLargeDevice(Activity ctx) {
public static boolean isXLargeDevice(@NonNull Activity ctx) {
int lt = (ctx.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK);
return lt == Configuration.SCREENLAYOUT_SIZE_XLARGE;
}
public static boolean isOrientationPortrait(Activity ctx) {
public static boolean isOrientationPortrait(@NonNull Activity ctx) {
int orientation = AndroidUiHelper.getScreenOrientation(ctx);
return orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;

View file

@ -0,0 +1,309 @@
package net.osmand.plus.myplaces;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.ListPopupWindow;
import android.text.TextUtils;
import android.view.ContextThemeWrapper;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import net.osmand.AndroidUtils;
import net.osmand.plus.GPXUtilities;
import net.osmand.plus.GPXUtilities.GPXFile;
import net.osmand.plus.GPXUtilities.WptPt;
import net.osmand.plus.GpxSelectionHelper.GpxDisplayGroup;
import net.osmand.plus.GpxSelectionHelper.GpxDisplayItem;
import net.osmand.plus.MapMarkersHelper;
import net.osmand.plus.MapMarkersHelper.MapMarkersGroup;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.EditFavoriteGroupDialogFragment.FavoriteColorAdapter;
import net.osmand.plus.activities.SavingTrackHelper;
import net.osmand.plus.activities.TrackActivity;
import net.osmand.plus.base.MenuBottomSheetDialogFragment;
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.util.Algorithms;
import java.io.File;
import java.lang.ref.WeakReference;
import java.util.List;
public class EditTrackGroupDialogFragment extends MenuBottomSheetDialogFragment {
public static final String TAG = EditTrackGroupDialogFragment.class.getSimpleName();
private GpxDisplayGroup group;
@Override
public void createMenuItems(Bundle savedInstanceState) {
final OsmandApplication app = getMyApplication();
if (group == null) {
return;
}
items.add(new TitleItem(getCategoryName(app, group.getName())));
BaseBottomSheetItem editNameItem = new SimpleBottomSheetItem.Builder()
.setIcon(getContentIcon(R.drawable.ic_action_edit_dark))
.setTitle(getString(R.string.edit_name))
.setLayoutId(R.layout.bottom_sheet_item_simple)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Activity activity = getActivity();
if (activity != null) {
AlertDialog.Builder b = new AlertDialog.Builder(activity);
b.setTitle(R.string.favorite_group_name);
final EditText nameEditText = new EditText(activity);
nameEditText.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
nameEditText.setText(group.getName());
LinearLayout container = new LinearLayout(activity);
int sidePadding = AndroidUtils.dpToPx(activity, 24f);
int topPadding = AndroidUtils.dpToPx(activity, 4f);
container.setPadding(sidePadding, topPadding, sidePadding, topPadding);
container.addView(nameEditText);
b.setView(container);
b.setNegativeButton(R.string.shared_string_cancel, null);
b.setPositiveButton(R.string.shared_string_save, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String name = nameEditText.getText().toString();
boolean nameChanged = !Algorithms.objectEquals(group.getName(), name);
if (nameChanged) {
TrackActivity trackActivity = getTrackActivity();
if (trackActivity != null) {
new UpdateGpxCategoryTask(trackActivity, group, name)
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
dismiss();
}
});
b.show();
}
}
})
.create();
items.add(editNameItem);
final int themeRes = nightMode ? R.style.OsmandDarkTheme : R.style.OsmandLightTheme;
final View changeColorView = View.inflate(new ContextThemeWrapper(getContext(), themeRes),
R.layout.change_fav_color, null);
((ImageView) changeColorView.findViewById(R.id.change_color_icon))
.setImageDrawable(getContentIcon(R.drawable.ic_action_appearance));
updateColorView((ImageView) changeColorView.findViewById(R.id.colorImage));
BaseBottomSheetItem changeColorItem = new BaseBottomSheetItem.Builder()
.setCustomView(changeColorView)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Activity activity = getActivity();
if (activity != null) {
final ListPopupWindow popup = new ListPopupWindow(activity);
popup.setAnchorView(v);
popup.setContentWidth(AndroidUtils.dpToPx(app, 200f));
popup.setModal(true);
popup.setDropDownGravity(Gravity.END | Gravity.TOP);
if (AndroidUiHelper.isOrientationPortrait(activity)) {
popup.setVerticalOffset(AndroidUtils.dpToPx(app, 48f));
} else {
popup.setVerticalOffset(AndroidUtils.dpToPx(app, -48f));
}
popup.setHorizontalOffset(AndroidUtils.dpToPx(app, -6f));
final FavoriteColorAdapter colorAdapter = new FavoriteColorAdapter(activity);
popup.setAdapter(colorAdapter);
popup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Integer color = colorAdapter.getItem(position);
if (color != null) {
if (color != group.getColor()) {
TrackActivity trackActivity = getTrackActivity();
if (trackActivity != null) {
new UpdateGpxCategoryTask(trackActivity, group, color)
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
}
popup.dismiss();
dismiss();
}
});
popup.show();
}
}
})
.create();
items.add(changeColorItem);
}
@Override
public void onResume() {
super.onResume();
if (group == null) {
dismiss();
}
}
@Nullable
private TrackActivity getTrackActivity() {
Activity activity = getActivity();
if (activity != null && activity instanceof TrackActivity) {
return (TrackActivity) activity;
}
return null;
}
private static String getCategoryName(@NonNull Context ctx, String category) {
return Algorithms.isEmpty(category) ? ctx.getString(R.string.waypoints) : category;
}
private void updateColorView(ImageView colorImageView) {
int color = (group.getColor() == 0 ? getResources().getColor(R.color.gpx_color_point) : group.getColor()) | 0xff000000;
if (color == 0) {
colorImageView.setImageDrawable(getContentIcon(R.drawable.ic_action_circle));
} else {
colorImageView.setImageDrawable(getMyApplication().getIconsCache().getPaintedIcon(R.drawable.ic_action_circle, color));
}
}
public static void showInstance(FragmentManager fragmentManager, GpxDisplayGroup group) {
EditTrackGroupDialogFragment f = (EditTrackGroupDialogFragment) fragmentManager
.findFragmentByTag(EditTrackGroupDialogFragment.TAG);
if (f == null ) {
f = new EditTrackGroupDialogFragment();
f.group = group;
f.show(fragmentManager, EditTrackGroupDialogFragment.TAG);
}
}
private static class UpdateGpxCategoryTask extends AsyncTask<Void, Void, Void> {
private OsmandApplication app;
private WeakReference<TrackActivity> activityRef;
private GpxDisplayGroup group;
private String newCategory;
private Integer newColor;
private ProgressDialog progressDialog;
private boolean wasUpdated = false;
private UpdateGpxCategoryTask(@NonNull TrackActivity activity, @NonNull GpxDisplayGroup group) {
this.app = (OsmandApplication) activity.getApplication();
activityRef = new WeakReference<>(activity);
this.group = group;
}
UpdateGpxCategoryTask(@NonNull TrackActivity activity, @NonNull GpxDisplayGroup group,
@NonNull String newCategory) {
this(activity, group);
this.newCategory = newCategory;
}
UpdateGpxCategoryTask(@NonNull TrackActivity activity, @NonNull GpxDisplayGroup group,
@NonNull Integer newColor) {
this(activity, group);
this.newColor = newColor;
}
@Override
protected void onPreExecute() {
TrackActivity activity = activityRef.get();
if (activity != null) {
progressDialog = new ProgressDialog(activity);
progressDialog.setTitle(EditTrackGroupDialogFragment.getCategoryName(app, group.getName()));
progressDialog.setMessage(newCategory != null ? "Changing name" : "Changing color");
progressDialog.setCancelable(false);
progressDialog.show();
GPXFile gpxFile = group.getGpx();
if (gpxFile != null) {
SavingTrackHelper savingTrackHelper = app.getSavingTrackHelper();
List<GpxDisplayItem> items = group.getModifiableList();
String prevCategory = group.getName();
boolean emptyCategory = TextUtils.isEmpty(prevCategory);
for (GpxDisplayItem item : items) {
WptPt wpt = item.locationStart;
if (wpt != null) {
boolean update = false;
if (emptyCategory) {
if (TextUtils.isEmpty(wpt.category)) {
update = true;
}
} else if (prevCategory.equals(wpt.category)) {
update = true;
}
if (update) {
wasUpdated = true;
String category = newCategory != null ? newCategory : wpt.category;
int color = newColor != null ? newColor : wpt.colourARGB;
if (gpxFile.showCurrentTrack) {
savingTrackHelper.updatePointData(wpt, wpt.getLatitude(), wpt.getLongitude(),
System.currentTimeMillis(), wpt.desc, wpt.name, category, color);
} else {
gpxFile.updateWptPt(wpt, wpt.getLatitude(), wpt.getLongitude(),
System.currentTimeMillis(), wpt.desc, wpt.name, category, color);
}
}
}
}
}
}
}
@Override
protected Void doInBackground(Void... voids) {
GPXFile gpxFile = group.getGpx();
if (gpxFile != null && !gpxFile.showCurrentTrack && wasUpdated) {
GPXUtilities.writeGpxFile(new File(gpxFile.path), gpxFile, app);
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
GPXFile gpxFile = group.getGpx();
if (gpxFile != null && wasUpdated) {
syncGpx(gpxFile);
}
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
TrackActivity activity = activityRef.get();
if (activity != null) {
activity.loadGpx();
}
}
private void syncGpx(GPXFile gpxFile) {
MapMarkersHelper markersHelper = app.getMapMarkersHelper();
MapMarkersGroup group = markersHelper.getMarkersGroup(gpxFile);
if (group != null) {
markersHelper.runSynchronization(group);
}
}
}
}

View file

@ -11,6 +11,7 @@ import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AlertDialog;
@ -38,6 +39,8 @@ import android.widget.ListView;
import android.widget.TextView;
import net.osmand.AndroidUtils;
import net.osmand.Collator;
import net.osmand.OsmAndCollator;
import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon;
import net.osmand.data.PointDescription;
@ -68,7 +71,6 @@ import net.osmand.util.Algorithms;
import java.io.File;
import java.lang.ref.WeakReference;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@ -681,8 +683,7 @@ public class TrackPointFragment extends OsmandExpandableListFragment implements
Comparator<String> comparator;
PointGPXAdapter() {
final Collator collator = Collator.getInstance();
collator.setStrength(Collator.SECONDARY);
final Collator collator = OsmAndCollator.primaryCollator();
comparator = new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
@ -773,6 +774,9 @@ public class TrackPointFragment extends OsmandExpandableListFragment implements
break;
}
}
List<GpxDisplayItem> headerGroupItems = headerGroup.getModifiableList();
headerGroupItems.clear();
headerGroupItems.addAll(values);
itemGroups.put(headerGroup, values);
groups.add(headerGroup);
} else {
@ -925,9 +929,9 @@ public class TrackPointFragment extends OsmandExpandableListFragment implements
options.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Activity activity = getActivity();
FragmentActivity activity = getActivity();
if (activity != null) {
// todo
EditTrackGroupDialogFragment.showInstance(activity.getSupportFragmentManager(), group);
}
}
});
@ -1011,7 +1015,7 @@ public class TrackPointFragment extends OsmandExpandableListFragment implements
options.setImageDrawable(iconsCache.getThemedIcon(R.drawable.ic_overflow_menu_white));
}
if (childPosition == 0) {
row.findViewById(R.id.divider).setVisibility(View.VISIBLE);
row.findViewById(R.id.divider).setVisibility(View.GONE);
row.findViewById(R.id.list_divider).setVisibility(View.GONE);
} else {
row.findViewById(R.id.divider).setVisibility(View.GONE);
@ -1146,7 +1150,8 @@ public class TrackPointFragment extends OsmandExpandableListFragment implements
for (GpxDisplayItem i : g.getModifiableList()) {
if (i.name.toLowerCase().contains(cs)) {
filter.add(i);
} else if (i.locationStart != null && cs.equals(i.locationStart.category)) {
} else if (i.locationStart != null && !TextUtils.isEmpty(i.locationStart.category)
&& i.locationStart.category.toLowerCase().contains(cs)) {
filter.add(i.locationStart.category);
}
}