Search in sources :

Example 1 with FetchBitmapTask

use of com.google.android.libraries.cast.companionlibrary.utils.FetchBitmapTask in project Shuttle by timusus.

the class VideoCastManager method updateMediaSessionMetadata.

/*
     * On ICS and JB, lock screen metadata is one liner: Title - Album Artist - Album. On KitKat, it
     * has two lines: Title , Album Artist - Album
     */
private void updateMediaSessionMetadata() {
    if ((mMediaSessionCompat == null) || !isFeatureEnabled(CastConfiguration.FEATURE_LOCKSCREEN)) {
        return;
    }
    try {
        MediaInfo info = getRemoteMediaInformation();
        if (info == null) {
            return;
        }
        final MediaMetadata mm = info.getMetadata();
        MediaMetadataCompat currentMetadata = mMediaSessionCompat.getController().getMetadata();
        MediaMetadataCompat.Builder newBuilder = currentMetadata == null ? new MediaMetadataCompat.Builder() : new MediaMetadataCompat.Builder(currentMetadata);
        MediaMetadataCompat metadata = newBuilder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, mm.getString(MediaMetadata.KEY_TITLE)).putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, mContext.getResources().getString(R.string.ccl_casting_to_device, getDeviceName())).putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, mm.getString(MediaMetadata.KEY_TITLE)).putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE, mm.getString(MediaMetadata.KEY_SUBTITLE)).putLong(MediaMetadataCompat.METADATA_KEY_DURATION, info.getStreamDuration()).build();
        mMediaSessionCompat.setMetadata(metadata);
        Uri iconUri = mm.hasImages() ? mm.getImages().get(0).getUrl() : null;
        if (iconUri == null) {
            Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.album_art_placeholder);
            mMediaSessionCompat.setMetadata(newBuilder.putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, bm).build());
        } else {
            if (mMediaSessionIconFetchTask != null) {
                mMediaSessionIconFetchTask.cancel(true);
            }
            mMediaSessionIconFetchTask = new FetchBitmapTask() {

                @Override
                protected void onPostExecute(Bitmap bitmap) {
                    if (bitmap != null && mMediaSessionCompat != null) {
                        MediaMetadataCompat currentMetadata = mMediaSessionCompat.getController().getMetadata();
                        MediaMetadataCompat.Builder newBuilder = currentMetadata == null ? new MediaMetadataCompat.Builder() : new MediaMetadataCompat.Builder(currentMetadata);
                        mMediaSessionCompat.setMetadata(newBuilder.putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, bitmap).build());
                    }
                    mMediaSessionIconFetchTask = null;
                }
            };
            mMediaSessionIconFetchTask.execute(iconUri);
        }
    } catch (NotFoundException e) {
        LOGE(TAG, "Failed to update Media Session due to resource not found", e);
    } catch (TransientNetworkDisconnectionException | NoConnectionException e) {
        LOGE(TAG, "Failed to update Media Session due to network issues", e);
    }
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) NoConnectionException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.NoConnectionException) Builder(com.google.android.gms.cast.Cast.CastOptions.Builder) NotFoundException(android.content.res.Resources.NotFoundException) FetchBitmapTask(com.google.android.libraries.cast.companionlibrary.utils.FetchBitmapTask) Uri(android.net.Uri) Bitmap(android.graphics.Bitmap) MediaInfo(com.google.android.gms.cast.MediaInfo) MediaMetadata(com.google.android.gms.cast.MediaMetadata) TransientNetworkDisconnectionException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.TransientNetworkDisconnectionException)

Example 2 with FetchBitmapTask

use of com.google.android.libraries.cast.companionlibrary.utils.FetchBitmapTask in project Shuttle by timusus.

the class MiniController method setUpcomingIcon.

private void setUpcomingIcon(Uri uri) {
    if (mUpcomingIconUri != null && mUpcomingIconUri.equals(uri)) {
        return;
    }
    mUpcomingIconUri = uri;
    if (mFetchUpcomingBitmapTask != null) {
        mFetchUpcomingBitmapTask.cancel(true);
    }
    mFetchUpcomingBitmapTask = new FetchBitmapTask() {

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (bitmap == null) {
                bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.album_art_placeholder);
            }
            setUpcomingIcon(bitmap);
            if (this == mFetchUpcomingBitmapTask) {
                mFetchUpcomingBitmapTask = null;
            }
        }
    };
    mFetchUpcomingBitmapTask.execute(uri);
}
Also used : Bitmap(android.graphics.Bitmap) FetchBitmapTask(com.google.android.libraries.cast.companionlibrary.utils.FetchBitmapTask)

Example 3 with FetchBitmapTask

use of com.google.android.libraries.cast.companionlibrary.utils.FetchBitmapTask in project Shuttle by timusus.

the class MiniController method setIcon.

@Override
public void setIcon(Uri uri) {
    if (mIconUri != null && mIconUri.equals(uri)) {
        return;
    }
    mIconUri = uri;
    if (mFetchBitmapTask != null) {
        mFetchBitmapTask.cancel(true);
    }
    mFetchBitmapTask = new FetchBitmapTask() {

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (bitmap == null) {
                bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.album_art_placeholder);
            }
            setIcon(bitmap);
            if (this == mFetchBitmapTask) {
                mFetchBitmapTask = null;
            }
        }
    };
    mFetchBitmapTask.execute(uri);
}
Also used : Bitmap(android.graphics.Bitmap) FetchBitmapTask(com.google.android.libraries.cast.companionlibrary.utils.FetchBitmapTask)

