Android Open Source - Muzei Artwork






From Project

Back to project page Muzei.

License

The source code is released under:

Apache License

If you think the Android project Muzei listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2014 Google Inc.//w  w w . ja v  a  2  s.co m
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.apps.muzei.api;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.URISyntaxException;

/**
 * A serializable object representing a single artwork produced by a {@link MuzeiArtSource}. An
 * artwork is simple an {@linkplain Artwork.Builder#imageUri(Uri) image} along with
 * some metadata.
 *
 * <p> To create an instance, use the {@link Artwork.Builder} class.
 */
public class Artwork {
    private static final String KEY_IMAGE_URI = "imageUri";
    private static final String KEY_TITLE = "title";
    private static final String KEY_BYLINE = "byline";
    private static final String KEY_TOKEN = "token";
    private static final String KEY_VIEW_INTENT = "viewIntent";
    private static final String KEY_DETAILS_URI = "detailsUri";

    private Uri mImageUri;
    private String mTitle;
    private String mByline;
    private String mToken;
    private Intent mViewIntent;

    private Artwork() {
    }

    /**
     * Returns the artwork's image URI, or null if it doesn't have one.
     *
     * @see Artwork.Builder#imageUri(Uri)
     */
    public Uri getImageUri() {
        return mImageUri;
    }

    /**
     * Returns the artwork's user-visible title, or null if it doesn't have one.
     *
     * @see Artwork.Builder#title(String)
     */
    public String getTitle() {
        return mTitle;
    }

    /**
     * Returns the artwork's user-visible byline (e.g. author and date), or null if it doesn't have
     * one.
     *
     * @see Artwork.Builder#byline(String)
     */
    public String getByline() {
        return mByline;
    }

    /**
     * Returns the artwork's opaque application-specific identifier, or null if it doesn't have
     * one.
     *
     * @see Artwork.Builder#token(String)
     */
    public String getToken() {
        return mToken;
    }

    /**
     * Returns the activity {@link Intent} that will be started when the user clicks
     * for more details about the artwork, or null if the artwork doesn't have one.
     *
     * @see Artwork.Builder#viewIntent(Intent)
     */
    public Intent getViewIntent() {
        return mViewIntent;
    }

    /**
     * A <a href="http://en.wikipedia.org/wiki/Builder_pattern">builder</a>-style, <a
     * href="http://en.wikipedia.org/wiki/Fluent_interface">fluent interface</a> for creating {@link
     * Artwork} objects. Example usage is below
     *
     * <pre class="prettyprint">
     * Artwork artwork = new Artwork.Builder()
     *         .imageUri(Uri.parse("http://example.com/image.jpg"))
     *         .title("Example image")
     *         .byline("Unknown person, c. 1980")
     *         .viewIntent(new Intent(Intent.ACTION_VIEW,
     *                 Uri.parse("http://example.com/imagedetails.html")))
     *         .build();
     * </pre>
     *
     * The only required field is {@linkplain #imageUri(Uri) the image URI}, but you
     * should really provide all the metadata, especially title, byline, and view intent.
     */
    public static class Builder {
        private Artwork mArtwork;

        public Builder() {
            mArtwork = new Artwork();
        }

        /**
         * Sets the artwork's image URI, which must resolve to a JPEG or PNG image, ideally
         * under 5MB. Supported URI schemes are:
         *
         * <ul>
         * <li><code>content://...</code>. Content URIs must be public (i.e. not require
         * permissions). To build a file-based content provider, see the
         * <a href="https://developer.android.com/reference/android/support/v4/content/FileProvider.html">FileProvider</a>
         * class in the Android support library.</a></li>
         * <li><code>http://...</code> or <code>https://...</code>. These URLs must be
         * publicly accessible (i.e. not require authentication of any kind).</li>
         * </ul>
         *
         * While Muzei will download and cache the artwork, these URIs should be as long-lived as
         * possible, since in the event Muzei's cache is wiped out, it will attempt to fetch the
         * image again. Also, given that the device may not be connected to the network at the time
         * an artwork is {@linkplain MuzeiArtSource#publishArtwork(Artwork) published}, the time
         * the URI may be fetched significantly after the artwork is published.
         */
        public Builder imageUri(Uri imageUri) {
            mArtwork.mImageUri = imageUri;
            return this;
        }

        /**
         * Sets the artwork's user-visible title.
         */
        public Builder title(String title) {
            mArtwork.mTitle = title;
            return this;
        }

        /**
         * Sets the artwork's user-visible byline (e.g. author and date).
         */
        public Builder byline(String byline) {
            mArtwork.mByline = byline;
            return this;
        }

