diff options
author | Chris Warrington <cmw@google.com> | 2016-10-18 12:29:21 +0100 |
---|---|---|
committer | Chris Warrington <cmw@google.com> | 2016-10-18 12:34:18 +0100 |
commit | e3780081075c01aa1dff6d1f373cb43192b33e68 (patch) | |
tree | fb734615933a39f3d009210dc0d1457160479b35 /WordPress/src/main/java/org/wordpress/android/ui/notifications/blocks/NoteBlock.java | |
parent | 7e05eb7e57827eddc885570bc00aed8a50320dbf (diff) | |
parent | 025b8b226c8d8edba2b309ca878572f40512eca7 (diff) | |
download | gradle-perf-android-medium-e3780081075c01aa1dff6d1f373cb43192b33e68.tar.gz |
Merge remote-tracking branch 'origin/upstream-master' into masterHEADstudio-3.4.0studio-3.2.1studio-3.1.2studio-3.0studio-2.3gradle_3.4.0gradle_3.1.2gradle_3.0.0gradle_2.3.0studio-master-devmirror-goog-studio-master-devmastermain
Change-Id: I63f5e16d09297c48432192761b840310935eb903
Diffstat (limited to 'WordPress/src/main/java/org/wordpress/android/ui/notifications/blocks/NoteBlock.java')
-rw-r--r-- | WordPress/src/main/java/org/wordpress/android/ui/notifications/blocks/NoteBlock.java | 275 |
1 files changed, 275 insertions, 0 deletions
diff --git a/WordPress/src/main/java/org/wordpress/android/ui/notifications/blocks/NoteBlock.java b/WordPress/src/main/java/org/wordpress/android/ui/notifications/blocks/NoteBlock.java new file mode 100644 index 000000000..d1f56a419 --- /dev/null +++ b/WordPress/src/main/java/org/wordpress/android/ui/notifications/blocks/NoteBlock.java @@ -0,0 +1,275 @@ +package org.wordpress.android.ui.notifications.blocks; + +import android.media.MediaPlayer; +import android.net.Uri; +import android.text.Spannable; +import android.text.TextUtils; +import android.view.Gravity; +import android.view.View; +import android.view.ViewGroup; +import android.view.animation.Animation; +import android.view.animation.AnimationUtils; +import android.widget.FrameLayout; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.MediaController; +import android.widget.VideoView; + +import com.android.volley.VolleyError; +import com.android.volley.toolbox.ImageLoader; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.wordpress.android.R; +import org.wordpress.android.WordPress; +import org.wordpress.android.ui.notifications.utils.NotificationsUtils; +import org.wordpress.android.util.DisplayUtils; +import org.wordpress.android.util.JSONUtils; +import org.wordpress.android.widgets.WPTextView; + +/** + * A block of data displayed in a notification. + * This basic block can support a media item (image/video) and/or text. + */ +public class NoteBlock { + + private static final String PROPERTY_MEDIA_TYPE = "type"; + private static final String PROPERTY_MEDIA_URL = "url"; + + private final JSONObject mNoteData; + private final OnNoteBlockTextClickListener mOnNoteBlockTextClickListener; + private JSONObject mMediaItem; + private boolean mIsBadge; + private boolean mHasAnimatedBadge; + private int mBackgroundColor; + + public interface OnNoteBlockTextClickListener { + void onNoteBlockTextClicked(NoteBlockClickableSpan clickedSpan); + void showDetailForNoteIds(); + void showReaderPostComments(); + void showSitePreview(long siteId, String siteUrl); + } + + public NoteBlock(JSONObject noteObject, OnNoteBlockTextClickListener onNoteBlockTextClickListener) { + mNoteData = noteObject; + mOnNoteBlockTextClickListener = onNoteBlockTextClickListener; + } + + OnNoteBlockTextClickListener getOnNoteBlockTextClickListener() { + return mOnNoteBlockTextClickListener; + } + + public BlockType getBlockType() { + return BlockType.BASIC; + } + + JSONObject getNoteData() { + return mNoteData; + } + + public Spannable getNoteText() { + return NotificationsUtils.getSpannableContentForRanges(mNoteData, null, + mOnNoteBlockTextClickListener, false); + } + + public String getMetaHomeTitle() { + return JSONUtils.queryJSON(mNoteData, "meta.titles.home", ""); + } + + public long getMetaSiteId() { + return JSONUtils.queryJSON(mNoteData, "meta.ids.site", -1); + } + + public String getMetaSiteUrl() { + return JSONUtils.queryJSON(mNoteData, "meta.links.home", ""); + } + + JSONObject getNoteMediaItem() { + if (mMediaItem == null) { + mMediaItem = JSONUtils.queryJSON(mNoteData, "media[0]", new JSONObject()); + } + + return mMediaItem; + } + + public void setIsBadge() { + mIsBadge = true; + } + + public void setBackgroundColor(int backgroundColor) { + mBackgroundColor = backgroundColor; + } + + public int getLayoutResourceId() { + return R.layout.note_block_basic; + } + + private boolean hasMediaArray() { + return mNoteData.has("media"); + } + + boolean hasImageMediaItem() { + String mediaType = getNoteMediaItem().optString(PROPERTY_MEDIA_TYPE, ""); + return hasMediaArray() && + (mediaType.startsWith("image") || mediaType.equals("badge")) && + getNoteMediaItem().has(PROPERTY_MEDIA_URL); + } + + boolean hasVideoMediaItem() { + return hasMediaArray() && + getNoteMediaItem().optString(PROPERTY_MEDIA_TYPE, "").startsWith("video") && + getNoteMediaItem().has(PROPERTY_MEDIA_URL); + } + + public boolean containsBadgeMediaType() { + try { + JSONArray mediaArray = mNoteData.getJSONArray("media"); + for (int i=0; i < mediaArray.length(); i++) { + JSONObject mediaObject = mediaArray.getJSONObject(i); + if (mediaObject.optString(PROPERTY_MEDIA_TYPE, "").equals("badge")) { + return true; + } + } + } catch (JSONException e) { + return false; + } + + return false; + } + + public View configureView(final View view) { + final BasicNoteBlockHolder noteBlockHolder = (BasicNoteBlockHolder)view.getTag(); + + // Note image + if (hasImageMediaItem()) { + // Request image, and animate it when loaded + noteBlockHolder.getImageView().setVisibility(View.VISIBLE); + WordPress.imageLoader.get(getNoteMediaItem().optString("url", ""), new ImageLoader.ImageListener() { + @Override + public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) { + if (!mHasAnimatedBadge && response.getBitmap() != null && view.getContext() != null) { + mHasAnimatedBadge = true; + noteBlockHolder.getImageView().setImageBitmap(response.getBitmap()); + Animation pop = AnimationUtils.loadAnimation(view.getContext(), R.anim.pop); + noteBlockHolder.getImageView().startAnimation(pop); + noteBlockHolder.getImageView().setVisibility(View.VISIBLE); + } + } + + @Override + public void onErrorResponse(VolleyError volleyError) { + noteBlockHolder.hideImageView(); + } + }); + } else { + noteBlockHolder.hideImageView(); + } + + // Note video + if (hasVideoMediaItem()) { + noteBlockHolder.getVideoView().setVideoURI(Uri.parse(getNoteMediaItem().optString("url", ""))); + noteBlockHolder.getVideoView().setVisibility(View.VISIBLE); + } else { + noteBlockHolder.hideVideoView(); + } + + // Note text + if (!TextUtils.isEmpty(getNoteText())) { + if (mIsBadge) { + LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT); + params.gravity = Gravity.CENTER_HORIZONTAL; + noteBlockHolder.getTextView().setLayoutParams(params); + noteBlockHolder.getTextView().setGravity(Gravity.CENTER_HORIZONTAL); + noteBlockHolder.getTextView().setPadding(0, DisplayUtils.dpToPx(view.getContext(), 8), 0, 0); + } else { + noteBlockHolder.getTextView().setGravity(Gravity.NO_GRAVITY); + noteBlockHolder.getTextView().setPadding(0, 0, 0, 0); + } + noteBlockHolder.getTextView().setText(getNoteText()); + noteBlockHolder.getTextView().setVisibility(View.VISIBLE); + } else { + noteBlockHolder.getTextView().setVisibility(View.GONE); + } + + view.setBackgroundColor(mBackgroundColor); + + return view; + } + + public Object getViewHolder(View view) { + return new BasicNoteBlockHolder(view); + } + + static class BasicNoteBlockHolder { + private final LinearLayout mRootLayout; + private final WPTextView mTextView; + + private ImageView mImageView; + private VideoView mVideoView; + + BasicNoteBlockHolder(View view) { + mRootLayout = (LinearLayout)view; + mTextView = (WPTextView) view.findViewById(R.id.note_text); + mTextView.setMovementMethod(new NoteBlockLinkMovementMethod()); + } + + public WPTextView getTextView() { + return mTextView; + } + + public ImageView getImageView() { + if (mImageView == null) { + mImageView = new ImageView(mRootLayout.getContext()); + int imageSize = DisplayUtils.dpToPx(mRootLayout.getContext(), 180); + int imagePadding = mRootLayout.getContext().getResources().getDimensionPixelSize(R.dimen.margin_large); + LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(imageSize, imageSize); + layoutParams.gravity = Gravity.CENTER_HORIZONTAL; + mImageView.setLayoutParams(layoutParams); + mImageView.setPadding(0, imagePadding, 0, 0); + mRootLayout.addView(mImageView, 0); + } + + return mImageView; + } + + public VideoView getVideoView() { + if (mVideoView == null) { + mVideoView = new VideoView(mRootLayout.getContext()); + FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, + DisplayUtils.dpToPx(mRootLayout.getContext(), 220)); + mVideoView.setLayoutParams(layoutParams); + mRootLayout.addView(mVideoView, 0); + + // Attach a mediaController if we are displaying a video. + final MediaController mediaController = new MediaController(mRootLayout.getContext()); + mediaController.setMediaPlayer(mVideoView); + + mVideoView.setMediaController(mediaController); + mediaController.requestFocus(); + mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { + + @Override + public void onPrepared(MediaPlayer mp) { + // Show the media controls when the video is ready to be played. + mediaController.show(0); + } + }); + } + + return mVideoView; + } + + public void hideImageView() { + if (mImageView != null) { + mImageView.setVisibility(View.GONE); + } + } + + public void hideVideoView() { + if (mVideoView != null) { + mVideoView.setVisibility(View.GONE); + } + } + } +} |