MainActivity.kt 6.21 KB
Newer Older
Nihar Thakkar's avatar
Nihar Thakkar committed
1
2
package io.eelo.appinstaller

3
import android.annotation.SuppressLint
4
import android.content.pm.PackageManager
Nihar Thakkar's avatar
Nihar Thakkar committed
5
import android.os.Bundle
6
7
8
import android.support.design.internal.BottomNavigationItemView
import android.support.design.internal.BottomNavigationMenuView
import android.support.design.widget.BottomNavigationView
9
import android.support.design.widget.Snackbar
10
import android.support.v4.app.Fragment
11
import android.support.v7.app.AppCompatActivity
12
import android.view.MenuItem
Nihar Thakkar's avatar
Nihar Thakkar committed
13
import androidx.work.*
14
15
import io.eelo.appinstaller.applicationmanager.ApplicationManager
import io.eelo.appinstaller.applicationmanager.ApplicationManagerServiceConnection
16
import io.eelo.appinstaller.applicationmanager.ApplicationManagerServiceConnectionCallback
17
18
19
20
21
import io.eelo.appinstaller.categories.CategoriesFragment
import io.eelo.appinstaller.home.HomeFragment
import io.eelo.appinstaller.search.SearchFragment
import io.eelo.appinstaller.settings.SettingsFragment
import io.eelo.appinstaller.updates.UpdatesFragment
Nihar Thakkar's avatar
Nihar Thakkar committed
22
import io.eelo.appinstaller.updates.model.UpdatesWorker
23
import io.eelo.appinstaller.utils.Constants
24
import io.eelo.appinstaller.utils.Constants.CURRENTLY_SELECTED_FRAGMENT_KEY
25
import kotlinx.android.synthetic.main.activity_main.*
Nihar Thakkar's avatar
Nihar Thakkar committed
26
import java.util.concurrent.TimeUnit
Nihar Thakkar's avatar
Nihar Thakkar committed
27

28
29
class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemSelectedListener,
        ApplicationManagerServiceConnectionCallback {
30
    private var currentFragmentId = 0
31
    private val homeFragment = HomeFragment()
32
    private val searchFragment = SearchFragment()
33
    private val updatesFragment = UpdatesFragment()
34
35
    private val applicationManagerServiceConnection =
            ApplicationManagerServiceConnection(this)
Nihar Thakkar's avatar
Nihar Thakkar committed
36
37
38
39

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
40

41
        bottom_navigation_view.setOnNavigationItemSelectedListener(this)
42
        disableShiftingOfNabBarItems()
43

Nihar Thakkar's avatar
Nihar Thakkar committed
44
45
        initialiseUpdatesWorker()

46
47
48
49
50
51
52
        // Show the home fragment by default
        currentFragmentId = if (savedInstanceState != null &&
                savedInstanceState.containsKey(CURRENTLY_SELECTED_FRAGMENT_KEY)) {
            savedInstanceState.getInt(CURRENTLY_SELECTED_FRAGMENT_KEY)
        } else {
            R.id.menu_home
        }
53

54
55
        applicationManagerServiceConnection.bindService(this)
    }
56

Nihar Thakkar's avatar
Nihar Thakkar committed
57
58
59
60
61
62
63
64
65
66
67
68
69
    private fun initialiseUpdatesWorker() {
        val constraints = Constraints.Builder().apply {
            setRequiresBatteryNotLow(true)
            setRequiredNetworkType(NetworkType.CONNECTED)
        }.build()
        val updatesCheckBuilder = PeriodicWorkRequest
                .Builder(UpdatesWorker::class.java, 15, TimeUnit.MINUTES).apply {
                    setConstraints(constraints)
                }
        WorkManager.getInstance().enqueueUniquePeriodicWork(Constants.UPDATES_WORK_NAME,
                ExistingPeriodicWorkPolicy.KEEP, updatesCheckBuilder.build())
    }

70
71
72
    override fun onServiceBind(applicationManager: ApplicationManager) {
        initialiseFragments(applicationManager)
        selectFragment(currentFragmentId)
Nihar Thakkar's avatar
Nihar Thakkar committed
73
    }
74

75
76
77
78
    private fun initialiseFragments(applicationManager: ApplicationManager) {
        homeFragment.initialise(applicationManager)
        searchFragment.initialise(applicationManager)
        updatesFragment.initialise(applicationManager)
79
80
    }

81
    override fun onNavigationItemSelected(item: MenuItem): Boolean {
82
83
84
85
86
87
88
89
90
91
        if (selectFragment(item.itemId)) {
            currentFragmentId = item.itemId
            return true
        }
        return false
    }

    private fun selectFragment(fragmentId: Int): Boolean {
        when (fragmentId) {
            R.id.menu_home -> {
92
93
94
                showFragment(homeFragment)
                return true
            }
95
            R.id.menu_categories -> {
Nihar Thakkar's avatar
Nihar Thakkar committed
96
                showFragment(CategoriesFragment())
97
98
                return true
            }
99
            R.id.menu_search -> {
100
101
102
                showFragment(searchFragment)
                return true
            }
Nihar Thakkar's avatar
Nihar Thakkar committed
103
            R.id.menu_updates -> {
104
105
                showFragment(updatesFragment)
                return true
Nihar Thakkar's avatar
Nihar Thakkar committed
106
            }
107
            R.id.menu_settings -> {
Nihar Thakkar's avatar
Nihar Thakkar committed
108
                showFragment(SettingsFragment())
109
110
111
112
113
114
115
116
117
118
119
120
121
                return true
            }
        }
        return false
    }

    private fun showFragment(fragment: Fragment) {
        supportFragmentManager
                .beginTransaction()
                .replace(R.id.frame_layout, fragment)
                .commit()
    }

122
    @SuppressLint("RestrictedApi")
123
124
    private fun disableShiftingOfNabBarItems() {
        val menuView = bottom_navigation_view.getChildAt(0) as BottomNavigationMenuView
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
        try {
            val mShiftingMode = menuView.javaClass.getDeclaredField("mShiftingMode")
            mShiftingMode.isAccessible = true
            mShiftingMode.setBoolean(menuView, false)
            mShiftingMode.isAccessible = false
        } catch (e: NoSuchFieldException) {
            e.printStackTrace()
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        }

        for (i in 0 until menuView.childCount) {
            val itemView = menuView.getChildAt(i) as BottomNavigationItemView
            itemView.setShiftingMode(false)
            itemView.setChecked(itemView.itemData.isChecked)
        }
    }
142

143
144
145
146
147
148
149
150
151
    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>,
                                            grantResults: IntArray) {
        if (requestCode == Constants.STORAGE_PERMISSION_REQUEST_CODE &&
                grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_DENIED) {
            Snackbar.make(container, R.string.error_storage_permission_denied,
                    Snackbar.LENGTH_LONG).show()
        }
    }

152
153
154
155
156
    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        outState?.putInt(CURRENTLY_SELECTED_FRAGMENT_KEY, currentFragmentId)
    }

157
158
    override fun onDestroy() {
        super.onDestroy()
159
160
161
        homeFragment.decrementApplicationUses()
        searchFragment.decrementApplicationUses()
        updatesFragment.decrementApplicationUses()
162
        applicationManagerServiceConnection.unbindService(this)
163
    }
Nihar Thakkar's avatar
Nihar Thakkar committed
164
}