Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check spinner bounds #3412

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.thoughtcrime.securesms.util.DynamicTheme;
import org.thoughtcrime.securesms.util.IntentUtils;
import org.thoughtcrime.securesms.util.Util;
import org.thoughtcrime.securesms.util.ViewUtil;
import org.thoughtcrime.securesms.util.views.ProgressDialog;

import java.lang.ref.WeakReference;
Expand Down Expand Up @@ -184,7 +185,7 @@ public void onTextChanged(CharSequence s, int start, int before, int count) { }
expandAdvanced = expandAdvanced || !TextUtils.isEmpty(strVal);

intVal = DcHelper.getInt(this, CONFIG_MAIL_SECURITY);
imapSecurity.setSelection(intVal);
imapSecurity.setSelection(ViewUtil.checkBounds(intVal, imapSecurity));
expandAdvanced = expandAdvanced || intVal != 0;

TextInputEditText smtpLoginInput = findViewById(R.id.smtp_login_text);
Expand All @@ -206,7 +207,7 @@ public void onTextChanged(CharSequence s, int start, int before, int count) { }
expandAdvanced = expandAdvanced || !TextUtils.isEmpty(strVal);

intVal = DcHelper.getInt(this, CONFIG_SEND_SECURITY);
smtpSecurity.setSelection(intVal);
smtpSecurity.setSelection(ViewUtil.checkBounds(intVal, smtpSecurity));
expandAdvanced = expandAdvanced || intVal != 0;

int serverFlags = DcHelper.getInt(this, CONFIG_SERVER_FLAGS);
Expand All @@ -221,14 +222,14 @@ public void onTextChanged(CharSequence s, int start, int before, int count) { }
}
// /remove gmail oauth2
}
authMethod.setSelection(sel);
authMethod.setSelection(ViewUtil.checkBounds(sel, authMethod));
expandAdvanced = expandAdvanced || sel != 0;

int imapCertificateChecks = DcHelper.getInt(this, "imap_certificate_checks");
if (imapCertificateChecks == 3) {
imapCertificateChecks = 2; // 3 is a deprecated alias for 2
}
certCheck.setSelection(imapCertificateChecks);
certCheck.setSelection(ViewUtil.checkBounds(imapCertificateChecks, certCheck));
expandAdvanced = expandAdvanced || imapCertificateChecks != 0;
} else if (getIntent() != null && getIntent().getBundleExtra(ACCOUNT_DATA) != null) {
// Companion app might have sent account data
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/thoughtcrime/securesms/util/ViewUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.interpolator.view.animation.FastOutSlowInInterpolator;

import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.AbsSpinner;
import android.widget.TextView;

import com.b44t.messenger.util.concurrent.ListenableFuture;
Expand All @@ -46,6 +49,8 @@
import org.thoughtcrime.securesms.util.views.Stub;

public class ViewUtil {
private final static String TAG = ViewUtil.class.getSimpleName();

@SuppressWarnings("deprecation")
public static void setBackground(final @NonNull View v, final @Nullable Drawable drawable) {
v.setBackground(drawable);
Expand Down Expand Up @@ -267,4 +272,15 @@ public static int getStatusBarHeight(@NonNull View view) {
return result;
}
}

// Checks if a selection is valid for a given Spinner view.
// Returns given selection if valid.
// Otherwise, to avoid ArrayIndexOutOfBoundsException, 0 is returned, assuming to refer to a good default.
public static int checkBounds(int selection, AbsSpinner view) {
if (selection < 0 || selection >= view.getCount()) {
Log.w(TAG, "index " + selection + " out of bounds of " + view.toString());
return 0;
Hocuri marked this conversation as resolved.
Show resolved Hide resolved
}
return selection;
}
}
Loading