Search in sources :

Example 6 with WorkerThread

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);
        }
    }
}
Also used : ContentValues(android.content.ContentValues) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Uri(android.net.Uri) FileInputStream(java.io.FileInputStream) ContentResolver(android.content.ContentResolver) WorkerThread(androidx.annotation.WorkerThread) RequiresPermission(androidx.annotation.RequiresPermission)

Example 7 with WorkerThread

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);
}
Also used : FirebaseMessaging(com.google.firebase.messaging.FirebaseMessaging) WorkerThread(androidx.annotation.WorkerThread)

Example 8 with WorkerThread

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);
}
Also used : Method(java.lang.reflect.Method) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ExecutionException(java.util.concurrent.ExecutionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) WorkerThread(androidx.annotation.WorkerThread)

Example 9 with WorkerThread

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);
}
Also used : JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Cursor(android.database.Cursor) WorkerThread(androidx.annotation.WorkerThread)

Example 10 with WorkerThread

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);
}
Also used : ContentValues(android.content.ContentValues) WorkerThread(androidx.annotation.WorkerThread)

Aggregations

WorkerThread (androidx.annotation.WorkerThread)204 NonNull (androidx.annotation.NonNull)77 IOException (java.io.IOException)45 Recipient (org.thoughtcrime.securesms.recipients.Recipient)41 Nullable (androidx.annotation.Nullable)26 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)23 Cursor (android.database.Cursor)22 ArrayList (java.util.ArrayList)22 Context (android.content.Context)20 Uri (android.net.Uri)18 LinkedList (java.util.LinkedList)15 List (java.util.List)15 HashMap (java.util.HashMap)14 RecipientDatabase (org.thoughtcrime.securesms.database.RecipientDatabase)14 Stream (com.annimon.stream.Stream)13 Log (org.signal.core.util.logging.Log)13 HashSet (java.util.HashSet)12 InputStream (java.io.InputStream)11 Bitmap (android.graphics.Bitmap)10 Collections (java.util.Collections)10