Commit 19ecfd8b authored by Arnau Vàzquez's avatar Arnau Vàzquez
Browse files

Merge branch 'issue-113' into 'master'

Show updates as soon as it is available

See merge request e/apps/apps!22
parents 4e479c5f a37c7869
Pipeline #59069 passed with stage
in 3 minutes and 26 seconds
......@@ -18,6 +18,7 @@
package foundation.e.apps.updates
import android.os.Bundle
import android.os.Handler
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
......@@ -59,6 +60,7 @@ class UpdatesFragment : Fragment() {
val updateAll = view.findViewById<Button>(R.id.update_all)
val splashContainer = view.findViewById<LinearLayout>(R.id.splash_container)
val progressBar = view.findViewById<ProgressBar>(R.id.progress_bar)
val reloadProgressBar = view.findViewById<ProgressBar>(R.id.progress_bar2)
val errorContainer = view.findViewById<LinearLayout>(R.id.error_container)
val errorDescription = view.findViewById<TextView>(R.id.error_description)
......@@ -74,6 +76,7 @@ class UpdatesFragment : Fragment() {
}
}
progressBar.visibility = View.VISIBLE
reloadProgressBar.visibility = View.GONE
errorContainer.visibility = View.GONE
splashContainer.visibility = View.GONE
view.findViewById<TextView>(R.id.error_resolve).setOnClickListener {
......@@ -90,9 +93,12 @@ class UpdatesFragment : Fragment() {
// Bind recycler view adapter to outdated applications list in view model
updatesViewModel.getApplications().observe(this, Observer {
if (it != null) {
applicationList.clear()
applicationList.addAll(it)
progressBar.visibility = View.GONE
reloadProgressBar.visibility=View.GONE
recyclerView.adapter?.notifyDataSetChanged()
recyclerView.scrollToPosition(0)
if (applicationList.isEmpty()) {
......@@ -114,28 +120,34 @@ class UpdatesFragment : Fragment() {
errorContainer.visibility = View.VISIBLE
updateAll.isEnabled = false
progressBar.visibility = View.GONE
reloadProgressBar.visibility = View.GONE
splashContainer.visibility = View.GONE
recyclerView.visibility = View.GONE
} else {
errorContainer.visibility = View.GONE
}
})
updatesViewModel.loadApplicationList(context!!)
if (updatesViewModel.getApplications().value == null ||
updatesViewModel.getApplications().value!!.isEmpty()) {
updatesViewModel.loadApplicationList(context!!)
}
return view
}
override fun onResume() {
super.onResume()
val progressBar2 = view!!.findViewById<ProgressBar>(R.id.progress_bar2)
if (::updatesViewModel.isInitialized) {
updatesViewModel.getApplications().value?.let {
it.forEach { application ->
progressBar2.visibility=View.VISIBLE
application.checkForStateUpdate(context!!)
}
val handler = Handler()
handler.postDelayed({
progressBar2.visibility=View.GONE
}, 10000)
}
}
}
......
......@@ -18,53 +18,28 @@
package foundation.e.apps.updates.model
import android.content.Context
import android.content.pm.PackageManager
import android.os.AsyncTask
import foundation.e.apps.application.model.Application
import foundation.e.apps.application.model.State
import foundation.e.apps.applicationmanager.ApplicationManager
import foundation.e.apps.utils.Common
import foundation.e.apps.utils.Constants
import foundation.e.apps.utils.Execute
import java.io.BufferedReader
import java.io.InputStreamReader
class OutdatedApplicationsFileReader(private val applicationManager: ApplicationManager,
class OutdatedApplicationsFileReader(private val packageManager: PackageManager,
private val applicationManager: ApplicationManager,
private val callback: UpdatesModelInterface) :
AsyncTask<Context, Void, ArrayList<Application>>() {
override fun doInBackground(vararg context: Context): ArrayList<Application> {
val applications = ArrayList<Application>()
var totalLines = 0
var requestsComplete = 0
val blocker = Object()
try {
context[0].openFileInput(Constants.OUTDATED_APPLICATIONS_FILENAME).use {
BufferedReader(InputStreamReader(it)).forEachLine {
totalLines++
}
}
if (totalLines > 0) {
context[0].openFileInput(Constants.OUTDATED_APPLICATIONS_FILENAME).use {
BufferedReader(InputStreamReader(it)).forEachLine { packageName ->
Execute({
val application = applicationManager.findOrCreateApp(packageName)
val error = application.assertBasicData(context[0])
if (error == null) {
if (application.state == State.NOT_UPDATED) {
applications.add(application)
}
}
}, {
requestsComplete++
if (requestsComplete == totalLines) {
synchronized(blocker) {
blocker.notify()
}
}
})
}
synchronized(blocker) {
blocker.wait()
}
}
val installedApplications = getInstalledApplications()
installedApplications.forEach { packageName ->
val application = applicationManager.findOrCreateApp(packageName)
verifyApplication(application, applications, context)
}
} catch (exception: Exception) {
exception.printStackTrace()
......@@ -75,4 +50,25 @@ class OutdatedApplicationsFileReader(private val applicationManager: Application
override fun onPostExecute(result: ArrayList<Application>) {
callback.onAppsFound(result)
}
private fun getInstalledApplications(): ArrayList<String> {
val result = ArrayList<String>()
packageManager.getInstalledApplications(0).forEach { app ->
if (!Common.isSystemApp(packageManager, app.packageName)) {
result.add(app.packageName)
}
}
return result
}
private fun verifyApplication(application: Application, apps: ArrayList<Application>,
context: Array<out Context>) {
val error = application.assertBasicData(context[0])
if (error == null && application.state == State.NOT_UPDATED) {
apps.add(application)
} else {
application.decrementUses()
}
}
}
......@@ -33,7 +33,7 @@ class UpdatesModel : UpdatesModelInterface {
override fun loadApplicationList(context: Context) {
if (Common.isNetworkAvailable(context)) {
OutdatedApplicationsFileReader(applicationManager!!, this)
OutdatedApplicationsFileReader(context.packageManager,applicationManager!!, this)
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, context)
} else {
screenError.value = Error.NO_INTERNET
......
......@@ -19,6 +19,7 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
......@@ -32,7 +33,6 @@
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="@style/AlertDialog.AppCompat.Light">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
......@@ -70,13 +70,26 @@
</androidx.appcompat.widget.Toolbar>
<ProgressBar
android:id="@+id/progress_bar2"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:paddingBottom="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar"
app:layout_constraintVertical_bias="0.0"
tools:layout_editor_absoluteX="111dp"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/app_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scrollbars="none"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar" />
app:layout_constraintTop_toBottomOf="@id/progress_bar2"/>
<include
layout="@layout/updates_splash_layout"
......
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