1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-27 20:36:51 +01:00

FIX: Fix find-my-phone for android 10 with notifications.

This commit is contained in:
Pauli Salmenrinne 2020-03-03 19:01:39 +02:00 committed by Gitea
parent 06bea85260
commit 8cfdd62eb4
5 changed files with 55 additions and 4 deletions

View File

@ -90,7 +90,7 @@ import static nodomain.freeyourgadget.gadgetbridge.model.DeviceType.MIBAND4;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceType.ZETIME;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceType.fromKey;
import static nodomain.freeyourgadget.gadgetbridge.util.GB.NOTIFICATION_CHANNEL_ID;
import static nodomain.freeyourgadget.gadgetbridge.util.GB.NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID;
/**
* Main Application class that initializes and provides access to certain things like
* logging and DB access.
@ -211,6 +211,14 @@ public class GBApplication extends Application {
notificationManager.createNotificationChannel(channel);
}
NotificationChannel channelHighPr = notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID );
if (channelHighPr == null) {
channelHighPr = new NotificationChannel(NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID,
getString(R.string.notification_channel_high_priority_name),
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channelHighPr);
}
bluetoothStateChangeReceiver = new BluetoothStateChangeReceiver();
registerReceiver(bluetoothStateChangeReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
}

View File

@ -17,6 +17,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@ -39,6 +40,7 @@ import java.io.IOException;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
public class FindPhoneActivity extends AbstractGBActivity {
@ -84,6 +86,9 @@ public class FindPhoneActivity extends AbstractGBActivity {
}
});
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel( GB.NOTIFICATION_ID_PHONE_FIND );
vibrate();
playRingtone();
}

View File

@ -26,6 +26,7 @@ import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.telephony.SmsManager;
import org.slf4j.Logger;
@ -67,6 +68,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.receivers.GBMusicControlRece
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import static nodomain.freeyourgadget.gadgetbridge.util.GB.NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID;
import static nodomain.freeyourgadget.gadgetbridge.util.GB.NOTIFICATION_CHANNEL_ID;
// TODO: support option for a single reminder notification when notifications could not be delivered?
@ -86,6 +88,8 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
private Context context;
private boolean autoReconnect;
@Override
public void setContext(GBDevice gbDevice, BluetoothAdapter btAdapter, Context context) {
this.gbDevice = gbDevice;
@ -170,9 +174,7 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
LOG.info("Got GBDeviceEventFindPhone");
switch (deviceEvent.event) {
case START:
Intent startIntent = new Intent(getContext(), FindPhoneActivity.class);
startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startIntent);
handleGBDeviceEventFindPhoneStart();
break;
case STOP:
Intent intent = new Intent(FindPhoneActivity.ACTION_FOUND);
@ -183,6 +185,38 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
}
}
private void handleGBDeviceEventFindPhoneStart() {
if ( Build.VERSION.SDK_INT < 29 ) { // this could be used if app in foreground
Intent startIntent = new Intent(getContext(), FindPhoneActivity.class);
startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startIntent);
}
else {
handleGBDeviceEventFindPhoneStartNotification();
}
}
private void handleGBDeviceEventFindPhoneStartNotification() {
LOG.info("Got handleGBDeviceEventFindPhoneStartNotification");
Intent intent = new Intent(context, FindPhoneActivity.class);
PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID );
notification
.setSmallIcon(R.drawable.ic_notification)
.setOngoing(false)
.setFullScreenIntent(pi, true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true)
.setContentTitle( context.getString( R.string.find_my_phone_notification ) );
notification.setGroup("BackgroundService");
notificationManager.notify( GB.NOTIFICATION_ID_PHONE_FIND, notification.build());
}
private void handleGBDeviceEvent(GBDeviceEventMusicControl musicEvent) {
Context context = getContext();
LOG.info("Got event for MUSIC_CONTROL");

View File

@ -59,6 +59,7 @@ import static nodomain.freeyourgadget.gadgetbridge.GBApplication.isRunningOreoOr
public class GB {
public static final String NOTIFICATION_CHANNEL_ID = "gadgetbridge";
public static final String NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID = "gadgetbridge_high_priority";
public static final String NOTIFICATION_CHANNEL_ID_TRANSFER = "gadgetbridge transfer";
public static final int NOTIFICATION_ID = 1;
@ -66,6 +67,7 @@ public class GB {
public static final int NOTIFICATION_ID_LOW_BATTERY = 3;
public static final int NOTIFICATION_ID_TRANSFER = 4;
public static final int NOTIFICATION_ID_EXPORT_FAILED = 5;
public static final int NOTIFICATION_ID_PHONE_FIND = 6;
private static final Logger LOG = LoggerFactory.getLogger(GB.class);
public static final int INFO = 1;

View File

@ -267,6 +267,7 @@
<string name="test">Test</string>
<string name="test_notification">Test notification</string>
<string name="this_is_a_test_notification_from_gadgetbridge">This is a test notification from Gadgetbridge</string>
<string name="find_my_phone_notification">Find my phone</string>
<string name="bluetooth_is_not_supported_">Bluetooth is not supported.</string>
<string name="bluetooth_is_disabled_">Bluetooth is disabled.</string>
<string name="tap_connected_device_for_app_mananger">Tap connected device for App manager</string>
@ -700,6 +701,7 @@
<string name="devicetype_banglejs">Bangle.js</string>
<string name="choose_auto_export_location">Choose export location</string>
<string name="notification_channel_name">Gadgetbridge notifications</string>
<string name="notification_channel_high_priority_name">Gadgetbridge notifications high priority</string>
<string name="devicetype_amazfit_gts">Amazfit GTS</string>
<!-- Menus on the smart device -->
<string name="menuitem_shortcut_alipay">Alipay (Shortcut)</string>