diff --git a/app/src/main/java/com/gh/common/util/DataCollectionUtils.java b/app/src/main/java/com/gh/common/util/DataCollectionUtils.java index f1086aba94..d30f9df502 100644 --- a/app/src/main/java/com/gh/common/util/DataCollectionUtils.java +++ b/app/src/main/java/com/gh/common/util/DataCollectionUtils.java @@ -5,6 +5,7 @@ import android.os.Build; import com.gh.gamecenter.common.constant.Constants; import com.gh.gamecenter.common.constant.EntranceConsts; +import com.gh.gamecenter.common.exposure.meta.MetaUtil; import com.gh.gamecenter.common.utils.NetworkUtils; import com.gh.gamecenter.feature.entity.GameEntity; import com.gh.gamecenter.entity.NewsDetailEntity; @@ -54,7 +55,7 @@ public class DataCollectionUtils { map.put("location", downloadEntity.getLocation()); map.put(EntranceConsts.KEY_ENTRANCE, downloadEntity.getEntrance()); map.put("installed", downloadEntity.getInstalled()); - map.put("network", NetworkUtils.getConnectedType(context)); + map.put("network", MetaUtil.getMeta().getNetwork()); DataCollectionManager.onEvent(context, "download", map); } @@ -118,7 +119,7 @@ public class DataCollectionUtils { map.put("key", args[0]); map.put("game_id", args[3]); map.put("game_name", args[4]); - map.put("netword", NetworkUtils.getConnectedType(context)); + map.put("netword", MetaUtil.getMeta().getNetwork()); map.put("type", "search"); map.put("device_type", android.os.Build.MODEL); map.put("device_system", android.os.Build.VERSION.SDK_INT + "=" + android.os.Build.VERSION.RELEASE); @@ -136,7 +137,7 @@ public class DataCollectionUtils { map.put("key", args[0]); map.put("game_id", args[3]); map.put("game_name", args[4]); - map.put("netword", NetworkUtils.getConnectedType(context)); + map.put("netword", MetaUtil.getMeta().getNetwork()); map.put("type", "click"); map.put("device_type", android.os.Build.MODEL); map.put("device_system", android.os.Build.VERSION.SDK_INT + "=" + android.os.Build.VERSION.RELEASE); diff --git a/app/src/main/java/com/gh/common/util/DownloadNotificationHelper.kt b/app/src/main/java/com/gh/common/util/DownloadNotificationHelper.kt index bec8135a10..7bb06fe691 100644 --- a/app/src/main/java/com/gh/common/util/DownloadNotificationHelper.kt +++ b/app/src/main/java/com/gh/common/util/DownloadNotificationHelper.kt @@ -62,60 +62,60 @@ object DownloadNotificationHelper { } else { intent.action = ACTION_DOWNLOAD } - val pendingIntent = PendingIntent.getBroadcast( - HaloApp.getInstance().application, - downloadNotificationId, - intent, - PendingIntent.FLAG_UPDATE_CURRENT - ) - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - val channel = - NotificationChannel(DOWNLOAD_CHANNEL_ID, DOWNLOAD_CHANNEL_ID, NotificationManager.IMPORTANCE_LOW) - notificationManager.createNotificationChannel(channel) - } - - val whenTime = 1000 * 60 * (System.currentTimeMillis() / 1000 / 60) - val builder = NotificationCompat.Builder(HaloApp.getInstance().application, DOWNLOAD_CHANNEL_ID) - .setContentTitle(entity.name) - .setSmallIcon(getNotificationIcon()) - .setContentIntent(pendingIntent) - .setGroup(DOWNLOAD_GROUP_KEY) - .setWhen(whenTime) - - if (xapkStatus == XapkUnzipStatus.FAILURE.name) { - builder.setContentText("《" + entity.name + "》解压失败,点击查看详情~") - } else { - when (entity.status) { - DownloadStatus.downloading -> builder.setContentText( - String.format( - "%s(剩%s)", - SpeedUtils.getSpeed(entity.speed), - SpeedUtils.getRemainTime(entity.size, entity.progress, entity.speed * 1024) - ) - ) - DownloadStatus.done -> builder.setContentText("下载完成,点击立即安装") - DownloadStatus.waiting -> builder.setContentText("等待中") - DownloadStatus.subscribe, - DownloadStatus.timeout, - DownloadStatus.diskisfull, - DownloadStatus.diskioerror, - DownloadStatus.neterror -> builder.setContentText("已暂停,连接WiFi自动下载") - else -> builder.setContentText("暂停中") - } - builder.setProgress(PROGRESS_MAX, entity.percent.toInt(), false) - } - - when (entity.status) { - DownloadStatus.done -> { - builder.setSortKey("A") - builder.setOngoing(true) // 垃圾华为 sortKey 不起效 priority 也不起效,要将下载完成任务的通知置顶只能设置为 ongoing,喷了 - } - DownloadStatus.downloading -> builder.setSortKey("B") - else -> builder.setSortKey("C") - } - tryCatchInRelease { + val pendingIntent = PendingIntent.getBroadcast( + HaloApp.getInstance().application, + downloadNotificationId, + intent, + PendingIntent.FLAG_UPDATE_CURRENT + ) + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val channel = + NotificationChannel(DOWNLOAD_CHANNEL_ID, DOWNLOAD_CHANNEL_ID, NotificationManager.IMPORTANCE_LOW) + notificationManager.createNotificationChannel(channel) + } + + val whenTime = 1000 * 60 * (System.currentTimeMillis() / 1000 / 60) + val builder = NotificationCompat.Builder(HaloApp.getInstance().application, DOWNLOAD_CHANNEL_ID) + .setContentTitle(entity.name) + .setSmallIcon(getNotificationIcon()) + .setContentIntent(pendingIntent) + .setGroup(DOWNLOAD_GROUP_KEY) + .setWhen(whenTime) + + if (xapkStatus == XapkUnzipStatus.FAILURE.name) { + builder.setContentText("《" + entity.name + "》解压失败,点击查看详情~") + } else { + when (entity.status) { + DownloadStatus.downloading -> builder.setContentText( + String.format( + "%s(剩%s)", + SpeedUtils.getSpeed(entity.speed), + SpeedUtils.getRemainTime(entity.size, entity.progress, entity.speed * 1024) + ) + ) + DownloadStatus.done -> builder.setContentText("下载完成,点击立即安装") + DownloadStatus.waiting -> builder.setContentText("等待中") + DownloadStatus.subscribe, + DownloadStatus.timeout, + DownloadStatus.diskisfull, + DownloadStatus.diskioerror, + DownloadStatus.neterror -> builder.setContentText("已暂停,连接WiFi自动下载") + else -> builder.setContentText("暂停中") + } + builder.setProgress(PROGRESS_MAX, entity.percent.toInt(), false) + } + + when (entity.status) { + DownloadStatus.done -> { + builder.setSortKey("A") + builder.setOngoing(true) // 垃圾华为 sortKey 不起效 priority 也不起效,要将下载完成任务的通知置顶只能设置为 ongoing,喷了 + } + DownloadStatus.downloading -> builder.setSortKey("B") + else -> builder.setSortKey("C") + } + val notification = builder.build() // 可能会抛出异常 notification.flags = notification.flags or Notification.FLAG_NO_CLEAR if (xapkStatus == XapkUnzipStatus.FAILURE.name) { diff --git a/app/src/main/java/com/gh/gamecenter/receiver/NetworkStateReceiver.java b/app/src/main/java/com/gh/gamecenter/receiver/NetworkStateReceiver.java index 8baa186820..d18a3a54c0 100644 --- a/app/src/main/java/com/gh/gamecenter/receiver/NetworkStateReceiver.java +++ b/app/src/main/java/com/gh/gamecenter/receiver/NetworkStateReceiver.java @@ -10,6 +10,7 @@ import com.gh.common.repository.ReservationRepository; import com.gh.download.DownloadManager; import com.gh.gamecenter.common.constant.Constants; import com.gh.gamecenter.common.eventbus.EBNetworkState; +import com.gh.gamecenter.common.exposure.meta.MetaUtil; import com.gh.gamecenter.common.utils.ExtensionsKt; import com.gh.gamecenter.common.utils.NetworkUtils; import com.halo.assistant.HaloApp; @@ -23,6 +24,7 @@ public class NetworkStateReceiver extends BroadcastReceiver { // 网络变更这里会被主进程和推送进程分别调用,这里只对主进程响应, // 奇怪,初次注册监听就会有回调,会导致部分接口短时间内触发两次调用 ExtensionsKt.doOnMainProcessOnly(context, () -> { + MetaUtil.updateCachedNetwork(); if (NetworkUtils.isNetworkConnected(context)) { AdDelegateHelper.INSTANCE.requestAdConfig(true, "", null); RegionSettingHelper.getRegionSetting(); diff --git a/module_common/src/main/java/com/gh/gamecenter/common/exposure/meta/Meta.kt b/module_common/src/main/java/com/gh/gamecenter/common/exposure/meta/Meta.kt index 8d66539981..0bb4d73b9f 100644 --- a/module_common/src/main/java/com/gh/gamecenter/common/exposure/meta/Meta.kt +++ b/module_common/src/main/java/com/gh/gamecenter/common/exposure/meta/Meta.kt @@ -14,7 +14,7 @@ data class Meta( val dia: String? = "", val android_sdk: Int? = -1, val android_version: String? = "", - val network: String? = "", + var network: String? = "", val os: String? = "", val gid: String? = "", val oaid: String? = "", diff --git a/module_common/src/main/java/com/gh/gamecenter/common/exposure/meta/MetaUtil.kt b/module_common/src/main/java/com/gh/gamecenter/common/exposure/meta/MetaUtil.kt index 287257ced9..217f84fc1b 100644 --- a/module_common/src/main/java/com/gh/gamecenter/common/exposure/meta/MetaUtil.kt +++ b/module_common/src/main/java/com/gh/gamecenter/common/exposure/meta/MetaUtil.kt @@ -6,6 +6,7 @@ import android.content.Context import android.content.pm.PackageManager import android.net.ConnectivityManager import android.os.Build +import android.os.DeadObjectException import android.provider.Settings import android.telephony.TelephonyManager import android.text.TextUtils @@ -56,6 +57,7 @@ object MetaUtil { ) } + @JvmStatic fun getMeta(): Meta { if (m == null) { refreshMeta() @@ -199,39 +201,52 @@ object MetaUtil { return Build.VERSION.RELEASE } + /** + * 更新通用 meta 里缓存的 network 字段 + */ + @JvmStatic + fun updateCachedNetwork() { + m?.network = getNetwork() + } + fun getNetwork(): String? { + // 有一定机率在 getActiveNetworkInfo 时触发 DeadSystemException,所以这里进行简单 try catch + try { + if (application.checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED) + return "unknown" - if (application.checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED) - return "unknown" + val activeNetwork = + (application.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager).activeNetworkInfo + ?: return "unknown" - val activeNetwork = - (application.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager).activeNetworkInfo - ?: return "unknown" - - return when (activeNetwork.type) { - ConnectivityManager.TYPE_WIFI -> "WIFI" - ConnectivityManager.TYPE_WIMAX -> "WIMAX" - ConnectivityManager.TYPE_MOBILE -> { - val telephonyManager = application.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager - if (telephonyManager.simState != TelephonyManager.SIM_STATE_READY) return "unknown" - when (telephonyManager.networkType) { - // Unknown - TelephonyManager.NETWORK_TYPE_UNKNOWN -> "unknown" - // Cellular Data–2G - TelephonyManager.NETWORK_TYPE_EDGE, TelephonyManager.NETWORK_TYPE_GPRS, TelephonyManager.NETWORK_TYPE_CDMA, - TelephonyManager.NETWORK_TYPE_IDEN, TelephonyManager.NETWORK_TYPE_1xRTT -> "2G" - // Cellular Data–3G - TelephonyManager.NETWORK_TYPE_UMTS, TelephonyManager.NETWORK_TYPE_HSDPA, TelephonyManager.NETWORK_TYPE_HSPA, - TelephonyManager.NETWORK_TYPE_HSPAP, TelephonyManager.NETWORK_TYPE_HSUPA, TelephonyManager.NETWORK_TYPE_EVDO_0, - TelephonyManager.NETWORK_TYPE_EVDO_A, TelephonyManager.NETWORK_TYPE_EVDO_B -> "3G" - // Cellular Data–4G - TelephonyManager.NETWORK_TYPE_LTE -> "4G" - TelephonyManager.NETWORK_TYPE_NR -> "5G" - else -> "unknown" + return when (activeNetwork.type) { + ConnectivityManager.TYPE_WIFI -> "WIFI" + ConnectivityManager.TYPE_WIMAX -> "WIMAX" + ConnectivityManager.TYPE_MOBILE -> { + val telephonyManager = application.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager + if (telephonyManager.simState != TelephonyManager.SIM_STATE_READY) return "unknown" + when (telephonyManager.networkType) { + // Unknown + TelephonyManager.NETWORK_TYPE_UNKNOWN -> "unknown" + // Cellular Data–2G + TelephonyManager.NETWORK_TYPE_EDGE, TelephonyManager.NETWORK_TYPE_GPRS, TelephonyManager.NETWORK_TYPE_CDMA, + TelephonyManager.NETWORK_TYPE_IDEN, TelephonyManager.NETWORK_TYPE_1xRTT -> "2G" + // Cellular Data–3G + TelephonyManager.NETWORK_TYPE_UMTS, TelephonyManager.NETWORK_TYPE_HSDPA, TelephonyManager.NETWORK_TYPE_HSPA, + TelephonyManager.NETWORK_TYPE_HSPAP, TelephonyManager.NETWORK_TYPE_HSUPA, TelephonyManager.NETWORK_TYPE_EVDO_0, + TelephonyManager.NETWORK_TYPE_EVDO_A, TelephonyManager.NETWORK_TYPE_EVDO_B -> "3G" + // Cellular Data–4G + TelephonyManager.NETWORK_TYPE_LTE -> "4G" + TelephonyManager.NETWORK_TYPE_NR -> "5G" + else -> "unknown" + } } + else -> "unknown" } - else -> "unknown" + } catch (e: DeadObjectException) { + e.printStackTrace() + return "unknown" } } diff --git a/module_common/src/main/java/com/gh/gamecenter/common/utils/NetworkUtils.java b/module_common/src/main/java/com/gh/gamecenter/common/utils/NetworkUtils.java index 65a1576a0a..384c4829f3 100644 --- a/module_common/src/main/java/com/gh/gamecenter/common/utils/NetworkUtils.java +++ b/module_common/src/main/java/com/gh/gamecenter/common/utils/NetworkUtils.java @@ -93,49 +93,6 @@ public class NetworkUtils { return quickLoginHelper.isOpenMobileData(context); } - /** - * 获取当前网络连接的类型信息 - * - * @param context 上下文 - * @return 当前网络连接的类型信息 - */ - public static String getConnectedType(Context context) { - if (context != null) { - context = context.getApplicationContext(); - ConnectivityManager mConnectivityManager = (ConnectivityManager) context - .getSystemService(Context.CONNECTIVITY_SERVICE); - NetworkInfo mNetworkInfo = mConnectivityManager - .getActiveNetworkInfo(); - if (mNetworkInfo != null && mNetworkInfo.isAvailable()) { - switch (mNetworkInfo.getType()) { - case ConnectivityManager.TYPE_BLUETOOTH: - return "BLUETOOTH"; - case ConnectivityManager.TYPE_DUMMY: - return "DUMMY"; - case ConnectivityManager.TYPE_ETHERNET: - return "ETHERNET"; - case ConnectivityManager.TYPE_MOBILE: - return "MOBILE"; - case ConnectivityManager.TYPE_MOBILE_DUN: - return "MOBILE_DUN"; - case ConnectivityManager.TYPE_MOBILE_HIPRI: - return "MOBILE_HIPRI"; - case ConnectivityManager.TYPE_MOBILE_MMS: - return "MOBILE_MMS"; - case ConnectivityManager.TYPE_MOBILE_SUPL: - return "MOBILE_SUPL"; - case ConnectivityManager.TYPE_WIFI: - return "WIFI"; - case ConnectivityManager.TYPE_WIMAX: - return "WIMAX"; - default: - break; - } - } - } - return "NONE"; - } - /** * 获取当前移动网络连接的类型信息(当连接的网络是移动网络时使用) *