Fix profiles import

This commit is contained in:
Dima-1 2020-11-26 19:22:33 +02:00
parent 9a6ce577e5
commit ca2b6f4d0e
5 changed files with 91 additions and 29 deletions

View file

@ -34,6 +34,7 @@ import net.osmand.plus.dialogs.ImportGpxBottomSheetDialogFragment;
import net.osmand.plus.helpers.GpxUiHelper;
import net.osmand.plus.helpers.GpxUiHelper.GPXInfo;
import net.osmand.plus.measurementtool.MeasurementToolFragment;
import net.osmand.plus.settings.backend.ExportSettingsType;
import net.osmand.plus.settings.backend.backup.SettingsHelper;
import net.osmand.plus.settings.backend.backup.SettingsItem;
import net.osmand.plus.views.OsmandMapTileView;
@ -67,6 +68,8 @@ import static net.osmand.IndexConstants.WPT_CHART_FILE_EXT;
import static net.osmand.data.FavouritePoint.DEFAULT_BACKGROUND_TYPE;
import static net.osmand.plus.myplaces.FavoritesActivity.GPX_TAB;
import static net.osmand.plus.myplaces.FavoritesActivity.TAB_ID;
import static net.osmand.plus.settings.backend.backup.SettingsHelper.REPLACE_KEY;
import static net.osmand.plus.settings.backend.backup.SettingsHelper.SETTINGS_TYPE_LIST_KEY;
/**
* @author Koen Rabaey
@ -203,6 +206,7 @@ public class ImportHelper {
}
}
@Nullable
public static String getNameFromContentUri(OsmandApplication app, Uri contentUri) {
try {
String name;
@ -257,18 +261,32 @@ public class ImportHelper {
}
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)) {
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);
String latestChanges = extras.getString(SettingsHelper.SETTINGS_LATEST_CHANGES_KEY);
handleOsmAndSettingsImport(intentUri, fileName, latestChanges, version, callback);
boolean replace = extras.getBoolean(REPLACE_KEY);
ArrayList<String> settingsTypeKeys = extras.getStringArrayList(SETTINGS_TYPE_LIST_KEY);
List<ExportSettingsType> settingsTypes = null;
if (settingsTypeKeys != null) {
settingsTypes = new ArrayList<>();
for (String key : settingsTypeKeys) {
settingsTypes.add(ExportSettingsType.valueOf(key));
}
}
handleOsmAndSettingsImport(intentUri, fileName, settingsTypes, replace, latestChanges, version, callback);
} else {
handleOsmAndSettingsImport(intentUri, fileName, null, -1, callback);
handleOsmAndSettingsImport(intentUri, fileName, null, false, null, -1,
callback);
}
}
protected void handleOsmAndSettingsImport(Uri uri, String name, String latestChanges, int version,
protected void handleOsmAndSettingsImport(Uri uri, String name, final List<ExportSettingsType> settingsTypes,
final boolean replace, String latestChanges, int version,
CallbackWithObject<List<SettingsItem>> callback) {
executeImportTask(new SettingsImportTask(activity, uri, name, latestChanges, version, callback));
executeImportTask(new SettingsImportTask(activity, uri, name, settingsTypes, replace, latestChanges, version,
callback));
}
protected void handleXmlFileImport(Uri intentUri, String fileName, CallbackWithObject routingCallback) {

View file

@ -11,38 +11,49 @@ import net.osmand.AndroidUtils;
import net.osmand.CallbackWithObject;
import net.osmand.FileUtils;
import net.osmand.IndexConstants;
import net.osmand.plus.AppInitializer;
import net.osmand.plus.CustomOsmandPlugin;
import net.osmand.plus.R;
import net.osmand.plus.base.BaseLoadAsyncTask;
import net.osmand.plus.settings.backend.ExportSettingsType;
import net.osmand.plus.settings.backend.backup.SettingsHelper;
import net.osmand.plus.settings.backend.backup.SettingsHelper.CheckDuplicatesListener;
import net.osmand.plus.settings.backend.backup.PluginSettingsItem;
import net.osmand.plus.settings.backend.backup.ProfileSettingsItem;
import net.osmand.plus.settings.backend.backup.SettingsHelper.SettingsCollectListener;
import net.osmand.plus.settings.backend.backup.SettingsHelper.SettingsImportListener;
import net.osmand.plus.settings.backend.backup.SettingsItem;
import net.osmand.plus.settings.fragments.ImportCompleteFragment;
import net.osmand.plus.settings.fragments.ImportSettingsFragment;
import net.osmand.util.Algorithms;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static net.osmand.plus.AppInitializer.loadRoutingFiles;
import static net.osmand.plus.settings.backend.backup.SettingsHelper.getSettingsToOperate;
class SettingsImportTask extends BaseLoadAsyncTask<Void, Void, String> {
private Uri uri;
private String name;
private List<ExportSettingsType> settingsTypes;
private boolean replace;
private String latestChanges;
private int version;
private CallbackWithObject<List<SettingsItem>> callback;
public SettingsImportTask(@NonNull FragmentActivity activity, @NonNull Uri uri,
@NonNull String name, String latestChanges, int version,
@NonNull String name, List<ExportSettingsType> settingsTypes,
boolean replace, String latestChanges, int version,
CallbackWithObject<List<SettingsItem>> callback) {
super(activity);
this.uri = uri;
this.name = name;
this.settingsTypes = settingsTypes;
this.replace = replace;
this.latestChanges = latestChanges;
this.version = version;
this.callback = callback;
@ -60,7 +71,8 @@ class SettingsImportTask extends BaseLoadAsyncTask<Void, Void, String> {
File tempDir = FileUtils.getTempDir(app);
final File file = new File(tempDir, name);
if (error == null && file.exists()) {
app.getSettingsHelper().collectSettings(file, latestChanges, version, new SettingsCollectListener() {
final SettingsHelper settingsHelper = app.getSettingsHelper();
settingsHelper.collectSettings(file, latestChanges, version, new SettingsCollectListener() {
@Override
public void onSettingsCollectFinished(boolean succeed, boolean empty, @NonNull List<SettingsItem> items) {
hideProgress();
@ -77,11 +89,15 @@ class SettingsImportTask extends BaseLoadAsyncTask<Void, Void, String> {
for (PluginSettingsItem pluginItem : pluginSettingsItems) {
handlePluginImport(pluginItem, file);
}
if (!pluginIndependentItems.isEmpty()) {
FragmentActivity activity = activityRef.get();
if (activity != null) {
if (!pluginIndependentItems.isEmpty() && activity != null) {
if (settingsTypes == null) {
FragmentManager fragmentManager = activity.getSupportFragmentManager();
ImportSettingsFragment.showInstance(fragmentManager, pluginIndependentItems, file);
} else {
Map<ExportSettingsType, List<?>> allSettingsList = getSettingsToOperate(pluginIndependentItems, false);
List<SettingsItem> settingsList = settingsHelper.getFilteredSettingsItems(allSettingsList, settingsTypes);
settingsHelper.checkDuplicates(file, settingsList, settingsList, getDuplicatesListener(file, replace));
}
}
} else if (empty) {
@ -95,6 +111,37 @@ class SettingsImportTask extends BaseLoadAsyncTask<Void, Void, String> {
}
}
private CheckDuplicatesListener getDuplicatesListener(final File file, final boolean replace) {
return new CheckDuplicatesListener() {
@Override
public void onDuplicatesChecked(@NonNull List<Object> duplicates, List<SettingsItem> items) {
if (replace) {
for (SettingsItem item : items) {
item.setShouldReplace(true);
}
}
app.getSettingsHelper().importSettings(file, items, "", 1, getImportListener(file));
}
};
}
private SettingsImportListener getImportListener(final File file) {
return new SettingsImportListener() {
@Override
public void onSettingsImportFinished(boolean succeed, @NonNull List<SettingsItem> items) {
FragmentActivity activity = activityRef.get();
if (activity != null && succeed) {
FragmentManager fm = activity.getSupportFragmentManager();
app.getRendererRegistry().updateExternalRenderers();
AppInitializer.loadRoutingFiles(app, null);
if (file != null) {
ImportCompleteFragment.showInstance(fm, items, file.getName());
}
}
}
};
}
private void handlePluginImport(final PluginSettingsItem pluginItem, final File file) {
FragmentActivity activity = activityRef.get();
final ProgressDialog progress;

View file

@ -85,7 +85,8 @@ public class ZipImportTask extends BaseLoadAsyncTask<Void, Void, ImportType> {
importHelper.handleKmzImport(uri, name + GPX_FILE_EXT, save, useImportDir);
} else if (importType == ImportType.SETTINGS) {
String name = createUniqueFileName(app, "settings", TEMP_DIR, OSMAND_SETTINGS_FILE_EXT);
importHelper.handleOsmAndSettingsImport(uri, name + OSMAND_SETTINGS_FILE_EXT, null, -1, null);
importHelper.handleOsmAndSettingsImport(uri, name + OSMAND_SETTINGS_FILE_EXT,
null, false, null, -1, null);
}
}
}

View file

@ -287,20 +287,17 @@ public class OsmAndAppCustomization {
if (!connectedAppDir.exists()) {
connectedAppDir.mkdirs();
}
OutputStream fout = new FileOutputStream(new File(connectedAppDir, iconName));
OutputStream fout = null;
if (!Algorithms.isEmpty(iconName)) {
fout = new FileOutputStream(new File(connectedAppDir, iconName));
}
try {
if (fout != null) {
Algorithms.streamCopy(is, fout);
}
} finally {
try {
is.close();
} catch (IOException e) {
LOG.error(e);
}
try {
fout.close();
} catch (IOException e) {
LOG.error(e);
}
Algorithms.closeStream(is);
Algorithms.closeStream(fout);
}
JSONObject json = new JSONObject();
json.put("", connectedAppDirPath + "/" + iconName);
@ -406,8 +403,7 @@ public class OsmAndAppCustomization {
return set.contains(appMode);
}
public boolean setNavDrawerLogoWithParams(String imageUri, @Nullable String packageName,
@Nullable String intent) {
public boolean setNavDrawerLogoWithParams(String imageUri, @Nullable String packageName, @Nullable String intent) {
return setNavDrawerLogo(imageUri, packageName, intent);
}

View file

@ -217,7 +217,7 @@ public class ExportItemsBottomSheet extends MenuBottomSheetDialogFragment {
String profileName = modeBean.userProfileName;
if (Algorithms.isEmpty(profileName)) {
ApplicationMode appMode = ApplicationMode.valueOfStringKey(modeBean.stringKey, null);
profileName = getString(appMode.getNameKeyResource());
profileName = appMode.toHumanString();
}
builder.setTitle(profileName);