Skip to content
This repository was archived by the owner on Jan 21, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 44 additions & 7 deletions app/src/main/java/com/indieweb/indigenous/post/TripActivity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.indieweb.indigenous.post;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.OpenableColumns;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
Expand Down Expand Up @@ -92,13 +94,23 @@ public boolean onCreateOptionsMenu(Menu menu) {
public boolean onOptionsItemSelected(@NonNull MenuItem item) {

if (item.getItemId() == R.id.loadGpx) {
Intent ii = new Intent();
ii.setType("application/gpx+xml");
ii.setAction(Intent.ACTION_OPEN_DOCUMENT);
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);

// Allow all file types - GPX files may have various MIME types
// (application/gpx+xml, application/octet-stream, text/xml, etc.)
// The app will validate the file content when parsing
intent.setType("*/*");

// Grant read permission
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);

if (!isMediaRequest) {
ii.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
}
startActivityForResult(Intent.createChooser(ii, getString(R.string.trip_load_gpx)), PICK_GPX_REQUEST);

startActivityForResult(intent, PICK_GPX_REQUEST);
return true;
}

Expand All @@ -110,13 +122,38 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == PICK_GPX_REQUEST && resultCode == RESULT_OK) {
if (data.getData() != null) {
parseGPXfile(data.getData(), data, true);
Uri uri = data.getData();
if (isGpxFile(uri)) {
parseGPXfile(uri, data, true);
} else {
Snackbar.make(layout, getString(R.string.trip_invalid_file_type), Snackbar.LENGTH_LONG).show();
}
}
}

super.onActivityResult(requestCode, resultCode, data);
}

/**
* Check if the selected file is a GPX file by extension.
*
* @param uri The file uri.
* @return true if the file has a .gpx extension.
*/
private boolean isGpxFile(Uri uri) {
String fileName = null;
try (Cursor cursor = getContentResolver().query(uri, null, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) {
int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
if (nameIndex >= 0) {
fileName = cursor.getString(nameIndex);
}
}
} catch (Exception ignored) {
}
return fileName != null && fileName.toLowerCase().endsWith(".gpx");
}

/**
* Parse a GPX file.
*
Expand Down Expand Up @@ -218,7 +255,7 @@ public void onPostButtonClick(MenuItem item) {
}

if (transport.getSelectedItemPosition() != 0) {
String t = getResources().getStringArray(R.array.transport_array_values)[read.getSelectedItemPosition()];
String t = getResources().getStringArray(R.array.transport_array_values)[transport.getSelectedItemPosition()];
bodyParams.put("transport", t);
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@
<string name="trip_load_gpx">Load GPX file</string>
<string name="trip_reading_error">Error reading GPX file: %s</string>
<string name="trip_no_points_found">No points found in GPX file.</string>
<string name="trip_invalid_file_type">Please select a GPX file (.gpx)</string>
<string name="location_start">Start location</string>
<string name="location_end">End location</string>
<string name="transport">Transport</string>
Expand Down