Search in sources :

Example 1 with ArtDetailOpenedClosedEvent

use of com.google.android.apps.muzei.event.ArtDetailOpenedClosedEvent in project muzei by romannurik.

the class MuzeiActivity method maybeUpdateArtDetailOpenedClosed.

private void maybeUpdateArtDetailOpenedClosed() {
    boolean currentlyOpened = false;
    ArtDetailOpenedClosedEvent adoce = EventBus.getDefault().getStickyEvent(ArtDetailOpenedClosedEvent.class);
    if (adoce != null) {
        currentlyOpened = adoce.isArtDetailOpened();
    }
    boolean shouldBeOpened = false;
    if (mUiMode == UI_MODE_ART_DETAIL && //                                    // a zoom out / in visual glitch
    (mWindowHasFocus || mOverflowMenuVisible) && !mPaused) {
        shouldBeOpened = true;
    }
    if (currentlyOpened != shouldBeOpened) {
        EventBus.getDefault().postSticky(new ArtDetailOpenedClosedEvent(shouldBeOpened));
    }
}
Also used : ArtDetailOpenedClosedEvent(com.google.android.apps.muzei.event.ArtDetailOpenedClosedEvent)

Example 2 with ArtDetailOpenedClosedEvent

use of com.google.android.apps.muzei.event.ArtDetailOpenedClosedEvent in project muzei by romannurik.

the class NewWallpaperNotificationReceiver method maybeShowNewArtworkNotification.

