Move import dialog to separate class

This commit is contained in:
Vitaliy 2020-05-20 12:26:12 +03:00
parent 1dc4813d3c
commit 7c86a40225
4 changed files with 144 additions and 198 deletions

View file

@ -250,6 +250,32 @@ public class Algorithms {
return test == 0x504b0304;
}
/**
* Checks, whether the child directory is a subdirectory of the parent
* directory.
*
* @param parent the parent directory.
* @param child the suspected child directory.
* @return true if the child is a subdirectory of the parent directory.
*/
public static boolean isSubDirectory(File parent, File child) {
try {
parent = parent.getCanonicalFile();
child = child.getCanonicalFile();
File dir = child;
while (dir != null) {
if (parent.equals(dir)) {
return true;
}
dir = dir.getParentFile();
}
} catch (IOException e) {
return false;
}
return false;
}
private static int readInt(InputStream in) throws IOException {
int ch1 = in.read();
int ch2 = in.read();

View file

@ -101,7 +101,7 @@ import net.osmand.plus.firstusage.FirstUsageWizardFragment;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.helpers.DiscountHelper;
import net.osmand.plus.helpers.ImportHelper;
import net.osmand.plus.helpers.ImportHelper.ImportGpxBottomSheetDialogFragment;
import net.osmand.plus.dialogs.ImportGpxBottomSheetDialogFragment;
import net.osmand.plus.helpers.IntentHelper;
import net.osmand.plus.helpers.LockHelper;
import net.osmand.plus.helpers.LockHelper.LockUIAdapter;

View file

@ -0,0 +1,94 @@
package net.osmand.plus.dialogs;
import android.os.Bundle;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import net.osmand.AndroidUtils;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.plus.R;
import net.osmand.plus.base.MenuBottomSheetDialogFragment;
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerHalfItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.ShortDescriptionItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem;
import net.osmand.plus.helpers.ImportHelper;
public class ImportGpxBottomSheetDialogFragment extends MenuBottomSheetDialogFragment {
public static final String TAG = "ImportGpxBottomSheetDialogFragment";
private ImportHelper importHelper;
private GPXFile gpxFile;
private String fileName;
private boolean save;
private boolean useImportDir;
public void setImportHelper(ImportHelper importHelper) {
this.importHelper = importHelper;
}
public void setGpxFile(GPXFile gpxFile) {
this.gpxFile = gpxFile;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public void setSave(boolean save) {
this.save = save;
}
public void setUseImportDir(boolean useImportDir) {
this.useImportDir = useImportDir;
}
@Override
public void createMenuItems(Bundle savedInstanceState) {
items.add(new TitleItem(getString(R.string.import_file)));
int nameColor = getResolvedColor(nightMode ? R.color.active_color_primary_dark : R.color.active_color_primary_light);
int descrColor = getResolvedColor(nightMode ? R.color.text_color_secondary_dark : R.color.text_color_secondary_light);
String descr = getString(R.string.import_gpx_file_description);
if (!descr.contains("%s")) {
descr = "%s " + descr;
}
CharSequence txt = AndroidUtils.getStyledString(descr, fileName, new ForegroundColorSpan(descrColor),
new ForegroundColorSpan(nameColor));
items.add(new ShortDescriptionItem(txt));
BaseBottomSheetItem asFavoritesItem = new SimpleBottomSheetItem.Builder()
.setIcon(getContentIcon(R.drawable.ic_action_favorite))
.setTitle(getString(R.string.import_as_favorites))
.setLayoutId(R.layout.bottom_sheet_item_simple)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
importHelper.importFavoritesFromGpx(gpxFile, fileName);
dismiss();
}
})
.create();
items.add(asFavoritesItem);
items.add(new DividerHalfItem(getContext()));
BaseBottomSheetItem asGpxItem = new SimpleBottomSheetItem.Builder()
.setIcon(getContentIcon(R.drawable.ic_action_polygom_dark))
.setTitle(getString(R.string.import_as_gpx))
.setLayoutId(R.layout.bottom_sheet_item_simple)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
importHelper.handleGpxImport(gpxFile, fileName, save, useImportDir);
dismiss();
}
})
.create();
items.add(asGpxItem);
}
}

