Bimba.git

ref: 85f4ee806ced6e075630cfb0cb52cead4c4a0f25

app/src/main/java/ml/adamsprogs/bimba/activities/NoDbActivity.kt


 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
package ml.adamsprogs.bimba.activities

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.content.IntentFilter
import android.widget.TextView
import ml.adamsprogs.bimba.*


class NoDbActivity : AppCompatActivity(), NetworkStateReceiver.OnConnectivityChangeListener, MessageReceiver.OnTimetableDownloadListener {
    val networkStateReceiver = NetworkStateReceiver()
    val timetableDownloadReceiver = MessageReceiver()
    var serviceRunning = false
    var askedForNetwork = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_nodb)
        var filter: IntentFilter = IntentFilter(TimetableDownloader.ACTION_DOWNLOADED)
        filter.addCategory(Intent.CATEGORY_DEFAULT)
        registerReceiver(timetableDownloadReceiver, filter)
        timetableDownloadReceiver.addOnTimetableDownloadListener(this)

        if (!NetworkStateReceiver.isNetworkAvailable(this)) {
            askedForNetwork = true
            (findViewById(R.id.noDbCaption) as TextView).text = getString(R.string.no_db_connect)
            filter = IntentFilter("android.net.conn.CONNECTIVITY_CHANGE")
            registerReceiver(networkStateReceiver, filter)
            networkStateReceiver.addOnConnectivityChangeListener(this)
        } else
            downloadTimetable()
    }

    override fun onResume() {
        super.onResume()
        var filter: IntentFilter = IntentFilter(TimetableDownloader.ACTION_DOWNLOADED)
        filter.addCategory(Intent.CATEGORY_DEFAULT)
        registerReceiver(timetableDownloadReceiver, filter)
        if (!NetworkStateReceiver.isNetworkAvailable(this)) {
            askedForNetwork = true
            (findViewById(R.id.noDbCaption) as TextView).text = getString(R.string.no_db_connect)
            filter = IntentFilter("android.net.conn.CONNECTIVITY_CHANGE")
            registerReceiver(networkStateReceiver, filter)
            networkStateReceiver.addOnConnectivityChangeListener(this)
        } else if (!serviceRunning)
            downloadTimetable()
    }

    fun downloadTimetable() {
        (findViewById(R.id.noDbCaption) as TextView).text = getString(R.string.no_db_downloading)
        serviceRunning = true
        intent = Intent(this, TimetableDownloader::class.java)
        intent.putExtra(TimetableDownloader.EXTRA_FORCE, true)
        startService(intent)
    }

    override fun onConnectivityChange(connected: Boolean) {
        if (connected && !serviceRunning)
            downloadTimetable()
        /*if (!connected)
            serviceRunning = false*/
    }

    override fun onTimetableDownload(result: String?) {
        when (result) {
            TimetableDownloader.RESULT_DOWNLOADED -> {
                timetableDownloadReceiver.removeOnTimetableDownloadListener(this)
                networkStateReceiver.removeOnConnectivityChangeListener(this)
                startActivity(Intent(this, DashActivity::class.java))
                finish()
            }
            else -> (findViewById(R.id.noDbCaption) as TextView).text = getString(R.string.error_try_later)
        }
    }

    override fun onPause() {
        super.onPause()
        unregisterReceiver(timetableDownloadReceiver)
        if (askedForNetwork)
            unregisterReceiver(networkStateReceiver)
    }
}