Files
assistant-android/app/src/main/java/com/gh/common/view/DownloadDialog.java

663 lines
26 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.gh.common.view;
import android.app.Dialog;
import android.content.Context;
import androidx.databinding.DataBindingUtil;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager.widget.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.gh.common.constant.Config;
import com.gh.common.exposure.ExposureEvent;
import com.gh.common.util.ClickUtils;
import com.gh.common.util.DirectUtils;
import com.gh.common.util.DisplayUtils;
import com.gh.common.util.PackageUtils;
import com.gh.common.util.PlatformUtils;
import com.gh.common.util.SPUtils;
import com.gh.download.DownloadManager;
import com.gh.gamecenter.R;
import com.gh.gamecenter.adapter.OnCollectionCallBackListener;
import com.gh.gamecenter.adapter.PlatformPagerAdapter;
import com.gh.gamecenter.databinding.ImprintContentItemBinding;
import com.gh.gamecenter.entity.ApkEntity;
import com.gh.gamecenter.entity.ApkLink;
import com.gh.gamecenter.entity.GameCollectionEntity;
import com.gh.gamecenter.entity.GameEntity;
import com.gh.gamecenter.entity.LinkEntity;
import com.gh.gamecenter.entity.SettingsEntity;
import com.gh.gamecenter.eventbus.EBDownloadStatus;
import com.gh.gamecenter.eventbus.EBPackage;
import com.gh.gamecenter.eventbus.EBReuse;
import com.gh.gamecenter.manager.PackagesManager;
import com.lightgame.download.DataWatcher;
import com.lightgame.download.DownloadEntity;
import com.lightgame.download.DownloadStatus;
import com.lightgame.utils.Utils;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author 温冠超
* @email 294299195@qq.com
* @date 2015-8-12
* @update 2015-8-12
* @des 弹出游戏版本下载按钮,点击并添加到下载任务中
*/
public class DownloadDialog implements OnCollectionCallBackListener {
private Context mContext;
private PopupWindow popupWindow;
private ViewPager viewPager;
private ViewPager collectionViewPager;
private LinearLayout dialog_ll_collection_hint;
private LinearLayout dialog_ll_hint;
private LinearLayout dialog_ll_collection;
private View mDialogMorePlatformHint;
private List<ApkEntity> gameApk;
private GameEntity gameEntity;
private PlatformPagerAdapter adapter;
private PlatformPagerAdapter collectionAdapter;
private ExposureEvent traceEvent;
private static final String DOWNLOAD_PLATFORM_HINT = "download_platform_hint";
private String entrance;
private String location;
private String mAutoDownloadPlatform;
private final int row = 3;
private final int column = 3;
private boolean isLoadPlatform;
private DataWatcher dataWatcher = new DataWatcher() {
@Override
public void onDataChanged(DownloadEntity downloadEntity) {
if (downloadEntity.getName().equals(gameEntity.getName())
&& !DownloadStatus.delete.equals(DownloadManager.getInstance(mContext).getStatus(downloadEntity.getUrl()))) {
adapter.putDownloadEntity(downloadEntity);
if (collectionAdapter != null) {
collectionAdapter.putDownloadEntity(downloadEntity);
}
}
}
};
private DownloadDialog(Context context) {
mContext = context;
}
public static DownloadDialog getInstance(Context context) {
return new DownloadDialog(context);
}
// 自动下载并翻到相应页面
public void showPopupWindowAutoDownload(View view, GameEntity gameEntity, String autoDownloadPlatform,
String entrance, String location, ExposureEvent traceEvent) {
mAutoDownloadPlatform = autoDownloadPlatform;
showPopupWindow(view, gameEntity, entrance, location, traceEvent);
}
public void showPopupWindow(View view, GameEntity gameEntity, String entrance, String location) {
showPopupWindow(view, gameEntity, entrance, location, null);
}
public void showPopupWindow(View view, GameEntity gameEntity, String entrance, String location,
@Nullable ExposureEvent traceEvent) {
if (ClickUtils.isFastDoubleClick()) {
return;
}
this.gameEntity = gameEntity;
this.entrance = entrance;
this.location = location;
this.traceEvent = traceEvent;
gameApk = gameEntity.getApk();
if (gameEntity.getCollection() != null) {
mergeApkCollection(gameEntity);
}
if (gameEntity.getApkLink() != null) {
mergeApkLink(gameEntity);
}
sortApk();
// 一个自定义的布局,作为显示的内容
View contentView = View.inflate(mContext, R.layout.game_download_dialog, null);
TextView title = contentView.findViewById(R.id.dialog_title);
title.setText(gameEntity.getName());
viewPager = contentView.findViewById(R.id.dialog_viewPager);
dialog_ll_hint = contentView.findViewById(R.id.dialog_ll_hint);
dialog_ll_collection = contentView.findViewById(R.id.dialog_ll_collection);
dialog_ll_collection_hint = contentView.findViewById(R.id.dialog_ll_collection_hint);
collectionViewPager = contentView.findViewById(R.id.dialog_collection_viewPager);
mDialogMorePlatformHint = contentView.findViewById(R.id.dialog_more_platform_hit);
TextView dialogAd = contentView.findViewById(R.id.dialog_ad);
TextView dialogAdMirror = contentView.findViewById(R.id.dialog_ad_mirror);
mDialogMorePlatformHint.setOnClickListener(v -> {
mDialogMorePlatformHint.setVisibility(View.GONE);
SPUtils.setBoolean(DOWNLOAD_PLATFORM_HINT, false);
});
LinkEntity downloadAd = gameEntity.getDownloadAd();
if (downloadAd != null) {
dialogAd.setVisibility(View.VISIBLE);
dialogAd.setText(downloadAd.getTitle());
dialogAdMirror.setVisibility(View.INVISIBLE);
dialogAdMirror.setText(downloadAd.getTitle());
dialogAd.setOnClickListener(v -> {
if ("imprint".equals(downloadAd.getType())) {
showImprintDialog(downloadAd.getTitle());
} else {
DirectUtils.directToLinkPage(
mContext,
downloadAd,
entrance, "下载多平台弹窗");
}
});
}
isLoadPlatform = false;
init(new ArrayList<>(gameApk));
viewPager.addOnPageChangeListener(new MyPageChangeListener(dialog_ll_hint));
popupWindow = new PopupWindow(contentView, LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, true);
contentView.setOnClickListener(v -> popupWindow.dismiss());
contentView.setFocusable(true);
contentView.setFocusableInTouchMode(true);
contentView.setOnKeyListener((v, keyCode, event) -> {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0
&& popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
}
return false;
});
popupWindow.setOnDismissListener(() -> {
EventBus.getDefault().unregister(DownloadDialog.this);
DownloadManager.getInstance(mContext).removeObserver(dataWatcher);
});
popupWindow.setTouchable(true);
popupWindow.setFocusable(true);
// 设置popWindow的显示和消失动画
popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
// 设置好参数之后再show
popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
if (!EventBus.getDefault().isRegistered(DownloadDialog.this)) {
EventBus.getDefault().register(DownloadDialog.this);
}
DownloadManager.getInstance(mContext).addObserver(dataWatcher);
}
private void showImprintDialog(String titleName) {
Dialog dialog = new Dialog(mContext, R.style.full_dialog);
View inflate = LayoutInflater.from(mContext).inflate(R.layout.imprint_dialog, null);
dialog.setContentView(inflate);
dialog.show();
Window window = dialog.getWindow();
WindowManager.LayoutParams params;
if (window != null) {
params = window.getAttributes();
params.width = (int) (mContext.getResources().getDisplayMetrics().widthPixels * 0.9);
window.setAttributes(params);
window.setBackgroundDrawableResource(R.drawable.full_dialog_background);
}
inflate.findViewById(R.id.imprint_close).setOnClickListener(v -> dialog.dismiss());
LinearLayout content = inflate.findViewById(R.id.imprint_content);
((TextView) inflate.findViewById(R.id.imprint_title)).setText(titleName);
View head = LayoutInflater.from(mContext).inflate(R.layout.imprint_content_item, null);
content.addView(head, LayoutParams.MATCH_PARENT, DisplayUtils.dip2px(30));
LimitHeightLinearLayout imprintContainer = inflate.findViewById(R.id.imprint_container);
imprintContainer.setLimitHeight((int) (mContext.getResources().getDisplayMetrics().heightPixels * 0.8));
ArrayList<ApkEntity> list = gameEntity.getApk();
SettingsEntity settings = Config.getSettings();
for (int i = 0; i < list.size(); i++) {
ApkEntity apk = gameEntity.getApk().get(i);
if (settings != null && settings.getGameDownloadBlackList().contains(apk.getPackageName())) {
continue;
}
View item = LayoutInflater.from(mContext).inflate(R.layout.imprint_content_item, null);
ImprintContentItemBinding bind = DataBindingUtil.bind(item);
bind.setApk(apk);
bind.setPlatformName(PlatformUtils.getInstance(mContext).getPlatformName(apk.getPlatform()));
content.addView(item, LayoutParams.MATCH_PARENT, DisplayUtils.dip2px(40));
}
// close line
View view = new View(mContext);
view.setBackgroundColor(mContext.getResources().getColor(R.color.text_5d5d5d));
view.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DisplayUtils.dip2px(1)));
content.addView(view);
}
private void sortApk() {
for (ApkEntity apkEntity : gameApk) {
GameCollectionEntity apkCollection = apkEntity.getApkCollection();
if (apkCollection != null) {
List<ApkEntity> saveApkEntity = apkCollection.getSaveApkEntity();
if (saveApkEntity != null) {
for (ApkEntity entity : saveApkEntity) {
int sortValue = getSortValue(entity);
if (apkEntity.getOrder() < sortValue) {
apkEntity.setOrder(sortValue);
}
}
}
continue;
}
apkEntity.setOrder(getSortValue(apkEntity));
}
Comparator<ApkEntity> comparator = (lhs, rhs) -> rhs.getOrder() - lhs.getOrder();
Collections.sort(gameApk, comparator);
}
private int getSortValue(ApkEntity apkEntity) {
/*
* 安装插件 10
* 插件化下载中 9
* 插件化 8
* 安装更新 7
* 更新下载中 6
* 更新 5
* 安装 4
* 下载中 3
* 启动 2
* 默认有图片1
* 默认无图片0
*/
Object gh_id;
String packageName = apkEntity.getPackageName();
DownloadEntity downloadEntity = DownloadManager.getInstance(mContext).getDownloadEntityByUrl(apkEntity.getUrl());
if (downloadEntity == null) {
if (!TextUtils.isEmpty(packageName) && PackagesManager.INSTANCE.isInstalled(packageName)) {
gh_id = PackageUtils.getMetaData(mContext, packageName, "gh_id");
if (gh_id == null || gh_id.equals(gameEntity.getId())) {
if (!PackageUtils.isSignature(mContext, packageName)) {
return 8;
} else if (PackagesManager.INSTANCE.isCanUpdate(gameEntity.getId(), packageName)) {
return 5;
} else {
return 2;
}
}
}
if (apkEntity.getOrder() < 1) apkEntity.setOrder(1);
String platform = apkEntity.getPlatform();
int id = PlatformUtils.getInstance(mContext).getPlatformPic(platform);
if (id == 0) {
String path = PlatformUtils.getInstance(mContext).getPlatformPicUrl(platform);
if (path == null) {
return 0;
}
}
} else {
if (downloadEntity.getStatus().equals(DownloadStatus.done)) {
if (downloadEntity.isPluggable()) {
return 10;
} else if (downloadEntity.isUpdate()) {
return 7;
} else {
return 4;
}
} else {
if (downloadEntity.isPluggable()) {
return 9;
} else if (downloadEntity.isUpdate()) {
return 6;
} else {
return 3;
}
}
}
return 0;
}
// 合并ApkLink
private void mergeApkLink(GameEntity gameEntity) {
for (ApkLink apkLink : gameEntity.getApkLink()) {
if (!TextUtils.isEmpty(apkLink.getCollection())) {
for (int i = 0; i < gameApk.size(); i++) {
ApkEntity apkEntity = gameApk.get(i);
GameCollectionEntity apkCollection = apkEntity.getApkCollection();
if (apkCollection != null && apkCollection.getSaveApkEntity() != null) {
List<ApkEntity> saveApkEntity = apkCollection.getSaveApkEntity();
if (apkLink.getCollection().equals(apkCollection.getId())) {
ApkEntity element = new ApkEntity();
element.setApkLink(apkLink);
if (saveApkEntity.size() > apkLink.getSort()) {
saveApkEntity.add(apkLink.getSort(), element);
} else {
saveApkEntity.add(element);
}
}
}
}
} else {
ApkEntity element = new ApkEntity();
element.setApkLink(apkLink);
if (gameApk.size() > apkLink.getSort()) {
gameApk.add(apkLink.getSort(), element);
} else {
gameApk.add(element);
}
}
}
}
// 合并ApkCollection
private void mergeApkCollection(GameEntity gameEntity) {
List<ApkEntity> gameCollectionApk = new ArrayList<>();
ConcurrentHashMap<String, Integer> hashMap = new ConcurrentHashMap<>();
boolean isCollection;
for (ApkEntity apkEntity : gameApk) {
isCollection = false;
for (GameCollectionEntity gameCollectionEntity : gameEntity.getCollection()) {
for (String packageName : gameCollectionEntity.getPackage()) {
if (packageName.equals(apkEntity.getPackageName())) {
isCollection = true;
if (hashMap.get(gameCollectionEntity.getName()) != null) {
gameCollectionApk.get(hashMap.get(gameCollectionEntity.getName()))
.getApkCollection().getSaveApkEntity().add(apkEntity);
} else {
ApkEntity newApkEntity = new ApkEntity();
GameCollectionEntity collectionEntity = new GameCollectionEntity();
List<ApkEntity> saveApkList = new ArrayList<>();
saveApkList.add(apkEntity);
collectionEntity.setSaveApkEntity(saveApkList);
collectionEntity.setId(gameCollectionEntity.getId());
collectionEntity.setName(gameCollectionEntity.getName());
collectionEntity.setIcon(gameCollectionEntity.getIcon());
collectionEntity.setColor(gameCollectionEntity.getColor());
newApkEntity.setApkCollection(collectionEntity);
gameCollectionApk.add(newApkEntity);
hashMap.put(gameCollectionEntity.getName(), gameCollectionApk.size() - 1);
}
}
}
}
if (!isCollection) {
gameCollectionApk.add(apkEntity);
}
}
gameApk = gameCollectionApk;
}
private void init(List<ApkEntity> apkList) {
for (int i = 0; i < apkList.size(); i++) {
String platformName = PlatformUtils.getInstance(mContext)
.getPlatformName(apkList.get(i).getPlatform());
if (platformName == null) {
apkList.remove(i);
i--;
if (!isLoadPlatform) {
PlatformUtils.getInstance(mContext).getPlatform();
isLoadPlatform = true;
}
}
}
ApkEntity skipEntiy = new ApkEntity();
skipEntiy.setPackageName("求版本");
apkList.add(skipEntiy); // 添加一个跳转投票
dialog_ll_hint.removeAllViews();
int size = (int) Math.ceil(apkList.size() / (double) (row * column));
addHintPoint(dialog_ll_hint, size);
if (size >= 2) {
boolean isShowPlatformHint = SPUtils.getBoolean(DOWNLOAD_PLATFORM_HINT, true);
if (isShowPlatformHint) {
mDialogMorePlatformHint.setVisibility(View.VISIBLE);
}
}
int count = apkList.size();
int vpHeight;
if (count <= 3) {
vpHeight = 90;
} else if (count <= 6) {
vpHeight = 150;
} else {
vpHeight = 210;
}
ViewGroup.LayoutParams layoutParams = viewPager.getLayoutParams();
layoutParams.height = DisplayUtils.dip2px(mContext, vpHeight);
viewPager.setLayoutParams(layoutParams);
int currentItem = 0;
if (viewPager != null) {
currentItem = viewPager.getCurrentItem();
}
if (!TextUtils.isEmpty(mAutoDownloadPlatform)) {
for (int i = 0; i < apkList.size(); i++) {
ApkEntity apkEntity = apkList.get(i);
if (mAutoDownloadPlatform.equals(apkEntity.getPlatform())) {
currentItem = (i / 9);
break;
}
GameCollectionEntity apkCollection = apkEntity.getApkCollection();
if (apkCollection != null) {
List<ApkEntity> saveApkEntity = apkCollection.getSaveApkEntity();
if (saveApkEntity != null) {
for (ApkEntity entity : saveApkEntity) {
if (mAutoDownloadPlatform.equals(entity.getPlatform())) {
currentItem = (i / 9);
break;
}
}
}
}
}
}
Utils.log("currentItem = " + currentItem);
adapter = new PlatformPagerAdapter(mContext, this,
gameEntity, apkList, mAutoDownloadPlatform, entrance, location, traceEvent);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(currentItem);
if (currentItem != 0) setDownloadOvalHint(dialog_ll_hint, currentItem);
}
private void addHintPoint(LinearLayout linearLayout, int size) {
if (size <= 1) {
return;
}
for (int i = 0; i < size; i++) {
ImageView imageView = new ImageView(mContext);
LayoutParams lparams = new LayoutParams(
DisplayUtils.dip2px(mContext, 6), DisplayUtils.dip2px(mContext, 6));
if (i == 0) {
lparams.leftMargin = 0;
imageView.setImageResource(R.drawable.download_oval_hint_up);
} else {
lparams.leftMargin = DisplayUtils.dip2px(mContext, 9);
imageView.setImageResource(R.drawable.oval_hint_gray_bg);
}
imageView.setLayoutParams(lparams);
linearLayout.addView(imageView);
}
}
// 接收安装成功的消息,更新界面
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(EBPackage busFour) {
if ("安装".equals(busFour.getType())) {
adapter.removeDownloadEntityByPackageName(busFour.getPackageName());
if (collectionAdapter != null) {
collectionAdapter.removeDownloadEntityByPackageName(busFour.getPackageName());
}
} else if ("卸载".equals(busFour.getType())) {
adapter.updateItem(busFour.getPackageName());
if (collectionAdapter != null) {
collectionAdapter.updateItem(busFour.getPackageName());
}
}
}
//接收下载被删除消息
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(EBDownloadStatus status) {
if ("delete".equals(status.getStatus())) {
String url = status.getUrl();
adapter.removeDownloadEntityByUrl(url);
if (collectionAdapter != null) {
collectionAdapter.removeDownloadEntityByUrl(url);
}
// DownloadManager.getInstance(mContext).putStatus(url, "delete");
}
}
// 接收platform数据改变消息更新界面
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(EBReuse reuse) {
if ("PlatformChanged".equals(reuse.getType())) {
DownloadManager.getInstance(mContext).removeObserver(dataWatcher);
init(new ArrayList<>(gameApk));
DownloadManager.getInstance(mContext).addObserver(dataWatcher);
}
}
// 显示合集
@Override
public void showCollection(GameCollectionEntity gameCollectionEntity) {
dialog_ll_collection.setVisibility(View.VISIBLE);
List<ApkEntity> saveApkList = gameCollectionEntity.getSaveApkEntity();
int count = saveApkList.size();
int vpHeight;
if (count <= 3) {
vpHeight = 82;
} else if (count <= 6) {
vpHeight = 142;
} else {
vpHeight = 202;
}
ViewGroup.LayoutParams layoutParams = collectionViewPager.getLayoutParams();
layoutParams.height = DisplayUtils.dip2px(mContext, vpHeight);
collectionViewPager.setLayoutParams(layoutParams);
dialog_ll_collection_hint.removeAllViews();
int size = (int) Math.ceil(count / (double) (row * column));
addHintPoint(dialog_ll_collection_hint, size);
collectionAdapter = new PlatformPagerAdapter(
mContext, null, gameEntity, saveApkList,
mAutoDownloadPlatform, entrance, location, traceEvent);
collectionViewPager.setAdapter(collectionAdapter);
collectionViewPager.addOnPageChangeListener(new MyPageChangeListener(dialog_ll_collection_hint));
int currentItem = 0;
if (!TextUtils.isEmpty(mAutoDownloadPlatform)) {
for (int i = 0; i < saveApkList.size(); i++) {
ApkEntity apkEntity = saveApkList.get(i);
if (mAutoDownloadPlatform.equals(apkEntity.getPlatform())) {
currentItem = (i / 9);
break;
}
}
}
if (currentItem != 0) {
collectionViewPager.setCurrentItem(currentItem);
setDownloadOvalHint(dialog_ll_collection_hint, currentItem);
}
mAutoDownloadPlatform = null;
}
// 隐藏合集
@Override
public void hideCollection() {
dialog_ll_collection.setVisibility(View.GONE);
collectionAdapter = null;
}
private class MyPageChangeListener implements OnPageChangeListener {
private LinearLayout linearLayout;
public MyPageChangeListener(LinearLayout linearLayout) {
this.linearLayout = linearLayout;
}
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int position) {
setDownloadOvalHint(linearLayout, position);
}
@Override
public void onPageScrollStateChanged(int i) {
}
}
private void setDownloadOvalHint(LinearLayout linearLayout, int position) {
for (int i = 0, size = linearLayout.getChildCount(); i < size; i++) {
if (i == position % size) {
((ImageView) linearLayout.getChildAt(i))
.setImageResource(R.drawable.download_oval_hint_up);
} else {
((ImageView) linearLayout.getChildAt(i))
.setImageResource(R.drawable.oval_hint_gray_bg);
}
}
}
}