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

3
4
5
6
7
import android.annotation.SuppressLint
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
8
import android.content.pm.PackageManager
Nihar Thakkar's avatar
Nihar Thakkar committed
9
import android.os.Bundle
10
11
12
import android.os.IBinder
import android.os.Message
import android.os.Messenger
13
14
15
import android.support.design.internal.BottomNavigationItemView
import android.support.design.internal.BottomNavigationMenuView
import android.support.design.widget.BottomNavigationView
16
import android.support.v4.app.Fragment
17
import android.support.v7.app.AppCompatActivity
18
import android.view.MenuItem
19
import android.widget.Toast
20
21
import io.eelo.appinstaller.application.model.InstallManager
import io.eelo.appinstaller.application.model.InstallManagerService
22
23
24
25
26
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
27
import io.eelo.appinstaller.utlis.Constants.STORAGE_PERMISSION_REQUEST_CODE
28
import kotlinx.android.synthetic.main.activity_main.*
Nihar Thakkar's avatar
Nihar Thakkar committed
29

30
class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemSelectedListener {
31
    private var currentFragment: Fragment? = null
32
33
    private val homeFragment = HomeFragment()
    private val categoriesFragment = CategoriesFragment()
34
    private val searchFragment = SearchFragment()
35
36
    private val updatesFragment = UpdatesFragment()
    private val settingsFragment = SettingsFragment()
Nihar Thakkar's avatar
Nihar Thakkar committed
37
38
39
40

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

42
43
44
        Thread {
            val installManager = createInstallManager()
            initialiseFragments(installManager)
45
            // Show the home fragment by default
46
47
            showFragment(homeFragment)
        }.start()
48

49
        bottom_navigation_view.setOnNavigationItemSelectedListener(this)
50
51

        // Disable shifting of nav bar items
52
        removeShiftMode(bottom_navigation_view)
Nihar Thakkar's avatar
Nihar Thakkar committed
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
    private fun initialiseFragments(installManager: InstallManager) {
        searchFragment.initialise(installManager)
        updatesFragment.initialise(installManager)
    }

    private fun createInstallManager(): InstallManager {
        startService(Intent(this, InstallManagerService::class.java))
        val blocker = Object()
        var installManager: InstallManager? = null
        bindService(Intent(this, InstallManagerService::class.java), object : ServiceConnection {
            override fun onServiceConnected(name: ComponentName, service: IBinder) {
                Messenger(service).send(Message.obtain(null, 0, { result: InstallManager ->
                    installManager = result
                    synchronized(blocker) {
                        blocker.notify()
                    }
                }))
            }

            override fun onServiceDisconnected(name: ComponentName) {}
        }, Context.BIND_AUTO_CREATE)
        synchronized(blocker) {
            blocker.wait()
        }
        return installManager!!
    }

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        when {
            item.itemId == R.id.menu_home -> {
                showFragment(homeFragment)
                return true
            }
            item.itemId == R.id.menu_categories -> {
                showFragment(categoriesFragment)
                return true
            }
            item.itemId == R.id.menu_search -> {
                showFragment(searchFragment)
                return true
            }
            item.itemId == R.id.menu_updates -> {
                showFragment(updatesFragment)
                return true
            }
            item.itemId == R.id.menu_settings -> {
                showFragment(settingsFragment)
                return true
            }
        }
        return false
    }

    private fun showFragment(fragment: Fragment) {
        supportFragmentManager
                .beginTransaction()
                .replace(R.id.frame_layout, fragment)
                .commit()
113
        currentFragment = fragment
114
115
    }

116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    @SuppressLint("RestrictedApi")
    private fun removeShiftMode(bottomNavigationView: BottomNavigationView) {
        val menuView = bottomNavigationView.getChildAt(0) as BottomNavigationMenuView
        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)
        }
    }
136
137
138
139
140
141

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        if (requestCode == STORAGE_PERMISSION_REQUEST_CODE && grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_DENIED) {
            Toast.makeText(this, resources.getString(R.string.error_storage_permission_denied), Toast.LENGTH_LONG).show()
        }
    }
Nihar Thakkar's avatar
Nihar Thakkar committed
142
}