Search in sources :

Example 41 with RequiresApi

use of androidx.annotation.RequiresApi in project xabber-android by redsolution.

the class NotificationChannelUtils method createEventsChannel.

@RequiresApi(api = Build.VERSION_CODES.O)
public static String createEventsChannel(NotificationManager notifManager) {
    @SuppressLint("WrongConstant") NotificationChannel channel = new NotificationChannel(EVENTS_CHANNEL_ID, getString(R.string.channel_events_title), android.app.NotificationManager.IMPORTANCE_HIGH);
    channel.setDescription(getString(R.string.channel_events_description));
    channel.setShowBadge(false);
    notifManager.createNotificationChannel(channel);
    return channel.getId();
}
Also used : NotificationChannel(android.app.NotificationChannel) SuppressLint(android.annotation.SuppressLint) RequiresApi(androidx.annotation.RequiresApi)

Example 42 with RequiresApi

use of androidx.annotation.RequiresApi in project mobile-center-sdk-android by Microsoft.

the class CryptoAesAndEtmHandler method encrypt.

@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public byte[] encrypt(CryptoUtils.ICryptoFactory cryptoFactory, int apiLevel, KeyStore.Entry keyStoreEntry, byte[] input) throws Exception {
    // Get secure key and subkeys.
    SecretKey key = ((KeyStore.SecretKeyEntry) keyStoreEntry).getSecretKey();
    byte[] encryptionSubkey = getSubkey(key, ENCRYPTION_KEY_LENGTH);
    byte[] authenticationSubkey = getSubkey(key, AUTHENTICATION_KEY_LENGTH);
    // Prepare cipher.
    CryptoUtils.ICipher cipher = cryptoFactory.getCipher(CryptoConstants.CIPHER_AES, null);
    cipher.init(ENCRYPT_MODE, new SecretKeySpec(encryptionSubkey, KeyProperties.KEY_ALGORITHM_AES));
    byte[] cipherIv = cipher.getIV();
    byte[] cipherOutput = cipher.doFinal(input);
    // Calculate mac.
    byte[] hMac = getMacBytes(authenticationSubkey, cipherIv, cipherOutput);
    // Convert data to common message.
    ByteBuffer byteBuffer = ByteBuffer.allocate(1 + cipherIv.length + 1 + hMac.length + cipherOutput.length);
    byteBuffer.put((byte) cipherIv.length);
    byteBuffer.put(cipherIv);
    byteBuffer.put((byte) hMac.length);
    byteBuffer.put(hMac);
    byteBuffer.put(cipherOutput);
    return byteBuffer.array();
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) ByteBuffer(java.nio.ByteBuffer) RequiresApi(androidx.annotation.RequiresApi)

Example 43 with RequiresApi

use of androidx.annotation.RequiresApi in project mobile-center-sdk-android by Microsoft.

the class CryptoAesAndEtmHandler method getSubkey.

/**
 * Get subkey from the secret key.
 * This method uses HKDF simple key derivation function (KDF) based on a hash-based message authentication code (HMAC).
 * See more: https://en.wikipedia.org/wiki/HKDF
 *
 * @param secretKey - secret key.
 * @param outputDataLength - subkey length.
 * @return byte array of the calculated subkey.
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 */
@RequiresApi(api = Build.VERSION_CODES.M)
@VisibleForTesting
byte[] getSubkey(@NonNull SecretKey secretKey, int outputDataLength) throws NoSuchAlgorithmException, InvalidKeyException {
    if (outputDataLength < 1) {
        throw new IllegalArgumentException("Output data length must be greater than zero.");
    }
    // Init mac.
    Mac hMac = Mac.getInstance(KeyProperties.KEY_ALGORITHM_HMAC_SHA256);
    hMac.init(secretKey);
    // Prepare array and calculate count of iterations.
    int iterations = (int) Math.ceil(((double) outputDataLength) / ((double) hMac.getMacLength()));
    if (iterations > 255) {
        throw new IllegalArgumentException("Output data length must be maximum of 255 * hash-length.");
    }
    // Calculate subkey.
    byte[] tempBlock = new byte[0];
    ByteBuffer buffer = ByteBuffer.allocate(outputDataLength);
    int restBytes = outputDataLength;
    int stepSize;
    for (int i = 0; i < iterations; i++) {
        hMac.update(tempBlock);
        hMac.update((byte) (i + 1));
        tempBlock = hMac.doFinal();
        stepSize = Math.min(restBytes, tempBlock.length);
        buffer.put(tempBlock, 0, stepSize);
        restBytes -= stepSize;
    }
    return buffer.array();
}
Also used : ByteBuffer(java.nio.ByteBuffer) Mac(javax.crypto.Mac) VisibleForTesting(androidx.annotation.VisibleForTesting) RequiresApi(androidx.annotation.RequiresApi)

Example 44 with RequiresApi

use of androidx.annotation.RequiresApi in project android-beacon-library by AltBeacon.

the class ScanHelper method startAndroidOBackgroundScan.

