Commit 2a3e2ac0 authored by Nihar Thakkar's avatar Nihar Thakkar
Browse files

Major improvements to application manager, bug fixes

parent 6d652ce7
......@@ -451,7 +451,7 @@ class ApplicationActivity : AppCompatActivity(), ApplicationStateListener,
app_size.visibility = View.VISIBLE
app_download_container.visibility = View.GONE
}
State.DOWNLOADING -> {
State.INSTALLING -> {
app_install.setBackgroundResource(R.drawable.app_install_border_simple)
app_install.setTextColor(resources.getColor(android.R.color.primary_text_light))
app_install.isEnabled = true
......@@ -461,13 +461,6 @@ class ApplicationActivity : AppCompatActivity(), ApplicationStateListener,
app_download_progress.progress = 0
app_download_container.visibility = View.VISIBLE
}
State.INSTALLING -> {
app_install.setBackgroundResource(R.drawable.app_install_border)
app_install.setTextColor(resources.getColor(android.R.color.primary_text_dark))
app_install.isEnabled = false
app_size.visibility = View.VISIBLE
app_download_container.visibility = View.GONE
}
else -> {
app_install.setBackgroundResource(R.drawable.app_install_border)
app_install.setTextColor(resources.getColor(android.R.color.primary_text_dark))
......@@ -559,7 +552,8 @@ class ApplicationActivity : AppCompatActivity(), ApplicationStateListener,
override fun onResume() {
super.onResume()
if (::application.isInitialized) {
if (::application.isInitialized &&
(application.state == State.INSTALLED || application.state == State.NOT_UPDATED)) {
application.checkForStateUpdate(this)
}
}
......
......@@ -82,9 +82,6 @@ class ApplicationViewHolder(private val activity: Activity, private val view: Vi
installButton.isEnabled =
Common.appHasLaunchActivity(activity, application!!.packageName)
}
State.INSTALLING -> {
installButton.isEnabled = false
}
else -> {
installButton.isEnabled = true
}
......
......@@ -65,9 +65,6 @@ class SmallApplicationViewHolder(private val activity: Activity, private val vie
installButton.isEnabled =
Common.appHasLaunchActivity(activity, application!!.packageName)
}
State.INSTALLING -> {
installButton.isEnabled = false
}
else -> {
installButton.isEnabled = true
}
......
......@@ -60,14 +60,10 @@ class Application(val packageName: String, private val applicationManager: Appli
INSTALLED -> info.launch(activity)
NOT_UPDATED, NOT_DOWNLOADED -> {
if (canWriteStorage(activity)) {
applicationManager.download(this)
applicationManager.install(activity, this)
}
}
INSTALLING -> {
applicationManager.stopInstalling(this)
return
}
DOWNLOADING -> {
if (downloader != null) {
downloader?.cancelDownload()
} else {
......@@ -76,7 +72,7 @@ class Application(val packageName: String, private val applicationManager: Appli
return
}
}
stateManager.find(activity, basicData!!)
checkForStateUpdate(activity)
}
private fun canWriteStorage(activity: Activity): Boolean {
......@@ -102,31 +98,27 @@ class Application(val packageName: String, private val applicationManager: Appli
downloader!!.download(context)
} else {
stateManager.notifyError(error)
applicationManager.stopDownloading(this)
downloader = null
stateManager.find(context, basicData!!)
onDownloadComplete(context, DownloadManager.STATUS_FAILED)
}
}
override fun onDownloadComplete(context: Context, status: Int) {
if (status == DownloadManager.STATUS_SUCCESSFUL) {
applicationManager.install(this)
install(context)
} else {
info.getApkFile(context, basicData!!).delete()
applicationManager.stopDownloading(this)
downloader = null
applicationManager.stopInstalling(context, this)
}
stateManager.find(context, basicData!!)
downloader = null
}
fun install(context: Context) {
private fun install(context: Context) {
info.install(context, basicData!!, this)
stateManager.find(context, basicData!!)
}
override fun onInstallationComplete(context: Context) {
info.getApkFile(context, basicData!!).delete()
stateManager.find(context, basicData!!)
applicationManager.stopInstalling(context, this)
}
fun isUsed(): Boolean {
......@@ -208,7 +200,7 @@ class Application(val packageName: String, private val applicationManager: Appli
fun update(basicData: BasicData, context: Context) {
this.basicData?.let { basicData.updateLoadedImages(it) }
this.basicData = basicData
stateManager.find(context, basicData)
checkForStateUpdate(context)
}
fun update(fullData: FullData, context: Context) {
......
......@@ -118,6 +118,7 @@ class Downloader(private val applicationInfo: ApplicationInfo, private val fullD
private var onComplete: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
unregisterReceivers(context)
try {
if (!fullData.getLastVersion()!!.apkSHA.isNullOrBlank() &&
fullData.getLastVersion()!!.apkSHA!! ==
......@@ -129,7 +130,6 @@ class Downloader(private val applicationInfo: ApplicationInfo, private val fullD
exception.printStackTrace()
}
downloaderInterface.onDownloadComplete(context, DownloadManager.STATUS_FAILED)
unregisterReceivers(context)
}
}
}
......@@ -5,7 +5,6 @@ import io.eelo.appinstaller.R
enum class State(val installButtonTextId: Int) {
NOT_DOWNLOADED(R.string.action_install),
NOT_UPDATED(R.string.action_update),
DOWNLOADING(R.string.state_downloading),
INSTALLING(R.string.state_installing),
INSTALLING(R.string.action_cancel),
INSTALLED(R.string.action_launch);
}
......@@ -12,22 +12,16 @@ class StateManager(private val info: ApplicationInfo, private val app: Applicati
private set
fun find(context: Context, basicData: BasicData) {
val state: State
if (appManager.isDownloading(app)) {
state = State.DOWNLOADING
} else if (appManager.isInstalling(app)) {
state = State.INSTALLING
val state = if (appManager.isInstalling(app)) {
State.INSTALLING
} else if (info.isLastVersionInstalled(context,
basicData.lastVersionNumber ?: "")) {
state = State.INSTALLED
info.getApkFile(context, basicData).delete()
State.INSTALLED
} else if (info.isInstalled(context) && !info.isLastVersionInstalled(context,
basicData.lastVersionNumber ?: "")) {
state = State.NOT_UPDATED
info.getApkFile(context, basicData).delete()
State.NOT_UPDATED
} else {
state = State.NOT_DOWNLOADED
info.getApkFile(context, basicData).delete()
State.NOT_DOWNLOADED
}
changeState(state)
}
......@@ -39,7 +33,7 @@ class StateManager(private val info: ApplicationInfo, private val app: Applicati
fun addListener(listener: ApplicationStateListener) {
listeners.add(listener)
if (state == State.DOWNLOADING) {
if (state == State.INSTALLING) {
app.downloader?.let { listener.downloading(it) }
}
}
......
......@@ -8,8 +8,7 @@ import java.util.concurrent.LinkedBlockingQueue
class ApplicationManager {
private val apps = HashMap<String, Application>()
private val downloading = LinkedBlockingQueue<Application>()
private val installing = LinkedBlockingQueue<Application>()
private val queue = LinkedBlockingQueue<Application>()
@Synchronized
fun findOrCreateApp(packageName: String): Application {
......@@ -22,69 +21,43 @@ class ApplicationManager {
}
@Synchronized
fun download(app: Application) {
if (!downloading.contains(app)) {
downloading.put(app)
downloading.put(app)
}
}
@Synchronized
fun install(app: Application) {
if (!installing.contains(app)) {
installing.put(app)
installing.put(app)
fun install(context: Context, app: Application) {
if (!queue.contains(app)) {
queue.put(app)
queue.put(app)
}
app.checkForStateUpdate(context)
}
fun start(context: Context) {
Thread {
startDownloads(context)
}.start()
Thread {
startInstalls(context)
}.start()
}
private fun startDownloads(context: Context) {
while (true) {
val app = downloading.take()
app.download(context)
stopDownloading(app)
tryRemove(app)
}
}
private fun startInstalls(context: Context) {
while (true) {
val app = installing.take()
app.install(context)
stopInstalling(app)
val app = queue.take()
app.download(context)
stopInstalling(context, app)
tryRemove(app)
}
}
fun tryRemove(app: Application) {
if (!app.isUsed() && !installing.contains(app) && !downloading.contains(app)) {
if (!app.isUsed() && !queue.contains(app)) {
apps.remove(app.packageName)
}
}
fun stopDownloading(app: Application) {
while (downloading.remove(app)) {
}
}
fun stopInstalling(app: Application) {
while (installing.remove(app)) {
@Synchronized
fun stopInstalling(context: Context, app: Application) {
while (queue.remove(app)) {
}
app.checkForStateUpdate(context)
}
fun isInstalling(app: Application): Boolean {
return installing.contains(app)
}
fun isDownloading(app: Application): Boolean {
return downloading.contains(app)
return queue.contains(app)
}
}
......@@ -19,7 +19,6 @@ import io.eelo.appinstaller.applicationmanager.ApplicationManager
import io.eelo.appinstaller.categories.model.Category
import io.eelo.appinstaller.common.SmallApplicationListAdapter
import io.eelo.appinstaller.home.viewmodel.HomeViewModel
import io.eelo.appinstaller.utils.Common
class HomeFragment : Fragment() {
private lateinit var homeViewModel: HomeViewModel
......
......@@ -11,8 +11,8 @@
<string name="app_privacy_score_content_description">App privacy score</string>
<string name="app_energy_score_content_description">App energy score</string>
<string name="app_rating_content_description">App rating</string>
<string name="state_downloading">Cancel</string>
<string name="action_install">Install</string>
<string name="action_cancel">Cancel</string>
<string name="state_installing">Installing</string>
<string name="action_update">Update</string>
<string name="action_update_all">Update all</string>
......
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