forked from Groggy/race-control-tv
-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
In-App Updating Option (Closes #121) #133
Open
LoVega1337
wants to merge
7
commits into
leonardoxh:master
Choose a base branch
from
LoVega1337:AutoUpdate
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
73b2c65
Introduced Auto Update
LoVega1337 cc727f8
Fixed link
LoVega1337 885e5b4
Replace old APK if old Update exists
LoVega1337 c1822e3
Multiple Fixes
LoVega1337 70d0122
Added notifications and checks
LoVega1337 e443e03
Show new version in update notification
LoVega1337 7f6ab8e
Merge branch 'master' into AutoUpdate
LoVega1337 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
app/src/main/java/fr/groggy/racecontrol/tv/upd/DownloadApk.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package fr.groggy.racecontrol.tv.upd | ||
|
||
import android.app.ProgressDialog | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.net.Uri | ||
import android.os.AsyncTask | ||
import android.os.Build | ||
import android.os.Environment | ||
import android.util.Log | ||
import android.webkit.URLUtil | ||
import android.widget.Toast | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.core.content.FileProvider | ||
import fr.groggy.racecontrol.tv.R | ||
import java.io.File | ||
import java.io.FileOutputStream | ||
import java.io.IOException | ||
import java.net.HttpURLConnection | ||
import java.net.MalformedURLException | ||
import java.net.URL | ||
|
||
|
||
class DownloadApk(var context: Context) : AppCompatActivity() { | ||
|
||
@JvmOverloads | ||
fun startDownloadingApk(url: String, fileName: String = "RaceControlTV") { | ||
if (URLUtil.isValidUrl(url)) { | ||
DownloadNewVersion(context, url, fileName).execute() | ||
} | ||
} | ||
|
||
@Suppress("DEPRECATION") | ||
private class DownloadNewVersion( | ||
val context: Context, | ||
val downloadUrl: String, | ||
val fileName: String | ||
): AsyncTask<String, Int, Boolean>() { | ||
private lateinit var bar: ProgressDialog | ||
override fun onPreExecute() { | ||
super.onPreExecute() | ||
bar = ProgressDialog(context).apply { | ||
setCancelable(false) | ||
setMessage(context.getResources().getString(R.string.update_downloading)) | ||
isIndeterminate = true | ||
setCanceledOnTouchOutside(false) | ||
show() | ||
} | ||
} | ||
|
||
override fun onProgressUpdate(vararg values: Int?) { | ||
super.onProgressUpdate(*values) | ||
var msg = "" | ||
val progress = values[0] | ||
if (progress != null) { | ||
bar.progress = progress | ||
msg = if (progress > 99) context.getResources().getString(R.string.update_finishing) else "${context.getResources().getString(R.string.update_downloading)} $progress%" | ||
} | ||
|
||
bar.apply { | ||
isIndeterminate = false | ||
max = 100 | ||
setMessage(msg) | ||
} | ||
} | ||
|
||
override fun onPostExecute(result: Boolean?) { | ||
super.onPostExecute(result) | ||
bar.dismiss() | ||
if (result != null && result) { | ||
Toast.makeText(context, R.string.update_downloaded, Toast.LENGTH_SHORT).show() | ||
} else { | ||
Toast.makeText(context, R.string.update_error, Toast.LENGTH_SHORT).show() | ||
} | ||
} | ||
|
||
override fun doInBackground(vararg p0: String?): Boolean { | ||
var flag = false | ||
|
||
try { | ||
val path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/" | ||
var outputFile = File("$path$fileName.apk") | ||
if (outputFile.exists()) { | ||
outputFile.delete() | ||
outputFile = File("$path$fileName.apk") | ||
} | ||
|
||
val directory = File(path) | ||
if (!directory.exists()) { | ||
directory.mkdirs() | ||
} | ||
|
||
val url = URL(downloadUrl) | ||
val c = url.openConnection() as HttpURLConnection | ||
c.requestMethod = "GET" | ||
c.connect() | ||
|
||
val fos = FileOutputStream(outputFile) | ||
val inputStream = c.inputStream | ||
val totalSize = c.contentLength.toFloat() //size of apk | ||
|
||
val buffer = ByteArray(1024) | ||
var len1: Int | ||
var per: Float | ||
var downloaded = 0f | ||
while (inputStream.read(buffer).also { len1 = it } != -1) { | ||
fos.write(buffer, 0, len1) | ||
downloaded += len1 | ||
per = (downloaded * 100 / totalSize) | ||
publishProgress(per.toInt()) | ||
} | ||
fos.close() | ||
inputStream.close() | ||
openNewVersion(outputFile.path) | ||
flag = true | ||
} catch (e: MalformedURLException) { | ||
Log.e("DownloadApk", "Update Error: " + e.message) | ||
flag = false | ||
} catch (e: IOException) { | ||
e.printStackTrace() | ||
} | ||
|
||
return flag | ||
} | ||
|
||
private fun openNewVersion(location: String) { | ||
val intent = Intent(Intent.ACTION_VIEW) | ||
intent.setDataAndType( | ||
getUriFromFile(location), | ||
"application/vnd.android.package-archive" | ||
) | ||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) | ||
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) | ||
context.startActivity(intent) | ||
} | ||
|
||
private fun getUriFromFile(filePath: String): Uri { | ||
return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { | ||
Uri.fromFile(File(filePath)) | ||
} else { | ||
FileProvider.getUriForFile( | ||
context, | ||
context.packageName + ".provider", | ||
File(filePath) | ||
) | ||
} | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
app/src/main/res/drawable/ic_baseline_system_update_alt_24.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24" | ||
android:viewportHeight="24" | ||
android:tint="?attr/colorControlNormal"> | ||
<path | ||
android:fillColor="@android:color/white" | ||
android:pathData="M12,16.5l4,-4h-3v-9h-2v9L8,12.5l4,4zM21,3.5h-6v1.99h6v14.03L3,19.52L3,5.49h6L9,3.5L3,3.5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h18c1.1,0 2,-0.9 2,-2v-14c0,-1.1 -0.9,-2 -2,-2z"/> | ||
</vector> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this entire thing to a separate class like
updateService
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And also use coroutines to handle this network part.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I can do that.
I thought about planning it like this: Merging and publishing a new release with this current version of the updater so the users have a general update service, so they don't need to manually sideload the new version every time.
After this I'll open a new PR to refine the code and put them into another class and also convert the ThreadWorker to Coroutines. This then can be updated into another release when there are other significant changes to the app.
Do we wanna do it like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is better to put all of this in 1 PR.
Because this async task work and manual work seems like 2010 :D Also
AsyncTask
is deprecated...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And second... As the app is pretty much dead (no live content yet) I'd say no rush on this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay so I have played around with Coroutines a bit and everything I try fails, so I need to do some research on this, since I only worked on small projects and with Async tasks so far. So this might take a while.