@RequiresApi(api = Build.VERSION_CODES.O)
void startAndroidOBackgroundScan(Set<BeaconParser> beaconParsers, List<Region> regions) {
    ScanSettings settings = (new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)).build();
    List<ScanFilter> filters = new ScanFilterUtils().createScanFiltersForBeaconParsers(new ArrayList<BeaconParser>(beaconParsers), regions);
    try {
        final BluetoothManager bluetoothManager = (BluetoothManager) mContext.getApplicationContext().getSystemService(Context.BLUETOOTH_SERVICE);
        BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
        if (bluetoothAdapter == null) {
            LogManager.w(TAG, "Failed to construct a BluetoothAdapter");
        } else if (!bluetoothAdapter.isEnabled()) {
            LogManager.w(TAG, "Failed to start background scan on Android O: BluetoothAdapter is not enabled");
        } else {
            BluetoothLeScanner scanner = bluetoothAdapter.getBluetoothLeScanner();
            if (scanner != null) {
                int result = scanner.startScan(filters, settings, getScanCallbackIntent());
                if (result != 0) {
                    LogManager.e(TAG, "Failed to start background scan on Android O.  Code: " + result);
                } else {
                    LogManager.d(TAG, "Started passive beacon scan");
                }
            } else {
                LogManager.e(TAG, "Failed to start background scan on Android O: scanner is null");
            }
        }
    } catch (SecurityException e) {
        LogManager.e(TAG, "SecurityException making Android O background scanner");
    } catch (NullPointerException e) {
        // Needed to stop a crash caused by internal NPE thrown by Android.  See issue #636
        LogManager.e(TAG, "NullPointerException starting Android O background scanner", e);
    } catch (RuntimeException e) {
        // Needed to stop a crash caused by internal Android throw.  See issue #701
        LogManager.e(TAG, "Unexpected runtime exception starting Android O background scanner", e);
    }
}
Also used : ScanSettings(android.bluetooth.le.ScanSettings) BluetoothLeScanner(android.bluetooth.le.BluetoothLeScanner) ScanFilter(android.bluetooth.le.ScanFilter) BluetoothManager(android.bluetooth.BluetoothManager) ScanFilterUtils(org.altbeacon.beacon.service.scanner.ScanFilterUtils) BeaconParser(org.altbeacon.beacon.BeaconParser) BluetoothAdapter(android.bluetooth.BluetoothAdapter) RequiresApi(androidx.annotation.RequiresApi)

Example 45 with RequiresApi

use of androidx.annotation.RequiresApi in project android-beacon-library by AltBeacon.

the class ScanHelper method stopAndroidOBackgroundScan.

@RequiresApi(api = Build.VERSION_CODES.O)
void stopAndroidOBackgroundScan() {
    try {
        final BluetoothManager bluetoothManager = (BluetoothManager) mContext.getApplicationContext().getSystemService(Context.BLUETOOTH_SERVICE);
        BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
        if (bluetoothAdapter == null) {
            LogManager.w(TAG, "Failed to construct a BluetoothAdapter");
        } else if (!bluetoothAdapter.isEnabled()) {
            LogManager.w(TAG, "BluetoothAdapter is not enabled");
        } else {
            BluetoothLeScanner scanner = bluetoothAdapter.getBluetoothLeScanner();
            if (scanner != null) {
                scanner.stopScan(getScanCallbackIntent());
            }
        }
    } catch (SecurityException e) {
        LogManager.e(TAG, "SecurityException stopping Android O background scanner");
    } catch (NullPointerException e) {
        // Needed to stop a crash caused by internal NPE thrown by Android.  See issue #636
        LogManager.e(TAG, "NullPointerException stopping Android O background scanner", e);
    } catch (RuntimeException e) {
        // Needed to stop a crash caused by internal Android throw.  See issue #701
        LogManager.e(TAG, "Unexpected runtime exception stopping Android O background scanner", e);
    }
}
Also used : BluetoothLeScanner(android.bluetooth.le.BluetoothLeScanner) BluetoothManager(android.bluetooth.BluetoothManager) BluetoothAdapter(android.bluetooth.BluetoothAdapter) RequiresApi(androidx.annotation.RequiresApi)

Aggregations

RequiresApi (androidx.annotation.RequiresApi)161 NotificationChannel (android.app.NotificationChannel)20 Intent (android.content.Intent)16 NonNull (androidx.annotation.NonNull)16 SuppressLint (android.annotation.SuppressLint)14 Uri (android.net.Uri)14 NotificationManager (android.app.NotificationManager)13 IOException (java.io.IOException)13 StatusBarNotification (android.service.notification.StatusBarNotification)10 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)10 Context (android.content.Context)9 Rect (android.graphics.Rect)8 Notification (android.app.Notification)7 ByteBuffer (java.nio.ByteBuffer)7 ArrayList (java.util.ArrayList)7 SecretKey (javax.crypto.SecretKey)7 PendingIntent (android.app.PendingIntent)6 Bitmap (android.graphics.Bitmap)5 Point (android.graphics.Point)5 Build (android.os.Build)5