2020-04-16 15:44:53 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Proton Technologies AG
|
|
|
|
*
|
|
|
|
* This file is part of ProtonMail.
|
|
|
|
*
|
|
|
|
* ProtonMail is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* ProtonMail is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with ProtonMail. If not, see https://www.gnu.org/licenses/.
|
|
|
|
*/
|
|
|
|
package ch.protonmail.android.activities
|
|
|
|
|
|
|
|
import android.os.Handler
|
|
|
|
import android.view.View
|
2020-05-06 06:25:14 +00:00
|
|
|
import android.view.ViewGroup
|
|
|
|
import android.widget.TextView
|
|
|
|
import androidx.annotation.StringRes
|
2020-04-16 15:44:53 +00:00
|
|
|
import butterknife.BindView
|
|
|
|
import ch.protonmail.android.R
|
|
|
|
import ch.protonmail.android.jobs.PingJob
|
2020-05-06 06:25:14 +00:00
|
|
|
import ch.protonmail.android.utils.INetworkConfiguratorCallback
|
2020-04-16 15:44:53 +00:00
|
|
|
import ch.protonmail.android.utils.NetworkUtil
|
2020-05-06 06:25:14 +00:00
|
|
|
import com.google.android.material.snackbar.Snackbar
|
|
|
|
import com.google.android.material.snackbar.SnackbarContentLayout
|
2020-05-05 10:16:14 +00:00
|
|
|
import timber.log.Timber
|
2020-09-28 14:48:42 +00:00
|
|
|
import javax.inject.Inject
|
2020-04-16 15:44:53 +00:00
|
|
|
|
2020-10-02 10:19:41 +00:00
|
|
|
abstract class BaseConnectivityActivity : BaseActivity() {
|
2020-04-16 15:44:53 +00:00
|
|
|
|
2020-09-28 14:48:42 +00:00
|
|
|
@Inject
|
|
|
|
lateinit var networkUtil : NetworkUtil
|
|
|
|
|
|
|
|
@BindView(R.id.layout_no_connectivity_info)
|
2020-10-02 10:19:41 +00:00
|
|
|
protected lateinit var mSnackLayout: View
|
|
|
|
var mNoConnectivitySnack: Snackbar? = null
|
|
|
|
protected var mCheckForConnectivitySnack: Snackbar? = null
|
2020-04-16 15:44:53 +00:00
|
|
|
|
2020-10-02 10:19:41 +00:00
|
|
|
protected var pingHandler = Handler()
|
|
|
|
protected var pingRunnable: Runnable = Runnable { mJobManager.addJobInBackground(PingJob()) }
|
2020-04-16 15:44:53 +00:00
|
|
|
|
2020-10-02 10:19:41 +00:00
|
|
|
private var connectivityRetryListener = RetryListener()
|
2020-04-16 15:44:53 +00:00
|
|
|
|
2020-10-02 10:19:41 +00:00
|
|
|
protected open inner class RetryListener : View.OnClickListener {
|
2020-04-16 15:44:53 +00:00
|
|
|
|
2020-10-02 10:19:41 +00:00
|
|
|
override fun onClick(v: View) {
|
2020-09-28 14:48:42 +00:00
|
|
|
mCheckForConnectivitySnack = networkUtil.setCheckingConnectionSnackLayout(
|
2020-10-02 10:19:41 +00:00
|
|
|
mSnackLayout,
|
|
|
|
this@BaseConnectivityActivity
|
|
|
|
)
|
2020-09-28 14:48:42 +00:00
|
|
|
mCheckForConnectivitySnack?.show()
|
2020-10-02 10:19:41 +00:00
|
|
|
if (mNoConnectivitySnack != null && mNoConnectivitySnack!!.isShownOrQueued) {
|
|
|
|
mNoConnectivitySnack!!.dismiss()
|
|
|
|
}
|
|
|
|
pingHandler.removeCallbacks(pingRunnable)
|
|
|
|
pingHandler.postDelayed(pingRunnable, 3000)
|
2020-05-06 06:25:14 +00:00
|
|
|
|
2020-10-02 10:19:41 +00:00
|
|
|
retryWithDoh()
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 06:25:14 +00:00
|
|
|
|
2020-10-02 10:19:41 +00:00
|
|
|
protected fun retryWithDoh() {
|
|
|
|
if (mNetworkUtil.isConnected()) {
|
|
|
|
val thirdPartyConnectionsEnabled = mUserManager.user.allowSecureConnectionsViaThirdParties
|
|
|
|
if (thirdPartyConnectionsEnabled) {
|
|
|
|
Timber.d("Third party connections enabled, attempting DoH...")
|
|
|
|
networkConfigurator.refreshDomainsAsync() // refreshDomains(false) // switch to new here
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-16 15:44:53 +00:00
|
|
|
|
2020-10-02 10:19:41 +00:00
|
|
|
@JvmOverloads
|
|
|
|
protected fun showNoConnSnack(
|
|
|
|
listener: RetryListener? = null,
|
|
|
|
@StringRes message: Int = R.string.no_connectivity_detected_troubleshoot,
|
|
|
|
view: View = mSnackLayout,
|
|
|
|
callback: INetworkConfiguratorCallback
|
|
|
|
) {
|
|
|
|
val user = mUserManager.user
|
2020-09-28 14:48:42 +00:00
|
|
|
mNoConnectivitySnack = mNoConnectivitySnack ?: networkUtil.setNoConnectionSnackLayout(
|
2020-10-02 10:19:41 +00:00
|
|
|
view,
|
|
|
|
this,
|
|
|
|
listener ?: connectivityRetryListener,
|
2020-09-28 14:48:42 +00:00
|
|
|
|
2020-10-02 10:19:41 +00:00
|
|
|
message,
|
|
|
|
user,
|
|
|
|
callback
|
2020-09-28 14:48:42 +00:00
|
|
|
, false)
|
2020-10-02 10:19:41 +00:00
|
|
|
mNoConnectivitySnack!!.show()
|
|
|
|
val contentLayout = (mNoConnectivitySnack!!.view as ViewGroup).getChildAt(0) as SnackbarContentLayout
|
|
|
|
val vvv: TextView = contentLayout.actionView
|
|
|
|
mCheckForConnectivitySnack?.saveDismiss()
|
2020-05-06 06:25:14 +00:00
|
|
|
|
2020-10-02 10:19:41 +00:00
|
|
|
if (mUserManager.user.allowSecureConnectionsViaThirdParties && autoRetry && !isDohOngoing && !isFinishing) {
|
|
|
|
window.decorView.postDelayed(
|
|
|
|
{ vvv.callOnClick() },
|
|
|
|
500
|
|
|
|
)
|
2020-05-06 06:25:14 +00:00
|
|
|
}
|
2020-10-02 10:19:41 +00:00
|
|
|
}
|
2020-04-16 15:44:53 +00:00
|
|
|
|
2020-10-02 10:19:41 +00:00
|
|
|
protected fun hideNoConnSnack() {
|
|
|
|
mNoConnectivitySnack?.saveDismiss()
|
|
|
|
mCheckForConnectivitySnack?.saveDismiss()
|
|
|
|
}
|
2020-04-16 15:44:53 +00:00
|
|
|
|
2020-10-02 10:19:41 +00:00
|
|
|
private fun Snackbar.saveDismiss() {
|
2020-04-16 15:44:53 +00:00
|
|
|
if (isShownOrQueued) {
|
2020-10-02 10:19:41 +00:00
|
|
|
dismiss()
|
|
|
|
}
|
|
|
|
}
|
2020-04-16 15:44:53 +00:00
|
|
|
}
|