MainActivity.kt 12.9 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
Dayona Joseph's avatar
Dayona Joseph committed
21

narinder Rana's avatar
narinder Rana committed
22

23
import android.annotation.SuppressLint
Dayona Joseph's avatar
Dayona Joseph committed
24
import android.content.*
25
import android.content.pm.PackageManager
narinder Rana's avatar
narinder Rana committed
26
import android.content.res.ColorStateList
27
import android.database.Cursor
narinder Rana's avatar
narinder Rana committed
28
import android.graphics.Color
Nihar Thakkar's avatar
Nihar Thakkar committed
29
import android.os.Bundle
Arnau Vàzquez's avatar
Arnau Vàzquez committed
30
31
import android.os.Handler
import android.preference.PreferenceManager
32
import android.util.Log
narinder Rana's avatar
narinder Rana committed
33
import android.util.TypedValue
34
import android.view.MenuItem
Arnau Vàzquez's avatar
Arnau Vàzquez committed
35
import android.widget.Toast
narinder Rana's avatar
narinder Rana committed
36
37
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
Arnau Vàzquez's avatar
Arnau Vàzquez committed
38
import androidx.appcompat.app.AppCompatActivity
narinder Rana's avatar
narinder Rana committed
39
40
41
import androidx.appcompat.view.ContextThemeWrapper
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.DrawableCompat
Arnau Vàzquez's avatar
Arnau Vàzquez committed
42
43
44
45
46
47
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
48
49
50
51
52
53
54
55
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
56
import foundation.e.apps.updates.UpdatesManager
57
58
import foundation.e.apps.utils.Constants
import foundation.e.apps.utils.Constants.CURRENTLY_SELECTED_FRAGMENT_KEY
59
import foundation.e.apps.utils.PreferenceStorage
60
import kotlinx.android.synthetic.main.activity_main.*
Nihar Thakkar's avatar
Nihar Thakkar committed
61

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

63
64
class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemSelectedListener,
        ApplicationManagerServiceConnectionCallback {
Arnau Vàzquez's avatar
Arnau Vàzquez committed
65

66
    private var currentFragmentId = 0
67
    private val homeFragment = HomeFragment()
68
    private val searchFragment = SearchFragment()
69
    private val updatesFragment = UpdatesFragment()
70
71
    private val applicationManagerServiceConnection =
            ApplicationManagerServiceConnection(this)
Arnau Vàzquez's avatar
Arnau Vàzquez committed
72
73
    private val codeRequestPermissions = 9527
    var doubleBackToExitPressedOnce = false;
74
    private var isReceiverRegistered = false
narinder Rana's avatar
narinder Rana committed
75
    var accentColorOS = 0
Arnau Vàzquez's avatar
Arnau Vàzquez committed
76
77
78
79
80


    companion object {
        lateinit var mActivity: MainActivity
        var sharedPreferences : SharedPreferences?=null
81
        val sharedPrefFile = "kotlinsharedpreference"
Arnau Vàzquez's avatar
Arnau Vàzquez committed
82
83
    }

Nihar Thakkar's avatar
Nihar Thakkar committed
84

narinder Rana's avatar
narinder Rana committed
85

Nihar Thakkar's avatar
Nihar Thakkar committed
86
87
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
narinder Rana's avatar
narinder Rana committed
88
        //ThemeColors(this);
Nihar Thakkar's avatar
Nihar Thakkar committed
89
        setContentView(R.layout.activity_main)
Arnau Vàzquez's avatar
Arnau Vàzquez committed
90
91
92
        mActivity = this
        disableCategoryIfOpenSource()

narinder Rana's avatar
narinder Rana committed
93

Arnau Vàzquez's avatar
Arnau Vàzquez committed
94
95
96
97
98
99
100
101
        bottom_navigation_view.setOnNavigationItemSelectedListener{
            if (selectFragment(it.itemId,it)) {
                disableCategoryIfOpenSource()
                currentFragmentId = it.itemId
                return@setOnNavigationItemSelectedListener true
            }
            return@setOnNavigationItemSelectedListener false
        }
102

Dayona Joseph's avatar
Dayona Joseph committed
103

104
        disableShiftingOfNabBarItems()
105

Nihar Thakkar's avatar
Nihar Thakkar committed
106
107
        initialiseUpdatesWorker()

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

109
110
111
112
        // Show the home fragment by default
        currentFragmentId = if (savedInstanceState != null &&
                savedInstanceState.containsKey(CURRENTLY_SELECTED_FRAGMENT_KEY)) {
            savedInstanceState.getInt(CURRENTLY_SELECTED_FRAGMENT_KEY)
113
114
        } else if (intent.hasExtra(Constants.UPDATES_NOTIFICATION_CLICK_EXTRA)) {
            R.id.menu_updates
115
116
117
        } else {
            R.id.menu_home
        }
Dayona Joseph's avatar
Dayona Joseph committed
118
        setupLangReceiver()
119
        applicationManagerServiceConnection.bindService(this)
narinder Rana's avatar
narinder Rana committed
120
121
122

        getAccentColor();
        bottom_navigation_view_color()
123
        openSearchFragment()
narinder Rana's avatar
narinder Rana committed
124
125
    }

