mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Minor style changes
This commit is contained in:
@@ -38,52 +38,46 @@ open class AndroidBugReporter @Inject constructor(@AppContext private val contex
|
|||||||
*/
|
*/
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
fun getBugReport(): String {
|
fun getBugReport(): String {
|
||||||
val logcat = logcat
|
|
||||||
val deviceInfo = getDeviceInfo()
|
|
||||||
var log = "---------- BUG REPORT BEGINS ----------\n"
|
var log = "---------- BUG REPORT BEGINS ----------\n"
|
||||||
log += """
|
log += "${getLogcat()}\n"
|
||||||
$deviceInfo
|
log += "${getDeviceInfo()}\n"
|
||||||
$logcat
|
|
||||||
""".trimIndent()
|
|
||||||
log += "---------- BUG REPORT ENDS ------------\n"
|
log += "---------- BUG REPORT ENDS ------------\n"
|
||||||
return log
|
return log
|
||||||
}
|
}
|
||||||
|
|
||||||
@get:Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
val logcat: String
|
fun getLogcat(): String {
|
||||||
get() {
|
val maxLineCount = 250
|
||||||
val maxLineCount = 250
|
val builder = StringBuilder()
|
||||||
val builder = StringBuilder()
|
val process = Runtime.getRuntime().exec(arrayOf("logcat", "-d"))
|
||||||
val command = arrayOf("logcat", "-d")
|
val inputReader = InputStreamReader(process.inputStream)
|
||||||
val process = Runtime.getRuntime().exec(command)
|
val bufferedReader = BufferedReader(inputReader)
|
||||||
val inputReader = InputStreamReader(process.inputStream)
|
val log = LinkedList<String>()
|
||||||
val bufferedReader = BufferedReader(inputReader)
|
var line: String?
|
||||||
val log = LinkedList<String>()
|
while (true) {
|
||||||
var line: String
|
line = bufferedReader.readLine()
|
||||||
while (bufferedReader.readLine().also { line = it } != null) {
|
if (line == null) break;
|
||||||
log.addLast(line)
|
log.addLast(line)
|
||||||
if (log.size > maxLineCount) log.removeFirst()
|
if (log.size > maxLineCount) log.removeFirst()
|
||||||
}
|
|
||||||
for (l in log) {
|
|
||||||
builder.appendln(l)
|
|
||||||
}
|
|
||||||
return builder.toString()
|
|
||||||
}
|
}
|
||||||
|
for (l in log) {
|
||||||
|
builder.appendln(l)
|
||||||
|
}
|
||||||
|
return builder.toString()
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Captures a bug report and saves it to a file in the SD card.
|
* Captures a bug report and saves it to a file in the SD card.
|
||||||
*
|
*
|
||||||
*
|
* The contents of the file are generated by the method [ ][.getBugReport]. The file is saved
|
||||||
* The contents of the file are generated by the method [ ][.getBugReport]. The file is saved in the apps's external private
|
* in the apps's external private storage.
|
||||||
* storage.
|
|
||||||
*
|
*
|
||||||
* @return the generated file.
|
* @return the generated file.
|
||||||
* @throws IOException when I/O errors occur.
|
* @throws IOException when I/O errors occur.
|
||||||
*/
|
*/
|
||||||
fun dumpBugReportToFile() {
|
fun dumpBugReportToFile() {
|
||||||
try {
|
try {
|
||||||
val date = SimpleDateFormat("yyyy-MM-dd HHmmss", Locale.US).format(
|
val date = SimpleDateFormat("yyyy-MM-dd HHmmss", Locale.US).format(Date())
|
||||||
Date())
|
|
||||||
val dir = AndroidDirFinder(context).getFilesDir("Logs")
|
val dir = AndroidDirFinder(context).getFilesDir("Logs")
|
||||||
?: throw IOException("log dir should not be null")
|
?: throw IOException("log dir should not be null")
|
||||||
val logFile = File(String.format("%s/Log %s.txt", dir.path, date))
|
val logFile = File(String.format("%s/Log %s.txt", dir.path, date))
|
||||||
|
|||||||
@@ -25,9 +25,10 @@ import java.io.File
|
|||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class AndroidDirFinder @Inject constructor(@param:AppContext private val context: Context) {
|
class AndroidDirFinder @Inject constructor(@param:AppContext private val context: Context) {
|
||||||
fun getFilesDir(relativePath: String?): File? =
|
fun getFilesDir(relativePath: String?): File? {
|
||||||
FileUtils.getDir(
|
return FileUtils.getDir(
|
||||||
ContextCompat.getExternalFilesDirs(context, null),
|
ContextCompat.getExternalFilesDirs(context, null),
|
||||||
relativePath
|
relativePath
|
||||||
)
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -22,25 +22,18 @@ import org.isoron.androidbase.activities.BaseActivity
|
|||||||
|
|
||||||
class BaseExceptionHandler(private val activity: BaseActivity) : Thread.UncaughtExceptionHandler {
|
class BaseExceptionHandler(private val activity: BaseActivity) : Thread.UncaughtExceptionHandler {
|
||||||
|
|
||||||
private val originalHandler: Thread.UncaughtExceptionHandler? = Thread.getDefaultUncaughtExceptionHandler()
|
private val originalHandler: Thread.UncaughtExceptionHandler? =
|
||||||
|
Thread.getDefaultUncaughtExceptionHandler()
|
||||||
|
|
||||||
override fun uncaughtException(thread: Thread?, ex: Throwable?) {
|
override fun uncaughtException(thread: Thread?, ex: Throwable?) {
|
||||||
ex?.let { ex ->
|
if (ex == null) return
|
||||||
try {
|
if (thread == null) return
|
||||||
ex.printStackTrace()
|
try {
|
||||||
AndroidBugReporter(activity).dumpBugReportToFile()
|
ex.printStackTrace()
|
||||||
} catch (e: Exception) {
|
AndroidBugReporter(activity).dumpBugReportToFile()
|
||||||
e.printStackTrace()
|
} catch (e: Exception) {
|
||||||
}
|
e.printStackTrace()
|
||||||
|
|
||||||
// if (ex.cause is InconsistentDatabaseException) {
|
|
||||||
// val app = activity.application as HabitsApplication
|
|
||||||
// val habits = app.getComponent().getHabitList()
|
|
||||||
// habits.repair()
|
|
||||||
// System.exit(0)
|
|
||||||
// }
|
|
||||||
|
|
||||||
originalHandler?.uncaughtException(thread, ex)
|
|
||||||
}
|
}
|
||||||
|
originalHandler?.uncaughtException(thread, ex)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -26,26 +26,23 @@ import javax.net.ssl.SSLContext
|
|||||||
import javax.net.ssl.TrustManagerFactory
|
import javax.net.ssl.TrustManagerFactory
|
||||||
|
|
||||||
class SSLContextProvider @Inject constructor(@param:AppContext private val context: Context) {
|
class SSLContextProvider @Inject constructor(@param:AppContext private val context: Context) {
|
||||||
fun getCACertSSLContext(): SSLContext =
|
fun getCACertSSLContext(): SSLContext {
|
||||||
try {
|
try {
|
||||||
val ca = CertificateFactory.getInstance("X.509")
|
val cf = CertificateFactory.getInstance("X.509")
|
||||||
.let { cf ->
|
val ca = cf.generateCertificate(context.assets.open("cacert.pem"))
|
||||||
context.assets.open("cacert.pem")
|
val ks = KeyStore.getInstance(KeyStore.getDefaultType()).apply {
|
||||||
.use { caInput ->
|
load(null, null)
|
||||||
cf.generateCertificate(caInput)
|
setCertificateEntry("ca", ca)
|
||||||
}
|
|
||||||
}
|
|
||||||
val ks = KeyStore.getInstance(KeyStore.getDefaultType())
|
|
||||||
.apply {
|
|
||||||
load(null, null)
|
|
||||||
setCertificateEntry("ca", ca)
|
|
||||||
}
|
|
||||||
val tmf =
|
|
||||||
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())
|
|
||||||
.apply { init(ks) }
|
|
||||||
SSLContext.getInstance("TLS")
|
|
||||||
.apply { init(null, tmf.trustManagers, null) }
|
|
||||||
} catch (e: Exception) {
|
|
||||||
throw RuntimeException(e)
|
|
||||||
}
|
}
|
||||||
|
val alg = TrustManagerFactory.getDefaultAlgorithm()
|
||||||
|
val tmf = TrustManagerFactory.getInstance(alg).apply {
|
||||||
|
init(ks)
|
||||||
|
}
|
||||||
|
return SSLContext.getInstance("TLS").apply {
|
||||||
|
init(null, tmf.trustManagers, null)
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
throw RuntimeException(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user