Skip to content

Commit

Permalink
Version 1.7.7: Minor bugs and better error messages during login
Browse files Browse the repository at this point in the history
  • Loading branch information
Nils committed Dec 27, 2020
1 parent ca5c390 commit 13f6c35
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 17 deletions.
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ apply plugin: 'kotlin-android-extensions'

android {
useLibrary 'org.apache.http.legacy'
compileSdkVersion 28
compileSdkVersion 29
defaultConfig {
applicationId "de.nils_beyer.android.testapplication"
minSdkVersion 17
targetSdkVersion 28
versionCode 22
versionName "1.7.6"
targetSdkVersion 29
versionCode 23
versionName "1.7.7"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<!-- Permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import kotlinx.android.synthetic.main.activity_login.*


import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.os.VibrationEffect
Expand All @@ -12,6 +13,7 @@ import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.inputmethod.InputMethodManager
import de.nils_beyer.android.Vertretungen.account.AccountSpinner
import de.nils_beyer.android.Vertretungen.download.UnAuthorizedException


class LoginActivity : AppCompatActivity() {
Expand All @@ -29,6 +31,8 @@ class LoginActivity : AppCompatActivity() {
canClose = !login_account_spinner.hasOnlyUnregistered(applicationContext)


activity_login_text_version.text = String.format(getString(R.string.info_app_with_version), getAppVersion())

if (Build.VERSION.SDK_INT >= 23) {
window.statusBarColor = resources.getColor(R.color.login_color, theme)
}
Expand Down Expand Up @@ -60,6 +64,7 @@ class LoginActivity : AppCompatActivity() {
finish()
} else {
login_error.visibility = View.VISIBLE
login_error.text = getString(R.string.login_error)
val vibratorService = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator

// Use new VibrationEffect call on Android Oreo
Expand All @@ -76,7 +81,23 @@ class LoginActivity : AppCompatActivity() {
// on error
progressBar.visibility = View.GONE
login_btn_login.isEnabled = true
Snackbar.make(activity_login, getString(R.string.io_error), Snackbar.LENGTH_LONG).show()

login_error.visibility = View.VISIBLE
login_error.text = it.message

val vibratorService = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator

// Use new VibrationEffect call on Android Oreo
// if not available, use the deprecated vibrate method.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibratorService.vibrate(VibrationEffect.createOneShot(
100, VibrationEffect.DEFAULT_AMPLITUDE
))
} else {
vibratorService.vibrate(100)
}

Snackbar.make(activity_login, String.format(getString(R.string.activity_login_generic_error_template), it.message), Snackbar.LENGTH_LONG).show()
})
}