126
127
128
129
130
131
132
133
134
135
136
137
138
    override fun onResume() {
        super.onResume()
        if (retrieveStatus() != null) {
            if (retrieveStatus().equals("true")) {
                PreferenceStorage(this).save(getString(R.string.prefs_microg_vrsn_installed), true)
            } else {
                PreferenceStorage(this).save(getString(R.string.prefs_microg_vrsn_installed), false)
            }
        } else {
            PreferenceStorage(this).save(getString(R.string.prefs_microg_vrsn_installed), false)
        }
    }

139
140
141
142
143
144
145
146
147
148
    private fun openSearchFragment() {
        if (intent.getBooleanExtra(Constants.OPEN_SEARCH,false)) {
            currentFragmentId = R.id.menu_search
            val bundle = Bundle()
            bundle.putString(Constants.MICROG_QUERY,"microg")
            searchFragment.arguments= bundle
        }
    }


narinder Rana's avatar
narinder Rana committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
    private fun bottom_navigation_view_color() {
        val iconsColorStates =
                ColorStateList(arrayOf(intArrayOf(-android.R.attr.state_checked),
                        intArrayOf(android.R.attr.state_checked)), intArrayOf(
                        Color.parseColor("#C4CFD9"),
                        accentColorOS
        ))

        val textColorStates = ColorStateList(arrayOf(intArrayOf(-android.R.attr.state_checked), intArrayOf(android.R.attr.state_checked)), intArrayOf(
                Color.parseColor("#C4CFD9"),
                accentColorOS

        ))

        bottom_navigation_view.setItemIconTintList(iconsColorStates)
        bottom_navigation_view.setItemTextColor(textColorStates)

166
    }
167

168
169
170
171
172
173
174
175
176
177
178
179
180
    private fun retrieveStatus(): String? {
        var status: String? = null
        val c: Cursor? = contentResolver.query(MicroGProvider.CONTENT_URI, null, "id=?", arrayOf("1"), "installStatus")
        if (c!!.moveToFirst()) {
            do {
                status = c.getString(c.getColumnIndex("installStatus"))
            } while (c.moveToNext())
        }
        c.close()
        return status
    }


Nihar Thakkar's avatar
Nihar Thakkar committed
181
    private fun initialiseUpdatesWorker() {
182
        UpdatesManager(applicationContext).startWorker()
Arnau Vàzquez's avatar
Arnau Vàzquez committed
183
184


Nihar Thakkar's avatar
Nihar Thakkar committed
185
186
    }

187
188
    override fun onServiceBind(applicationManager: ApplicationManager) {
        initialiseFragments(applicationManager)
Arnau Vàzquez's avatar
Arnau Vàzquez committed
189
        selectFragment(currentFragmentId, null)
Nihar Thakkar's avatar
Nihar Thakkar committed
190
    }
191

192
    private fun initialiseFragments(applicationManager: ApplicationManager) {
narinder Rana's avatar
narinder Rana committed
193
194
195
        homeFragment.initialise(applicationManager, accentColorOS)
        searchFragment.initialise(applicationManager, accentColorOS)
        updatesFragment.initialise(applicationManager, accentColorOS)
196
197
    }

198
    override fun onNavigationItemSelected(item: MenuItem): Boolean {
Arnau Vàzquez's avatar
Arnau Vàzquez committed
199
        if (selectFragment(item.itemId,item)) {
200
201
202
203
204
205
            currentFragmentId = item.itemId
            return true
        }
        return false
    }

Arnau Vàzquez's avatar
Arnau Vàzquez committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
    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"
    }

narinder Rana's avatar
narinder Rana committed
221
222
223
224
225
226
227
    fun tintMenuIcon(context: Context, item: MenuItem, @ColorRes color: Int) {
        val normalDrawable = item.icon
        val wrapDrawable = DrawableCompat.wrap(normalDrawable)

        DrawableCompat.setTint(wrapDrawable, ContextCompat.getColor(context, color))//context.resources.getColor(color))
        item.icon = wrapDrawable
    }
