From 2e541f08ec217704efd8e8d39ff4cf074492dbbe Mon Sep 17 00:00:00 2001 From: alperozturk Date: Thu, 6 Jun 2024 12:05:46 +0200 Subject: [PATCH 1/4] Rename .java to .kt Signed-off-by: alperozturk --- .../{SslUntrustedCertDialog.java => SslUntrustedCertDialog.kt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename app/src/main/java/com/owncloud/android/ui/dialog/{SslUntrustedCertDialog.java => SslUntrustedCertDialog.kt} (100%) diff --git a/app/src/main/java/com/owncloud/android/ui/dialog/SslUntrustedCertDialog.java b/app/src/main/java/com/owncloud/android/ui/dialog/SslUntrustedCertDialog.kt similarity index 100% rename from app/src/main/java/com/owncloud/android/ui/dialog/SslUntrustedCertDialog.java rename to app/src/main/java/com/owncloud/android/ui/dialog/SslUntrustedCertDialog.kt From d3d4231164982cea5c59d38748d0d3c5e9cb73fb Mon Sep 17 00:00:00 2001 From: alperozturk Date: Thu, 6 Jun 2024 12:05:47 +0200 Subject: [PATCH 2/4] Convert to kt and fix npe Signed-off-by: alperozturk --- .../ui/dialog/SslUntrustedCertDialog.kt | 325 +++++++++--------- 1 file changed, 159 insertions(+), 166 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/ui/dialog/SslUntrustedCertDialog.kt b/app/src/main/java/com/owncloud/android/ui/dialog/SslUntrustedCertDialog.kt index 4457105320..bf36c450ef 100644 --- a/app/src/main/java/com/owncloud/android/ui/dialog/SslUntrustedCertDialog.kt +++ b/app/src/main/java/com/owncloud/android/ui/dialog/SslUntrustedCertDialog.kt @@ -10,206 +10,199 @@ * SPDX-FileCopyrightText: 2014 MarĂ­a Asensio Valverde * SPDX-License-Identifier: GPL-2.0-only AND AGPL-3.0-or-later */ -package com.owncloud.android.ui.dialog; +package com.owncloud.android.ui.dialog -import android.app.Activity; -import android.app.Dialog; -import android.net.http.SslError; -import android.os.Bundle; -import android.view.View; -import android.view.View.OnClickListener; -import android.view.Window; -import android.webkit.SslErrorHandler; -import android.widget.Button; - -import com.google.android.material.dialog.MaterialAlertDialogBuilder; -import com.nextcloud.client.di.Injectable; -import com.owncloud.android.R; -import com.owncloud.android.databinding.SslUntrustedCertLayoutBinding; -import com.owncloud.android.lib.common.network.CertificateCombinedException; -import com.owncloud.android.lib.common.network.NetworkUtils; -import com.owncloud.android.lib.common.utils.Log_OC; -import com.owncloud.android.ui.adapter.CertificateCombinedExceptionViewAdapter; -import com.owncloud.android.ui.adapter.SslCertificateViewAdapter; -import com.owncloud.android.ui.adapter.SslErrorViewAdapter; -import com.owncloud.android.ui.adapter.X509CertificateViewAdapter; -import com.owncloud.android.utils.theme.ViewThemeUtils; - -import java.io.IOException; -import java.security.GeneralSecurityException; -import java.security.cert.X509Certificate; - -import javax.inject.Inject; - -import androidx.annotation.NonNull; -import androidx.fragment.app.DialogFragment; +import android.app.Dialog +import android.content.Context +import android.net.http.SslError +import android.os.Bundle +import android.view.View +import android.view.Window +import android.webkit.SslErrorHandler +import android.widget.Button +import androidx.fragment.app.DialogFragment +import com.google.android.material.dialog.MaterialAlertDialogBuilder +import com.nextcloud.client.di.Injectable +import com.owncloud.android.R +import com.owncloud.android.databinding.SslUntrustedCertLayoutBinding +import com.owncloud.android.lib.common.network.CertificateCombinedException +import com.owncloud.android.lib.common.network.NetworkUtils +import com.owncloud.android.lib.common.utils.Log_OC +import com.owncloud.android.ui.adapter.CertificateCombinedExceptionViewAdapter +import com.owncloud.android.ui.adapter.SslCertificateViewAdapter +import com.owncloud.android.ui.adapter.SslErrorViewAdapter +import com.owncloud.android.ui.adapter.X509CertificateViewAdapter +import com.owncloud.android.utils.theme.ViewThemeUtils +import java.io.IOException +import java.security.GeneralSecurityException +import java.security.cert.X509Certificate +import javax.inject.Inject /** * Dialog to show information about an untrusted certificate and allow the user to decide trust on it or not. * Abstract implementation of common functionality for different dialogs that get the information about the error and * the certificate from different classes. */ -public class SslUntrustedCertDialog extends DialogFragment implements Injectable { +open class SslUntrustedCertDialog : DialogFragment(), Injectable { + @JvmField + @Inject + var viewThemeUtils: ViewThemeUtils? = null - private final static String TAG = SslUntrustedCertDialog.class.getSimpleName(); + protected var binding: SslUntrustedCertLayoutBinding? = null + protected var mHandler: SslErrorHandler? = null + protected var m509Certificate: X509Certificate? = null - @Inject ViewThemeUtils viewThemeUtils; + private var mErrorViewAdapter: ErrorViewAdapter? = null + private var mCertificateViewAdapter: CertificateViewAdapter? = null - protected SslUntrustedCertLayoutBinding binding; - protected SslErrorHandler mHandler; - protected X509Certificate m509Certificate; - - private ErrorViewAdapter mErrorViewAdapter; - private CertificateViewAdapter mCertificateViewAdapter; - - public static SslUntrustedCertDialog newInstanceForEmptySslError(SslError error, SslErrorHandler handler) { - if (error == null) { - throw new IllegalArgumentException("Trying to create instance with parameter error == null"); - } - if (handler == null) { - throw new IllegalArgumentException("Trying to create instance with parameter handler == null"); - } - SslUntrustedCertDialog dialog = new SslUntrustedCertDialog(); - dialog.mHandler = handler; - dialog.mErrorViewAdapter = new SslErrorViewAdapter(error); - dialog.mCertificateViewAdapter = new SslCertificateViewAdapter(error.getCertificate()); - return dialog; - } - - public static SslUntrustedCertDialog newInstanceForFullSslError(CertificateCombinedException sslException) { - if (sslException == null) { - throw new IllegalArgumentException("Trying to create instance with parameter sslException == null"); - } - SslUntrustedCertDialog dialog = new SslUntrustedCertDialog(); - dialog.m509Certificate = sslException.getServerCertificate(); - dialog.mErrorViewAdapter = new CertificateCombinedExceptionViewAdapter(sslException); - dialog.mCertificateViewAdapter = new X509CertificateViewAdapter(sslException.getServerCertificate()); - return dialog; - } - - public static SslUntrustedCertDialog newInstanceForFullSslError(X509Certificate cert, SslError error, SslErrorHandler handler) { - if (cert == null) { - throw new IllegalArgumentException("Trying to create instance with parameter cert == null"); - } - if (error == null) { - throw new IllegalArgumentException("Trying to create instance with parameter error == null"); - } - if (handler == null) { - throw new IllegalArgumentException("Trying to create instance with parameter handler == null"); - } - SslUntrustedCertDialog dialog = new SslUntrustedCertDialog(); - dialog.m509Certificate = cert; - dialog.mHandler = handler; - dialog.mErrorViewAdapter = new SslErrorViewAdapter(error); - dialog.mCertificateViewAdapter = new X509CertificateViewAdapter(cert); - return dialog; - } - - @Override - public void onAttach(@NonNull Activity activity) { - Log_OC.d(TAG, "onAttach"); - super.onAttach(activity); - if (!(activity instanceof OnSslUntrustedCertListener)) { - throw new IllegalArgumentException("The host activity must implement " + OnSslUntrustedCertListener.class.getCanonicalName()); + override fun onAttach(context: Context) { + Log_OC.d(TAG, "onAttach") + super.onAttach(context) + require(activity is OnSslUntrustedCertListener) { + "The host activity must implement " + OnSslUntrustedCertListener::class.java.canonicalName } } - @Override - public void onCreate(Bundle savedInstanceState) { - Log_OC.d(TAG, "onCreate, savedInstanceState is " + savedInstanceState); - super.onCreate(savedInstanceState); - setRetainInstance(true); // force to keep the state of the fragment on configuration changes (such as device rotations) - setCancelable(false); - binding = null; + override fun onCreate(savedInstanceState: Bundle?) { + Log_OC.d(TAG, "onCreate, savedInstanceState is $savedInstanceState") + super.onCreate(savedInstanceState) + + // force to keep the state of the fragment on configuration changes (such as device rotations) + retainInstance = true + isCancelable = false + binding = null } - @NonNull - @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - Log_OC.d(TAG, "onCreateDialog, savedInstanceState is " + savedInstanceState); + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + Log_OC.d(TAG, "onCreateDialog, savedInstanceState is $savedInstanceState") - binding = SslUntrustedCertLayoutBinding.inflate(getLayoutInflater(), null, false); - binding.detailsScroll.setVisibility(View.GONE); - mErrorViewAdapter.updateErrorView(binding); + binding = SslUntrustedCertLayoutBinding.inflate(layoutInflater, null, false) + binding?.detailsScroll?.visibility = View.GONE + mErrorViewAdapter?.updateErrorView(binding) - binding.ok.setOnClickListener(new OnCertificateTrusted()); + binding?.ok?.setOnClickListener(OnCertificateTrusted()) - binding.cancel.setOnClickListener(new OnCertificateNotTrusted()); - - binding.detailsBtn.setOnClickListener(v -> { - if (binding.detailsScroll.getVisibility() == View.VISIBLE) { - binding.detailsScroll.setVisibility(View.GONE); - ((Button) v).setText(R.string.ssl_validator_btn_details_see); + binding?.cancel?.setOnClickListener(OnCertificateNotTrusted()) + binding?.detailsBtn?.setOnClickListener { v: View -> + if (binding?.detailsScroll?.visibility == View.VISIBLE) { + binding?.detailsScroll?.visibility = View.GONE + (v as Button).setText(R.string.ssl_validator_btn_details_see) } else { - binding.detailsScroll.setVisibility(View.VISIBLE); - ((Button) v).setText(R.string.ssl_validator_btn_details_hide); - mCertificateViewAdapter.updateCertificateView(binding); + binding?.detailsScroll?.visibility = View.VISIBLE + (v as Button).setText(R.string.ssl_validator_btn_details_hide) + mCertificateViewAdapter?.updateCertificateView(binding) } - }); - - - MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(binding.getRoot().getContext()); - builder.setView(binding.getRoot()); - - viewThemeUtils.dialog.colorMaterialAlertDialogBackground(binding.getRoot().getContext(), builder); - - final Dialog dialog = builder.create(); - dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); - return dialog; - } - - @Override - public void onDestroyView() { - Log_OC.d(TAG, "onDestroyView"); - if (getDialog() != null && getRetainInstance()) { - getDialog().setDismissMessage(null); } - super.onDestroyView(); + + val builder = MaterialAlertDialogBuilder(requireContext()) + builder.setView(binding?.getRoot()) + + viewThemeUtils?.dialog?.colorMaterialAlertDialogBackground(requireContext(), builder) + + return builder.create().apply { + requestWindowFeature(Window.FEATURE_NO_TITLE) + } } - private class OnCertificateNotTrusted implements OnClickListener { + override fun onDestroyView() { + Log_OC.d(TAG, "onDestroyView") + if (retainInstance) { + dialog?.setDismissMessage(null) + } + super.onDestroyView() + } - @Override - public void onClick(View v) { - getDialog().cancel(); - if (mHandler != null) { - mHandler.cancel(); + private inner class OnCertificateNotTrusted : View.OnClickListener { + override fun onClick(v: View) { + dialog?.cancel() + mHandler?.cancel() + } + } + + private inner class OnCertificateTrusted : View.OnClickListener { + override fun onClick(v: View) { + dismiss() + mHandler?.proceed() + + if (m509Certificate == null) { + Log_OC.d(TAG, "m509Certificate is null onClick dismissed") + return + } + + if (activity == null) { + Log_OC.d(TAG, "activity is null onClick dismissed") + return + } + + try { + // TODO make this asynchronously, it can take some time + NetworkUtils.addCertToKnownServersStore(m509Certificate, activity) + (activity as OnSslUntrustedCertListener?)?.onSavedCertificate() + } catch (e: GeneralSecurityException) { + (activity as OnSslUntrustedCertListener?)?.onFailedSavingCertificate() + Log_OC.e(TAG, "Server certificate could not be saved in the known-servers trust store ", e) + } catch (e: IOException) { + (activity as OnSslUntrustedCertListener?)?.onFailedSavingCertificate() + Log_OC.e(TAG, "Server certificate could not be saved in the known-servers trust store ", e) } } } - private class OnCertificateTrusted implements OnClickListener { + interface OnSslUntrustedCertListener { + fun onSavedCertificate() + fun onFailedSavingCertificate() + } - @Override - public void onClick(View v) { - dismiss(); - if (mHandler != null) { - mHandler.proceed(); - } - if (m509Certificate != null) { - Activity activity = getActivity(); - try { - NetworkUtils.addCertToKnownServersStore(m509Certificate, activity); // TODO make this asynchronously, it can take some time - ((OnSslUntrustedCertListener)activity).onSavedCertificate(); - } catch (GeneralSecurityException | IOException e) { - ((OnSslUntrustedCertListener)activity).onFailedSavingCertificate(); - Log_OC.e(TAG, "Server certificate could not be saved in the known-servers trust store ", e); - } + interface ErrorViewAdapter { + fun updateErrorView(binding: SslUntrustedCertLayoutBinding?) + } + + interface CertificateViewAdapter { + fun updateCertificateView(binding: SslUntrustedCertLayoutBinding?) + } + + companion object { + private val TAG: String = SslUntrustedCertDialog::class.java.simpleName + + @JvmStatic + fun newInstanceForEmptySslError(error: SslError?, handler: SslErrorHandler?): SslUntrustedCertDialog { + requireNotNull(error) { "Trying to create instance with parameter error == null" } + requireNotNull(handler) { "Trying to create instance with parameter handler == null" } + + return SslUntrustedCertDialog().apply { + mHandler = handler + mErrorViewAdapter = SslErrorViewAdapter(error) + mCertificateViewAdapter = SslCertificateViewAdapter(error.certificate) } } - } - public interface OnSslUntrustedCertListener { - void onSavedCertificate(); - void onFailedSavingCertificate(); - } + @JvmStatic + fun newInstanceForFullSslError(sslException: CertificateCombinedException?): SslUntrustedCertDialog { + requireNotNull(sslException) { "Trying to create instance with parameter sslException == null" } - public interface ErrorViewAdapter { - void updateErrorView(SslUntrustedCertLayoutBinding binding); - } + return SslUntrustedCertDialog().apply { + m509Certificate = sslException.serverCertificate + mErrorViewAdapter = CertificateCombinedExceptionViewAdapter(sslException) + mCertificateViewAdapter = X509CertificateViewAdapter(sslException.serverCertificate) + } + } - public interface CertificateViewAdapter { - void updateCertificateView(SslUntrustedCertLayoutBinding binding); + fun newInstanceForFullSslError( + cert: X509Certificate?, + error: SslError?, + handler: SslErrorHandler? + ): SslUntrustedCertDialog { + requireNotNull(cert) { "Trying to create instance with parameter cert == null" } + requireNotNull(error) { "Trying to create instance with parameter error == null" } + requireNotNull(handler) { "Trying to create instance with parameter handler == null" } + + return SslUntrustedCertDialog().apply { + m509Certificate = cert + mHandler = handler + mErrorViewAdapter = SslErrorViewAdapter(error) + mCertificateViewAdapter = X509CertificateViewAdapter(cert) + } + } } } From b6e25924172d9e5c59343febd4e57dad03ab7693 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Thu, 6 Jun 2024 12:28:49 +0200 Subject: [PATCH 3/4] Reduce lint Signed-off-by: alperozturk --- ...rtificateCombinedExceptionViewAdapter.java | 6 ++-- .../ui/adapter/SslCertificateViewAdapter.java | 4 +-- .../ui/adapter/SslErrorViewAdapter.java | 6 ++-- .../adapter/X509CertificateViewAdapter.java | 6 ++-- .../ui/dialog/SslUntrustedCertDialog.kt | 28 ++++++++++--------- 5 files changed, 28 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/ui/adapter/CertificateCombinedExceptionViewAdapter.java b/app/src/main/java/com/owncloud/android/ui/adapter/CertificateCombinedExceptionViewAdapter.java index 56af62867d..3cd549e298 100644 --- a/app/src/main/java/com/owncloud/android/ui/adapter/CertificateCombinedExceptionViewAdapter.java +++ b/app/src/main/java/com/owncloud/android/ui/adapter/CertificateCombinedExceptionViewAdapter.java @@ -16,18 +16,20 @@ import com.owncloud.android.databinding.SslUntrustedCertLayoutBinding; import com.owncloud.android.lib.common.network.CertificateCombinedException; import com.owncloud.android.ui.dialog.SslUntrustedCertDialog; +import androidx.annotation.NonNull; + public class CertificateCombinedExceptionViewAdapter implements SslUntrustedCertDialog.ErrorViewAdapter { //private final static String TAG = CertificateCombinedExceptionViewAdapter.class.getSimpleName(); - private CertificateCombinedException mSslException; + private final CertificateCombinedException mSslException; public CertificateCombinedExceptionViewAdapter(CertificateCombinedException sslException) { mSslException = sslException; } @Override - public void updateErrorView(SslUntrustedCertLayoutBinding binding) { + public void updateErrorView(@NonNull SslUntrustedCertLayoutBinding binding) { /// clean binding.reasonNoInfoAboutError.setVisibility(View.GONE); diff --git a/app/src/main/java/com/owncloud/android/ui/adapter/SslCertificateViewAdapter.java b/app/src/main/java/com/owncloud/android/ui/adapter/SslCertificateViewAdapter.java index 5a6a457b2c..615f187926 100644 --- a/app/src/main/java/com/owncloud/android/ui/adapter/SslCertificateViewAdapter.java +++ b/app/src/main/java/com/owncloud/android/ui/adapter/SslCertificateViewAdapter.java @@ -24,7 +24,7 @@ public class SslCertificateViewAdapter implements SslUntrustedCertDialog.Certifi //private final static String TAG = SslCertificateViewAdapter.class.getSimpleName(); - private SslCertificate mCertificate; + private final SslCertificate mCertificate; /** * Constructor @@ -36,7 +36,7 @@ public class SslCertificateViewAdapter implements SslUntrustedCertDialog.Certifi } @Override - public void updateCertificateView(SslUntrustedCertLayoutBinding binding) { + public void updateCertificateView(@NonNull SslUntrustedCertLayoutBinding binding) { if (mCertificate != null) { binding.nullCert.setVisibility(View.GONE); showSubject(mCertificate.getIssuedTo(), binding); diff --git a/app/src/main/java/com/owncloud/android/ui/adapter/SslErrorViewAdapter.java b/app/src/main/java/com/owncloud/android/ui/adapter/SslErrorViewAdapter.java index 804c262262..6ebc3c40ac 100644 --- a/app/src/main/java/com/owncloud/android/ui/adapter/SslErrorViewAdapter.java +++ b/app/src/main/java/com/owncloud/android/ui/adapter/SslErrorViewAdapter.java @@ -14,6 +14,8 @@ import android.view.View; import com.owncloud.android.databinding.SslUntrustedCertLayoutBinding; import com.owncloud.android.ui.dialog.SslUntrustedCertDialog; +import androidx.annotation.NonNull; + /** * Dialog to show an Untrusted Certificate */ @@ -21,14 +23,14 @@ public class SslErrorViewAdapter implements SslUntrustedCertDialog.ErrorViewAdap //private final static String TAG = SslErrorViewAdapter.class.getSimpleName(); - private SslError mSslError; + private final SslError mSslError; public SslErrorViewAdapter(SslError sslError) { mSslError = sslError; } @Override - public void updateErrorView(SslUntrustedCertLayoutBinding binding) { + public void updateErrorView(@NonNull SslUntrustedCertLayoutBinding binding) { /// clean binding.reasonNoInfoAboutError.setVisibility(View.GONE); diff --git a/app/src/main/java/com/owncloud/android/ui/adapter/X509CertificateViewAdapter.java b/app/src/main/java/com/owncloud/android/ui/adapter/X509CertificateViewAdapter.java index f7cf41ba52..8c46c8a15d 100644 --- a/app/src/main/java/com/owncloud/android/ui/adapter/X509CertificateViewAdapter.java +++ b/app/src/main/java/com/owncloud/android/ui/adapter/X509CertificateViewAdapter.java @@ -37,7 +37,7 @@ import androidx.annotation.NonNull; */ public class X509CertificateViewAdapter implements SslUntrustedCertDialog.CertificateViewAdapter { - private X509Certificate mCertificate; + private final X509Certificate mCertificate; private static final String TAG = X509CertificateViewAdapter.class.getSimpleName(); @@ -46,7 +46,7 @@ public class X509CertificateViewAdapter implements SslUntrustedCertDialog.Certif } @Override - public void updateCertificateView(SslUntrustedCertLayoutBinding binding) { + public void updateCertificateView(@NonNull SslUntrustedCertLayoutBinding binding) { if (mCertificate != null) { binding.nullCert.setVisibility(View.GONE); showSubject(mCertificate.getSubjectX500Principal(), binding); @@ -97,7 +97,7 @@ public class X509CertificateViewAdapter implements SslUntrustedCertDialog.Certif private String getDigestHexBytesWithColonsAndNewLines(Context context, final String digestType, final byte [] cert) { final byte[] rawDigest; - final String newLine = System.getProperty("line.separator"); + final String newLine = System.lineSeparator(); rawDigest = getDigest(digestType, cert); diff --git a/app/src/main/java/com/owncloud/android/ui/dialog/SslUntrustedCertDialog.kt b/app/src/main/java/com/owncloud/android/ui/dialog/SslUntrustedCertDialog.kt index bf36c450ef..2e5115596c 100644 --- a/app/src/main/java/com/owncloud/android/ui/dialog/SslUntrustedCertDialog.kt +++ b/app/src/main/java/com/owncloud/android/ui/dialog/SslUntrustedCertDialog.kt @@ -76,27 +76,29 @@ open class SslUntrustedCertDialog : DialogFragment(), Injectable { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { Log_OC.d(TAG, "onCreateDialog, savedInstanceState is $savedInstanceState") - binding = SslUntrustedCertLayoutBinding.inflate(layoutInflater, null, false) - binding?.detailsScroll?.visibility = View.GONE - mErrorViewAdapter?.updateErrorView(binding) + val layoutBinding = SslUntrustedCertLayoutBinding.inflate(layoutInflater, null, false) + this.binding = layoutBinding - binding?.ok?.setOnClickListener(OnCertificateTrusted()) + layoutBinding.detailsScroll.visibility = View.GONE + mErrorViewAdapter?.updateErrorView(layoutBinding) - binding?.cancel?.setOnClickListener(OnCertificateNotTrusted()) + layoutBinding.ok.setOnClickListener(OnCertificateTrusted()) - binding?.detailsBtn?.setOnClickListener { v: View -> - if (binding?.detailsScroll?.visibility == View.VISIBLE) { - binding?.detailsScroll?.visibility = View.GONE + layoutBinding.cancel.setOnClickListener(OnCertificateNotTrusted()) + + layoutBinding.detailsBtn.setOnClickListener { v: View -> + if (layoutBinding.detailsScroll.visibility == View.VISIBLE) { + layoutBinding.detailsScroll.visibility = View.GONE (v as Button).setText(R.string.ssl_validator_btn_details_see) } else { - binding?.detailsScroll?.visibility = View.VISIBLE + layoutBinding.detailsScroll.visibility = View.VISIBLE (v as Button).setText(R.string.ssl_validator_btn_details_hide) - mCertificateViewAdapter?.updateCertificateView(binding) + mCertificateViewAdapter?.updateCertificateView(layoutBinding) } } val builder = MaterialAlertDialogBuilder(requireContext()) - builder.setView(binding?.getRoot()) + builder.setView(layoutBinding.getRoot()) viewThemeUtils?.dialog?.colorMaterialAlertDialogBackground(requireContext(), builder) @@ -155,11 +157,11 @@ open class SslUntrustedCertDialog : DialogFragment(), Injectable { } interface ErrorViewAdapter { - fun updateErrorView(binding: SslUntrustedCertLayoutBinding?) + fun updateErrorView(binding: SslUntrustedCertLayoutBinding) } interface CertificateViewAdapter { - fun updateCertificateView(binding: SslUntrustedCertLayoutBinding?) + fun updateCertificateView(binding: SslUntrustedCertLayoutBinding) } companion object { From ee7c6e7874bbfd4d09152e52e677096570c67504 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Thu, 6 Jun 2024 14:33:00 +0200 Subject: [PATCH 4/4] Simplify variable namings Signed-off-by: alperozturk --- .../ui/dialog/SslUntrustedCertDialog.kt | 69 ++++++++++--------- 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/ui/dialog/SslUntrustedCertDialog.kt b/app/src/main/java/com/owncloud/android/ui/dialog/SslUntrustedCertDialog.kt index 2e5115596c..2324713791 100644 --- a/app/src/main/java/com/owncloud/android/ui/dialog/SslUntrustedCertDialog.kt +++ b/app/src/main/java/com/owncloud/android/ui/dialog/SslUntrustedCertDialog.kt @@ -44,16 +44,16 @@ import javax.inject.Inject * the certificate from different classes. */ open class SslUntrustedCertDialog : DialogFragment(), Injectable { + @JvmField @Inject var viewThemeUtils: ViewThemeUtils? = null protected var binding: SslUntrustedCertLayoutBinding? = null - protected var mHandler: SslErrorHandler? = null - protected var m509Certificate: X509Certificate? = null - - private var mErrorViewAdapter: ErrorViewAdapter? = null - private var mCertificateViewAdapter: CertificateViewAdapter? = null + protected var sslErrorHandler: SslErrorHandler? = null + protected var x509Certificate: X509Certificate? = null + private var errorViewAdapter: ErrorViewAdapter? = null + private var certificateViewAdapter: CertificateViewAdapter? = null override fun onAttach(context: Context) { Log_OC.d(TAG, "onAttach") @@ -79,26 +79,29 @@ open class SslUntrustedCertDialog : DialogFragment(), Injectable { val layoutBinding = SslUntrustedCertLayoutBinding.inflate(layoutInflater, null, false) this.binding = layoutBinding - layoutBinding.detailsScroll.visibility = View.GONE - mErrorViewAdapter?.updateErrorView(layoutBinding) + layoutBinding.run { + detailsScroll.visibility = View.GONE + errorViewAdapter?.updateErrorView(layoutBinding) - layoutBinding.ok.setOnClickListener(OnCertificateTrusted()) + ok.setOnClickListener(OnCertificateTrusted()) - layoutBinding.cancel.setOnClickListener(OnCertificateNotTrusted()) + cancel.setOnClickListener(OnCertificateNotTrusted()) - layoutBinding.detailsBtn.setOnClickListener { v: View -> - if (layoutBinding.detailsScroll.visibility == View.VISIBLE) { - layoutBinding.detailsScroll.visibility = View.GONE - (v as Button).setText(R.string.ssl_validator_btn_details_see) - } else { - layoutBinding.detailsScroll.visibility = View.VISIBLE - (v as Button).setText(R.string.ssl_validator_btn_details_hide) - mCertificateViewAdapter?.updateCertificateView(layoutBinding) + detailsBtn.setOnClickListener { v: View -> + if (detailsScroll.visibility == View.VISIBLE) { + detailsScroll.visibility = View.GONE + (v as Button).setText(R.string.ssl_validator_btn_details_see) + } else { + detailsScroll.visibility = View.VISIBLE + (v as Button).setText(R.string.ssl_validator_btn_details_hide) + certificateViewAdapter?.updateCertificateView(layoutBinding) + } } } - val builder = MaterialAlertDialogBuilder(requireContext()) - builder.setView(layoutBinding.getRoot()) + val builder = MaterialAlertDialogBuilder(requireContext()).apply { + setView(layoutBinding.getRoot()) + } viewThemeUtils?.dialog?.colorMaterialAlertDialogBackground(requireContext(), builder) @@ -118,16 +121,16 @@ open class SslUntrustedCertDialog : DialogFragment(), Injectable { private inner class OnCertificateNotTrusted : View.OnClickListener { override fun onClick(v: View) { dialog?.cancel() - mHandler?.cancel() + sslErrorHandler?.cancel() } } private inner class OnCertificateTrusted : View.OnClickListener { override fun onClick(v: View) { dismiss() - mHandler?.proceed() + sslErrorHandler?.proceed() - if (m509Certificate == null) { + if (x509Certificate == null) { Log_OC.d(TAG, "m509Certificate is null onClick dismissed") return } @@ -139,7 +142,7 @@ open class SslUntrustedCertDialog : DialogFragment(), Injectable { try { // TODO make this asynchronously, it can take some time - NetworkUtils.addCertToKnownServersStore(m509Certificate, activity) + NetworkUtils.addCertToKnownServersStore(x509Certificate, activity) (activity as OnSslUntrustedCertListener?)?.onSavedCertificate() } catch (e: GeneralSecurityException) { (activity as OnSslUntrustedCertListener?)?.onFailedSavingCertificate() @@ -173,9 +176,9 @@ open class SslUntrustedCertDialog : DialogFragment(), Injectable { requireNotNull(handler) { "Trying to create instance with parameter handler == null" } return SslUntrustedCertDialog().apply { - mHandler = handler - mErrorViewAdapter = SslErrorViewAdapter(error) - mCertificateViewAdapter = SslCertificateViewAdapter(error.certificate) + sslErrorHandler = handler + errorViewAdapter = SslErrorViewAdapter(error) + certificateViewAdapter = SslCertificateViewAdapter(error.certificate) } } @@ -184,9 +187,9 @@ open class SslUntrustedCertDialog : DialogFragment(), Injectable { requireNotNull(sslException) { "Trying to create instance with parameter sslException == null" } return SslUntrustedCertDialog().apply { - m509Certificate = sslException.serverCertificate - mErrorViewAdapter = CertificateCombinedExceptionViewAdapter(sslException) - mCertificateViewAdapter = X509CertificateViewAdapter(sslException.serverCertificate) + x509Certificate = sslException.serverCertificate + errorViewAdapter = CertificateCombinedExceptionViewAdapter(sslException) + certificateViewAdapter = X509CertificateViewAdapter(sslException.serverCertificate) } } @@ -200,10 +203,10 @@ open class SslUntrustedCertDialog : DialogFragment(), Injectable { requireNotNull(handler) { "Trying to create instance with parameter handler == null" } return SslUntrustedCertDialog().apply { - m509Certificate = cert - mHandler = handler - mErrorViewAdapter = SslErrorViewAdapter(error) - mCertificateViewAdapter = X509CertificateViewAdapter(cert) + x509Certificate = cert + sslErrorHandler = handler + errorViewAdapter = SslErrorViewAdapter(error) + certificateViewAdapter = X509CertificateViewAdapter(cert) } } }