From fd7b9263bf48e6456c3ab60b4c0c9ecda9b64a79 Mon Sep 17 00:00:00 2001 From: chenjuntao Date: Fri, 13 Dec 2019 15:23:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=B8=8B=E8=BD=BD=E6=8C=89?= =?UTF-8?q?=E9=92=AE=E6=94=AF=E6=8C=81=E6=89=93=E5=BC=80=20H5=20=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=20https://gitlab.ghzs.com/pm/halo-app-issues/issues/7?= =?UTF-8?q?27?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gh/common/databind/BindingAdapters.java | 40 ++- .../gh/common/util/DetailDownloadUtils.java | 23 +- .../com/gh/common/util/DownloadItemUtils.java | 59 ++-- .../gh/common/view/DownloadProgressBar.java | 10 +- .../java/com/gh/gamecenter/WebActivity.java | 24 +- .../adapter/viewholder/DetailViewHolder.java | 8 + .../com/gh/gamecenter/entity/GameEntity.kt | 2 + .../gamedetail/rating/RatingReplyAdapter.kt | 26 +- .../halo/assistant/fragment/WebFragment.java | 253 ++++++++++-------- 9 files changed, 288 insertions(+), 157 deletions(-) diff --git a/app/src/main/java/com/gh/common/databind/BindingAdapters.java b/app/src/main/java/com/gh/common/databind/BindingAdapters.java index ac600affe0..a3d1d0ac37 100644 --- a/app/src/main/java/com/gh/common/databind/BindingAdapters.java +++ b/app/src/main/java/com/gh/common/databind/BindingAdapters.java @@ -13,12 +13,6 @@ import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; -import androidx.annotation.Nullable; -import androidx.appcompat.app.AppCompatActivity; -import androidx.core.content.ContextCompat; -import androidx.databinding.BindingAdapter; -import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; - import com.facebook.drawee.view.SimpleDraweeView; import com.gh.base.OnViewClickListener; import com.gh.common.constant.Config; @@ -34,6 +28,7 @@ import com.gh.common.util.DownloadDialogHelper; import com.gh.common.util.GameUtils; import com.gh.common.util.GameViewUtils; import com.gh.common.util.ImageUtils; +import com.gh.common.util.MtaHelper; import com.gh.common.util.NewsUtils; import com.gh.common.util.NumberUtils; import com.gh.common.util.PackageUtils; @@ -46,11 +41,13 @@ import com.gh.common.view.DrawableView; import com.gh.download.DownloadManager; import com.gh.gamecenter.DownloadManagerActivity; import com.gh.gamecenter.R; +import com.gh.gamecenter.WebActivity; import com.gh.gamecenter.baselist.LoadStatus; import com.gh.gamecenter.databinding.KaifuAddItemBinding; import com.gh.gamecenter.databinding.KaifuDetailItemRowBinding; import com.gh.gamecenter.entity.ApkEntity; import com.gh.gamecenter.entity.GameEntity; +import com.gh.gamecenter.entity.LinkEntity; import com.gh.gamecenter.entity.PluginLocation; import com.gh.gamecenter.entity.ServerCalendarEntity; import com.gh.gamecenter.entity.TagStyleEntity; @@ -67,6 +64,12 @@ import java.text.SimpleDateFormat; import java.util.List; import java.util.Locale; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.content.ContextCompat; +import androidx.databinding.BindingAdapter; +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; + /** * Created by khy on 12/02/18. */ @@ -410,6 +413,12 @@ public class BindingAdapters { }); } break; + case H5_GAME: + MtaHelper.onEvent("H5页面", "入口", "列表页_" + gameEntity.getName()); + LinkEntity linkEntity = gameEntity.getH5Link(); + Intent i = new Intent(WebActivity.getIntentForWebGame(progressBar.getContext(), linkEntity.getLink(), gameEntity.getName(), "play".equals(linkEntity.getType()))); + progressBar.getContext().startActivity(i); + break; } }); @@ -427,13 +436,24 @@ public class BindingAdapters { // 显示下载按钮状态 if (gameEntity.getApk().isEmpty() || gameEntity.getDownloadOffStatus() != null) { + LinkEntity h5LinkEntity = gameEntity.getH5Link(); String offStatus = gameEntity.getDownloadOffStatus(); - if (offStatus != null && "dialog".equals(offStatus)) { - progressBar.setText("查看"); + if (h5LinkEntity != null) { + if ("play".equals(h5LinkEntity.getType())) { + progressBar.setText("开始玩"); + } else { + progressBar.setText("查看"); + } + progressBar.setDownloadType(DownloadProgressBar.DownloadType.H5_GAME); } else { - progressBar.setText("暂无"); + if (offStatus != null && "dialog".equals(offStatus)) { + progressBar.setText("查看"); + } else { + progressBar.setText("暂无"); + } + progressBar.setDownloadType(DownloadProgressBar.DownloadType.NONE); } - progressBar.setDownloadType(DownloadProgressBar.DownloadType.NONE); + } else { String status = GameUtils.getDownloadBtnText(progressBar.getContext(), gameEntity, PluginLocation.only_game); switch (status) { diff --git a/app/src/main/java/com/gh/common/util/DetailDownloadUtils.java b/app/src/main/java/com/gh/common/util/DetailDownloadUtils.java index aaf0745499..213215a040 100644 --- a/app/src/main/java/com/gh/common/util/DetailDownloadUtils.java +++ b/app/src/main/java/com/gh/common/util/DetailDownloadUtils.java @@ -9,6 +9,7 @@ import com.gh.common.view.DownloadProgressBar; import com.gh.download.DownloadManager; import com.gh.gamecenter.R; import com.gh.gamecenter.adapter.viewholder.DetailViewHolder; +import com.gh.gamecenter.entity.LinkEntity; import com.gh.gamecenter.entity.PluginLocation; import com.gh.gamecenter.manager.PackagesManager; import com.lightgame.download.DownloadEntity; @@ -47,12 +48,24 @@ public class DetailDownloadUtils { } if (viewHolder.gameEntity.getApk().isEmpty() || viewHolder.gameEntity.getDownloadOffStatus() != null) { - if ("dialog".equals(viewHolder.gameEntity.getDownloadOffStatus())) { - viewHolder.mDownloadPb.setText(TextUtils.isEmpty(viewHolder.downloadOffText) ? "查看详情" : viewHolder.downloadOffText); - viewHolder.mDownloadPb.setDownloadType(DownloadProgressBar.DownloadType.NONE_WITH_HINT); + LinkEntity h5LinkEntity = viewHolder.gameEntity.getH5Link(); + + if (h5LinkEntity != null) { + if ("play".equals(h5LinkEntity.getType())) { + String defaultString = String.format("开始玩" + "《%s》", viewHolder.gameEntity.getName()); + viewHolder.mDownloadPb.setText(TextUtils.isEmpty(h5LinkEntity.getText()) ? defaultString : h5LinkEntity.getText()); + } else { + viewHolder.mDownloadPb.setText(TextUtils.isEmpty(h5LinkEntity.getText()) ? "查看" : h5LinkEntity.getText()); + } + viewHolder.mDownloadPb.setDownloadType(DownloadProgressBar.DownloadType.H5_GAME); } else { - viewHolder.mDownloadPb.setText(TextUtils.isEmpty(viewHolder.downloadOffText) ? "暂无下载" : viewHolder.downloadOffText); - viewHolder.mDownloadPb.setDownloadType(DownloadProgressBar.DownloadType.NONE); + if ("dialog".equals(viewHolder.gameEntity.getDownloadOffStatus())) { + viewHolder.mDownloadPb.setText(TextUtils.isEmpty(viewHolder.downloadOffText) ? "查看详情" : viewHolder.downloadOffText); + viewHolder.mDownloadPb.setDownloadType(DownloadProgressBar.DownloadType.NONE_WITH_HINT); + } else { + viewHolder.mDownloadPb.setText(TextUtils.isEmpty(viewHolder.downloadOffText) ? "暂无下载" : viewHolder.downloadOffText); + viewHolder.mDownloadPb.setDownloadType(DownloadProgressBar.DownloadType.NONE); + } } } else { String status = GameUtils.getDownloadBtnText(viewHolder.context, viewHolder.gameEntity, PluginLocation.only_game); diff --git a/app/src/main/java/com/gh/common/util/DownloadItemUtils.java b/app/src/main/java/com/gh/common/util/DownloadItemUtils.java index c8965296f3..e5bc54e41e 100644 --- a/app/src/main/java/com/gh/common/util/DownloadItemUtils.java +++ b/app/src/main/java/com/gh/common/util/DownloadItemUtils.java @@ -1,18 +1,13 @@ package com.gh.common.util; import android.content.Context; +import android.content.Intent; import android.graphics.Color; import android.os.Message; import android.text.TextUtils; import android.view.View; import android.widget.TextView; -import androidx.annotation.Nullable; -import androidx.appcompat.app.AppCompatActivity; -import androidx.collection.ArrayMap; -import androidx.core.content.ContextCompat; -import androidx.recyclerview.widget.RecyclerView; - import com.gh.common.constant.Config; import com.gh.common.dialog.ReserveDialogFragment; import com.gh.common.exposure.ExposureEvent; @@ -22,9 +17,11 @@ import com.gh.common.view.DownloadDialog; import com.gh.download.DownloadManager; import com.gh.gamecenter.DownloadManagerActivity; import com.gh.gamecenter.R; +import com.gh.gamecenter.WebActivity; import com.gh.gamecenter.adapter.viewholder.GameViewHolder; import com.gh.gamecenter.entity.ApkEntity; import com.gh.gamecenter.entity.GameEntity; +import com.gh.gamecenter.entity.LinkEntity; import com.gh.gamecenter.entity.PluginLocation; import com.gh.gamecenter.manager.PackagesManager; import com.lightgame.download.DownloadConfig; @@ -35,6 +32,12 @@ import com.lightgame.utils.Utils; import java.util.concurrent.LinkedBlockingQueue; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; +import androidx.collection.ArrayMap; +import androidx.core.content.ContextCompat; +import androidx.recyclerview.widget.RecyclerView; + public class DownloadItemUtils { // 更新下载进度条 @@ -167,22 +170,34 @@ public class DownloadItemUtils { return; } - if (gameEntity.getApk().isEmpty() || gameEntity.getDownloadOffStatus() != null) { + if (gameEntity.getApk().isEmpty() + || gameEntity.getDownloadOffStatus() != null) { + LinkEntity h5LinkEntity = gameEntity.getH5Link(); + String offStatus = gameEntity.getDownloadOffStatus(); + holder.gameDes.setVisibility(View.VISIBLE); holder.gameProgressbar.setVisibility(View.GONE); holder.gameInfo.setVisibility(View.GONE); - - String offStatus = gameEntity.getDownloadOffStatus(); - if ("dialog".equals(offStatus)) { - holder.gameDownloadBtn.setText("查看"); - holder.gameDownloadBtn.setTextColor(ContextCompat.getColor(context, R.color.white)); - holder.gameDownloadBtn.setBackgroundResource(R.drawable.game_item_btn_download_style); + + if (h5LinkEntity != null) { + if ("play".equals(h5LinkEntity.getType())) { + holder.gameDownloadBtn.setText("开始玩"); + } else { + holder.gameDownloadBtn.setText("查看"); + } + holder.gameDownloadBtn.setClickable(true); } else { - holder.gameDownloadBtn.setText("暂无"); - holder.gameDownloadBtn.setTextColor(ContextCompat.getColor(context, R.color.button_gray)); - holder.gameDownloadBtn.setBackgroundResource(R.drawable.news_detail_comment); + if ("dialog".equals(offStatus)) { + holder.gameDownloadBtn.setText("查看"); + holder.gameDownloadBtn.setTextColor(ContextCompat.getColor(context, R.color.white)); + holder.gameDownloadBtn.setBackgroundResource(R.drawable.game_item_btn_download_style); + } else { + holder.gameDownloadBtn.setText("暂无"); + holder.gameDownloadBtn.setTextColor(ContextCompat.getColor(context, R.color.button_gray)); + holder.gameDownloadBtn.setBackgroundResource(R.drawable.news_detail_comment); + } + holder.gameDownloadBtn.setClickable(false); } - holder.gameDownloadBtn.setClickable(false); } else if (gameEntity.getApk().size() == 1) { updateNormalItem(context, holder, gameEntity, isShowPlatform, pluginLocation); } else { @@ -402,8 +417,14 @@ public class DownloadItemUtils { } return; } - - if (gameEntity.getApk().size() == 1) { + + if (gameEntity.getApk().size() == 0 && gameEntity.getH5Link() != null) { + downloadBtn.setOnClickListener(v -> { + MtaHelper.onEvent("H5页面", "入口", "列表页_" + gameEntity.getName()); + Intent i = WebActivity.getIntentForWebGame(context, gameEntity.getH5Link().getLink(), gameEntity.getName(), true); + context.startActivity(i); + }); + } else if (gameEntity.getApk().size() == 1) { downloadBtn.setOnClickListener(v -> { EmptyCallback clickRunnable = () -> { if (clickCallback != null) { diff --git a/app/src/main/java/com/gh/common/view/DownloadProgressBar.java b/app/src/main/java/com/gh/common/view/DownloadProgressBar.java index 13eb4d1001..562150a926 100644 --- a/app/src/main/java/com/gh/common/view/DownloadProgressBar.java +++ b/app/src/main/java/com/gh/common/view/DownloadProgressBar.java @@ -15,12 +15,12 @@ import android.text.TextUtils; import android.util.AttributeSet; import android.widget.ProgressBar; -import androidx.annotation.StringRes; -import androidx.core.content.ContextCompat; - import com.gh.common.util.DisplayUtils; import com.gh.gamecenter.R; +import androidx.annotation.StringRes; +import androidx.core.content.ContextCompat; + public class DownloadProgressBar extends ProgressBar { public static final int MAX_LENGTH = 1000; public static final int DOWNLOAD_NORMAL_STYLE = 0; @@ -39,7 +39,8 @@ public class DownloadProgressBar extends ProgressBar { DOWNLOADING_NORMAL, DOWNLOADING_PLUGIN, RESERVABLE, - RESERVED + RESERVED, + H5_GAME } private PorterDuffXfermode mDuffXFerMode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN); @@ -165,6 +166,7 @@ public class DownloadProgressBar extends ProgressBar { case NORMAL: case NONE_WITH_HINT: case INSTALL_NORMAL: + case H5_GAME: switch (mDownloadStyle) { case DOWNLOAD_RECT_STYLE: setProgressDrawable(getResources().getDrawable(R.drawable.detail_download_normal_rect_style)); diff --git a/app/src/main/java/com/gh/gamecenter/WebActivity.java b/app/src/main/java/com/gh/gamecenter/WebActivity.java index 321f39a00a..18b3a5872b 100644 --- a/app/src/main/java/com/gh/gamecenter/WebActivity.java +++ b/app/src/main/java/com/gh/gamecenter/WebActivity.java @@ -125,7 +125,7 @@ public class WebActivity extends NormalActivity { url = String.format(Locale.CHINA, "%s?user_id=%s&name=%s&icon=%s×tamp=%d", url, userId, name, URLEncoder.encode(icon), Math.round(new Date().getTime() / 1000)); Bundle bundle = new Bundle(); bundle.putString(EntranceUtils.KEY_URL, url); - bundle.putBoolean(WebFragment.KEY_IS_BADGE_CENTER, true); + bundle.putBoolean(WebFragment.KEY_LEAVE_WEB_PAGE_TO_HANDLE_TITLE, true); return getTargetIntent(context, WebActivity.class, WebFragment.class, bundle); } @@ -154,9 +154,29 @@ public class WebActivity extends NormalActivity { public static Intent getIntentByUrl(Context context, String url) { Bundle bundle = new Bundle(); bundle.putString(EntranceUtils.KEY_URL, url); + return getIntentByUrl(context, url, false); + } + + @NonNull + public static Intent getIntentByUrl(Context context, String url, boolean leaveWebPageHandleTitle) { + Bundle bundle = new Bundle(); + bundle.putString(EntranceUtils.KEY_URL, url); + bundle.putBoolean(WebFragment.KEY_LEAVE_WEB_PAGE_TO_HANDLE_TITLE, leaveWebPageHandleTitle); return getTargetIntent(context, WebActivity.class, WebFragment.class, bundle); } - + + public static Intent getIntentForWebGame(Context context, String url, String gameName, boolean disableWebviewBackStack) { + Bundle bundle = new Bundle(); + bundle.putString(EntranceUtils.KEY_URL, url); + bundle.putString(WebFragment.KEY_GAME_NAME, gameName); + bundle.putBoolean(WebFragment.KEY_LEAVE_WEB_PAGE_TO_HANDLE_TITLE, true); + if (disableWebviewBackStack) { + bundle.putBoolean(WebFragment.KEY_REQUIRE_BACK_CONFIRMATION, true); + bundle.putString(WebFragment.KEY_BACK_CONFIRMATION_CONTENT, "退出后将不保存当前游戏进度,确定退出吗?"); + } + return getTargetIntent(context, WebActivity.class, WebFragment.class, bundle); + } + @Override protected View.OnClickListener provideNavigationItemClickListener() { Bundle bundle = getIntent().getBundleExtra(NORMAL_FRAGMENT_BUNDLE); diff --git a/app/src/main/java/com/gh/gamecenter/adapter/viewholder/DetailViewHolder.java b/app/src/main/java/com/gh/gamecenter/adapter/viewholder/DetailViewHolder.java index b580c0f308..ccfbeaa1db 100644 --- a/app/src/main/java/com/gh/gamecenter/adapter/viewholder/DetailViewHolder.java +++ b/app/src/main/java/com/gh/gamecenter/adapter/viewholder/DetailViewHolder.java @@ -25,8 +25,10 @@ import com.gh.common.view.DownloadProgressBar; import com.gh.download.DownloadManager; import com.gh.gamecenter.DownloadManagerActivity; import com.gh.gamecenter.R; +import com.gh.gamecenter.WebActivity; import com.gh.gamecenter.entity.ApkEntity; import com.gh.gamecenter.entity.GameEntity; +import com.gh.gamecenter.entity.LinkEntity; import com.gh.gamecenter.eventbus.EBReuse; import com.gh.gamecenter.gamedetail.GameDetailFragment; import com.lightgame.download.DownloadEntity; @@ -232,6 +234,12 @@ public class DetailViewHolder { }); } break; + case H5_GAME: + MtaHelper.onEvent("H5页面", "入口", "详情页_" + mGameEntity.getName()); + LinkEntity linkEntity = mGameEntity.getH5Link(); + Intent i = new Intent(WebActivity.getIntentForWebGame(mViewHolder.context, linkEntity.getLink(), mGameEntity.getName(), "play".equals(linkEntity.getType()))); + mViewHolder.context.startActivity(i); + break; } } diff --git a/app/src/main/java/com/gh/gamecenter/entity/GameEntity.kt b/app/src/main/java/com/gh/gamecenter/entity/GameEntity.kt index a567007958..7de932b747 100644 --- a/app/src/main/java/com/gh/gamecenter/entity/GameEntity.kt +++ b/app/src/main/java/com/gh/gamecenter/entity/GameEntity.kt @@ -148,6 +148,8 @@ data class GameEntity( @SerializedName("direct_comment") var directComment: Boolean = false, // xxx人想玩 + @SerializedName("h5_link") + var h5Link: LinkEntity? = null, val visit: Int = 0, @SerializedName("played_time") val playedTime: Long = 0, diff --git a/app/src/main/java/com/gh/gamecenter/gamedetail/rating/RatingReplyAdapter.kt b/app/src/main/java/com/gh/gamecenter/gamedetail/rating/RatingReplyAdapter.kt index 030c5edf72..adf5dc016f 100644 --- a/app/src/main/java/com/gh/gamecenter/gamedetail/rating/RatingReplyAdapter.kt +++ b/app/src/main/java/com/gh/gamecenter/gamedetail/rating/RatingReplyAdapter.kt @@ -171,16 +171,28 @@ class RatingReplyAdapter(context: Context, goneIf(!Config.isShowDownload(game.id) || "光环助手" == game.name) if (game.getApk().size == 0 || game.downloadOffStatus != null) { + val h5LinkEntity = game.h5Link val offStatus = game.downloadOffStatus - if ("dialog" == offStatus) { - text = "查看" - setTextColor(ContextCompat.getColor(mContext, R.color.white)) + if (h5LinkEntity != null) run { + text = if ("play" == h5LinkEntity.type) { + "开始玩" + } else { + "查看" + } + setTextColor(Color.WHITE) + setBackgroundResource(R.drawable.game_item_btn_download_style) + isClickable = true } else { - text = "暂无" - setTextColor(ContextCompat.getColor(mContext, R.color.button_gray)) - setBackgroundResource(R.drawable.news_detail_comment) + if ("dialog" == offStatus) { + text = "查看" + setTextColor(ContextCompat.getColor(mContext, R.color.white)) + } else { + text = "暂无" + setTextColor(ContextCompat.getColor(mContext, R.color.button_gray)) + setBackgroundResource(R.drawable.news_detail_comment) + } + isClickable = false } - isClickable = false } else if (game.getApk().size == 1) { GameUtils.setDownloadBtnStatus(mContext, game, this, PluginLocation.only_game) val downloadEntity = DownloadManager.getInstance(mContext).getDownloadEntityByUrl(game.getApk()[0].url) diff --git a/app/src/main/java/com/halo/assistant/fragment/WebFragment.java b/app/src/main/java/com/halo/assistant/fragment/WebFragment.java index 27b3d0d966..3fcf0d788b 100644 --- a/app/src/main/java/com/halo/assistant/fragment/WebFragment.java +++ b/app/src/main/java/com/halo/assistant/fragment/WebFragment.java @@ -18,11 +18,15 @@ import android.widget.TextView; import com.gh.base.BaseActivity; import com.gh.common.DefaultJsApi; import com.gh.common.DefaultWebViewUrlHandler; +import com.gh.common.TimeElapsedHelper; import com.gh.common.constant.Constants; import com.gh.common.util.CheckLoginUtils; import com.gh.common.util.CollectionUtils; +import com.gh.common.util.DialogHelper; import com.gh.common.util.EntranceUtils; +import com.gh.common.util.MtaHelper; import com.gh.common.util.ShareUtils; +import com.gh.gamecenter.BuildConfig; import com.gh.gamecenter.MessageDetailActivity; import com.gh.gamecenter.R; import com.gh.gamecenter.entity.CommentnumEntity; @@ -50,17 +54,22 @@ import io.reactivex.schedulers.Schedulers; import wendu.dsbridge.DWebView; /** + * TODO 整理这个类 * Created by CsHeng on 18/12/2017. * 网页浏览器页面 */ public class WebFragment extends NormalFragment { - + public static final String KEY_ISTOOLS = "isTools"; public static final String KEY_IS_BIND_WECHAT = "is_bind_wechat"; public static final String KEY_ISCOLLECTIONTOOLS = "isCollectionTools"; - public static final String KEY_IS_BADGE_CENTER = "is_badge_center"; public static final String KEY_IS_SECURITY_CERTIFICATION = "is_security_certification"; - + + public static final String KEY_LEAVE_WEB_PAGE_TO_HANDLE_TITLE = "leave_web_page_to_handle_title"; + public static final String KEY_REQUIRE_BACK_CONFIRMATION = "require_back_confirmation"; + public static final String KEY_BACK_CONFIRMATION_CONTENT = "back_confirmation_content"; + public static final String KEY_GAME_NAME = "game_name"; + @BindView(R.id.news_webview) DWebView mWebView; @BindView(R.id.web_progressbar) @@ -71,141 +80,140 @@ public class WebFragment extends NormalFragment { RelativeLayout newsBottom; MenuItem mMenuShare; MenuItem mMenuCollect; - + private ToolBoxEntity mToolBoxEntity; - + private String newsId; private String mNavigationTitle; - + private int commentNum; - + private boolean mIsTools; private boolean mIsBindWechat; - + private boolean mIsBackStackDisabled; + private String mBackConfirmationContent; + private String mGameName; + + private TimeElapsedHelper mTimeElapsedHelper; + @Override protected int getLayoutId() { return R.layout.fragment_web; } - + @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == MessageDetailFragment.REQUEST_UPDATE_COMMENT && data != null && data.getExtras() != null) { - webComment.setText(getString(R.string.web_newscomment_count, data.getExtras().getInt("commentNum"))); + webComment.setText(getString(R.string.web_newscomment_count, data.getExtras() + .getInt("commentNum"))); } - } - + @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); initMenu(); } - + private void initMenu() { setNavigationTitle(mNavigationTitle); initMenu(R.menu.menu_web); mMenuShare = getItemMenu(R.id.menu_share); mMenuCollect = getItemMenu(R.id.menu_collect); if (mMenuShare == null || mMenuCollect == null) return; - + mMenuShare.setVisible(mIsTools); mMenuCollect.setVisible(mIsTools); - + if (mIsTools) { mToolBoxEntity = getArguments().getParcelable(ToolBoxEntity.TAG); - if (mToolBoxEntity != null && mToolBoxEntity.getMe() != null && mToolBoxEntity.getMe().isToolkitFavorite()) { + if (mToolBoxEntity != null && mToolBoxEntity.getMe() != null && mToolBoxEntity.getMe() + .isToolkitFavorite()) { mMenuCollect.setIcon(R.drawable.menu_ic_collect_select); } else { mMenuCollect.setIcon(R.drawable.menu_ic_collect_unselect); } } - } - + @Override public void onMenuItemClick(MenuItem menuItem) { switch (menuItem.getItemId()) { case R.id.menu_share: if (mMenuShare != null && mToolBoxEntity != null) { if (getActivity() instanceof BaseActivity) { - ((BaseActivity) getActivity()).showShare( - mToolBoxEntity.getUrl(), mToolBoxEntity.getIcon(), - mToolBoxEntity.getName(), mToolBoxEntity.getDes(), - ShareUtils.ShareType.tools); + ((BaseActivity) getActivity()).showShare(mToolBoxEntity.getUrl(), mToolBoxEntity + .getIcon(), mToolBoxEntity.getName(), mToolBoxEntity.getDes(), ShareUtils.ShareType.tools); } } break; case R.id.menu_collect: CheckLoginUtils.checkLogin(getActivity(), "工具详情-收藏", () -> { mMenuCollect.setEnabled(false); - if (mToolBoxEntity.getMe() != null && mToolBoxEntity.getMe().isToolkitFavorite()) { - CollectionUtils.INSTANCE.deleteCollection(getContext(), mToolBoxEntity.getId(), - CollectionUtils.CollectionType.toolkit, - new CollectionUtils.OnCollectionListener() { - @Override - public void onSuccess() { - mToolBoxEntity.getMe().setToolkitFavorite(false); - mMenuCollect.setEnabled(true); - mMenuCollect.setIcon(R.drawable.menu_ic_collect_unselect); - toast(R.string.collection_cancel); - } - - @Override - public void onError() { - mMenuCollect.setEnabled(true); - toast(R.string.collection_cancel_failure); - } - }); - + if (mToolBoxEntity.getMe() != null && mToolBoxEntity.getMe() + .isToolkitFavorite()) { + CollectionUtils.INSTANCE.deleteCollection(getContext(), mToolBoxEntity.getId(), CollectionUtils.CollectionType.toolkit, new CollectionUtils.OnCollectionListener() { + @Override + public void onSuccess() { + mToolBoxEntity.getMe().setToolkitFavorite(false); + mMenuCollect.setEnabled(true); + mMenuCollect.setIcon(R.drawable.menu_ic_collect_unselect); + toast(R.string.collection_cancel); + } + + @Override + public void onError() { + mMenuCollect.setEnabled(true); + toast(R.string.collection_cancel_failure); + } + }); + } else { -// Map map = new HashMap<>(); -// map.put("_id", mToolBoxEntity.getId()); - CollectionUtils.INSTANCE.postCollection(getContext(), mToolBoxEntity.getId(), - CollectionUtils.CollectionType.toolkit, - new CollectionUtils.OnCollectionListener() { - @Override - public void onSuccess() { - MeEntity userData = mToolBoxEntity.getMe(); - if (userData == null) { - userData = new MeEntity(); - mToolBoxEntity.setMe(userData); - } - userData.setToolkitFavorite(true); - mMenuCollect.setEnabled(true); - mMenuCollect.setIcon(R.drawable.menu_ic_collect_select); - toast(R.string.collection_success); - } - - @Override - public void onError() { - mMenuCollect.setEnabled(true); - toast(R.string.collection_failure); - } - }); + CollectionUtils.INSTANCE.postCollection(getContext(), mToolBoxEntity.getId(), CollectionUtils.CollectionType.toolkit, new CollectionUtils.OnCollectionListener() { + @Override + public void onSuccess() { + MeEntity userData = mToolBoxEntity.getMe(); + if (userData == null) { + userData = new MeEntity(); + mToolBoxEntity.setMe(userData); + } + userData.setToolkitFavorite(true); + mMenuCollect.setEnabled(true); + mMenuCollect.setIcon(R.drawable.menu_ic_collect_select); + toast(R.string.collection_success); + } + + @Override + public void onError() { + mMenuCollect.setEnabled(true); + toast(R.string.collection_failure); + } + }); } }); break; } - - } - + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); - + Bundle args = getArguments(); - + String webUrl = args.getString(EntranceUtils.KEY_URL); - + mIsBindWechat = args.getBoolean(KEY_IS_BIND_WECHAT, false); mIsTools = args.getBoolean(KEY_ISTOOLS, false); mNavigationTitle = args.getString(EntranceUtils.KEY_GAMENAME); newsId = args.getString(EntranceUtils.KEY_NEWSID); - boolean isBadgeCenter = args.getBoolean(KEY_IS_BADGE_CENTER, false); - + mIsBackStackDisabled = args.getBoolean(KEY_REQUIRE_BACK_CONFIRMATION); + mBackConfirmationContent = args.getString(KEY_BACK_CONFIRMATION_CONTENT); + mGameName = args.getString(KEY_GAME_NAME); + boolean leaveWebpageToHandleTitle = args.getBoolean(KEY_LEAVE_WEB_PAGE_TO_HANDLE_TITLE, false); + WebSettings settings = mWebView.getSettings(); settings.setJavaScriptEnabled(true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { @@ -215,19 +223,22 @@ public class WebFragment extends NormalFragment { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { settings.setSafeBrowsingEnabled(false); } -// settings.setSupportZoom(true); - + + mTimeElapsedHelper = new TimeElapsedHelper(this); + + DWebView.setWebContentsDebuggingEnabled(BuildConfig.DEBUG || ("internal").equals(BuildConfig.FLAVOR)); + // 适配大于屏幕宽度的页面 settings.setUseWideViewPort(true); settings.setLoadWithOverviewMode(true); settings.setDomStorageEnabled(true); - + // 自适应屏幕 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING); } - - //用webview打开url + + // 用webview打开url mWebView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { @@ -237,27 +248,27 @@ public class WebFragment extends NormalFragment { return false; } } - + @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); - if (isBadgeCenter) { + if (leaveWebpageToHandleTitle) { mNavigationTitle = view.getTitle(); setNavigationTitle(mNavigationTitle); } } }); - + // 页面里的下载跳转到自带浏览器 mWebView.setDownloadListener((url, userAgent, contentDisposition, mimetype, contentLength) -> { Uri uri = Uri.parse(url); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent); }); - + //设置加载进度条 mWebView.setWebChromeClient(new WebChromeClient() { - + @Override public void onProgressChanged(WebView view, int newProgress) { progressBar.setProgress(newProgress); @@ -272,7 +283,7 @@ public class WebFragment extends NormalFragment { } } } - + @Override public void onReceivedTitle(WebView view, String title) { super.onReceivedTitle(view, title); @@ -285,7 +296,7 @@ public class WebFragment extends NormalFragment { }); boolean isSecurityCertification = args.getBoolean(KEY_IS_SECURITY_CERTIFICATION, false); if (isSecurityCertification) { - HashMap extraHeaders = new HashMap<>(); + HashMap extraHeaders = new HashMap<>(); extraHeaders.put("Referer", "https://www.ghzs.com"); // 应该是设置完 mWebView.loadUrl(webUrl, extraHeaders); @@ -293,30 +304,40 @@ public class WebFragment extends NormalFragment { mWebView.loadUrl(webUrl); } mWebView.addJavascriptObject(new DefaultJsApi(requireContext()), null); - + if (newsId != null) { getNewsCommentNum(); } } - - public void getNewsCommentNum() { - RetrofitManager.getInstance(getContext()).getApi() - .getNewsCommentnum(newsId, Utils.getTime(getContext())) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(new Response>() { - - @Override - public void onNext(List response) { - super.onNext(response); - if (response.size() > 0 && response.get(0).getNum() > 0) { - commentNum = response.get(0).getNum(); - webComment.setText("查看评论(" + response.get(0).getNum() + ")"); - } - } - }); + + @Override + public void onDestroy() { + super.onDestroy(); + + if (!TextUtils.isEmpty(mGameName)) { + MtaHelper.onEventWithTime("H5页面", mTimeElapsedHelper.getElapsedTime(), "停留", mGameName); + } } - + + public void getNewsCommentNum() { + RetrofitManager.getInstance(getContext()) + .getApi() + .getNewsCommentnum(newsId, Utils.getTime(getContext())) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(new Response>() { + + @Override + public void onNext(List response) { + super.onNext(response); + if (response.size() > 0 && response.get(0).getNum() > 0) { + commentNum = response.get(0).getNum(); + webComment.setText("查看评论(" + response.get(0).getNum() + ")"); + } + } + }); + } + @OnClick(R.id.web_comment) public void onWebClick(View v) { if (v == webComment) { @@ -324,22 +345,34 @@ public class WebFragment extends NormalFragment { startActivityForResult(intent, MessageDetailFragment.REQUEST_UPDATE_COMMENT); } } - + @Override public boolean onBackPressed() { - if (mWebView.canGoBack()) { + if (mIsBackStackDisabled) { + DialogHelper.showDialog( + requireContext(), + "注意", + mBackConfirmationContent, + "确定退出", + "暂不", + () -> requireActivity().finish(), + () -> {}, true, "H5页面", "退出弹窗"); + return true; + } else if (mWebView.canGoBack()) { mWebView.goBack(); return true; } return false; } - + @Subscribe(threadMode = ThreadMode.MAIN) public void onEventMainThread(EBReuse bean) { - // 是绑定微信页面时,绑定后刷新页面 - if (mIsBindWechat) { // 登入 - if (bean.getType().equals(PersonalFragment.LOGIN_TAG) || bean.getType().equals(Constants.EB_QUIT_LOGIN)) + // 是绑定微信页面时,登录后刷新页面 + if (mIsBindWechat) { // 登录 + if (bean.getType().equals(PersonalFragment.LOGIN_TAG) || bean.getType() + .equals(Constants.EB_QUIT_LOGIN)) { mWebView.reload(); + } } } }