Migrate to ktx API

This commit is contained in:
Mygod
2020-04-20 03:29:05 +08:00
parent cf2beb8aac
commit 3260783eec
6 changed files with 26 additions and 20 deletions

View File

@@ -84,7 +84,7 @@ dependencies {
implementation 'com.github.topjohnwu.libsu:core:2.5.1' implementation 'com.github.topjohnwu.libsu:core:2.5.1'
implementation 'com.google.android.gms:play-services-oss-licenses:17.0.0' implementation 'com.google.android.gms:play-services-oss-licenses:17.0.0'
implementation 'com.google.android.material:material:1.1.0' implementation 'com.google.android.material:material:1.1.0'
implementation 'com.google.firebase:firebase-analytics:17.3.0' implementation 'com.google.firebase:firebase-analytics-ktx:17.3.0'
implementation 'com.google.firebase:firebase-crashlytics:17.0.0-beta04' implementation 'com.google.firebase:firebase-crashlytics:17.0.0-beta04'
implementation 'com.google.zxing:core:3.4.0' implementation 'com.google.zxing:core:3.4.0'
implementation 'com.jakewharton.timber:timber:4.7.1' implementation 'com.jakewharton.timber:timber:4.7.1'

View File

@@ -8,7 +8,6 @@ import android.content.res.Configuration
import android.net.ConnectivityManager import android.net.ConnectivityManager
import android.net.wifi.WifiManager import android.net.wifi.WifiManager
import android.os.Build import android.os.Build
import android.os.Bundle
import android.util.Log import android.util.Log
import androidx.annotation.Size import androidx.annotation.Size
import androidx.browser.customtabs.CustomTabColorSchemeParams import androidx.browser.customtabs.CustomTabColorSchemeParams
@@ -23,9 +22,11 @@ import be.mygod.vpnhotspot.net.DhcpWorkaround
import be.mygod.vpnhotspot.room.AppDatabase import be.mygod.vpnhotspot.room.AppDatabase
import be.mygod.vpnhotspot.util.DeviceStorageApp import be.mygod.vpnhotspot.util.DeviceStorageApp
import be.mygod.vpnhotspot.util.RootSession import be.mygod.vpnhotspot.util.RootSession
import com.google.firebase.FirebaseApp import com.google.firebase.analytics.ktx.ParametersBuilder
import com.google.firebase.analytics.FirebaseAnalytics import com.google.firebase.analytics.ktx.analytics
import com.google.firebase.crashlytics.FirebaseCrashlytics import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.google.firebase.ktx.Firebase
import com.google.firebase.ktx.initialize
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import timber.log.Timber import timber.log.Timber
@@ -46,15 +47,15 @@ class App : Application() {
deviceStorage.moveSharedPreferencesFrom(this, PreferenceManager(this).sharedPreferencesName) deviceStorage.moveSharedPreferencesFrom(this, PreferenceManager(this).sharedPreferencesName)
deviceStorage.moveDatabaseFrom(this, AppDatabase.DB_NAME) deviceStorage.moveDatabaseFrom(this, AppDatabase.DB_NAME)
} else deviceStorage = this } else deviceStorage = this
FirebaseApp.initializeApp(deviceStorage) Firebase.initialize(deviceStorage)
Timber.plant(object : Timber.DebugTree() { Timber.plant(object : Timber.DebugTree() {
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) { override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
if (t == null) { if (t == null) {
if (priority != Log.DEBUG || BuildConfig.DEBUG) Log.println(priority, tag, message) if (priority != Log.DEBUG || BuildConfig.DEBUG) Log.println(priority, tag, message)
crashlytics.log("${"XXVDIWEF".getOrElse(priority) { 'X' }}/$tag: $message") FirebaseCrashlytics.getInstance().log("${"XXVDIWEF".getOrElse(priority) { 'X' }}/$tag: $message")
} else { } else {
if (priority >= Log.WARN || priority == Log.DEBUG) Log.println(priority, tag, message) if (priority >= Log.WARN || priority == Log.DEBUG) Log.println(priority, tag, message)
if (priority >= Log.INFO) crashlytics.recordException(t) if (priority >= Log.INFO) FirebaseCrashlytics.getInstance().recordException(t)
} }
} }
}) })
@@ -88,14 +89,14 @@ class App : Application() {
* This method is used to log "expected" and well-handled errors, i.e. we care less about logs, etc. * This method is used to log "expected" and well-handled errors, i.e. we care less about logs, etc.
* logException is inappropriate sometimes because it flushes all logs that could be used to investigate other bugs. * logException is inappropriate sometimes because it flushes all logs that could be used to investigate other bugs.
*/ */
fun logEvent(@Size(min = 1L, max = 40L) event: String, extras: Bundle? = null) { fun logEvent(@Size(min = 1L, max = 40L) event: String, block: ParametersBuilder.() -> Unit = { }) {
Timber.i(if (extras == null) event else "$event, extras: $extras") val builder = ParametersBuilder()
analytics.logEvent(event, extras) builder.block()
Timber.i(if (builder.bundle.isEmpty) event else "$event, extras: ${builder.bundle}")
Firebase.analytics.logEvent(event, builder.bundle)
} }
lateinit var deviceStorage: Application lateinit var deviceStorage: Application
private val analytics by lazy { FirebaseAnalytics.getInstance(app.deviceStorage) }
val crashlytics by lazy { FirebaseCrashlytics.getInstance() }
val english by lazy { val english by lazy {
createConfigurationContext(Configuration(resources.configuration).apply { createConfigurationContext(Configuration(resources.configuration).apply {
setLocale(Locale.ENGLISH) setLocale(Locale.ENGLISH)

View File

@@ -14,7 +14,6 @@ import android.provider.Settings
import android.view.View import android.view.View
import android.widget.Toast import android.widget.Toast
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.LocalOnlyHotspotService import be.mygod.vpnhotspot.LocalOnlyHotspotService
@@ -61,7 +60,7 @@ class LocalOnlyHotspotManager(private val parent: TetheringFragment) : Manager()
context.startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)) context.startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))
Toast.makeText(context, R.string.tethering_temp_hotspot_location, Toast.LENGTH_LONG).show() Toast.makeText(context, R.string.tethering_temp_hotspot_location, Toast.LENGTH_LONG).show()
} catch (e: ActivityNotFoundException) { } catch (e: ActivityNotFoundException) {
app.logEvent("location_settings", bundleOf("message" to e.message)) app.logEvent("location_settings") { param("message", e.message.toString()) }
SmartSnackbar.make(R.string.tethering_temp_hotspot_location).show() SmartSnackbar.make(R.string.tethering_temp_hotspot_location).show()
} }
return return

View File

@@ -37,9 +37,12 @@ object ManageBar : Manager() {
} catch (e1: RuntimeException) { } catch (e1: RuntimeException) {
try { try {
context.startActivity(Intent().setClassName(SETTINGS_PACKAGE, SETTINGS_2)) context.startActivity(Intent().setClassName(SETTINGS_PACKAGE, SETTINGS_2))
app.logEvent(TAG, bundleOf(SETTINGS_1 to e1.message)) app.logEvent(TAG) { param(SETTINGS_1, e1.message.toString()) }
} catch (e2: RuntimeException) { } catch (e2: RuntimeException) {
app.logEvent(TAG, bundleOf(SETTINGS_1 to e1.message, SETTINGS_2 to e2.message)) app.logEvent(TAG) {
param(SETTINGS_1, e1.message.toString())
param(SETTINGS_2, e2.message.toString())
}
} }
} }
} }

