use of androidx.annotation.WorkerThread in project BigImageViewer by Piasy.
the class BigImageView method saveImageIntoGallery.
@WorkerThread
@RequiresPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
public void saveImageIntoGallery() {
if (mCurrentImageFile == null) {
fireSaveImageCallback(null, new IllegalStateException("image not downloaded yet"));
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
OutputStream outputStream = null;
FileInputStream inputStream = null;
Uri imageUri = null;
boolean saved = false;
try {
ContentResolver resolver = getContext().getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, mCurrentImageFile.getName());
// http://androidxref.com/4.4.4_r1/xref/libcore/luni/src/main/java/libcore/net/MimeUtils.java
// Please select the appropriate MIME_TYPE in the webpage
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES);
imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
if (imageUri != null) {
outputStream = resolver.openOutputStream(imageUri);
inputStream = new FileInputStream(mCurrentImageFile);
// a simple file copy is enough.
IOUtils.copy(inputStream, outputStream);
saved = true;
} else {
fireSaveImageCallback(null, new RuntimeException("saveImageIntoGallery fail: insert to MediaStore error"));
}
} catch (IOException e) {
fireSaveImageCallback(null, e);
} finally {
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
}
if (saved) {
fireSaveImageCallback(imageUri.toString(), null);
}
} else {
try {
String result = MediaStore.Images.Media.insertImage(getContext().getContentResolver(), mCurrentImageFile.getAbsolutePath(), mCurrentImageFile.getName(), "");
fireSaveImageCallback(result, null);
} catch (IOException e) {
fireSaveImageCallback(null, e);
}
}
}
use of androidx.annotation.WorkerThread in project OneSignal-Android-SDK by OneSignal.
the class PushRegistratorFCM method getTokenWithClassFirebaseMessaging.
@WorkerThread
private String getTokenWithClassFirebaseMessaging() throws ExecutionException, InterruptedException {
// We use firebaseApp.get(FirebaseMessaging.class) instead of FirebaseMessaging.getInstance()
// as the latter uses the default Firebase app. We need to use a custom Firebase app as
// the senderId is provided at runtime.
FirebaseMessaging fcmInstance = firebaseApp.get(FirebaseMessaging.class);
// FirebaseMessaging.getToken API was introduced in firebase-messaging:21.0.0
Task<String> tokenTask = fcmInstance.getToken();
return Tasks.await(tokenTask);
}
use of androidx.annotation.WorkerThread in project OneSignal-Android-SDK by OneSignal.
the class PushRegistratorFCM method getTokenWithClassFirebaseInstanceId.
// This method is only used if firebase-message older than 21.0.0 is in the app
// We are using reflection here so we can compile with firebase-message:22.0.0 and newer
// - This version of Firebase has completely removed FirebaseInstanceId
@Deprecated
@WorkerThread
private String getTokenWithClassFirebaseInstanceId(String senderId) throws IOException {
// The following code is equivalent to:
// FirebaseInstanceId instanceId = FirebaseInstanceId.getInstance(firebaseApp);
// return instanceId.getToken(senderId, FirebaseMessaging.INSTANCE_ID_SCOPE);
Exception exception;
try {
Class<?> FirebaseInstanceIdClass = Class.forName("com.google.firebase.iid.FirebaseInstanceId");
Method getInstanceMethod = FirebaseInstanceIdClass.getMethod("getInstance", FirebaseApp.class);
Object instanceId = getInstanceMethod.invoke(null, firebaseApp);
Method getTokenMethod = instanceId.getClass().getMethod("getToken", String.class, String.class);
Object token = getTokenMethod.invoke(instanceId, senderId, "FCM");
return (String) token;
} catch (ClassNotFoundException e) {
exception = e;
} catch (NoSuchMethodException e) {
exception = e;
} catch (IllegalAccessException e) {
exception = e;
} catch (InvocationTargetException e) {
exception = e;
}
throw new Error("Reflection error on FirebaseInstanceId.getInstance(firebaseApp).getToken(senderId, FirebaseMessaging.INSTANCE_ID_SCOPE)", exception);
}
use of androidx.annotation.WorkerThread in project OneSignal-Android-SDK by OneSignal.
the class OSInAppMessageRepository method cleanCachedInAppMessages.
@WorkerThread
synchronized void cleanCachedInAppMessages() {
// 1. Query for all old message ids and old clicked click ids
String[] retColumns = new String[] { OneSignalDbContract.InAppMessageTable.COLUMN_NAME_MESSAGE_ID, OneSignalDbContract.InAppMessageTable.COLUMN_CLICK_IDS };
String whereStr = OneSignalDbContract.InAppMessageTable.COLUMN_NAME_LAST_DISPLAY + " < ?";
String sixMonthsAgoInSeconds = String.valueOf((System.currentTimeMillis() / 1_000L) - IAM_CACHE_DATA_LIFETIME);
String[] whereArgs = new String[] { sixMonthsAgoInSeconds };
Set<String> oldMessageIds = OSUtils.newConcurrentSet();
Set<String> oldClickedClickIds = OSUtils.newConcurrentSet();
Cursor cursor = null;
try {
cursor = dbHelper.query(OneSignalDbContract.InAppMessageTable.TABLE_NAME, retColumns, whereStr, whereArgs, null, null, null);
if (cursor == null || cursor.getCount() == 0) {
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.DEBUG, "Attempted to clean 6 month old IAM data, but none exists!");
return;
}
// From cursor get all of the old message ids and old clicked click ids
if (cursor.moveToFirst()) {
do {
String oldMessageId = cursor.getString(cursor.getColumnIndex(OneSignalDbContract.InAppMessageTable.COLUMN_NAME_MESSAGE_ID));
String oldClickIds = cursor.getString(cursor.getColumnIndex(OneSignalDbContract.InAppMessageTable.COLUMN_CLICK_IDS));
oldMessageIds.add(oldMessageId);
oldClickedClickIds.addAll(OSUtils.newStringSetFromJSONArray(new JSONArray(oldClickIds)));
} while (cursor.moveToNext());
}
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (cursor != null && !cursor.isClosed())
cursor.close();
}
// 2. Delete old IAMs from SQL
dbHelper.delete(OneSignalDbContract.InAppMessageTable.TABLE_NAME, whereStr, whereArgs);
// 3. Use queried data to clean SharedPreferences
cleanInAppMessageIds(oldMessageIds);
cleanInAppMessageClickedClickIds(oldClickedClickIds);
}
use of androidx.annotation.WorkerThread in project OneSignal-Android-SDK by OneSignal.
the class OSInAppMessageRepository method saveInAppMessage.
@WorkerThread
synchronized void saveInAppMessage(OSInAppMessageInternal inAppMessage) {
ContentValues values = new ContentValues();
values.put(OneSignalDbContract.InAppMessageTable.COLUMN_NAME_MESSAGE_ID, inAppMessage.messageId);
values.put(OneSignalDbContract.InAppMessageTable.COLUMN_NAME_DISPLAY_QUANTITY, inAppMessage.getRedisplayStats().getDisplayQuantity());
values.put(OneSignalDbContract.InAppMessageTable.COLUMN_NAME_LAST_DISPLAY, inAppMessage.getRedisplayStats().getLastDisplayTime());
values.put(OneSignalDbContract.InAppMessageTable.COLUMN_CLICK_IDS, inAppMessage.getClickedClickIds().toString());
values.put(OneSignalDbContract.InAppMessageTable.COLUMN_DISPLAYED_IN_SESSION, inAppMessage.isDisplayedInSession());
int rowsUpdated = dbHelper.update(OneSignalDbContract.InAppMessageTable.TABLE_NAME, values, OneSignalDbContract.InAppMessageTable.COLUMN_NAME_MESSAGE_ID + " = ?", new String[] { inAppMessage.messageId });
if (rowsUpdated == 0)
dbHelper.insert(OneSignalDbContract.InAppMessageTable.TABLE_NAME, null, values);
}
Aggregations