BaseFragment: in the event no view is available, use toast

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2018-07-09 17:29:24 +02:00
parent d7ea078cdf
commit b997a2581b

View File

@ -15,6 +15,7 @@ import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.Toast;
import com.wireguard.android.Application; import com.wireguard.android.Application;
import com.wireguard.android.R; import com.wireguard.android.R;
@ -109,16 +110,14 @@ public abstract class BaseFragment extends Fragment implements OnSelectedTunnelC
tunnel.setState(State.of(checked)).whenComplete((state, throwable) -> { tunnel.setState(State.of(checked)).whenComplete((state, throwable) -> {
if (throwable == null) if (throwable == null)
return; return;
final View view = getView();
if (view == null) {
Log.e(TAG, "setTunnelStateWithPermissionsResult() with no view");
return;
}
final Context context = view.getContext();
final String error = ExceptionLoggers.unwrapMessage(throwable); final String error = ExceptionLoggers.unwrapMessage(throwable);
final int messageResId = checked ? R.string.error_up : R.string.error_down; final int messageResId = checked ? R.string.error_up : R.string.error_down;
final String message = context.getString(messageResId, error); final String message = getContext().getString(messageResId, error);
final View view = getView();
if (view != null)
Snackbar.make(view, message, Snackbar.LENGTH_LONG).show(); Snackbar.make(view, message, Snackbar.LENGTH_LONG).show();
else
Toast.makeText(getContext(), message, Toast.LENGTH_LONG).show();
Log.e(TAG, message, throwable); Log.e(TAG, message, throwable);
}); });
} }