Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: reconnecting with wrong url/credentials in the background after logout #401

Merged
merged 2 commits into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.gotify.service

import android.app.AlarmManager
import android.app.AlarmManager.OnAlarmListener
import android.os.Build
import android.os.Handler
import android.os.Looper
Expand Down Expand Up @@ -29,9 +30,10 @@ internal class WebSocketConnection(
private val ID = AtomicLong(0)
}

private var alarmManagerCallback: OnAlarmListener? = null
private var handlerCallback: Runnable? = null
private val client: OkHttpClient
private val reconnectHandler = Handler(Looper.getMainLooper())
private val reconnectCallback = Runnable { start() }
private var errorCount = 0

private var webSocket: WebSocket? = null
Expand Down Expand Up @@ -106,6 +108,13 @@ internal class WebSocketConnection(

@Synchronized
fun close() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
alarmManagerCallback?.run(alarmManager::cancel)
alarmManagerCallback = null
} else {
handlerCallback?.run(reconnectHandler::removeCallbacks)
handlerCallback = null
}
if (webSocket != null) {
webSocket?.close(1000, "")
closed()
Expand All @@ -119,8 +128,10 @@ internal class WebSocketConnection(
state = State.Disconnected
}

fun scheduleReconnectNow(seconds: Long) = scheduleReconnect(ID.get(), seconds)

@Synchronized
fun scheduleReconnect(seconds: Long) {
fun scheduleReconnect(id: Long, seconds: Long) {
if (state == State.Connecting || state == State.Connected) {
return
}
Expand All @@ -130,17 +141,23 @@ internal class WebSocketConnection(
Logger.info("WebSocket: scheduling a restart in $seconds second(s) (via alarm manager)")
val future = Calendar.getInstance()
future.add(Calendar.SECOND, seconds.toInt())

alarmManagerCallback?.run(alarmManager::cancel)
val cb = OnAlarmListener { syncExec(id) { start() } }
alarmManagerCallback = cb
alarmManager.setExact(
AlarmManager.RTC_WAKEUP,
future.timeInMillis,
"reconnect-tag",
{ start() },
cb,
null
)
} else {
Logger.info("WebSocket: scheduling a restart in $seconds second(s)")
reconnectHandler.removeCallbacks(reconnectCallback)
reconnectHandler.postDelayed(reconnectCallback, TimeUnit.SECONDS.toMillis(seconds))
handlerCallback?.run(reconnectHandler::removeCallbacks)
val cb = Runnable { syncExec(id) { start() } }
handlerCallback = cb
reconnectHandler.postDelayed(cb, TimeUnit.SECONDS.toMillis(seconds))
}
}

Expand Down Expand Up @@ -190,7 +207,7 @@ internal class WebSocketConnection(
val minutes = (errorCount * 2 - 1).coerceAtMost(20)

onFailure.execute(response?.message ?: "unreachable", minutes)
scheduleReconnect(TimeUnit.MINUTES.toSeconds(minutes.toLong()))
scheduleReconnect(id, TimeUnit.MINUTES.toSeconds(minutes.toLong()))
}
super.onFailure(webSocket, t, response)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,7 @@ internal class WebSocketService : Service() {
}

private fun doReconnect() {
if (connection == null) {
return
}
connection!!.scheduleReconnect(15)
connection?.scheduleReconnectNow(15)
}

private fun onFailure(status: String, minutes: Int) {
Expand Down
263 changes: 139 additions & 124 deletions app/src/main/res/layout/activity_share.xml
Original file line number Diff line number Diff line change
@@ -1,137 +1,152 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
android:fitsSystemWindows="true"
tools:openDrawer="start">

<LinearLayout
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_height="match_parent">

<include
android:id="@+id/app_bar_drawer"
layout="@layout/app_bar_drawer"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp">
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/title"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/push_title_hint"
android:inputType="text"
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginBottom="20dp">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/push_content_hint"
android:inputType="textCapSentences|textMultiLine"
android:maxLength="2000"
android:maxLines="10" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginBottom="20dp">
android:orientation="vertical">

<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/push_title_hint"
android:inputType="text"
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginBottom="20dp">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/push_content_hint"
android:inputType="textCapSentences|textMultiLine"
android:maxLength="2000"
android:maxLines="10" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginBottom="20dp">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edtTxtPriority"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="0123456789"
android:hint="@string/push_priority_hint"
android:imeOptions="actionDone"
android:inputType="numberSigned"
android:maxLength="3"
android:text="0" />
</com.google.android.material.textfield.TextInputLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginBottom="20dp">

<com.google.android.material.textview.MaterialTextView
android:id="@+id/txtAppListDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/appListDescription" />

<Spinner
android:id="@+id/appSpinner"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:spinnerMode="dropdown"
android:textSize="18sp" />
</LinearLayout>

<LinearLayout
android:id="@+id/missingAppsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginBottom="20dp"
android:gravity="center"
android:orientation="horizontal"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:visibility="gone">

<ImageView
android:id="@+id/missingAppsIcon"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_weight="0"
app:srcCompat="@drawable/ic_info" />

<Space
android:layout_width="10dp"
android:layout_height="match_parent"
android:layout_weight="0" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/missingAppsText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0"
android:text="@string/push_missing_app_info"
android:textAlignment="center"
android:visibility="visible" />
</LinearLayout>

<com.google.android.material.button.MaterialButton
android:id="@+id/push_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginHorizontal="20dp"
android:layout_marginBottom="20dp"
android:text="@string/push_button"
android:textColor="@android:color/white" />

</LinearLayout>


</androidx.core.widget.NestedScrollView>

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edtTxtPriority"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="0123456789"
android:hint="@string/push_priority_hint"
android:imeOptions="actionDone"
android:inputType="numberSigned"
android:maxLength="3"
android:text="0" />
</com.google.android.material.textfield.TextInputLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginBottom="20dp">

<com.google.android.material.textview.MaterialTextView
android:id="@+id/txtAppListDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/appListDescription" />

<Spinner
android:id="@+id/appSpinner"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:spinnerMode="dropdown"
android:textSize="18sp" />
</LinearLayout>

<LinearLayout
android:id="@+id/missingAppsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginBottom="20dp"
android:gravity="center"
android:orientation="horizontal"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:visibility="gone">

<ImageView
android:id="@+id/missingAppsIcon"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_weight="0"
app:srcCompat="@drawable/ic_info" />

<Space
android:layout_width="10dp"
android:layout_height="match_parent"
android:layout_weight="0" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/missingAppsText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0"
android:text="@string/push_missing_app_info"
android:textAlignment="center"
android:visibility="visible" />
</LinearLayout>

<com.google.android.material.button.MaterialButton
android:id="@+id/push_button"
<include
android:id="@+id/app_bar_drawer"
layout="@layout/app_bar_drawer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginHorizontal="20dp"
android:layout_marginBottom="20dp"
android:textColor="@android:color/white"
android:text="@string/push_button" />

</LinearLayout>
</ScrollView>
android:layout_height="wrap_content" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.drawerlayout.widget.DrawerLayout>