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

View file

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