Merge branch 'dev-4.7.0' of git.ghzs.com:halo/assistant-android into dev-4.7.0

This commit is contained in:
张玉久
2021-01-26 15:28:30 +08:00
23 changed files with 394 additions and 37 deletions

View File

@ -732,26 +732,29 @@ fun Context.doOnMainProcessOnly(callback: EmptyCallback) {
doOnMainProcessOnly { callback.onCallback() }
}
/**
* 虽然现在我们没有了友盟以后只是单进程APP但在 debug 模式下还有 whatTheStack 这个进程如果不限定主进程会出现奇奇怪怪的问题 (BroadcastReceiver相关)
*/
inline fun Context.doOnMainProcessOnly(f: () -> Unit) {
// val processName = PackageUtils.obtainProcessName(this)
// if (processName == null || BuildConfig.APPLICATION_ID == processName) {
val processName = PackageUtils.obtainProcessName(this)
if (processName == null || BuildConfig.APPLICATION_ID == processName) {
f.invoke()
// } else {
// tryWithDefaultCatch {
// Utils.log("Block one useless sub process method call from ${Thread.currentThread().stackTrace[3].methodName} -> ${Thread.currentThread().stackTrace[2].methodName}.")
// }
// }
} else {
tryWithDefaultCatch {
Utils.log("Block one useless sub process method call from ${Thread.currentThread().stackTrace[3].methodName} -> ${Thread.currentThread().stackTrace[2].methodName}.")
}
}
}
inline fun doOnMainProcessOnly(f: () -> Unit) {
// val processName = PackageUtils.obtainProcessName(HaloApp.getInstance().application)
// if (processName == null || BuildConfig.APPLICATION_ID == processName) {
val processName = PackageUtils.obtainProcessName(HaloApp.getInstance().application)
if (processName == null || BuildConfig.APPLICATION_ID == processName) {
f.invoke()
// } else {
// tryWithDefaultCatch {
// Utils.log("Block one useless sub process method call from ${Thread.currentThread().stackTrace[3].methodName} -> ${Thread.currentThread().stackTrace[2].methodName}.")
// }
// }
} else {
tryWithDefaultCatch {
Utils.log("Block one useless sub process method call from ${Thread.currentThread().stackTrace[3].methodName} -> ${Thread.currentThread().stackTrace[2].methodName}.")
}
}
}
/**

View File

@ -119,15 +119,25 @@ object PackageInstaller {
*/
@JvmStatic
fun uninstall(context: Context, path: String) {
val packageName = PackageUtils.getPackageNameByPath(context, path)
uninstallForPackageName(context, packageName)
}
/**
* 根据包名卸载应用
*/
@JvmStatic
fun uninstallForPackageName(context: Context, pkn: String) {
if (pkn.isBlank()) return
val uninstallIntent = Intent()
uninstallIntent.action = Intent.ACTION_DELETE
uninstallIntent.addCategory(Intent.CATEGORY_DEFAULT)
if (context !is Activity) {
uninstallIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
val packageName = PackageUtils.getPackageNameByPath(context, path)
uninstallIntent.data = Uri.parse("package:$packageName")
InstallUtils.getInstance(context).addUninstall(packageName)
uninstallIntent.data = Uri.parse("package:$pkn")
InstallUtils.getInstance(context).addUninstall(pkn)
context.startActivity(uninstallIntent)
}

View File

@ -52,6 +52,7 @@ import com.lightgame.download.DownloadService;
import com.lightgame.download.DownloadStatus;
import com.lightgame.download.DownloadStatusListener;
import com.lightgame.download.DownloadStatusManager;
import com.lightgame.download.DownloadTask;
import com.lightgame.download.FileUtils;
import com.lightgame.download.HttpDnsManager;
import com.lightgame.utils.Utils;
@ -633,7 +634,7 @@ public class DownloadManager implements DownloadStatusListener {
FileUtils.deleteFile(entry.getPath());
}
mDownloadDao.delete(url);
Utils.log(DownloadManager.class.getSimpleName(), "cancel==>file and record were deleted!");
Utils.log(DownloadManager.class.getSimpleName(), "cancel==>record were deleted!");
}
if (entry != null) {
if (automatic) {
@ -642,7 +643,23 @@ public class DownloadManager implements DownloadStatusListener {
entry.getMeta().put(DownloadDataHelper.DOWNLOAD_CANCEL_WAY, DownloadDataHelper.DOWNLOAD_CANCEL_MANUAL);
}
entry.setStatus(DownloadStatus.cancel);
startDownloadService(entry, DownloadStatus.cancel);
// startDownloadService(entry, DownloadStatus.cancel);
// 将原来安装完成后在 downloadService 完成的功能放到这里,尝试避免因为 ANR 造成闪退
mDownloadDao.removeErrorMessage(entry.getUrl());
DownloadTask task = DataChanger.INSTANCE.getDownloadingTasks().get(entry.getUrl());
if (task != null) {
task.cancel();
// 改任务队列的状态
DataChanger.INSTANCE.getDownloadingTasks().remove(entry.getUrl());
DataChanger.INSTANCE.notifyDataChanged(entry);
}
DataChanger.INSTANCE.getDownloadEntries().remove(entry.getUrl());
DataChanger.INSTANCE.notifyDataChanged(entry);
DownloadStatusManager.getInstance().onTaskCancelled(entry);
Utils.log(DownloadManager.class.getSimpleName(), "cancel");
}
}

