aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.kt
blob: 196ccd3bf75205ed73e30945f6534723e1ab4f5f (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon
 * Copyright (C) 2017-2018 Pacien TRAN-GIRARD
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package org.pacien.tincapp.activities

import android.content.Intent
import android.os.Bundle
import android.support.annotation.LayoutRes
import android.support.annotation.StringRes
import android.support.design.widget.Snackbar
import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatActivity
import android.view.*
import kotlinx.android.synthetic.main.base_activity.*
import org.pacien.tincapp.R
import org.pacien.tincapp.context.App
import org.pacien.tincapp.context.AppInfo

/**
 * @author pacien
 */
abstract class BaseActivity : AppCompatActivity() {
  private val rootView by lazy { base_activity_frame!! }
  private var active = false

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    super.setContentView(R.layout.base_activity)
  }

  override fun onCreateOptionsMenu(m: Menu): Boolean {
    menuInflater.inflate(R.menu.menu_base, m)
    return true
  }

  override fun onStart() {
    super.onStart()
    active = true
  }

  override fun onResume() {
    super.onResume()
    active = true
  }

  override fun onPause() {
    active = false
    super.onPause()
  }

  override fun onStop() {
    active = false
    super.onStop()
  }

  override fun setContentView(layoutResID: Int) {
    layoutInflater.inflate(layoutResID, rootView)
  }

  override fun getSupportActionBar() = super.getSupportActionBar()!!

  fun startActivityChooser(target: Intent, title: String) {
    val intentChooser = Intent.createChooser(target, title)
    startActivity(intentChooser)
  }

  @Suppress("UNUSED_PARAMETER")
  fun aboutDialog(m: MenuItem) {
    AlertDialog.Builder(this)
      .setTitle(resources.getString(R.string.app_name))
      .setMessage(resources.getString(R.string.about_app_short_desc) + "\n\n" +
        resources.getString(R.string.about_app_copyright) + " " +
        resources.getString(R.string.about_app_license) + "\n\n" +
        AppInfo.all())
      .setNeutralButton(R.string.about_app_open_project_website) { _, _ -> App.openURL(resources.getString(R.string.about_app_website_url)) }
      .setPositiveButton(R.string.generic_action_close) { _, _ -> Unit }
      .show()
  }

  fun runOnUiThread(action: () -> Unit) {
    if (active) super.runOnUiThread(action)
  }

  fun inflate(@LayoutRes layout: Int) = layoutInflater.inflate(layout, rootView, false)!!
  fun inflate(inflateFunc: (LayoutInflater, ViewGroup?, Boolean) -> View) = inflateFunc(layoutInflater, rootView, false)

  fun notify(@StringRes msg: Int) = Snackbar.make(base_activity_frame, msg, Snackbar.LENGTH_LONG).show()
  fun notify(msg: String) = Snackbar.make(base_activity_frame, msg, Snackbar.LENGTH_LONG).show()

  fun showErrorDialog(msg: String): AlertDialog = AlertDialog.Builder(this)
    .setTitle(R.string.generic_title_error).setMessage(msg)
    .setPositiveButton(R.string.generic_action_close) { _, _ -> Unit }.show()
}