commit
d7dbe591e3
10 changed files with 548 additions and 35 deletions
|
@ -6,10 +6,16 @@
|
|||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:orientation="vertical">
|
||||
|
||||
<LinearLayout
|
||||
<FrameLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="?attr/bg_color"
|
||||
android:background="?attr/bg_color">
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/saveButtonContainer"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="?android:selectableItemBackground"
|
||||
android:minHeight="56dp"
|
||||
android:orientation="horizontal">
|
||||
|
||||
|
@ -29,13 +35,15 @@
|
|||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:layout_marginLeft="@dimen/content_padding"
|
||||
android:layout_marginRight="@dimen/content_padding"
|
||||
android:layout_marginStart="@dimen/content_padding"
|
||||
android:layout_marginEnd="@dimen/content_padding" />
|
||||
android:layout_marginLeft="@dimen/content_padding"
|
||||
android:layout_marginEnd="@dimen/content_padding"
|
||||
android:layout_marginRight="@dimen/content_padding" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</FrameLayout>
|
||||
|
||||
<ImageView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
|
|
|
@ -36,7 +36,6 @@
|
|||
android:layout_marginLeft="16dp"
|
||||
android:layout_marginRight="16dp"
|
||||
android:scaleType="centerInside"
|
||||
android:tint="?attr/default_icon_color"
|
||||
osmand:srcCompat="@drawable/ic_map"
|
||||
android:layout_marginStart="16dp"
|
||||
android:layout_marginEnd="16dp" />
|
||||
|
|
|
@ -11,6 +11,10 @@
|
|||
Thx - Hardy
|
||||
|
||||
-->
|
||||
<string name="profiles_for_action_not_found">Profiles selected for this action not found.</string>
|
||||
<string name="change_application_profile">Change application profile</string>
|
||||
<string name="shared_string_add_profile">Add profile</string>
|
||||
<string name="quick_action_switch_profile_descr">Taping action button will switch between selected profiles.</string>
|
||||
<string name="back_to_editing">Back to editing</string>
|
||||
<string name="reset_deafult_order">Restore default items order</string>
|
||||
<string name="add_edit_favorite">Add / Edit Favorite</string>
|
||||
|
|
|
@ -4,6 +4,7 @@ package net.osmand.plus.dialogs;
|
|||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.res.ColorStateList;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.view.ContextThemeWrapper;
|
||||
import android.view.LayoutInflater;
|
||||
|
@ -22,6 +23,7 @@ import androidx.core.widget.NestedScrollView;
|
|||
import androidx.fragment.app.FragmentManager;
|
||||
|
||||
import net.osmand.AndroidUtils;
|
||||
import net.osmand.plus.ApplicationMode;
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.OsmandSettings;
|
||||
import net.osmand.plus.R;
|
||||
|
@ -34,8 +36,7 @@ import net.osmand.plus.quickaction.QuickAction;
|
|||
import net.osmand.plus.quickaction.QuickActionRegistry;
|
||||
import net.osmand.plus.quickaction.SwitchableAction;
|
||||
import net.osmand.plus.quickaction.actions.MapStyleAction;
|
||||
import net.osmand.plus.render.RendererRegistry;
|
||||
import net.osmand.render.RenderingRulesStorage;
|
||||
import net.osmand.plus.quickaction.actions.SwitchProfileAction;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -161,22 +162,38 @@ public class SelectMapViewQuickActionsBottomSheet extends MenuBottomSheetDialogF
|
|||
List<String> stylesList = mapStyleAction.getFilteredStyles();
|
||||
for (String entry : stylesList) {
|
||||
boolean selected = entry.equals(selectedItem);
|
||||
createItemRow(selected, counter, mapStyleAction.getTranslatedItemName(context, entry), entry);
|
||||
createItemRow(selected, counter, getContentIcon(action.getIconRes()),
|
||||
mapStyleAction.getTranslatedItemName(context, entry), entry);
|
||||
counter++;
|
||||
}
|
||||
} else if (action instanceof SwitchProfileAction) {
|
||||
SwitchProfileAction switchProfileAction = (SwitchProfileAction) action;
|
||||
List<String> profilesKeys = (List<String>) switchProfileAction.loadListFromParams();
|
||||
for (String key : profilesKeys) {
|
||||
ApplicationMode appMode = ApplicationMode.valueOfStringKey(key, null);
|
||||
if (appMode != null) {
|
||||
boolean selected = key.equals(selectedItem);
|
||||
int iconId = appMode.getIconRes();
|
||||
int colorId = appMode.getIconColorInfo().getColor(nightMode);
|
||||
Drawable icon = getIcon(iconId, colorId);
|
||||
String translatedName = appMode.toHumanString();
|
||||
createItemRow(selected, counter, icon, translatedName, key);
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
} else if (action instanceof SwitchableAction) {
|
||||
SwitchableAction switchableAction = (SwitchableAction) action;
|
||||
List<Pair<String, String>> sources = (List<Pair<String, String>>) switchableAction.loadListFromParams();
|
||||
for (Pair<String, String> entry : sources) {
|
||||
String tag = entry.first;
|
||||
boolean selected = tag.equals(selectedItem);
|
||||
createItemRow(selected, counter, entry.second, tag);
|
||||
createItemRow(selected, counter, getContentIcon(action.getIconRes()), entry.second, tag);
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void createItemRow(boolean selected, int counter, String text, String tag) {
|
||||
private void createItemRow(boolean selected, int counter, Drawable icon, String text, String tag) {
|
||||
View view = itemsContainer.getChildAt(counter);
|
||||
view.setTag(tag);
|
||||
view.setOnClickListener(getOnClickListener());
|
||||
|
@ -189,7 +206,7 @@ public class SelectMapViewQuickActionsBottomSheet extends MenuBottomSheetDialogF
|
|||
rb.setChecked(selected);
|
||||
CompoundButtonCompat.setButtonTintList(rb, rbColorList);
|
||||
ImageView imageView = (ImageView) view.findViewById(R.id.icon);
|
||||
imageView.setImageDrawable(getContentIcon(action.getIconRes()));
|
||||
imageView.setImageDrawable(icon);
|
||||
}
|
||||
|
||||
@ColorInt
|
||||
|
|
|
@ -10,6 +10,7 @@ public class ProfileDataObject implements Comparable<ProfileDataObject> {
|
|||
private int iconRes;
|
||||
private String stringKey;
|
||||
private boolean isSelected;
|
||||
private boolean isEnabled;
|
||||
private ProfileIconColors iconColor;
|
||||
|
||||
public ProfileDataObject(String name, String description, String stringKey, int iconRes, boolean isSelected, ProfileIconColors iconColor) {
|
||||
|
@ -41,6 +42,14 @@ public class ProfileDataObject implements Comparable<ProfileDataObject> {
|
|||
isSelected = selected;
|
||||
}
|
||||
|
||||
public boolean isEnabled() {
|
||||
return isEnabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
isEnabled = enabled;
|
||||
}
|
||||
|
||||
public String getStringKey() {
|
||||
return stringKey;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,179 @@
|
|||
package net.osmand.plus.profiles;
|
||||
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.CompoundButton;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import net.osmand.CallbackWithObject;
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.UiUtilities;
|
||||
import net.osmand.plus.activities.MapActivity;
|
||||
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem;
|
||||
import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem;
|
||||
import net.osmand.plus.settings.NavigationFragment;
|
||||
import net.osmand.plus.settings.bottomsheets.BasePreferenceBottomSheet;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class SelectMultipleProfilesBottomSheet extends BasePreferenceBottomSheet {
|
||||
|
||||
public static final String TAG = SelectMultipleProfilesBottomSheet.class.getSimpleName();
|
||||
public static final String SELECTED_KEYS = "selected_keys";
|
||||
public static final String DISABLED_KEYS = "disabled_keys";
|
||||
|
||||
private List<ProfileDataObject> profiles = new ArrayList<>();
|
||||
private List<String> selectedProfiles;
|
||||
private List<String> disabledProfiles;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
Bundle args = getArguments();
|
||||
if (savedInstanceState != null) {
|
||||
readBundle(savedInstanceState);
|
||||
} else if (args != null) {
|
||||
readBundle(args);
|
||||
}
|
||||
}
|
||||
|
||||
private void readBundle(Bundle bundle) {
|
||||
selectedProfiles = bundle.getStringArrayList(SELECTED_KEYS);
|
||||
disabledProfiles = bundle.getStringArrayList(DISABLED_KEYS);
|
||||
refreshProfiles(getMyApplication());
|
||||
}
|
||||
|
||||
private void refreshProfiles(OsmandApplication app) {
|
||||
profiles.clear();
|
||||
profiles.addAll(NavigationFragment.getBaseProfiles(app));
|
||||
for (ProfileDataObject profile : profiles) {
|
||||
String key = profile.getStringKey();
|
||||
profile.setSelected(selectedProfiles.contains(key));
|
||||
profile.setEnabled(!disabledProfiles.contains(key));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createMenuItems(Bundle savedInstanceState) {
|
||||
items.add(new TitleItem(getString(R.string.application_profiles)));
|
||||
|
||||
for (int i = 0; i < profiles.size(); i++) {
|
||||
addProfileItem(profiles.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
private void addProfileItem(final ProfileDataObject profile) {
|
||||
OsmandApplication app = requiredMyApplication();
|
||||
View itemView = UiUtilities.getInflater(app, nightMode)
|
||||
.inflate(R.layout.bottom_sheet_item_with_descr_and_checkbox_56dp, null);
|
||||
|
||||
int profileColorId = profile.getIconColor(nightMode);
|
||||
int activeColorId = nightMode ?
|
||||
R.color.active_color_primary_dark : R.color.active_color_primary_light;
|
||||
int disableColorId = nightMode ?
|
||||
R.color.icon_color_default_dark : R.color.icon_color_default_light;
|
||||
boolean enable = profile.isEnabled();
|
||||
|
||||
TextView tvTitle = itemView.findViewById(R.id.title);
|
||||
TextView tvDescription = itemView.findViewById(R.id.description);
|
||||
ImageView ivIcon = itemView.findViewById(R.id.icon);
|
||||
final CompoundButton compoundButton = itemView.findViewById(R.id.compound_button);
|
||||
|
||||
tvTitle.setText(profile.getName());
|
||||
tvDescription.setText(profile.getDescription());
|
||||
|
||||
if (!enable) {
|
||||
tvTitle.setTextColor(ContextCompat.getColor(app, disableColorId));
|
||||
tvDescription.setTextColor(ContextCompat.getColor(app, disableColorId));
|
||||
}
|
||||
|
||||
Drawable drawableIcon = app.getUIUtilities().getIcon(
|
||||
profile.getIconRes(), enable ? profileColorId : disableColorId);
|
||||
ivIcon.setImageDrawable(drawableIcon);
|
||||
UiUtilities.setupCompoundButton(nightMode, ContextCompat.getColor(app,
|
||||
enable ? activeColorId : disableColorId), compoundButton);
|
||||
compoundButton.setSaveEnabled(false);
|
||||
compoundButton.setChecked(profile.isSelected());
|
||||
|
||||
View.OnClickListener l = !enable ? null : new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
String key = profile.getStringKey();
|
||||
boolean selected = !profile.isSelected();
|
||||
if (selected) {
|
||||
selectedProfiles.add(key);
|
||||
} else {
|
||||
selectedProfiles.remove(key);
|
||||
}
|
||||
profile.setSelected(selected);
|
||||
compoundButton.setChecked(selected);
|
||||
}
|
||||
};
|
||||
|
||||
items.add(new BaseBottomSheetItem.Builder()
|
||||
.setCustomView(itemView)
|
||||
.setOnClickListener(l)
|
||||
.create());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
outState.putStringArrayList(SELECTED_KEYS, new ArrayList<>(selectedProfiles));
|
||||
outState.putStringArrayList(DISABLED_KEYS, new ArrayList<>(disabledProfiles));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDismissButtonClickAction() {
|
||||
super.onDismissButtonClickAction();
|
||||
dismiss();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getRightBottomButtonTextId() {
|
||||
return R.string.shared_string_apply;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onRightBottomButtonClick() {
|
||||
Fragment targetFragment = getTargetFragment();
|
||||
if (targetFragment instanceof CallbackWithObject) {
|
||||
List<String> newSelected = new ArrayList<>();
|
||||
for (String profile : selectedProfiles) {
|
||||
if (!disabledProfiles.contains(profile)) {
|
||||
newSelected.add(profile);
|
||||
}
|
||||
}
|
||||
((CallbackWithObject) targetFragment).processResult(newSelected);
|
||||
}
|
||||
dismiss();
|
||||
}
|
||||
|
||||
public static void showInstance(@NonNull MapActivity mapActivity, Fragment targetFragment,
|
||||
@Nullable List<String> selectedProfiles,
|
||||
@Nullable List<String> disabledProfiles,
|
||||
boolean usedOnMap) {
|
||||
SelectMultipleProfilesBottomSheet fragment = new SelectMultipleProfilesBottomSheet();
|
||||
Bundle args = new Bundle();
|
||||
args.putStringArrayList(SELECTED_KEYS, selectedProfiles != null ?
|
||||
new ArrayList<>(selectedProfiles) : new ArrayList<String>());
|
||||
args.putStringArrayList(DISABLED_KEYS, disabledProfiles != null ?
|
||||
new ArrayList<>(disabledProfiles) : new ArrayList<String>());
|
||||
fragment.setArguments(args);
|
||||
fragment.setTargetFragment(targetFragment, 0);
|
||||
fragment.setUsedOnMap(usedOnMap);
|
||||
fragment.show(mapActivity.getSupportFragmentManager(), SelectMultipleProfilesBottomSheet.TAG);
|
||||
}
|
||||
|
||||
}
|
|
@ -23,16 +23,19 @@ import androidx.core.content.ContextCompat;
|
|||
import androidx.fragment.app.DialogFragment;
|
||||
|
||||
import net.osmand.AndroidUtils;
|
||||
import net.osmand.CallbackWithObject;
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.UiUtilities;
|
||||
import net.osmand.plus.activities.MapActivity;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by rosty on 12/27/16.
|
||||
*/
|
||||
|
||||
public class CreateEditActionDialog extends DialogFragment {
|
||||
public class CreateEditActionDialog extends DialogFragment implements CallbackWithObject<Object> {
|
||||
|
||||
public static final String TAG = CreateEditActionDialog.class.getSimpleName();
|
||||
|
||||
|
@ -254,4 +257,12 @@ public class CreateEditActionDialog extends DialogFragment {
|
|||
private UiUtilities getIconsCache(){
|
||||
return getApplication().getUIUtilities();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean processResult(Object result) {
|
||||
if (action instanceof SwitchableAction) {
|
||||
((SwitchableAction) action).onItemsSelected(getContext(), (List) result);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,6 +35,7 @@ import net.osmand.plus.quickaction.actions.ShowHideFavoritesAction;
|
|||
import net.osmand.plus.quickaction.actions.ShowHideGpxTracksAction;
|
||||
import net.osmand.plus.quickaction.actions.ShowHidePoiAction;
|
||||
import net.osmand.plus.quickaction.actions.ShowHideTransportLinesAction;
|
||||
import net.osmand.plus.quickaction.actions.SwitchProfileAction;
|
||||
import net.osmand.util.Algorithms;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
@ -234,6 +235,7 @@ public class QuickActionRegistry {
|
|||
quickActionTypes.add(NavAutoZoomMapAction.TYPE);
|
||||
quickActionTypes.add(NavStartStopAction.TYPE);
|
||||
quickActionTypes.add(NavResumePauseAction.TYPE);
|
||||
quickActionTypes.add(SwitchProfileAction.TYPE);
|
||||
OsmandPlugin.registerQuickActionTypesPlugins(quickActionTypes);
|
||||
|
||||
Map<Integer, QuickActionType> quickActionTypesInt = new TreeMap<>();
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package net.osmand.plus.quickaction;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MotionEvent;
|
||||
|
@ -11,6 +12,8 @@ import android.widget.EditText;
|
|||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.ColorRes;
|
||||
import androidx.annotation.DrawableRes;
|
||||
import androidx.annotation.StringRes;
|
||||
import androidx.appcompat.widget.SwitchCompat;
|
||||
import androidx.core.view.MotionEventCompat;
|
||||
|
@ -36,6 +39,7 @@ public abstract class SwitchableAction<T> extends QuickAction {
|
|||
|
||||
private transient EditText title;
|
||||
|
||||
private transient Adapter adapter;
|
||||
private transient ItemTouchHelper touchHelper;
|
||||
|
||||
protected SwitchableAction(QuickActionType type) {
|
||||
|
@ -57,13 +61,20 @@ public abstract class SwitchableAction<T> extends QuickAction {
|
|||
View view = LayoutInflater.from(parent.getContext())
|
||||
.inflate(R.layout.quick_action_switchable_action, parent, false);
|
||||
|
||||
SwitchCompat showDialog = (SwitchCompat) view.findViewById(R.id.saveButton);
|
||||
final SwitchCompat showDialog = (SwitchCompat) view.findViewById(R.id.saveButton);
|
||||
if (!getParams().isEmpty()) {
|
||||
showDialog.setChecked(Boolean.valueOf(getParams().get(KEY_DIALOG)));
|
||||
}
|
||||
view.findViewById(R.id.saveButtonContainer).setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
boolean selected = showDialog.isChecked();
|
||||
showDialog.setChecked(!selected);
|
||||
}
|
||||
});
|
||||
|
||||
RecyclerView list = (RecyclerView) view.findViewById(R.id.list);
|
||||
Adapter adapter = new Adapter(activity, new QuickActionListFragment.OnStartDragListener() {
|
||||
adapter = new Adapter(activity, new QuickActionListFragment.OnStartDragListener() {
|
||||
@Override
|
||||
public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
|
||||
touchHelper.startDrag(viewHolder);
|
||||
|
@ -105,6 +116,10 @@ public abstract class SwitchableAction<T> extends QuickAction {
|
|||
return hasParams;
|
||||
}
|
||||
|
||||
protected Adapter getAdapter() {
|
||||
return adapter;
|
||||
}
|
||||
|
||||
public abstract List<T> loadListFromParams();
|
||||
|
||||
public abstract void executeWithParams(MapActivity activity, String params);
|
||||
|
@ -145,6 +160,12 @@ public abstract class SwitchableAction<T> extends QuickAction {
|
|||
public void onBindViewHolder(final Adapter.ItemHolder holder, final int position) {
|
||||
final T item = itemsList.get(position);
|
||||
|
||||
OsmandApplication app = (OsmandApplication) context.getApplicationContext();
|
||||
|
||||
Drawable icon = app.getUIUtilities().getIcon(
|
||||
getItemIconRes(app, item), getItemIconColorRes(app, item));
|
||||
holder.icon.setImageDrawable(icon);
|
||||
|
||||
holder.title.setText(getItemName(context, item));
|
||||
|
||||
holder.handleView.setOnTouchListener(new View.OnTouchListener() {
|
||||
|
@ -181,6 +202,10 @@ public abstract class SwitchableAction<T> extends QuickAction {
|
|||
return itemsList.size();
|
||||
}
|
||||
|
||||
public List<T> getItemsList() {
|
||||
return itemsList;
|
||||
}
|
||||
|
||||
public void deleteItem(int position) {
|
||||
|
||||
if (position == -1) {
|
||||
|
@ -262,6 +287,7 @@ public abstract class SwitchableAction<T> extends QuickAction {
|
|||
public TextView title;
|
||||
public ImageView handleView;
|
||||
public ImageView closeBtn;
|
||||
public ImageView icon;
|
||||
|
||||
public ItemHolder(View itemView) {
|
||||
super(itemView);
|
||||
|
@ -269,6 +295,7 @@ public abstract class SwitchableAction<T> extends QuickAction {
|
|||
title = (TextView) itemView.findViewById(R.id.title);
|
||||
handleView = (ImageView) itemView.findViewById(R.id.handle_view);
|
||||
closeBtn = (ImageView) itemView.findViewById(R.id.closeImageButton);
|
||||
icon = (ImageView) itemView.findViewById(R.id.imageView);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -279,6 +306,17 @@ public abstract class SwitchableAction<T> extends QuickAction {
|
|||
|
||||
protected abstract String getItemName(Context context, T item);
|
||||
|
||||
@DrawableRes
|
||||
protected int getItemIconRes(Context context, T item) {
|
||||
return R.drawable.ic_map;
|
||||
}
|
||||
|
||||
@ColorRes
|
||||
protected int getItemIconColorRes(OsmandApplication app, T item) {
|
||||
boolean nightMode = !app.getSettings().isLightContent();
|
||||
return nightMode ? R.color.icon_color_default_dark : R.color.icon_color_default_light;
|
||||
}
|
||||
|
||||
protected abstract
|
||||
@StringRes
|
||||
int getAddBtnText();
|
||||
|
@ -294,4 +332,8 @@ public abstract class SwitchableAction<T> extends QuickAction {
|
|||
protected abstract String getListKey();
|
||||
|
||||
protected abstract View.OnClickListener getOnAddBtnClickListener(MapActivity activity, final Adapter adapter);
|
||||
|
||||
protected void onItemsSelected(Context ctx, List<T> selectedItems) {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,242 @@
|
|||
package net.osmand.plus.quickaction.actions;
|
||||
|
||||
import android.content.Context;
|
||||
import android.text.TextUtils;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.appcompat.widget.SwitchCompat;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import net.osmand.plus.ApplicationMode;
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.OsmandSettings;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.activities.MapActivity;
|
||||
import net.osmand.plus.profiles.SelectMultipleProfilesBottomSheet;
|
||||
import net.osmand.plus.quickaction.CreateEditActionDialog;
|
||||
import net.osmand.plus.quickaction.QuickAction;
|
||||
import net.osmand.plus.quickaction.QuickActionType;
|
||||
import net.osmand.plus.quickaction.SwitchableAction;
|
||||
import net.osmand.plus.views.MapQuickActionLayer;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class SwitchProfileAction extends SwitchableAction<String> {
|
||||
|
||||
private final static String KEY_PROFILES = "profiles";
|
||||
|
||||
public static final QuickActionType TYPE = new QuickActionType(32,
|
||||
"profile.change", SwitchProfileAction.class)
|
||||
.nameRes(R.string.change_application_profile)
|
||||
.iconRes(R.drawable.ic_action_manage_profiles)
|
||||
.category(QuickActionType.NAVIGATION);
|
||||
|
||||
public SwitchProfileAction() {
|
||||
super(TYPE);
|
||||
}
|
||||
|
||||
public SwitchProfileAction(QuickAction quickAction) {
|
||||
super(quickAction);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTitle(List<String> filters) {
|
||||
List<String> profileNames = new ArrayList<>();
|
||||
for (String key : filters) {
|
||||
ApplicationMode appMode = getModeForKey(key);
|
||||
if (appMode != null) {
|
||||
profileNames.add(appMode.toHumanString());
|
||||
}
|
||||
}
|
||||
return TextUtils.join(", ", profileNames);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void saveListToParams(List<String> list) {
|
||||
getParams().put(getListKey(), new Gson().toJson(list));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> loadListFromParams() {
|
||||
String json = getParams().get(getListKey());
|
||||
|
||||
if (json == null || json.isEmpty()) return new ArrayList<>();
|
||||
|
||||
Type listType = new TypeToken<ArrayList<String>>() {
|
||||
}.getType();
|
||||
|
||||
List<String> list = new Gson().fromJson(json, listType);
|
||||
|
||||
Iterator<String> it = list.iterator();
|
||||
while (it.hasNext()) {
|
||||
ApplicationMode appMode = getModeForKey(it.next());
|
||||
if (appMode == null) {
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(MapActivity activity) {
|
||||
OsmandSettings settings = activity.getMyApplication().getSettings();
|
||||
List<String> profiles = loadListFromParams();
|
||||
|
||||
if (profiles.size() == 0) {
|
||||
Toast.makeText(activity, activity.getString(R.string.profiles_for_action_not_found),
|
||||
Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
boolean showDialog = Boolean.valueOf(getParams().get(KEY_DIALOG));
|
||||
if (showDialog) {
|
||||
showChooseDialog(activity.getSupportFragmentManager());
|
||||
return;
|
||||
}
|
||||
|
||||
int index = -1;
|
||||
final String currentProfile = settings.getApplicationMode().getStringKey();
|
||||
|
||||
for (int idx = 0; idx < profiles.size(); idx++) {
|
||||
if (currentProfile.equals(profiles.get(idx))) {
|
||||
index = idx;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
String nextProfile = profiles.get(0);
|
||||
|
||||
if (index >= 0 && index + 1 < profiles.size()) {
|
||||
nextProfile = profiles.get(index + 1);
|
||||
}
|
||||
executeWithParams(activity, nextProfile);
|
||||
|
||||
super.execute(activity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void executeWithParams(MapActivity activity, String params) {
|
||||
OsmandApplication app = activity.getMyApplication();
|
||||
OsmandSettings settings = app.getSettings();
|
||||
|
||||
ApplicationMode appMode = getModeForKey(params);
|
||||
if (appMode != null) {
|
||||
settings.APPLICATION_MODE.set(appMode);
|
||||
|
||||
app.getQuickActionRegistry().setQuickActionFabState(true);
|
||||
|
||||
MapQuickActionLayer mil = activity.getMapLayers().getMapQuickActionLayer();
|
||||
if (mil != null) {
|
||||
mil.refreshLayer();
|
||||
}
|
||||
|
||||
String message = String.format(activity.getString(
|
||||
R.string.application_profile_changed), appMode.toHumanString());
|
||||
Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTranslatedItemName(Context context, String item) {
|
||||
return getModeForKey(item).toHumanString();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getItemName(Context context, String item) {
|
||||
ApplicationMode appMode = getModeForKey(item);
|
||||
if (appMode != null) {
|
||||
return appMode.toHumanString();
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSelectedItem(OsmandApplication app) {
|
||||
ApplicationMode appMode = app.getSettings().getApplicationMode();
|
||||
return appMode.getStringKey();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getAddBtnText() {
|
||||
return R.string.shared_string_add_profile;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getDiscrHint() {
|
||||
return R.string.quick_action_switch_profile_descr;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getDiscrTitle() {
|
||||
return R.string.application_profiles;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getListKey() {
|
||||
return KEY_PROFILES;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getItemIconRes(Context context, String item) {
|
||||
ApplicationMode appMode = getModeForKey(item);
|
||||
if (appMode != null) {
|
||||
return appMode.getIconRes();
|
||||
}
|
||||
return super.getItemIconRes(context, item);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getItemIconColorRes(OsmandApplication app, String item) {
|
||||
ApplicationMode appMode = getModeForKey(item);
|
||||
if (appMode != null) {
|
||||
boolean nightMode = !app.getSettings().isLightContent();
|
||||
return appMode.getIconColorInfo().getColor(nightMode);
|
||||
}
|
||||
return super.getItemIconColorRes(app, item);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected View.OnClickListener getOnAddBtnClickListener(final MapActivity activity, final Adapter adapter) {
|
||||
return new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
CreateEditActionDialog targetFragment = (CreateEditActionDialog) activity
|
||||
.getSupportFragmentManager().findFragmentByTag(CreateEditActionDialog.TAG);
|
||||
List<String> selectedProfilesKeys = new ArrayList<>(adapter.getItemsList());
|
||||
SelectMultipleProfilesBottomSheet.showInstance(activity, targetFragment,
|
||||
selectedProfilesKeys, selectedProfilesKeys, false);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private ApplicationMode getModeForKey(String key) {
|
||||
return ApplicationMode.valueOfStringKey(key, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean fillParams(View root, MapActivity activity) {
|
||||
getParams().put(KEY_DIALOG, Boolean.toString(((SwitchCompat) root.findViewById(R.id.saveButton)).isChecked()));
|
||||
return super.fillParams(root, activity);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onItemsSelected(Context ctx, List<String> selectedItems) {
|
||||
Adapter adapter = getAdapter();
|
||||
if (adapter == null) {
|
||||
return;
|
||||
}
|
||||
for (String key : selectedItems) {
|
||||
ApplicationMode appMode = getModeForKey(key);
|
||||
if (appMode != null) {
|
||||
adapter.addItem(key, ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue