Skip to content

Commit

Permalink
clean ups around deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
F43nd1r committed Jul 9, 2023
1 parent 1ca7ae2 commit 1bf91f3
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import org.acra.util.SystemServices.getTelephonyManager
* @author F43nd1r
* @since 4.9.1
*/
@Suppress("DEPRECATION")
@AutoService(Collector::class)
class DeviceIdCollector : BaseReportFieldCollector(ReportField.DEVICE_ID) {
override fun shouldCollect(context: Context, config: CoreConfiguration, collect: ReportField, reportBuilder: ReportBuilder): Boolean {
Expand All @@ -47,7 +48,6 @@ class DeviceIdCollector : BaseReportFieldCollector(ReportField.DEVICE_ID) {
@RequiresPermission(Manifest.permission.READ_PHONE_STATE)
@Throws(Exception::class)
override fun collect(reportField: ReportField, context: Context, config: CoreConfiguration, reportBuilder: ReportBuilder, target: CrashReportData) {
@Suppress("DEPRECATION")
val deviceId = if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) getTelephonyManager(context).deviceId else null
target.put(ReportField.DEVICE_ID, deviceId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ class MediaCodecListCollector : BaseReportFieldCollector(ReportField.MEDIA_CODEC
// Retrieve list of possible Color Format
for (f in codecCapabilitiesClass.fields) {
if (Modifier.isStatic(f.modifiers) && Modifier.isFinal(f.modifiers)
&& f.name.startsWith(COLOR_FORMAT_PREFIX)) {
&& f.name.startsWith(COLOR_FORMAT_PREFIX)
) {
mColorFormatValues.put(f.getInt(null), f.name)
}
}
Expand Down Expand Up @@ -108,28 +109,23 @@ class MediaCodecListCollector : BaseReportFieldCollector(ReportField.MEDIA_CODEC
*
* @return The media codecs information
*/
@Suppress("DEPRECATION")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Throws(JSONException::class)
private fun collectMediaCodecList(): JSONObject {
prepare()
val infos: Array<MediaCodecInfo?>
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
val codecCount = MediaCodecList.getCodecCount()
infos = arrayOfNulls(codecCount)
for (codecIdx in 0 until codecCount) {
infos[codecIdx] = MediaCodecList.getCodecInfoAt(codecIdx)
}
val infos: Array<MediaCodecInfo?> = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
MediaCodecList(MediaCodecList.ALL_CODECS).codecInfos
} else {
infos = MediaCodecList(MediaCodecList.ALL_CODECS).codecInfos
@Suppress("DEPRECATION")
(0 until MediaCodecList.getCodecCount()).map { MediaCodecList.getCodecInfoAt(it) }.toTypedArray()
}
val result = JSONObject()
for (i in infos.indices) {
val codecInfo = infos[i]
val codec = JSONObject()
val supportedTypes = codecInfo!!.supportedTypes
codec.put("name", codecInfo.name)
.put("isEncoder", codecInfo.isEncoder)
.put("isEncoder", codecInfo.isEncoder)
val supportedTypesJson = JSONObject()
for (type in supportedTypes) {
supportedTypesJson.put(type, collectCapabilitiesForType(codecInfo, type))
Expand Down
43 changes: 17 additions & 26 deletions acra-core/src/main/java/org/acra/collector/MemoryInfoCollector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class MemoryInfoCollector : BaseReportFieldCollector(ReportField.DUMPSYS_MEMINFO
val process = Runtime.getRuntime().exec(commandLine.toTypedArray())
StreamReader(process.inputStream).read()
} catch (e: IOException) {
error(e) { "MemoryInfoCollector.meminfo could not retrieve data"}
error(e) { "MemoryInfoCollector.meminfo could not retrieve data" }
null
}
}
Expand All @@ -75,21 +75,16 @@ class MemoryInfoCollector : BaseReportFieldCollector(ReportField.DUMPSYS_MEMINFO
*
* @return Number of bytes available.
*/
@Suppress("DEPRECATION")
private fun getAvailableInternalMemorySize(): Long {
val path = Environment.getDataDirectory()
val stat = StatFs(path.path)
val blockSize: Long
val availableBlocks: Long
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
blockSize = stat.blockSizeLong
availableBlocks = stat.availableBlocksLong
} else {
blockSize = stat.blockSize.toLong()
availableBlocks = stat.availableBlocks.toLong()
}
return availableBlocks * blockSize
val path = Environment.getDataDirectory()
val stat = StatFs(path.path)
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
stat.blockSizeLong * stat.availableBlocksLong
} else {
@Suppress("DEPRECATION")
stat.blockSize.toLong() * stat.availableBlocks.toLong()
}
}

/**
* Calculates the total memory of the device. This is based on an inspection of the filesystem, which in android devices is stored in RAM.
Expand All @@ -98,17 +93,13 @@ class MemoryInfoCollector : BaseReportFieldCollector(ReportField.DUMPSYS_MEMINFO
*/
@Suppress("DEPRECATION")
private fun getTotalInternalMemorySize(): Long {
val path = Environment.getDataDirectory()
val stat = StatFs(path.path)
val blockSize: Long
val totalBlocks: Long
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
blockSize = stat.blockSizeLong
totalBlocks = stat.blockCountLong
} else {
blockSize = stat.blockSize.toLong()
totalBlocks = stat.blockCount.toLong()
}
return totalBlocks * blockSize
val path = Environment.getDataDirectory()
val stat = StatFs(path.path)
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
stat.blockSizeLong * stat.blockCountLong
} else {
@Suppress("DEPRECATION")
stat.blockSize.toLong() * stat.blockCount.toLong()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import org.acra.builder.ReportBuilder
import org.acra.config.CoreConfiguration
import org.acra.data.CrashReportData
import org.acra.util.PackageManagerWrapper
import org.acra.util.versionCodeLong

/**
* Collects [PackageInfo] values
* Collects [android.content.pm.PackageInfo] values
*
* @author F43nd1r
* @since 4.9.1
Expand All @@ -37,10 +38,9 @@ class PackageManagerCollector : BaseReportFieldCollector(ReportField.APP_VERSION
if (info == null) {
throw CollectorException("Failed to get package info")
} else {
@Suppress("NON_EXHAUSTIVE_WHEN", "DEPRECATION")
when (reportField) {
ReportField.APP_VERSION_NAME -> target.put(ReportField.APP_VERSION_NAME, info.versionName)
ReportField.APP_VERSION_CODE -> target.put(ReportField.APP_VERSION_CODE, info.versionCode)
ReportField.APP_VERSION_CODE -> target.put(ReportField.APP_VERSION_CODE, info.versionCodeLong)
else -> throw IllegalArgumentException()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@ class SharedPreferencesCollector : BaseReportFieldCollector(ReportField.USER_EMA
* Collects all key/value pairs in SharedPreferences.
* The application default SharedPreferences are always
* collected, and the developer can provide additional SharedPreferences
* names in the [org.acra.annotation.AcraCore.additionalSharedPreferences]
* names in the [org.acra.config.CoreConfiguration.additionalSharedPreferences]
* configuration item.
*
* @return the collected key/value pairs.
*/
@Suppress("DEPRECATION")
@Throws(JSONException::class)
private fun collect(context: Context, config: CoreConfiguration): JSONObject {
val result = JSONObject()

// Include the default SharedPreferences
val sharedPrefs: MutableMap<String, SharedPreferences> = TreeMap()
@Suppress("DEPRECATION")
sharedPrefs["default"] = PreferenceManager.getDefaultSharedPreferences(context)

// Add in any additional SharedPreferences
Expand Down
10 changes: 9 additions & 1 deletion acra-core/src/main/java/org/acra/util/PackageManagerWrapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,12 @@ class PackageManagerWrapper(private val context: Context) {
null
}
}
}
}

val PackageInfo.versionCodeLong
get() = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
longVersionCode
} else {
@Suppress("DEPRECATION")
versionCode.toLong()
}
6 changes: 3 additions & 3 deletions acra-core/src/main/java/org/acra/util/ProcessFinisher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.acra.util

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Process
Expand Down Expand Up @@ -62,16 +63,15 @@ class ProcessFinisher(private val context: Context, private val config: CoreConf
lastActivityManager.clearLastActivities()
}

@Suppress("DEPRECATION")
private fun stopServices() {
if (config.stopServicesOnCrash) {
try {
val activityManager = getActivityManager(context)
@Suppress("DEPRECATION") // this only returns the apps own services on newer android versions which is exactly what we want
val runningServices = activityManager.getRunningServices(Int.MAX_VALUE)
val pid = Process.myPid()
for (serviceInfo in runningServices) {
if (serviceInfo.pid == pid && LegacySenderService::class.java.name != serviceInfo.service.className
&& JobSenderService::class.java.name != serviceInfo.service.className) {
if (serviceInfo.pid == pid && !serviceInfo.service.className.startsWith("org.acra")) {
try {
val intent = Intent()
intent.component = serviceInfo.service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.acra.log.warn
import org.acra.plugins.HasConfigPlugin
import org.acra.prefs.SharedPreferencesFactory
import org.acra.util.PackageManagerWrapper
import org.acra.util.versionCodeLong
import java.io.IOException

/**
Expand All @@ -38,11 +39,11 @@ class LimiterStartupProcessor : HasConfigPlugin(LimiterConfiguration::class.java
val limiterConfiguration = config.getPluginConfiguration<LimiterConfiguration>()
if (limiterConfiguration.deleteReportsOnAppUpdate || limiterConfiguration.resetLimitsOnAppUpdate) {
val prefs = SharedPreferencesFactory(context, config).create()
val lastVersionNr = prefs.getInt(ACRA.PREF_LAST_VERSION_NR, 0).toLong()
val lastVersionNr = prefs.getLong(ACRA.PREF_LAST_VERSION_NR, 0)
val appVersion = getAppVersion(context)
if (appVersion > lastVersionNr) {
if (limiterConfiguration.deleteReportsOnAppUpdate) {
prefs.edit().putInt(ACRA.PREF_LAST_VERSION_NR, appVersion).apply()
prefs.edit().putLong(ACRA.PREF_LAST_VERSION_NR, appVersion).apply()
for (report in reports) {
report.delete = true
}
Expand All @@ -61,7 +62,5 @@ class LimiterStartupProcessor : HasConfigPlugin(LimiterConfiguration::class.java
/**
* @return app version or 0 if PackageInfo was not available.
*/
private fun getAppVersion(context: Context): Int {
return PackageManagerWrapper(context).getPackageInfo()?.versionCode ?: 0
}
private fun getAppVersion(context: Context): Long = PackageManagerWrapper(context).getPackageInfo()?.versionCodeLong ?: 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,5 @@ class EmailIntentSender(private val config: CoreConfiguration) : ReportSender {
private fun PackageManager.queryDefaultActivities(intent: Intent) = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
queryIntentActivities(intent, PackageManager.ResolveInfoFlags.of(PackageManager.MATCH_DEFAULT_ONLY.toLong()))
} else {
@Suppress("DEPRECATION")
queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
}

0 comments on commit 1bf91f3

Please sign in to comment.