我的光环-我的游戏完成

This commit is contained in:
kehaoyuan
2018-05-25 14:10:57 +08:00
parent ffbbd44beb
commit 2992e3ca64
20 changed files with 356 additions and 172 deletions

View File

@ -1,5 +1,6 @@
package com.gh.common.databind;
import android.content.Intent;
import android.databinding.BindingAdapter;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
@ -8,15 +9,25 @@ 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.entity.ApkEntity;
import com.gh.gamecenter.entity.GameEntity;
import com.gh.gamecenter.manager.PackageManager;
import com.lightgame.download.DownloadEntity;
import com.lightgame.download.FileUtils;
import com.lightgame.utils.Utils;
/**
* Created by khy on 12/02/18.
@ -189,42 +200,108 @@ public class BindingAdapters {
}
// 显示下载过程状态
if (gameEntity.getApk().size() == 1) {
// DownloadEntity downloadEntity = gameEntity.getEntryMap().get(gameEntity.getApk().get(0).getPackageName());
String url = gameEntity.getApk().get(0).getUrl();
DownloadEntity downloadEntity = DownloadManager.getInstance(progressBar.getContext()).getDownloadEntityByUrl(url);
if (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;
}
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);
}
}
}

View File

@ -1,6 +1,7 @@
package com.gh.common.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
@ -38,7 +39,9 @@ public class DownloadProgressBar extends ProgressBar {
private String mText;
private boolean mIsRectStyle;
private int mDefaultColor;
private int mTextSize;
public DownloadProgressBar(Context context) {
super(context);
@ -46,6 +49,12 @@ public class DownloadProgressBar extends ProgressBar {
public DownloadProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
if (attrs != null) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.DownloadProgressBar);
mIsRectStyle = ta.getBoolean(R.styleable.DownloadProgressBar_rectStyle, false);
mTextSize = ta.getDimensionPixelSize(R.styleable.DownloadProgressBar_textSize, DisplayUtils.sp2px(getContext(), 14));
ta.recycle();
}
setMax(MAX_LENGTH);
setProgressDrawable(getResources().getDrawable(R.drawable.game_item_btn_download_style));
}
@ -66,7 +75,7 @@ public class DownloadProgressBar extends ProgressBar {
if (TextUtils.isEmpty(mText)) return;
mPaint.setColor(mDefaultColor == 0 ? ContextCompat.getColor(getContext(), R.color.theme) : mDefaultColor); // 初始化颜色
mPaint.setTextSize(DisplayUtils.sp2px(getContext(), 14));
mPaint.setTextSize(mTextSize);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setXfermode(null);
create();
@ -100,29 +109,35 @@ public class DownloadProgressBar extends ProgressBar {
switch (downloadType) {
case NORMAL:
case INSTALL_NORMAL:
setProgressDrawable(getResources().getDrawable(R.drawable.game_item_btn_download_style));
setProgressDrawable(getResources().getDrawable(mIsRectStyle
? R.drawable.detail_download_normal_rect_style : R.drawable.game_item_btn_download_style));
mDefaultColor = Color.WHITE;
break;
case PLUGIN:
case INSTALL_PLUGIN:
setProgressDrawable(getResources().getDrawable(R.drawable.game_item_btn_plugin_style));
setProgressDrawable(getResources().getDrawable(mIsRectStyle
? R.drawable.detail_download_plugin_install_rect_style : R.drawable.game_item_btn_plugin_style));
mDefaultColor = Color.WHITE;
break;
case NONE:
setProgressDrawable(getResources().getDrawable(R.drawable.news_detail_comment));
setProgressDrawable(getResources().getDrawable(mIsRectStyle
? R.drawable.detail_download_none_rect_style : R.drawable.news_detail_comment));
mDefaultColor = ContextCompat.getColor(getContext(), R.color.hint);
break;
case LAUNCH_OR_OPEN:
setProgress(0);
setProgressDrawable(getResources().getDrawable(R.drawable.detail_downloading_normal_style));
setProgressDrawable(getResources().getDrawable(mIsRectStyle
? R.drawable.detail_download_open_rect_style : R.drawable.detail_download_open_style));
mDefaultColor = ContextCompat.getColor(getContext(), R.color.theme);
break;
case DOWNLOADING_NORMAL: // todo bug 和列表用相同的drawable会复用
setProgressDrawable(getResources().getDrawable(R.drawable.detail_downloading_normal_style2));
case DOWNLOADING_NORMAL:
setProgressDrawable(getResources().getDrawable(mIsRectStyle
? R.drawable.detail_downloading_normal_rect_style : R.drawable.detail_downloading_normal_style));
mDefaultColor = ContextCompat.getColor(getContext(), R.color.theme);
break;
case DOWNLOADING_PLUGIN:
setProgressDrawable(getResources().getDrawable(R.drawable.detail_downloading_plugin_style));
setProgressDrawable(getResources().getDrawable(mIsRectStyle
? R.drawable.detail_downloading_plugin_rect_style : R.drawable.detail_downloading_plugin_style));
mDefaultColor = ContextCompat.getColor(getContext(), R.color.btn_plugin);
break;
}

View File

@ -43,7 +43,9 @@ import com.gh.gamecenter.entity.LoginTokenEntity;
import com.gh.gamecenter.entity.SignEntity;
import com.gh.gamecenter.entity.UserInfoEntity;
import com.gh.gamecenter.eventbus.EBConcernChanged;
import com.gh.gamecenter.eventbus.EBDownloadStatus;
import com.gh.gamecenter.eventbus.EBNetworkState;
import com.gh.gamecenter.eventbus.EBPackage;
import com.gh.gamecenter.eventbus.EBReuse;
import com.gh.gamecenter.eventbus.EBSkip;
import com.gh.gamecenter.login.ApiResponse;
@ -145,7 +147,7 @@ public class PersonalFragment extends BaseFragment implements Observer<ApiRespon
private DataWatcher dataWatcher = new DataWatcher() {
@Override
public void onDataChanged(DownloadEntity downloadEntity) {
mInstalledViewModel.downloadChange(downloadEntity);
mGameAdapter.notifyItemById(downloadEntity.getGameId());
}
};
@ -502,4 +504,15 @@ public class PersonalFragment extends BaseFragment implements Observer<ApiRespon
super.onResume();
DownloadManager.getInstance(getContext()).addObserver(dataWatcher);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(EBDownloadStatus busFour) {
if ("delete".equals(busFour.getStatus()))
mGameAdapter.notifyDataSetChanged();
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(EBPackage busFour) {
mInstalledViewModel.initInstalledData();
}
}

View File

@ -6,8 +6,8 @@ import android.arch.lifecycle.MutableLiveData
import android.support.v4.util.ArrayMap
import com.gh.common.util.ApkActiveUtils
import com.gh.common.util.PackageUtils
import com.gh.common.util.TrafficUtils
import com.gh.download.DownloadManager
import com.gh.gamecenter.db.info.InstallInfo
import com.gh.gamecenter.entity.ApkEntity
import com.gh.gamecenter.entity.GameEntity
import com.gh.gamecenter.entity.GameInfoEntity
@ -16,7 +16,6 @@ import com.gh.gamecenter.manager.InstallManager
import com.gh.gamecenter.retrofit.Response
import com.gh.gamecenter.retrofit.RetrofitManager
import com.halo.assistant.HaloApp
import com.lightgame.download.DownloadEntity
import rx.Observable
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
@ -34,14 +33,18 @@ class InstallGameViewModel(application: Application) : AndroidViewModel(applicat
var mCacheGameData = ArrayList<GameEntity>()
init {
initInstalledData()
}
fun initInstalledData() {
val sortList = ArrayList<GameInfoEntity>()
val cManager = InstallManager(getApplication())
val runnableGame = cManager.allInstall
if (runnableGame.isEmpty()) {
val installedGame = cManager.allInstall
if (installedGame.isEmpty()) {
gameEntity.postValue(null)
} else {
HaloApp.getInstance().mainExecutor.execute {
init(sortList)
transformationData(installedGame, sortList)
// 按安装时间排序
sortList.sortWith(Comparator { lhs, rhs ->
@ -53,27 +56,18 @@ class InstallGameViewModel(application: Application) : AndroidViewModel(applicat
}
}
//初始化 todo 整理
private fun init(sortList: ArrayList<GameInfoEntity>) {
//转换数据(游戏多包名拆分)
private fun transformationData(installedGame: List<InstallInfo>, sortList: ArrayList<GameInfoEntity>) {
val list = ArrayList<GameInfoEntity>()
val signatureList = ArrayList<GameInfoEntity>() // 是我们签名的游戏
val unsignatureList = ArrayList<GameInfoEntity>() // 不是我们签名的游戏
val noopenList = ArrayList<GameInfoEntity>() // 未打开过的游戏
val oftenuseList = ArrayList<GameInfoEntity>() // 已经打开过的游戏
val unSignatureList = ArrayList<GameInfoEntity>() // 不是我们签名的游戏
val trafficUtils = TrafficUtils.getInstance(getApplication())
val cManager = InstallManager(getApplication())
val runnableGame = cManager.allInstall
for (concernEntity in runnableGame) {
for (concernEntity in installedGame) {
for ((key, value) in concernEntity.packageNames) {
if (value) {
val info = GameInfoEntity()
info.id = concernEntity.id
info.packageName = key
info.traffic = trafficUtils.getTraffice(key)
info.isSignature = PackageUtils.isSignature(getApplication(), key)
info.installTime = PackageUtils.getInstalledTime(getApplication(), key)
list.add(info)
@ -94,7 +88,8 @@ class InstallGameViewModel(application: Application) : AndroidViewModel(applicat
mList.add(list[i])
i++
}
var comparator: Comparator<GameInfoEntity> = Comparator { lhs, rhs -> rhs.id!!.compareTo(lhs.id!!) }
val comparator: Comparator<GameInfoEntity> = Comparator { lhs, rhs -> rhs.id!!.compareTo(lhs.id!!) }
var gh_id: Any?
for (key in map.keys) {
mList = map[key]
@ -110,45 +105,11 @@ class InstallGameViewModel(application: Application) : AndroidViewModel(applicat
}
}
} else {
unsignatureList.add(mList[0])
unSignatureList.add(mList[0])
}
}
for (info in signatureList) {
if (info.traffic == 0L) {
noopenList.add(info)
} else {
oftenuseList.add(info)
}
}
comparator = Comparator { lhs, rhs ->
if (rhs.installTime > lhs.installTime) {
1
} else if (rhs.installTime < lhs.installTime) {
-1
} else {
0
}
}
Collections.sort(noopenList, comparator)
comparator = Comparator { lhs, rhs ->
if (rhs.traffic > lhs.traffic) {
1
} else if (rhs.traffic < lhs.traffic) {
-1
} else {
0
}
}
Collections.sort(oftenuseList, comparator)
Collections.sort(unsignatureList, comparator)
sortList.addAll(noopenList)
sortList.addAll(oftenuseList)
sortList.addAll(unsignatureList)
sortList.addAll(signatureList)
sortList.addAll(unSignatureList)
}
private fun getGameData(sortList: ArrayList<GameInfoEntity>) {
@ -188,7 +149,7 @@ class InstallGameViewModel(application: Application) : AndroidViewModel(applicat
manager.addOrUpdate(entity)
}
mCacheGameData = result as ArrayList<GameEntity>;
mCacheGameData = result
gameEntity.postValue(result)
} else {
gameEntity.postValue(null)
@ -201,17 +162,4 @@ class InstallGameViewModel(application: Application) : AndroidViewModel(applicat
}
})
}
fun downloadChange(downloadEntity: DownloadEntity) {
// var gameList: List<GameEntity>? = gameEntity.value ?: return
for (game in mCacheGameData) {
if (game.getApk().size == 1 && downloadEntity.packageName == game.getApk()[0].packageName) {
val entryMap = game.getEntryMap()
entryMap[downloadEntity.platform] = downloadEntity
gameEntity.postValue(mCacheGameData.clone() as List<GameEntity>?)
break
}
}
}
}

View File

@ -1,9 +1,6 @@
package com.gh.gamecenter.personal.installed
import android.annotation.SuppressLint
import android.content.Context
import android.os.AsyncTask
import android.support.v7.util.DiffUtil
import android.support.v7.widget.RecyclerView
import android.view.ViewGroup
import com.gh.base.BaseRecyclerViewHolder
@ -12,55 +9,23 @@ import com.gh.gamecenter.databinding.PersonalGameItemBinding
import com.gh.gamecenter.entity.GameEntity
import com.lightgame.adapter.BaseRecyclerAdapter
import java.util.*
import kotlin.collections.HashMap
class PersonalInstallGameAdapter(context: Context) : BaseRecyclerAdapter<RecyclerView.ViewHolder>(context) {
var mEntityList = ArrayList<GameEntity>()
var mPositionAndIdMap = HashMap<String, Int>() // key: gameId + position, value: position
@SuppressLint("StaticFieldLeak")
fun setListData(updateData: List<GameEntity>?) {
if (updateData == null) {
mEntityList = ArrayList()
notifyDataSetChanged()
return
}
mPositionAndIdMap.clear()
mEntityList = updateData as ArrayList<GameEntity>
notifyDataSetChanged()
}
if (mEntityList.size > updateData.size) {
mEntityList = ArrayList(updateData)
notifyDataSetChanged()
return
}
object : AsyncTask<Void, Void, DiffUtil.DiffResult>() {
override fun doInBackground(vararg voids: Void): DiffUtil.DiffResult {
return DiffUtil.calculateDiff(object : DiffUtil.Callback() {
override fun getOldListSize(): Int {
return mEntityList.size
}
override fun getNewListSize(): Int {
return updateData.size
}
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
val oldItem = mEntityList[oldItemPosition]
val newItem = updateData[newItemPosition]
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
val oldItem = mEntityList[oldItemPosition]
val newItem = updateData[newItemPosition]
return oldItem.getEntryMap() == newItem.getEntryMap()
}
})
}
override fun onPostExecute(diffResult: DiffUtil.DiffResult) {
mEntityList = ArrayList(updateData)
diffResult.dispatchUpdatesTo(this@PersonalInstallGameAdapter)
}
}.execute()
// notifyItemRemoved notifyItemInserted 会出现异常
override fun getItemViewType(position: Int): Int {
mPositionAndIdMap[mEntityList[position].id + position] = position
return super.getItemViewType(position)
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
@ -76,6 +41,11 @@ class PersonalInstallGameAdapter(context: Context) : BaseRecyclerAdapter<Recycle
override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
if (holder is InstallGameViewHolder) {
holder.bind.game = mEntityList[position]
holder.adapterPosition
holder.itemView.setOnLongClickListener({
notifyItemRemoved(1)
return@setOnLongClickListener true
})
}
}
@ -83,4 +53,11 @@ class PersonalInstallGameAdapter(context: Context) : BaseRecyclerAdapter<Recycle
val bind: PersonalGameItemBinding = binding
}
fun notifyItemById(gameId: String) {
for (key in mPositionAndIdMap.keys) {
if (key.contains(gameId)) {
notifyItemChanged(mPositionAndIdMap[key]!!)
}
}
}
}

View File

@ -226,7 +226,9 @@ public class AnswerCommentFragment extends ListFragment<CommentEntity, AnswerCom
mCommentSend.setEnabled(false);
}
mCommentListener.onCommentDraftChange(mCommentDetailCommentEt.getText().toString());
if (mCommentListener != null) {
mCommentListener.onCommentDraftChange(mCommentDetailCommentEt.getText().toString());
}
}
@Override