Search in sources :

Example 1 with RequiresPermission

use of android.annotation.RequiresPermission in project platform_frameworks_base by android.

the class AccountManager method addOnAccountsUpdatedListener.

/**
     * Adds an {@link OnAccountsUpdateListener} to this instance of the
     * {@link AccountManager}.  This listener will be notified whenever the
     * list of accounts on the device changes.
     *
     * <p>As long as this listener is present, the AccountManager instance
     * will not be garbage-collected, and neither will the {@link Context}
     * used to retrieve it, which may be a large Activity instance.  To avoid
     * memory leaks, you must remove this listener before then.  Normally
     * listeners are added in an Activity or Service's {@link Activity#onCreate}
     * and removed in {@link Activity#onDestroy}.
     *
     * <p>The listener will only be informed of accounts that would be returned
     * to the caller via {@link #getAccounts()}. Typically this means that to
     * get any accounts, the caller will need to be grated the GET_ACCOUNTS
     * permission.
     *
     * <p>It is safe to call this method from the main thread.
     *
     * @param listener The listener to send notifications to
     * @param handler {@link Handler} identifying the thread to use
     *     for notifications, null for the main thread
     * @param updateImmediately If true, the listener will be invoked
     *     (on the handler thread) right away with the current account list
     * @throws IllegalArgumentException if listener is null
     * @throws IllegalStateException if listener was already added
     */
@RequiresPermission(GET_ACCOUNTS)
public void addOnAccountsUpdatedListener(final OnAccountsUpdateListener listener, Handler handler, boolean updateImmediately) {
    if (listener == null) {
        throw new IllegalArgumentException("the listener is null");
    }
    synchronized (mAccountsUpdatedListeners) {
        if (mAccountsUpdatedListeners.containsKey(listener)) {
            throw new IllegalStateException("this listener is already added");
        }
        final boolean wasEmpty = mAccountsUpdatedListeners.isEmpty();
        mAccountsUpdatedListeners.put(listener, handler);
        if (wasEmpty) {
            // Register a broadcast receiver to monitor account changes
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(LOGIN_ACCOUNTS_CHANGED_ACTION);
            // To recover from disk-full.
            intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_OK);
            mContext.registerReceiver(mAccountsChangedBroadcastReceiver, intentFilter);
        }
    }
    if (updateImmediately) {
        postToHandler(handler, listener, getAccounts());
    }
}
Also used : IntentFilter(android.content.IntentFilter) RequiresPermission(android.annotation.RequiresPermission)

Example 2 with RequiresPermission

use of android.annotation.RequiresPermission in project android_frameworks_base by DirtyUnicorns.

the class AccountManager method addOnAccountsUpdatedListener.

/**
     * Adds an {@link OnAccountsUpdateListener} to this instance of the
     * {@link AccountManager}.  This listener will be notified whenever the
     * list of accounts on the device changes.
     *
     * <p>As long as this listener is present, the AccountManager instance
     * will not be garbage-collected, and neither will the {@link Context}
     * used to retrieve it, which may be a large Activity instance.  To avoid
     * memory leaks, you must remove this listener before then.  Normally
     * listeners are added in an Activity or Service's {@link Activity#onCreate}
     * and removed in {@link Activity#onDestroy}.
     *
     * <p>The listener will only be informed of accounts that would be returned
     * to the caller via {@link #getAccounts()}. Typically this means that to
     * get any accounts, the caller will need to be grated the GET_ACCOUNTS
     * permission.
     *
     * <p>It is safe to call this method from the main thread.
     *
     * @param listener The listener to send notifications to
     * @param handler {@link Handler} identifying the thread to use
     *     for notifications, null for the main thread
     * @param updateImmediately If true, the listener will be invoked
     *     (on the handler thread) right away with the current account list
     * @throws IllegalArgumentException if listener is null
     * @throws IllegalStateException if listener was already added
     */
@RequiresPermission(GET_ACCOUNTS)
public void addOnAccountsUpdatedListener(final OnAccountsUpdateListener listener, Handler handler, boolean updateImmediately) {
    if (listener == null) {
        throw new IllegalArgumentException("the listener is null");
    }
    synchronized (mAccountsUpdatedListeners) {
        if (mAccountsUpdatedListeners.containsKey(listener)) {
            throw new IllegalStateException("this listener is already added");
        }
        final boolean wasEmpty = mAccountsUpdatedListeners.isEmpty();
        mAccountsUpdatedListeners.put(listener, handler);
        if (wasEmpty) {
            // Register a broadcast receiver to monitor account changes
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(LOGIN_ACCOUNTS_CHANGED_ACTION);
            // To recover from disk-full.
            intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_OK);
            mContext.registerReceiver(mAccountsChangedBroadcastReceiver, intentFilter);
        }
    }
    if (updateImmediately) {
        postToHandler(handler, listener, getAccounts());
    }
}
Also used : IntentFilter(android.content.IntentFilter) RequiresPermission(android.annotation.RequiresPermission)

Example 3 with RequiresPermission

