aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/context/App.kt
blob: 7ec1a470a3f3c8af0a995f924889d770fbdf8641 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package org.pacien.tincapp.context

import android.app.Application
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Handler
import android.support.annotation.StringRes
import android.support.v7.app.AlertDialog
import android.view.WindowManager
import org.pacien.tincapp.R
import org.slf4j.LoggerFactory
import java.io.File

/**
 * @author pacien
 */
class App : Application() {
  override fun onCreate() {
    super.onCreate()
    appContext = applicationContext
    handler = Handler()
    AppLogger.configure()
    setupCrashHandler()
  }

  private fun setupCrashHandler() {
    val logger = LoggerFactory.getLogger(this.javaClass)
    val systemCrashHandler = Thread.getDefaultUncaughtExceptionHandler()
    val crashRecorder = CrashRecorder(logger, systemCrashHandler)
    Thread.setDefaultUncaughtExceptionHandler(crashRecorder)
  }

  companion object {
    private var appContext: Context? = null
    private var handler: Handler? = null

    fun getContext() = appContext!!
    fun getResources() = getContext().resources!!

    fun alert(@StringRes title: Int, msg: String, manualLink: String? = null) = handler!!.post {
      AlertDialog.Builder(getContext(), R.style.Theme_AppCompat_Dialog)
        .setTitle(title).setMessage(msg)
        .apply { if (manualLink != null) setNeutralButton(R.string.action_open_manual) { _, _ -> openURL(manualLink) } }
        .setPositiveButton(R.string.action_close, { _, _ -> Unit })
        .create().apply { window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) }.show()
    }

    fun openURL(url: String) {
      val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
      val chooser = Intent.createChooser(intent, getResources().getString(R.string.action_open_web_page))
      appContext?.startActivity(chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
    }

    fun sendMail(recipient: String, subject: String, body: String? = null, attachment: File? = null) {
      val intent = Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"))
        .putExtra(Intent.EXTRA_EMAIL, arrayOf(recipient))
        .putExtra(Intent.EXTRA_SUBJECT, subject)
        .apply { if (body != null) putExtra(Intent.EXTRA_TEXT, body) }
        .apply { if (attachment != null) putExtra(Intent.EXTRA_STREAM, Uri.fromFile(attachment)) }

      val chooser = Intent.createChooser(intent, getResources().getString(R.string.action_send_email))
      appContext?.startActivity(chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
    }
  }
}