package com.gh.common.databind; import android.content.Intent; import android.databinding.BindingAdapter; import android.support.v4.content.ContextCompat; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.widget.LinearLayout; import android.widget.TextView; import com.facebook.drawee.view.SimpleDraweeView; import com.gh.common.constant.Config; import com.gh.common.util.DataUtils; import com.gh.common.util.DialogUtils; import com.gh.common.util.DisplayUtils; import com.gh.common.util.GameUtils; import com.gh.common.util.NetworkUtils; import com.gh.common.util.NewsUtils; import com.gh.common.util.PackageUtils; import com.gh.common.util.StringUtils; import com.gh.common.view.DownloadDialog; 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.databinding.KaifuAddItemBinding; import com.gh.gamecenter.entity.ApkEntity; import com.gh.gamecenter.entity.GameEntity; import com.gh.gamecenter.entity.KaiFuCalendarEntity; import com.gh.gamecenter.manager.PackageManager; import com.lightgame.download.DownloadEntity; import com.lightgame.download.FileUtils; import com.lightgame.utils.Utils; import java.util.List; /** * Created by khy on 12/02/18. */ public class BindingAdapters { @BindingAdapter("imageUrl") public static void loadImage(SimpleDraweeView view, String imageUrl) { view.setImageURI(imageUrl); } @BindingAdapter("addKaiFuView") public static void addKaiFuView(LinearLayout view, List list) { if (list == null) return; view.removeAllViews(); view.addView(LayoutInflater.from(view.getContext()).inflate(R.layout.kaifu_add_item_title, null)); for (int i = 0; i < list.size(); i++) { View inflate = LayoutInflater.from(view.getContext()).inflate(R.layout.kaifu_add_item, null); KaifuAddItemBinding binding = KaifuAddItemBinding.bind(inflate); binding.setEntity(list.get(i)); binding.setIsCloseBottom(list.size() - 1 == i); view.addView(inflate); } } @BindingAdapter("visibleGone") public static void showHide(View view, Boolean show) { if (show) { view.setVisibility(View.VISIBLE); } else { view.setVisibility(View.GONE); } } @BindingAdapter("messageUnread") public static void setMessageUnread(TextView view, int unreadCount) { if (unreadCount < 100) { view.setText(String.valueOf(unreadCount)); } else { view.setText("99+"); } } @BindingAdapter("serverTypePadding") public static void setServerTypePadding(TextView view, String serverType) { int paddRight = 0; if (TextUtils.isEmpty(serverType)) { } else { if ("删档内测".equals(serverType) || "不删档内测".equals(serverType)) { if ("删档内测".equals(serverType)) { paddRight = DisplayUtils.dip2px(view.getContext(), 50); } else { paddRight = DisplayUtils.dip2px(view.getContext(), 60); } } else { paddRight = DisplayUtils.dip2px(view.getContext(), 30); } } view.setPadding(0, 0, paddRight, 0); } @BindingAdapter("serverType") public static void setServerType(TextView view, String serverType) { view.setText(serverType); if ("删档内测".equals(serverType) || "不删档内测".equals(serverType)) { view.setBackgroundResource(R.drawable.textview_server_tag); } else { view.setBackgroundResource(R.drawable.textview_orange_up); } } @BindingAdapter("articleType") public static void setArticleType(TextView view, String articleType) { NewsUtils.setNewsType(view, articleType, 0, 0); } @BindingAdapter("detailDownloadText") public static void setDetailDownloadText(TextView view, GameEntity gameEntity) { if (gameEntity == null || gameEntity.getApk().isEmpty()) { view.setBackgroundResource(R.drawable.game_item_btn_pause_style); view.setTextColor(0xFF999999); view.setClickable(false); } } @BindingAdapter("liBaoBtn") public static void setLiBaoBtn(TextView view, String status) { if (TextUtils.isEmpty(status)) return; switch (status) { case "coming": view.setText(R.string.libao_coming); view.setBackgroundResource(R.drawable.textview_blue_style); break; case "ling": view.setText(R.string.libao_ling); view.setBackgroundResource(R.drawable.textview_green_style); break; case "tao": view.setText(R.string.libao_tao); view.setBackgroundResource(R.drawable.textview_orange_style); break; case "used_up": view.setText(R.string.libao_used_up); view.setBackgroundResource(R.drawable.textview_cancel_up); break; case "finish": view.setText(R.string.libao_finish); view.setBackgroundResource(R.drawable.textview_cancel_up); break; case "linged": view.setText(R.string.libao_linged); view.setBackgroundResource(R.drawable.libao_linged_style); view.setTextColor(ContextCompat.getColorStateList(view.getContext(), R.color.libao_linged_selector)); break; case "taoed": view.setText(R.string.libao_taoed); view.setBackgroundResource(R.drawable.libao_taoed_style); view.setTextColor(ContextCompat.getColorStateList(view.getContext(), R.color.libao_taoed_selector)); break; case "copy": view.setText(R.string.libao_copy); view.setBackgroundResource(R.drawable.textview_blue_style); break; case "repeatLing": view.setText(R.string.libao_repeat_ling); view.setBackgroundResource(R.drawable.textview_cancel_up); break; case "repeatLinged": view.setText(R.string.libao_repeat_ling); view.setBackgroundResource(R.drawable.textview_green_style); break; case "repeatTao": view.setText(R.string.libao_repeat_tao); view.setBackgroundResource(R.drawable.textview_cancel_up); break; case "repeatTaoed": view.setText(R.string.libao_repeat_tao); view.setBackgroundResource(R.drawable.textview_orange_style); break; case "unshelve": view.setBackgroundResource(R.drawable.textview_cancel_style); view.setText(R.string.libao_unshelve); break; default: view.setBackgroundResource(R.drawable.textview_cancel_style); view.setText("异常"); } } @BindingAdapter("downloadButton") public static void setDownloadButton(DownloadProgressBar progressBar, GameEntity gameEntity) { // 判断是否显示按钮 if (gameEntity != null && Config.isShowDownload(gameEntity.getId()) && !"光环助手".equals(gameEntity.getName())) { progressBar.setVisibility(View.VISIBLE); } else { progressBar.setVisibility(View.GONE); return; } // 显示下载按钮状态 if (gameEntity.getApk().isEmpty()) { progressBar.setText("暂无下载"); progressBar.setDownloadType(DownloadProgressBar.DownloadType.NONE); } else { String status = GameUtils.getDownloadBtnText(progressBar.getContext(), gameEntity); switch (status) { case "插件化": progressBar.setDownloadType(DownloadProgressBar.DownloadType.PLUGIN); break; case "打开": if (gameEntity.getApk().size() == 1) { status = "启动"; } progressBar.setDownloadType(DownloadProgressBar.DownloadType.LAUNCH_OR_OPEN); break; default: progressBar.setDownloadType(DownloadProgressBar.DownloadType.NORMAL); break; } progressBar.setText(status); } // 显示下载过程状态 DownloadEntity downloadEntity = DownloadManager.getInstance(progressBar.getContext()).getDownloadEntityByUrl(gameEntity.getApk().get(0).getUrl()); if (gameEntity.getApk().size() == 1 && downloadEntity != null) { progressBar.setProgress((int) (downloadEntity.getPercent() * 10)); switch (downloadEntity.getStatus()) { case downloading: case pause: case timeout: case neterror: case waiting: progressBar.setText(R.string.downloading); if (downloadEntity.isPluggable() && PackageManager.isInstalled(downloadEntity.getPackageName())) { progressBar.setDownloadType(DownloadProgressBar.DownloadType.DOWNLOADING_PLUGIN); } else { progressBar.setDownloadType(DownloadProgressBar.DownloadType.DOWNLOADING_NORMAL); } break; case done: progressBar.setText(R.string.install); if (downloadEntity.isPluggable() && PackageManager.isInstalled(downloadEntity.getPackageName())) { progressBar.setDownloadType(DownloadProgressBar.DownloadType.INSTALL_PLUGIN); } else { progressBar.setDownloadType(DownloadProgressBar.DownloadType.INSTALL_NORMAL); } break; case cancel: case hijack: case notfound: break; default: break; } } // 点击事件 progressBar.setOnClickListener(v -> { switch (progressBar.getDownloadType()) { case DOWNLOADING_PLUGIN: case DOWNLOADING_NORMAL: Intent intent = DownloadManagerActivity.getDownloadMangerIntent(v.getContext(), gameEntity.getApk().get(0).getUrl(), "(我的光环:我的游戏)"); v.getContext().startActivity(intent); break; case NONE: Utils.toast(v.getContext(), "该游戏已关闭下载"); break; case NORMAL: case PLUGIN: if (gameEntity.getApk().size() == 1) { if (NetworkUtils.isWifiConnected(v.getContext())) { download(progressBar, gameEntity); } else { DialogUtils.showDownloadDialog(v.getContext(), () -> { download(progressBar, gameEntity); }); } } else { DownloadDialog.getInstance(v.getContext()).showPopupWindow(v, gameEntity, "(我的光环:我的游戏)", "我的光环-我的游戏:" + gameEntity.getName()); } break; case LAUNCH_OR_OPEN: if (gameEntity.getApk().size() == 1) { DataUtils.onGameLaunchEvent(v.getContext(), gameEntity.getName(), gameEntity.getApk().get(0).getPlatform(), "我的光环-我的游戏"); PackageUtils.launchApplicationByPackageName(v.getContext(), gameEntity.getApk().get(0).getPackageName()); } else { DownloadDialog.getInstance(v.getContext()).showPopupWindow(v, gameEntity, "(我的光环:我的游戏)", "我的光环-我的游戏:" + gameEntity.getName()); } break; case INSTALL_PLUGIN: case INSTALL_NORMAL: if (downloadEntity != null) PackageUtils.launchSetup(v.getContext(), downloadEntity.getPath()); } }); } // 开始下载 private static void download(DownloadProgressBar progressBar, GameEntity gameEntity) { String str = progressBar.getText(); String method; if (str.contains("更新")) { method = "更新"; } else if (str.contains("插件化")) { method = "插件化"; } else { method = progressBar.getContext().getString(R.string.download); } ApkEntity apkEntity = gameEntity.getApk().get(0); String msg = FileUtils.isCanDownload(progressBar.getContext(), apkEntity.getSize()); if (TextUtils.isEmpty(msg)) { DataUtils.onGameDownloadEvent(progressBar.getContext(), gameEntity.getName(), apkEntity.getPlatform(), "(我的光环:我的游戏)", "下载开始"); DownloadManager.createDownload(progressBar.getContext(), apkEntity, gameEntity, method, StringUtils.buildString("(我的光环:我的游戏)"), "我的光环-我的游戏:" + gameEntity.getName()); progressBar.setProgress(0); progressBar.setDownloadType("插件化".equals(method) ? DownloadProgressBar.DownloadType.DOWNLOADING_PLUGIN : DownloadProgressBar.DownloadType.DOWNLOADING_NORMAL); } else { Utils.toast(progressBar.getContext(), msg); } } }