From 3f753c56775a49c517e47c41fa30d4ec61b7a99e Mon Sep 17 00:00:00 2001 From: veliymolfar Date: Thu, 13 Feb 2020 18:49:31 +0200 Subject: [PATCH] import ui, duplicate quick actions --- OsmAnd/res/layout/fragment_import.xml | 24 +- .../res/layout/fragment_import_duplicates.xml | 2 +- OsmAnd/res/layout/list_item_header_import.xml | 44 +++ OsmAnd/res/layout/list_item_import.xml | 54 ++++ .../layout/profile_data_list_item_child.xml | 103 ++++--- .../layout/profile_data_list_item_group.xml | 129 +++++---- OsmAnd/res/values/strings.xml | 4 + .../src/net/osmand/plus/SQLiteTileSource.java | 4 - .../src/net/osmand/plus/SettingsHelper.java | 44 ++- .../net/osmand/plus/helpers/ImportHelper.java | 6 +- .../plus/profiles/AdditionalDataWrapper.java | 1 + .../settings/DuplicatesSettingsAdapter.java | 143 ++++++++++ .../settings/ExportImportSettingsAdapter.java | 260 ++++++++++++++++++ .../settings/ImportDuplicatesFragment.java | 99 ++++++- .../plus/settings/ImportSettingsFragment.java | 2 +- 15 files changed, 782 insertions(+), 137 deletions(-) create mode 100644 OsmAnd/res/layout/list_item_header_import.xml create mode 100644 OsmAnd/res/layout/list_item_import.xml create mode 100644 OsmAnd/src/net/osmand/plus/settings/DuplicatesSettingsAdapter.java create mode 100644 OsmAnd/src/net/osmand/plus/settings/ExportImportSettingsAdapter.java diff --git a/OsmAnd/res/layout/fragment_import.xml b/OsmAnd/res/layout/fragment_import.xml index 7e223e51a5..3a5a25728b 100644 --- a/OsmAnd/res/layout/fragment_import.xml +++ b/OsmAnd/res/layout/fragment_import.xml @@ -1,5 +1,5 @@ - @@ -38,6 +40,7 @@ android:id="@+id/description" android:layout_width="match_parent" android:layout_height="wrap_content" + android:layout_below="@+id/toolbar_layout" android:background="?attr/bg_color" android:paddingStart="@dimen/content_padding" android:paddingTop="@dimen/list_header_settings_top_margin" @@ -48,20 +51,27 @@ android:textSize="@dimen/default_list_text_size" osmand:typeface="@string/font_roboto_medium" /> - + + android:layout_height="wrap_content" + android:layout_below="@+id/description_divider" + android:layout_marginBottom="@dimen/dialog_button_ex_height" + android:groupIndicator="@android:color/transparent" + android:listSelector="@android:color/transparent" /> - \ No newline at end of file + \ No newline at end of file diff --git a/OsmAnd/res/layout/fragment_import_duplicates.xml b/OsmAnd/res/layout/fragment_import_duplicates.xml index 3539093fa6..a60ea21119 100644 --- a/OsmAnd/res/layout/fragment_import_duplicates.xml +++ b/OsmAnd/res/layout/fragment_import_duplicates.xml @@ -50,7 +50,7 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/layout/list_item_import.xml b/OsmAnd/res/layout/list_item_import.xml new file mode 100644 index 0000000000..86e6498ba1 --- /dev/null +++ b/OsmAnd/res/layout/list_item_import.xml @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/layout/profile_data_list_item_child.xml b/OsmAnd/res/layout/profile_data_list_item_child.xml index be6477bb17..231c8060d0 100644 --- a/OsmAnd/res/layout/profile_data_list_item_child.xml +++ b/OsmAnd/res/layout/profile_data_list_item_child.xml @@ -1,49 +1,74 @@ + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:gravity="center_vertical" + android:orientation="vertical"> - + - + - + - + - + - + + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/layout/profile_data_list_item_group.xml b/OsmAnd/res/layout/profile_data_list_item_group.xml index 2f99266f6a..e4ca55f934 100644 --- a/OsmAnd/res/layout/profile_data_list_item_group.xml +++ b/OsmAnd/res/layout/profile_data_list_item_group.xml @@ -1,72 +1,85 @@ + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:orientation="vertical"> - + - + - + - + - + - + - + - + - + - + + + + + \ No newline at end of file diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 4c13b072ff..4e7af07263 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -11,6 +11,10 @@ Thx - Hardy --> + Nothing selected + Quick actions + Profiles + Listed %1$s, already exist in OsmAnd. Current items will be replaced with items from the file Replace all Keep both diff --git a/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java b/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java index d34ddcc75e..10e7499297 100644 --- a/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java +++ b/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java @@ -658,8 +658,4 @@ public class SQLiteTileSource implements ITileSource { public String getReferer() { return referer; } - - public String getUrlTemplate() { - return urlTemplate; - } } diff --git a/OsmAnd/src/net/osmand/plus/SettingsHelper.java b/OsmAnd/src/net/osmand/plus/SettingsHelper.java index b302cfd0e1..0f29edd37f 100644 --- a/OsmAnd/src/net/osmand/plus/SettingsHelper.java +++ b/OsmAnd/src/net/osmand/plus/SettingsHelper.java @@ -153,6 +153,8 @@ public class SettingsHelper { private SettingsItemType type; + boolean shouldReplace = false; + SettingsItem(@NonNull SettingsItemType type) { this.type = type; } @@ -176,10 +178,14 @@ public class SettingsHelper { @NonNull public abstract String getFileName(); - public Boolean shouldReadOnCollecting() { + public boolean shouldReadOnCollecting() { return false; } + public void setShouldReplace(boolean shouldReplace) { + this.shouldReplace = shouldReplace; + } + static SettingsItemType parseItemType(@NonNull JSONObject json) throws IllegalArgumentException, JSONException { return SettingsItemType.valueOf(json.getString("type")); } @@ -760,20 +766,42 @@ public class SettingsHelper { QuickActionFactory factory = new QuickActionFactory(); List savedActions = factory.parseActiveActionsList(getSettings().QUICK_ACTION_LIST.get()); List newActions = new ArrayList<>(savedActions); - for (QuickAction action : quickActions) { - for (QuickAction savedAction : savedActions) { - if (action.getName(app).equals(savedAction.getName(app))) { - newActions.remove(savedAction); + if (shouldReplace) { + for (QuickAction action : quickActions) { + for (QuickAction savedAction : savedActions) { + if (action.getName(app).equals(savedAction.getName(app))) { + newActions.remove(savedAction); + } } } + } else { + for (QuickAction action : quickActions) { + action.setName("new_" + action.getName(app)); + } } newActions.addAll(quickActions); ((MapActivity) app.getSettingsHelper().getActivity()).getMapLayers().getQuickActionRegistry().updateQuickActions(newActions); } } + public List getDuplicates() { + List duplicates = new ArrayList<>(); + if (!quickActions.isEmpty()) { + QuickActionFactory factory = new QuickActionFactory(); + List savedActions = factory.parseActiveActionsList(getSettings().QUICK_ACTION_LIST.get()); + for (QuickAction action : quickActions) { + for (QuickAction savedAction : savedActions) { + if (action.getName(app).equals(savedAction.getName(app))) { + duplicates.add(action); + } + } + } + } + return duplicates; + } + @Override - public Boolean shouldReadOnCollecting() { + public boolean shouldReadOnCollecting() { return true; } @@ -916,7 +944,7 @@ public class SettingsHelper { } @Override - public Boolean shouldReadOnCollecting() { + public boolean shouldReadOnCollecting() { return true; } @@ -1053,7 +1081,7 @@ public class SettingsHelper { } @Override - public Boolean shouldReadOnCollecting() { + public boolean shouldReadOnCollecting() { return true; } diff --git a/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java b/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java index 64b18a4d02..5f4a479dd3 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java @@ -786,11 +786,7 @@ public class ImportHelper { if (succeed) { FragmentManager fragmentManager = activity.getSupportFragmentManager(); if (fragmentManager != null) { - ExportImportProfileBottomSheet.showInstance( - fragmentManager, - ExportImportProfileBottomSheet.State.IMPORT, - file, - items); + ImportSettingsFragment.showInstance(fragmentManager, items, file); } } else { app.showShortToastMessage(app.getString(R.string.file_import_error, name, app.getString(R.string.shared_string_unexpected_error))); diff --git a/OsmAnd/src/net/osmand/plus/profiles/AdditionalDataWrapper.java b/OsmAnd/src/net/osmand/plus/profiles/AdditionalDataWrapper.java index bb46a92393..9eb2ceed13 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/AdditionalDataWrapper.java +++ b/OsmAnd/src/net/osmand/plus/profiles/AdditionalDataWrapper.java @@ -26,6 +26,7 @@ public class AdditionalDataWrapper { } public enum Type { + PROFILE, QUICK_ACTIONS, POI_TYPES, MAP_SOURCES, diff --git a/OsmAnd/src/net/osmand/plus/settings/DuplicatesSettingsAdapter.java b/OsmAnd/src/net/osmand/plus/settings/DuplicatesSettingsAdapter.java new file mode 100644 index 0000000000..5bf3705084 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/settings/DuplicatesSettingsAdapter.java @@ -0,0 +1,143 @@ +package net.osmand.plus.settings; + +import android.graphics.drawable.Drawable; +import android.support.annotation.NonNull; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import net.osmand.plus.ApplicationMode; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.quickaction.QuickAction; + + +import java.util.List; + +public class DuplicatesSettingsAdapter extends RecyclerView.Adapter { + + private static final int HEADER_TYPE = 0; + private static final int ITEM_TYPE = 1; + + private boolean nightMode; + + private OsmandApplication app; + + private List items; + + DuplicatesSettingsAdapter(OsmandApplication app, List items, boolean nightMode) { + this.app = app; + this.items = items; + this.nightMode = nightMode; + } + + @NonNull + @Override + public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + LayoutInflater inflater = LayoutInflater.from(app); + if (viewType == HEADER_TYPE) { + View view = inflater.inflate(R.layout.list_item_header_import, parent, false); + return new HeaderViewHolder(view); + } else { + View view = inflater.inflate(R.layout.list_item_import, parent, false); + return new ItemViewHolder(view); + } + } + + @Override + public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { + Object currentItem = items.get(position); + if (holder instanceof HeaderViewHolder) { + ((HeaderViewHolder) holder).title.setText((String) currentItem); + ((HeaderViewHolder) holder).subTitle.setText(String.format( + app.getString(R.string.listed_exist), + (String) currentItem)); + ((HeaderViewHolder) holder).divider.setVisibility(View.VISIBLE); + } else if (holder instanceof ItemViewHolder) { + String title = null; + String subTitle = null; + Drawable drawable = null; + if (currentItem instanceof ApplicationMode) { + title = ((ApplicationMode) currentItem).toHumanString(); + subTitle = ((ApplicationMode) currentItem).getRoutingProfile(); + drawable = app.getUIUtilities().getIcon( + ((ApplicationMode) currentItem).getIconRes(), + ((ApplicationMode) currentItem).getIconColorInfo().getColor(nightMode) + ); + } else if (currentItem instanceof QuickAction) { + title = ((QuickAction) currentItem).getName(app); + drawable = app.getUIUtilities().getIcon(((QuickAction) currentItem).getIconRes(), nightMode); + } + ((ItemViewHolder) holder).title.setText(title != null ? title : ""); + if (subTitle != null) { + ((ItemViewHolder) holder).subTitle.setText(subTitle); + ((ItemViewHolder) holder).subTitle.setVisibility(View.VISIBLE); + } else { + ((ItemViewHolder) holder).subTitle.setVisibility(View.GONE); + } + if (drawable != null) { + ((ItemViewHolder) holder).icon.setImageDrawable(drawable); + ((ItemViewHolder) holder).icon.setImageResource(View.VISIBLE); + } else { + ((ItemViewHolder) holder).icon.setImageResource(R.drawable.ic_action_info_dark); + ((ItemViewHolder) holder).icon.setVisibility(View.GONE); + } + ((ItemViewHolder) holder).divider.setVisibility(shouldShowDivider(position) ? View.VISIBLE : View.GONE); + } + } + + @Override + public int getItemCount() { + return items.size(); + } + + @Override + public int getItemViewType(int position) { + if (items.get(position) instanceof String) { + return HEADER_TYPE; + } else { + return ITEM_TYPE; + } + } + + private class HeaderViewHolder extends RecyclerView.ViewHolder { + TextView title; + TextView subTitle; + View divider; + + HeaderViewHolder(View itemView) { + super(itemView); + title = itemView.findViewById(R.id.title); + subTitle = itemView.findViewById(R.id.sub_title); + divider = itemView.findViewById(R.id.top_divider); + } + } + + private class ItemViewHolder extends RecyclerView.ViewHolder { + TextView title; + TextView subTitle; + ImageView icon; + View divider; + + ItemViewHolder(View itemView) { + super(itemView); + title = itemView.findViewById(R.id.title); + subTitle = itemView.findViewById(R.id.sub_title); + icon = itemView.findViewById(R.id.icon); + divider = itemView.findViewById(R.id.bottom_divider); + } + } + + private boolean shouldShowDivider(int position) { + boolean isLast = position == items.size() - 1; + if (isLast) { + return true; + } else { + Object next = items.get(position + 1); + return next instanceof String; + } + } +} diff --git a/OsmAnd/src/net/osmand/plus/settings/ExportImportSettingsAdapter.java b/OsmAnd/src/net/osmand/plus/settings/ExportImportSettingsAdapter.java new file mode 100644 index 0000000000..2a9d82f0e6 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/settings/ExportImportSettingsAdapter.java @@ -0,0 +1,260 @@ +package net.osmand.plus.settings; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.CheckBox; +import android.widget.ImageView; +import android.widget.TextView; + +import net.osmand.map.ITileSource; +import net.osmand.plus.ApplicationMode; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.activities.OsmandBaseExpandableListAdapter; +import net.osmand.plus.poi.PoiUIFilter; +import net.osmand.plus.profiles.AdditionalDataWrapper; +import net.osmand.plus.quickaction.QuickAction; +import net.osmand.plus.render.RenderingIcons; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +class ExportImportSettingsAdapter extends OsmandBaseExpandableListAdapter { + + private OsmandApplication app; + + private List settingsToOperate; + + private List settingsList; + + private boolean nightMode; + + private boolean importState; + + ExportImportSettingsAdapter(OsmandApplication app, List settingsList, boolean nightMode, boolean importState) { + this.app = app; + this.settingsList = settingsList; + this.nightMode = nightMode; + this.importState = importState; + this.settingsToOperate = new ArrayList<>(); + } + + public void updateSettingsList(List settingsList) { + this.settingsList = settingsList; + notifyDataSetChanged(); + } + + public List getSettingsToOperate() { + return this.settingsToOperate; + } + + public void selectAll(boolean selectAll) { + settingsToOperate.clear(); + if (selectAll) { + for (AdditionalDataWrapper item : settingsList) { + settingsToOperate.addAll(item.getItems()); + } + } + notifyDataSetChanged(); + } + + @Override + public int getGroupCount() { + return settingsList.size(); + } + + @Override + public int getChildrenCount(int i) { + return settingsList.get(i).getItems().size(); + } + + @Override + public Object getGroup(int i) { + return settingsList.get(i); + } + + @Override + public Object getChild(int groupPosition, int childPosition) { + return settingsList.get(groupPosition).getItems().get(childPosition); + } + + @Override + public long getGroupId(int i) { + return i; + } + + @Override + public long getChildId(int groupPosition, int childPosition) { + return groupPosition * 10000 + childPosition; + } + + @Override + public boolean hasStableIds() { + return false; + } + + @Override + public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { + View group = convertView; + if (group == null) { +// LayoutInflater inflater = UiUtilities.getInflater(app, nightMode); + LayoutInflater inflater = LayoutInflater.from(app); + group = inflater.inflate(R.layout.profile_data_list_item_group, parent, false); + } + + boolean isLastGroup = groupPosition == getGroupCount() - 1; + final AdditionalDataWrapper.Type type = settingsList.get(groupPosition).getType(); + + TextView titleTv = group.findViewById(R.id.title_tv); + TextView subTextTv = group.findViewById(R.id.sub_text_tv); + final CheckBox checkBox = group.findViewById(R.id.check_box); + ImageView expandIv = group.findViewById(R.id.explist_indicator); + View divider = group.findViewById(R.id.divider); + View cardTopDivider = group.findViewById(R.id.card_top_divider); + View cardBottomDivider = group.findViewById(R.id.card_bottom_divider); + + titleTv.setText(getGroupTitle(type)); + cardTopDivider.setVisibility(importState ? View.VISIBLE : View.GONE); + if (importState) { + cardBottomDivider.setVisibility(isExpanded ? View.GONE : View.VISIBLE); + divider.setVisibility(View.GONE); + } else { + cardBottomDivider.setVisibility(View.GONE); + divider.setVisibility(isExpanded || isLastGroup ? View.GONE : View.VISIBLE); + } +// CompoundButtonCompat.setButtonTintList(checkBox, ColorStateList.valueOf(ContextCompat.getColor(app, profileColor))); + + final List listItems = settingsList.get(groupPosition).getItems(); + subTextTv.setText(String.valueOf(listItems.size())); + + checkBox.setChecked(settingsToOperate.containsAll(listItems)); + checkBox.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + + if (checkBox.isChecked()) { + for (Object object : listItems) { + if (!settingsToOperate.contains(object)) { + settingsToOperate.add(object); + } + } + } else { + settingsToOperate.removeAll(listItems); + } + notifyDataSetInvalidated(); + } + }); + + adjustIndicator(app, groupPosition, isExpanded, group, true); + + return group; + } + + @Override + public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { + View child = convertView; + if (child == null) { +// LayoutInflater inflater = UiUtilities.getInflater(app, nightMode); + LayoutInflater inflater = LayoutInflater.from(app); + child = inflater.inflate(R.layout.profile_data_list_item_child, parent, false); + } + final Object currentItem = settingsList.get(groupPosition).getItems().get(childPosition); + + + boolean isLastGroup = groupPosition == getGroupCount() - 1; + final AdditionalDataWrapper.Type type = settingsList.get(groupPosition).getType(); + + TextView title = child.findViewById(R.id.title_tv); +// TextView subText = child.findViewById(R.id.sub_text_tv); + final CheckBox checkBox = child.findViewById(R.id.check_box); + ImageView icon = child.findViewById(R.id.icon); + View divider = child.findViewById(R.id.divider); + View cardBottomDivider = child.findViewById(R.id.card_bottom_divider); + + divider.setVisibility(isLastChild && !isLastGroup && !importState ? View.VISIBLE : View.GONE); + cardBottomDivider.setVisibility(isLastChild && importState ? View.VISIBLE : View.GONE); +// CompoundButtonCompat.setButtonTintList(checkBox, ColorStateList.valueOf(ContextCompat.getColor(app, profileColor))); + + checkBox.setChecked(settingsToOperate.contains(currentItem)); + checkBox.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if (checkBox.isChecked()) { + settingsToOperate.add(currentItem); + } else { + settingsToOperate.remove(currentItem); + } + notifyDataSetInvalidated(); + } + }); + + switch (type) { + case PROFILE: + title.setText(((ApplicationMode) currentItem).toHumanString()); +// subText.setText(((ApplicationMode) currentItem)); + icon.setVisibility(View.INVISIBLE); + icon.setImageResource(((ApplicationMode) currentItem).getIconRes()); + break; + case QUICK_ACTIONS: + title.setText(((QuickAction) currentItem).getName(app.getApplicationContext())); + icon.setVisibility(View.INVISIBLE); + icon.setImageResource(R.drawable.ic_action_info_dark); + break; + case POI_TYPES: + title.setText(((PoiUIFilter) currentItem).getName()); + icon.setVisibility(View.VISIBLE); + int iconRes = RenderingIcons.getBigIconResourceId(((PoiUIFilter) currentItem).getIconId()); +// icon.setImageDrawable(app.getUIUtilities().getIcon(iconRes != 0 ? iconRes : R.drawable.ic_person, profileColor)); + break; + case MAP_SOURCES: + title.setText(((ITileSource) currentItem).getName()); + icon.setVisibility(View.INVISIBLE); + icon.setImageResource(R.drawable.ic_action_info_dark); + break; + case CUSTOM_RENDER_STYLE: + String renderName = ((File) currentItem).getName(); + renderName = renderName.replace('_', ' ').replaceAll(".render.xml", ""); + title.setText(renderName); + icon.setVisibility(View.INVISIBLE); + icon.setImageResource(R.drawable.ic_action_info_dark); + break; + case CUSTOM_ROUTING: + String routingName = ((File) currentItem).getName(); + routingName = routingName.replace('_', ' ').replaceAll(".xml", ""); + title.setText(routingName); + icon.setVisibility(View.INVISIBLE); + icon.setImageResource(R.drawable.ic_action_info_dark); + break; + default: + return child; + } + return child; + } + + @Override + public boolean isChildSelectable(int i, int i1) { + return false; + } + + private int getGroupTitle(AdditionalDataWrapper.Type type) { + switch (type) { + case PROFILE: + return R.string.shared_sting_profiles; + case QUICK_ACTIONS: + return R.string.configure_screen_quick_action; + case POI_TYPES: + return R.string.poi_dialog_poi_type; + case MAP_SOURCES: + return R.string.quick_action_map_source_title; + case CUSTOM_RENDER_STYLE: + return R.string.shared_string_custom_rendering_style; + case CUSTOM_ROUTING: + return R.string.shared_string_routing; + default: + return R.string.access_empty_list; + } + } +} + diff --git a/OsmAnd/src/net/osmand/plus/settings/ImportDuplicatesFragment.java b/OsmAnd/src/net/osmand/plus/settings/ImportDuplicatesFragment.java index faf497d146..25e0ecd624 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ImportDuplicatesFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ImportDuplicatesFragment.java @@ -4,31 +4,52 @@ import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.FragmentManager; +import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import net.osmand.plus.ApplicationMode; +import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; +import net.osmand.plus.SettingsHelper; import net.osmand.plus.SettingsHelper.SettingsItem; import net.osmand.plus.base.BaseOsmAndDialogFragment; +import net.osmand.plus.quickaction.QuickAction; import net.osmand.view.ComplexButton; +import java.io.File; +import java.util.ArrayList; import java.util.List; public class ImportDuplicatesFragment extends BaseOsmAndDialogFragment implements View.OnClickListener { public static final String TAG = ImportSettingsFragment.class.getSimpleName(); - + private OsmandApplication app; private RecyclerView list; + private List duplicatesList; + private List settingsItems; + private DuplicatesSettingsAdapter adapter; + private File file; - public static void showInstance(@NonNull FragmentManager fm) { + public static void showInstance(@NonNull FragmentManager fm, List duplicatesList, + List settingsItems, File file) { ImportDuplicatesFragment fragment = new ImportDuplicatesFragment(); + fragment.setDuplicatesList(duplicatesList); + fragment.setSettingsItems(settingsItems); + fragment.setFile(file); fragment.show(fm, TAG); } + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + app = getMyApplication(); + } + @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { @@ -53,30 +74,68 @@ public class ImportDuplicatesFragment extends BaseOsmAndDialogFragment implement return root; } + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + adapter = new DuplicatesSettingsAdapter(getMyApplication(), prepareDuplicates(), getSettings().isLightContent()); + list.setLayoutManager(new LinearLayoutManager(getMyApplication())); + list.setAdapter(adapter); + } + + private List prepareDuplicates() { + List duplicates = new ArrayList<>(); + List profiles = new ArrayList<>(); + List actions = new ArrayList<>(); + + for (Object object : duplicatesList) { + if (object instanceof ApplicationMode) { + profiles.add((ApplicationMode) object); + } else if (object instanceof QuickAction) { + actions.add((QuickAction) object); + } + } + + if (!profiles.isEmpty()) { + duplicates.add(getString(R.string.shared_sting_profiles)); + duplicates.addAll(profiles); + } + + if (!actions.isEmpty()) { + duplicates.add(getString(R.string.shared_sting_quick_actions)); + duplicates.addAll(actions); + } + return duplicates; + } + @Override public void onClick(View view) { switch (view.getId()) { case R.id.keep_both_btn: { - keepBothItems(); + importItems(false); break; } case R.id.replace_all_btn: { - replaceAllItems(); + importItems(true); break; } } } - private void keepBothItems() { - - } - - private void replaceAllItems() { - - } - - private void importItems(List list) { - + private void importItems(boolean shouldReplace) { + for (SettingsItem item : settingsItems) { + item.setShouldReplace(shouldReplace); + } + app.getSettingsHelper().importSettings(file, settingsItems, "", 1, new SettingsHelper.SettingsImportListener() { + @Override + public void onSettingsImportFinished(boolean succeed, boolean empty, @NonNull List items) { + if (succeed) { + app.showShortToastMessage(app.getString(R.string.file_imported_successfully, file.getName())); + } else if (empty) { + app.showShortToastMessage(app.getString(R.string.file_import_error, file.getName(), app.getString(R.string.shared_string_unexpected_error))); + } + } + }); + dismiss(); } private void setupToolbar(Toolbar toolbar) { @@ -92,4 +151,16 @@ public class ImportDuplicatesFragment extends BaseOsmAndDialogFragment implement } }); } + + public void setDuplicatesList(List duplicatesList) { + this.duplicatesList = duplicatesList; + } + + public void setSettingsItems(List settingsItems) { + this.settingsItems = settingsItems; + } + + public void setFile(File file) { + this.file = file; + } } diff --git a/OsmAnd/src/net/osmand/plus/settings/ImportSettingsFragment.java b/OsmAnd/src/net/osmand/plus/settings/ImportSettingsFragment.java index 7910f189f3..b3a5931e25 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ImportSettingsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ImportSettingsFragment.java @@ -1 +1 @@ -package net.osmand.plus.settings; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.FragmentManager; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnGroupClickListener; import android.widget.ExpandableListView.OnChildClickListener; import net.osmand.plus.R; import net.osmand.plus.SettingsHelper.SettingsItem; import net.osmand.plus.base.BaseOsmAndDialogFragment; import net.osmand.plus.widgets.TextViewEx; import java.util.List; public class ImportSettingsFragment extends BaseOsmAndDialogFragment implements OnChildClickListener, OnGroupClickListener, View.OnClickListener { public static final String TAG = ImportSettingsFragment.class.getSimpleName(); private boolean allSelected; private ExpandableListView listView; private TextViewEx selectBtn; private TextViewEx continueBtn; private List settingsItems; public static void showInstance(@NonNull FragmentManager fm, @NonNull List settingsItems) { ImportSettingsFragment fragment = new ImportSettingsFragment(); fragment.setSettingsItems(settingsItems); fragment.show(fm, TAG); } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View root = inflater.inflate(R.layout.fragment_import, container, false); setupToolbar((Toolbar) root.findViewById(R.id.toolbar)); continueBtn = root.findViewById(R.id.continue_button); selectBtn = root.findViewById(R.id.select_button); listView = root.findViewById(android.R.id.list); continueBtn.setOnClickListener(this); selectBtn.setOnClickListener(this); return root; } @Override public void onClick(View view) { switch (view.getId()) { case R.id.select_button: { allSelected = !allSelected; selectBtn.setText(allSelected ? R.string.shared_string_deselect_all : R.string.shared_string_select_all); selectDeselectAll(); break; } case R.id.continue_button: { importItems(); break; } } } private void selectDeselectAll() { if (allSelected) { } else { } } private void importItems() { } private boolean checkDuplicates() { return false; } @Override public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) { return false; } @Override public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) { return false; } public void setSettingsItems(List settingsItems) { this.settingsItems = settingsItems; } private void setupToolbar(Toolbar toolbar) { toolbar.setNavigationIcon(getPaintedContentIcon(R.drawable.headline_close_button, getSettings().isLightContent() ? getResources().getColor(R.color.active_buttons_and_links_text_light) : getResources().getColor(R.color.active_buttons_and_links_text_dark))); toolbar.setNavigationContentDescription(R.string.shared_string_close); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dismiss(); } }); } } \ No newline at end of file +package net.osmand.plus.settings; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.FragmentManager; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ExpandableListView; import net.osmand.map.ITileSource; import net.osmand.map.TileSourceManager; import net.osmand.plus.ApplicationMode; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.SQLiteTileSource; import net.osmand.plus.SettingsHelper; import net.osmand.plus.SettingsHelper.SettingsItem; import net.osmand.plus.base.BaseOsmAndDialogFragment; import net.osmand.plus.poi.PoiUIFilter; import net.osmand.plus.profiles.AdditionalDataWrapper; import net.osmand.plus.quickaction.QuickAction; import net.osmand.plus.widgets.TextViewEx; import java.io.File; import java.util.ArrayList; import java.util.List; public class ImportSettingsFragment extends BaseOsmAndDialogFragment implements View.OnClickListener { public static final String TAG = ImportSettingsFragment.class.getSimpleName(); private OsmandApplication app; private ExportImportSettingsAdapter adapter; private ExpandableListView expandableList; private TextViewEx selectBtn; private List settingsItems; private File file; private boolean allSelected; public static void showInstance(@NonNull FragmentManager fm, @NonNull List settingsItems, @NonNull File file) { ImportSettingsFragment fragment = new ImportSettingsFragment(); fragment.setSettingsItems(settingsItems); fragment.setFile(file); fragment.show(fm, TAG); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); app = getMyApplication(); if (settingsItems == null) { settingsItems = app.getSettingsHelper().getSettingsItems(); } if (file == null) { file = app.getSettingsHelper().getSettingsFile(); } } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View root = inflater.inflate(R.layout.fragment_import, container, false); setupToolbar((Toolbar) root.findViewById(R.id.toolbar)); TextViewEx continueBtn = root.findViewById(R.id.continue_button); selectBtn = root.findViewById(R.id.select_button); expandableList = root.findViewById(android.R.id.list); continueBtn.setOnClickListener(this); selectBtn.setOnClickListener(this); expandableList.findViewById(R.id.list); return root; } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); adapter = new ExportImportSettingsAdapter(getMyApplication(), getSettingsToOperate(), getSettings().isLightContent(), true); expandableList.setAdapter(adapter); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.select_button: { allSelected = !allSelected; selectBtn.setText(allSelected ? R.string.shared_string_deselect_all : R.string.shared_string_select_all); adapter.selectAll(allSelected); break; } case R.id.continue_button: { if (adapter.getSettingsToOperate().isEmpty()) { app.showShortToastMessage(getString(R.string.shared_sting_nothing_selected)); } else { importItems(); } break; } } } private void importItems() { List settingsItems = getSettingsItemsFromData(adapter.getSettingsToOperate()); List duplicateItems = getDuplicatesData(settingsItems); if (duplicateItems.isEmpty()) { app.getSettingsHelper().importSettings(file, settingsItems, "", 1, new SettingsHelper.SettingsImportListener() { @Override public void onSettingsImportFinished(boolean succeed, boolean empty, @NonNull List items) { if (succeed) { app.showShortToastMessage(app.getString(R.string.file_imported_successfully, file.getName())); } else if (empty) { app.showShortToastMessage(app.getString(R.string.file_import_error, file.getName(), app.getString(R.string.shared_string_unexpected_error))); } } }); dismiss(); } else { ImportDuplicatesFragment.showInstance(getChildFragmentManager(), duplicateItems, settingsItems, file); } } private List getDuplicatesData(List items) { List duplicateItems = new ArrayList<>(); for (SettingsItem item : items) { if (item instanceof SettingsHelper.ProfileSettingsItem) { if (((SettingsHelper.ProfileSettingsItem) item).exists()) { duplicateItems.add(((SettingsHelper.ProfileSettingsItem) item).getAppMode()); } } else if (item instanceof SettingsHelper.QuickActionSettingsItem) { List duplicates = ((SettingsHelper.QuickActionSettingsItem) item).getDuplicates(); if (!duplicates.isEmpty()) { duplicateItems.addAll(duplicates); } } } return duplicateItems; } public void setSettingsItems(List settingsItems) { this.settingsItems = settingsItems; } private List getSettingsItemsFromData(List dataToOperate) { List settingsItems = new ArrayList<>(); List quickActions = new ArrayList<>(); List poiUIFilters = new ArrayList<>(); List tileSourceTemplates = new ArrayList<>(); for (Object object : dataToOperate) { if (object instanceof ApplicationMode) { settingsItems.add(new SettingsHelper.ProfileSettingsItem(getSettings(), (ApplicationMode) object)); } if (object instanceof QuickAction) { quickActions.add((QuickAction) object); } else if (object instanceof PoiUIFilter) { poiUIFilters.add((PoiUIFilter) object); } else if (object instanceof TileSourceManager.TileSourceTemplate || object instanceof SQLiteTileSource) { tileSourceTemplates.add((ITileSource) object); } else if (object instanceof File) { settingsItems.add(new SettingsHelper.FileSettingsItem(getMyApplication(), (File) object)); } } if (!quickActions.isEmpty()) { settingsItems.add(new SettingsHelper.QuickActionSettingsItem(getMyApplication(), quickActions)); } if (!poiUIFilters.isEmpty()) { settingsItems.add(new SettingsHelper.PoiUiFilterSettingsItem(getMyApplication(), poiUIFilters)); } if (!tileSourceTemplates.isEmpty()) { settingsItems.add(new SettingsHelper.MapSourcesSettingsItem(getMyApplication(), tileSourceTemplates)); } return settingsItems; } private List getSettingsToOperate() { List settingsToOperate = new ArrayList<>(); List profiles = new ArrayList<>(); List quickActions = new ArrayList<>(); List poiUIFilters = new ArrayList<>(); List tileSourceTemplates = new ArrayList<>(); List routingFilesList = new ArrayList<>(); List renderFilesList = new ArrayList<>(); for (SettingsHelper.SettingsItem item : settingsItems) { if (item.getType().equals(SettingsHelper.SettingsItemType.PROFILE)) { profiles.add(((SettingsHelper.ProfileSettingsItem) item).getAppMode()); } else if (item.getType().equals(SettingsHelper.SettingsItemType.QUICK_ACTION)) { quickActions.addAll(((SettingsHelper.QuickActionSettingsItem) item).getQuickActions()); } else if (item.getType().equals(SettingsHelper.SettingsItemType.POI_UI_FILTERS)) { poiUIFilters.addAll(((SettingsHelper.PoiUiFilterSettingsItem) item).getPoiUIFilters()); } else if (item.getType().equals(SettingsHelper.SettingsItemType.MAP_SOURCES)) { tileSourceTemplates.addAll(((SettingsHelper.MapSourcesSettingsItem) item).getMapSources()); } else if (item.getType().equals(SettingsHelper.SettingsItemType.FILE)) { if (item.getName().startsWith("/rendering/")) { renderFilesList.add(((SettingsHelper.FileSettingsItem) item).getFile()); } else if (item.getName().startsWith("/routing/")) { routingFilesList.add(((SettingsHelper.FileSettingsItem) item).getFile()); } } } if (!profiles.isEmpty()) { settingsToOperate.add(new AdditionalDataWrapper( AdditionalDataWrapper.Type.PROFILE, profiles)); } if (!quickActions.isEmpty()) { settingsToOperate.add(new AdditionalDataWrapper( AdditionalDataWrapper.Type.QUICK_ACTIONS, quickActions)); } if (!poiUIFilters.isEmpty()) { settingsToOperate.add(new AdditionalDataWrapper( AdditionalDataWrapper.Type.POI_TYPES, poiUIFilters)); } if (!tileSourceTemplates.isEmpty()) { settingsToOperate.add(new AdditionalDataWrapper( AdditionalDataWrapper.Type.MAP_SOURCES, tileSourceTemplates )); } if (!renderFilesList.isEmpty()) { settingsToOperate.add(new AdditionalDataWrapper( AdditionalDataWrapper.Type.CUSTOM_RENDER_STYLE, renderFilesList )); } if (!routingFilesList.isEmpty()) { settingsToOperate.add(new AdditionalDataWrapper( AdditionalDataWrapper.Type.CUSTOM_ROUTING, routingFilesList )); } return settingsToOperate; } private void setupToolbar(Toolbar toolbar) { toolbar.setNavigationIcon(getPaintedContentIcon(R.drawable.headline_close_button, getSettings().isLightContent() ? getResources().getColor(R.color.active_buttons_and_links_text_light) : getResources().getColor(R.color.active_buttons_and_links_text_dark))); toolbar.setNavigationContentDescription(R.string.shared_string_close); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dismiss(); } }); } public void setFile(File file) { this.file = file; } } \ No newline at end of file