1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-12-23 03:11:30 +00:00

Prevent crashes when dismissing dialogs

This commit is contained in:
Felix Ableitner 2017-09-27 12:11:26 +09:00
parent 9ca1c8acd5
commit bdec750b34
7 changed files with 40 additions and 32 deletions

View file

@ -235,20 +235,14 @@ public class DeviceActivity extends SyncthingActivity implements View.OnClickLis
outState.putString("device", new Gson().toJson(mDevice));
if (mIsCreateMode){
outState.putBoolean(IS_SHOWING_DISCARD_DIALOG, mDiscardDialog != null && mDiscardDialog.isShowing());
if(mDiscardDialog != null){
mDiscardDialog.cancel();
}
Util.dismissDialogSafe(mDiscardDialog, this);
}
outState.putBoolean(IS_SHOWING_COMPRESSION_DIALOG, mCompressionDialog != null && mCompressionDialog.isShowing());
if(mCompressionDialog != null){
mCompressionDialog.cancel();
}
Util.dismissDialogSafe(mCompressionDialog, this);
outState.putBoolean(IS_SHOWING_DELETE_DIALOG, mDeleteDialog != null && mDeleteDialog.isShowing());
if (mDeleteDialog != null) {
mDeleteDialog.cancel();
}
Util.dismissDialogSafe(mDeleteDialog, this);
}
private void onServiceConnected() {

View file

@ -31,6 +31,7 @@ import com.nutomic.syncthingandroid.model.Device;
import com.nutomic.syncthingandroid.model.Folder;
import com.nutomic.syncthingandroid.service.SyncthingService;
import com.nutomic.syncthingandroid.util.TextWatcherAdapter;
import com.nutomic.syncthingandroid.util.Util;
import java.io.File;
import java.io.IOException;
@ -242,15 +243,11 @@ public class FolderActivity extends SyncthingActivity
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(IS_SHOWING_DELETE_DIALOG, mDeleteDialog != null && mDeleteDialog.isShowing());
if (mDeleteDialog != null) {
mDeleteDialog.cancel();
}
Util.dismissDialogSafe(mDeleteDialog, this);
if (mIsCreateMode){
outState.putBoolean(IS_SHOW_DISCARD_DIALOG, mDiscardDialog != null && mDiscardDialog.isShowing());
if(mDiscardDialog != null){
mDiscardDialog.cancel();
}
Util.dismissDialogSafe(mDiscardDialog, this);
}
}

View file

@ -288,9 +288,7 @@ public class MainActivity extends StateDialogActivity
outState.putParcelable(QRCODE_BITMAP_KEY, ((BitmapDrawable) qrCode.getDrawable()).getBitmap());
outState.putString(DEVICEID_KEY, deviceID.getText().toString());
}
if (mRestartDialog != null){
mRestartDialog.cancel();
}
Util.dismissDialogSafe(mRestartDialog, this);
}
}

View file

@ -22,6 +22,7 @@ import com.google.common.io.Files;
import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.model.Folder;
import com.nutomic.syncthingandroid.service.SyncthingService;
import com.nutomic.syncthingandroid.util.Util;
import java.io.File;
import java.io.FileNotFoundException;
@ -258,7 +259,7 @@ public class ShareActivity extends StateDialogActivity
}
protected void onPostExecute(Boolean isError) {
mProgress.dismiss();
Util.dismissDialogSafe(mProgress, ShareActivity.this);
Toast.makeText(ShareActivity.this, mIgnored > 0 ?
getResources().getQuantityString(R.plurals.copy_success_partially, mCopied,
mCopied, mFolder.label, mIgnored) :

View file

@ -13,6 +13,7 @@ import android.widget.TextView;
import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.service.SyncthingService;
import com.nutomic.syncthingandroid.util.Util;
/**
* Handles loading/disabled dialogs.
@ -78,10 +79,8 @@ public abstract class StateDialogActivity extends SyncthingActivity {
}
private void dismissDisabledDialog() {
if (mDisabledDialog != null && mDisabledDialog.isShowing()) {
mDisabledDialog.dismiss();
mDisabledDialog = null;
}
Util.dismissDialogSafe(mDisabledDialog, this);
mDisabledDialog = null;
}
/**
@ -111,9 +110,7 @@ public abstract class StateDialogActivity extends SyncthingActivity {
}
private void dismissLoadingDialog() {
if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
mLoadingDialog.dismiss();
mLoadingDialog = null;
}
Util.dismissDialogSafe(mLoadingDialog, this);
mLoadingDialog = null;
}
}

View file

@ -10,6 +10,7 @@ import com.annimon.stream.Stream;
import com.nutomic.syncthingandroid.service.RestApi;
import com.nutomic.syncthingandroid.service.SyncthingService;
import com.nutomic.syncthingandroid.service.SyncthingServiceBinder;
import com.nutomic.syncthingandroid.util.Util;
import java.util.LinkedList;

View file

@ -1,19 +1,22 @@
package com.nutomic.syncthingandroid.util;
import android.app.Activity;
import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.widget.Toast;
import android.util.Log;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import java.io.IOException;
import java.io.DataOutputStream;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;
import com.nutomic.syncthingandroid.R;
import java.io.DataOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import eu.chainfire.libsuperuser.Shell;
public class Util {
@ -116,4 +119,21 @@ public class Util {
}
return false;
}
/**
* Make sure that dialog is showing and activity is valid before dismissing dialog, to prevent
* various crashes.
*/
public static void dismissDialogSafe(Dialog dialog, Activity activity) {
if (dialog == null || !dialog.isShowing())
return;
if (activity.isFinishing())
return;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN && activity.isDestroyed())
return;
dialog.dismiss();
}
}