Made the find phone activity start instantly when allowed

This commit is contained in:
TaaviE
2020-10-06 22:00:42 +03:00
parent ad30c528e5
commit 3c842bd441
@@ -21,6 +21,7 @@ import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.companion.CompanionDeviceManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
@@ -29,6 +30,7 @@ import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.telephony.SmsManager; import android.telephony.SmsManager;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.content.FileProvider; import androidx.core.content.FileProvider;
import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.localbroadcastmanager.content.LocalBroadcastManager;
@@ -195,25 +197,35 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
} }
} }
@RequiresApi(Build.VERSION_CODES.Q)
private void handleGBDeviceEventFindPhoneStartNotification() { private void handleGBDeviceEventFindPhoneStartNotification() {
LOG.info("Got handleGBDeviceEventFindPhoneStartNotification"); LOG.info("Got handleGBDeviceEventFindPhoneStartNotification");
Intent intent = new Intent(context, FindPhoneActivity.class); Intent intent = new Intent(context, FindPhoneActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID ); NotificationCompat.Builder notification = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID )
notification
.setSmallIcon(R.drawable.ic_notification) .setSmallIcon(R.drawable.ic_notification)
.setOngoing(false) .setOngoing(false)
.setFullScreenIntent(pi, true) .setFullScreenIntent(pi, true)
.setPriority(NotificationCompat.PRIORITY_HIGH) .setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true) .setAutoCancel(true)
.setContentTitle( context.getString( R.string.find_my_phone_notification ) ); .setContentTitle( context.getString( R.string.find_my_phone_notification ) );
notification.setGroup("BackgroundService"); notification.setGroup("BackgroundService");
notificationManager.notify(GB.NOTIFICATION_ID_PHONE_FIND, notification.build()); CompanionDeviceManager manager = (CompanionDeviceManager) context.getSystemService(Context.COMPANION_DEVICE_SERVICE);
if (manager.getAssociations().size() > 0) {
notificationManager.notify(GB.NOTIFICATION_ID_PHONE_FIND, notification.build());
context.startActivity(intent);
LOG.debug("CompanionDeviceManager associations were found, starting intent");
} else {
notificationManager.notify(GB.NOTIFICATION_ID_PHONE_FIND, notification.build());
LOG.warn("CompanionDeviceManager associations were not found, can't start intent");
}
} }