445 lines
16 KiB
Java
445 lines
16 KiB
Java
package com.gh.gamecenter.adapter;
|
|
|
|
import android.content.SharedPreferences;
|
|
import android.content.pm.ApplicationInfo;
|
|
import android.content.pm.PackageInfo;
|
|
import android.content.pm.PackageManager;
|
|
import android.graphics.drawable.Drawable;
|
|
import android.preference.PreferenceManager;
|
|
import android.support.annotation.Nullable;
|
|
import android.support.v4.util.ArrayMap;
|
|
import android.support.v7.widget.RecyclerView.ViewHolder;
|
|
import android.text.TextUtils;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.TextView;
|
|
|
|
import com.gh.common.constant.ItemViewType;
|
|
import com.gh.common.util.ApkActiveUtils;
|
|
import com.gh.common.util.BitmapUtils;
|
|
import com.gh.common.util.DataCollectionUtils;
|
|
import com.gh.common.util.DisplayUtils;
|
|
import com.gh.common.util.DownloadItemUtils;
|
|
import com.gh.common.util.GameViewUtils;
|
|
import com.gh.common.util.ImageUtils;
|
|
import com.gh.common.util.PackageUtils;
|
|
import com.gh.common.util.PlatformUtils;
|
|
import com.gh.common.util.ThirdPartyPackageHelper;
|
|
import com.gh.common.view.SwipeLayout;
|
|
import com.gh.download.DownloadManager;
|
|
import com.gh.gamecenter.GameDetailActivity;
|
|
import com.gh.gamecenter.InstallActivity;
|
|
import com.gh.gamecenter.KcSelectGameActivity;
|
|
import com.gh.gamecenter.R;
|
|
import com.gh.gamecenter.adapter.viewholder.FooterViewHolder;
|
|
import com.gh.gamecenter.adapter.viewholder.GameNormalSwipeViewHolder;
|
|
import com.gh.gamecenter.entity.ApkEntity;
|
|
import com.gh.gamecenter.entity.GameEntity;
|
|
import com.gh.gamecenter.entity.GameInstall;
|
|
import com.gh.gamecenter.kuaichuan.FileInfo;
|
|
import com.gh.gamecenter.retrofit.Response;
|
|
import com.gh.gamecenter.retrofit.RetrofitManager;
|
|
import com.halo.assistant.HaloApp;
|
|
import com.lightgame.adapter.BaseRecyclerAdapter;
|
|
|
|
import java.io.File;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
|
|
import io.reactivex.Observable;
|
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
import io.reactivex.schedulers.Schedulers;
|
|
import retrofit2.HttpException;
|
|
|
|
/**
|
|
* Created by LGT on 2016/8/12.
|
|
* 已安装界面-数据适配器
|
|
*/
|
|
public class InstallFragmentAdapter extends BaseRecyclerAdapter<ViewHolder> {
|
|
|
|
private InstallActivity mActivity;
|
|
|
|
private onSmoothLayoutListener smoothListener;
|
|
|
|
private SharedPreferences sp;
|
|
|
|
private ArrayList<GameEntity> gameList;
|
|
private ArrayList<GameInstall> sortedList;
|
|
|
|
//下载用到的map
|
|
private ArrayMap<String, ArrayList<Integer>> locationMap;
|
|
|
|
private boolean isRemove;
|
|
|
|
private boolean isSwipe;
|
|
|
|
private boolean showKcHint;
|
|
|
|
private int maxWidth;
|
|
|
|
|
|
public InstallFragmentAdapter(InstallActivity activity) {
|
|
super(activity);
|
|
this.mActivity = activity;
|
|
smoothListener = activity;
|
|
|
|
gameList = new ArrayList<>();
|
|
sortedList = new ArrayList<>();
|
|
|
|
locationMap = new ArrayMap<>();
|
|
|
|
isRemove = false;
|
|
isSwipe = false;
|
|
|
|
sp = PreferenceManager.getDefaultSharedPreferences(mContext);
|
|
showKcHint = sp.getBoolean("showKcHint", true);
|
|
|
|
maxWidth = mContext.getResources().getDisplayMetrics().widthPixels;
|
|
}
|
|
|
|
public void initData(List<GameInstall> list) {
|
|
for (GameInstall gameInstall : list) {
|
|
Object gh_id = PackageUtils.getMetaData(mContext, gameInstall.getPackageName(), "gh_id");
|
|
if (gh_id != null && !gh_id.equals(gameInstall.getId())) {
|
|
gameInstall.setId(gh_id.toString());
|
|
} else {
|
|
String gameId = ThirdPartyPackageHelper.getGameId(gameInstall.getPackageName());
|
|
if (!TextUtils.isEmpty(gameId)) {
|
|
gameInstall.setId(gameId);
|
|
}
|
|
}
|
|
}
|
|
|
|
sortedList.clear();
|
|
gameList.clear();
|
|
sortedList.addAll(list);
|
|
|
|
if (sortedList.isEmpty()) {
|
|
mActivity.loadEmpty();
|
|
} else {
|
|
HaloApp.getInstance().getMainExecutor().execute(() -> {
|
|
List<String> ids = new ArrayList<>();
|
|
Collections.sort(sortedList, (lhs, rhs) -> { // 按安装时间排序
|
|
if (rhs.getInstallTime() > lhs.getInstallTime()) {
|
|
return 1;
|
|
} else if (rhs.getInstallTime() < lhs.getInstallTime()) {
|
|
return -1;
|
|
} else {
|
|
return 0;
|
|
}
|
|
});
|
|
for (GameInstall info : sortedList) {
|
|
if (!ids.contains(info.getId())) {
|
|
ids.add(info.getId());
|
|
}
|
|
}
|
|
initGameList(ids);
|
|
});
|
|
}
|
|
}
|
|
|
|
//获取游戏简介
|
|
private void initGameList(List<String> ids) {
|
|
final List<GameEntity> result = new ArrayList<>();
|
|
|
|
List<Observable<GameEntity>> sequences = new ArrayList<>();
|
|
for (String id : ids) {
|
|
sequences.add(RetrofitManager.getInstance(mContext).getApi().getGameDigest(id));
|
|
}
|
|
Observable.mergeDelayError(sequences)
|
|
.map(ApkActiveUtils.filterMapper)
|
|
.subscribeOn(Schedulers.io())
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
.subscribe(new Response<GameEntity>() {
|
|
@Override
|
|
public void onComplete() {
|
|
processingData(result);
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(@Nullable HttpException e) {
|
|
processingData(result);
|
|
}
|
|
|
|
@Override
|
|
public void onNext(GameEntity response) {
|
|
ApkActiveUtils.filterHideApk(response);
|
|
result.add(response);
|
|
}
|
|
});
|
|
}
|
|
|
|
private void processingData(List<GameEntity> gameList) {
|
|
if (gameList.size() != 0) {
|
|
for (int i = 0, size = sortedList.size(); i < size; i++) {
|
|
String id = sortedList.get(i).getId();
|
|
for (GameEntity entity : gameList) {
|
|
if (entity.getId().equals(id)) {
|
|
GameEntity newEntity = entity.clone();
|
|
newEntity.setLibaoExists(entity.isLibaoExists());
|
|
if (newEntity.getApk().size() > 1) {
|
|
for (ApkEntity apkEntity : newEntity.getApk()) {
|
|
if (sortedList.get(i).getPackageName().equals(apkEntity.getPackageName())) {
|
|
ArrayList<ApkEntity> list = new ArrayList<>();
|
|
list.add(apkEntity);
|
|
newEntity.setApk(list);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
this.gameList.add(newEntity);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
for (GameEntity entity : this.gameList) {
|
|
entity.setEntryMap(DownloadManager.getInstance(mContext).getEntryMap(entity.getName()));
|
|
}
|
|
}
|
|
|
|
if (this.gameList.size() != 0) {
|
|
isRemove = true;
|
|
notifyDataSetChanged();
|
|
|
|
initLocationMap();
|
|
} else {
|
|
mActivity.loadEmpty();
|
|
}
|
|
}
|
|
|
|
private void initLocationMap() {
|
|
locationMap.clear();
|
|
GameEntity gameEntity;
|
|
ArrayList<Integer> list;
|
|
for (int i = 0; i < gameList.size(); i++) {
|
|
gameEntity = gameList.get(i);
|
|
if (gameEntity.getApk() != null && gameEntity.getApk().size() != 0) {
|
|
for (ApkEntity apkEntity : gameEntity.getApk()) {
|
|
list = locationMap.get(apkEntity.getPackageName());
|
|
if (list == null) {
|
|
list = new ArrayList<>();
|
|
locationMap.put(apkEntity.getPackageName(), list);
|
|
}
|
|
list.add(i);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public ArrayList<GameEntity> getGameList() {
|
|
return gameList;
|
|
}
|
|
|
|
public ArrayMap<String, ArrayList<Integer>> getLocationMap() {
|
|
return locationMap;
|
|
}
|
|
|
|
@Override
|
|
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
|
switch (viewType) {
|
|
case ItemViewType.GAME_NORMAL:
|
|
return new GameNormalSwipeViewHolder(
|
|
mLayoutInflater.inflate(R.layout.game_normal_item_swipe, parent, false));
|
|
case ItemViewType.LOADING:
|
|
return new FooterViewHolder(
|
|
mLayoutInflater.inflate(R.layout.refresh_footerview, parent, false));
|
|
case ItemViewType.KC_HINT:
|
|
return new KcHintViewHolder(
|
|
mLayoutInflater.inflate(R.layout.installfragment_footerview, parent, false));
|
|
default:
|
|
break;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public void onBindViewHolder(final ViewHolder holder, int position) {
|
|
if (holder instanceof GameNormalSwipeViewHolder) {
|
|
initGameNormal((GameNormalSwipeViewHolder) holder, gameList.get(position), position);
|
|
} else if (holder instanceof KcHintViewHolder) {
|
|
((KcHintViewHolder) holder).mTextView.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
sp.edit().putBoolean("showKcHint", false).apply();
|
|
smoothListener.onSmooth();
|
|
((KcHintViewHolder) holder).mTextView.postDelayed(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
showKcHint = false;
|
|
notifyDataSetChanged();
|
|
}
|
|
}, 500);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public int getItemViewType(int position) {
|
|
if (gameList.size() != 0 && position >= 0 && position < gameList.size()) {
|
|
return ItemViewType.GAME_NORMAL;
|
|
}
|
|
|
|
if (isRemove && showKcHint) {
|
|
return ItemViewType.KC_HINT;
|
|
}
|
|
|
|
return ItemViewType.LOADING;
|
|
}
|
|
|
|
@Override
|
|
public int getItemCount() {
|
|
if (gameList.isEmpty() && !isRemove) {
|
|
return 1;
|
|
}
|
|
if (isRemove && showKcHint) {
|
|
return gameList.size() + 1;
|
|
}
|
|
if (isRemove) {
|
|
return gameList.size();
|
|
}
|
|
return gameList.size() + 1;
|
|
}
|
|
|
|
private void initGameNormal(final GameNormalSwipeViewHolder holder, final GameEntity gameEntity, final int i) {
|
|
|
|
gameEntity.setCollection(new ArrayList<>()); // 清空集合,防止下载按钮会因为存在集合而清空合集包的状态判断
|
|
holder.initServerType(gameEntity);
|
|
|
|
holder.swipeText.setPadding(0, 0, DisplayUtils.dip2px(mContext, 15), 0);
|
|
String name;
|
|
if (gameEntity.getApk().size() > 0) {
|
|
name = String.format("%s - %s", gameEntity.getName(),
|
|
PlatformUtils.getInstance(mContext).getPlatformName(gameEntity.getApk().get(0).getPlatform()));
|
|
holder.gameThumb.setImageDrawable(PackageUtils.getIconByPackage(mContext, gameEntity.getApk().get(0).getPackageName()));
|
|
holder.gameDes.setText(String.format("V%s", PackageUtils.getVersionByPackage(mContext, gameEntity.getApk().get(0).getPackageName())));
|
|
} else {
|
|
name = gameEntity.getName();
|
|
ImageUtils.display(holder.gameThumb, gameEntity.getIcon());
|
|
holder.gameDes.setText(gameEntity.getBrief());
|
|
}
|
|
|
|
if (!holder.gameName.getText().equals(name)) {
|
|
holder.gameName.setText(name);
|
|
}
|
|
if (!holder.gameName.isSelected()) {
|
|
holder.gameName.setSelected(true);
|
|
}
|
|
|
|
GameViewUtils.setLabelList(mContext, holder.gameLabelList, gameEntity.getTag(), "", gameEntity.getTagStyle());
|
|
|
|
holder.cardView.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
if (!isSwipe) {
|
|
DataCollectionUtils.uploadClick(mContext, "列表", "我的光环-我的游戏", gameEntity.getName());
|
|
|
|
GameDetailActivity.startGameDetailActivity(mContext, gameEntity.getId(), "(我的光环:我的游戏)");
|
|
}
|
|
}
|
|
});
|
|
|
|
DownloadItemUtils.setOnClickListener(mContext, holder.gameDownloadBtn, gameEntity, i,
|
|
this, "(我的光环:我的游戏)", "我的光环-我的游戏" + ":" + gameEntity.getName());
|
|
|
|
DownloadItemUtils.updateItem(mContext, gameEntity, holder, false);
|
|
|
|
holder.swipeLayout.close();
|
|
holder.swipeShareText.setPadding((maxWidth / 12), 0, 0, 0);
|
|
holder.swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
|
|
@Override
|
|
public void onStartOpen(SwipeLayout layout) {
|
|
isSwipe = true;
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onOpen(SwipeLayout layout) {
|
|
skipKc(i);
|
|
holder.swipeLayout.close();
|
|
}
|
|
|
|
@Override
|
|
public void onStartClose(SwipeLayout layout) {
|
|
}
|
|
|
|
@Override
|
|
public void onClose(SwipeLayout layout) {
|
|
isSwipe = false;
|
|
holder.swipeText.setText("右划发给好友\n免流量安装");
|
|
}
|
|
|
|
@Override
|
|
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
|
|
if (leftOffset > maxWidth / 2.7 && leftOffset < maxWidth / 2.5) {
|
|
holder.swipeText.setVisibility(View.GONE);
|
|
holder.swipeShareText.setVisibility(View.VISIBLE);
|
|
} else if (leftOffset > maxWidth / 2.5) {
|
|
holder.swipeText.setVisibility(View.GONE);
|
|
holder.swipeShareText.setVisibility(View.VISIBLE);
|
|
holder.swipeShareText.setText("放手发给好友\n免流量安装");
|
|
} else if (leftOffset < maxWidth / 2.7) {
|
|
holder.swipeText.setVisibility(View.VISIBLE);
|
|
holder.swipeShareText.setVisibility(View.GONE);
|
|
holder.swipeShareText.setText("右划发给好友\n免流量安装");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
|
|
}
|
|
});
|
|
}
|
|
|
|
//跳转到快传 - 搜索接收者页面
|
|
public void skipKc(int i) {
|
|
GameEntity gameEntity = gameList.get(i);
|
|
if (gameEntity.getApk().size() == 0) return;
|
|
String packageName = gameEntity.getApk().get(0).getPackageName();
|
|
PackageManager pm = mContext.getPackageManager();
|
|
FileInfo fileInfo = new FileInfo();
|
|
List<PackageInfo> installedPackages = pm.getInstalledPackages(0);
|
|
for (PackageInfo installedPackage : installedPackages) {
|
|
if ((installedPackage.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0
|
|
&& installedPackage.packageName.equals(packageName)) {
|
|
fileInfo.setFilePath(installedPackage.applicationInfo.sourceDir);
|
|
File file = new File(installedPackage.applicationInfo.sourceDir);
|
|
fileInfo.setSize(file.length());
|
|
Drawable drawable = installedPackage.applicationInfo.loadIcon(pm);
|
|
fileInfo.setBitmap(BitmapUtils.drawableToBitmap(drawable));
|
|
fileInfo.setFileTag(String.valueOf(System.currentTimeMillis()));
|
|
}
|
|
}
|
|
if (gameEntity.getApk() == null || gameEntity.getApk().isEmpty()
|
|
|| gameEntity.getTag() == null || gameEntity.getTag().isEmpty()) {
|
|
fileInfo.setName(gameEntity.getName());
|
|
} else {
|
|
fileInfo.setName(String.format("%s - %s", gameEntity.getName(),
|
|
PlatformUtils.getInstance(mContext).getPlatformName(gameEntity.getApk().get(0).getPlatform())));
|
|
}
|
|
fileInfo.setPackageName(packageName);
|
|
List<FileInfo> fileInfos = new ArrayList<>();
|
|
fileInfos.add(fileInfo);
|
|
|
|
HaloApp.put(KcSelectGameActivity.KEY_FILE_INFO, fileInfos);
|
|
smoothListener.onOpen();
|
|
}
|
|
|
|
public interface onSmoothLayoutListener {
|
|
void onSmooth();
|
|
|
|
void onOpen(); // 启动跳转
|
|
}
|
|
|
|
public class KcHintViewHolder extends ViewHolder {
|
|
TextView mTextView;
|
|
|
|
public KcHintViewHolder(View itemView) {
|
|
super(itemView);
|
|
mTextView = (TextView) itemView.findViewById(R.id.kuaichuan_hint);
|
|
}
|
|
}
|
|
|
|
}
|