public static void maybeShowNewArtworkNotification(Context context) {
    ArtDetailOpenedClosedEvent adoce = EventBus.getDefault().getStickyEvent(ArtDetailOpenedClosedEvent.class);
    if (adoce != null && adoce.isArtDetailOpened()) {
        return;
    }
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
    if (!sp.getBoolean(PREF_ENABLED, true)) {
        return;
    }
    ContentResolver contentResolver = context.getContentResolver();
    Cursor artwork = contentResolver.query(MuzeiContract.Artwork.CONTENT_URI, new String[] { BaseColumns._ID, MuzeiContract.Artwork.COLUMN_NAME_IMAGE_URI, MuzeiContract.Artwork.COLUMN_NAME_TOKEN, MuzeiContract.Artwork.COLUMN_NAME_TITLE, MuzeiContract.Artwork.COLUMN_NAME_BYLINE, MuzeiContract.Artwork.COLUMN_NAME_VIEW_INTENT, MuzeiContract.Sources.COLUMN_NAME_SUPPORTS_NEXT_ARTWORK_COMMAND, MuzeiContract.Sources.COLUMN_NAME_COMMANDS }, null, null, null);
    if (artwork == null || !artwork.moveToFirst()) {
        if (artwork != null) {
            artwork.close();
        }
        return;
    }
    long currentArtworkId = artwork.getLong(artwork.getColumnIndex(BaseColumns._ID));
    long lastReadArtworkId = sp.getLong(PREF_LAST_READ_NOTIFICATION_ARTWORK_ID, -1);
    String currentImageUri = artwork.getString(artwork.getColumnIndex(MuzeiContract.Artwork.COLUMN_NAME_IMAGE_URI));
    String lastReadImageUri = sp.getString(PREF_LAST_READ_NOTIFICATION_ARTWORK_IMAGE_URI, null);
    String currentToken = artwork.getString(artwork.getColumnIndex(MuzeiContract.Artwork.COLUMN_NAME_TOKEN));
    String lastReadToken = sp.getString(PREF_LAST_READ_NOTIFICATION_ARTWORK_TOKEN, null);
    // We've already dismissed the notification if the IDs match
    boolean previouslyDismissedNotification = lastReadArtworkId == currentArtworkId;
    // We've already dismissed the notification if the image URIs match and both are not empty
    previouslyDismissedNotification = previouslyDismissedNotification || (!TextUtils.isEmpty(lastReadImageUri) && !TextUtils.isEmpty(currentImageUri) && TextUtils.equals(lastReadImageUri, currentImageUri));
    // We've already dismissed the notification if the tokens match and both are not empty
    previouslyDismissedNotification = previouslyDismissedNotification || (!TextUtils.isEmpty(lastReadToken) && !TextUtils.isEmpty(currentToken) && TextUtils.equals(lastReadToken, currentToken));
    if (previouslyDismissedNotification) {
        artwork.close();
        return;
    }
    Bitmap largeIcon;
    Bitmap background;
    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(contentResolver.openInputStream(MuzeiContract.Artwork.CONTENT_URI), null, options);
        int width = options.outWidth;
        int height = options.outHeight;
        int shortestLength = Math.min(width, height);
        options.inJustDecodeBounds = false;
        int largeIconHeight = context.getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_height);
        options.inSampleSize = ImageUtil.calculateSampleSize(shortestLength, largeIconHeight);
        largeIcon = BitmapFactory.decodeStream(contentResolver.openInputStream(MuzeiContract.Artwork.CONTENT_URI), null, options);
        // Use the suggested 400x400 for Android Wear background images per
        // http://developer.android.com/training/wearables/notifications/creating.html#AddWearableFeatures
        options.inSampleSize = ImageUtil.calculateSampleSize(height, 400);
        background = BitmapFactory.decodeStream(contentResolver.openInputStream(MuzeiContract.Artwork.CONTENT_URI), null, options);
    } catch (FileNotFoundException e) {
        Log.e(TAG, "Unable to read artwork to show notification", e);
        return;
    }
    String artworkTitle = artwork.getString(artwork.getColumnIndex(MuzeiContract.Artwork.COLUMN_NAME_TITLE));
    String title = TextUtils.isEmpty(artworkTitle) ? context.getString(R.string.app_name) : artworkTitle;
    NotificationCompat.Builder nb = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_stat_muzei).setColor(ContextCompat.getColor(context, R.color.notification)).setPriority(Notification.PRIORITY_MIN).setAutoCancel(true).setContentTitle(title).setContentText(context.getString(R.string.notification_new_wallpaper)).setLargeIcon(largeIcon).setContentIntent(PendingIntent.getActivity(context, 0, Intent.makeMainActivity(new ComponentName(context, MuzeiActivity.class)), PendingIntent.FLAG_UPDATE_CURRENT)).setDeleteIntent(PendingIntent.getBroadcast(context, 0, new Intent(context, NewWallpaperNotificationReceiver.class).setAction(ACTION_MARK_NOTIFICATION_READ), PendingIntent.FLAG_UPDATE_CURRENT));
    NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle().bigLargeIcon(null).setBigContentTitle(title).setSummaryText(artwork.getString(artwork.getColumnIndex(MuzeiContract.Artwork.COLUMN_NAME_BYLINE))).bigPicture(background);
    nb.setStyle(style);
    NotificationCompat.WearableExtender extender = new NotificationCompat.WearableExtender();
    // Support Next Artwork
    if (artwork.getInt(artwork.getColumnIndex(MuzeiContract.Sources.COLUMN_NAME_SUPPORTS_NEXT_ARTWORK_COMMAND)) != 0) {
        PendingIntent nextPendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(context, NewWallpaperNotificationReceiver.class).setAction(ACTION_NEXT_ARTWORK), PendingIntent.FLAG_UPDATE_CURRENT);
        nb.addAction(R.drawable.ic_notif_next_artwork, context.getString(R.string.action_next_artwork_condensed), nextPendingIntent);
        // Android Wear uses larger action icons so we build a
        // separate action
        extender.addAction(new NotificationCompat.Action.Builder(R.drawable.ic_notif_full_next_artwork, context.getString(R.string.action_next_artwork_condensed), nextPendingIntent).extend(new NotificationCompat.Action.WearableExtender().setAvailableOffline(false)).build());
    }
    List<UserCommand> commands = MuzeiContract.Sources.parseCommands(artwork.getString(artwork.getColumnIndex(MuzeiContract.Sources.COLUMN_NAME_COMMANDS)));
    // Show custom actions as a selectable list on Android Wear devices
    if (!commands.isEmpty()) {
        String[] actions = new String[commands.size()];
        for (int h = 0; h < commands.size(); h++) {
            actions[h] = commands.get(h).getTitle();
        }
        PendingIntent userCommandPendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(context, NewWallpaperNotificationReceiver.class).setAction(ACTION_USER_COMMAND), PendingIntent.FLAG_UPDATE_CURRENT);
        RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_USER_COMMAND).setAllowFreeFormInput(false).setLabel(context.getString(R.string.action_user_command_prompt)).setChoices(actions).build();
        extender.addAction(new NotificationCompat.Action.Builder(R.drawable.ic_notif_full_user_command, context.getString(R.string.action_user_command), userCommandPendingIntent).addRemoteInput(remoteInput).extend(new NotificationCompat.Action.WearableExtender().setAvailableOffline(false)).build());
    }
    Intent viewIntent = null;
    try {
        String viewIntentString = artwork.getString(artwork.getColumnIndex(MuzeiContract.Artwork.COLUMN_NAME_VIEW_INTENT));
        if (!TextUtils.isEmpty(viewIntentString)) {
            viewIntent = Intent.parseUri(viewIntentString, Intent.URI_INTENT_SCHEME);
        }
    } catch (URISyntaxException ignored) {
    }
    if (viewIntent != null) {
        viewIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            PendingIntent nextPendingIntent = PendingIntent.getActivity(context, 0, viewIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            nb.addAction(R.drawable.ic_notif_info, context.getString(R.string.action_artwork_info), nextPendingIntent);
            // Android Wear uses larger action icons so we build a
            // separate action
            extender.addAction(new NotificationCompat.Action.Builder(R.drawable.ic_notif_full_info, context.getString(R.string.action_artwork_info), nextPendingIntent).extend(new NotificationCompat.Action.WearableExtender().setAvailableOffline(false)).build());
        } catch (RuntimeException ignored) {
        // This is actually meant to catch a FileUriExposedException, but you can't
        // have catch statements for exceptions that don't exist at your minSdkVersion
        }
    }
    nb.extend(extender);
    // Hide the image and artwork title for the public version
    NotificationCompat.Builder publicBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_stat_muzei).setColor(ContextCompat.getColor(context, R.color.notification)).setPriority(Notification.PRIORITY_MIN).setAutoCancel(true).setContentTitle(context.getString(R.string.app_name)).setContentText(context.getString(R.string.notification_new_wallpaper)).setContentIntent(PendingIntent.getActivity(context, 0, Intent.makeMainActivity(new ComponentName(context, MuzeiActivity.class)), PendingIntent.FLAG_UPDATE_CURRENT)).setDeleteIntent(PendingIntent.getBroadcast(context, 0, new Intent(context, NewWallpaperNotificationReceiver.class).setAction(ACTION_MARK_NOTIFICATION_READ), PendingIntent.FLAG_UPDATE_CURRENT));
    nb.setPublicVersion(publicBuilder.build());
    NotificationManagerCompat nm = NotificationManagerCompat.from(context);
    nm.notify(NOTIFICATION_ID, nb.build());
    artwork.close();
}
Also used : FileNotFoundException(java.io.FileNotFoundException) NotificationManagerCompat(android.support.v4.app.NotificationManagerCompat) URISyntaxException(java.net.URISyntaxException) Cursor(android.database.Cursor) ContentResolver(android.content.ContentResolver) Bitmap(android.graphics.Bitmap) NotificationCompat(android.support.v4.app.NotificationCompat) ComponentName(android.content.ComponentName) BitmapFactory(android.graphics.BitmapFactory) RemoteInput(android.support.v4.app.RemoteInput) SharedPreferences(android.content.SharedPreferences) ArtDetailOpenedClosedEvent(com.google.android.apps.muzei.event.ArtDetailOpenedClosedEvent) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) UserCommand(com.google.android.apps.muzei.api.UserCommand) PendingIntent(android.app.PendingIntent)

Aggregations

ArtDetailOpenedClosedEvent (com.google.android.apps.muzei.event.ArtDetailOpenedClosedEvent)2 PendingIntent (android.app.PendingIntent)1 ComponentName (android.content.ComponentName)1 ContentResolver (android.content.ContentResolver)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 Cursor (android.database.Cursor)1 Bitmap (android.graphics.Bitmap)1 BitmapFactory (android.graphics.BitmapFactory)1 NotificationCompat (android.support.v4.app.NotificationCompat)1 NotificationManagerCompat (android.support.v4.app.NotificationManagerCompat)1 RemoteInput (android.support.v4.app.RemoteInput)1 UserCommand (com.google.android.apps.muzei.api.UserCommand)1 FileNotFoundException (java.io.FileNotFoundException)1 URISyntaxException (java.net.URISyntaxException)1