        /**
         * Sets the artwork's opaque application-specific identifier.
         */
        public Builder token(String token) {
            mArtwork.mToken = token;
            return this;
        }

        /**
         * Sets the activity {@link Intent} that will be
         * {@linkplain Context#startActivity(Intent) started} when
         * the user clicks for more details about the artwork.
         *
         * <p> The activity that this intent resolves to must have <code>android:exported</code>
         * set to <code>true</code>.
         *
         * <p> Because artwork objects can be persisted across device reboots,
         * {@linkplain android.app.PendingIntent pending intents}, which would alleviate the
         * exported requirement, are not currently supported.
         */
        public Builder viewIntent(Intent viewIntent) {
            mArtwork.mViewIntent = viewIntent;
            return this;
        }

        /**
         * Creates and returns the final Artwork object. Once this method is called, it is not valid
         * to further use this {@link Artwork.Builder} object.
         */
        public Artwork build() {
            return mArtwork;
        }
    }

    /**
     * Serializes this artwork object to a {@link Bundle} representation.
     */
    public Bundle toBundle() {
        Bundle bundle = new Bundle();
        bundle.putString(KEY_IMAGE_URI, (mImageUri != null) ? mImageUri.toString() : null);
        bundle.putString(KEY_TITLE, mTitle);
        bundle.putString(KEY_BYLINE, mByline);
        bundle.putString(KEY_TOKEN, mToken);
        bundle.putString(KEY_VIEW_INTENT, (mViewIntent != null)
                ? mViewIntent.toUri(Intent.URI_INTENT_SCHEME) : null);
        return bundle;
    }

    /**
     * Deserializes an artwork object from a {@link Bundle}.
     */
    public static Artwork fromBundle(Bundle bundle) {
        Builder builder = new Builder()
                .title(bundle.getString(KEY_TITLE))
                .byline(bundle.getString(KEY_BYLINE))
                .token(bundle.getString(KEY_TOKEN));

        String imageUri = bundle.getString(KEY_IMAGE_URI);
        if (!TextUtils.isEmpty(imageUri)) {
            builder.imageUri(Uri.parse(imageUri));
        }

        try {
            String viewIntent = bundle.getString(KEY_VIEW_INTENT);
            if (!TextUtils.isEmpty(viewIntent)) {
                builder.viewIntent(Intent.parseUri(viewIntent, Intent.URI_INTENT_SCHEME));
            }
        } catch (URISyntaxException ignored) {
        }

        return builder.build();
    }

