diff --git a/res/values/strings.xml b/res/values/strings.xml --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -1,6 +1,9 @@ + KDE Connect + Not connected to any device + Connected to: %s Telephony notifier Send notifications for SMS and calls Battery report diff --git a/src/org/kde/kdeconnect/BackgroundService.java b/src/org/kde/kdeconnect/BackgroundService.java --- a/src/org/kde/kdeconnect/BackgroundService.java +++ b/src/org/kde/kdeconnect/BackgroundService.java @@ -20,22 +20,29 @@ package org.kde.kdeconnect; +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.os.Binder; import android.os.Build; import android.os.IBinder; +import android.support.v4.app.NotificationCompat; +import android.text.TextUtils; import android.util.Log; import org.kde.kdeconnect.Backends.BaseLink; import org.kde.kdeconnect.Backends.BaseLinkProvider; //import org.kde.kdeconnect.Backends.BluetoothBackend.BluetoothLinkProvider; import org.kde.kdeconnect.Backends.LanBackend.LanLinkProvider; import org.kde.kdeconnect.Helpers.SecurityHelpers.RsaHelper; import org.kde.kdeconnect.Helpers.SecurityHelpers.SslHelper; +import org.kde.kdeconnect.UserInterface.MaterialActivity; +import org.kde.kdeconnect_tp.R; import java.util.ArrayList; import java.util.HashSet; @@ -45,6 +52,7 @@ import java.util.concurrent.locks.ReentrantLock; public class BackgroundService extends Service { + public static final int FOREGROUND_NOTIFICATION_ID = 1; private static BackgroundService instance; @@ -134,6 +142,10 @@ for (DeviceListChangedCallback callback : deviceListChangedCallbacks.values()) { callback.onDeviceListChanged(); } + + //Update the foreground notification with the currently connected device list + NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + nm.notify(FOREGROUND_NOTIFICATION_ID, createForegroundNotification()); } private void loadRememberedDevicesFromSettings() { @@ -279,13 +291,44 @@ } } + private Notification createForegroundNotification() { + Intent intent = new Intent(this, MaterialActivity.class); + PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); + NotificationCompat.Builder notification = new NotificationCompat.Builder(this); + notification + .setSmallIcon(R.drawable.ic_notification) + .setColor(getResources().getColor(R.color.primary)) + .setContentTitle(getString(R.string.kde_connect)) + .setOngoing(true) + .setContentIntent(pi) + .setPriority(NotificationCompat.PRIORITY_MIN) + .setShowWhen(false) + .setAutoCancel(false); + + ArrayList connectedDevices = new ArrayList<>(); + for (Device device : getDevices().values()) { + if (device.isReachable() && device.isPaired()) { + connectedDevices.add(device.getName()); + } + } + + if (connectedDevices.isEmpty()) { + notification.setContentText(getString(R.string.foreground_notification_no_devices)); + } else { + notification.setContentText(getString(R.string.foreground_notification_devices, TextUtils.join(", ", connectedDevices))); + } + + return notification.build(); + } + void initializeSecurityParameters() { RsaHelper.initialiseRsaKeys(this); SslHelper.initialiseCertificate(this); } @Override public void onDestroy() { + stopForeground(true); for (BaseLinkProvider a : linkProviders) { a.onStop(); } @@ -320,6 +363,8 @@ } finally { mutex.unlock(); } + + startForeground(FOREGROUND_NOTIFICATION_ID, createForegroundNotification()); return Service.START_STICKY; }