Merge pull request #8602 from osmandapp/import_rendering_files

Import rendering files
This commit is contained in:
max-klaus 2020-03-02 17:41:26 +03:00 committed by GitHub
commit 1a259302ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 143 additions and 17 deletions

View file

@ -42,7 +42,7 @@ public class IndexConstants {
public static final String OSMAND_SETTINGS_FILE_EXT = ".osf";
public static final String ROUTING_FILE_EXT = ".xml";
public static final String ROUTING_AND_RENDERING_FILE_EXT = ".xml";
public static final String RENDERER_INDEX_EXT = ".render.xml"; //$NON-NLS-1$

View file

@ -236,9 +236,9 @@ public class RoutingConfiguration {
String id = parser.getAttributeValue("", "id");
String type = parser.getAttributeValue("", "type");
boolean defaultValue = Boolean.parseBoolean(parser.getAttributeValue("", "default"));
if (type.equalsIgnoreCase("boolean")) {
if ("boolean".equalsIgnoreCase(type)) {
currentRouter.registerBooleanParameter(id, Algorithms.isEmpty(group) ? null : group, name, description, defaultValue);
} else if(type.equalsIgnoreCase("numeric")) {
} else if ("numeric".equalsIgnoreCase(type)) {
String values = parser.getAttributeValue("", "values");
String valueDescriptions = parser.getAttributeValue("", "valueDescriptions");
String[] strValues = values.split(",");
@ -292,7 +292,7 @@ public class RoutingConfiguration {
for (int i = 0; i < stack.size(); i++) {
addSubclause(stack.get(i), ctx);
}
} else if(stack.size() > 0 && stack.peek().tagName.equals("select")) {
} else if (stack.size() > 0 && "select".equals(stack.peek().tagName)) {
addSubclause(rr, ctx);
}
stack.push(rr);
@ -312,11 +312,11 @@ public class RoutingConfiguration {
if (!Algorithms.isEmpty(rr.t)) {
ctx.getLastRule().registerAndTagValueCondition(rr.t, Algorithms.isEmpty(rr.v) ? null : rr.v, not);
}
if (rr.tagName.equals("gt")) {
if ("gt".equals(rr.tagName)) {
ctx.getLastRule().registerGreatCondition(rr.value1, rr.value2, rr.type);
} else if (rr.tagName.equals("le")) {
} else if ("le".equals(rr.tagName)) {
ctx.getLastRule().registerLessCondition(rr.value1, rr.value2, rr.type);
} else if (rr.tagName.equals("eq")) {
} else if ("eq".equals(rr.tagName)) {
ctx.getLastRule().registerEqualCondition(rr.value1, rr.value2, rr.type);
}
}

View file

@ -11,6 +11,7 @@
Thx - Hardy
-->
<string name="import_rendering_file">Import rendering file</string>
<string name="profile_type_custom_string">Custom profile</string>
<string name="shared_string_angle_param">Angle: %s°</string>
<string name="shared_string_angle">Angle</string>

View file

@ -617,7 +617,7 @@ public class AppInitializer implements IProgress {
File[] fl = routingFolder.listFiles();
if (fl != null && fl.length > 0) {
for (File f : fl) {
if (f.isFile() && f.getName().endsWith(IndexConstants.ROUTING_FILE_EXT) && f.canRead()) {
if (f.isFile() && f.getName().endsWith(IndexConstants.ROUTING_AND_RENDERING_FILE_EXT) && f.canRead()) {
try {
String fileName = f.getName();
RoutingConfiguration.Builder builder = new RoutingConfiguration.Builder(defaultAttributes);

View file

@ -90,7 +90,7 @@ import java.util.concurrent.ConcurrentHashMap;
import btools.routingapp.BRouterServiceConnection;
import btools.routingapp.IBRouterService;
import static net.osmand.IndexConstants.ROUTING_FILE_EXT;
import static net.osmand.IndexConstants.ROUTING_AND_RENDERING_FILE_EXT;
public class OsmandApplication extends MultiDexApplication {
public static final String EXCEPTION_PATH = "exception.log";
@ -840,9 +840,9 @@ public class OsmandApplication extends MultiDexApplication {
RoutingConfiguration.Builder builder = null;
String routingProfileKey = mode.getRoutingProfile();
if (!Algorithms.isEmpty(routingProfileKey)) {
int index = routingProfileKey.indexOf(ROUTING_FILE_EXT);
int index = routingProfileKey.indexOf(ROUTING_AND_RENDERING_FILE_EXT);
if (index != -1) {
String configKey = routingProfileKey.substring(0, index + ROUTING_FILE_EXT.length());
String configKey = routingProfileKey.substring(0, index + ROUTING_AND_RENDERING_FILE_EXT.length());
builder = customRoutingConfigs.get(configKey);
}
}

View file

@ -6,6 +6,7 @@ import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
@ -19,7 +20,13 @@ import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.style.ForegroundColorSpan;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import net.osmand.AndroidUtils;
@ -41,6 +48,7 @@ import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.R;
import net.osmand.plus.SettingsHelper;
import net.osmand.plus.SettingsHelper.SettingsImportListener;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.activities.ActivityResultListener;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.TrackActivity;
@ -76,7 +84,7 @@ import java.util.zip.ZipInputStream;
import static android.app.Activity.RESULT_OK;
import static net.osmand.IndexConstants.OSMAND_SETTINGS_FILE_EXT;
import static net.osmand.IndexConstants.ROUTING_FILE_EXT;
import static net.osmand.IndexConstants.ROUTING_AND_RENDERING_FILE_EXT;
import static net.osmand.plus.AppInitializer.loadRoutingFiles;
import static net.osmand.plus.myplaces.FavoritesActivity.FAV_TAB;
import static net.osmand.plus.myplaces.FavoritesActivity.GPX_TAB;
@ -99,7 +107,7 @@ public class ImportHelper {
public enum ImportType {
SETTINGS(IndexConstants.OSMAND_SETTINGS_FILE_EXT),
ROUTING(ROUTING_FILE_EXT);
ROUTING(ROUTING_AND_RENDERING_FILE_EXT);
ImportType(String extension) {
this.extension = extension;
@ -184,8 +192,8 @@ public class ImportHelper {
handleSqliteTileImport(intentUri, fileName);
} else if (fileName != null && fileName.endsWith(OSMAND_SETTINGS_FILE_EXT)) {
handleOsmAndSettingsImport(intentUri, fileName, extras, true, null);
} else if (fileName != null && fileName.endsWith(ROUTING_FILE_EXT)) {
handleRoutingFileImport(intentUri, fileName, null);
} else if (fileName != null && fileName.endsWith(ROUTING_AND_RENDERING_FILE_EXT)) {
handleXmlFileImport(intentUri, fileName);
} else {
handleFavouritesImport(intentUri, fileName, saveFile, useImportDir, false);
}
@ -818,6 +826,123 @@ public class ImportHelper {
return !activity.isFinishing();
}
private void handleXmlFileImport(final Uri intentUri, final String fileName) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
final boolean nightMode;
if (activity instanceof MapActivity) {
nightMode = app.getDaynightHelper().isNightModeForMapControls();
} else {
nightMode = !app.getSettings().isLightContent();
}
final LayoutInflater themedInflater = UiUtilities.getInflater(activity, nightMode);
View dialogTitle = themedInflater.inflate(R.layout.bottom_sheet_item_simple, null);
dialogTitle.findViewById(R.id.icon).setVisibility(View.GONE);
TextView tvTitle = dialogTitle.findViewById(R.id.title);
tvTitle.setText(R.string.import_from_file);
int textSize = (int) app.getResources().getDimension(R.dimen.dialog_header_text_size);
tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
builder.setCustomTitle(dialogTitle);
String[] strings = new String[2];
strings[0] = app.getString(R.string.import_routing_file);
strings[1] = app.getString(R.string.import_rendering_file);
final int[] icons = new int[2];
icons[0] = R.drawable.ic_action_gdirections_dark;
icons[1] = R.drawable.ic_map;
ArrayAdapter<String> singleChoiceAdapter = new ArrayAdapter<String>(activity, R.layout.bottom_sheet_item_simple, R.id.title, strings) {
@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
View v = convertView;
if (v == null) {
v = themedInflater.inflate(R.layout.bottom_sheet_item_simple, parent, false);
}
int activeColor = nightMode ? R.color.active_color_primary_dark : R.color.active_color_primary_light;
Drawable icon = app.getUIUtilities().getIcon(icons[position], activeColor);
((TextView) v.findViewById(R.id.title)).setText(getItem(position));
((ImageView) v.findViewById(R.id.icon)).setImageDrawable(icon);
return v;
}
};
builder.setAdapter(singleChoiceAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
handleRoutingFileImport(intentUri, fileName, null);
} else {
handleRenderingFileImport(intentUri, fileName);
}
}
});
builder.setNegativeButton(R.string.shared_string_cancel, null);
AlertDialog dialog = builder.create();
dialog.getListView().setDividerHeight(0);
dialog.show();
}
@SuppressLint("StaticFieldLeak")
private void handleRenderingFileImport(final Uri intentUri, final String fileName) {
final AsyncTask<Void, Void, String> renderingImportTask = new AsyncTask<Void, Void, String>() {
String mFileName;
ProgressDialog progress;
@Override
protected void onPreExecute() {
progress = ProgressDialog.show(activity, app.getString(R.string.loading_smth, ""), app.getString(R.string.loading_data));
mFileName = fileName;
}
@Override
protected String doInBackground(Void... voids) {
File renderingDir = app.getAppPath(IndexConstants.RENDERERS_DIR);
if (!renderingDir.exists()) {
renderingDir.mkdirs();
}
File dest = new File(renderingDir, mFileName);
while (dest.exists()) {
mFileName = AndroidUtils.createNewFileName(mFileName);
dest = new File(renderingDir, mFileName);
}
return copyFile(app, dest, intentUri, true);
}
@Override
protected void onPostExecute(String error) {
File renderingDir = app.getAppPath(IndexConstants.RENDERERS_DIR);
File file = new File(renderingDir, mFileName);
if (error == null && file.exists()) {
app.getRendererRegistry().updateExternalRenderers();
} else {
app.showShortToastMessage(app.getString(R.string.file_import_error, mFileName, error));
}
if (isActivityNotDestroyed(activity)) {
progress.dismiss();
}
}
};
if (app.isApplicationInitializing()) {
app.getAppInitializer().addListener(new AppInitializer.AppInitializeListener() {
@Override
public void onProgress(AppInitializer init, AppInitializer.InitEvents event) {
}
@Override
public void onFinish(AppInitializer init) {
renderingImportTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
});
} else {
renderingImportTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
private void handleResult(final GPXFile result, final String name, final boolean save,
final boolean useImportDir, boolean forceImportFavourites) {
if (result != null) {

View file

@ -257,9 +257,9 @@ public class RendererRegistry {
File[] lf = file.listFiles();
if (lf != null) {
for (File f : lf) {
if (f != null && f.getName().endsWith(IndexConstants.RENDERER_INDEX_EXT)) {
if (f != null && f.getName().endsWith(IndexConstants.ROUTING_AND_RENDERING_FILE_EXT)) {
if(!internalRenderers.containsValue(f.getName())) {
String name = f.getName().substring(0, f.getName().length() - IndexConstants.RENDERER_INDEX_EXT.length());
String name = f.getName().substring(0, f.getName().length() - IndexConstants.ROUTING_AND_RENDERING_FILE_EXT.length());
externalRenderers.put(name.replace('_', ' ').replace('-', ' '), f);
}
}