From 3500c89c4625e7fdf5dcb60dad3b0f743de84afc Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Thu, 16 Nov 2017 11:59:10 +0200 Subject: [PATCH] Refactor code --- .../osmand/plus/audionotes/NotesFragment.java | 376 ++++++++---------- .../audionotes/adapters/NotesAdapter.java | 113 ++++++ 2 files changed, 279 insertions(+), 210 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/audionotes/adapters/NotesAdapter.java diff --git a/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java b/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java index 84b80dacef..639faaac68 100644 --- a/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java +++ b/OsmAnd/src/net/osmand/plus/audionotes/NotesFragment.java @@ -21,7 +21,6 @@ import android.view.inputmethod.InputMethodManager; import android.widget.ArrayAdapter; import android.widget.CheckBox; import android.widget.EditText; -import android.widget.ImageButton; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; @@ -31,7 +30,6 @@ import net.osmand.data.PointDescription; import net.osmand.plus.GPXUtilities; import net.osmand.plus.GPXUtilities.GPXFile; import net.osmand.plus.GPXUtilities.WptPt; -import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; import net.osmand.plus.R; import net.osmand.plus.activities.ActionBarProgressActivity; @@ -40,6 +38,8 @@ import net.osmand.plus.activities.OsmandActionBarActivity; import net.osmand.plus.audionotes.AudioVideoNotesPlugin.Recording; import net.osmand.plus.audionotes.ItemMenuBottomSheetDialogFragment.ItemMenuFragmentListener; import net.osmand.plus.audionotes.SortByMenuBottomSheetDialogFragment.SortFragmentListener; +import net.osmand.plus.audionotes.adapters.NotesAdapter; +import net.osmand.plus.audionotes.adapters.NotesAdapter.NotesAdapterListener; import net.osmand.plus.base.OsmAndListFragment; import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.myplaces.FavoritesActivity; @@ -53,27 +53,28 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; +import java.util.LinkedList; import java.util.List; public class NotesFragment extends OsmAndListFragment { + public static final Recording SHARE_LOCATION_FILE = new Recording(new File(".")); + private static final Log LOG = PlatformUtil.getLog(NotesFragment.class); private static final int MODE_DELETE = 100; private static final int MODE_SHARE = 101; - AudioVideoNotesPlugin plugin; - List items; - NotesAdapter listAdapter; + private AudioVideoNotesPlugin plugin; + private NotesAdapter listAdapter; + private List items; + private List selected = new LinkedList<>(); private View footerView; - private boolean selectionMode = false; + private boolean selectionMode; private ActionMode actionMode; - private ArrayList selected = new ArrayList<>(); - Recording shareLocationFile = new Recording(new File(".")); - @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { // Handle screen rotation: @@ -87,8 +88,9 @@ public class NotesFragment extends OsmAndListFragment { ((ItemMenuBottomSheetDialogFragment) itemMenu).setListener(createItemMenuFragmentListener()); } - setHasOptionsMenu(true); plugin = OsmandPlugin.getEnabledPlugin(AudioVideoNotesPlugin.class); + setHasOptionsMenu(true); + View view = getActivity().getLayoutInflater().inflate(R.layout.update_index, container, false); view.findViewById(R.id.select_all).setVisibility(View.GONE); ((TextView) view.findViewById(R.id.header)).setText(R.string.notes); @@ -108,26 +110,6 @@ public class NotesFragment extends OsmAndListFragment { return view; } - @Override - public ArrayAdapter getAdapter() { - return listAdapter; - } - - private void selectAll() { - for (int i = 0; i < listAdapter.getCount(); i++) { - Recording point = listAdapter.getItem(i); - if (!selected.contains(point)) { - selected.add(point); - } - } - listAdapter.notifyDataSetInvalidated(); - } - - private void deselectAll() { - selected.clear(); - listAdapter.notifyDataSetInvalidated(); - } - @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); @@ -147,25 +129,15 @@ public class NotesFragment extends OsmAndListFragment { listView.setHeaderDividersEnabled(false); listView.setFooterDividersEnabled(false); } - listAdapter = new NotesAdapter(items); + listAdapter = new NotesAdapter(getMyApplication(), items); + listAdapter.setSelected(selected); + listAdapter.setListener(createAdapterListener()); listView.setAdapter(listAdapter); } - private void sortItemsDescending() { - Collections.sort(items, new Comparator() { - @Override - public int compare(Recording first, Recording second) { - long firstTime = first.getLastModified(); - long secondTime = second.getLastModified(); - if (firstTime < secondTime) { - return 1; - } else if (firstTime == secondTime) { - return 0; - } else { - return -1; - } - } - }); + @Override + public ArrayAdapter getAdapter() { + return listAdapter; } @Override @@ -182,10 +154,7 @@ public class NotesFragment extends OsmAndListFragment { item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { - SortByMenuBottomSheetDialogFragment fragment = new SortByMenuBottomSheetDialogFragment(); - fragment.setUsedOnMap(false); - fragment.setListener(createSortFragmentListener()); - fragment.show(getChildFragmentManager(), SortByMenuBottomSheetDialogFragment.TAG); + showSortMenuFragment(); return true; } }); @@ -212,6 +181,80 @@ public class NotesFragment extends OsmAndListFragment { item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); } + public OsmandActionBarActivity getActionBarActivity() { + if (getActivity() instanceof OsmandActionBarActivity) { + return (OsmandActionBarActivity) getActivity(); + } + return null; + } + + private NotesAdapterListener createAdapterListener() { + return new NotesAdapterListener() { + @Override + public void onItemClick(Recording rec, boolean checked) { + if (selectionMode) { + if (checked) { + selected.add(rec); + } else { + selected.remove(rec); + } + updateSelectionMode(actionMode); + } else { + showOnMap(rec); + } + } + + @Override + public void onOptionsClick(Recording rec) { + ItemMenuBottomSheetDialogFragment fragment = new ItemMenuBottomSheetDialogFragment(); + fragment.setUsedOnMap(false); + fragment.setListener(createItemMenuFragmentListener()); + fragment.setRecording(rec); + fragment.setRetainInstance(true); + fragment.show(getChildFragmentManager(), ItemMenuBottomSheetDialogFragment.TAG); + } + }; + } + + private void showSortMenuFragment() { + SortByMenuBottomSheetDialogFragment fragment = new SortByMenuBottomSheetDialogFragment(); + fragment.setUsedOnMap(false); + fragment.setListener(createSortFragmentListener()); + fragment.show(getChildFragmentManager(), SortByMenuBottomSheetDialogFragment.TAG); + } + + private void selectAll() { + for (int i = 0; i < listAdapter.getCount(); i++) { + Recording rec = listAdapter.getItem(i); + if (!selected.contains(rec)) { + selected.add(rec); + } + } + listAdapter.notifyDataSetInvalidated(); + } + + private void deselectAll() { + selected.clear(); + listAdapter.notifyDataSetInvalidated(); + } + + private void sortItemsDescending() { + Collections.sort(items, new Comparator() { + @Override + public int compare(Recording first, Recording second) { + long firstTime = first.getLastModified(); + long secondTime = second.getLastModified(); + if (firstTime < secondTime) { + return 1; + } else if (firstTime == secondTime) { + return 0; + } else { + return -1; + } + } + }); + } + private SortFragmentListener createSortFragmentListener() { return new SortFragmentListener() { @Override @@ -228,15 +271,12 @@ public class NotesFragment extends OsmAndListFragment { public boolean onCreateActionMode(final ActionMode mode, Menu menu) { LOG.debug("onCreateActionMode"); if (type == MODE_SHARE) { - listAdapter.insert(shareLocationFile, 0); + listAdapter.insert(SHARE_LOCATION_FILE, 0); } switchSelectionMode(true); - MenuItem item; - if (type == MODE_DELETE) { - item = menu.add(R.string.shared_string_delete_all).setIcon(R.drawable.ic_action_delete_dark); - } else { - item = menu.add(R.string.shared_string_share).setIcon(R.drawable.ic_action_gshare_dark); - } + int titleRes = type == MODE_DELETE ? R.string.shared_string_delete_all : R.string.shared_string_share; + int iconRes = type == MODE_DELETE ? R.drawable.ic_action_delete_dark : R.drawable.ic_action_gshare_dark; + MenuItem item = menu.add(titleRes).setIcon(iconRes); item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { @@ -272,7 +312,7 @@ public class NotesFragment extends OsmAndListFragment { public void onDestroyActionMode(ActionMode mode) { LOG.debug("onDestroyActionMode"); if (type == MODE_SHARE) { - listAdapter.remove(shareLocationFile); + listAdapter.remove(SHARE_LOCATION_FILE); } switchSelectionMode(false); listAdapter.notifyDataSetInvalidated(); @@ -280,19 +320,13 @@ public class NotesFragment extends OsmAndListFragment { }); } - public OsmandActionBarActivity getActionBarActivity() { - if (getActivity() instanceof OsmandActionBarActivity) { - return (OsmandActionBarActivity) getActivity(); - } - return null; - } - private void switchSelectionMode(boolean enable) { - this.selectionMode = enable; + selectionMode = enable; + listAdapter.setSelectionMode(enable); View view = getView(); if (view != null) { - view.findViewById(R.id.select_all).setVisibility(selectionMode ? View.VISIBLE : View.GONE); - ((FavoritesActivity) getActivity()).setToolbarVisibility(!selectionMode + view.findViewById(R.id.select_all).setVisibility(enable ? View.VISIBLE : View.GONE); + ((FavoritesActivity) getActivity()).setToolbarVisibility(!enable && AndroidUiHelper.isOrientationPortrait(getActivity())); ((FavoritesActivity) getActivity()).updateListViewFooter(footerView); } @@ -300,7 +334,7 @@ public class NotesFragment extends OsmAndListFragment { private void updateSelectionTitle(ActionMode m) { if (selected.size() > 0) { - m.setTitle(selected.size() + " " + getMyApplication().getString(R.string.shared_string_selected_lowercase)); + m.setTitle(selected.size() + " " + getString(R.string.shared_string_selected_lowercase)); } else { m.setTitle(""); } @@ -327,33 +361,33 @@ public class NotesFragment extends OsmAndListFragment { selectAll.setChecked(true); } - private void deleteItems(final ArrayList selected) { - AlertDialog.Builder b = new AlertDialog.Builder(getActivity()); - b.setMessage(getString(R.string.local_recordings_delete_all_confirm, selected.size())); - b.setPositiveButton(R.string.shared_string_delete, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - Iterator it = selected.iterator(); - while (it.hasNext()) { - Recording pnt = it.next(); - plugin.deleteRecording(pnt, true); - it.remove(); - listAdapter.delete(pnt); - } - listAdapter.notifyDataSetChanged(); - } - }); - b.setNegativeButton(R.string.shared_string_cancel, null); - b.show(); + private void deleteItems(final List selected) { + new AlertDialog.Builder(getActivity()) + .setMessage(getString(R.string.local_recordings_delete_all_confirm, selected.size())) + .setPositiveButton(R.string.shared_string_delete, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + Iterator it = selected.iterator(); + while (it.hasNext()) { + Recording rec = it.next(); + plugin.deleteRecording(rec, true); + it.remove(); + listAdapter.remove(rec); + } + listAdapter.notifyDataSetChanged(); + } + }) + .setNegativeButton(R.string.shared_string_cancel, null) + .show(); } - private void shareItems(ArrayList selected) { + private void shareItems(List selected) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND_MULTIPLE); intent.setType("image/*"); /* This example is sharing jpeg images. */ ArrayList files = new ArrayList(); for (Recording path : selected) { - if (path == shareLocationFile) { + if (path == SHARE_LOCATION_FILE) { File fl = generateGPXForRecordings(selected); if (fl != null) { files.add(FileProvider.getUriForFile(getActivity(), getActivity().getPackageName() + ".fileprovider", fl)); @@ -374,12 +408,12 @@ public class NotesFragment extends OsmAndListFragment { startActivity(Intent.createChooser(intent, getString(R.string.share_note))); } - private File generateGPXForRecordings(ArrayList selected) { + private File generateGPXForRecordings(List selected) { File tmpFile = new File(getActivity().getCacheDir(), "share/noteLocations.gpx"); tmpFile.getParentFile().mkdirs(); GPXFile file = new GPXFile(); for (Recording r : selected) { - if (r != shareLocationFile) { + if (r != SHARE_LOCATION_FILE) { String desc = r.getDescriptionName(r.getFileName()); if (desc == null) { desc = r.getFileName(); @@ -398,99 +432,6 @@ public class NotesFragment extends OsmAndListFragment { return tmpFile; } - public OsmandApplication getMyApplication() { - return (OsmandApplication) getActivity().getApplication(); - } - - class NotesAdapter extends ArrayAdapter { - - NotesAdapter(List recordingList) { - super(getActivity(), R.layout.note, recordingList); - } - - public void delete(Recording pnt) { - remove(pnt); - } - - @Override - public View getView(final int position, View convertView, ViewGroup parent) { - LayoutInflater inflater = getActivity().getLayoutInflater(); - View row = convertView; - if (row == null) { - row = inflater.inflate(R.layout.note, parent, false); - } - - final Recording recording = getItem(position); - if (recording == shareLocationFile) { - ((TextView) row.findViewById(R.id.name)).setText(R.string.av_locations); - ((TextView) row.findViewById(R.id.description)).setText(R.string.av_locations_descr); - } else { - DashAudioVideoNotesFragment.getNoteView(recording, row, getMyApplication()); - } - row.findViewById(R.id.play).setVisibility(View.GONE); - - final CheckBox ch = (CheckBox) row.findViewById(R.id.check_local_index); - ImageButton options = (ImageButton) row.findViewById(R.id.options); - options.setImageDrawable(getMyApplication().getIconsCache().getThemedIcon(R.drawable.ic_overflow_menu_white)); - if (selectionMode) { - options.setVisibility(View.GONE); - ch.setVisibility(View.VISIBLE); - ch.setChecked(selected.contains(recording)); - row.findViewById(R.id.icon).setVisibility(View.GONE); - ch.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - onItemSelect(ch, recording); - } - }); - } else { - row.findViewById(R.id.icon).setVisibility(View.VISIBLE); - options.setVisibility(View.VISIBLE); - ch.setVisibility(View.GONE); - } - - options.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - ItemMenuBottomSheetDialogFragment fragment = new ItemMenuBottomSheetDialogFragment(); - fragment.setUsedOnMap(false); - fragment.setListener(createItemMenuFragmentListener()); - fragment.setRecording(recording); - fragment.setRetainInstance(true); - fragment.show(getChildFragmentManager(), ItemMenuBottomSheetDialogFragment.TAG); - } - }); - row.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - if (selectionMode) { - ch.setChecked(!ch.isChecked()); - onItemSelect(ch, recording); - } else { - showOnMap(recording); - } - } - }); - return row; - } - - public void onItemSelect(CheckBox ch, Recording child) { - if (ch.isChecked()) { - selected.add(child); - } else { - selected.remove(child); - } - updateSelectionMode(actionMode); - } - } - - private void showOnMap(Recording recording) { - getMyApplication().getSettings().setMapLocationToShow(recording.getLatitude(), recording.getLongitude(), 15, - new PointDescription(recording.getSearchHistoryType(), recording.getName(getActivity(), true)), - true, recording); - MapActivity.launchMapActivityMoveToTop(getActivity()); - } - private ItemMenuFragmentListener createItemMenuFragmentListener() { return new ItemMenuFragmentListener() { @Override @@ -500,21 +441,7 @@ public class NotesFragment extends OsmAndListFragment { @Override public void shareOnClick(Recording recording) { - Intent sharingIntent = new Intent(Intent.ACTION_SEND); - if (recording.isPhoto()) { - Uri screenshotUri = Uri.parse(recording.getFile().getAbsolutePath()); - sharingIntent.setType("image/*"); - sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri); - } else if (recording.isAudio()) { - Uri audioUri = Uri.parse(recording.getFile().getAbsolutePath()); - sharingIntent.setType("audio/*"); - sharingIntent.putExtra(Intent.EXTRA_STREAM, audioUri); - } else if (recording.isVideo()) { - Uri videoUri = Uri.parse(recording.getFile().getAbsolutePath()); - sharingIntent.setType("video/*"); - sharingIntent.putExtra(Intent.EXTRA_STREAM, videoUri); - } - startActivity(Intent.createChooser(sharingIntent, getString(R.string.share_note))); + shareNote(recording); } @Override @@ -529,21 +456,36 @@ public class NotesFragment extends OsmAndListFragment { @Override public void deleteOnClick(final Recording recording) { - AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - builder.setMessage(R.string.recording_delete_confirm); - builder.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - plugin.deleteRecording(recording, true); - listAdapter.remove(recording); - } - }); - builder.setNegativeButton(R.string.shared_string_cancel, null); - builder.show(); + deleteNote(recording); } }; } + private void shareNote(Recording recording) { + Intent sharingIntent = new Intent(Intent.ACTION_SEND); + if (recording.isPhoto()) { + Uri screenshotUri = Uri.parse(recording.getFile().getAbsolutePath()); + sharingIntent.setType("image/*"); + sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri); + } else if (recording.isAudio()) { + Uri audioUri = Uri.parse(recording.getFile().getAbsolutePath()); + sharingIntent.setType("audio/*"); + sharingIntent.putExtra(Intent.EXTRA_STREAM, audioUri); + } else if (recording.isVideo()) { + Uri videoUri = Uri.parse(recording.getFile().getAbsolutePath()); + sharingIntent.setType("video/*"); + sharingIntent.putExtra(Intent.EXTRA_STREAM, videoUri); + } + startActivity(Intent.createChooser(sharingIntent, getString(R.string.share_note))); + } + + private void showOnMap(Recording recording) { + getMyApplication().getSettings().setMapLocationToShow(recording.getLatitude(), recording.getLongitude(), 15, + new PointDescription(recording.getSearchHistoryType(), recording.getName(getActivity(), true)), + true, recording); + MapActivity.launchMapActivityMoveToTop(getActivity()); + } + private void editNote(final Recording recording) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.shared_string_rename); @@ -566,4 +508,18 @@ public class NotesFragment extends OsmAndListFragment { builder.create().show(); editText.requestFocus(); } + + private void deleteNote(final Recording recording) { + new AlertDialog.Builder(getActivity()) + .setMessage(R.string.recording_delete_confirm) + .setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + plugin.deleteRecording(recording, true); + listAdapter.remove(recording); + } + }) + .setNegativeButton(R.string.shared_string_cancel, null) + .show(); + } } diff --git a/OsmAnd/src/net/osmand/plus/audionotes/adapters/NotesAdapter.java b/OsmAnd/src/net/osmand/plus/audionotes/adapters/NotesAdapter.java new file mode 100644 index 0000000000..81e2359f49 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/audionotes/adapters/NotesAdapter.java @@ -0,0 +1,113 @@ +package net.osmand.plus.audionotes.adapters; + +import android.content.Context; +import android.support.annotation.NonNull; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.CheckBox; +import android.widget.ImageButton; +import android.widget.TextView; + +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.audionotes.AudioVideoNotesPlugin.Recording; +import net.osmand.plus.audionotes.DashAudioVideoNotesFragment; +import net.osmand.plus.audionotes.NotesFragment; + +import java.util.List; + +public class NotesAdapter extends ArrayAdapter { + + private OsmandApplication app; + private NotesAdapterListener listener; + + private boolean selectionMode; + private List selected; + + public void setListener(NotesAdapterListener listener) { + this.listener = listener; + } + + public void setSelectionMode(boolean selectionMode) { + this.selectionMode = selectionMode; + } + + public void setSelected(List selected) { + this.selected = selected; + } + + public NotesAdapter(OsmandApplication app, List items) { + super(app, R.layout.note, items); + this.app = app; + } + + @NonNull + @Override + public View getView(final int position, View convertView, @NonNull ViewGroup parent) { + LayoutInflater inflater = (LayoutInflater) app.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + View row = convertView; + if (row == null) { + row = inflater.inflate(R.layout.note, parent, false); + } + + final Recording recording = getItem(position); + if (recording == NotesFragment.SHARE_LOCATION_FILE) { + ((TextView) row.findViewById(R.id.name)).setText(R.string.av_locations); + ((TextView) row.findViewById(R.id.description)).setText(R.string.av_locations_descr); + } else { + DashAudioVideoNotesFragment.getNoteView(recording, row, app); + } + row.findViewById(R.id.play).setVisibility(View.GONE); + + final CheckBox ch = (CheckBox) row.findViewById(R.id.check_local_index); + ImageButton options = (ImageButton) row.findViewById(R.id.options); + options.setImageDrawable(app.getIconsCache().getThemedIcon(R.drawable.ic_overflow_menu_white)); + if (selectionMode) { + options.setVisibility(View.GONE); + ch.setVisibility(View.VISIBLE); + ch.setChecked(selected.contains(recording)); + row.findViewById(R.id.icon).setVisibility(View.GONE); + ch.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (listener != null) { + listener.onItemClick(recording, ch.isChecked()); + } + } + }); + } else { + row.findViewById(R.id.icon).setVisibility(View.VISIBLE); + options.setVisibility(View.VISIBLE); + ch.setVisibility(View.GONE); + } + + options.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (listener != null) { + listener.onOptionsClick(recording); + } + } + }); + row.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + boolean checked = !ch.isChecked(); + ch.setChecked(checked); + if (listener != null) { + listener.onItemClick(recording, checked); + } + } + }); + return row; + } + + public interface NotesAdapterListener { + + void onItemClick(Recording rec, boolean checked); + + void onOptionsClick(Recording rec); + } +}