Commit a54298fe authored by Nihar Thakkar's avatar Nihar Thakkar
Browse files

Don't block the service connection, use callback instead

parent a2540f00
package io.eelo.appinstaller
import android.annotation.SuppressLint
import android.os.AsyncTask
import android.os.Bundle
import android.support.design.internal.BottomNavigationItemView
import android.support.design.internal.BottomNavigationMenuView
......@@ -11,21 +10,23 @@ import android.support.v7.app.AppCompatActivity
import android.view.MenuItem
import io.eelo.appinstaller.applicationmanager.ApplicationManager
import io.eelo.appinstaller.applicationmanager.ApplicationManagerServiceConnection
import io.eelo.appinstaller.applicationmanager.ApplicationManagerServiceConnectionCallback
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.CURRENTLY_SELECTED_FRAGMENT_KEY
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemSelectedListener {
class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemSelectedListener,
ApplicationManagerServiceConnectionCallback {
private var currentFragmentId = 0
private val homeFragment = HomeFragment()
private val searchFragment = SearchFragment()
private val updatesFragment = UpdatesFragment()
private val applicationManagerServiceConnection = ApplicationManagerServiceConnection()
private val applicationManagerServiceConnection =
ApplicationManagerServiceConnection(this)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......@@ -34,27 +35,20 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS
bottom_navigation_view.setOnNavigationItemSelectedListener(this)
disableShiftingOfNabBarItems()
object : AsyncTask<Void, Void, Void>() {
// Show the home fragment by default
currentFragmentId = if (savedInstanceState != null &&
savedInstanceState.containsKey(CURRENTLY_SELECTED_FRAGMENT_KEY)) {
savedInstanceState.getInt(CURRENTLY_SELECTED_FRAGMENT_KEY)
} else {
R.id.menu_home
}
override fun doInBackground(vararg p0: Void?): Void? {
val installManager = applicationManagerServiceConnection.connectAndGet(this@MainActivity)
initialiseFragments(installManager)
return null
}
applicationManagerServiceConnection.bindService(this)
}
override fun onPostExecute(result: Void?) {
// Show the home fragment by default
if (savedInstanceState != null && savedInstanceState.containsKey(CURRENTLY_SELECTED_FRAGMENT_KEY)) {
if (selectFragment(savedInstanceState.getInt(CURRENTLY_SELECTED_FRAGMENT_KEY))) {
currentFragmentId = savedInstanceState.getInt(CURRENTLY_SELECTED_FRAGMENT_KEY)
}
} else {
if (selectFragment(R.id.menu_home)) {
currentFragmentId = R.id.menu_home
}
}
}
}.executeOnExecutor(Common.EXECUTOR)
override fun onServiceBind(applicationManager: ApplicationManager) {
initialiseFragments(applicationManager)
selectFragment(currentFragmentId)
}
private fun initialiseFragments(applicationManager: ApplicationManager) {
......@@ -136,6 +130,6 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS
homeFragment.decrementApplicationUses()
searchFragment.decrementApplicationUses()
updatesFragment.decrementApplicationUses()
applicationManagerServiceConnection.disconnect(this)
applicationManagerServiceConnection.unbindService(this)
}
}
......@@ -15,7 +15,9 @@ import android.view.View
import android.widget.*
import io.eelo.appinstaller.R
import io.eelo.appinstaller.application.model.*
import io.eelo.appinstaller.applicationmanager.ApplicationManager
import io.eelo.appinstaller.applicationmanager.ApplicationManagerServiceConnection
import io.eelo.appinstaller.applicationmanager.ApplicationManagerServiceConnectionCallback
import io.eelo.appinstaller.utils.Common
import io.eelo.appinstaller.utils.Common.toMiB
import io.eelo.appinstaller.utils.Constants.APPLICATION_DESCRIPTION_KEY
......@@ -28,9 +30,12 @@ import kotlinx.android.synthetic.main.activity_application.*
import kotlinx.android.synthetic.main.install_button_layout.*
import kotlin.math.roundToInt
class ApplicationActivity : AppCompatActivity(), ApplicationStateListener {
class ApplicationActivity : AppCompatActivity(), ApplicationStateListener,
ApplicationManagerServiceConnectionCallback {
private lateinit var applicationPackageName: String
private lateinit var application: Application
private val applicationManagerServiceConnection = ApplicationManagerServiceConnection()
private val applicationManagerServiceConnection =
ApplicationManagerServiceConnection(this)
private var imageWidth = 0
private var imageHeight = 0
private var imageMargin = 0
......@@ -49,10 +54,26 @@ class ApplicationActivity : AppCompatActivity(), ApplicationStateListener {
val applicationPackageName: String? = intent.getStringExtra(APPLICATION_PACKAGE_NAME_KEY)
if (!applicationPackageName.isNullOrEmpty()) {
initialise(applicationPackageName!!)
this.applicationPackageName = applicationPackageName!!
applicationManagerServiceConnection.bindService(this)
}
}
override fun onServiceBind(applicationManager: ApplicationManager) {
application = applicationManager.findOrCreateApp(applicationPackageName)
var error: Error? = null
Execute({
error = application.assertFullData(this)
}, {
if (error == null) {
onApplicationInfoLoaded()
} else {
Toast.makeText(this, getString(Common.getScreenErrorDescriptionId(error!!)), Toast.LENGTH_LONG).show()
finish()
}
})
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.activity_application_menu, menu)
return true
......@@ -356,22 +377,6 @@ class ApplicationActivity : AppCompatActivity(), ApplicationStateListener {
})
}
private fun initialise(packageName: String) {
var error: Error? = null
Execute({
val installManager = applicationManagerServiceConnection.connectAndGet(this)
application = installManager.findOrCreateApp(packageName)
error = application.assertFullData(this)
}, {
if (error == null) {
onApplicationInfoLoaded()
} else {
Toast.makeText(this, getString(Common.getScreenErrorDescriptionId(error!!)), Toast.LENGTH_LONG).show()
finish()
}
})
}
private fun setRatingBorder(rating: Float, textView: TextView) {
when {
rating >= 7f -> {
......@@ -449,6 +454,6 @@ class ApplicationActivity : AppCompatActivity(), ApplicationStateListener {
override fun onDestroy() {
super.onDestroy()
application.decrementUses()
applicationManagerServiceConnection.disconnect(this)
applicationManagerServiceConnection.unbindService(this)
}
}
......@@ -6,14 +6,17 @@ import android.support.v4.view.ViewPager
import android.view.View
import io.eelo.appinstaller.R
import io.eelo.appinstaller.application.model.Application
import io.eelo.appinstaller.applicationmanager.ApplicationManager
import io.eelo.appinstaller.applicationmanager.ApplicationManagerServiceConnection
import io.eelo.appinstaller.applicationmanager.ApplicationManagerServiceConnectionCallback
import io.eelo.appinstaller.utils.Constants
import io.eelo.appinstaller.utils.Constants.SELECTED_APPLICATION_SCREENSHOT_KEY
import io.eelo.appinstaller.utils.Execute
import kotlinx.android.synthetic.main.activity_screenshots.*
class ScreenshotsActivity : AppCompatActivity() {
private val applicationManagerServiceConnection = ApplicationManagerServiceConnection()
class ScreenshotsActivity : AppCompatActivity(), ApplicationManagerServiceConnectionCallback {
private val applicationManagerServiceConnection =
ApplicationManagerServiceConnection(this)
private lateinit var applicationPackageName: String
private lateinit var application: Application
private lateinit var screenshotsCarousel: ViewPager
private var lastSelectedScreenshotIndex = 0
......@@ -35,17 +38,14 @@ class ScreenshotsActivity : AppCompatActivity() {
val applicationPackageName: String? =
intent.getStringExtra(Constants.APPLICATION_PACKAGE_NAME_KEY)
if (!applicationPackageName.isNullOrEmpty()) {
initialise(applicationPackageName!!)
this.applicationPackageName = applicationPackageName!!
applicationManagerServiceConnection.bindService(this)
}
}
private fun initialise(packageName: String) {
Execute({
val installManager = applicationManagerServiceConnection.connectAndGet(this)
application = installManager.findOrCreateApp(packageName)
}, {
onApplicationInfoLoaded()
})
override fun onServiceBind(applicationManager: ApplicationManager) {
application = applicationManager.findOrCreateApp(applicationPackageName)
onApplicationInfoLoaded()
}
private fun onApplicationInfoLoaded() {
......@@ -73,6 +73,6 @@ class ScreenshotsActivity : AppCompatActivity() {
override fun onDestroy() {
super.onDestroy()
application.decrementUses()
applicationManagerServiceConnection.disconnect(this)
applicationManagerServiceConnection.unbindService(this)
}
}
......@@ -8,33 +8,24 @@ import android.os.IBinder
import android.os.Message
import android.os.Messenger
class ApplicationManagerServiceConnection : ServiceConnection {
private lateinit var applicationManager: ApplicationManager
private val blocker = Object()
class ApplicationManagerServiceConnection(
private val callback: ApplicationManagerServiceConnectionCallback) : ServiceConnection {
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
Messenger(service).send(Message.obtain(null, 0, { result: ApplicationManager ->
applicationManager = result
synchronized(blocker) {
blocker.notify()
}
callback.onServiceBind(result)
}))
}
override fun onServiceDisconnected(name: ComponentName?) {
}
fun connectAndGet(context: Context): ApplicationManager {
fun bindService(context: Context) {
context.startService(Intent(context, ApplicationManagerService::class.java))
context.bindService(Intent(context, ApplicationManagerService::class.java), this, Context.BIND_AUTO_CREATE)
synchronized(blocker) {
blocker.wait()
}
return applicationManager
}
fun disconnect(context: Context) {
fun unbindService(context: Context) {
context.unbindService(this)
}
......
package io.eelo.appinstaller.applicationmanager
interface ApplicationManagerServiceConnectionCallback {
fun onServiceBind(applicationManager: ApplicationManager)
}
......@@ -17,20 +17,23 @@ import io.eelo.appinstaller.R
import io.eelo.appinstaller.application.model.Application
import io.eelo.appinstaller.applicationmanager.ApplicationManagerServiceConnection
import io.eelo.appinstaller.application.model.State
import io.eelo.appinstaller.applicationmanager.ApplicationManager
import io.eelo.appinstaller.applicationmanager.ApplicationManagerServiceConnectionCallback
import io.eelo.appinstaller.categories.category.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() {
class CategoryActivity : AppCompatActivity(), ApplicationManagerServiceConnectionCallback {
private lateinit var category: Category
private lateinit var categoryViewModel: CategoryViewModel
private lateinit var recyclerView: RecyclerView
private lateinit var progressBar: ProgressBar
private var applicationList = ArrayList<Application>()
private val applicationManagerServiceConnection = ApplicationManagerServiceConnection()
private val applicationManagerServiceConnection =
ApplicationManagerServiceConnection(this)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......@@ -85,18 +88,12 @@ class CategoryActivity : AppCompatActivity() {
}
})
object : AsyncTask<Void, Void, Void>() {
override fun doInBackground(vararg p0: Void?): Void? {
val installManager = applicationManagerServiceConnection.connectAndGet(this@CategoryActivity)
categoryViewModel.initialise(installManager, category.id)
return null
}
applicationManagerServiceConnection.bindService(this)
}
override fun onPostExecute(result: Void?) {
categoryViewModel.loadApplications(this@CategoryActivity)
}
}.executeOnExecutor(Common.EXECUTOR)
override fun onServiceBind(applicationManager: ApplicationManager) {
categoryViewModel.initialise(applicationManager, category.id)
categoryViewModel.loadApplications(this)
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
......@@ -124,6 +121,6 @@ class CategoryActivity : AppCompatActivity() {
applicationList.forEach {
it.decrementUses()
}
applicationManagerServiceConnection.disconnect(this)
applicationManagerServiceConnection.unbindService(this)
}
}
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="576dp"
android:width="384dp"
android:height="512dp"
android:viewportWidth="576"
android:viewportWidth="384"
android:viewportHeight="512">
<path
android:fillColor="#FF000000"
android:pathData="M528,288h-92.1l46.1,-46.1c30.1,-30.1 8.8,-81.9 -33.9,-81.9h-64L384.1,48c0,-26.5 -21.5,-48 -48,-48h-96c-26.5,0 -48,21.5 -48,48v112h-64c-42.6,0 -64.2,51.7 -33.9,81.9l46.1,46.1L48,288c-26.5,0 -48,21.5 -48,48v128c0,26.5 21.5,48 48,48h480c26.5,0 48,-21.5 48,-48L576,336c0,-26.5 -21.5,-48 -48,-48zM128,208h112L240,48h96v160h112L288,368 128,208zM528,464L48,464L48,336h140.1l65.9,65.9c18.8,18.8 49.1,18.7 67.9,0l65.9,-65.9L528,336v128zM440,400c0,-13.3 10.7,-24 24,-24s24,10.7 24,24 -10.7,24 -24,24 -24,-10.7 -24,-24z"/>
android:pathData="M336,176h-51.6L284.4,80c0,-26.5 -21.5,-48 -48,-48h-88.6c-26.5,0 -48,21.5 -48,48v96L48.1,176c-42.6,0 -64.2,51.7 -33.9,81.9l143.9,144c18.7,18.7 49.1,18.7 67.9,0l144,-144c30,-30.1 8.7,-81.9 -34,-81.9zM192,368L48,224h99.7L147.7,80h88.6v144L336,224L192,368zM384,444v24c0,6.6 -5.4,12 -12,12L12,480c-6.6,0 -12,-5.4 -12,-12v-24c0,-6.6 5.4,-12 12,-12h360c6.6,0 12,5.4 12,12z"/>
</vector>
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