    /**
     * Serializes this artwork object to a {@link JSONObject} representation.
     */
    public JSONObject toJson() throws JSONException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(KEY_IMAGE_URI, (mImageUri != null) ? mImageUri.toString() : null);
        jsonObject.put(KEY_TITLE, mTitle);
        jsonObject.put(KEY_BYLINE, mByline);
        jsonObject.put(KEY_TOKEN, mToken);
        jsonObject.put(KEY_VIEW_INTENT, (mViewIntent != null)
                ? mViewIntent.toUri(Intent.URI_INTENT_SCHEME) : null);
        return jsonObject;
    }

    /**
     * Deserializes an artwork object from a {@link JSONObject}.
     */
    public static Artwork fromJson(JSONObject jsonObject) throws JSONException {
        Builder builder = new Builder()
                .title(jsonObject.optString(KEY_TITLE))
                .byline(jsonObject.optString(KEY_BYLINE))
                .token(jsonObject.optString(KEY_TOKEN));

        String imageUri = jsonObject.optString(KEY_IMAGE_URI);
        if (!TextUtils.isEmpty(imageUri)) {
            builder.imageUri(Uri.parse(imageUri));
        }

        try {
            String viewIntent = jsonObject.optString(KEY_VIEW_INTENT);
            String detailsUri = jsonObject.optString(KEY_DETAILS_URI);
            if (!TextUtils.isEmpty(viewIntent)) {
                builder.viewIntent(Intent.parseUri(viewIntent, Intent.URI_INTENT_SCHEME));
            } else if (!TextUtils.isEmpty(detailsUri)) {
                builder.viewIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(detailsUri)));
            }
        } catch (URISyntaxException ignored) {
        }

        return builder.build();
    }

    /**
     * Serializes this artwork object to a {@link ContentValues} representation.
     */
    public ContentValues toContentValues() {
        ContentValues values = new ContentValues();
        values.put(MuzeiContract.Artwork.COLUMN_NAME_IMAGE_URI, (mImageUri != null)
                ? mImageUri.toString() : null);
        values.put(MuzeiContract.Artwork.COLUMN_NAME_TITLE, mTitle);
        values.put(MuzeiContract.Artwork.COLUMN_NAME_BYLINE, mByline);
        values.put(MuzeiContract.Artwork.COLUMN_NAME_TOKEN, mToken);
        values.put(MuzeiContract.Artwork.COLUMN_NAME_VIEW_INTENT, (mViewIntent != null)
                ? mViewIntent.toUri(Intent.URI_INTENT_SCHEME) : null);
        return values;
    }


    /**
     * Deserializes an artwork object from a {@link Cursor}.
     */
    public static Artwork fromCursor(Cursor cursor) {
        Builder builder = new Builder();
        int imageUriColumnIndex = cursor.getColumnIndex(MuzeiContract.Artwork.COLUMN_NAME_IMAGE_URI);
        if (imageUriColumnIndex != -1) {
            builder.imageUri(Uri.parse(cursor.getString(imageUriColumnIndex)));
        }
        int titleColumnIndex = cursor.getColumnIndex(MuzeiContract.Artwork.COLUMN_NAME_TITLE);
        if (titleColumnIndex != -1) {
            builder.title(cursor.getString(titleColumnIndex));
        }
        int bylineColumnIndex = cursor.getColumnIndex(MuzeiContract.Artwork.COLUMN_NAME_BYLINE);
        if (bylineColumnIndex != -1) {
            builder.byline(cursor.getString(bylineColumnIndex));
        }
        int tokenColumnIndex = cursor.getColumnIndex(MuzeiContract.Artwork.COLUMN_NAME_TOKEN);
        if (tokenColumnIndex != -1) {
            builder.token(cursor.getString(tokenColumnIndex));
        }
        int viewIntentColumnIndex = cursor.getColumnIndex(MuzeiContract.Artwork.COLUMN_NAME_VIEW_INTENT);
        if (viewIntentColumnIndex != -1) {
            try {
                String viewIntent = cursor.getString(viewIntentColumnIndex);
                if (!TextUtils.isEmpty(viewIntent)) {
                    builder.viewIntent(Intent.parseUri(viewIntent, Intent.URI_INTENT_SCHEME));
                }
            } catch (URISyntaxException ignored) {
            }
        }
        return builder.build();
    }
}




Java Source Code List

