commit
fa67fc1cbb
4 changed files with 341 additions and 200 deletions
|
@ -133,6 +133,20 @@
|
|||
<category android:name="android.intent.category.DESK_DOCK" />
|
||||
</intent-filter>
|
||||
|
||||
<intent-filter>
|
||||
<data android:scheme="http" />
|
||||
<data android:scheme="https" />
|
||||
<data android:host="osmand.net" />
|
||||
<data android:pathPrefix="/open-gpx" />
|
||||
<action android:name="android.intent.action.VIEW" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
<category android:name="android.intent.category.BROWSABLE" />
|
||||
<category android:name="android.intent.category.APP_MAPS" />
|
||||
<category android:name="android.intent.category.CAR_MODE" />
|
||||
<category android:name="android.intent.category.CAR_DOCK" />
|
||||
<category android:name="android.intent.category.DESK_DOCK" />
|
||||
</intent-filter>
|
||||
|
||||
<!-- android matches non-greedy : http://stackoverflow.com/questions/3400072/pathpattern-to-match-file-extension-does-not-work-if-a-period-exists-elsewhere-i-->
|
||||
<!-- mimeType&host are both needed or you will either have unwanted matching or no match when needed -->
|
||||
<!-- Capture file open requests (pathPattern is honoured) where no MIME type is provided in the Intent. An Intent with a null
|
||||
|
|
|
@ -20,6 +20,7 @@ import java.io.BufferedOutputStream;
|
|||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
@ -127,6 +128,25 @@ public class AndroidNetworkUtils {
|
|||
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
|
||||
}
|
||||
|
||||
public static void downloadFileAsync(final String url,
|
||||
final File fileToSave,
|
||||
final CallbackWithObject<String> listener) {
|
||||
|
||||
new AsyncTask<Void, Void, String>() {
|
||||
|
||||
@Override
|
||||
protected String doInBackground(Void... params) {
|
||||
return downloadFile(url, fileToSave);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(String error) {
|
||||
if (listener != null) {
|
||||
listener.processResult(error);
|
||||
}
|
||||
}
|
||||
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
|
||||
}
|
||||
|
||||
public static String sendRequest(OsmandApplication ctx, String url, Map<String, String> parameters,
|
||||
String userOperation, boolean toastAllowed, boolean post) {
|
||||
|
@ -247,6 +267,33 @@ public class AndroidNetworkUtils {
|
|||
return res;
|
||||
}
|
||||
|
||||
public static String downloadFile(@NonNull String url, @NonNull File fileToSave) {
|
||||
String error = null;
|
||||
try {
|
||||
URLConnection connection = NetworkUtils.getHttpURLConnection(url);
|
||||
connection.setConnectTimeout(CONNECTION_TIMEOUT);
|
||||
connection.setReadTimeout(CONNECTION_TIMEOUT);
|
||||
BufferedInputStream inputStream = new BufferedInputStream(connection.getInputStream(), 8 * 1024);
|
||||
fileToSave.getParentFile().mkdirs();
|
||||
OutputStream stream = null;
|
||||
try {
|
||||
stream = new FileOutputStream(fileToSave);
|
||||
Algorithms.streamCopy(inputStream, stream);
|
||||
stream.flush();
|
||||
} finally {
|
||||
Algorithms.closeStream(inputStream);
|
||||
Algorithms.closeStream(stream);
|
||||
}
|
||||
} catch (UnknownHostException e) {
|
||||
error = e.getMessage();
|
||||
LOG.error("UnknownHostException, cannot download file " + url + " " + error);
|
||||
} catch (Exception e) {
|
||||
error = e.getMessage();
|
||||
LOG.warn("Cannot download file : " + url, e);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
private static final String BOUNDARY = "CowMooCowMooCowCowCow";
|
||||
|
||||
public static String uploadFile(String urlText, File file, boolean gzip, Map<String, String> additionalParams) throws IOException {
|
||||
|
|
|
@ -44,6 +44,7 @@ import androidx.fragment.app.FragmentManager;
|
|||
import androidx.fragment.app.FragmentManager.BackStackEntry;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceFragmentCompat;
|
||||
import androidx.preference.PreferenceFragmentCompat.OnPreferenceStartFragmentCallback;
|
||||
|
||||
import net.osmand.AndroidUtils;
|
||||
import net.osmand.Location;
|
||||
|
@ -62,15 +63,12 @@ import net.osmand.data.QuadRect;
|
|||
import net.osmand.data.RotatedTileBox;
|
||||
import net.osmand.map.MapTileDownloader.DownloadRequest;
|
||||
import net.osmand.map.MapTileDownloader.IMapDownloaderCallback;
|
||||
import net.osmand.map.TileSourceManager;
|
||||
import net.osmand.map.TileSourceManager.TileSourceTemplate;
|
||||
import net.osmand.plus.AppInitializer;
|
||||
import net.osmand.plus.AppInitializer.AppInitializeListener;
|
||||
import net.osmand.plus.AppInitializer.InitEvents;
|
||||
import net.osmand.plus.ApplicationMode;
|
||||
import net.osmand.plus.GpxSelectionHelper.GpxDisplayItem;
|
||||
import net.osmand.plus.HuaweiDrmHelper;
|
||||
import net.osmand.plus.MapMarkersHelper;
|
||||
import net.osmand.plus.MapMarkersHelper.MapMarker;
|
||||
import net.osmand.plus.MapMarkersHelper.MapMarkerChangedListener;
|
||||
import net.osmand.plus.OnDismissDialogFragmentListener;
|
||||
|
@ -103,11 +101,13 @@ import net.osmand.plus.firstusage.FirstUsageWelcomeFragment;
|
|||
import net.osmand.plus.firstusage.FirstUsageWizardFragment;
|
||||
import net.osmand.plus.helpers.AndroidUiHelper;
|
||||
import net.osmand.plus.helpers.DiscountHelper;
|
||||
import net.osmand.plus.helpers.ExternalApiHelper;
|
||||
import net.osmand.plus.helpers.ImportHelper;
|
||||
import net.osmand.plus.helpers.ImportHelper.ImportGpxBottomSheetDialogFragment;
|
||||
import net.osmand.plus.helpers.IntentHelper;
|
||||
import net.osmand.plus.helpers.LockHelper;
|
||||
import net.osmand.plus.helpers.LockHelper.LockUIAdapter;
|
||||
import net.osmand.plus.helpers.ScrollHelper;
|
||||
import net.osmand.plus.helpers.ScrollHelper.OnScrollEventListener;
|
||||
import net.osmand.plus.mapcontextmenu.AdditionalActionsBottomSheetDialogFragment;
|
||||
import net.osmand.plus.mapcontextmenu.MapContextMenu;
|
||||
import net.osmand.plus.mapcontextmenu.MenuController.MenuState;
|
||||
|
@ -162,10 +162,8 @@ import net.osmand.util.Algorithms;
|
|||
|
||||
import org.apache.commons.logging.Log;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -173,8 +171,6 @@ import java.util.Timer;
|
|||
import java.util.TimerTask;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_SETTINGS_ID;
|
||||
import static net.osmand.plus.OsmandSettings.GENERIC_EXTERNAL_DEVICE;
|
||||
|
@ -184,8 +180,8 @@ import static net.osmand.plus.OsmandSettings.WUNDERLINQ_EXTERNAL_DEVICE;
|
|||
public class MapActivity extends OsmandActionBarActivity implements DownloadEvents,
|
||||
OnRequestPermissionsResultCallback, IRouteInformationListener, AMapPointUpdateListener,
|
||||
MapMarkerChangedListener, OnDismissDialogFragmentListener, OnDrawMapListener,
|
||||
OsmAndAppCustomizationListener, LockHelper.LockUIAdapter, PreferenceFragmentCompat.OnPreferenceStartFragmentCallback,
|
||||
ScrollHelper.OnScrollEventListener {
|
||||
OsmAndAppCustomizationListener, LockUIAdapter, OnPreferenceStartFragmentCallback,
|
||||
OnScrollEventListener {
|
||||
|
||||
public static final String INTENT_KEY_PARENT_MAP_ACTIVITY = "intent_parent_map_activity_key";
|
||||
public static final String INTENT_PARAMS = "intent_prarams";
|
||||
|
@ -227,13 +223,15 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
|
|||
private OsmandApplication app;
|
||||
private OsmandSettings settings;
|
||||
|
||||
private boolean landscapeLayout;
|
||||
private LockHelper lockHelper;
|
||||
private ImportHelper importHelper;
|
||||
private IntentHelper intentHelper;
|
||||
private ScrollHelper mapScrollHelper;
|
||||
|
||||
private Dialog progressDlg = null;
|
||||
private boolean landscapeLayout;
|
||||
|
||||
private List<DialogProvider> dialogProviders = new ArrayList<>(2);
|
||||
private StateChangedListener<ApplicationMode> applicationModeListener;
|
||||
private ImportHelper importHelper;
|
||||
private boolean intentLocation = false;
|
||||
|
||||
private DashboardOnMap dashboardOnMap = new DashboardOnMap(this);
|
||||
|
@ -253,8 +251,6 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
|
|||
private boolean stopped = true;
|
||||
|
||||
private ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
|
||||
private LockHelper lockHelper;
|
||||
private ScrollHelper mapScrollHelper;
|
||||
|
||||
private StateChangedListener<Integer> mapScreenOrientationSettingListener = new StateChangedListener<Integer>() {
|
||||
@Override
|
||||
|
@ -304,11 +300,8 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
|
|||
AndroidUtils.addStatusBarPadding21v(this, findViewById(R.id.menuItems));
|
||||
}
|
||||
|
||||
int statusBarHeight = 0;
|
||||
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
|
||||
if (resourceId > 0) {
|
||||
statusBarHeight = getResources().getDimensionPixelSize(resourceId);
|
||||
}
|
||||
int statusBarHeight = AndroidUtils.getStatusBarHeight(this);
|
||||
|
||||
DisplayMetrics dm = new DisplayMetrics();
|
||||
getWindowManager().getDefaultDisplay().getMetrics(dm);
|
||||
int w = dm.widthPixels;
|
||||
|
@ -327,7 +320,10 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
|
|||
}
|
||||
dashboardOnMap.createDashboardView();
|
||||
checkAppInitialization();
|
||||
parseLaunchIntents();
|
||||
|
||||
intentHelper = new IntentHelper(this, getMyApplication());
|
||||
intentHelper.parseLaunchIntents();
|
||||
|
||||
mapView.setTrackBallDelegate(new OsmandMapTileView.OnTrackBallListener() {
|
||||
@Override
|
||||
public boolean onTrackBallEvent(MotionEvent e) {
|
||||
|
@ -769,7 +765,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
|
|||
@Override
|
||||
protected void onRestart() {
|
||||
super.onRestart();
|
||||
parseLaunchIntents();
|
||||
intentHelper.parseLaunchIntents();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -858,68 +854,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
|
|||
OsmandPlugin.checkInstalledMarketPlugins(app, this);
|
||||
OsmandPlugin.onMapActivityResume(this);
|
||||
|
||||
boolean showOsmAndWelcomeScreen = true;
|
||||
final Intent intent = getIntent();
|
||||
if (intent != null) {
|
||||
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
|
||||
final Uri data = intent.getData();
|
||||
if (data != null) {
|
||||
final String scheme = data.getScheme();
|
||||
if ("file".equals(scheme)) {
|
||||
final String path = data.getPath();
|
||||
if (path != null) {
|
||||
importHelper.handleFileImport(data, new File(path).getName(), intent.getExtras(), true);
|
||||
}
|
||||
clearIntent(intent);
|
||||
} else if ("content".equals(scheme)) {
|
||||
importHelper.handleContentImport(data, intent.getExtras(), true);
|
||||
clearIntent(intent);
|
||||
} else if ("google.navigation".equals(scheme) || "osmand.navigation".equals(scheme)) {
|
||||
parseNavigationIntent(data);
|
||||
} else if ("osmand.api".equals(scheme)) {
|
||||
ExternalApiHelper apiHelper = new ExternalApiHelper(this);
|
||||
Intent result = apiHelper.processApiRequest(intent);
|
||||
setResult(apiHelper.getResultCode(), result);
|
||||
result.setAction(null);
|
||||
setIntent(result);
|
||||
if (apiHelper.needFinish()) {
|
||||
finish();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (intent.hasExtra(FirstUsageWelcomeFragment.SHOW_OSMAND_WELCOME_SCREEN)) {
|
||||
showOsmAndWelcomeScreen = intent.getBooleanExtra(FirstUsageWelcomeFragment.SHOW_OSMAND_WELCOME_SCREEN, true);
|
||||
}
|
||||
if (intent.hasExtra(MapMarkersDialogFragment.OPEN_MAP_MARKERS_GROUPS)) {
|
||||
Bundle openMapMarkersGroupsExtra = intent.getBundleExtra(MapMarkersDialogFragment.OPEN_MAP_MARKERS_GROUPS);
|
||||
if (openMapMarkersGroupsExtra != null) {
|
||||
MapMarkersDialogFragment.showInstance(this, openMapMarkersGroupsExtra.getString(MapMarkersHelper.MapMarkersGroup.MARKERS_SYNC_GROUP_ID));
|
||||
}
|
||||
setIntent(null);
|
||||
}
|
||||
if (intent.hasExtra(BaseSettingsFragment.OPEN_SETTINGS)) {
|
||||
String settingsType = intent.getStringExtra(BaseSettingsFragment.OPEN_SETTINGS);
|
||||
String appMode = intent.getStringExtra(BaseSettingsFragment.APP_MODE_KEY);
|
||||
if (BaseSettingsFragment.OPEN_CONFIG_PROFILE.equals(settingsType)) {
|
||||
BaseSettingsFragment.showInstance(this, SettingsScreenType.CONFIGURE_PROFILE, ApplicationMode.valueOfStringKey(appMode, null));
|
||||
}
|
||||
setIntent(null);
|
||||
}
|
||||
if (intent.hasExtra(BaseSettingsFragment.OPEN_CONFIG_ON_MAP)) {
|
||||
switch (intent.getStringExtra(BaseSettingsFragment.OPEN_CONFIG_ON_MAP)) {
|
||||
case BaseSettingsFragment.MAP_CONFIG:
|
||||
this.getDashboard().setDashboardVisibility(true, DashboardType.CONFIGURE_MAP, null);
|
||||
break;
|
||||
|
||||
case BaseSettingsFragment.SCREEN_CONFIG:
|
||||
this.getDashboard().setDashboardVisibility(true, DashboardType.CONFIGURE_SCREEN, null);
|
||||
break;
|
||||
}
|
||||
setIntent(null);
|
||||
}
|
||||
|
||||
}
|
||||
intentHelper.parseContentIntent();
|
||||
mapView.refreshMap(true);
|
||||
if (atlasMapRendererView != null) {
|
||||
atlasMapRendererView.handleOnResume();
|
||||
|
@ -947,6 +882,11 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
|
|||
System.err.println("OnCreate for MapActivity took " + (System.currentTimeMillis() - tm) + " ms");
|
||||
}
|
||||
|
||||
boolean showOsmAndWelcomeScreen = true;
|
||||
final Intent intent = getIntent();
|
||||
if (intent != null && intent.hasExtra(FirstUsageWelcomeFragment.SHOW_OSMAND_WELCOME_SCREEN)) {
|
||||
showOsmAndWelcomeScreen = intent.getBooleanExtra(FirstUsageWelcomeFragment.SHOW_OSMAND_WELCOME_SCREEN, true);
|
||||
}
|
||||
boolean showWelcomeScreen = ((app.getAppInitializer().isFirstTime() && Version.isDeveloperVersion(app)) || !app.getResourceManager().isAnyMapInstalled())
|
||||
&& FirstUsageWelcomeFragment.SHOW && settings.SHOW_OSMAND_WELCOME_SCREEN.get() && showOsmAndWelcomeScreen;
|
||||
|
||||
|
@ -1035,11 +975,6 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
|
|||
}
|
||||
}
|
||||
|
||||
private void clearIntent(Intent intent) {
|
||||
intent.setAction(null);
|
||||
intent.setData(null);
|
||||
}
|
||||
|
||||
public void updateStatusBarColor() {
|
||||
if (Build.VERSION.SDK_INT >= 21) {
|
||||
int colorId = -1;
|
||||
|
@ -1233,32 +1168,6 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
|
|||
}
|
||||
}
|
||||
|
||||
private void parseNavigationIntent(final Uri data) {
|
||||
final String schemeSpecificPart = data.getSchemeSpecificPart();
|
||||
|
||||
final Matcher matcher = Pattern.compile("(?:q|ll)=([\\-0-9.]+),([\\-0-9.]+)(?:.*)").matcher(
|
||||
schemeSpecificPart);
|
||||
if (matcher.matches()) {
|
||||
try {
|
||||
final double lat = Double.valueOf(matcher.group(1));
|
||||
final double lon = Double.valueOf(matcher.group(2));
|
||||
|
||||
getMyApplication().getTargetPointsHelper().navigateToPoint(new LatLon(lat, lon), false,
|
||||
-1);
|
||||
getMapActions().enterRoutePlanningModeGivenGpx(null, null, null, false, true);
|
||||
} catch (NumberFormatException e) {
|
||||
Toast.makeText(this,
|
||||
getString(R.string.navigation_intent_invalid, schemeSpecificPart),
|
||||
Toast.LENGTH_LONG).show(); //$NON-NLS-1$
|
||||
}
|
||||
} else {
|
||||
Toast.makeText(this,
|
||||
getString(R.string.navigation_intent_invalid, schemeSpecificPart),
|
||||
Toast.LENGTH_LONG).show(); //$NON-NLS-1$
|
||||
}
|
||||
clearIntent(getIntent());
|
||||
}
|
||||
|
||||
public void readLocationToShow() {
|
||||
showMapControls();
|
||||
|
||||
|
@ -1438,18 +1347,10 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
|
|||
getMyApplication().getNotificationHelper().showNotifications();
|
||||
}
|
||||
|
||||
protected void setProgressDlg(Dialog progressDlg) {
|
||||
this.progressDlg = progressDlg;
|
||||
}
|
||||
|
||||
protected Dialog getProgressDlg() {
|
||||
return progressDlg;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
getMyApplication().getNotificationHelper().removeNotifications(true);
|
||||
if(pendingPause) {
|
||||
if (pendingPause) {
|
||||
onPauseActivity();
|
||||
}
|
||||
stopped = true;
|
||||
|
@ -1746,80 +1647,6 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
|
|||
return mapViewTrackingUtilities;
|
||||
}
|
||||
|
||||
private boolean parseLaunchIntents() {
|
||||
boolean applied = parseLocationIntent();
|
||||
if (!applied) {
|
||||
applied = parseTileSourceIntent();
|
||||
}
|
||||
return applied;
|
||||
}
|
||||
|
||||
private boolean parseLocationIntent() {
|
||||
Intent intent = getIntent();
|
||||
if (intent != null && intent.getData() != null) {
|
||||
Uri data = intent.getData();
|
||||
if (("http".equalsIgnoreCase(data.getScheme()) || "https".equalsIgnoreCase(data.getScheme())) && data.getHost() != null && data.getHost().contains("osmand.net") &&
|
||||
data.getPath() != null && data.getPath().startsWith("/go")) {
|
||||
String lat = data.getQueryParameter("lat");
|
||||
String lon = data.getQueryParameter("lon");
|
||||
String url = data.getQueryParameter("url");
|
||||
if (lat != null && lon != null) {
|
||||
try {
|
||||
double lt = Double.parseDouble(lat);
|
||||
double ln = Double.parseDouble(lon);
|
||||
String zoom = data.getQueryParameter("z");
|
||||
int z = settings.getLastKnownMapZoom();
|
||||
if (zoom != null) {
|
||||
z = Integer.parseInt(zoom);
|
||||
}
|
||||
settings.setMapLocationToShow(lt, ln, z, new PointDescription(lt, ln));
|
||||
} catch (NumberFormatException e) {
|
||||
LOG.error("error", e);
|
||||
}
|
||||
} else if (url != null) {
|
||||
url = DiscountHelper.parseUrl(app, url);
|
||||
if (DiscountHelper.validateUrl(app, url)) {
|
||||
DiscountHelper.openUrl(this, url);
|
||||
}
|
||||
}
|
||||
setIntent(null);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean parseTileSourceIntent() {
|
||||
Intent intent = getIntent();
|
||||
if (intent != null && intent.getData() != null) {
|
||||
Uri data = intent.getData();
|
||||
if (("http".equalsIgnoreCase(data.getScheme()) || "https".equalsIgnoreCase(data.getScheme())) && data.getHost() != null && data.getHost().contains("osmand.net") &&
|
||||
data.getPath() != null && data.getPath().startsWith("/add-tile-source")) {
|
||||
Map<String, String> attrs = new HashMap<>();
|
||||
for (String name : data.getQueryParameterNames()) {
|
||||
String value = data.getQueryParameter(name);
|
||||
if (value != null) {
|
||||
attrs.put(name, value);
|
||||
}
|
||||
}
|
||||
if (!attrs.isEmpty()) {
|
||||
try {
|
||||
TileSourceTemplate r = TileSourceManager.createTileSourceTemplate(attrs);
|
||||
if (r != null && settings.installTileSource(r)) {
|
||||
Toast.makeText(this, getString(R.string.edit_tilesource_successfully, r.getName()),
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.error("parseAddTileSourceIntent error", e);
|
||||
}
|
||||
}
|
||||
setIntent(null);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public MapActivityActions getMapActions() {
|
||||
return mapActions;
|
||||
}
|
||||
|
@ -2573,5 +2400,4 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
|
|||
return this != NEW && this != NEW_IF_EXPIRED && this != CURRENT;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
254
OsmAnd/src/net/osmand/plus/helpers/IntentHelper.java
Normal file
254
OsmAnd/src/net/osmand/plus/helpers/IntentHelper.java
Normal file
|
@ -0,0 +1,254 @@
|
|||
package net.osmand.plus.helpers;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
|
||||
import net.osmand.AndroidNetworkUtils;
|
||||
import net.osmand.CallbackWithObject;
|
||||
import net.osmand.IndexConstants;
|
||||
import net.osmand.PlatformUtil;
|
||||
import net.osmand.data.LatLon;
|
||||
import net.osmand.data.PointDescription;
|
||||
import net.osmand.map.TileSourceManager;
|
||||
import net.osmand.plus.ApplicationMode;
|
||||
import net.osmand.plus.MapMarkersHelper;
|
||||
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.dashboard.DashboardOnMap.DashboardType;
|
||||
import net.osmand.plus.mapmarkers.MapMarkersDialogFragment;
|
||||
import net.osmand.plus.settings.BaseSettingsFragment;
|
||||
import net.osmand.plus.settings.BaseSettingsFragment.SettingsScreenType;
|
||||
import net.osmand.util.Algorithms;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class IntentHelper {
|
||||
|
||||
private static final Log LOG = PlatformUtil.getLog(IntentHelper.class);
|
||||
|
||||
private final OsmandApplication app;
|
||||
private final OsmandSettings settings;
|
||||
private final MapActivity mapActivity;
|
||||
|
||||
public IntentHelper(MapActivity mapActivity, OsmandApplication app) {
|
||||
this.app = app;
|
||||
this.mapActivity = mapActivity;
|
||||
this.settings = app.getSettings();
|
||||
}
|
||||
|
||||
public boolean parseLaunchIntents() {
|
||||
boolean applied = parseLocationIntent();
|
||||
if (!applied) {
|
||||
applied = parseTileSourceIntent();
|
||||
}
|
||||
if (!applied) {
|
||||
applied = parseOpenGpxIntent();
|
||||
}
|
||||
return applied;
|
||||
}
|
||||
|
||||
private boolean parseLocationIntent() {
|
||||
Intent intent = mapActivity.getIntent();
|
||||
if (intent != null && intent.getData() != null) {
|
||||
Uri data = intent.getData();
|
||||
if (("http".equalsIgnoreCase(data.getScheme()) || "https".equalsIgnoreCase(data.getScheme()))
|
||||
&& data.getHost() != null && data.getHost().contains("osmand.net") &&
|
||||
data.getPath() != null && data.getPath().startsWith("/go")) {
|
||||
String lat = data.getQueryParameter("lat");
|
||||
String lon = data.getQueryParameter("lon");
|
||||
String url = data.getQueryParameter("url");
|
||||
if (lat != null && lon != null) {
|
||||
try {
|
||||
double lt = Double.parseDouble(lat);
|
||||
double ln = Double.parseDouble(lon);
|
||||
String zoom = data.getQueryParameter("z");
|
||||
int z = settings.getLastKnownMapZoom();
|
||||
if (zoom != null) {
|
||||
z = Integer.parseInt(zoom);
|
||||
}
|
||||
settings.setMapLocationToShow(lt, ln, z, new PointDescription(lt, ln));
|
||||
} catch (NumberFormatException e) {
|
||||
LOG.error("error", e);
|
||||
}
|
||||
} else if (url != null) {
|
||||
url = DiscountHelper.parseUrl(app, url);
|
||||
if (DiscountHelper.validateUrl(app, url)) {
|
||||
DiscountHelper.openUrl(mapActivity, url);
|
||||
}
|
||||
}
|
||||
mapActivity.setIntent(null);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean parseTileSourceIntent() {
|
||||
Intent intent = mapActivity.getIntent();
|
||||
if (intent != null && intent.getData() != null) {
|
||||
Uri data = intent.getData();
|
||||
if (("http".equalsIgnoreCase(data.getScheme()) || "https".equalsIgnoreCase(data.getScheme()))
|
||||
&& data.getHost() != null && data.getHost().contains("osmand.net") &&
|
||||
data.getPath() != null && data.getPath().startsWith("/add-tile-source")) {
|
||||
Map<String, String> attrs = new HashMap<>();
|
||||
for (String name : data.getQueryParameterNames()) {
|
||||
String value = data.getQueryParameter(name);
|
||||
if (value != null) {
|
||||
attrs.put(name, value);
|
||||
}
|
||||
}
|
||||
if (!attrs.isEmpty()) {
|
||||
try {
|
||||
TileSourceManager.TileSourceTemplate r = TileSourceManager.createTileSourceTemplate(attrs);
|
||||
if (r != null && settings.installTileSource(r)) {
|
||||
app.showShortToastMessage(app.getString(R.string.edit_tilesource_successfully, r.getName()));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.error("parseAddTileSourceIntent error", e);
|
||||
}
|
||||
}
|
||||
mapActivity.setIntent(null);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean parseOpenGpxIntent() {
|
||||
Intent intent = mapActivity.getIntent();
|
||||
if (intent != null && intent.getData() != null) {
|
||||
Uri data = intent.getData();
|
||||
if (("http".equalsIgnoreCase(data.getScheme()) || "https".equalsIgnoreCase(data.getScheme()))
|
||||
&& data.getHost() != null && data.getHost().contains("osmand.net")
|
||||
&& data.getPath() != null && data.getPath().startsWith("/open-gpx")) {
|
||||
String url = data.getQueryParameter("url");
|
||||
if (Algorithms.isEmpty(url)) {
|
||||
return false;
|
||||
}
|
||||
String name = data.getQueryParameter("name");
|
||||
if (Algorithms.isEmpty(name)) {
|
||||
name = Algorithms.getFileWithoutDirs(url);
|
||||
}
|
||||
if (!name.endsWith(IndexConstants.GPX_FILE_EXT)) {
|
||||
name += IndexConstants.GPX_FILE_EXT;
|
||||
}
|
||||
final String fileName = name;
|
||||
AndroidNetworkUtils.downloadFileAsync(url, app.getAppPath(IndexConstants.GPX_IMPORT_DIR + fileName),
|
||||
new CallbackWithObject<String>() {
|
||||
@Override
|
||||
public boolean processResult(String error) {
|
||||
if (error == null) {
|
||||
String downloaded = app.getString(R.string.shared_string_download_successful);
|
||||
app.showShortToastMessage(app.getString(R.string.ltr_or_rtl_combine_via_colon, downloaded, fileName));
|
||||
} else {
|
||||
app.showShortToastMessage(app.getString(R.string.error_occurred_loading_gpx));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
mapActivity.setIntent(null);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void parseContentIntent() {
|
||||
final Intent intent = mapActivity.getIntent();
|
||||
if (intent != null) {
|
||||
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
|
||||
final Uri data = intent.getData();
|
||||
if (data != null) {
|
||||
final String scheme = data.getScheme();
|
||||
if ("file".equals(scheme)) {
|
||||
final String path = data.getPath();
|
||||
if (path != null) {
|
||||
mapActivity.getImportHelper().handleFileImport(data, new File(path).getName(), intent.getExtras(), true);
|
||||
}
|
||||
clearIntent(intent);
|
||||
} else if ("content".equals(scheme)) {
|
||||
mapActivity.getImportHelper().handleContentImport(data, intent.getExtras(), true);
|
||||
clearIntent(intent);
|
||||
} else if ("google.navigation".equals(scheme) || "osmand.navigation".equals(scheme)) {
|
||||
parseNavigationIntent(intent);
|
||||
} else if ("osmand.api".equals(scheme)) {
|
||||
ExternalApiHelper apiHelper = new ExternalApiHelper(mapActivity);
|
||||
Intent result = apiHelper.processApiRequest(intent);
|
||||
mapActivity.setResult(apiHelper.getResultCode(), result);
|
||||
result.setAction(null);
|
||||
mapActivity.setIntent(result);
|
||||
if (apiHelper.needFinish()) {
|
||||
mapActivity.finish();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (intent.hasExtra(MapMarkersDialogFragment.OPEN_MAP_MARKERS_GROUPS)) {
|
||||
Bundle openMapMarkersGroupsExtra = intent.getBundleExtra(MapMarkersDialogFragment.OPEN_MAP_MARKERS_GROUPS);
|
||||
if (openMapMarkersGroupsExtra != null) {
|
||||
MapMarkersDialogFragment.showInstance(mapActivity, openMapMarkersGroupsExtra.getString(MapMarkersHelper.MapMarkersGroup.MARKERS_SYNC_GROUP_ID));
|
||||
}
|
||||
mapActivity.setIntent(null);
|
||||
}
|
||||
if (intent.hasExtra(BaseSettingsFragment.OPEN_SETTINGS)) {
|
||||
String settingsType = intent.getStringExtra(BaseSettingsFragment.OPEN_SETTINGS);
|
||||
String appMode = intent.getStringExtra(BaseSettingsFragment.APP_MODE_KEY);
|
||||
if (BaseSettingsFragment.OPEN_CONFIG_PROFILE.equals(settingsType)) {
|
||||
BaseSettingsFragment.showInstance(mapActivity, SettingsScreenType.CONFIGURE_PROFILE, ApplicationMode.valueOfStringKey(appMode, null));
|
||||
}
|
||||
mapActivity.setIntent(null);
|
||||
}
|
||||
if (intent.hasExtra(BaseSettingsFragment.OPEN_CONFIG_ON_MAP)) {
|
||||
switch (intent.getStringExtra(BaseSettingsFragment.OPEN_CONFIG_ON_MAP)) {
|
||||
case BaseSettingsFragment.MAP_CONFIG:
|
||||
mapActivity.getDashboard().setDashboardVisibility(true, DashboardType.CONFIGURE_MAP, null);
|
||||
break;
|
||||
|
||||
case BaseSettingsFragment.SCREEN_CONFIG:
|
||||
mapActivity.getDashboard().setDashboardVisibility(true, DashboardType.CONFIGURE_SCREEN, null);
|
||||
break;
|
||||
}
|
||||
mapActivity.setIntent(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void parseNavigationIntent(Intent intent) {
|
||||
Uri data = intent.getData();
|
||||
if (data != null) {
|
||||
final String schemeSpecificPart = data.getSchemeSpecificPart();
|
||||
|
||||
final Matcher matcher = Pattern.compile("(?:q|ll)=([\\-0-9.]+),([\\-0-9.]+)(?:.*)").matcher(schemeSpecificPart);
|
||||
if (matcher.matches()) {
|
||||
try {
|
||||
double lat = Double.parseDouble(matcher.group(1));
|
||||
double lon = Double.parseDouble(matcher.group(2));
|
||||
|
||||
app.getTargetPointsHelper().navigateToPoint(new LatLon(lat, lon), false, -1);
|
||||
mapActivity.getMapActions().enterRoutePlanningModeGivenGpx(null, null, null, false, true);
|
||||
} catch (NumberFormatException e) {
|
||||
app.showToastMessage(app.getString(R.string.navigation_intent_invalid, schemeSpecificPart));
|
||||
}
|
||||
} else {
|
||||
app.showToastMessage(app.getString(R.string.navigation_intent_invalid, schemeSpecificPart));
|
||||
}
|
||||
clearIntent(intent);
|
||||
}
|
||||
}
|
||||
|
||||
private void clearIntent(Intent intent) {
|
||||
intent.setAction(null);
|
||||
intent.setData(null);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue