486 lines
18 KiB
Java
486 lines
18 KiB
Java
package com.gh.common.view;
|
||
|
||
import android.content.Context;
|
||
import android.content.SharedPreferences;
|
||
import android.preference.PreferenceManager;
|
||
import android.support.v4.view.ViewPager;
|
||
import android.support.v4.view.ViewPager.OnPageChangeListener;
|
||
import android.view.Gravity;
|
||
import android.view.KeyEvent;
|
||
import android.view.View;
|
||
import android.view.View.OnClickListener;
|
||
import android.view.ViewGroup;
|
||
import android.widget.ImageView;
|
||
import android.widget.LinearLayout;
|
||
import android.widget.LinearLayout.LayoutParams;
|
||
import android.widget.PopupWindow;
|
||
import android.widget.TextView;
|
||
|
||
import com.gh.download.DownloadManager;
|
||
import com.gh.common.util.DisplayUtils;
|
||
import com.gh.common.util.PackageUtils;
|
||
import com.gh.common.util.PlatformUtils;
|
||
import com.lightgame.utils.Utils;
|
||
import com.gh.gamecenter.R;
|
||
import com.gh.gamecenter.adapter.OnCollectionCallBackListener;
|
||
import com.gh.gamecenter.adapter.PlatformPagerAdapter;
|
||
import com.gh.gamecenter.entity.ApkEntity;
|
||
import com.gh.gamecenter.entity.GameCollectionEntity;
|
||
import com.gh.gamecenter.entity.GameEntity;
|
||
import com.gh.gamecenter.eventbus.EBDownloadStatus;
|
||
import com.gh.gamecenter.eventbus.EBPackage;
|
||
import com.gh.gamecenter.eventbus.EBReuse;
|
||
import com.gh.gamecenter.manager.PackageManager;
|
||
import com.lightgame.download.DataWatcher;
|
||
import com.lightgame.download.DownloadEntity;
|
||
import com.lightgame.download.DownloadStatus;
|
||
|
||
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 static DownloadDialog instance;
|
||
|
||
private Context mContext;
|
||
|
||
private boolean isShow;
|
||
private PopupWindow popupWindow;
|
||
private List<ApkEntity> gameApk;
|
||
private GameEntity gameEntity;
|
||
private ViewPager viewPager;
|
||
private PlatformPagerAdapter adapter;
|
||
private LinearLayout dialog_ll_hint;
|
||
private LinearLayout dialog_ll_collection;
|
||
private ViewPager collectionViewPager;
|
||
private PlatformPagerAdapter collectionAdapter;
|
||
private DataWatcher dataWatcher = new DataWatcher() {
|
||
@Override
|
||
public void onDataChanged(DownloadEntity downloadEntity) {
|
||
if (downloadEntity.getName().equals(gameEntity.getName())
|
||
&& !"delete".equals(DownloadManager.getInstance(mContext).getStatus(downloadEntity.getUrl()))) {
|
||
|
||
adapter.putDownloadEntity(downloadEntity);
|
||
|
||
if (collectionAdapter != null) {
|
||
collectionAdapter.putDownloadEntity(downloadEntity);
|
||
}
|
||
}
|
||
}
|
||
};
|
||
private LinearLayout dialog_ll_collection_hint;
|
||
private String entrance;
|
||
private String location;
|
||
private int row;
|
||
private int column;
|
||
private boolean isLoadPlatform;
|
||
|
||
private DownloadDialog() {
|
||
}
|
||
|
||
private DownloadDialog(Context context) {
|
||
mContext = context;
|
||
isShow = false;
|
||
}
|
||
|
||
public static DownloadDialog getInstance(Context context) {
|
||
if (instance == null) {
|
||
instance = new DownloadDialog(context);
|
||
}
|
||
return instance;
|
||
}
|
||
|
||
public void showPopupWindow(View view, GameEntity gameEntity, String entrance, String location) {
|
||
|
||
if (isShow && (popupWindow == null || !popupWindow.isShowing())) {
|
||
isShow = false;
|
||
}
|
||
if (isShow) {
|
||
return;
|
||
}
|
||
isShow = true;
|
||
|
||
this.gameEntity = gameEntity;
|
||
this.entrance = entrance;
|
||
this.location = location;
|
||
|
||
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
|
||
row = sp.getInt("download_box_row", 3);
|
||
column = sp.getInt("download_box_column", 3);
|
||
|
||
gameApk = sortApk(new ArrayList<>(gameEntity.getApk()));
|
||
|
||
if (gameEntity.getCollection() != null) {
|
||
mergeApkCollection(gameEntity);
|
||
}
|
||
|
||
// 一个自定义的布局,作为显示的内容
|
||
View contentView = View.inflate(mContext, R.layout.game_download_dialog, null);
|
||
|
||
TextView title = (TextView) contentView.findViewById(R.id.dialog_title);
|
||
title.setText(gameEntity.getName());
|
||
|
||
viewPager = (ViewPager) contentView.findViewById(R.id.dialog_viewPager);
|
||
dialog_ll_hint = (LinearLayout) contentView.findViewById(R.id.dialog_ll_hint);
|
||
dialog_ll_collection = (LinearLayout) contentView.findViewById(R.id.dialog_ll_collection);
|
||
dialog_ll_collection_hint = (LinearLayout) contentView.findViewById(R.id.dialog_ll_collection_hint);
|
||
collectionViewPager = (ViewPager) contentView.findViewById(R.id.dialog_collection_viewPager);
|
||
|
||
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(new OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
popupWindow.dismiss();
|
||
}
|
||
});
|
||
contentView.setFocusable(true);
|
||
contentView.setFocusableInTouchMode(true);
|
||
contentView.setOnKeyListener(new View.OnKeyListener() {
|
||
@Override
|
||
public boolean onKey(View v, int keyCode, KeyEvent event) {
|
||
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0
|
||
&& popupWindow != null && popupWindow.isShowing()) {
|
||
popupWindow.dismiss();
|
||
}
|
||
return false;
|
||
}
|
||
});
|
||
|
||
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
|
||
@Override
|
||
public void onDismiss() {
|
||
isShow = false;
|
||
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 ArrayList<ApkEntity> sortApk(ArrayList<ApkEntity> apkList) {
|
||
/*
|
||
* 安装插件 10
|
||
* 插件化下载中 9
|
||
* 插件化 8
|
||
* 安装更新 7
|
||
* 更新下载中 6
|
||
* 更新 5
|
||
* 安装 4
|
||
* 下载中 3
|
||
* 启动 2
|
||
* 默认(有图片)1
|
||
* 默认(无图片)0
|
||
*/
|
||
|
||
DownloadEntity downloadEntity;
|
||
String packageName;
|
||
Object gh_id;
|
||
for (ApkEntity apkEntity : apkList) {
|
||
downloadEntity = DownloadManager.getInstance(mContext).get(apkEntity.getUrl());
|
||
if (downloadEntity == null) {
|
||
packageName = apkEntity.getPackageName();
|
||
if (PackageManager.isInstalled(packageName)) {
|
||
gh_id = PackageUtils.getMetaData(mContext, packageName, "gh_id");
|
||
if (gh_id == null || gh_id.equals(gameEntity.getId())) {
|
||
if (!PackageUtils.isSignature(mContext, packageName)) {
|
||
apkEntity.setOrder(8);
|
||
} else if (PackageManager.isCanUpdate(gameEntity.getId(), packageName)) {
|
||
apkEntity.setOrder(5);
|
||
} else {
|
||
apkEntity.setOrder(2);
|
||
}
|
||
continue;
|
||
}
|
||
}
|
||
apkEntity.setOrder(1);
|
||
|
||
String platform = apkEntity.getPlatform();
|
||
int id = PlatformUtils.getInstance(mContext).getPlatformPic(platform);
|
||
if (id == 0) {
|
||
String path = PlatformUtils.getInstance(mContext).getPlatformPicPath(platform);
|
||
if (path == null) {
|
||
apkEntity.setOrder(0);
|
||
}
|
||
}
|
||
} else {
|
||
if (downloadEntity.getStatus().equals(DownloadStatus.done)) {
|
||
if (downloadEntity.isPluggable()) {
|
||
apkEntity.setOrder(10);
|
||
} else if (downloadEntity.isUpdate()) {
|
||
apkEntity.setOrder(7);
|
||
} else {
|
||
apkEntity.setOrder(4);
|
||
}
|
||
} else {
|
||
if (downloadEntity.isPluggable()) {
|
||
apkEntity.setOrder(9);
|
||
} else if (downloadEntity.isUpdate()) {
|
||
apkEntity.setOrder(6);
|
||
} else {
|
||
apkEntity.setOrder(3);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
Comparator<ApkEntity> comparator = new Comparator<ApkEntity>() {
|
||
@Override
|
||
public int compare(ApkEntity lhs, ApkEntity rhs) {
|
||
return rhs.getOrder() - lhs.getOrder();
|
||
}
|
||
};
|
||
Collections.sort(apkList, comparator);
|
||
return apkList;
|
||
}
|
||
|
||
// 合并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.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);
|
||
|
||
int count = apkList.size();
|
||
int vpHeight;
|
||
if (count <= 3) {
|
||
vpHeight = 88;
|
||
} else if (count <= 6) {
|
||
vpHeight = 148;
|
||
} else {
|
||
vpHeight = 208;
|
||
}
|
||
ViewGroup.LayoutParams layoutParams = viewPager.getLayoutParams();
|
||
layoutParams.height = DisplayUtils.dip2px(mContext, vpHeight);
|
||
viewPager.setLayoutParams(layoutParams);
|
||
|
||
int currentItem = 0;
|
||
if (viewPager != null) {
|
||
currentItem = viewPager.getCurrentItem();
|
||
}
|
||
Utils.log("currentItem = " + currentItem);
|
||
adapter = new PlatformPagerAdapter(mContext, this, gameEntity, apkList, entrance, location);
|
||
viewPager.setAdapter(adapter);
|
||
viewPager.setCurrentItem(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.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);
|
||
|
||
int count = gameCollectionEntity.getSaveApkEntity().size();
|
||
int vpHeight;
|
||
if (count <= 3) {
|
||
vpHeight = 80;
|
||
} else if (count <= 6) {
|
||
vpHeight = 140;
|
||
} else {
|
||
vpHeight = 200;
|
||
}
|
||
|
||
ViewGroup.LayoutParams layoutParams = collectionViewPager.getLayoutParams();
|
||
layoutParams.height = DisplayUtils.dip2px(mContext, vpHeight);
|
||
collectionViewPager.setLayoutParams(layoutParams);
|
||
|
||
dialog_ll_collection_hint.removeAllViews();
|
||
int size = (int) Math.ceil(gameCollectionEntity.getSaveApkEntity().size() / (double) (row * column));
|
||
addHintPoint(dialog_ll_collection_hint, size);
|
||
|
||
collectionAdapter = new PlatformPagerAdapter(
|
||
mContext, this, gameEntity, gameCollectionEntity.getSaveApkEntity(), entrance, location);
|
||
collectionViewPager.setAdapter(collectionAdapter);
|
||
|
||
collectionViewPager.addOnPageChangeListener(new MyPageChangeListener(dialog_ll_collection_hint));
|
||
}
|
||
|
||
// 隐藏合集
|
||
@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) {
|
||
for (int i = 0, size = linearLayout.getChildCount(); i < size; i++) {
|
||
if (i == position % size) {
|
||
((ImageView) linearLayout.getChildAt(i))
|
||
.setImageResource(R.drawable.oval_hint_up);
|
||
} else {
|
||
((ImageView) linearLayout.getChildAt(i))
|
||
.setImageResource(R.drawable.oval_hint_gray_bg);
|
||
}
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onPageScrollStateChanged(int i) {
|
||
|
||
}
|
||
}
|
||
|
||
}
|