From 0c0233fc6348e1ee6b665fddbc7cb1b50515046c Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Thu, 25 Jun 2020 12:53:34 +0300 Subject: [PATCH 1/3] Add preference and UI --- OsmAnd/res/values/strings.xml | 2 ++ OsmAnd/res/xml/general_profile_settings.xml | 8 ++++++++ .../net/osmand/plus/settings/backend/OsmandSettings.java | 1 + .../fragments/GeneralProfileSettingsFragment.java | 9 +++++++++ 4 files changed, 20 insertions(+) diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 096d691d91..6eacd5c202 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -11,6 +11,8 @@ Thx - Hardy --> + Volume buttons as zoom + Enable to control the map zoom level with device volume buttons. Inline skates This device doesn\'t have speed cameras. Uninstall and Restart diff --git a/OsmAnd/res/xml/general_profile_settings.xml b/OsmAnd/res/xml/general_profile_settings.xml index e0c7ec3fa7..6a4148647e 100644 --- a/OsmAnd/res/xml/general_profile_settings.xml +++ b/OsmAnd/res/xml/general_profile_settings.xml @@ -82,6 +82,14 @@ android:layout="@layout/preference_category_with_descr" android:title="@string/shared_string_other" /> + + USE_MAGNETIC_FIELD_SENSOR_COMPASS = new BooleanPreference("use_magnetic_field_sensor_compass", false).makeProfile().cache(); public final OsmandPreference USE_KALMAN_FILTER_FOR_COMPASS = new BooleanPreference("use_kalman_filter_compass", true).makeProfile().cache(); + public final OsmandPreference USE_VOLUME_BUTTONS_AS_ZOOM = new BooleanPreference("use_volume_buttons_as_zoom", false).makeProfile().cache(); public final OsmandPreference DO_NOT_SHOW_STARTUP_MESSAGES = new BooleanPreference("do_not_show_startup_messages", false).makeGlobal().cache(); public final OsmandPreference SHOW_DOWNLOAD_MAP_DIALOG = new BooleanPreference("show_download_map_dialog", true).makeGlobal().cache(); diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/GeneralProfileSettingsFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/GeneralProfileSettingsFragment.java index 4ac4d78d32..80488797ec 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/GeneralProfileSettingsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/GeneralProfileSettingsFragment.java @@ -54,6 +54,7 @@ public class GeneralProfileSettingsFragment extends BaseSettingsFragment impleme setupAngularUnitsPref(); setupSpeedSystemPref(); + setupVolumeButtonsAsZoom(); setupKalmanFilterPref(); setupMagneticFieldSensorPref(); setupMapEmptyStateAllowedPref(); @@ -224,6 +225,14 @@ public class GeneralProfileSettingsFragment extends BaseSettingsFragment impleme speedSystem.setIcon(getActiveIcon(R.drawable.ic_action_speed)); } + private void setupVolumeButtonsAsZoom() { + SwitchPreferenceEx volumeButtonsPref = (SwitchPreferenceEx) findPreference(settings.USE_VOLUME_BUTTONS_AS_ZOOM.getId()); + volumeButtonsPref.setTitle(getString(R.string.use_volume_buttons_as_zoom)); + volumeButtonsPref.setDescription(getString(R.string.use_volume_buttons_as_zoom_descr)); + Drawable icon = getPersistentPrefIcon(R.drawable.ic_action_zoom_volume_buttons); + volumeButtonsPref.setIcon(icon); + } + private void setupKalmanFilterPref() { SwitchPreferenceEx kalmanFilterPref = (SwitchPreferenceEx) findPreference(settings.USE_KALMAN_FILTER_FOR_COMPASS.getId()); kalmanFilterPref.setTitle(getString(R.string.use_kalman_filter_compass)); From 9cbb11493d1821b0dae85aede8432313ea90f7e9 Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Thu, 25 Jun 2020 16:39:47 +0300 Subject: [PATCH 2/3] Add keycode --- OsmAnd/src/net/osmand/plus/activities/MapActivity.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index 5b41e8c76c..c077006585 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -1546,6 +1546,14 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven uiHandler.sendMessageDelayed(msg, LONG_KEYPRESS_DELAY); } return true; + } else if (settings.USE_VOLUME_BUTTONS_AS_ZOOM.get()) { + if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { + changeZoom(-1); + return true; + } else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) { + changeZoom(1); + return true; + } } else if (mapScrollHelper.isScrollingDirectionKeyCode(keyCode)) { return mapScrollHelper.onKeyDown(keyCode, event); } From ca67fb4b8a8559e09669f0217214cdf161eaf233 Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Thu, 25 Jun 2020 21:56:06 +0300 Subject: [PATCH 3/3] Map activity refactoring --- .../osmand/plus/activities/MapActivity.java | 88 ++--------- .../activities/MapActivityKeyListener.java | 137 ++++++++++++++++++ 2 files changed, 147 insertions(+), 78 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/activities/MapActivityKeyListener.java diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index c077006585..e31a4c1b98 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -17,8 +17,6 @@ import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.os.Environment; -import android.os.Handler; -import android.os.Message; import android.util.DisplayMetrics; import android.view.Gravity; import android.view.KeyEvent; @@ -173,9 +171,6 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_SETTINGS_ID; -import static net.osmand.plus.settings.backend.OsmandSettings.GENERIC_EXTERNAL_DEVICE; -import static net.osmand.plus.settings.backend.OsmandSettings.PARROT_EXTERNAL_DEVICE; -import static net.osmand.plus.settings.backend.OsmandSettings.WUNDERLINQ_EXTERNAL_DEVICE; public class MapActivity extends OsmandActionBarActivity implements DownloadEvents, OnRequestPermissionsResultCallback, IRouteInformationListener, AMapPointUpdateListener, @@ -187,8 +182,6 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven public static final String INTENT_PARAMS = "intent_prarams"; private static final int SHOW_POSITION_MSG_ID = OsmAndConstants.UI_HANDLER_MAP_VIEW + 1; - private static final int LONG_KEYPRESS_MSG_ID = OsmAndConstants.UI_HANDLER_MAP_VIEW + 2; - private static final int LONG_KEYPRESS_DELAY = 500; private static final int ZOOM_LABEL_DISPLAY = 16; private static final int MIN_ZOOM_LABEL_DISPLAY = 12; private static final int SECOND_SPLASH_TIME_OUT = 8000; @@ -217,8 +210,6 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven private MapActivityActions mapActions; private MapActivityLayers mapLayers; - // handler to show/hide trackball position and to link map with delay - private Handler uiHandler = new Handler(); // App variables private OsmandApplication app; private OsmandSettings settings; @@ -265,6 +256,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven changeKeyguardFlags(); } }; + private MapActivityKeyListener mapActivityKeyListener; @Override public void onCreate(Bundle savedInstanceState) { @@ -387,7 +379,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven app.getAidlApi().onCreateMapActivity(this); lockHelper.setLockUIAdapter(this); - + mapActivityKeyListener = new MapActivityKeyListener(this); mIsDestroyed = false; } @@ -1532,86 +1524,26 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven } + public ScrollHelper getMapScrollHelper() { + return mapScrollHelper; + } + @Override public boolean onKeyDown(int keyCode, KeyEvent event) { - if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER && app.accessibilityEnabled()) { - if (!uiHandler.hasMessages(LONG_KEYPRESS_MSG_ID)) { - Message msg = Message.obtain(uiHandler, new Runnable() { - @Override - public void run() { - app.getLocationProvider().emitNavigationHint(); - } - }); - msg.what = LONG_KEYPRESS_MSG_ID; - uiHandler.sendMessageDelayed(msg, LONG_KEYPRESS_DELAY); - } - return true; - } else if (settings.USE_VOLUME_BUTTONS_AS_ZOOM.get()) { - if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { - changeZoom(-1); - return true; - } else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) { - changeZoom(1); + if (mapActivityKeyListener != null) { + if (mapActivityKeyListener.onKeyDown(keyCode, event)) { return true; } - } else if (mapScrollHelper.isScrollingDirectionKeyCode(keyCode)) { - return mapScrollHelper.onKeyDown(keyCode, event); } return super.onKeyDown(keyCode, event); } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { - if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) { - if (!app.accessibilityEnabled()) { - mapActions.contextMenuPoint(mapView.getLatitude(), mapView.getLongitude()); - } else if (uiHandler.hasMessages(LONG_KEYPRESS_MSG_ID)) { - uiHandler.removeMessages(LONG_KEYPRESS_MSG_ID); - mapActions.contextMenuPoint(mapView.getLatitude(), mapView.getLongitude()); - } - return true; - } else if (keyCode == KeyEvent.KEYCODE_MENU /*&& event.getRepeatCount() == 0*/) { - // repeat count 0 doesn't work for samsung, 1 doesn't work for lg - toggleDrawer(); - return true; - } else if (keyCode == KeyEvent.KEYCODE_C) { - mapViewTrackingUtilities.backToLocationImpl(); - } else if (settings.EXTERNAL_INPUT_DEVICE.get() == PARROT_EXTERNAL_DEVICE) { - // Parrot device has only dpad left and right - if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) { - changeZoom(-1); - return true; - } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) { - changeZoom(1); + if (mapActivityKeyListener != null) { + if (mapActivityKeyListener.onKeyUp(keyCode, event)) { return true; } - } else if (settings.EXTERNAL_INPUT_DEVICE.get() == WUNDERLINQ_EXTERNAL_DEVICE) { - // WunderLINQ device, motorcycle smart phone control - if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) { - changeZoom(-1); - return true; - } else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) { - changeZoom(1); - return true; - } else if (keyCode == KeyEvent.KEYCODE_ESCAPE) { - String callingApp = "wunderlinq://datagrid"; - Intent intent = new Intent(Intent.ACTION_VIEW); - intent.setData(Uri.parse(callingApp)); - startActivity(intent); - return true; - } - } else if (mapScrollHelper.isScrollingDirectionKeyCode(keyCode)) { - return mapScrollHelper.onKeyUp(keyCode, event); - } else if (settings.EXTERNAL_INPUT_DEVICE.get() == GENERIC_EXTERNAL_DEVICE) { - if (keyCode == KeyEvent.KEYCODE_MINUS) { - changeZoom(-1); - return true; - } else if (keyCode == KeyEvent.KEYCODE_PLUS) { - changeZoom(1); - return true; - } - } else if (OsmandPlugin.onMapActivityKeyUp(this, keyCode)) { - return true; } return super.onKeyUp(keyCode, event); } diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityKeyListener.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityKeyListener.java new file mode 100644 index 0000000000..be642ff80d --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityKeyListener.java @@ -0,0 +1,137 @@ +package net.osmand.plus.activities; + +import android.content.Intent; +import android.net.Uri; +import android.os.Handler; +import android.os.Message; +import android.view.KeyEvent; + +import net.osmand.plus.OsmAndConstants; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandPlugin; +import net.osmand.plus.helpers.ScrollHelper; +import net.osmand.plus.settings.backend.OsmandSettings; +import net.osmand.plus.views.OsmandMapTileView; + +import static net.osmand.plus.settings.backend.OsmandSettings.GENERIC_EXTERNAL_DEVICE; +import static net.osmand.plus.settings.backend.OsmandSettings.PARROT_EXTERNAL_DEVICE; +import static net.osmand.plus.settings.backend.OsmandSettings.WUNDERLINQ_EXTERNAL_DEVICE; + +public class MapActivityKeyListener implements KeyEvent.Callback { + + public static final int LONG_KEYPRESS_MSG_ID = OsmAndConstants.UI_HANDLER_MAP_VIEW + 2; + public static final int LONG_KEYPRESS_DELAY = 500; + + private MapActivity mapActivity; + private OsmandApplication app; + private MapActivityActions mapActions; + private OsmandSettings settings; + // handler to show/hide trackball position and to link map with delay + private Handler uiHandler = new Handler(); + private ScrollHelper mapScrollHelper; + + public MapActivityKeyListener(MapActivity mapActivity) { + this.mapActivity = mapActivity; + app = mapActivity.getMyApplication(); + mapActions = mapActivity.getMapActions(); + settings = app.getSettings(); + mapScrollHelper = mapActivity.getMapScrollHelper(); + } + + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER && app.accessibilityEnabled()) { + if (!uiHandler.hasMessages(LONG_KEYPRESS_MSG_ID)) { + Message msg = Message.obtain(uiHandler, new Runnable() { + @Override + public void run() { + app.getLocationProvider().emitNavigationHint(); + } + }); + msg.what = LONG_KEYPRESS_MSG_ID; + uiHandler.sendMessageDelayed(msg, LONG_KEYPRESS_DELAY); + } + return true; + } else if (settings.USE_VOLUME_BUTTONS_AS_ZOOM.get()) { + if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { + mapActivity.changeZoom(-1); + return true; + } else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) { + mapActivity.changeZoom(1); + return true; + } + } else if (mapScrollHelper.isScrollingDirectionKeyCode(keyCode)) { + return mapScrollHelper.onKeyDown(keyCode, event); + } + return false; + } + + @Override + public boolean onKeyLongPress(int keyCode, KeyEvent event) { + return false; + } + + @Override + public boolean onKeyUp(int keyCode, KeyEvent event) { + if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) { + OsmandMapTileView mapView = mapActivity.getMapView(); + + if (!app.accessibilityEnabled()) { + mapActions.contextMenuPoint(mapView.getLatitude(), mapView.getLongitude()); + } else if (uiHandler.hasMessages(LONG_KEYPRESS_MSG_ID)) { + uiHandler.removeMessages(LONG_KEYPRESS_MSG_ID); + mapActions.contextMenuPoint(mapView.getLatitude(), mapView.getLongitude()); + } + return true; + } else if (keyCode == KeyEvent.KEYCODE_MENU /*&& event.getRepeatCount() == 0*/) { + // repeat count 0 doesn't work for samsung, 1 doesn't work for lg + mapActivity.toggleDrawer(); + return true; + } else if (keyCode == KeyEvent.KEYCODE_C) { + mapActivity.getMapViewTrackingUtilities().backToLocationImpl(); + } else if (settings.EXTERNAL_INPUT_DEVICE.get() == PARROT_EXTERNAL_DEVICE) { + // Parrot device has only dpad left and right + if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) { + mapActivity.changeZoom(-1); + return true; + } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) { + mapActivity.changeZoom(1); + return true; + } + } else if (settings.EXTERNAL_INPUT_DEVICE.get() == WUNDERLINQ_EXTERNAL_DEVICE) { + // WunderLINQ device, motorcycle smart phone control + if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) { + mapActivity.changeZoom(-1); + return true; + } else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) { + mapActivity.changeZoom(1); + return true; + } else if (keyCode == KeyEvent.KEYCODE_ESCAPE) { + String callingApp = "wunderlinq://datagrid"; + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setData(Uri.parse(callingApp)); + mapActivity.startActivity(intent); + return true; + } + } else if (mapScrollHelper.isScrollingDirectionKeyCode(keyCode)) { + return mapScrollHelper.onKeyUp(keyCode, event); + } else if (settings.EXTERNAL_INPUT_DEVICE.get() == GENERIC_EXTERNAL_DEVICE) { + if (keyCode == KeyEvent.KEYCODE_MINUS) { + mapActivity.changeZoom(-1); + return true; + } else if (keyCode == KeyEvent.KEYCODE_PLUS) { + mapActivity.changeZoom(1); + return true; + } + } else { + return OsmandPlugin.onMapActivityKeyUp(mapActivity, keyCode); + } + return false; + } + + @Override + public boolean onKeyMultiple(int keyCode, int count, KeyEvent event) { + return false; + } +} +