MainActivity.kt 6.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
    Copyright (C) 2019  e Foundation

    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/>.
 */

18
package foundation.e.apps
Nihar Thakkar's avatar
Nihar Thakkar committed
19

20
import android.annotation.SuppressLint
21
import android.content.pm.PackageManager
Nihar Thakkar's avatar
Nihar Thakkar committed
22
import android.os.Bundle
23
24
25
import android.support.design.internal.BottomNavigationItemView
import android.support.design.internal.BottomNavigationMenuView
import android.support.design.widget.BottomNavigationView
26
import android.support.design.widget.Snackbar
27
import android.support.v4.app.Fragment
28
import android.support.v7.app.AppCompatActivity
29
import android.view.MenuItem
30
31
32
33
34
35
36
37
import foundation.e.apps.applicationmanager.ApplicationManager
import foundation.e.apps.applicationmanager.ApplicationManagerServiceConnection
import foundation.e.apps.applicationmanager.ApplicationManagerServiceConnectionCallback
import foundation.e.apps.categories.CategoriesFragment
import foundation.e.apps.home.HomeFragment
import foundation.e.apps.search.SearchFragment
import foundation.e.apps.settings.SettingsFragment
import foundation.e.apps.updates.UpdatesFragment
38
import foundation.e.apps.updates.UpdatesManager
39
40
import foundation.e.apps.utils.Constants
import foundation.e.apps.utils.Constants.CURRENTLY_SELECTED_FRAGMENT_KEY
41
import kotlinx.android.synthetic.main.activity_main.*
Nihar Thakkar's avatar
Nihar Thakkar committed
42

43
44
class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemSelectedListener,
        ApplicationManagerServiceConnectionCallback {
45
    private var currentFragmentId = 0
46
    private val homeFragment = HomeFragment()
47
    private val searchFragment = SearchFragment()
48
    private val updatesFragment = UpdatesFragment()
49
50
    private val applicationManagerServiceConnection =
            ApplicationManagerServiceConnection(this)
Nihar Thakkar's avatar
Nihar Thakkar committed
51
52
53
54

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

56
        bottom_navigation_view.setOnNavigationItemSelectedListener(this)
57
        disableShiftingOfNabBarItems()
58

Nihar Thakkar's avatar
Nihar Thakkar committed
59
60
        initialiseUpdatesWorker()

61
62
63
64
        // Show the home fragment by default
        currentFragmentId = if (savedInstanceState != null &&
                savedInstanceState.containsKey(CURRENTLY_SELECTED_FRAGMENT_KEY)) {
            savedInstanceState.getInt(CURRENTLY_SELECTED_FRAGMENT_KEY)
65
66
        } else if (intent.hasExtra(Constants.UPDATES_NOTIFICATION_CLICK_EXTRA)) {
            R.id.menu_updates
67
68
69
        } else {
            R.id.menu_home
        }
70

71
72
        applicationManagerServiceConnection.bindService(this)
    }
73

Nihar Thakkar's avatar
Nihar Thakkar committed
74
    private fun initialiseUpdatesWorker() {
75
        UpdatesManager(applicationContext).startWorker()
Nihar Thakkar's avatar
Nihar Thakkar committed
76
77
    }

78
79
80
    override fun onServiceBind(applicationManager: ApplicationManager) {
        initialiseFragments(applicationManager)
        selectFragment(currentFragmentId)
Nihar Thakkar's avatar
Nihar Thakkar committed
81
    }
82

83
84
85
86
    private fun initialiseFragments(applicationManager: ApplicationManager) {
        homeFragment.initialise(applicationManager)
        searchFragment.initialise(applicationManager)
        updatesFragment.initialise(applicationManager)
87
88
    }

89
    override fun onNavigationItemSelected(item: MenuItem): Boolean {
90
91
92
93
94
95
96
97
98
99
        if (selectFragment(item.itemId)) {
            currentFragmentId = item.itemId
            return true
        }
        return false
    }

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

    private fun showFragment(fragment: Fragment) {
124
        bottom_navigation_view.menu.findItem(currentFragmentId).isChecked = true
125
126
127
128
129
130
        supportFragmentManager
                .beginTransaction()
                .replace(R.id.frame_layout, fragment)
                .commit()
    }

131
    @SuppressLint("RestrictedApi")
132
133
    private fun disableShiftingOfNabBarItems() {
        val menuView = bottom_navigation_view.getChildAt(0) as BottomNavigationMenuView
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
        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)
        }
    }
151

152
153
154
155
156
157
158
159
160
    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()
        }
    }

161
162
163
164
165
    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        outState?.putInt(CURRENTLY_SELECTED_FRAGMENT_KEY, currentFragmentId)
    }

166
167
    override fun onDestroy() {
        super.onDestroy()
168
169
170
        homeFragment.decrementApplicationUses()
        searchFragment.decrementApplicationUses()
        updatesFragment.decrementApplicationUses()
171
        applicationManagerServiceConnection.unbindService(this)
172
    }
Nihar Thakkar's avatar
Nihar Thakkar committed
173
}