MainActivity.kt 8.99 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

Arnau Vàzquez's avatar
Arnau Vàzquez committed
20
//import androidx.fragment.app.ListFragment
21
import android.annotation.SuppressLint
Arnau Vàzquez's avatar
Arnau Vàzquez committed
22
import android.content.SharedPreferences
23
import android.content.pm.PackageManager
Nihar Thakkar's avatar
Nihar Thakkar committed
24
import android.os.Bundle
Arnau Vàzquez's avatar
Arnau Vàzquez committed
25
26
import android.os.Handler
import android.preference.PreferenceManager
27
import android.view.MenuItem
Arnau Vàzquez's avatar
Arnau Vàzquez committed
28
29
30
31
32
33
34
35
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.google.android.material.bottomnavigation.BottomNavigationItemView
import com.google.android.material.bottomnavigation.BottomNavigationMenuView
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.bottomnavigation.LabelVisibilityMode
import com.google.android.material.snackbar.Snackbar
36
37
38
39
40
41
42
43
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
44
import foundation.e.apps.updates.UpdatesManager
45
46
import foundation.e.apps.utils.Constants
import foundation.e.apps.utils.Constants.CURRENTLY_SELECTED_FRAGMENT_KEY
47
import kotlinx.android.synthetic.main.activity_main.*
Nihar Thakkar's avatar
Nihar Thakkar committed
48

Arnau Vàzquez's avatar
Arnau Vàzquez committed
49

50
51
class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemSelectedListener,
        ApplicationManagerServiceConnectionCallback {
Arnau Vàzquez's avatar
Arnau Vàzquez committed
52

53
    private var currentFragmentId = 0
54
    private val homeFragment = HomeFragment()
55
    private val searchFragment = SearchFragment()
56
    private val updatesFragment = UpdatesFragment()
57
58
    private val applicationManagerServiceConnection =
            ApplicationManagerServiceConnection(this)
Arnau Vàzquez's avatar
Arnau Vàzquez committed
59
60
61
62
63
64
65
66
67
68
    private val codeRequestPermissions = 9527
    var doubleBackToExitPressedOnce = false;



    companion object {
        lateinit var mActivity: MainActivity
        var sharedPreferences : SharedPreferences?=null
    }

Nihar Thakkar's avatar
Nihar Thakkar committed
69
70
71
72

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
Arnau Vàzquez's avatar
Arnau Vàzquez committed
73
74
75
76
77
78
79
80
81
82
83
84
        mActivity = this
        disableCategoryIfOpenSource()


        bottom_navigation_view.setOnNavigationItemSelectedListener{
            if (selectFragment(it.itemId,it)) {
                disableCategoryIfOpenSource()
                currentFragmentId = it.itemId
                return@setOnNavigationItemSelectedListener true
            }
            return@setOnNavigationItemSelectedListener false
        }
85

86
        disableShiftingOfNabBarItems()
87

Nihar Thakkar's avatar
Nihar Thakkar committed
88
89
        initialiseUpdatesWorker()

Arnau Vàzquez's avatar
Arnau Vàzquez committed
90

91
92
93
94
        // Show the home fragment by default
        currentFragmentId = if (savedInstanceState != null &&
                savedInstanceState.containsKey(CURRENTLY_SELECTED_FRAGMENT_KEY)) {
            savedInstanceState.getInt(CURRENTLY_SELECTED_FRAGMENT_KEY)
95
96
        } else if (intent.hasExtra(Constants.UPDATES_NOTIFICATION_CLICK_EXTRA)) {
            R.id.menu_updates
97
98
99
        } else {
            R.id.menu_home
        }
100

101
102
        applicationManagerServiceConnection.bindService(this)
    }
103

Nihar Thakkar's avatar
Nihar Thakkar committed
104
    private fun initialiseUpdatesWorker() {
105
        UpdatesManager(applicationContext).startWorker()
Arnau Vàzquez's avatar
Arnau Vàzquez committed
106
107


Nihar Thakkar's avatar
Nihar Thakkar committed
108
109
    }

110
111
    override fun onServiceBind(applicationManager: ApplicationManager) {
        initialiseFragments(applicationManager)
Arnau Vàzquez's avatar
Arnau Vàzquez committed
112
        selectFragment(currentFragmentId, null)
Nihar Thakkar's avatar
Nihar Thakkar committed
113
    }
114

115
116
117
118
    private fun initialiseFragments(applicationManager: ApplicationManager) {
        homeFragment.initialise(applicationManager)
        searchFragment.initialise(applicationManager)
        updatesFragment.initialise(applicationManager)
119
120
    }

121
    override fun onNavigationItemSelected(item: MenuItem): Boolean {
Arnau Vàzquez's avatar
Arnau Vàzquez committed
122
        if (selectFragment(item.itemId,item)) {
123
124
125
126
127
128
            currentFragmentId = item.itemId
            return true
        }
        return false
    }

