From 678be49bff9a121999d7c5e77473679d4b887459 Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Sun, 6 Feb 2022 15:51:30 -0500 Subject: [PATCH] Fix scrolling issue in subscribe dialog; fix base URL background color; fix dark mode action bar --- app/build.gradle | 2 +- .../java/io/heckel/ntfy/msg/ApiService.kt | 13 +- .../java/io/heckel/ntfy/ui/AddFragment.kt | 19 ++- .../main/res/layout/fragment_add_dialog.xml | 116 +++++++++--------- app/src/main/res/values/strings.xml | 9 +- app/src/main/res/values/styles.xml | 6 - .../metadata/android/en-US/changelog/21.txt | 16 +++ 7 files changed, 104 insertions(+), 77 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 4cc6909..a681428 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -13,7 +13,7 @@ android { targetSdkVersion 31 versionCode 21 - versionName "1.9.0" + versionName "1.8.1" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" diff --git a/app/src/main/java/io/heckel/ntfy/msg/ApiService.kt b/app/src/main/java/io/heckel/ntfy/msg/ApiService.kt index a4219ca..cf26aac 100644 --- a/app/src/main/java/io/heckel/ntfy/msg/ApiService.kt +++ b/app/src/main/java/io/heckel/ntfy/msg/ApiService.kt @@ -118,7 +118,7 @@ class ApiService { return call } - fun authTopicRead(baseUrl: String, topic: String, user: User?): Boolean { + fun checkAuth(baseUrl: String, topic: String, user: User?): Boolean { if (user == null) { Log.d(TAG, "Checking anonymous read against ${topicUrl(baseUrl, topic)}") } else { @@ -127,11 +127,14 @@ class ApiService { val url = topicUrlAuth(baseUrl, topic) val request = requestBuilder(url, user).build() client.newCall(request).execute().use { response -> - return if (user == null) { - response.isSuccessful || response.code == 404 // Treat 404 as success (old server; to be removed in future versions) - } else { - response.isSuccessful + if (response.isSuccessful) { + return true + } else if (user == null && response.code == 404) { + return true // Special case: Anonymous login to old servers return 404 since //auth doesn't exist + } else if (response.code == 401 || response.code == 403) { // See server/server.go + return false } + throw Exception("Unexpected server response ${response.code}") } } diff --git a/app/src/main/java/io/heckel/ntfy/ui/AddFragment.kt b/app/src/main/java/io/heckel/ntfy/ui/AddFragment.kt index 4a3c72a..c4246f5 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/AddFragment.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/AddFragment.kt @@ -7,6 +7,7 @@ import android.content.Context import android.os.Bundle import android.text.Editable import android.text.TextWatcher +import android.util.TypedValue import android.view.View import android.view.WindowManager import android.view.inputmethod.InputMethodManager @@ -88,7 +89,9 @@ class AddFragment : DialogFragment() { // Fields for "subscribe page" subscribeTopicText = view.findViewById(R.id.add_dialog_subscribe_topic_text) subscribeBaseUrlLayout = view.findViewById(R.id.add_dialog_subscribe_base_url_layout) + subscribeBaseUrlLayout.background = view.background subscribeBaseUrlText = view.findViewById(R.id.add_dialog_subscribe_base_url_text) + subscribeBaseUrlText.background = view.background subscribeInstantDeliveryBox = view.findViewById(R.id.add_dialog_subscribe_instant_delivery_box) subscribeInstantDeliveryCheckbox = view.findViewById(R.id.add_dialog_subscribe_instant_delivery_checkbox) subscribeInstantDeliveryDescription = view.findViewById(R.id.add_dialog_subscribe_instant_delivery_description) @@ -100,6 +103,14 @@ class AddFragment : DialogFragment() { subscribeErrorTextImage = view.findViewById(R.id.add_dialog_subscribe_error_text_image) subscribeErrorTextImage.visibility = View.GONE + // Hack: Make end icon smaller, see https://stackoverflow.com/a/57098715/1440785 + val dimension = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30f, resources.displayMetrics) + val endIconImageView = subscribeBaseUrlLayout.findViewById(R.id.text_input_end_icon) + endIconImageView.minimumHeight = dimension.toInt() + endIconImageView.minimumWidth = dimension.toInt() + subscribeBaseUrlLayout.requestLayout() + + // Fields for "login page" loginUsernameText = view.findViewById(R.id.add_dialog_login_username) loginPasswordText = view.findViewById(R.id.add_dialog_login_password) @@ -280,14 +291,14 @@ class AddFragment : DialogFragment() { lifecycleScope.launch(Dispatchers.IO) { try { val user = repository.getUser(baseUrl) // May be null - val authorized = api.authTopicRead(baseUrl, topic, user) + val authorized = api.checkAuth(baseUrl, topic, user) if (authorized) { Log.d(TAG, "Access granted to topic ${topicUrl(baseUrl, topic)}") dismissDialog() } else { if (user != null) { Log.w(TAG, "Access not allowed to topic ${topicUrl(baseUrl, topic)}, but user already exists") - showErrorAndReenableSubscribeView(getString(R.string.add_dialog_login_error_not_authorized)) + showErrorAndReenableSubscribeView(getString(R.string.add_dialog_login_error_not_authorized, user.username)) } else { Log.w(TAG, "Access not allowed to topic ${topicUrl(baseUrl, topic)}, showing login dialog") val activity = activity ?: return@launch // We may have pressed "Cancel" @@ -327,14 +338,14 @@ class AddFragment : DialogFragment() { lifecycleScope.launch(Dispatchers.IO) { Log.d(TAG, "Checking read access for user ${user.username} to topic ${topicUrl(baseUrl, topic)}") try { - val authorized = api.authTopicRead(baseUrl, topic, user) + val authorized = api.checkAuth(baseUrl, topic, user) if (authorized) { Log.d(TAG, "Access granted for user ${user.username} to topic ${topicUrl(baseUrl, topic)}, adding to database") repository.addUser(user) dismissDialog() } else { Log.w(TAG, "Access not allowed for user ${user.username} to topic ${topicUrl(baseUrl, topic)}") - showErrorAndReenableLoginView(getString(R.string.add_dialog_login_error_not_authorized)) + showErrorAndReenableLoginView(getString(R.string.add_dialog_login_error_not_authorized, user.username)) } } catch (e: Exception) { Log.w(TAG, "Connection to topic failed during login: ${e.message}", e) diff --git a/app/src/main/res/layout/fragment_add_dialog.xml b/app/src/main/res/layout/fragment_add_dialog.xml index 6e8fbf7..29da8cd 100644 --- a/app/src/main/res/layout/fragment_add_dialog.xml +++ b/app/src/main/res/layout/fragment_add_dialog.xml @@ -6,13 +6,14 @@ android:orientation="vertical" android:paddingLeft="16dp" android:paddingRight="16dp"> - - + android:id="@+id/add_dialog_subscribe_view"> + - - - + - + android:layout_margin="0dp" + android:padding="0dp" + android:visibility="gone" + app:endIconMode="custom" + app:hintEnabled="false" + app:boxBackgroundColor="@null" app:layout_constraintStart_toStartOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintTop_toBottomOf="@id/add_dialog_subscribe_use_another_server_description"> + + - + - + + android:id="@+id/add_dialog_login_view"> + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 84c8605..8118894 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -77,7 +77,7 @@ Subscribe to topic Topics may not be password-protected, so choose a name that\'s not easy to - guess. Once subscribed, you can PUT/POST to receive notifications. + guess. Once subscribed, you can PUT/POST notifications. Topic name, e.g. phils_alerts Use another server @@ -85,12 +85,11 @@ You can subscribe to topics from your own server. This option requires a foreground service. - You can subscribe to topics from your own server. Simply type in the base - URL of your server. + You can subscribe to topics from your own server. Type the server URL below. Instant delivery in doze mode - Ensures that messages are immediately delivered, even if the device is inactive or in doze mode. + Ensures that messages are immediately delivered, even if the device is inactive. This requires a foreground service. Cancel @@ -102,7 +101,7 @@ This topic requires you to login. Please type in a username and password. Username Password - Login failed. User not authorized. + Login failed. User %1$s not authorized. New user diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index e2ed459..1a3091e 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -3,16 +3,10 @@ - - -