MainActivity.kt 4.52 KB
Newer Older
Andreas Schildbach's avatar
Andreas Schildbach committed
1
2
3
/*
 * Copyright by the original author or authors.
 *
4
5
6
7
8
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
Andreas Schildbach's avatar
Andreas Schildbach committed
10
 *
11
12
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
Andreas Schildbach's avatar
Andreas Schildbach committed
13
 *
14
15
16
17
18
19
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
Andreas Schildbach's avatar
Andreas Schildbach committed
21
22
 */

23
package de.ccc.events.badge.card10
24

25
import android.Manifest
26
import android.app.Activity
27
import android.bluetooth.BluetoothAdapter
28
import android.content.Intent
29
import android.content.pm.PackageManager
30
import android.os.Bundle
31
32
33
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
34
import androidx.work.ExistingPeriodicWorkPolicy
35
36
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
37
import de.ccc.events.badge.card10.background.UpdateClockJob
38
import de.ccc.events.badge.card10.installer.InstallerFragment
39
import de.ccc.events.badge.card10.main.MainFragment
40
import java.util.concurrent.TimeUnit
41

42
43
44
private const val REQUEST_CODE_ENABLE_BLUETOOTH = 1
private const val REQUEST_CODE_PERMISSIONS = 2

45
46
private const val WORK_NAME = "update_clock"

47
48
class MainActivity : AppCompatActivity() {

49
50
    val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()

51
52
53
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
54
55
56
57
58
59
60
61
62
        maybePermissionGranted()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == REQUEST_CODE_ENABLE_BLUETOOTH) {
            if (resultCode == Activity.RESULT_OK)
                maybePermissionGranted()
            else
                finish()
63
        } else {
64
            super.onActivityResult(requestCode, resultCode, data)
65
66
67
68
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
        if (requestCode == REQUEST_CODE_PERMISSIONS) {
            if (!grantResults.isEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                maybePermissionGranted()
            else
                finish()
        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        }
    }

    fun maybePermissionGranted() {
        if (!bluetoothAdapter.isEnabled) {
            startActivityForResult(Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), REQUEST_CODE_ENABLE_BLUETOOTH)
        } else if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_COARSE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
                REQUEST_CODE_PERMISSIONS
            )
92
93
94
95
96
97
        } else {
            permissionGranted()
        }
    }

    fun permissionGranted() {
98
99
100
        val fragment = when {
            intent.action == "application/x.card10.app" -> InstallerFragment()
            else -> MainFragment()
101
        }
102

103
104
105
106
        supportFragmentManager.beginTransaction()
            .replace(R.id.fragment_container, fragment)
            .commit()

Anon's avatar
Anon committed
107
108
109
110
111
112
113
114
115
116
117
//        val workManager = WorkManager.getInstance(this)
//        if (!bluetoothAdapter.bondedDevices.isEmpty()
//        ) {
//            val updateClockRequest =
//                PeriodicWorkRequestBuilder<UpdateClockJob>(UPDATE_CLOCK_FREQUENCY_MINS.toLong(), TimeUnit.MINUTES)
//                    .build()
//            workManager
//                .enqueueUniquePeriodicWork(WORK_NAME, ExistingPeriodicWorkPolicy.REPLACE, updateClockRequest)
//        } else {
//            workManager.cancelUniqueWork(WORK_NAME)
//        }
118
119
    }
}