Fix progress dialog in export screen

This commit is contained in:
Vitaliy 2020-03-05 17:39:36 +02:00
parent 19e58c33a5
commit 2e89b0f3ab
2 changed files with 123 additions and 47 deletions

View file

@ -17,6 +17,7 @@ import androidx.annotation.NonNull;
import androidx.appcompat.widget.SwitchCompat; import androidx.appcompat.widget.SwitchCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
@ -45,7 +46,6 @@ import net.osmand.plus.settings.bottomsheets.BasePreferenceBottomSheet;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import java.io.File; import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -54,29 +54,42 @@ import java.util.Set;
public class ExportProfileBottomSheet extends BasePreferenceBottomSheet { public class ExportProfileBottomSheet extends BasePreferenceBottomSheet {
private static final Log LOG = PlatformUtil.getLog(ExportProfileBottomSheet.class);
public static final String TAG = ExportProfileBottomSheet.class.getSimpleName(); public static final String TAG = ExportProfileBottomSheet.class.getSimpleName();
private static final Log LOG = PlatformUtil.getLog(ExportProfileBottomSheet.class);
private static final String INCLUDE_ADDITIONAL_DATA_KEY = "INCLUDE_ADDITIONAL_DATA_KEY"; private static final String INCLUDE_ADDITIONAL_DATA_KEY = "INCLUDE_ADDITIONAL_DATA_KEY";
private boolean includeAdditionalData = false; private static final String EXPORTING_PROFILE_KEY = "exporting_profile_key";
private OsmandApplication app; private OsmandApplication app;
private ApplicationMode profile; private ApplicationMode profile;
private List<AdditionalDataWrapper> dataList = new ArrayList<>(); private List<AdditionalDataWrapper> dataList = new ArrayList<>();
private ExportImportSettingsAdapter adapter; private ExportImportSettingsAdapter adapter;
private SettingsHelper.SettingsExportListener exportListener;
private ProgressDialog progress;
private boolean includeAdditionalData = false;
private boolean exportingProfile = false;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState != null) {
includeAdditionalData = savedInstanceState.getBoolean(INCLUDE_ADDITIONAL_DATA_KEY);
}
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
app = requiredMyApplication(); app = requiredMyApplication();
profile = getAppMode();
dataList = getAdditionalData(); dataList = getAdditionalData();
exportListener = getSettingsExportListener();
if (savedInstanceState != null) {
includeAdditionalData = savedInstanceState.getBoolean(INCLUDE_ADDITIONAL_DATA_KEY);
exportingProfile = savedInstanceState.getBoolean(EXPORTING_PROFILE_KEY);
}
} }
@Override @Override
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
outState.putBoolean(INCLUDE_ADDITIONAL_DATA_KEY, includeAdditionalData); outState.putBoolean(INCLUDE_ADDITIONAL_DATA_KEY, includeAdditionalData);
outState.putBoolean(EXPORTING_PROFILE_KEY, exportingProfile);
} }
@Override @Override
@ -87,8 +100,6 @@ public class ExportProfileBottomSheet extends BasePreferenceBottomSheet {
} }
LayoutInflater inflater = UiUtilities.getInflater(app, nightMode); LayoutInflater inflater = UiUtilities.getInflater(app, nightMode);
profile = getAppMode();
int profileColor = profile.getIconColorInfo().getColor(nightMode); int profileColor = profile.getIconColorInfo().getColor(nightMode);
int colorNoAlpha = ContextCompat.getColor(context, profileColor); int colorNoAlpha = ContextCompat.getColor(context, profileColor);
@ -189,6 +200,21 @@ public class ExportProfileBottomSheet extends BasePreferenceBottomSheet {
return true; return true;
} }
@Override
public void onResume() {
super.onResume();
checkExportingFile();
}
@Override
public void onPause() {
super.onPause();
if (exportingProfile) {
File file = getExportFile();
app.getSettingsHelper().updateExportListener(file, null);
}
}
private List<AdditionalDataWrapper> getAdditionalData() { private List<AdditionalDataWrapper> getAdditionalData() {
List<AdditionalDataWrapper> dataList = new ArrayList<>(); List<AdditionalDataWrapper> dataList = new ArrayList<>();
@ -304,37 +330,82 @@ public class ExportProfileBottomSheet extends BasePreferenceBottomSheet {
} }
private void prepareFile() { private void prepareFile() {
Context context = getContext(); if (app != null) {
if (app != null && context != null) { exportingProfile = true;
File tempDir = app.getAppPath(IndexConstants.TEMP_DIR); showExportProgressDialog();
if (!tempDir.exists()) { File tempDir = getTempDir();
tempDir.mkdirs();
}
String fileName = profile.toHumanString(); String fileName = profile.toHumanString();
app.getSettingsHelper().exportSettings(tempDir, fileName, exportListener, prepareSettingsItemsForExport());
}
}
ProgressDialog progress = new ProgressDialog(context); private void showExportProgressDialog() {
progress.setTitle(app.getString(R.string.export_profile)); Context context = getContext();
progress.setMessage(app.getString(R.string.shared_string_preparing)); if (context == null) {
progress.setCancelable(false); return;
progress.show(); }
if (progress != null) {
progress.dismiss();
}
progress = new ProgressDialog(context);
progress.setTitle(app.getString(R.string.export_profile));
progress.setMessage(app.getString(R.string.shared_string_preparing));
progress.setCancelable(false);
progress.show();
}
final WeakReference<ProgressDialog> progressRef = new WeakReference<>(progress); private SettingsHelper.SettingsExportListener getSettingsExportListener() {
if (exportListener == null) {
exportListener = new SettingsHelper.SettingsExportListener() {
app.getSettingsHelper().exportSettings(tempDir, fileName, new SettingsHelper.SettingsExportListener() {
@Override @Override
public void onSettingsExportFinished(@NonNull File file, boolean succeed) { public void onSettingsExportFinished(@NonNull File file, boolean succeed) {
ProgressDialog progress = progressRef.get(); dismissExportProgressDialog();
if (progress != null) { exportingProfile = false;
progress.dismiss();
}
if (succeed) { if (succeed) {
shareProfile(file, profile); shareProfile(file, profile);
} else { } else {
app.showToastMessage(R.string.export_profile_failed); app.showToastMessage(R.string.export_profile_failed);
} }
} }
}, prepareSettingsItemsForExport()); };
} }
return exportListener;
}
private void checkExportingFile() {
if (exportingProfile) {
File file = getExportFile();
boolean fileExporting = app.getSettingsHelper().isFileExporting(file);
if (fileExporting) {
showExportProgressDialog();
app.getSettingsHelper().updateExportListener(file, exportListener);
} else if (file.exists()) {
dismissExportProgressDialog();
shareProfile(file, profile);
}
}
}
private void dismissExportProgressDialog() {
FragmentActivity activity = getActivity();
if (progress != null && activity != null && AndroidUtils.isActivityNotDestroyed(activity)) {
progress.dismiss();
}
}
private File getExportFile() {
File tempDir = getTempDir();
String fileName = profile.toHumanString();
return new File(tempDir, fileName + IndexConstants.OSMAND_SETTINGS_FILE_EXT);
}
private File getTempDir() {
File tempDir = app.getAppPath(IndexConstants.TEMP_DIR);
if (!tempDir.exists()) {
tempDir.mkdirs();
}
return tempDir;
} }
private void shareProfile(@NonNull File file, @NonNull ApplicationMode profile) { private void shareProfile(@NonNull File file, @NonNull ApplicationMode profile) {

View file

@ -134,7 +134,6 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment {
changedProfile = new ApplicationProfileObject(); changedProfile = new ApplicationProfileObject();
if (savedInstanceState != null) { if (savedInstanceState != null) {
restoreState(savedInstanceState); restoreState(savedInstanceState);
checkSavingProfile();
} else { } else {
changedProfile.stringKey = profile.stringKey; changedProfile.stringKey = profile.stringKey;
changedProfile.parent = profile.parent; changedProfile.parent = profile.parent;
@ -427,20 +426,18 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment {
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
updateExportListener(exportListener); checkSavingProfile();
} }
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
updateExportListener(null); if (isNewProfile) {
} File file = ConfigureProfileFragment.getBackupFileForCustomMode(app, changedProfile.stringKey);
boolean fileExporting = app.getSettingsHelper().isFileExporting(file);
private void updateExportListener(SettingsHelper.SettingsExportListener listener) { if (fileExporting) {
File file = ConfigureProfileFragment.getBackupFileForCustomMode(app, changedProfile.stringKey); app.getSettingsHelper().updateExportListener(file, null);
boolean fileExporting = app.getSettingsHelper().isFileExporting(file); }
if (fileExporting) {
app.getSettingsHelper().updateExportListener(file, listener);
} }
} }
@ -673,10 +670,7 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment {
@Override @Override
public void onSettingsExportFinished(@NonNull File file, boolean succeed) { public void onSettingsExportFinished(@NonNull File file, boolean succeed) {
FragmentActivity activity = getActivity(); dismissProfileSavingDialog();
if (progress != null && activity != null && AndroidUtils.isActivityNotDestroyed(activity)) {
progress.dismiss();
}
if (succeed) { if (succeed) {
customProfileSaved(); customProfileSaved();
} else { } else {
@ -717,6 +711,7 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment {
private void saveProfile() { private void saveProfile() {
if (isNewProfile) { if (isNewProfile) {
DialogInterface.OnShowListener showListener = new DialogInterface.OnShowListener() { DialogInterface.OnShowListener showListener = new DialogInterface.OnShowListener() {
@Override @Override
public void onShow(DialogInterface dialog) { public void onShow(DialogInterface dialog) {
app.runInUIThread(new Runnable() { app.runInUIThread(new Runnable() {
@ -790,13 +785,23 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment {
} }
private void checkSavingProfile() { private void checkSavingProfile() {
File file = ConfigureProfileFragment.getBackupFileForCustomMode(app, changedProfile.stringKey); if (isNewProfile) {
boolean fileExporting = app.getSettingsHelper().isFileExporting(file); File file = ConfigureProfileFragment.getBackupFileForCustomMode(app, changedProfile.stringKey);
if (fileExporting) { boolean fileExporting = app.getSettingsHelper().isFileExporting(file);
showNewProfileSavingDialog(null); if (fileExporting) {
app.getSettingsHelper().updateExportListener(file, exportListener); showNewProfileSavingDialog(null);
} else if (isNewProfile) { app.getSettingsHelper().updateExportListener(file, exportListener);
customProfileSaved(); } else if (file.exists()) {
dismissProfileSavingDialog();
customProfileSaved();
}
}
}
private void dismissProfileSavingDialog() {
FragmentActivity activity = getActivity();
if (progress != null && activity != null && AndroidUtils.isActivityNotDestroyed(activity)) {
progress.dismiss();
} }
} }