From 6efc1e06bb19342ff7de796ce2dd0f3fb53f9006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=8E=89=E4=B9=85?= <1484288157@qq.com> Date: Mon, 12 Oct 2020 16:21:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E6=A8=A1=E6=8B=9F=E5=99=A8?= =?UTF-8?q?=E6=B8=B8=E6=88=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gh/common/databind/BindingAdapters.java | 25 ++++++++--- .../emulator/EmulatorDownloadManager.kt | 5 +++ .../gh/common/emulator/EmulatorGameManager.kt | 44 +++++++++++++++++++ .../com/gh/common/util/DownloadItemUtils.java | 12 ++++- .../com/gh/common/util/DownloadObserver.kt | 11 +++++ .../java/com/gh/common/util/GameUtils.java | 16 +++++-- .../java/com/gh/download/DownloadManager.java | 21 ++++++--- .../java/com/gh/gamecenter/MainActivity.java | 8 ++-- .../adapter/viewholder/DetailViewHolder.java | 12 +++++ .../gamecenter/entity/EmulatorGameEntity.kt | 6 +++ .../personal/PersonalFunctionAdapter.kt | 5 +++ 11 files changed, 146 insertions(+), 19 deletions(-) create mode 100644 app/src/main/java/com/gh/common/emulator/EmulatorDownloadManager.kt create mode 100644 app/src/main/java/com/gh/common/emulator/EmulatorGameManager.kt create mode 100644 app/src/main/java/com/gh/gamecenter/entity/EmulatorGameEntity.kt diff --git a/app/src/main/java/com/gh/common/databind/BindingAdapters.java b/app/src/main/java/com/gh/common/databind/BindingAdapters.java index ba5d6f9f00..b99c42c802 100644 --- a/app/src/main/java/com/gh/common/databind/BindingAdapters.java +++ b/app/src/main/java/com/gh/common/databind/BindingAdapters.java @@ -14,17 +14,12 @@ import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; -import androidx.annotation.Nullable; -import androidx.appcompat.app.AppCompatActivity; -import androidx.core.content.ContextCompat; -import androidx.databinding.BindingAdapter; -import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; - import com.facebook.drawee.view.SimpleDraweeView; import com.gh.base.OnViewClickListener; import com.gh.common.constant.Config; import com.gh.common.dialog.CertificationDialog; import com.gh.common.dialog.ReserveDialogFragment; +import com.gh.common.emulator.EmulatorGameManager; import com.gh.common.exposure.ExposureEvent; import com.gh.common.history.HistoryHelper; import com.gh.common.repository.ReservationRepository; @@ -45,6 +40,7 @@ import com.gh.common.util.PackageUtils; import com.gh.common.util.PermissionHelper; import com.gh.common.util.PlatformUtils; import com.gh.common.util.ReservationHelper; +import com.gh.common.util.ToastUtils; import com.gh.common.view.DownloadProgressBar; import com.gh.common.view.DrawableView; import com.gh.common.view.GameIconView; @@ -57,6 +53,7 @@ import com.gh.gamecenter.baselist.LoadStatus; import com.gh.gamecenter.databinding.KaifuAddItemBinding; import com.gh.gamecenter.databinding.KaifuDetailItemRowBinding; import com.gh.gamecenter.entity.ApkEntity; +import com.gh.gamecenter.entity.EmulatorGameEntity; import com.gh.gamecenter.entity.GameEntity; import com.gh.gamecenter.entity.LinkEntity; import com.gh.gamecenter.entity.PluginLocation; @@ -77,6 +74,12 @@ import java.util.ArrayList; import java.util.List; import java.util.Locale; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.content.ContextCompat; +import androidx.databinding.BindingAdapter; +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; + /** * Created by khy on 12/02/18. */ @@ -461,6 +464,14 @@ public class BindingAdapters { break; case LAUNCH_OR_OPEN: if (gameEntity.getApk().size() == 1) { + //启动模拟器游戏 + if (EmulatorGameManager.formats.contains(gameEntity.getApk().get(0).getFormat())) { + EmulatorGameEntity emulatorGameEntity = EmulatorGameManager.findGameByName(gameEntity.getName()); + if (emulatorGameEntity != null) { + ToastUtils.INSTANCE.showToast("启动模拟器游戏-" + emulatorGameEntity.getGameName()); + } + return; + } DataUtils.onGameLaunchEvent(v.getContext(), gameEntity.getName(), gameEntity.getApk().get(0).getPlatform(), location); PackageUtils.launchApplicationByPackageName(v.getContext(), gameEntity.getApk().get(0).getPackageName()); } else { @@ -516,7 +527,7 @@ public class BindingAdapters { HistoryHelper.insertGameEntity(gameEntity); } - Intent i = new Intent(WebActivity.getIntentForWebGame(progressBar.getContext(), linkEntity.getLink(), gameEntity.getName(), isPlay,linkEntity.getCloseButton())); + Intent i = new Intent(WebActivity.getIntentForWebGame(progressBar.getContext(), linkEntity.getLink(), gameEntity.getName(), isPlay, linkEntity.getCloseButton())); progressBar.getContext().startActivity(i); break; } diff --git a/app/src/main/java/com/gh/common/emulator/EmulatorDownloadManager.kt b/app/src/main/java/com/gh/common/emulator/EmulatorDownloadManager.kt new file mode 100644 index 0000000000..e941d437ce --- /dev/null +++ b/app/src/main/java/com/gh/common/emulator/EmulatorDownloadManager.kt @@ -0,0 +1,5 @@ +package com.gh.common.emulator + +class EmulatorDownloadManager { + +} \ No newline at end of file diff --git a/app/src/main/java/com/gh/common/emulator/EmulatorGameManager.kt b/app/src/main/java/com/gh/common/emulator/EmulatorGameManager.kt new file mode 100644 index 0000000000..945796f68e --- /dev/null +++ b/app/src/main/java/com/gh/common/emulator/EmulatorGameManager.kt @@ -0,0 +1,44 @@ +package com.gh.common.emulator + +import com.gh.common.runOnIoThread +import com.gh.gamecenter.entity.EmulatorGameEntity +import com.halo.assistant.HaloApp +import com.lightgame.download.FileUtils +import java.io.File + +object EmulatorGameManager { + + @JvmField + val formats = arrayListOf("gba", "zip", "nes", "iso", "smc", "gbc") + + @JvmField + val gamePath = FileUtils.getDownloadPath(HaloApp.getInstance().application, "emulator_game") + + var localGameSet = hashSetOf() + + //刷新本地已下载的所有的游戏 + @JvmStatic + fun refreshLocalPackageList() { + localGameSet.clear() + runOnIoThread { + val directory = File(gamePath) + if (directory.exists()) { + val fileList = directory.list() + fileList.forEach { + val file = File("${gamePath}/${it}") + if (!file.isDirectory) { + val entity = EmulatorGameEntity(file.name.substring(0, file.name.lastIndexOf('.')), file.path) + localGameSet.add(entity) + } + } + } + + } + } + + @JvmStatic + fun findGameByName(name: String?): EmulatorGameEntity? { + return localGameSet.find { it.gameName == name ?: "" } + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/gh/common/util/DownloadItemUtils.java b/app/src/main/java/com/gh/common/util/DownloadItemUtils.java index 89163114fe..b9760e86a4 100644 --- a/app/src/main/java/com/gh/common/util/DownloadItemUtils.java +++ b/app/src/main/java/com/gh/common/util/DownloadItemUtils.java @@ -12,6 +12,7 @@ import com.gh.common.constant.Config; import com.gh.common.dialog.CertificationDialog; import com.gh.common.dialog.DeviceRemindDialog; import com.gh.common.dialog.ReserveDialogFragment; +import com.gh.common.emulator.EmulatorGameManager; import com.gh.common.exposure.ExposureEvent; import com.gh.common.history.HistoryHelper; import com.gh.common.repository.ReservationRepository; @@ -24,6 +25,7 @@ import com.gh.gamecenter.R; import com.gh.gamecenter.WebActivity; import com.gh.gamecenter.adapter.viewholder.GameViewHolder; import com.gh.gamecenter.entity.ApkEntity; +import com.gh.gamecenter.entity.EmulatorGameEntity; import com.gh.gamecenter.entity.GameEntity; import com.gh.gamecenter.entity.LinkEntity; import com.gh.gamecenter.entity.PluginLocation; @@ -468,7 +470,7 @@ public class DownloadItemUtils { HistoryHelper.insertGameEntity(gameEntity); } - Intent i = WebActivity.getIntentForWebGame(context, gameEntity.getH5Link().getLink(), gameEntity.getName(), isPlay,linkEntity.getCloseButton()); + Intent i = WebActivity.getIntentForWebGame(context, gameEntity.getH5Link().getLink(), gameEntity.getName(), isPlay, linkEntity.getCloseButton()); context.startActivity(i); }); } else if (gameEntity.getApk().size() == 1) { @@ -572,6 +574,14 @@ public class DownloadItemUtils { } else if (str.equals(context.getString(R.string.install))) { install(context, gameEntity, position, adapter); } else if (str.equals(context.getString(R.string.launch))) { + //启动模拟器游戏 + if (EmulatorGameManager.formats.contains(gameEntity.getApk().get(0).getFormat())) { + EmulatorGameEntity emulatorGameEntity = EmulatorGameManager.findGameByName(gameEntity.getName()); + if (emulatorGameEntity != null) { + ToastUtils.INSTANCE.showToast("启动模拟器游戏-" + emulatorGameEntity.getGameName()); + } + return; + } if (entrance.contains("我的游戏")) { MtaHelper.onEvent("我的游戏_启动", "启动", gameEntity.getName()); } diff --git a/app/src/main/java/com/gh/common/util/DownloadObserver.kt b/app/src/main/java/com/gh/common/util/DownloadObserver.kt index 1c11c59755..c86d32ff23 100644 --- a/app/src/main/java/com/gh/common/util/DownloadObserver.kt +++ b/app/src/main/java/com/gh/common/util/DownloadObserver.kt @@ -5,6 +5,7 @@ import android.os.Build import android.preference.PreferenceManager import com.gh.base.BaseActivity import com.gh.common.constant.Constants +import com.gh.common.emulator.EmulatorGameManager import com.gh.common.exposure.ExposureUtils import com.gh.common.xapk.XapkInstaller import com.gh.download.DownloadDataHelper @@ -130,6 +131,16 @@ object DownloadObserver { Utils.toast(mApplication, downloadEntity.name + " - 下载完成") } if (!downloadEntity.isPluggable) { + + val path = downloadEntity.path + val format = path.substring(path.lastIndexOf(".") + 1) + // 模拟器游戏下载成功 + if (EmulatorGameManager.formats.contains(format)) { + downloadManager.cancel(downloadEntity.url, false, false) + EmulatorGameManager.refreshLocalPackageList() + return + } + // 是否是自动安装 if (PreferenceManager.getDefaultSharedPreferences(mApplication).getBoolean(SettingsFragment.AUTO_INSTALL_SP_KEY, true)) { if (FileUtils.isEmptyFile(downloadEntity.path)) { diff --git a/app/src/main/java/com/gh/common/util/GameUtils.java b/app/src/main/java/com/gh/common/util/GameUtils.java index e5e5ea4b56..dd36413da1 100644 --- a/app/src/main/java/com/gh/common/util/GameUtils.java +++ b/app/src/main/java/com/gh/common/util/GameUtils.java @@ -5,13 +5,13 @@ import android.graphics.Color; import android.text.TextUtils; import android.widget.TextView; -import androidx.annotation.Nullable; - import com.gh.common.constant.Config; +import com.gh.common.emulator.EmulatorGameManager; import com.gh.download.DownloadManager; import com.gh.gamecenter.R; import com.gh.gamecenter.entity.ApkEntity; import com.gh.gamecenter.entity.ApkLink; +import com.gh.gamecenter.entity.EmulatorGameEntity; import com.gh.gamecenter.entity.GameCollectionEntity; import com.gh.gamecenter.entity.GameEntity; import com.gh.gamecenter.entity.GameUpdateEntity; @@ -24,6 +24,8 @@ import com.lightgame.download.DownloadStatus; import java.util.ArrayList; import java.util.List; +import androidx.annotation.Nullable; + public class GameUtils { /** @@ -116,6 +118,14 @@ public class GameUtils { installCount++; } } + // TODO: 修改后缀 + apkEntity.setFormat("nes"); + if (EmulatorGameManager.formats.contains(apkEntity.getFormat())) { + EmulatorGameEntity emulatorGameEntity = EmulatorGameManager.findGameByName(gameEntity.getName()); + if (emulatorGameEntity != null) { + installCount++; + } + } } if (doneCount != 0) { return context.getString(R.string.install); @@ -238,7 +248,7 @@ public class GameUtils { } } } - + collectionEntity.setSaveApkEntity(saveApkEntity); return collectionEntity; } diff --git a/app/src/main/java/com/gh/download/DownloadManager.java b/app/src/main/java/com/gh/download/DownloadManager.java index eb80e2668d..7ebca09824 100644 --- a/app/src/main/java/com/gh/download/DownloadManager.java +++ b/app/src/main/java/com/gh/download/DownloadManager.java @@ -8,12 +8,9 @@ import android.os.Looper; import android.os.Message; import android.text.TextUtils; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.collection.ArrayMap; - import com.gh.common.AppExecutor; import com.gh.common.constant.Constants; +import com.gh.common.emulator.EmulatorGameManager; import com.gh.common.exposure.ExposureEvent; import com.gh.common.exposure.ExposureUtils; import com.gh.common.history.HistoryHelper; @@ -61,6 +58,10 @@ import java.util.List; import java.util.Set; import java.util.concurrent.LinkedBlockingQueue; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.collection.ArrayMap; + public class DownloadManager implements DownloadStatusListener { private static DownloadManager mInstance; @@ -240,10 +241,20 @@ public class DownloadManager implements DownloadStatusListener { } } + //TODO: 模拟器游戏下载测试 + apkEntity.setFormat("nes"); + + String path; + if (EmulatorGameManager.formats.contains(apkEntity.getFormat())) { + path = EmulatorGameManager.gamePath + "/" + gameEntity.getName() + "." + apkEntity.getFormat(); + } else { + path = PackageInstaller.getDownloadPath(gameEntity.getName(), apkEntity.getFormat()); + } + DownloadEntity downloadEntity = new DownloadEntity(); downloadEntity.setUrl(apkEntity.getUrl()); downloadEntity.setName(gameEntity.getName()); - downloadEntity.setPath(PackageInstaller.getDownloadPath(gameEntity.getName(), apkEntity.getFormat())); + downloadEntity.setPath(path); downloadEntity.setETag(apkEntity.getEtag()); downloadEntity.setIcon(gameEntity.getIcon()); downloadEntity.setPlatform(apkEntity.getPlatform()); diff --git a/app/src/main/java/com/gh/gamecenter/MainActivity.java b/app/src/main/java/com/gh/gamecenter/MainActivity.java index e07464cd37..aaf0c3c17a 100644 --- a/app/src/main/java/com/gh/gamecenter/MainActivity.java +++ b/app/src/main/java/com/gh/gamecenter/MainActivity.java @@ -23,9 +23,6 @@ import android.view.View; import android.view.Window; import android.widget.TextView; -import androidx.annotation.NonNull; -import androidx.lifecycle.ViewModelProviders; - import com.gh.base.AppUncaughtHandler; import com.gh.base.BaseActivity; import com.gh.base.fragment.BaseFragment_ViewPager; @@ -34,6 +31,7 @@ import com.gh.common.DefaultUrlHandler; import com.gh.common.avoidcallback.AvoidOnResultManager; import com.gh.common.constant.Config; import com.gh.common.constant.Constants; +import com.gh.common.emulator.EmulatorGameManager; import com.gh.common.exposure.meta.MetaUtil; import com.gh.common.im.ImManager; import com.gh.common.loghub.LoghubUtils; @@ -122,6 +120,8 @@ import java.util.TimerTask; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; +import androidx.annotation.NonNull; +import androidx.lifecycle.ViewModelProviders; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.schedulers.Schedulers; import okhttp3.MediaType; @@ -271,6 +271,8 @@ public class MainActivity extends BaseActivity { HomePluggableHelper.activationFilterData(); }); + // 刷新本地已下载的模拟器游戏 + EmulatorGameManager.refreshLocalPackageList(); //启动app删除视频缓存文件 diff --git a/app/src/main/java/com/gh/gamecenter/adapter/viewholder/DetailViewHolder.java b/app/src/main/java/com/gh/gamecenter/adapter/viewholder/DetailViewHolder.java index acc83304a5..c64fd192e1 100644 --- a/app/src/main/java/com/gh/gamecenter/adapter/viewholder/DetailViewHolder.java +++ b/app/src/main/java/com/gh/gamecenter/adapter/viewholder/DetailViewHolder.java @@ -9,6 +9,7 @@ import com.gh.common.dialog.CertificationDialog; import com.gh.common.dialog.DeviceRemindDialog; import com.gh.common.dialog.GameOffServiceDialogFragment; import com.gh.common.dialog.ReserveDialogFragment; +import com.gh.common.emulator.EmulatorGameManager; import com.gh.common.exposure.ExposureEvent; import com.gh.common.history.HistoryHelper; import com.gh.common.util.CheckLoginUtils; @@ -24,6 +25,7 @@ import com.gh.common.util.PackageUtils; import com.gh.common.util.PermissionHelper; import com.gh.common.util.ReservationHelper; import com.gh.common.util.StringUtils; +import com.gh.common.util.ToastUtils; import com.gh.common.view.DownloadProgressBar; import com.gh.download.DownloadManager; import com.gh.download.dialog.DownloadDialog; @@ -31,6 +33,7 @@ import com.gh.gamecenter.DownloadManagerActivity; import com.gh.gamecenter.R; import com.gh.gamecenter.WebActivity; import com.gh.gamecenter.entity.ApkEntity; +import com.gh.gamecenter.entity.EmulatorGameEntity; import com.gh.gamecenter.entity.GameEntity; import com.gh.gamecenter.entity.LinkEntity; import com.gh.gamecenter.eventbus.EBReuse; @@ -187,6 +190,15 @@ public class DetailViewHolder { break; case LAUNCH_OR_OPEN: if (mGameEntity.getApk().size() == 1) { + //启动模拟器游戏 + if(EmulatorGameManager.formats.contains(mGameEntity.getApk().get(0).getFormat())){ + EmulatorGameEntity emulatorGameEntity = EmulatorGameManager.findGameByName(mGameEntity.getName()); + if (emulatorGameEntity != null) { + ToastUtils.INSTANCE.showToast("启动模拟器游戏-"+emulatorGameEntity.getGameName()); + } + return; + } + DataUtils.onGameLaunchEvent(mViewHolder.context, mGameEntity.getName(), mGameEntity.getApk().get(0).getPlatform(), mName); PackageUtils.launchApplicationByPackageName(mViewHolder.context, mGameEntity.getApk().get(0).getPackageName()); } else { diff --git a/app/src/main/java/com/gh/gamecenter/entity/EmulatorGameEntity.kt b/app/src/main/java/com/gh/gamecenter/entity/EmulatorGameEntity.kt new file mode 100644 index 0000000000..a4d8c5edbc --- /dev/null +++ b/app/src/main/java/com/gh/gamecenter/entity/EmulatorGameEntity.kt @@ -0,0 +1,6 @@ +package com.gh.gamecenter.entity + +data class EmulatorGameEntity( + var gameName: String = "", + var filePath: String = "" +) \ No newline at end of file diff --git a/app/src/main/java/com/gh/gamecenter/personal/PersonalFunctionAdapter.kt b/app/src/main/java/com/gh/gamecenter/personal/PersonalFunctionAdapter.kt index 640c5ec854..08680cc724 100644 --- a/app/src/main/java/com/gh/gamecenter/personal/PersonalFunctionAdapter.kt +++ b/app/src/main/java/com/gh/gamecenter/personal/PersonalFunctionAdapter.kt @@ -11,6 +11,7 @@ import android.widget.TextView import androidx.recyclerview.widget.RecyclerView import com.facebook.drawee.view.SimpleDraweeView import com.gh.common.constant.Constants +import com.gh.common.emulator.EmulatorGameManager import com.gh.common.util.* import com.gh.gamecenter.* import com.gh.gamecenter.db.GameTrendsDao @@ -105,6 +106,10 @@ class PersonalFunctionAdapter(val context: Context, val groupName: String, var m MtaHelper.onEvent("我的光环_新", "功能入口", "$groupName+${linkEntity.name}") when (linkEntity.type) { "我的游戏" -> { + debugOnly { + EmulatorGameManager.refreshLocalPackageList() + return + } if (UserManager.getInstance().isLoggedIn) { MtaHelper.onEvent("我的光环", "我的游戏") context.startActivity(Intent(context, MyGameActivity::class.java))