use of android.annotation.RequiresPermission in project android_frameworks_base by DirtyUnicorns.

the class BluetoothAdapter method startLeScan.

/**
     * Starts a scan for Bluetooth LE devices, looking for devices that
     * advertise given services.
     *
     * <p>Devices which advertise all specified services are reported using the
     * {@link LeScanCallback#onLeScan} callback.
     *
     * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.
     *
     * @param serviceUuids Array of services to look for
     * @param callback the callback LE scan results are delivered
     * @return true, if the scan was started successfully
     * @deprecated use {@link BluetoothLeScanner#startScan(List, ScanSettings, ScanCallback)}
     *             instead.
     */
@Deprecated
@RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
public boolean startLeScan(final UUID[] serviceUuids, final LeScanCallback callback) {
    if (DBG)
        Log.d(TAG, "startLeScan(): " + Arrays.toString(serviceUuids));
    if (callback == null) {
        if (DBG)
            Log.e(TAG, "startLeScan: null callback");
        return false;
    }
    BluetoothLeScanner scanner = getBluetoothLeScanner();
    if (scanner == null) {
        if (DBG)
            Log.e(TAG, "startLeScan: cannot get BluetoothLeScanner");
        return false;
    }
    synchronized (mLeScanClients) {
        if (mLeScanClients.containsKey(callback)) {
            if (DBG)
                Log.e(TAG, "LE Scan has already started");
            return false;
        }
        try {
            IBluetoothGatt iGatt = mManagerService.getBluetoothGatt();
            if (iGatt == null) {
                // BLE is not supported
                return false;
            }
            ScanCallback scanCallback = new ScanCallback() {

                @Override
                public void onScanResult(int callbackType, ScanResult result) {
                    if (callbackType != ScanSettings.CALLBACK_TYPE_ALL_MATCHES) {
                        // Should not happen.
                        Log.e(TAG, "LE Scan has already started");
                        return;
                    }
                    ScanRecord scanRecord = result.getScanRecord();
                    if (scanRecord == null) {
                        return;
                    }
                    if (serviceUuids != null) {
                        List<ParcelUuid> uuids = new ArrayList<ParcelUuid>();
                        for (UUID uuid : serviceUuids) {
                            uuids.add(new ParcelUuid(uuid));
                        }
                        List<ParcelUuid> scanServiceUuids = scanRecord.getServiceUuids();
                        if (scanServiceUuids == null || !scanServiceUuids.containsAll(uuids)) {
                            if (DBG)
                                Log.d(TAG, "uuids does not match");
                            return;
                        }
                    }
                    callback.onLeScan(result.getDevice(), result.getRssi(), scanRecord.getBytes());
                }
            };
            ScanSettings settings = new ScanSettings.Builder().setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES).setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
            List<ScanFilter> filters = new ArrayList<ScanFilter>();
            if (serviceUuids != null && serviceUuids.length > 0) {
                // Note scan filter does not support matching an UUID array so we put one
                // UUID to hardware and match the whole array in callback.
                ScanFilter filter = new ScanFilter.Builder().setServiceUuid(new ParcelUuid(serviceUuids[0])).build();
                filters.add(filter);
            }
            scanner.startScan(filters, settings, scanCallback);
            mLeScanClients.put(callback, scanCallback);
            return true;
        } catch (RemoteException e) {
            Log.e(TAG, "", e);
        }
    }
    return false;
}
Also used : ParcelUuid(android.os.ParcelUuid) BluetoothLeScanner(android.bluetooth.le.BluetoothLeScanner) ScanResult(android.bluetooth.le.ScanResult) ScanSettings(android.bluetooth.le.ScanSettings) ScanFilter(android.bluetooth.le.ScanFilter) ArrayList(java.util.ArrayList) ScanCallback(android.bluetooth.le.ScanCallback) UUID(java.util.UUID) RemoteException(android.os.RemoteException) ScanRecord(android.bluetooth.le.ScanRecord) RequiresPermission(android.annotation.RequiresPermission)

Example 4 with RequiresPermission

use of android.annotation.RequiresPermission in project android_frameworks_base by AOSPA.

the class BluetoothAdapter method startLeScan.

/**
     * Starts a scan for Bluetooth LE devices, looking for devices that
     * advertise given services.
     *
     * <p>Devices which advertise all specified services are reported using the
     * {@link LeScanCallback#onLeScan} callback.
     *
     * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.
     *
     * @param serviceUuids Array of services to look for
     * @param callback the callback LE scan results are delivered
     * @return true, if the scan was started successfully
     * @deprecated use {@link BluetoothLeScanner#startScan(List, ScanSettings, ScanCallback)}
     *             instead.
     */