View File

@@ -48,7 +48,7 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(),
"package:${mainActivity.packageName}".toUri())) "package:${mainActivity.packageName}".toUri()))
return return
} catch (e: RuntimeException) { } catch (e: RuntimeException) {
app.logEvent("manage_write_settings", bundleOf("message" to e.message)) app.logEvent("manage_write_settings") { param("message", e.message.toString()) }
} }
val started = manager.isStarted val started = manager.isStarted
try { try {

View File

@@ -5,6 +5,7 @@ import android.os.Build
import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.RepeaterService import be.mygod.vpnhotspot.RepeaterService
import be.mygod.vpnhotspot.util.RootSession import be.mygod.vpnhotspot.util.RootSession
import com.google.firebase.crashlytics.FirebaseCrashlytics
import java.io.File import java.io.File
/** /**
@@ -108,9 +109,11 @@ class P2pSupplicantConfiguration(private val group: WifiP2pGroup, ownerAddress:
} }
Triple(result, target!!, shell.err.isNotEmpty()) Triple(result, target!!, shell.err.isNotEmpty())
} catch (e: RuntimeException) { } catch (e: RuntimeException) {
app.crashlytics.setCustomKey(TAG, parser.lines.joinToString("\n")) FirebaseCrashlytics.getInstance().apply {
app.crashlytics.setCustomKey("$TAG.ownerAddress", ownerAddress.toString()) setCustomKey(TAG, parser.lines.joinToString("\n"))
app.crashlytics.setCustomKey("$TAG.p2pGroup", group.toString()) setCustomKey("$TAG.ownerAddress", ownerAddress.toString())
setCustomKey("$TAG.p2pGroup", group.toString())
}
throw e throw e
} }
} }