Arnau Vàzquez's avatar
Arnau Vàzquez committed
228
229

    private fun selectFragment(fragmentId: Int, item: MenuItem?): Boolean {
narinder Rana's avatar
narinder Rana committed
230

231
        when (fragmentId) {
narinder Rana's avatar
narinder Rana committed
232

233
            R.id.menu_home -> {
Arnau Vàzquez's avatar
Arnau Vàzquez committed
234
                item?.setIcon(R.drawable.ic_menu_home)
235
                showFragment(homeFragment)
narinder Rana's avatar
narinder Rana committed
236

237
238
                return true
            }
239
            R.id.menu_categories -> {
Arnau Vàzquez's avatar
Arnau Vàzquez committed
240
                item?.setIcon(R.drawable.ic_menu_categories)
Nihar Thakkar's avatar
Nihar Thakkar committed
241
                showFragment(CategoriesFragment())
narinder Rana's avatar
narinder Rana committed
242

243
244
                return true
            }
245
            R.id.menu_search -> {
narinder Rana's avatar
narinder Rana committed
246
                item?.setIcon(lineageos.platform.R.drawable.ic_search)
247
248
249
                showFragment(searchFragment)
                return true
            }
Nihar Thakkar's avatar
Nihar Thakkar committed
250
            R.id.menu_updates -> {
Arnau Vàzquez's avatar
Arnau Vàzquez committed
251
                item?.setIcon(R.drawable.ic_menu_updates)
252
253
                showFragment(updatesFragment)
                return true
Nihar Thakkar's avatar
Nihar Thakkar committed
254
            }
255
            R.id.menu_settings -> {
narinder Rana's avatar
narinder Rana committed
256
                item?.setIcon(lineageos.platform.R.drawable.ic_settings)
Nihar Thakkar's avatar
Nihar Thakkar committed
257
                showFragment(SettingsFragment())
258
259
260
261
262
                return true
            }
        }
        return false
    }
Dayona Joseph's avatar
Dayona Joseph committed
263
264
265
266
267
268
269
270
271
272
    private var mLangReceiver: BroadcastReceiver? = null
    protected fun setupLangReceiver(): BroadcastReceiver? {
        if (mLangReceiver == null) {
            mLangReceiver = object : BroadcastReceiver() {
                override fun onReceive(context: Context?, intent: Intent?) {
                    finish()
                }
            }
            val filter = IntentFilter(Intent.ACTION_LOCALE_CHANGED)
            registerReceiver(mLangReceiver, filter)
273
            isReceiverRegistered = true;
Dayona Joseph's avatar
Dayona Joseph committed
274
275
276
        }
        return mLangReceiver
    }
277
278

    private fun showFragment(fragment: Fragment) {
279
        bottom_navigation_view.menu.findItem(currentFragmentId).isChecked = true
280
281
282
        supportFragmentManager
                .beginTransaction()
                .replace(R.id.frame_layout, fragment)
Dayona Joseph's avatar
Dayona Joseph committed
283
                .commitAllowingStateLoss();
284
285
    }

286
    @SuppressLint("RestrictedApi")
287
288
    private fun disableShiftingOfNabBarItems() {
        val menuView = bottom_navigation_view.getChildAt(0) as BottomNavigationMenuView
289
290
291
292
293
294
295
296
297
298
299
300
301
        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
302
303
304
305
306
307
308
            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)
309
310
        }
    }
311

312
313
314
315
316
317
318
319
320
    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
321
    override fun onSaveInstanceState(outState: Bundle) {
322
323
324
325
        super.onSaveInstanceState(outState)
        outState?.putInt(CURRENTLY_SELECTED_FRAGMENT_KEY, currentFragmentId)
    }

326
327
    override fun onDestroy() {
        super.onDestroy()
328
329
330
331
        if (isReceiverRegistered) {
            unregisterReceiver(mLangReceiver)
            isReceiverRegistered = false // set it back to false.
        }
332
333
334
        homeFragment.decrementApplicationUses()
        searchFragment.decrementApplicationUses()
        updatesFragment.decrementApplicationUses()
335
        applicationManagerServiceConnection.unbindService(this)
336
    }
Arnau Vàzquez's avatar
Arnau Vàzquez committed
337
338
339
340
341
342
343
344

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

        this.doubleBackToExitPressedOnce = true;
Dayona Joseph's avatar
Dayona Joseph committed
345
        Toast.makeText(this, R.string.exit, Toast.LENGTH_SHORT).show();
Arnau Vàzquez's avatar
Arnau Vàzquez committed
346
347
348
349
350
351
352

        Handler().postDelayed(Runnable() {
            run {
                doubleBackToExitPressedOnce = false;
            }
        }, 2000)
    }
narinder Rana's avatar
narinder Rana committed
353
354
355
356
357
358
359

    /*
    * get Accent color from OS
    *
    *  */
    private fun getAccentColor() {

narinder Rana's avatar
narinder Rana committed
360
361
362
363
        accentColorOS=this.resources.getColor(R.color.colorAccent);



narinder Rana's avatar
narinder Rana committed
364
365
    }

366
}