This commit is contained in:
Vitaliy 2020-05-15 20:21:23 +03:00
parent 0c21e52a78
commit d3ab1445aa
3 changed files with 107 additions and 0 deletions

View file

@ -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

View file

@ -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 {

View file

@ -45,7 +45,10 @@ import androidx.fragment.app.FragmentManager.BackStackEntry;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import net.osmand.AndroidNetworkUtils;
import net.osmand.AndroidUtils;
import net.osmand.CallbackWithObject;
import net.osmand.IndexConstants;
import net.osmand.Location;
import net.osmand.PlatformUtil;
import net.osmand.SecondSplashScreenFragment;
@ -1751,6 +1754,9 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
if (!applied) {
applied = parseTileSourceIntent();
}
if (!applied) {
applied = parseOpenGpxIntent();
}
return applied;
}
@ -1820,6 +1826,46 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
return false;
}
private boolean parseOpenGpxIntent() {
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("/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;
}
});
setIntent(null);
return true;
}
}
return false;
}
public MapActivityActions getMapActions() {
return mapActions;
}