Commit 7da5fd3b authored by jo's avatar jo
Browse files

don't repeat same actions (connect to installManagerService)

parent 11598fe2
package io.eelo.appinstaller
import android.annotation.SuppressLint
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.IBinder
import android.os.Message
import android.os.Messenger
import android.support.design.internal.BottomNavigationItemView
import android.support.design.internal.BottomNavigationMenuView
import android.support.design.widget.BottomNavigationView
......@@ -18,12 +11,13 @@ import android.support.v7.app.AppCompatActivity
import android.view.MenuItem
import android.widget.Toast
import io.eelo.appinstaller.application.model.InstallManager
import io.eelo.appinstaller.application.model.InstallManagerService
import io.eelo.appinstaller.application.model.InstallManagerGetter
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
import io.eelo.appinstaller.utils.Common
import io.eelo.appinstaller.utils.Constants.STORAGE_PERMISSION_REQUEST_CODE
import kotlinx.android.synthetic.main.activity_main.*
......@@ -34,23 +28,21 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS
private val searchFragment = SearchFragment()
private val updatesFragment = UpdatesFragment()
private val settingsFragment = SettingsFragment()
private lateinit var serviceConnection: ServiceConnection
private val installManagerGetter = InstallManagerGetter()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Thread {
val installManager = createInstallManager()
Common.EXECUTOR.submit {
val installManager = installManagerGetter.connectAndGet(this)
initialiseFragments(installManager)
// Show the home fragment by default
showFragment(homeFragment)
}.start()
}
bottom_navigation_view.setOnNavigationItemSelectedListener(this)
// Disable shifting of nav bar items
removeShiftMode(bottom_navigation_view)
disableShiftingOfNabBarItems()
}
private fun initialiseFragments(installManager: InstallManager) {
......@@ -60,29 +52,6 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS
updatesFragment.initialise(installManager)
}
private fun createInstallManager(): InstallManager {
startService(Intent(this, InstallManagerService::class.java))
val blocker = Object()
var installManager: InstallManager? = null
serviceConnection = 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) {}
}
bindService(Intent(this, InstallManagerService::class.java), serviceConnection, Context.BIND_AUTO_CREATE)
synchronized(blocker) {
blocker.wait()
}
return installManager!!
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when {
item.itemId == R.id.menu_home -> {
......@@ -118,8 +87,8 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS
}
@SuppressLint("RestrictedApi")
private fun removeShiftMode(bottomNavigationView: BottomNavigationView) {
val menuView = bottomNavigationView.getChildAt(0) as BottomNavigationMenuView
private fun disableShiftingOfNabBarItems() {
val menuView = bottom_navigation_view.getChildAt(0) as BottomNavigationMenuView
try {
val mShiftingMode = menuView.javaClass.getDeclaredField("mShiftingMode")
mShiftingMode.isAccessible = true
......@@ -146,6 +115,6 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS
override fun onDestroy() {
super.onDestroy()
unbindService(serviceConnection)
installManagerGetter.disconnect(this)
}
}
......@@ -2,12 +2,10 @@ package io.eelo.appinstaller.application
import android.Manifest
import android.annotation.SuppressLint
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.content.pm.PackageManager
import android.os.*
import android.os.AsyncTask
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.text.Html
......@@ -25,7 +23,7 @@ import kotlin.math.roundToInt
class ApplicationActivity : AppCompatActivity(), ApplicationStateListener {
private lateinit var application: Application
private lateinit var serviceConnection: ServiceConnection
private val installManagerGetter = InstallManagerGetter()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......@@ -151,46 +149,24 @@ class ApplicationActivity : AppCompatActivity(), ApplicationStateListener {
// TODO alert the user of the error (while downloading)
}
private fun createInstallManager(): InstallManager {
startService(Intent(this, InstallManagerService::class.java))
val blocker = Object()
var installManager: InstallManager? = null
serviceConnection = 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) {}
}
bindService(Intent(this, InstallManagerService::class.java), serviceConnection, Context.BIND_AUTO_CREATE)
synchronized(blocker) {
blocker.wait()
}
return installManager!!
}
inner class InitialiseTask : AsyncTask<String, Void, Void>() {
override fun doInBackground(vararg params: String): Void? {
val installManager = createInstallManager()
application = installManager.findOrCreateApp(this@ApplicationActivity, ApplicationData(params[0]))
private inner class InitialiseTask : AsyncTask<String, Any, Any>() {
override fun doInBackground(vararg params: String): Any? {
val context = this@ApplicationActivity
val installManager = installManagerGetter.connectAndGet(context)
application = installManager.findOrCreateApp(context, ApplicationData(params[0]))
if (application.data.fullnessLevel != 2) {
application.searchFullData()
}
return null
}
override fun onPostExecute(result: Void?) {
override fun onPostExecute(result: Any?) {
onApplicationInfoLoaded()
}
}
override fun onDestroy() {
super.onDestroy()
unbindService(serviceConnection)
installManagerGetter.disconnect(this)
}
}
package io.eelo.appinstaller.application.model
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.IBinder
import android.os.Message
import android.os.Messenger
class InstallManagerGetter : ServiceConnection {
private lateinit var installManager: InstallManager
private val blocker = Object()
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?) {
}
fun connectAndGet(context: Context): InstallManager {
context.startService(Intent(context, InstallManagerService::class.java))
val blocker = Object()
context.bindService(Intent(context, InstallManagerService::class.java), this, Context.BIND_AUTO_CREATE)
synchronized(blocker) {
blocker.wait()
}
return installManager
}
fun disconnect(context: Context) {
context.unbindService(this)
}
}
......@@ -2,7 +2,6 @@ package io.eelo.appinstaller.application.model
import android.os.AsyncTask
import io.eelo.appinstaller.utils.Common
import java.util.concurrent.atomic.AtomicBoolean
class ThreadedListeners(private val action: () -> Unit) {
......@@ -11,7 +10,7 @@ class ThreadedListeners(private val action: () -> Unit) {
private val waiter = Object()
fun start() {
Thread(this::run).start()
Common.EXECUTOR.submit(this::run)
}
fun stop() {
......
......@@ -12,10 +12,11 @@ import android.view.View
import android.widget.ProgressBar
import io.eelo.appinstaller.R
import io.eelo.appinstaller.application.model.Application
import io.eelo.appinstaller.application.model.InstallManager
import io.eelo.appinstaller.application.model.InstallManagerGetter
import io.eelo.appinstaller.categories.activity.viewModel.CategoryViewModel
import io.eelo.appinstaller.categories.model.Category
import io.eelo.appinstaller.common.ApplicationListAdapter
import io.eelo.appinstaller.utils.Common
import io.eelo.appinstaller.utils.Constants.CATEGORY_KEY
class CategoryActivity : AppCompatActivity() {
......@@ -25,6 +26,7 @@ class CategoryActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
private lateinit var progressBar: ProgressBar
private var applicationList = ArrayList<Application>()
private val installManagerGetter = InstallManagerGetter()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......@@ -35,11 +37,15 @@ class CategoryActivity : AppCompatActivity() {
supportActionBar?.setDisplayHomeAsUpEnabled(true)
category = intent.getSerializableExtra(CATEGORY_KEY) as Category
supportActionBar?.setTitle(category.title)
supportActionBar?.title = category.title
categoryViewModel = ViewModelProviders.of(this).get(CategoryViewModel::class.java)
// TODO categoryViewModel.initialise(installManager, category.id)
categoryViewModel.loadApplications(this)
Common.EXECUTOR.submit {
val installManager = installManagerGetter.connectAndGet(this)
categoryViewModel.initialise(installManager, category.id)
categoryViewModel.loadApplications(this)
}
recyclerView = findViewById(R.id.app_list)
recyclerView.visibility = View.GONE
......@@ -69,4 +75,9 @@ class CategoryActivity : AppCompatActivity() {
}
return true
}
override fun onDestroy() {
super.onDestroy()
installManagerGetter.disconnect(this)
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment