From 6326f5eba576013d80a2f020e0e00ae808003f45 Mon Sep 17 00:00:00 2001 From: M M Arif Date: Fri, 12 Aug 2022 04:02:04 +0200 Subject: [PATCH] Port accept cert dialog to M3 (#1184) Actually it is working, just need to restart the app. So closing #1119 and also porting the Alert dialogs for accepting the certificates and remove account to M3. Co-authored-by: M M Arif Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1184 --- .../activities/SettingsSecurityActivity.java | 67 ++++++++----------- .../gitnex/adapters/UserAccountsAdapter.java | 12 ++-- .../helpers/ssl/MemorizingTrustManager.java | 16 ++--- 3 files changed, 43 insertions(+), 52 deletions(-) diff --git a/app/src/main/java/org/mian/gitnex/activities/SettingsSecurityActivity.java b/app/src/main/java/org/mian/gitnex/activities/SettingsSecurityActivity.java index ffd3a5e4..ca1704a5 100644 --- a/app/src/main/java/org/mian/gitnex/activities/SettingsSecurityActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/SettingsSecurityActivity.java @@ -2,7 +2,6 @@ package org.mian.gitnex.activities; import android.app.KeyguardManager; import android.content.Context; -import android.os.Build; import android.os.Bundle; import android.util.Log; import android.view.View; @@ -82,61 +81,53 @@ public class SettingsSecurityActivity extends BaseActivity { // biometric switcher switchBiometric.setOnCheckedChangeListener((buttonView, isChecked) -> { - if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if(isChecked) { - if(isChecked) { + BiometricManager biometricManager = BiometricManager.from(ctx); + KeyguardManager keyguardManager = (KeyguardManager) ctx.getSystemService(Context.KEYGUARD_SERVICE); - BiometricManager biometricManager = BiometricManager.from(ctx); - KeyguardManager keyguardManager = (KeyguardManager) ctx.getSystemService(Context.KEYGUARD_SERVICE); + if (!keyguardManager.isDeviceSecure()) { - if (!keyguardManager.isDeviceSecure()) { + switch(biometricManager.canAuthenticate(BIOMETRIC_STRONG | DEVICE_CREDENTIAL)) { - switch(biometricManager.canAuthenticate(BIOMETRIC_STRONG | DEVICE_CREDENTIAL)) { + case BiometricManager.BIOMETRIC_SUCCESS: - case BiometricManager.BIOMETRIC_SUCCESS: + tinyDB.putBoolean("biometricStatus", true); + Toasty.success(appCtx, getResources().getString(R.string.settingsSave)); + break; + case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE: + case BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED: + case BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED: + case BiometricManager.BIOMETRIC_STATUS_UNKNOWN: - tinyDB.putBoolean("biometricStatus", true); - Toasty.success(appCtx, getResources().getString(R.string.settingsSave)); - break; - case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE: - case BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED: - case BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED: - case BiometricManager.BIOMETRIC_STATUS_UNKNOWN: + tinyDB.putBoolean("biometricStatus", false); + switchBiometric.setChecked(false); + Toasty.error(appCtx, getResources().getString(R.string.biometricNotSupported)); + break; + case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE: - tinyDB.putBoolean("biometricStatus", false); - switchBiometric.setChecked(false); - Toasty.error(appCtx, getResources().getString(R.string.biometricNotSupported)); - break; - case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE: + tinyDB.putBoolean("biometricStatus", false); + switchBiometric.setChecked(false); + Toasty.error(appCtx, getResources().getString(R.string.biometricNotAvailable)); + break; + case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED: - tinyDB.putBoolean("biometricStatus", false); - switchBiometric.setChecked(false); - Toasty.error(appCtx, getResources().getString(R.string.biometricNotAvailable)); - break; - case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED: - - tinyDB.putBoolean("biometricStatus", false); - switchBiometric.setChecked(false); - Toasty.info(appCtx, getResources().getString(R.string.enrollBiometric)); - break; - } - } - else { - - tinyDB.putBoolean("biometricStatus", true); - Toasty.success(appCtx, getResources().getString(R.string.settingsSave)); + tinyDB.putBoolean("biometricStatus", false); + switchBiometric.setChecked(false); + Toasty.info(appCtx, getResources().getString(R.string.enrollBiometric)); + break; } } else { - tinyDB.putBoolean("biometricStatus", false); + tinyDB.putBoolean("biometricStatus", true); Toasty.success(appCtx, getResources().getString(R.string.settingsSave)); } } else { tinyDB.putBoolean("biometricStatus", false); - Toasty.warning(appCtx, getResources().getString(R.string.biometricNotSupported)); + Toasty.success(appCtx, getResources().getString(R.string.settingsSave)); } }); diff --git a/app/src/main/java/org/mian/gitnex/adapters/UserAccountsAdapter.java b/app/src/main/java/org/mian/gitnex/adapters/UserAccountsAdapter.java index cec56acc..2095609b 100644 --- a/app/src/main/java/org/mian/gitnex/adapters/UserAccountsAdapter.java +++ b/app/src/main/java/org/mian/gitnex/adapters/UserAccountsAdapter.java @@ -12,9 +12,8 @@ import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.NonNull; -import androidx.appcompat.app.AlertDialog; -import androidx.appcompat.content.res.AppCompatResources; import androidx.recyclerview.widget.RecyclerView; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; import org.gitnex.tea4j.v2.models.NotificationCount; import org.mian.gitnex.R; import org.mian.gitnex.activities.AddNewAccountActivity; @@ -66,18 +65,19 @@ public class UserAccountsAdapter extends RecyclerView.Adapter { - new AlertDialog.Builder(context) - .setIcon(AppCompatResources.getDrawable(context, R.drawable.ic_delete)) + MaterialAlertDialogBuilder materialAlertDialogBuilder = new MaterialAlertDialogBuilder(context) .setTitle(context.getResources().getString(R.string.removeAccountPopupTitle)) .setMessage(context.getResources().getString(R.string.removeAccountPopupMessage)) + .setNeutralButton(context.getResources().getString(R.string.cancelButton), null) .setPositiveButton(context.getResources().getString(R.string.removeButton), (dialog, which) -> { updateLayoutByPosition(getBindingAdapterPosition()); UserAccountsApi userAccountsApi = BaseApi.getInstance(context, UserAccountsApi.class); assert userAccountsApi != null; userAccountsApi.deleteAccount(Integer.parseInt(String.valueOf(accountId))); - }).setNeutralButton(context.getResources().getString(R.string.cancelButton), null) - .show(); + }); + + materialAlertDialogBuilder.create().show(); }); itemView.setOnClickListener(switchAccount -> { diff --git a/app/src/main/java/org/mian/gitnex/helpers/ssl/MemorizingTrustManager.java b/app/src/main/java/org/mian/gitnex/helpers/ssl/MemorizingTrustManager.java index 4cae2bf4..f5d23824 100644 --- a/app/src/main/java/org/mian/gitnex/helpers/ssl/MemorizingTrustManager.java +++ b/app/src/main/java/org/mian/gitnex/helpers/ssl/MemorizingTrustManager.java @@ -1,6 +1,5 @@ package org.mian.gitnex.helpers.ssl; -import android.app.AlertDialog; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; @@ -10,6 +9,7 @@ import android.content.SharedPreferences; import android.util.Base64; import android.util.SparseArray; import androidx.core.app.NotificationCompat; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; import org.mian.gitnex.R; import org.mian.gitnex.activities.BaseActivity; import java.io.ByteArrayInputStream; @@ -562,15 +562,15 @@ public class MemorizingTrustManager implements X509TrustManager { final int myId = createDecisionId(choice); ((BaseActivity) context).runOnUiThread(() -> { - AlertDialog.Builder builder = new AlertDialog.Builder(context); - builder.setTitle(titleId); - builder.setMessage(message); - builder.setPositiveButton(R.string.mtmDecisionAlways, (dialog, which) -> interactResult(myId, MTMDecision.DECISION_ALWAYS)); - builder.setNeutralButton(R.string.mtmDecisionAbort, (dialog, which) -> interactResult(myId, MTMDecision.DECISION_ABORT)); - builder.setOnCancelListener(dialog -> interactResult(myId, MTMDecision.DECISION_ABORT)); + MaterialAlertDialogBuilder materialAlertDialogBuilder = new MaterialAlertDialogBuilder(context) + .setTitle(titleId) + .setMessage(message) + .setPositiveButton(R.string.mtmDecisionAlways, (dialog, which) -> interactResult(myId, MTMDecision.DECISION_ALWAYS)) + .setNeutralButton(R.string.mtmDecisionAbort, (dialog, which) -> interactResult(myId, MTMDecision.DECISION_ABORT)) + .setOnCancelListener(dialog -> interactResult(myId, MTMDecision.DECISION_ABORT)); - builder.create().show(); + materialAlertDialogBuilder.create().show(); }); try {