Commit 396a9037 authored by Nihar Thakkar's avatar Nihar Thakkar
Browse files

Install app updates automatically in the background

parent 2a3e2ac0
......@@ -419,7 +419,7 @@ class ApplicationActivity : AppCompatActivity(), ApplicationStateListener,
Snackbar.LENGTH_LONG).show()
return
}
application.buttonClicked(this)
application.buttonClicked(this, this)
}
@SuppressLint("SetTextI18n")
......@@ -542,7 +542,7 @@ class ApplicationActivity : AppCompatActivity(), ApplicationStateListener,
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
if (requestCode == Constants.STORAGE_PERMISSION_REQUEST_CODE) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
application.buttonClicked(this)
application.buttonClicked(this, this)
} else if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_DENIED) {
Snackbar.make(container, R.string.error_storage_permission_denied,
Snackbar.LENGTH_LONG).show()
......
......@@ -47,7 +47,7 @@ class ApplicationViewHolder(private val activity: Activity, private val view: Vi
Error.APK_UNAVAILABLE.description),
Snackbar.LENGTH_LONG).show()
} else {
application?.buttonClicked(activity)
application?.buttonClicked(activity, activity)
}
}
}
......
......@@ -42,7 +42,7 @@ class SmallApplicationViewHolder(private val activity: Activity, private val vie
Error.APK_UNAVAILABLE.description),
Snackbar.LENGTH_LONG).show()
} else {
application?.buttonClicked(activity)
application?.buttonClicked(activity, activity)
}
}
}
......
......@@ -55,24 +55,28 @@ class Application(val packageName: String, private val applicationManager: Appli
}
@Synchronized
fun buttonClicked(activity: Activity) {
fun buttonClicked(context: Context, activity: Activity?) {
when (stateManager.state) {
INSTALLED -> info.launch(activity)
INSTALLED -> info.launch(context)
NOT_UPDATED, NOT_DOWNLOADED -> {
if (canWriteStorage(activity)) {
applicationManager.install(activity, this)
if (activity != null) {
if (canWriteStorage(activity)) {
applicationManager.install(context, this)
}
} else {
applicationManager.install(context, this)
}
}
INSTALLING -> {
if (downloader != null) {
downloader?.cancelDownload()
} else {
onDownloadComplete(activity, DownloadManager.STATUS_FAILED)
onDownloadComplete(context, DownloadManager.STATUS_FAILED)
}
return
}
}
checkForStateUpdate(activity)
checkForStateUpdate(context)
}
private fun canWriteStorage(activity: Activity): Boolean {
......
......@@ -19,7 +19,6 @@ import io.eelo.appinstaller.application.model.State
import io.eelo.appinstaller.applicationmanager.ApplicationManager
import io.eelo.appinstaller.common.ApplicationListAdapter
import io.eelo.appinstaller.updates.viewModel.UpdatesViewModel
import io.eelo.appinstaller.utils.Common
class UpdatesFragment : Fragment() {
private lateinit var updatesViewModel: UpdatesViewModel
......@@ -53,7 +52,7 @@ class UpdatesFragment : Fragment() {
updateAll.setOnClickListener {
applicationList.forEach { application ->
if (application.state == State.NOT_UPDATED) {
application.buttonClicked(activity!!)
application.buttonClicked(context!!, activity!!)
}
}
}
......
package io.eelo.appinstaller.updates.model
import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.AsyncTask
import android.preference.PreferenceManager
import android.util.Log
......@@ -8,8 +12,10 @@ import androidx.work.Worker
import androidx.work.WorkerParameters
import io.eelo.appinstaller.R
import io.eelo.appinstaller.application.model.Application
import io.eelo.appinstaller.application.model.State
import io.eelo.appinstaller.applicationmanager.ApplicationManager
import io.eelo.appinstaller.updates.UpdatesNotifier
import io.eelo.appinstaller.utils.Common
import io.eelo.appinstaller.utils.Constants
class UpdatesWorker(context: Context, params: WorkerParameters) : Worker(context, params),
......@@ -68,9 +74,44 @@ class UpdatesWorker(context: Context, params: WorkerParameters) : Worker(context
applications.size,
installAutomatically)
}
if (installAutomatically && canWriteStorage(applicationContext)) {
if (wifiOnly) {
if (isConnectedToUnmeteredNetwork(applicationContext)) {
applications.forEach {
if (it.state == State.NOT_UPDATED) {
Log.i(TAG, "Updating ${it.packageName}")
it.buttonClicked(applicationContext, null)
}
}
}
} else {
applications.forEach {
if (it.state == State.NOT_UPDATED) {
Log.i(TAG, "Updating ${it.packageName}")
it.buttonClicked(applicationContext, null)
}
}
}
}
}
synchronized(blocker) {
blocker.notify()
}
}
private fun canWriteStorage(context: Context) = !(android.os.Build.VERSION.SDK_INT >= 23 &&
context.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
PackageManager.PERMISSION_GRANTED)
private fun isConnectedToUnmeteredNetwork(context: Context): Boolean {
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as
ConnectivityManager
return if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
val network = connectivityManager.activeNetwork
val capabilities = connectivityManager.getNetworkCapabilities(network)
capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)
} else {
Common.isNetworkAvailable(context)
}
}
}
......@@ -70,7 +70,7 @@
<string name="preference_updates_title">Updates</string>
<string name="preference_update_interval_default">24</string>
<string name="preference_update_interval_title">Update check interval</string>
<string name="preference_update_wifi_only_title">Only on Wi-Fi</string>
<string name="preference_update_wifi_only_title">Only on un-metered networks</string>
<string name="preference_update_wifi_only_description">Update apps automatically only on
un-metered networks such as Wi-Fi</string>
<string name="preference_update_install_automatically_title">Automatically install updates</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