Skip to content

Commit

Permalink
Merge branch 'import_msf_files'
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpoole committed Dec 13, 2023
2 parents 52650df + 98a1a8f commit eb2fd32
Show file tree
Hide file tree
Showing 20 changed files with 576 additions and 347 deletions.
17 changes: 15 additions & 2 deletions src/androidTest/java/de/blau/android/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -1577,13 +1579,24 @@ public static void disableTip(@NonNull Context ctx, int res) {
}

/**
* Click away a tip dialod
* Click away a tip dialog
*
* @param device the current UiDevice
* @param ctx an Android Context
*/
public static void clickAwayTip(@NonNull UiDevice device, @NonNull Context ctx) {
if (TestUtils.findText(device, false, ctx.getString(R.string.tip_title))) {
clickAwayTip(device, ctx, 500);
}

/**
* Click away a tip dialog
*
* @param device the current UiDevice
* @param ctx an Android Context
* @param wait time mS to wait for the tip
*/
public static void clickAwayTip(@NonNull UiDevice device, @NonNull Context ctx, long wait) {
if (TestUtils.findText(device, false, ctx.getString(R.string.tip_title), wait)) {
TestUtils.clickText(device, false, ctx.getString(R.string.okay), true, false); // TIP
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.orhanobut.mockwebserverplus.MockWebServerPlus;

import android.app.Instrumentation;
import android.os.Build;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.test.core.app.ApplicationProvider;
Expand Down Expand Up @@ -64,8 +65,8 @@
@LargeTest
public class LayerDialogCustomImageryTest {

public static final int EXTENT_BUTTON = 1;
public static final int MENU_BUTTON = 3;
public static final int EXTENT_BUTTON = 1;
public static final int MENU_BUTTON = 3;

AdvancedPrefDatabase prefDB = null;
Main main = null;
Expand Down Expand Up @@ -158,7 +159,9 @@ public void customImageryMBT() {
assertTrue(TestUtils.findText(device, false, main.getString(R.string.add_layer_title)));
assertTrue(TestUtils.clickResource(device, true, device.getCurrentPackageName() + ":id/file_button", true));
TestUtils.selectFile(device, main, null, fileName, true);
TestUtils.clickAwayTip(device, main, 5000); // only used when the file is imported
assertTrue(TestUtils.findText(device, false, "Vespucci Test"));
TestUtils.findText(device, false, main.getString(R.string.save_and_set), 2000);
assertTrue(TestUtils.clickText(device, false, main.getString(R.string.save_and_set), true));
assertTrue(TestUtils.textGone(device, main.getString(R.string.layer_add_custom_imagery), 2000));
assertTrue(TestUtils.findText(device, false, "Vespucci Test")); // layer dialog
Expand Down Expand Up @@ -188,6 +191,7 @@ public void customImageryLocalPMTiles() {
assertTrue(TestUtils.findText(device, false, main.getString(R.string.add_layer_title)));
assertTrue(TestUtils.clickResource(device, true, device.getCurrentPackageName() + ":id/file_button", true));
TestUtils.selectFile(device, main, null, fileName, true);
TestUtils.clickAwayTip(device, main, 5000); // only used when the file is imported
assertTrue(TestUtils.findText(device, false, "protomaps 2023-01"));
assertTrue(TestUtils.clickText(device, false, main.getString(R.string.save_and_set), true));
assertTrue(TestUtils.textGone(device, main.getString(R.string.layer_add_custom_imagery), 2000));
Expand Down
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
Loading

0 comments on commit eb2fd32

Please sign in to comment.