Search in sources :

Example 1 with CompanionDeviceManager

use of android.companion.CompanionDeviceManager in project robolectric by robolectric.

the class ShadowCompanionDeviceManagerTest method setUp.

@Before
public void setUp() throws Exception {
    companionDeviceManager = getApplicationContext().getSystemService(CompanionDeviceManager.class);
    shadowCompanionDeviceManager = shadowOf(companionDeviceManager);
    componentName = new ComponentName(getApplicationContext(), Application.class);
}
Also used : CompanionDeviceManager(android.companion.CompanionDeviceManager) ComponentName(android.content.ComponentName) Application(android.app.Application) Before(org.junit.Before)

Example 2 with CompanionDeviceManager

use of android.companion.CompanionDeviceManager in project Gadgetbridge by Freeyourgadget.

the class BondingUtil method companionDeviceManagerBond.

/**
 * Uses the CompanionDeviceManager bonding method
 */
@RequiresApi(Build.VERSION_CODES.O)
private static void companionDeviceManagerBond(BondingInterface bondingInterface, final GBDeviceCandidate deviceCandidate) {
    BluetoothDeviceFilter deviceFilter = new BluetoothDeviceFilter.Builder().setAddress(deviceCandidate.getMacAddress()).build();
    AssociationRequest pairingRequest = new AssociationRequest.Builder().addDeviceFilter(deviceFilter).setSingleDevice(true).build();
    CompanionDeviceManager manager = (CompanionDeviceManager) bondingInterface.getContext().getSystemService(Context.COMPANION_DEVICE_SERVICE);
    LOG.debug(String.format("Searching for %s associations", deviceCandidate.getMacAddress()));
    for (String association : manager.getAssociations()) {
        LOG.debug(String.format("Already associated with: %s", association));
        if (association.equals(deviceCandidate.getMacAddress())) {
            LOG.info("The device has already been bonded through CompanionDeviceManager, using regular");
            // If it's already "associated", we should immediately pair
            // because the callback is never called (AFAIK?)
            BondingUtil.bluetoothBond(bondingInterface, deviceCandidate);
            return;
        }
    }
    LOG.debug("Starting association request");
    manager.associate(pairingRequest, getCompanionDeviceManagerCallback(bondingInterface), null);
}
Also used : CompanionDeviceManager(android.companion.CompanionDeviceManager) AssociationRequest(android.companion.AssociationRequest) BluetoothDeviceFilter(android.companion.BluetoothDeviceFilter) RequiresApi(androidx.annotation.RequiresApi)

Example 3 with CompanionDeviceManager

use of android.companion.CompanionDeviceManager in project Gadgetbridge by Freeyourgadget.

the class AbstractDeviceSupport method handleGBDeviceEventFindPhoneStartNotification.

@RequiresApi(Build.VERSION_CODES.Q)
private void handleGBDeviceEventFindPhoneStartNotification() {
    LOG.info("Got handleGBDeviceEventFindPhoneStartNotification");
    Intent intent = new Intent(context, FindPhoneActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder notification = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID).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");
    CompanionDeviceManager manager = (CompanionDeviceManager) context.getSystemService(Context.COMPANION_DEVICE_SERVICE);
    if (manager.getAssociations().size() > 0) {
        GB.notify(GB.NOTIFICATION_ID_PHONE_FIND, notification.build(), context);
        context.startActivity(intent);
        LOG.debug("CompanionDeviceManager associations were found, starting intent");
    } else {
        GB.notify(GB.NOTIFICATION_ID_PHONE_FIND, notification.build(), context);
        LOG.warn("CompanionDeviceManager associations were not found, can't start intent");
    }
}
Also used : CompanionDeviceManager(android.companion.CompanionDeviceManager) NotificationCompat(androidx.core.app.NotificationCompat) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) RequiresApi(androidx.annotation.RequiresApi)

Aggregations

CompanionDeviceManager (android.companion.CompanionDeviceManager)3 RequiresApi (androidx.annotation.RequiresApi)2 Application (android.app.Application)1 PendingIntent (android.app.PendingIntent)1 AssociationRequest (android.companion.AssociationRequest)1 BluetoothDeviceFilter (android.companion.BluetoothDeviceFilter)1 ComponentName (android.content.ComponentName)1 Intent (android.content.Intent)1 NotificationCompat (androidx.core.app.NotificationCompat)1 Before (org.junit.Before)1