Arnau Vàzquez's avatar
Arnau Vàzquez committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    fun showApplicationTypePreference(): String {
        val preferences = PreferenceManager.getDefaultSharedPreferences(mActivity)
        var showAllApps = preferences.getBoolean(mActivity.getString(R.string.Show_all_apps), true)
        var showAllOpenSourceApps = preferences.getBoolean(mActivity.getString(R.string.show_only_open_source_apps_key), false)
        var showAllPwaApps = preferences.getBoolean(mActivity.getString(R.string.show_only_pwa_apps_key), false)
        if (showAllOpenSourceApps) {
            return "open"
        } else if (showAllApps) {
            return "any"
        } else if (showAllPwaApps) {
            return "pwa"
        }
        return "any"
    }


    private fun selectFragment(fragmentId: Int, item: MenuItem?): Boolean {
146
147
        when (fragmentId) {
            R.id.menu_home -> {
Arnau Vàzquez's avatar
Arnau Vàzquez committed
148
                item?.setIcon(R.drawable.ic_menu_home)
149
150
151
                showFragment(homeFragment)
                return true
            }
152
            R.id.menu_categories -> {
Arnau Vàzquez's avatar
Arnau Vàzquez committed
153
                item?.setIcon(R.drawable.ic_menu_categories)
Nihar Thakkar's avatar
Nihar Thakkar committed
154
                showFragment(CategoriesFragment())
155
156
                return true
            }
157
            R.id.menu_search -> {
Arnau Vàzquez's avatar
Arnau Vàzquez committed
158
                item?.setIcon(R.drawable.ic_menu_search)
159
160
161
                showFragment(searchFragment)
                return true
            }
Nihar Thakkar's avatar
Nihar Thakkar committed
162
            R.id.menu_updates -> {
Arnau Vàzquez's avatar
Arnau Vàzquez committed
163
                item?.setIcon(R.drawable.ic_menu_updates)
164
165
                showFragment(updatesFragment)
                return true
Nihar Thakkar's avatar
Nihar Thakkar committed
166
            }
167
            R.id.menu_settings -> {
Arnau Vàzquez's avatar
Arnau Vàzquez committed
168
                item?.setIcon(R.drawable.ic_menu_settings)
Nihar Thakkar's avatar
Nihar Thakkar committed
169
                showFragment(SettingsFragment())
170
171
172
173
174
175
176
                return true
            }
        }
        return false
    }

    private fun showFragment(fragment: Fragment) {
177
        bottom_navigation_view.menu.findItem(currentFragmentId).isChecked = true
178
179
180
181
182
183
        supportFragmentManager
                .beginTransaction()
                .replace(R.id.frame_layout, fragment)
                .commit()
    }

184
    @SuppressLint("RestrictedApi")
185
186
    private fun disableShiftingOfNabBarItems() {
        val menuView = bottom_navigation_view.getChildAt(0) as BottomNavigationMenuView
187
188
189
190
191
192
193
194
195
196
197
198
199
        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
Arnau Vàzquez's avatar
Arnau Vàzquez committed
200
201
202
203
204
205
206
            itemView.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);            itemView.setChecked(itemView.itemData.isChecked)
        }
    }

    private fun disableCategoryIfOpenSource(){
        if(showApplicationTypePreference()=="open") {
            bottom_navigation_view.menu.removeItem(R.id.menu_categories)
207
208
        }
    }
209

210
211
212
213
214
215
216
217
218
    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()
        }
    }

Arnau Vàzquez's avatar
Arnau Vàzquez committed
219
    override fun onSaveInstanceState(outState: Bundle) {
220
221
222
223
        super.onSaveInstanceState(outState)
        outState?.putInt(CURRENTLY_SELECTED_FRAGMENT_KEY, currentFragmentId)
    }

224
225
    override fun onDestroy() {
        super.onDestroy()
226
227
228
        homeFragment.decrementApplicationUses()
        searchFragment.decrementApplicationUses()
        updatesFragment.decrementApplicationUses()
229
        applicationManagerServiceConnection.unbindService(this)
230
    }
Arnau Vàzquez's avatar
Arnau Vàzquez committed
231
232
233
234
235
236
237
238

    override fun onBackPressed() {
        if (doubleBackToExitPressedOnce) {
            super.onBackPressed()
            return
        }

        this.doubleBackToExitPressedOnce = true;
Dayona Joseph's avatar
Dayona Joseph committed
239
        Toast.makeText(this, R.string.exit, Toast.LENGTH_SHORT).show();
Arnau Vàzquez's avatar
Arnau Vàzquez committed
240
241
242
243
244
245
246
247

        Handler().postDelayed(Runnable() {
            run {
                doubleBackToExitPressedOnce = false;
            }
        }, 2000)
    }
}