Example 4 with FetchBitmapTask

use of com.google.android.libraries.cast.companionlibrary.utils.FetchBitmapTask in project zype-android by zype.

the class VideoCastManager method setBitmapForLockScreen.

/*
     * Sets the appropriate {@link Bitmap} for the right size image for lock screen. In ICS and
     * JB, the image shown on the lock screen is a small size bitmap but for KitKat, the image is a
     * full-screen image so we need to separately handle these two cases.
     */
private void setBitmapForLockScreen(MediaInfo video) {
    if (video == null || mMediaSessionCompat == null) {
        return;
    }
    Uri imgUrl = null;
    Bitmap bm = null;
    List<WebImage> images = video.getMetadata().getImages();
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
        if (images.size() > 1) {
            imgUrl = images.get(1).getUrl();
        } else if (images.size() == 1) {
            imgUrl = images.get(0).getUrl();
        } else if (mContext != null) {
            // we don't have a url for image so get a placeholder image from resources
            bm = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.album_art_placeholder_large);
        }
    } else if (!images.isEmpty()) {
        imgUrl = images.get(0).getUrl();
    } else {
        // we don't have a url for image so get a placeholder image from resources
        bm = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.album_art_placeholder);
    }
    if (bm != null) {
        MediaMetadataCompat currentMetadata = mMediaSessionCompat.getController().getMetadata();
        MediaMetadataCompat.Builder newBuilder = currentMetadata == null ? new MediaMetadataCompat.Builder() : new MediaMetadataCompat.Builder(currentMetadata);
        mMediaSessionCompat.setMetadata(newBuilder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bm).build());
    } else {
        new FetchBitmapTask() {

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                MediaMetadataCompat currentMetadata = mMediaSessionCompat.getController().getMetadata();
                MediaMetadataCompat.Builder newBuilder = currentMetadata == null ? new MediaMetadataCompat.Builder() : new MediaMetadataCompat.Builder(currentMetadata);
                mMediaSessionCompat.setMetadata(newBuilder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bitmap).build());
            }
        }.execute(imgUrl);
    }
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) Bitmap(android.graphics.Bitmap) Builder(com.google.android.gms.cast.Cast.CastOptions.Builder) WebImage(com.google.android.gms.common.images.WebImage) FetchBitmapTask(com.google.android.libraries.cast.companionlibrary.utils.FetchBitmapTask) Uri(android.net.Uri)

Example 5 with FetchBitmapTask

use of com.google.android.libraries.cast.companionlibrary.utils.FetchBitmapTask in project zype-android by zype.

the class VideoCastControllerFragment method showImage.

/*
     * Gets the image at the given url and populates the image view with that. It tries to cache the
     * image to avoid unnecessary network calls.
     */
private void showImage(final Uri uri) {
    if (mImageAsyncTask != null) {
        mImageAsyncTask.cancel(true);
    }
    if (uri == null) {
        mCastController.setImage(BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.album_art_placeholder_large));
        return;
    }
    if (mUrlAndBitmap != null && mUrlAndBitmap.isMatch(uri)) {
        // we can reuse mBitmap
        mCastController.setImage(mUrlAndBitmap.mBitmap);
        return;
    }
    mUrlAndBitmap = null;
    mImageAsyncTask = new FetchBitmapTask() {

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (bitmap != null) {
                mUrlAndBitmap = new UrlAndBitmap();
                mUrlAndBitmap.mBitmap = bitmap;
                mUrlAndBitmap.mUrl = uri;
                mCastController.setImage(bitmap);
            }
            if (this == mImageAsyncTask) {
                mImageAsyncTask = null;
            }
        }
    };
    mImageAsyncTask.execute(uri);
}
Also used : Bitmap(android.graphics.Bitmap) FetchBitmapTask(com.google.android.libraries.cast.companionlibrary.utils.FetchBitmapTask)

Aggregations

Bitmap (android.graphics.Bitmap)14 FetchBitmapTask (com.google.android.libraries.cast.companionlibrary.utils.FetchBitmapTask)14 Uri (android.net.Uri)5 MediaMetadataCompat (android.support.v4.media.MediaMetadataCompat)3 Builder (com.google.android.gms.cast.Cast.CastOptions.Builder)3 Point (android.graphics.Point)2 WebImage (com.google.android.gms.common.images.WebImage)2 CastException (com.google.android.libraries.cast.companionlibrary.cast.exceptions.CastException)2 NotFoundException (android.content.res.Resources.NotFoundException)1 MediaInfo (com.google.android.gms.cast.MediaInfo)1 MediaMetadata (com.google.android.gms.cast.MediaMetadata)1 NoConnectionException (com.google.android.libraries.cast.companionlibrary.cast.exceptions.NoConnectionException)1 TransientNetworkDisconnectionException (com.google.android.libraries.cast.companionlibrary.cast.exceptions.TransientNetworkDisconnectionException)1