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();
}
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();
}
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();
}
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);
}
}
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);
}
}
Aggregations