Skip to content

Commit

Permalink
Import MSF files on Android 10+ and correctness fixes
Browse files Browse the repository at this point in the history
This adds importing MSF files on Android 10 and later, including
displaying a tip the 1st time a file is "imported".

Fixes #2455

This further corrects the behaviour when files are selected and the
initial activity is paused and potentially destroyed by using the
restored activity and using the fragment manager to find fragments that
need to be updated from the imported data.
  • Loading branch information
simonpoole committed Dec 13, 2023
1 parent 52650df commit 55c776c
Show file tree
Hide file tree
Showing 18 changed files with 555 additions and 343 deletions.
54 changes: 27 additions & 27 deletions src/main/java/de/blau/android/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2288,8 +2288,8 @@ protected void onPostExecute(Void result) {
private static final long serialVersionUID = 1L;

@Override
public boolean save(Uri fileUri) {
SavingHelper.asyncExport(Main.this, delegator, fileUri);
public boolean save(FragmentActivity currentActivity, Uri fileUri) {
SavingHelper.asyncExport(currentActivity, delegator, fileUri);
SelectFile.savePref(prefs, R.string.config_osmPreferredDir_key, fileUri);
return true;
}
Expand All @@ -2301,9 +2301,9 @@ public boolean save(Uri fileUri) {
private static final long serialVersionUID = 1L;

@Override
public boolean read(Uri fileUri) {
public boolean read(FragmentActivity currentActivity, Uri fileUri) {
try {
logic.applyOscFile(Main.this, fileUri, new PostFileReadCallback(Main.this, fileUri.toString()));
logic.applyOscFile(currentActivity, fileUri, new PostFileReadCallback(currentActivity, fileUri.toString()));
} catch (FileNotFoundException e) {
fileNotFound(fileUri);
}
Expand All @@ -2320,12 +2320,12 @@ public boolean read(Uri fileUri) {
private static final long serialVersionUID = 1L;

@Override
public boolean read(Uri fileUri) {
public boolean read(FragmentActivity currentActivity, Uri fileUri) {
try {
if (item.getItemId() == R.id.menu_transfer_read_file) {
logic.readOsmFile(Main.this, fileUri, false);
logic.readOsmFile(currentActivity, fileUri, false);
} else {
logic.readPbfFile(Main.this, fileUri, false);
logic.readPbfFile(currentActivity, fileUri, false);
}
} catch (FileNotFoundException e) {
fileNotFound(fileUri);
Expand All @@ -2347,8 +2347,8 @@ public boolean read(Uri fileUri) {
private static final long serialVersionUID = 1L;

@Override
public boolean save(Uri fileUri) {
App.getLogic().writeOsmFile(Main.this, fileUri, new PostFileWriteCallback(Main.this, fileUri.getPath()));
public boolean save(FragmentActivity currentActivity, Uri fileUri) {
App.getLogic().writeOsmFile(currentActivity, fileUri, new PostFileWriteCallback(currentActivity, fileUri.getPath()));
SelectFile.savePref(prefs, R.string.config_osmPreferredDir_key, fileUri);
return true;
}
Expand Down Expand Up @@ -2392,9 +2392,9 @@ public boolean save(Uri fileUri) {
private static final long serialVersionUID = 1L;

@Override
public boolean save(Uri fileUri) {
TransferTasks.writeOsnFile(Main.this, item.getItemId() == R.id.menu_transfer_save_notes_all, fileUri,
new PostFileWriteCallback(Main.this, fileUri.toString()));
public boolean save(FragmentActivity currentActivity, Uri fileUri) {
TransferTasks.writeOsnFile(currentActivity, item.getItemId() == R.id.menu_transfer_save_notes_all, fileUri,
new PostFileWriteCallback(currentActivity, fileUri.toString()));
SelectFile.savePref(prefs, R.string.config_notesPreferredDir_key, fileUri);
return true;
}
Expand All @@ -2406,8 +2406,8 @@ public boolean save(Uri fileUri) {
private static final long serialVersionUID = 1L;

@Override
public boolean read(Uri fileUri) {
TransferTasks.readOsnFile(Main.this, fileUri, true, new PostFileReadCallback(Main.this, fileUri.toString()));
public boolean read(FragmentActivity currentActivity, Uri fileUri) {
TransferTasks.readOsnFile(currentActivity, fileUri, true, new PostFileReadCallback(currentActivity, fileUri.toString()));
SelectFile.savePref(prefs, R.string.config_osmPreferredDir_key, fileUri);
map.invalidate();
return true;
Expand All @@ -2420,8 +2420,8 @@ public boolean read(Uri fileUri) {
private static final long serialVersionUID = 1L;

@Override
public boolean read(Uri fileUri) {
TransferTasks.readTodos(Main.this, fileUri, false, new PostFileReadCallback(Main.this, fileUri.toString()));
public boolean read(FragmentActivity currentActivity, Uri fileUri) {
TransferTasks.readTodos(currentActivity, fileUri, false, new PostFileReadCallback(currentActivity, fileUri.toString()));
SelectFile.savePref(prefs, R.string.config_osmPreferredDir_key, fileUri);
map.invalidate();
return true;
Expand Down Expand Up @@ -2488,9 +2488,9 @@ public boolean read(Uri fileUri) {
private static final long serialVersionUID = 1L;

@Override
public boolean read(Uri fileUri) {
try (KeyDatabaseHelper keyDatabase = new KeyDatabaseHelper(Main.this)) {
keyDatabase.keysFromStream(Main.this, Main.this.getContentResolver().openInputStream(fileUri));
public boolean read(FragmentActivity currentActivity, Uri fileUri) {
try (KeyDatabaseHelper keyDatabase = new KeyDatabaseHelper(currentActivity)) {
keyDatabase.keysFromStream(currentActivity, currentActivity.getContentResolver().openInputStream(fileUri));
SelectFile.savePref(prefs, R.string.config_osmPreferredDir_key, fileUri);
} catch (FileNotFoundException fex) {
fileNotFound(fileUri);
Expand All @@ -2505,18 +2505,18 @@ public boolean read(Uri fileUri) {
private static final long serialVersionUID = 1L;

@Override
public boolean read(Uri fileUri) {
try (InputStream in = Main.this.getContentResolver().openInputStream(fileUri)) {
File destDir = FileUtil.getApplicationDirectory(Main.this, Paths.DIRECTORY_PATH_STYLES);
String filename = ContentResolverUtil.getDisplaynameColumn(Main.this, fileUri);
public boolean read(FragmentActivity currentActivity, Uri fileUri) {
try (InputStream in = currentActivity.getContentResolver().openInputStream(fileUri)) {
File destDir = FileUtil.getApplicationDirectory(currentActivity, Paths.DIRECTORY_PATH_STYLES);
String filename = ContentResolverUtil.getDisplaynameColumn(currentActivity, fileUri);
File dest = new File(destDir, filename);
FileUtil.copy(in, dest);
if (filename.toLowerCase(Locale.US).endsWith("." + FileExtensions.ZIP)) {
FileUtil.unpackZip(destDir.getAbsolutePath() + Paths.DELIMITER, filename);
dest.delete(); // NOSONAR delete the zip file
}
DataStyle.reset();
DataStyle.getStylesFromFiles(Main.this);
DataStyle.getStylesFromFiles(currentActivity);
SelectFile.savePref(prefs, R.string.config_osmPreferredDir_key, fileUri);
} catch (IOException fex) {
fileNotFound(fileUri);
Expand Down Expand Up @@ -2666,8 +2666,8 @@ private void writeTodos(@Nullable String listName) {
private static final long serialVersionUID = 1L;

@Override
public boolean save(Uri fileUri) {
TransferTasks.writeTodoFile(Main.this, fileUri, listName, true, null);
public boolean save(FragmentActivity currentActivity, Uri fileUri) {
TransferTasks.writeTodoFile(currentActivity, fileUri, listName, true, null);
SelectFile.savePref(prefs, R.string.config_osmPreferredDir_key, fileUri);
return true;
}
Expand Down Expand Up @@ -3060,7 +3060,7 @@ protected void onActivityResult(final int requestCode, final int resultCode, fin
if (requestCode == REQUEST_EDIT_TAG && resultCode == RESULT_OK) {
handlePropertyEditorResult();
} else if ((requestCode == SelectFile.READ_FILE || requestCode == SelectFile.SAVE_FILE) && resultCode == RESULT_OK) {
SelectFile.handleResult(requestCode, data);
SelectFile.handleResult(this, requestCode, data);
} else {
ActivityResultHandler.Listener listener = activityResultListeners.get(requestCode);
if (listener != null) {
Expand Down
26 changes: 20 additions & 6 deletions src/main/java/de/blau/android/dialogs/ConsoleDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public class ConsoleDialog extends DialogFragment {
private static final String CHECKBOX1_KEY = "checkbox1";
private static final String CHECKBOX2_KEY = "checkbox2";

private EditText input;

/**
* Show an info dialog for the supplied OsmElement
*
Expand Down Expand Up @@ -136,7 +138,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
final LayoutInflater inflater = ThemeUtils.getLayoutInflater(activity);

View v = inflater.inflate(R.layout.console, null);
final EditText input = (EditText) v.findViewById(R.id.input);
input = (EditText) v.findViewById(R.id.input);
final TextView output = (TextView) v.findViewById(R.id.output);
final CheckBox checkbox1 = (CheckBox) v.findViewById(R.id.checkbox1);
final CheckBox checkbox2 = (CheckBox) v.findViewById(R.id.checkbox2);
Expand Down Expand Up @@ -224,13 +226,19 @@ private OnMenuItemClickListener getOnItemClickListener(@NonNull final Preference
private static final long serialVersionUID = 1L;

@Override
public boolean save(Uri fileUri) {
fileUri = FileUtil.contentUriToFileUri(activity, fileUri);
public boolean save(FragmentActivity currentActivity, Uri fileUri) {
FragmentManager fm = currentActivity.getSupportFragmentManager();
ConsoleDialog fragment = (ConsoleDialog) fm.findFragmentByTag(TAG);
if (fragment == null) {
Log.e(DEBUG_TAG, "Restored fragment is null");
return false;
}
fileUri = FileUtil.contentUriToFileUri(currentActivity, fileUri);
if (fileUri == null) {
Log.e(DEBUG_TAG, "Couldn't convert " + fileUri);
return false;
}
writeScriptFile(activity, fileUri, input.getText().toString(), null);
writeScriptFile(currentActivity, fileUri, fragment.input.getText().toString(), null);
SelectFile.savePref(prefs, R.string.config_scriptsPreferredDir_key, fileUri);
return true;
}
Expand All @@ -241,8 +249,14 @@ public boolean save(Uri fileUri) {
private static final long serialVersionUID = 1L;

@Override
public boolean read(Uri fileUri) {
readScriptFile(activity, fileUri, input, null);
public boolean read(FragmentActivity currentActivity, Uri fileUri) {
FragmentManager fm = currentActivity.getSupportFragmentManager();
ConsoleDialog fragment = (ConsoleDialog) fm.findFragmentByTag(TAG);
if (fragment == null) {
Log.e(DEBUG_TAG, "Restored fragment is null");
return false;
}
readScriptFile(currentActivity, fileUri, fragment.input, null);
SelectFile.savePref(prefs, R.string.config_scriptsPreferredDir_key, fileUri);
return true;
}
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/de/blau/android/dialogs/Layers.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public class Layers extends AbstractConfigurationDialog implements OnUpdateListe
private static final int VERTICAL_OFFSET = 64;
private static final long MAX_STYLE_FILE_SIZE = 10000000L;

private static final String TAG = "fragment_layers";
public static final String TAG = "fragment_layers";

private int visibleId;
private int invisibleId;
Expand Down Expand Up @@ -410,13 +410,13 @@ private void addStyleableLayerFromFile(final FragmentActivity activity, final Pr
private static final long serialVersionUID = 1L;

@Override
public boolean read(Uri fileUri) {
public boolean read(FragmentActivity activity, Uri fileUri) {
addStyleableLayerFromUri(activity, prefs, map, type, fileUri, true);
return true;
}

@Override
public void read(List<Uri> fileUris) {
public void read(FragmentActivity activity, List<Uri> fileUris) {
for (Uri fileUri : fileUris) {
addStyleableLayerFromUri(activity, prefs, map, type, fileUri, false);
}
Expand Down Expand Up @@ -471,7 +471,7 @@ private void addMVTLayerFromStyle(@NonNull final FragmentActivity activity, @Non
private static final long serialVersionUID = 1L;

@Override
public boolean read(Uri fileUri) {
public boolean read(FragmentActivity activity, Uri fileUri) {
Style style = new Style();
try {
if (ContentResolverUtil.getSizeColumn(activity, fileUri) > MAX_STYLE_FILE_SIZE) {
Expand Down Expand Up @@ -989,11 +989,12 @@ protected void onPostExecute(Void result) {
private static final long serialVersionUID = 1L;

@Override
public boolean save(Uri fileUri) {
public boolean save(FragmentActivity currentActivity, Uri fileUri) {
// FIXME layer will likely not be valid if the activity has been recreated
if (layer != null) {
final Track track = ((de.blau.android.layer.gpx.MapOverlay) layer).getTrack();
if (track != null) {
SavingHelper.asyncExport(activity, track, fileUri);
SavingHelper.asyncExport(currentActivity, track, fileUri);
SelectFile.savePref(App.getLogic().getPrefs(), R.string.config_osmPreferredDir_key, fileUri);
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/de/blau/android/layer/mvt/MapOverlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -524,10 +524,10 @@ public void loadStyleFromFile(@NonNull FragmentActivity activity) throws IOExcep
private static final long serialVersionUID = 1L;

@Override
public boolean read(Uri fileUri) {
try (InputStream is = activity.getContentResolver().openInputStream(fileUri)) {
public boolean read(FragmentActivity currentActivity, Uri fileUri) {
try (InputStream is = currentActivity.getContentResolver().openInputStream(fileUri)) {
Style style = new Style();
style.loadStyle(activity, is);
style.loadStyle(currentActivity, is);
setStyle(style);
flushTileCache();
dirty = true;
Expand All @@ -536,12 +536,12 @@ public boolean read(Uri fileUri) {
} catch (SecurityException sex) {
Log.e(DEBUG_TAG, sex.getMessage());
// note need a context here that is on the ui thread
ScreenMessage.toastTopError(map.getContext(), activity.getString(R.string.toast_permission_denied, fileUri.toString()));
ScreenMessage.toastTopError(currentActivity, currentActivity.getString(R.string.toast_permission_denied, fileUri.toString()));
return false;
} catch (FileNotFoundException e) {
ScreenMessage.toastTopError(map.getContext(), activity.getString(R.string.toast_file_not_found, fileUri.toString()));
ScreenMessage.toastTopError(currentActivity, currentActivity.getString(R.string.toast_file_not_found, fileUri.toString()));
} catch (IOException e) {
ScreenMessage.toastTopError(map.getContext(), activity.getString(R.string.toast_error_reading, fileUri.toString()));
ScreenMessage.toastTopError(currentActivity, currentActivity.getString(R.string.toast_error_reading, fileUri.toString()));
}
return true;
}
Expand Down
Loading

0 comments on commit 55c776c

Please sign in to comment.