View file

@ -12,8 +12,6 @@ import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.provider.OpenableColumns;
import android.provider.Settings;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.NonNull;
@ -44,12 +42,7 @@ import net.osmand.plus.R;
import net.osmand.plus.activities.ActivityResultListener;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.TrackActivity;
import net.osmand.plus.base.MenuBottomSheetDialogFragment;
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerHalfItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.ShortDescriptionItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem;
import net.osmand.plus.dialogs.ImportGpxBottomSheetDialogFragment;
import net.osmand.plus.rastermaps.OsmandRasterMapsPlugin;
import net.osmand.plus.settings.backend.SettingsHelper;
import net.osmand.plus.settings.backend.SettingsHelper.CheckDuplicatesListener;
@ -144,21 +137,29 @@ public class ImportHelper {
handleFileImport(contentUri, name, extras, useImportDir);
}
public void importFavoritesFromGpx(final GPXFile gpxFile, final String fileName) {
importFavoritesImpl(gpxFile, fileName, false);
}
public void handleGpxImport(GPXFile result, String name, boolean save, boolean useImportDir) {
handleResult(result, name, save, useImportDir, false);
}
public boolean handleGpxImport(final Uri contentUri, final boolean useImportDir) {
String name = getNameFromContentUri(app, contentUri);
boolean isOsmandSubdir = isSubDirectory(app.getAppPath(IndexConstants.GPX_INDEX_DIR), new File(contentUri.getPath()));
boolean isOsmandSubdir = Algorithms.isSubDirectory(app.getAppPath(IndexConstants.GPX_INDEX_DIR), new File(contentUri.getPath()));
if (!isOsmandSubdir && name != null) {
String nameLC = name.toLowerCase();
if (nameLC.endsWith(GPX_FILE_EXT)) {
name = name.substring(0, name.length() - 4) + GPX_FILE_EXT;
name = name.substring(0, name.length() - GPX_FILE_EXT.length()) + GPX_FILE_EXT;
handleGpxImport(contentUri, name, true, useImportDir);
return true;
} else if (nameLC.endsWith(KML_SUFFIX)) {
name = name.substring(0, name.length() - 4) + KML_SUFFIX;
name = name.substring(0, name.length() - KML_SUFFIX.length()) + KML_SUFFIX;
handleKmlImport(contentUri, name, true, useImportDir);
return true;
} else if (nameLC.endsWith(KMZ_SUFFIX)) {
name = name.substring(0, name.length() - 4) + KMZ_SUFFIX;
name = name.substring(0, name.length() - KMZ_SUFFIX.length()) + KMZ_SUFFIX;
handleKmzImport(contentUri, name, true, useImportDir);
return true;
}
@ -170,7 +171,7 @@ public class ImportHelper {
String scheme = uri.getScheme();
boolean isFileIntent = "file".equals(scheme);
boolean isContentIntent = "content".equals(scheme);
boolean isOsmandSubdir = isSubDirectory(app.getAppPath(IndexConstants.GPX_INDEX_DIR), new File(uri.getPath()));
boolean isOsmandSubdir = Algorithms.isSubDirectory(app.getAppPath(IndexConstants.GPX_INDEX_DIR), new File(uri.getPath()));
final boolean saveFile = !isFileIntent || !isOsmandSubdir;
String fileName = "";
if (isFileIntent) {
@ -183,7 +184,7 @@ public class ImportHelper {
public void handleFileImport(Uri intentUri, String fileName, Bundle extras, boolean useImportDir) {
final boolean isFileIntent = "file".equals(intentUri.getScheme());
final boolean isOsmandSubdir = isSubDirectory(app.getAppPath(IndexConstants.GPX_INDEX_DIR), new File(intentUri.getPath()));
final boolean isOsmandSubdir = Algorithms.isSubDirectory(app.getAppPath(IndexConstants.GPX_INDEX_DIR), new File(intentUri.getPath()));
final boolean saveFile = !isFileIntent || !isOsmandSubdir;
@ -198,7 +199,7 @@ public class ImportHelper {
} else if (fileName != null && fileName.endsWith(OSMAND_SETTINGS_FILE_EXT)) {
handleOsmAndSettingsImport(intentUri, fileName, extras, null);
} else if (fileName != null && fileName.endsWith(ROUTING_FILE_EXT)) {
handleXmlFileImport(intentUri, fileName);
handleXmlFileImport(intentUri, fileName, null);
} else {
handleFavouritesImport(intentUri, fileName, saveFile, useImportDir, false);
}
@ -661,7 +662,7 @@ public class ImportHelper {
if (importType.equals(ImportType.SETTINGS)) {
handleOsmAndSettingsImport(data, fileName, resultData.getExtras(), callback);
} else if (importType.equals(ImportType.ROUTING)){
handleRoutingFileImport(data, fileName, callback);
handleXmlFileImport(data, fileName, callback);
}
} else {
app.showToastMessage(app.getString(R.string.not_support_file_type_with_ext,
@ -675,81 +676,6 @@ public class ImportHelper {
mapActivity.startActivityForResult(intent, IMPORT_FILE_REQUEST);
}
@SuppressLint("StaticFieldLeak")
private void handleRoutingFileImport(final Uri uri, final String fileName, final CallbackWithObject<RoutingConfiguration.Builder> callback) {
final AsyncTask<Void, Void, String> routingImportTask = new AsyncTask<Void, Void, String>() {
String mFileName;
ProgressDialog progress;
@Override
protected void onPreExecute() {
if (AndroidUtils.isActivityNotDestroyed(activity)) {
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 routingDir = app.getAppPath(IndexConstants.ROUTING_PROFILES_DIR);
if (!routingDir.exists()) {
routingDir.mkdirs();
}
File dest = new File(routingDir, mFileName);
while (dest.exists()) {
mFileName = AndroidUtils.createNewFileName(mFileName);
dest = new File(routingDir, mFileName);
}
return copyFile(app, dest, uri, true);
}
@Override
protected void onPostExecute(String error) {
File routingDir = app.getAppPath(IndexConstants.ROUTING_PROFILES_DIR);
final File file = new File(routingDir, mFileName);
if (error == null && file.exists()) {
loadRoutingFiles(app, new AppInitializer.LoadRoutingFilesCallback() {
@Override
public void onRoutingFilesLoaded() {
if (progress != null && AndroidUtils.isActivityNotDestroyed(activity)) {
progress.dismiss();
}
RoutingConfiguration.Builder builder = app.getCustomRoutingConfig(mFileName);
if (builder != null) {
app.showShortToastMessage(app.getString(R.string.file_imported_successfully, mFileName));
if (callback != null) {
callback.processResult(builder);
}
} else {
app.showToastMessage(app.getString(R.string.file_does_not_contain_routing_rules, mFileName));
}
}
});
} else {
if (progress != null && AndroidUtils.isActivityNotDestroyed(activity)) {
progress.dismiss();
}
app.showShortToastMessage(app.getString(R.string.file_import_error, mFileName, error));
}
}
};
if (app.isApplicationInitializing()) {
app.getAppInitializer().addListener(new AppInitializer.AppInitializeListener() {
@Override
public void onProgress(AppInitializer init, AppInitializer.InitEvents event) {
}
@Override
public void onFinish(AppInitializer init) {
routingImportTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
});
} else {
routingImportTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
private void handleOsmAndSettingsImport(Uri intentUri, String fileName, Bundle extras, CallbackWithObject<List<SettingsItem>> callback) {
if (extras != null && extras.containsKey(SettingsHelper.SETTINGS_VERSION_KEY) && extras.containsKey(SettingsHelper.SETTINGS_LATEST_CHANGES_KEY)) {
int version = extras.getInt(SettingsHelper.SETTINGS_VERSION_KEY, -1);
@ -897,7 +823,8 @@ public class ImportHelper {
}
@SuppressLint("StaticFieldLeak")
private void handleXmlFileImport(final Uri intentUri, final String fileName) {
private void handleXmlFileImport(final Uri intentUri, final String fileName,
final CallbackWithObject routingCallback) {
final AsyncTask<Void, Void, String> renderingImportTask = new AsyncTask<Void, Void, String>() {
private String destFileName;
@ -940,6 +867,9 @@ public class ImportHelper {
hideProgress();
RoutingConfiguration.Builder builder = app.getCustomRoutingConfig(destFileName);
if (builder != null) {
if (routingCallback != null) {
routingCallback.processResult(builder);
}
app.showShortToastMessage(app.getString(R.string.file_imported_successfully, destFileName));
} else {
app.showToastMessage(app.getString(R.string.file_does_not_contain_routing_rules, destFileName));
@ -1281,108 +1211,4 @@ public class ImportHelper {
}
return favourites;
}
/**
* Checks, whether the child directory is a subdirectory of the parent
* directory.
*
* @param parent the parent directory.
* @param child the suspected child directory.
* @return true if the child is a subdirectory of the parent directory.
*/
public boolean isSubDirectory(File parent, File child) {
try {
parent = parent.getCanonicalFile();
child = child.getCanonicalFile();
File dir = child;
while (dir != null) {
if (parent.equals(dir)) {
return true;
}
dir = dir.getParentFile();
}
} catch (IOException e) {
return false;
}
return false;
}
public static class ImportGpxBottomSheetDialogFragment extends MenuBottomSheetDialogFragment {
public static final String TAG = "ImportGpxBottomSheetDialogFragment";
private ImportHelper importHelper;
private GPXFile gpxFile;
private String fileName;
private boolean save;
private boolean useImportDir;
public void setImportHelper(ImportHelper importHelper) {
this.importHelper = importHelper;
}
public void setGpxFile(GPXFile gpxFile) {
this.gpxFile = gpxFile;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public void setSave(boolean save) {
this.save = save;
}
public void setUseImportDir(boolean useImportDir) {
this.useImportDir = useImportDir;
}
@Override
public void createMenuItems(Bundle savedInstanceState) {
items.add(new TitleItem(getString(R.string.import_file)));
int nameColor = getResolvedColor(nightMode ? R.color.active_color_primary_dark : R.color.active_color_primary_light);
int descrColor = getResolvedColor(nightMode ? R.color.text_color_secondary_dark : R.color.text_color_secondary_light);
String descr = getString(R.string.import_gpx_file_description);
if(!descr.contains("%s")) {
descr = "%s " +descr;
}
CharSequence txt = AndroidUtils.getStyledString(descr, fileName, new ForegroundColorSpan(descrColor),
new ForegroundColorSpan(nameColor));
items.add(new ShortDescriptionItem(txt));
BaseBottomSheetItem asFavoritesItem = new SimpleBottomSheetItem.Builder()
.setIcon(getContentIcon(R.drawable.ic_action_favorite))
.setTitle(getString(R.string.import_as_favorites))
.setLayoutId(R.layout.bottom_sheet_item_simple)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
importHelper.importFavoritesImpl(gpxFile, fileName, false);
dismiss();
}
})
.create();
items.add(asFavoritesItem);
items.add(new DividerHalfItem(getContext()));
BaseBottomSheetItem asGpxItem = new SimpleBottomSheetItem.Builder()
.setIcon(getContentIcon(R.drawable.ic_action_polygom_dark))
.setTitle(getString(R.string.import_as_gpx))
.setLayoutId(R.layout.bottom_sheet_item_simple)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
importHelper.handleResult(gpxFile, fileName, save, useImportDir, false);
dismiss();
}
})
.create();
items.add(asGpxItem);
}
}
}
}