From eb805f8f22933995a6d43834615c3d6bbcd1b89a Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Tue, 26 Jan 2016 22:05:07 +0800 Subject: [PATCH] fixed external player crash #293 --- .../twidere/model/ParcelableCardEntity.java | 16 ++++++++-------- .../twidere/model/ParcelableMedia.java | 7 ++++++- .../activity/support/MediaViewerActivity.java | 5 +++-- .../support/card/CardBrowserFragment.java | 6 +++++- .../util/TwitterCardFragmentFactory.java | 5 +++-- .../twidere/util/TwitterCardUtils.java | 17 ++++++++++++++--- 6 files changed, 39 insertions(+), 17 deletions(-) diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableCardEntity.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableCardEntity.java index 763f007d8..88bb520dd 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableCardEntity.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableCardEntity.java @@ -32,12 +32,12 @@ import com.hannesdorfmann.parcelableplease.annotation.Bagger; import com.hannesdorfmann.parcelableplease.annotation.ParcelablePlease; import com.hannesdorfmann.parcelableplease.annotation.ParcelableThisPlease; +import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.math.NumberUtils; import org.apache.commons.lang3.time.DateFormatUtils; import org.mariotaku.twidere.api.twitter.model.CardEntity; import java.text.ParseException; -import java.util.Arrays; import java.util.Date; import java.util.Map; @@ -142,13 +142,13 @@ public final class ParcelableCardEntity implements Parcelable { @Override public String toString() { - return "ParcelableCardEntity{" + - "account_id=" + account_id + - ", name='" + name + '\'' + - ", url='" + url + '\'' + - ", users=" + Arrays.toString(users) + - ", values=" + values + - '}'; + return new ToStringBuilder(this) + .append("account_id", account_id) + .append("name", name) + .append("url", url) + .append("users", users) + .append("values", values) + .toString(); } @Override diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableMedia.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableMedia.java index 4030209c6..a666dbbd6 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableMedia.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableMedia.java @@ -181,7 +181,8 @@ public class ParcelableMedia implements Parcelable { final ParcelableMedia media = new ParcelableMedia(); final BindingValue playerStreamUrl = card.getBindingValue("player_stream_url"); media.card = ParcelableCardEntity.fromCardEntity(card, -1); - media.url = card.getUrl(); + StringValue appUrlResolved = (StringValue) card.getBindingValue("app_url_resolved"); + media.url = appUrlResolved != null ? appUrlResolved.getValue() : card.getUrl(); if ("animated_gif".equals(name)) { media.media_url = ((StringValue) playerStreamUrl).getValue(); media.type = Type.TYPE_CARD_ANIMATED_GIF; @@ -189,6 +190,10 @@ public class ParcelableMedia implements Parcelable { media.media_url = ((StringValue) playerStreamUrl).getValue(); media.type = Type.TYPE_VIDEO; } else { + StringValue playerUrl = (StringValue) card.getBindingValue("player_url"); + if (playerUrl != null) { + media.media_url = playerUrl.getValue(); + } media.type = Type.TYPE_EXTERNAL_PLAYER; } final BindingValue playerImage = card.getBindingValue("player_image"); diff --git a/twidere/src/main/java/org/mariotaku/twidere/activity/support/MediaViewerActivity.java b/twidere/src/main/java/org/mariotaku/twidere/activity/support/MediaViewerActivity.java index b09e36efd..fb1633a89 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/activity/support/MediaViewerActivity.java +++ b/twidere/src/main/java/org/mariotaku/twidere/activity/support/MediaViewerActivity.java @@ -61,6 +61,7 @@ import org.mariotaku.twidere.R; import org.mariotaku.twidere.adapter.support.SupportFixedFragmentStatePagerAdapter; import org.mariotaku.twidere.fragment.support.CacheDownloadFragment; import org.mariotaku.twidere.fragment.support.ViewStatusDialogFragment; +import org.mariotaku.twidere.fragment.support.card.CardBrowserFragment; import org.mariotaku.twidere.loader.support.CacheDownloadLoader.Listener; import org.mariotaku.twidere.loader.support.CacheDownloadLoader.Result; import org.mariotaku.twidere.model.ParcelableMedia; @@ -70,7 +71,6 @@ import org.mariotaku.twidere.util.IntentUtils; import org.mariotaku.twidere.util.KeyboardShortcutsHandler; import org.mariotaku.twidere.util.MenuUtils; import org.mariotaku.twidere.util.ThemeUtils; -import org.mariotaku.twidere.util.TwitterCardFragmentFactory; import org.mariotaku.twidere.util.Utils; import java.util.concurrent.TimeUnit; @@ -424,6 +424,7 @@ public final class MediaViewerActivity extends BaseAppCompatActivity implements return mMedia.length; } + @NonNull @Override public Fragment getItem(int position) { final ParcelableMedia media = mMedia[position]; @@ -446,7 +447,7 @@ public final class MediaViewerActivity extends BaseAppCompatActivity implements return Fragment.instantiate(mActivity, ImagePageFragment.class.getName(), args); } case ParcelableMedia.Type.TYPE_EXTERNAL_PLAYER: { - return TwitterCardFragmentFactory.createGenericPlayerFragment(media.card); + return CardBrowserFragment.show(media.media_url != null ? media.media_url : media.url, args); } } return new UnsupportedPageFragment(); diff --git a/twidere/src/main/java/org/mariotaku/twidere/fragment/support/card/CardBrowserFragment.java b/twidere/src/main/java/org/mariotaku/twidere/fragment/support/card/CardBrowserFragment.java index 350c323c4..87d52066f 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/fragment/support/card/CardBrowserFragment.java +++ b/twidere/src/main/java/org/mariotaku/twidere/fragment/support/card/CardBrowserFragment.java @@ -21,6 +21,7 @@ package org.mariotaku.twidere.fragment.support.card; import android.os.Bundle; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.webkit.WebSettings; import android.webkit.WebView; @@ -38,9 +39,12 @@ public class CardBrowserFragment extends SupportBrowserFragment { settings.setBuiltInZoomControls(false); } - public static CardBrowserFragment show(@NonNull String uri) { + public static CardBrowserFragment show(@NonNull String uri, @Nullable Bundle extraArgs) { final Bundle args = new Bundle(); args.putString(EXTRA_URI, uri); + if (extraArgs != null) { + args.putAll(extraArgs); + } final CardBrowserFragment fragment = new CardBrowserFragment(); fragment.setArguments(args); return fragment; diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/TwitterCardFragmentFactory.java b/twidere/src/main/java/org/mariotaku/twidere/util/TwitterCardFragmentFactory.java index 237c2af7a..3743825ea 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/TwitterCardFragmentFactory.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/TwitterCardFragmentFactory.java @@ -19,6 +19,7 @@ package org.mariotaku.twidere.util; +import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; @@ -43,11 +44,11 @@ public abstract class TwitterCardFragmentFactory { } @Nullable - public static Fragment createGenericPlayerFragment(@Nullable ParcelableCardEntity card) { + public static Fragment createGenericPlayerFragment(@Nullable ParcelableCardEntity card, Bundle args) { if (card == null) return null; final String playerUrl = card.getString("player_url"); if (playerUrl == null) return null; - return CardBrowserFragment.show(playerUrl); + return CardBrowserFragment.show(playerUrl, args); } public static Fragment createCardPollFragment(ParcelableStatus status) { diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/TwitterCardUtils.java b/twidere/src/main/java/org/mariotaku/twidere/util/TwitterCardUtils.java index 3f481978f..589319d83 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/TwitterCardUtils.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/TwitterCardUtils.java @@ -24,8 +24,10 @@ import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.text.TextUtils; +import org.apache.commons.lang3.ArrayUtils; import org.mariotaku.twidere.fragment.support.card.CardPollFragment; import org.mariotaku.twidere.model.ParcelableCardEntity; +import org.mariotaku.twidere.model.ParcelableMedia; import org.mariotaku.twidere.model.ParcelableStatus; /** @@ -46,15 +48,15 @@ public class TwitterCardUtils { if (CARD_NAME_PLAYER.equals(card.name)) { final Fragment playerFragment = sFactory.createPlayerFragment(card); if (playerFragment != null) return playerFragment; - return TwitterCardFragmentFactory.createGenericPlayerFragment(card); + return TwitterCardFragmentFactory.createGenericPlayerFragment(card, null); } else if (CARD_NAME_AUDIO.equals(card.name)) { final Fragment playerFragment = sFactory.createAudioFragment(card); if (playerFragment != null) return playerFragment; - return TwitterCardFragmentFactory.createGenericPlayerFragment(card); + return TwitterCardFragmentFactory.createGenericPlayerFragment(card, null); } else if (CARD_NAME_ANIMATED_GIF.equals(card.name)) { final Fragment playerFragment = sFactory.createAnimatedGifFragment(card); if (playerFragment != null) return playerFragment; - return TwitterCardFragmentFactory.createGenericPlayerFragment(card); + return TwitterCardFragmentFactory.createGenericPlayerFragment(card, null); } else if (CardPollFragment.isPoll(card)) { return TwitterCardFragmentFactory.createCardPollFragment(status); } @@ -75,6 +77,15 @@ public class TwitterCardUtils { if (status.card == null || status.card_name == null) return false; switch (status.card_name) { case CARD_NAME_PLAYER: { + if (!ArrayUtils.isEmpty(status.media)) { + String appUrlResolved = status.card.getString("app_url_resolved"); + String cardUrl = status.card.url; + for (ParcelableMedia media : status.media) { + if (media.url.equals(appUrlResolved) || media.url.equals(cardUrl)) { + return false; + } + } + } return TextUtils.isEmpty(status.card.getString("player_stream_url")); } case CARD_NAME_AUDIO: {