Skip to content

Commit

Permalink
[#198] Display different error if there's no connection
Browse files Browse the repository at this point in the history
  • Loading branch information
dzolnai committed Nov 20, 2019
1 parent cd4dfa8 commit e80929d
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 79 deletions.
79 changes: 0 additions & 79 deletions app/src/main/java/nl/eduvpn/app/utils/ErrorDialog.java

This file was deleted.

98 changes: 98 additions & 0 deletions app/src/main/java/nl/eduvpn/app/utils/ErrorDialog.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* This file is part of eduVPN.
*
* eduVPN 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.
*
* eduVPN 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 eduVPN. If not, see <http://www.gnu.org/licenses/>.
*/

package nl.eduvpn.app.utils

import android.app.Dialog
import android.content.Context
import android.view.View
import android.widget.Button
import android.widget.TextView

import androidx.annotation.StringRes
import nl.eduvpn.app.R
import android.net.NetworkInfo
import androidx.core.content.ContextCompat.getSystemService
import android.net.ConnectivityManager



/**
* Utility class for displaying error dialogs through the entire application.
* Created by Daniel Zolnai on 2016-10-12.
*/
object ErrorDialog {

/**
* Shows a new error dialog.
*
* @param context The application or activity context.
* @param title The title of the dialog (string resource ID).
* @param message The message to show as a string.
*/
@JvmStatic
fun show(context: Context, @StringRes title: Int, message: String): Dialog {
return show(context, context.getString(title), message)
}

/**
* Shows a new error dialog.
*
* @param context The application or activity context.
* @param title The title of the dialog (string resource ID).
* @param message The message to show (string resource ID).
*/
@JvmStatic
fun show(context: Context, @StringRes title: Int, @StringRes message: Int): Dialog {
return show(context, context.getString(title), context.getString(message))
}

/**
* Shows a new error dialog
*
* @param context The application or activity context.
* @param title The title of the dialog.
* @param message The message to show.
*/
@JvmStatic
fun show(context: Context, title: String, message: String): Dialog {
val dialog = Dialog(context, R.style.ErrorDialog)
dialog.setCanceledOnTouchOutside(true)
dialog.setContentView(R.layout.dialog_error)
val view = dialog.findViewById<View>(R.id.errorDialog)
val titleView = view.findViewById<TextView>(R.id.title)
val errorTextView = view.findViewById<TextView>(R.id.errorText)
val confirmButton = view.findViewById<Button>(R.id.confirmButton)
if (hasNetworkConnection(context)) {
titleView.text = title
errorTextView.text = message
} else {
titleView.setText(R.string.error_no_internet_connection)
errorTextView.text = context.getString(R.string.error_no_internet_connection_message, message)
}
confirmButton.setOnClickListener { dialog.dismiss() }
dialog.show()
return dialog
}

@Suppress("DEPRECATION")
fun hasNetworkConnection(context: Context) : Boolean {
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
val activeNetworkInfo = connectivityManager?.activeNetworkInfo
return activeNetworkInfo?.isConnected == true
}
}
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,7 @@
<string name="error_parsing_profiles">Unable to parse the profile list.</string>
<string name="select_a_profile_title">Select a profile</string>
<string name="loading_browser_for_authorization">Loading browser for authorization</string>
<string name="error_no_internet_connection">No internet connection</string>
<string name="error_no_internet_connection_message">It seems that you have no connection to the internet. Please check your connection, and try again.\nOriginal error: %s</string>

</resources>

0 comments on commit e80929d

Please sign in to comment.