2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-31 06:05:12 +00:00

refactor: migrate KdeConnectBroadcastReceiver to Kotlin

This commit is contained in:
ShellWen Chen
2024-03-28 03:32:04 +08:00
parent e5eb61fd54
commit 0a2080fdcd
2 changed files with 50 additions and 60 deletions

View File

@@ -1,60 +0,0 @@
/*
* SPDX-FileCopyrightText: 2014 Albert Vaca Cintora <albertvaka@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
package org.kde.kdeconnect;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.util.Log;
public class KdeConnectBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
//Log.e("KdeConnect", "Broadcast event: "+intent.getAction());
String action = intent.getAction();
switch (action) {
case Intent.ACTION_MY_PACKAGE_REPLACED:
Log.i("KdeConnect", "MyUpdateReceiver");
BackgroundService.Start(context);
break;
case Intent.ACTION_BOOT_COMPLETED:
Log.i("KdeConnect", "KdeConnectBroadcastReceiver");
try {
BackgroundService.Start(context);
} catch (IllegalStateException e) { // To catch ForegroundServiceStartNotAllowedException
Log.w("BroadcastReceiver", "Couldn't start the foreground service.");
e.printStackTrace();
}
break;
case WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION:
case WifiManager.WIFI_STATE_CHANGED_ACTION:
case ConnectivityManager.CONNECTIVITY_ACTION:
Log.i("KdeConnect", "Connection state changed, trying to connect");
BackgroundService.ForceRefreshConnections(context);
break;
case Intent.ACTION_SCREEN_ON:
try {
BackgroundService.ForceRefreshConnections(context);
} catch (IllegalStateException e) { // To catch ForegroundServiceStartNotAllowedException
Log.w("BroadcastReceiver", "Couldn't start the foreground service.");
e.printStackTrace();
}
break;
default:
Log.i("BroadcastReceiver", "Ignoring broadcast event: " + intent.getAction());
break;
}
}
}

View File

@@ -0,0 +1,50 @@
/*
* SPDX-FileCopyrightText: 2014 Albert Vaca Cintora <albertvaka@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
package org.kde.kdeconnect
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.net.wifi.WifiManager
import android.util.Log
class KdeConnectBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
// Log.e("KdeConnect", "Broadcast event: "+intent.getAction());
val action = intent.action
when (action) {
Intent.ACTION_MY_PACKAGE_REPLACED -> {
Log.i("KdeConnect", "MyUpdateReceiver")
BackgroundService.Start(context)
}
Intent.ACTION_BOOT_COMPLETED -> {
Log.i("KdeConnect", "KdeConnectBroadcastReceiver")
try {
BackgroundService.Start(context)
} catch (e: IllegalStateException) { // To catch ForegroundServiceStartNotAllowedException
Log.w("BroadcastReceiver", "Couldn't start the foreground service.", e)
}
}
WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION, WifiManager.WIFI_STATE_CHANGED_ACTION, ConnectivityManager.CONNECTIVITY_ACTION -> {
Log.i("KdeConnect", "Connection state changed, trying to connect")
BackgroundService.ForceRefreshConnections(context)
}
Intent.ACTION_SCREEN_ON -> try {
BackgroundService.ForceRefreshConnections(context)
} catch (e: IllegalStateException) { // To catch ForegroundServiceStartNotAllowedException
Log.w("BroadcastReceiver", "Couldn't start the foreground service.", e)
}
else -> Log.i("BroadcastReceiver", "Ignoring broadcast event: ${intent.action}")
}
}
}