Expand All @@ -100,9 +121,9 @@ class LoginActivity : AppCompatActivity() {
private fun checkPassword(callback: (Boolean)->(Unit), error : (Exception) -> Unit) {
Thread(Runnable {
try {
login_account_spinner.getSelectedAccount()!!.tryRegister(this, login_username.text.toString(), login_password.text.toString())
login_account_spinner.getSelectedAccount().tryRegister(this, login_username.text.toString(), login_password.text.toString())
runOnUiThread { callback(true) }
} catch (e: SecurityException) {
} catch (e: UnAuthorizedException) {
// In case that the authorization
// credentials are wrong
// a security Exception is thrown by the
Expand All @@ -128,4 +149,16 @@ class LoginActivity : AppCompatActivity() {
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
}

/**
* Returns the app version name or null if packageManager throws an error
*/
private fun getAppVersion() : String? {
return try {
val pInfo = packageManager.getPackageInfo(packageName, 0)
pInfo.versionName
} catch (e: PackageManager.NameNotFoundException) {
null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import java.net.HttpURLConnection
import java.net.URL
import javax.net.ssl.HttpsURLConnection

@Throws(SecurityException::class, IllegalStateException::class)
class UnAuthorizedException : Exception()

@Throws(SecurityException::class, IllegalStateException::class, UnAuthorizedException::class)
fun downloadHTMLFileWithCredientials(url : String, username : String, password : String) : String {
// Setup HTTP connection
val urlConnection = URL(url).openConnection() as HttpsURLConnection
Expand All @@ -18,7 +20,7 @@ fun downloadHTMLFileWithCredientials(url : String, username : String, password :

// 401 - Unauthorized e.g. wrong credentials
if (httpStatusCode == 401) {
throw SecurityException("Unauthorized HTTP request")
throw UnAuthorizedException()
}

if (httpStatusCode != 200) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected void onHandleIntent(Intent intent) {


reply.send(this, DownloadResultCodes.RESULT_SUCCESS.ordinal(), result);
} catch (SecurityException exc) {
} catch (UnAuthorizedException exc) {
// The given username/password are incorrect
StudentAccount.INSTANCE.setLoginValid(getApplicationContext(), false);
Intent data = new Intent();
Expand Down Expand Up @@ -124,11 +124,11 @@ private String readMotd(String html) {
}
}

protected String downloadHTMLFile(String url) throws SecurityException, IllegalStateException {
protected String downloadHTMLFile(String url) throws SecurityException, IllegalStateException, UnAuthorizedException {
return downloadHTMLFile(url, StudentAccount.getUserName(getApplicationContext()), StudentAccount.getPassword(getApplicationContext()));
}

public static String downloadHTMLFile(String url, String username, String password) throws SecurityException, IllegalStateException {
public static String downloadHTMLFile(String url, String username, String password) throws SecurityException, IllegalStateException, UnAuthorizedException {
return DownloadHTMLKt.downloadHTMLFileWithCredientials(url, username, password);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected void onHandleIntent(Intent intent) {

VertretungenWidgetProvider.updateWidgetData(this);
reply.send(this, DownloadResultCodes.RESULT_SUCCESS.ordinal(), result);
} catch (SecurityException exc) {
} catch (UnAuthorizedException exc) {
// The given username/password are incorrect
StudentAccount.INSTANCE.setLoginValid(getApplicationContext(), false);
Intent data = new Intent();
Expand All @@ -120,7 +120,7 @@ protected String downloadHTMLFile(String url) throws Exception {
return downloadHTMLFile(url, TeacherAccount.getUserName(getApplicationContext()), TeacherAccount.getPassword(getApplicationContext()));
}

public static String downloadHTMLFile(String url, String username, String password) throws IllegalStateException, SecurityException {
public static String downloadHTMLFile(String url, String username, String password) throws IllegalStateException, SecurityException, UnAuthorizedException {
return DownloadHTMLKt.downloadHTMLFileWithCredientials(url, username, password);
}

Expand Down
13 changes: 11 additions & 2 deletions app/src/main/res/layout/activity_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,19 @@


<TextView
android:text="@string/login_info"
android:id="@+id/activity_login_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
android:layout_alignParentBottom="true"
android:text="@string/login_info"
android:textStyle="italic" />

<TextView
android:id="@+id/activity_login_text_version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/activity_login_info"
android:layout_marginBottom="5dp" />


</RelativeLayout>
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
<string name="no_data_downloaded">Keine Daten heruntergeladen</string>

<!-- Connection Info Texts -->
<string name="io_error">Verbindungsfehler \nBitte überprüfen Sie Ihre Internetverbindung</string>
<string name="activity_login_generic_error_template">Etwas ist fehlgeschlagen.\n%s</string>
<string name="io_error">Etwas ist fehlgeschlagen. Prüfen Sie Ihre Internetverbindung oder versuchen Sie sich erneut anzumelden.</string>
<string name="authentication_error">Anmeldedaten fehlerhaft.</string>
<string name="authentication_error_reregister">Neu anmelden</string>
<string name="download_success">Daten erfolgreich heruntergeladen</string>
Expand Down

0 comments on commit 13f6c35

Please sign in to comment.