@Deprecated
@RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
public boolean startLeScan(final UUID[] serviceUuids, final LeScanCallback callback) {
    if (DBG)
        Log.d(TAG, "startLeScan(): " + Arrays.toString(serviceUuids));
    if (callback == null) {
        if (DBG)
            Log.e(TAG, "startLeScan: null callback");
        return false;
    }
    BluetoothLeScanner scanner = getBluetoothLeScanner();
    if (scanner == null) {
        if (DBG)
            Log.e(TAG, "startLeScan: cannot get BluetoothLeScanner");
        return false;
    }
    synchronized (mLeScanClients) {
        if (mLeScanClients.containsKey(callback)) {
            if (DBG)
                Log.e(TAG, "LE Scan has already started");
            return false;
        }
        try {
            IBluetoothGatt iGatt = mManagerService.getBluetoothGatt();
            if (iGatt == null) {
                // BLE is not supported
                return false;
            }
            ScanCallback scanCallback = new ScanCallback() {

                @Override
                public void onScanResult(int callbackType, ScanResult result) {
                    if (callbackType != ScanSettings.CALLBACK_TYPE_ALL_MATCHES) {
                        // Should not happen.
                        Log.e(TAG, "LE Scan has already started");
                        return;
                    }
                    ScanRecord scanRecord = result.getScanRecord();
                    if (scanRecord == null) {
                        return;
                    }
                    if (serviceUuids != null) {
                        List<ParcelUuid> uuids = new ArrayList<ParcelUuid>();
                        for (UUID uuid : serviceUuids) {
                            uuids.add(new ParcelUuid(uuid));
                        }
                        List<ParcelUuid> scanServiceUuids = scanRecord.getServiceUuids();
                        if (scanServiceUuids == null || !scanServiceUuids.containsAll(uuids)) {
                            if (DBG)
                                Log.d(TAG, "uuids does not match");
                            return;
                        }
                    }
                    callback.onLeScan(result.getDevice(), result.getRssi(), scanRecord.getBytes());
                }
            };
            ScanSettings settings = new ScanSettings.Builder().setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES).setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
            List<ScanFilter> filters = new ArrayList<ScanFilter>();
            if (serviceUuids != null && serviceUuids.length > 0) {
                // Note scan filter does not support matching an UUID array so we put one
                // UUID to hardware and match the whole array in callback.
                ScanFilter filter = new ScanFilter.Builder().setServiceUuid(new ParcelUuid(serviceUuids[0])).build();
                filters.add(filter);
            }
            scanner.startScan(filters, settings, scanCallback);
            mLeScanClients.put(callback, scanCallback);
            return true;
        } catch (RemoteException e) {
            Log.e(TAG, "", e);
        }
    }
    return false;
}
Also used : ParcelUuid(android.os.ParcelUuid) BluetoothLeScanner(android.bluetooth.le.BluetoothLeScanner) ScanResult(android.bluetooth.le.ScanResult) ScanSettings(android.bluetooth.le.ScanSettings) ScanFilter(android.bluetooth.le.ScanFilter) ArrayList(java.util.ArrayList) ScanCallback(android.bluetooth.le.ScanCallback) UUID(java.util.UUID) RemoteException(android.os.RemoteException) ScanRecord(android.bluetooth.le.ScanRecord) RequiresPermission(android.annotation.RequiresPermission)

Example 5 with RequiresPermission

use of android.annotation.RequiresPermission in project android_frameworks_base by AOSPA.

the class BluetoothAdapter method stopLeScan.

/**
     * Stops an ongoing Bluetooth LE device scan.
     *
     * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.
     *
     * @param callback used to identify which scan to stop
     *        must be the same handle used to start the scan
     * @deprecated Use {@link BluetoothLeScanner#stopScan(ScanCallback)} instead.
     */
@Deprecated
@RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
public void stopLeScan(LeScanCallback callback) {
    if (DBG)
        Log.d(TAG, "stopLeScan()");
    BluetoothLeScanner scanner = getBluetoothLeScanner();
    if (scanner == null) {
        return;
    }
    synchronized (mLeScanClients) {
        ScanCallback scanCallback = mLeScanClients.remove(callback);
        if (scanCallback == null) {
            if (DBG)
                Log.d(TAG, "scan not started yet");
            return;
        }
        scanner.stopScan(scanCallback);
    }
}
Also used : BluetoothLeScanner(android.bluetooth.le.BluetoothLeScanner) ScanCallback(android.bluetooth.le.ScanCallback) RequiresPermission(android.annotation.RequiresPermission)

Aggregations

RequiresPermission (android.annotation.RequiresPermission)19 BluetoothLeScanner (android.bluetooth.le.BluetoothLeScanner)10 ScanCallback (android.bluetooth.le.ScanCallback)10 RemoteException (android.os.RemoteException)9 ScanFilter (android.bluetooth.le.ScanFilter)5 ScanRecord (android.bluetooth.le.ScanRecord)5 ScanResult (android.bluetooth.le.ScanResult)5 ScanSettings (android.bluetooth.le.ScanSettings)5 IntentFilter (android.content.IntentFilter)5 ParcelUuid (android.os.ParcelUuid)5 ArrayList (java.util.ArrayList)5 UUID (java.util.UUID)5 SystemApi (android.annotation.SystemApi)4 ITelecomService (com.android.internal.telecom.ITelecomService)4