View File

@ -1,12 +1,16 @@
package com.gh.gamecenter.adapter.viewholder
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.gh.base.BaseRecyclerViewHolder
import com.gh.gamecenter.R
import kotterknife.bindView
class SimulatorManagementViewHolder(view: View) : BaseRecyclerViewHolder<Any>(view) {
val container by bindView<RelativeLayout>(R.id.container)
val simulatorName by bindView<TextView>(R.id.simulator_name)
val selectIv by bindView<ImageView>(R.id.select_iv)
val simulatorInstallBtn by bindView<TextView>(R.id.simulator_install_btn)
}

View File

@ -39,8 +39,11 @@ class SimulatorGameFragment : NormalFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mViewModel = viewModelProvider()
}
override fun onResume() {
super.onResume()
mViewModel.getSimulators()
}
@ -86,6 +89,7 @@ class SimulatorGameFragment : NormalFragment() {
}
}
} else {
mReuseNoData.visibility = View.GONE
list.forEachIndexed { index, entity ->
mTabContainer.goneIf(list.size == 1)
mTypeAliasList.add(entity.typeAlias)
@ -96,6 +100,7 @@ class SimulatorGameFragment : NormalFragment() {
if (mViewPager.adapter != null) {
// 删除了一个tab里面所有游戏后刷新adapter
mViewPager.adapter?.notifyDataSetChanged()
updateTabLayout()
} else {
initViewPager()
}
@ -135,12 +140,16 @@ class SimulatorGameFragment : NormalFragment() {
setIndicatorWidth(20)
}
updateTabLayout()
BaseFragment_TabLayout.initTabStyle(mTabLayout, mViewPager.currentItem)
}
private fun updateTabLayout() {
for (i in 0 until mTabLayout.tabCount) {
val tab = mTabLayout.getTabAt(i) ?: continue
val tabTitle = if (tab.text != null) tab.text.toString() else ""
val tabView = BaseFragment_TabLayout.createDefaultTabCustomView(tabTitle)
tab.customView = tabView
}
BaseFragment_TabLayout.initTabStyle(mTabLayout, mViewPager.currentItem)
}
}

View File

@ -1,18 +1,37 @@
package com.gh.gamecenter.simulatorgame
import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.g00fy2.versioncompare.Version
import com.gh.common.simulator.SimulatorDownloadManager
import com.gh.common.util.PackageUtils
import com.gh.common.util.toColor
import com.gh.common.util.*
import com.gh.gamecenter.R
import com.gh.gamecenter.adapter.viewholder.SimulatorManagementViewHolder
import com.gh.gamecenter.baselist.ListAdapter
import com.gh.gamecenter.entity.SimulatorEntity
class SimulatorManagementAdapter(context: Context) : ListAdapter<SimulatorEntity>(context) {
class SimulatorManagementAdapter(context: Context,
var fragment: SimulatorManagementFragment) : ListAdapter<SimulatorEntity>(context) {
private var mSingleSelectedPosition = -1
private var mSingleSelectFlag = false
private var mShowSelectFlag = false
private var mSelectList = ArrayList<Boolean>()
private var mPopWindow: PopupWindow? = null
override fun setListData(updateData: MutableList<SimulatorEntity>?) {
mSelectList.clear()
updateData?.forEach { _ ->
mSelectList.add(false)
}
super.setListData(updateData)
}
override fun getItemCount() = mEntityList.size
@ -22,28 +41,156 @@ class SimulatorManagementAdapter(context: Context) : ListAdapter<SimulatorEntity
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder) {
is SimulatorManagementViewHolder -> {
val simulator = mEntityList[position]
val isInstalled = PackageUtils.isInstalledFromAllPackage(mContext, simulator.apk?.packageName)
holder.simulatorInstallBtn.run {
val simulator = mEntityList[position]
holder.simulatorName.text = simulator.name
if (PackageUtils.isInstalledFromAllPackage(mContext, simulator.apk?.packageName)) {
var shouldShowUpdate = false
if (isInstalled) {
val versionFromInstalledApp = PackageUtils.getVersionByPackage(simulator.apk?.packageName)
val shouldShowUpdate = Version(simulator.apk?.version).isHigherThan(versionFromInstalledApp)
shouldShowUpdate = Version(simulator.apk?.version).isHigherThan(versionFromInstalledApp)
text = if (shouldShowUpdate) "更新" else "已安装"
setTextColor(if (shouldShowUpdate) R.color.theme.toColor() else R.color.text_999999.toColor())
setOnClickListener {
if (shouldShowUpdate) {
SimulatorDownloadManager.getInstance().showDownloadDialog(context, simulator, SimulatorDownloadManager.SimulatorLocation.SIMULATOR_MANAGE)
}
}
} else {
text = "安装"
setTextColor(R.color.theme.toColor())
setOnClickListener {
}
setOnClickListener {
if (isInstalled) {
if (shouldShowUpdate) {
SimulatorDownloadManager.getInstance().showDownloadDialog(context, simulator, SimulatorDownloadManager.SimulatorLocation.SIMULATOR_MANAGE)
} else {
holder.itemView.performClick()
}
} else {
SimulatorDownloadManager.getInstance().showDownloadDialog(mContext, simulator, SimulatorDownloadManager.SimulatorLocation.SIMULATOR_MANAGE)
}
}
}
holder.selectIv.run {
goneIf(!mShowSelectFlag)
setImageResource(when {
mSelectList[position] && isInstalled -> R.drawable.ic_simulator_selected
!mSelectList[position] && isInstalled -> R.drawable.ic_simulator_unselect
else -> {
mSelectList[position] = false
R.drawable.ic_simulator_unable
}
})
setOnClickListener {
if (isInstalled) {
setImageResource(if (mSelectList[position])
R.drawable.ic_simulator_unselect
else
R.drawable.ic_simulator_selected)
mSelectList[position] = !mSelectList[position]
}
}
}
if (mSingleSelectFlag) {
holder.container.setBackgroundResource(R.color.bg_F6FBFF)
} else {
holder.container.setBackgroundResource(R.color.white)
}
holder.itemView.setOnClickListener {
if (isInstalled && !mShowSelectFlag) {
showUnInstallWindow(position)
}
}
}
}
}
private fun showUnInstallWindow(position: Int = -1) {
if (mPopWindow != null && mPopWindow!!.isShowing) {
if (!mShowSelectFlag) mSingleSelectFlag = false
mPopWindow?.dismiss()
} else if (mPopWindow != null) {
if (!mShowSelectFlag) mSingleSelectFlag = true
mPopWindow?.showAtLocation(fragment.requireActivity().window.decorView, Gravity.BOTTOM, 0, 0)
} else {
if (!mShowSelectFlag) mSingleSelectFlag = true
val contentView = View.inflate(mContext, R.layout.popup_simulator_uninstall, null)
contentView.isFocusable = true
contentView.isFocusableInTouchMode = true
val unInstallBtn = contentView.findViewById<LinearLayout>(R.id.unInstallBtn)
mPopWindow = PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
mPopWindow?.showAtLocation(fragment.requireActivity().window.decorView, Gravity.BOTTOM, 0, 0)
unInstallBtn.setOnClickListener {
val dialog = DialogUtils.showNewAlertDialog(mContext,
"卸载模拟器",
"即将卸载模拟器,是否确定卸载?",
"取消",
"确定",
{},
{ unInstallSimulator() })
dialog?.window?.findViewById<TextView>(R.id.content)?.setTextColor(R.color.text_333333.toColor())
}
}
if (!mShowSelectFlag) {
if (mSingleSelectFlag) {
mSingleSelectedPosition = position
}
if (mSingleSelectedPosition != -1) {
notifyItemChanged(mSingleSelectedPosition)
}
}
}
private fun unInstallSimulator() {
if (mShowSelectFlag) {
if (mSelectList.contains(true)) {
mSelectList.forEachIndexed { index, b ->
if (b) {
val simulator = mEntityList[index]
PackageInstaller.uninstallForPackageName(mContext, simulator.apk?.packageName ?: "")
}
}
} else {
ToastUtils.showToast("请选择模拟器")
}
} else {
val simulator = mEntityList[mSingleSelectedPosition]
PackageInstaller.uninstallForPackageName(mContext, simulator.apk?.packageName ?: "")
dismissPopWindow()
}
}
fun showUnInstallWindowAndSelectIv(show: Boolean) {
mShowSelectFlag = show
mSingleSelectFlag = false
mSingleSelectedPosition = -1
for (i in 0 until mSelectList.size) {
mSelectList[i] = false
}
notifyDataSetChanged()
dismissPopWindow(true)
if (show) showUnInstallWindow()
}
fun dismissPopWindow(isForce: Boolean = false) {
if (!isForce && mShowSelectFlag) return
if (mPopWindow != null && mPopWindow!!.isShowing) {
mSingleSelectFlag = false
mPopWindow?.dismiss()
}
if (mSingleSelectedPosition != -1) {
notifyItemChanged(mSingleSelectedPosition)
}
}
}

View File

@ -1,11 +1,18 @@
package com.gh.gamecenter.simulatorgame
import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.gh.common.util.EntranceUtils
import com.gh.common.util.dip2px
import com.gh.common.util.viewModelProvider
import com.gh.common.view.SpacingItemDecoration
import com.gh.gamecenter.R
import com.gh.gamecenter.baselist.ListFragment
import com.gh.gamecenter.databinding.FragmentSimulatorListBinding
import com.gh.gamecenter.entity.SimulatorEntity
import com.gh.gamecenter.eventbus.EBPackage
import org.greenrobot.eventbus.Subscribe
@ -14,6 +21,7 @@ import org.greenrobot.eventbus.ThreadMode
class SimulatorManagementFragment : ListFragment<SimulatorEntity, SimulatorManagementViewModel>() {
private lateinit var mSimulatorEntity: SimulatorEntity
private lateinit var mBinding: FragmentSimulatorListBinding
private var mAdapter: SimulatorManagementAdapter? = null
//安装、卸载事件
@ -24,6 +32,8 @@ class SimulatorManagementFragment : ListFragment<SimulatorEntity, SimulatorManag
}
}
override fun getInflatedLayout() = FragmentSimulatorListBinding.inflate(layoutInflater).apply { mBinding = this }.root
override fun getItemDecoration() = SpacingItemDecoration(onlyDecorateTheFirstItem = true, top = 8F.dip2px())
override fun onCreate(savedInstanceState: Bundle?) {
@ -33,8 +43,37 @@ class SimulatorManagementFragment : ListFragment<SimulatorEntity, SimulatorManag
super.onCreate(savedInstanceState)
}
override fun provideListAdapter() = mAdapter ?: SimulatorManagementAdapter(requireContext()).apply { mAdapter = this }
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
initMenu(R.menu.menu_simulator_uninstall)
}
override fun onMenuItemClick(menuItem: MenuItem) {
super.onMenuItemClick(menuItem)
if (menuItem.itemId == R.id.menu_simulator_uninstall) {
val unInstallIv = menuItem.actionView.findViewById<ImageView>(R.id.unInstallIv)
val completeTv = menuItem.actionView.findViewById<TextView>(R.id.completeTv)
val showFlag = unInstallIv.visibility == View.VISIBLE
if (showFlag) {
unInstallIv.visibility = View.GONE
completeTv.visibility = View.VISIBLE
} else {
unInstallIv.visibility = View.VISIBLE
completeTv.visibility = View.GONE
}
mAdapter?.showUnInstallWindowAndSelectIv(showFlag)
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
mBinding.blankView.setOnClickListener {
mAdapter?.dismissPopWindow()
}
}
override fun provideListAdapter() = mAdapter ?: SimulatorManagementAdapter(requireContext(), this).apply { mAdapter = this }
override fun provideListViewModel() = viewModelProvider<SimulatorManagementViewModel>()