com.example.muzei.examplecontractwidget.ArtworkUpdateReceiver.java
com.example.muzei.examplecontractwidget.ArtworkUpdateService.java
com.example.muzei.examplecontractwidget.MuzeiAppWidgetProvider.java
com.example.muzei.examplesource500px.Config.java
com.example.muzei.examplesource500px.FiveHundredPxExampleArtSource.java
com.example.muzei.examplesource500px.FiveHundredPxService.java
com.example.muzei.watchface.ArtworkImageLoader.java
com.example.muzei.watchface.MuzeiExampleWatchface.java
com.example.muzei.watchface.WatchfaceArtworkImageLoader.java
com.google.android.apps.muzei.ActivateMuzeiIntentService.java
com.google.android.apps.muzei.ArtDetailViewport.java
com.google.android.apps.muzei.ArtworkCacheIntentService.java
com.google.android.apps.muzei.ArtworkCache.java
com.google.android.apps.muzei.FullScreenActivity.java
com.google.android.apps.muzei.LockScreenVisibleReceiver.java
com.google.android.apps.muzei.MuzeiActivity.java
com.google.android.apps.muzei.MuzeiApplication.java
com.google.android.apps.muzei.MuzeiWallpaperService.java
com.google.android.apps.muzei.MuzeiWatchFace.java
com.google.android.apps.muzei.MuzeiWearableListenerService.java
com.google.android.apps.muzei.MuzeiWearableListenerService.java
com.google.android.apps.muzei.NetworkChangeReceiver.java
com.google.android.apps.muzei.NewWallpaperNotificationReceiver.java
com.google.android.apps.muzei.PhotoSetAsTargetActivity.java
com.google.android.apps.muzei.SourceManager.java
com.google.android.apps.muzei.SourcePackageChangeReceiver.java
com.google.android.apps.muzei.SourceSubscriberService.java
com.google.android.apps.muzei.TaskQueueService.java
com.google.android.apps.muzei.WearableController.java
com.google.android.apps.muzei.api.Artwork.java
com.google.android.apps.muzei.api.MuzeiArtSource.java
com.google.android.apps.muzei.api.MuzeiContract.java
com.google.android.apps.muzei.api.RemoteMuzeiArtSource.java
com.google.android.apps.muzei.api.UserCommand.java
com.google.android.apps.muzei.api.internal.ProtocolConstants.java
com.google.android.apps.muzei.api.internal.SourceState.java
com.google.android.apps.muzei.event.ArtDetailOpenedClosedEvent.java
com.google.android.apps.muzei.event.ArtworkLoadingStateChangedEvent.java
com.google.android.apps.muzei.event.ArtworkSizeChangedEvent.java
com.google.android.apps.muzei.event.BlurAmountChangedEvent.java
com.google.android.apps.muzei.event.CurrentArtworkDownloadedEvent.java
com.google.android.apps.muzei.event.DimAmountChangedEvent.java
com.google.android.apps.muzei.event.GainedNetworkConnectivityEvent.java
com.google.android.apps.muzei.event.GalleryChosenUrisChangedEvent.java
com.google.android.apps.muzei.event.GreyAmountChangedEvent.java
com.google.android.apps.muzei.event.LockScreenVisibleChangedEvent.java
com.google.android.apps.muzei.event.SelectedSourceChangedEvent.java
com.google.android.apps.muzei.event.SelectedSourceStateChangedEvent.java
com.google.android.apps.muzei.event.SwitchingPhotosStateChangedEvent.java
com.google.android.apps.muzei.event.WallpaperActiveStateChangedEvent.java
com.google.android.apps.muzei.event.WallpaperSizeChangedEvent.java
com.google.android.apps.muzei.featuredart.FeaturedArtSource.java
com.google.android.apps.muzei.gallery.GalleryArtSource.java
com.google.android.apps.muzei.gallery.GalleryDatabase.java
com.google.android.apps.muzei.gallery.GalleryEmptyStateGraphicView.java
com.google.android.apps.muzei.gallery.GallerySettingsActivity.java
com.google.android.apps.muzei.gallery.GalleryStore.java
com.google.android.apps.muzei.provider.MuzeiProvider.java
com.google.android.apps.muzei.render.BitmapRegionLoader.java
com.google.android.apps.muzei.render.DemoRenderController.java
com.google.android.apps.muzei.render.GLColorOverlay.java
com.google.android.apps.muzei.render.GLPicture.java
com.google.android.apps.muzei.render.GLTextureView.java
com.google.android.apps.muzei.render.GLUtil.java
com.google.android.apps.muzei.render.ImageUtil.java
com.google.android.apps.muzei.render.MuzeiBlurRenderer.java
com.google.android.apps.muzei.render.MuzeiRendererFragment.java
com.google.android.apps.muzei.render.RealRenderController.java
com.google.android.apps.muzei.render.RenderController.java
com.google.android.apps.muzei.settings.AboutActivity.java
com.google.android.apps.muzei.settings.Prefs.java
com.google.android.apps.muzei.settings.SettingsActivity.java
com.google.android.apps.muzei.settings.SettingsAdvancedFragment.java
com.google.android.apps.muzei.settings.SettingsChooseSourceFragment.java
com.google.android.apps.muzei.util.AnimatedMuzeiLoadingSpinnerView.java
com.google.android.apps.muzei.util.AnimatedMuzeiLogoFragment.java
com.google.android.apps.muzei.util.AnimatedMuzeiLogoView.java
com.google.android.apps.muzei.util.CheatSheet.java
com.google.android.apps.muzei.util.DrawInsetsFrameLayout.java
com.google.android.apps.muzei.util.IOUtil.java
com.google.android.apps.muzei.util.ImageBlurrer.java
com.google.android.apps.muzei.util.LogUtil.java
com.google.android.apps.muzei.util.LogoPaths.java
com.google.android.apps.muzei.util.MathUtil.java
com.google.android.apps.muzei.util.MultiSelectionController.java
com.google.android.apps.muzei.util.ObservableHorizontalScrollView.java
com.google.android.apps.muzei.util.PanScaleProxyView.java
com.google.android.apps.muzei.util.PanView.java
com.google.android.apps.muzei.util.ScrimUtil.java
com.google.android.apps.muzei.util.Scrollbar.java
com.google.android.apps.muzei.util.SelectionBuilder.java
com.google.android.apps.muzei.util.ShadowDipsTextView.java
com.google.android.apps.muzei.util.SvgPathParser.java
com.google.android.apps.muzei.util.TickingFloatAnimator.java
com.google.android.apps.muzei.util.TypefaceUtil.java
com.google.android.apps.muzei.util.Zoomer.java
net.rbgrn.android.glwallpaperservice.BaseConfigChooser.java
net.rbgrn.android.glwallpaperservice.GLWallpaperService.java