Skip to content

Commit 2d2d595

Browse files
use runCatching
1 parent 7739c73 commit 2d2d595

21 files changed

Lines changed: 151 additions & 19584 deletions

File tree

app/build.gradle

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,20 @@ dependencies {
6161
}
6262

6363
android {
64-
namespace 'com.vrem.wifianalyzer'
65-
compileSdk 35
64+
namespace = 'com.vrem.wifianalyzer'
65+
compileSdk = 35
6666

6767
sourceSets.each {
6868
it.java.srcDirs += "src/$it.name/kotlin"
6969
}
7070

7171
defaultConfig {
72-
applicationId "com.vrem.wifianalyzer"
72+
applicationId = "com.vrem.wifianalyzer"
7373
minSdkVersion 24
7474
targetSdkVersion 35
7575
versionCode
7676
versionName
77-
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
77+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
7878
}
7979

8080
buildFeatures {
@@ -83,28 +83,28 @@ android {
8383

8484
buildTypes {
8585
release {
86-
minifyEnabled true
87-
shrinkResources true
86+
minifyEnabled = true
87+
shrinkResources = true
8888
proguardFiles getDefaultProguardFile("proguard-android.txt")
8989
signingConfig
9090
}
9191
debug {
92-
applicationIdSuffix ".BETA"
93-
versionNameSuffix "-BETA"
94-
minifyEnabled false
95-
shrinkResources false
96-
debuggable true
97-
enableUnitTestCoverage true
92+
applicationIdSuffix = ".BETA"
93+
versionNameSuffix = "-BETA"
94+
minifyEnabled = false
95+
shrinkResources = false
96+
debuggable = true
97+
enableUnitTestCoverage = true
9898
}
9999
}
100100

101101
testOptions {
102102
unitTests {
103-
includeAndroidResources true
103+
includeAndroidResources = true
104104
all {
105105
jvmArgs("-XX:+EnableDynamicAgentLoading")
106106
testLogging {
107-
events "passed", "skipped", "failed", "standardOut", "standardError"
107+
events = ["passed", "skipped", "failed", "standardOut", "standardError"]
108108
outputs.upToDateWhen { false }
109109
showStandardStreams = true
110110
}
@@ -113,8 +113,8 @@ android {
113113
}
114114

115115
compileOptions {
116-
sourceCompatibility JavaVersion.VERSION_17
117-
targetCompatibility JavaVersion.VERSION_17
116+
sourceCompatibility = JavaVersion.VERSION_17
117+
targetCompatibility = JavaVersion.VERSION_17
118118
}
119119
kotlinOptions {
120120
jvmTarget = "17"

app/build.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Build Properties
2-
#Mon Jul 07 15:36:11 EDT 2025
3-
version_build=10
2+
#Sat Jul 12 11:38:03 EDT 2025
3+
version_build=11
44
version_major=3
55
version_minor=2
66
version_patch=1

app/src/androidTest/kotlin/com/vrem/wifianalyzer/InstrumentedTestUtils.kt

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,11 @@ internal fun withToolbarTitle(expectedTitle: CharSequence): Matcher<View> {
5252
}
5353
}
5454

55-
internal fun pressBackButton() {
56-
pressBack()
57-
}
55+
internal fun pressBackButton() = pressBack()
5856

59-
internal fun pauseShort() {
60-
pause(SLEEP_1_SECOND)
61-
}
57+
internal fun pauseShort() = pause(SLEEP_1_SECOND)
6258

63-
internal fun pauseLong() {
64-
pause(SLEEP_3_SECONDS)
65-
}
59+
internal fun pauseLong() = pause(SLEEP_3_SECONDS)
6660

67-
private fun pause(sleepTime: Int) {
68-
try {
69-
Thread.sleep(sleepTime.toLong())
70-
} catch (e: InterruptedException) {
71-
e.printStackTrace()
72-
}
73-
}
61+
private fun pause(sleepTime: Int) =
62+
runCatching { Thread.sleep(sleepTime.toLong()) }.getOrElse{ it.printStackTrace() }

app/src/main/kotlin/com/vrem/util/FileUtils.kt

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,24 @@ package com.vrem.util
1919

2020
import android.content.res.Resources
2121
import androidx.annotation.RawRes
22-
import java.io.InputStream
22+
import java.util.zip.ZipInputStream
2323

24-
fun readFile(resources: Resources, @RawRes id: Int): String {
25-
return try {
26-
resources.openRawResource(id).use { read(it) }
27-
} catch (e: Exception) {
28-
String.EMPTY
24+
fun readFile(resources: Resources, @RawRes id: Int): String =
25+
runCatching {
26+
resources.openRawResource(id)
27+
.bufferedReader()
28+
.use { it.readText() }
29+
.replace("\r", String.EMPTY)
2930
}
30-
}
31+
.getOrDefault(String.EMPTY)
3132

32-
private fun read(inputStream: InputStream): String {
33-
val size = inputStream.available()
34-
val bytes = ByteArray(size)
35-
val count = inputStream.read(bytes)
36-
return if (count == size) String(bytes).replace("\r", "") else String.EMPTY
37-
}
33+
fun readZipFile(resources: Resources, @RawRes id: Int): List<String> =
34+
runCatching {
35+
resources.openRawResource(id).use { inputStream ->
36+
ZipInputStream(inputStream).use { zipInputStream ->
37+
zipInputStream.nextEntry
38+
zipInputStream.bufferedReader().readLines()
39+
}
40+
}
41+
}
42+
.getOrDefault(emptyList())

app/src/main/kotlin/com/vrem/wifianalyzer/about/AboutFragment.kt

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ package com.vrem.wifianalyzer.about
1919

2020
import android.app.Activity
2121
import android.app.AlertDialog
22-
import android.content.ActivityNotFoundException
2322
import android.content.Intent
2423
import android.content.pm.PackageInfo
25-
import android.content.pm.PackageManager.NameNotFoundException
2624
import android.os.Build
2725
import android.os.Bundle
2826
import android.view.LayoutInflater
@@ -118,22 +116,19 @@ class AboutFragment : Fragment() {
118116
}
119117

120118
private fun applicationVersion(activity: FragmentActivity): String =
121-
try {
119+
runCatching {
122120
val packageInfo: PackageInfo = activity.packageInfo()
123121
packageInfo.versionName + " - " + PackageInfoCompat.getLongVersionCode(packageInfo)
124-
} catch (e: NameNotFoundException) {
125-
String.EMPTY
126-
}
127-
122+
}.getOrDefault(String.EMPTY)
128123

129124
private class WriteReviewClickListener(private val activity: Activity) : View.OnClickListener {
130125
override fun onClick(view: View) {
131126
val url = "market://details?id=" + activity.applicationContext.packageName
132127
val intent = Intent(Intent.ACTION_VIEW, url.toUri())
133-
try {
128+
runCatching {
134129
activity.startActivity(intent)
135-
} catch (e: ActivityNotFoundException) {
136-
Toast.makeText(view.context, e.localizedMessage, Toast.LENGTH_LONG).show()
130+
}.getOrElse{
131+
Toast.makeText(view.context, it.localizedMessage, Toast.LENGTH_LONG).show()
137132
}
138133
}
139134
}

app/src/main/kotlin/com/vrem/wifianalyzer/navigation/items/ExportItem.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*/
1818
package com.vrem.wifianalyzer.navigation.items
1919

20-
import android.content.ActivityNotFoundException
2120
import android.content.Intent
2221
import android.widget.Toast
2322
import com.vrem.wifianalyzer.MainActivity
@@ -40,11 +39,11 @@ internal class ExportItem(private val export: Export) : NavigationItem {
4039
Toast.makeText(mainActivity, R.string.export_not_available, Toast.LENGTH_LONG).show()
4140
return
4241
}
43-
try {
44-
mainActivity.startActivity(intent)
45-
} catch (e: ActivityNotFoundException) {
46-
Toast.makeText(mainActivity, e.localizedMessage, Toast.LENGTH_LONG).show()
47-
}
42+
runCatching { mainActivity.startActivity(intent) }
43+
.getOrElse {
44+
Toast.makeText(mainActivity, it.localizedMessage, Toast.LENGTH_LONG)
45+
.show()
46+
}
4847
}
4948

5049
private fun exportAvailable(mainActivity: MainActivity, chooser: Intent): Boolean =

app/src/main/kotlin/com/vrem/wifianalyzer/navigation/options/OptionMenu.kt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,7 @@ class OptionMenu {
3838
fun select(item: MenuItem): Unit = OptionAction.findOptionAction(item.itemId).action()
3939

4040
@SuppressLint("RestrictedApi")
41-
private fun iconsVisible(menu: Menu) {
42-
try {
43-
(menu as MenuBuilder).setOptionalIconsVisible(true)
44-
} catch (e: Exception) {
45-
// do nothing
46-
}
47-
}
41+
private fun iconsVisible(menu: Menu): Result<Unit> =
42+
runCatching { (menu as MenuBuilder).setOptionalIconsVisible(true) }
43+
4844
}

app/src/main/kotlin/com/vrem/wifianalyzer/permission/LocationPermission.kt

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,38 +28,26 @@ import com.vrem.util.buildMinVersionP
2828
class LocationPermission(private val activity: Activity) {
2929
fun enabled(): Boolean =
3030
if (buildMinVersionP()) {
31-
try {
31+
runCatching {
3232
val locationManager = activity.getSystemService(LocationManager::class.java)
3333
locationEnabled(locationManager) ||
34-
networkProviderEnabled(locationManager) ||
35-
gpsProviderEnabled(locationManager)
36-
} catch (e: Exception) {
37-
false
38-
}
34+
networkProviderEnabled(locationManager) ||
35+
gpsProviderEnabled(locationManager)
36+
}.getOrDefault(false)
3937
} else {
4038
true
4139
}
4240

4341
private fun gpsProviderEnabled(locationManager: LocationManager): Boolean =
44-
try {
45-
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
46-
} catch (e: Exception) {
47-
false
48-
}
42+
runCatching { locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) }
43+
.getOrDefault(false)
4944

5045
private fun networkProviderEnabled(locationManager: LocationManager): Boolean =
51-
try {
52-
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
53-
} catch (e: Exception) {
54-
false
55-
}
46+
runCatching { locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) }
47+
.getOrDefault(false)
5648

5749
@RequiresApi(Build.VERSION_CODES.P)
5850
private fun locationEnabled(locationManager: LocationManager): Boolean =
59-
try {
60-
locationManager.isLocationEnabled
61-
} catch (e: Exception) {
62-
false
63-
}
51+
runCatching { locationManager.isLocationEnabled }.getOrDefault(false)
6452

6553
}

app/src/main/kotlin/com/vrem/wifianalyzer/settings/Repository.kt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,44 +40,45 @@ class Repository(private val context: Context) {
4040

4141
fun string(key: Int, defaultValue: String): String {
4242
val keyValue: String = context.getString(key)
43-
return try {
43+
return runCatching {
4444
sharedPreferences().getString(keyValue, defaultValue) ?: defaultValue
45-
} catch (e: Exception) {
45+
}.getOrElse {
4646
sharedPreferences().edit { putString(keyValue, defaultValue) }
4747
defaultValue
4848
}
4949
}
5050

5151
fun boolean(key: Int, defaultValue: Boolean): Boolean {
5252
val keyValue: String = context.getString(key)
53-
return try {
53+
return runCatching {
5454
sharedPreferences().getBoolean(keyValue, defaultValue)
55-
} catch (e: Exception) {
55+
}.getOrElse {
5656
sharedPreferences().edit { putBoolean(keyValue, defaultValue) }
57-
defaultValue
57+
return defaultValue
5858
}
5959
}
6060

6161
fun resourceBoolean(key: Int): Boolean = context.resources.getBoolean(key)
6262

6363
fun integer(key: Int, defaultValue: Int): Int {
6464
val keyValue: String = context.getString(key)
65-
return try {
65+
return runCatching {
6666
sharedPreferences().getInt(keyValue, defaultValue)
67-
} catch (e: Exception) {
67+
}.getOrElse {
6868
sharedPreferences().edit { putString(keyValue, defaultValue.toString()) }
69-
defaultValue
69+
return defaultValue
7070
}
7171
}
7272

7373
fun stringSet(key: Int, defaultValues: Set<String>): Set<String> {
7474
val keyValue: String = context.getString(key)
75-
return try {
75+
return runCatching {
7676
sharedPreferences().getStringSet(keyValue, defaultValues)!!
77-
} catch (e: Exception) {
77+
}.getOrElse {
7878
sharedPreferences().edit { putStringSet(keyValue, defaultValues) }
79-
defaultValues
79+
return defaultValues
8080
}
81+
8182
}
8283

8384
fun saveStringSet(key: Int, values: Set<String>): Unit =

app/src/main/kotlin/com/vrem/wifianalyzer/vendor/model/VendorService.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ package com.vrem.wifianalyzer.vendor.model
2020
import android.content.res.Resources
2121
import com.vrem.annotation.OpenClass
2222
import com.vrem.util.EMPTY
23-
import com.vrem.util.readFile
23+
import com.vrem.util.readZipFile
2424
import com.vrem.wifianalyzer.R
2525
import java.util.Locale
2626
import java.util.TreeMap
@@ -51,8 +51,7 @@ class VendorService(private val resources: Resources) {
5151
private fun load(resources: Resources): VendorData {
5252
val macs: MutableMap<String, String> = TreeMap()
5353
val vendors: MutableMap<String, List<String>> = TreeMap()
54-
readFile(resources, R.raw.data)
55-
.split("\n")
54+
readZipFile(resources, R.raw.data)
5655
.map { it.split("|").toTypedArray() }
5756
.filter { it.size == 2 }
5857
.forEach {

0 commit comments

Comments
 (0)