-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a4378c0
commit eb44a86
Showing
3 changed files
with
122 additions
and
134 deletions.
There are no files selected for viewing
131 changes: 0 additions & 131 deletions
131
app/src/main/java/org/schabi/newpipe/NewVersionWorker.java
This file was deleted.
Oops, something went wrong.
122 changes: 122 additions & 0 deletions
122
app/src/main/java/org/schabi/newpipe/NewVersionWorker.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,122 @@ | ||
package org.schabi.newpipe | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import androidx.core.content.edit | ||
import androidx.preference.PreferenceManager | ||
import androidx.work.OneTimeWorkRequest | ||
import androidx.work.WorkManager | ||
import androidx.work.WorkRequest | ||
import androidx.work.Worker | ||
import androidx.work.WorkerParameters | ||
import com.grack.nanojson.JsonParser | ||
import com.grack.nanojson.JsonParserException | ||
import org.schabi.newpipe.extractor.downloader.Response | ||
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException | ||
import org.schabi.newpipe.util.ReleaseVersionUtil.coerceUpdateCheckExpiry | ||
import org.schabi.newpipe.util.ReleaseVersionUtil.compareAppVersionAndShowNotification | ||
import org.schabi.newpipe.util.ReleaseVersionUtil.isLastUpdateCheckExpired | ||
import org.schabi.newpipe.util.ReleaseVersionUtil.isReleaseApk | ||
import java.io.IOException | ||
|
||
class NewVersionWorker( | ||
context: Context, | ||
workerParams: WorkerParameters | ||
) : Worker(context, workerParams) { | ||
|
||
@Throws(IOException::class, ReCaptchaException::class) | ||
private fun checkNewVersion() { | ||
// Check if the current apk is a github one or not. | ||
if (!isReleaseApk()) { | ||
return | ||
} | ||
|
||
val prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext) | ||
// Check if the last request has happened a certain time ago | ||
// to reduce the number of API requests. | ||
val expiry = prefs.getLong(applicationContext.getString(R.string.update_expiry_key), 0) | ||
if (!isLastUpdateCheckExpired(expiry)) { | ||
return | ||
} | ||
|
||
// Make a network request to get latest NewPipe data. | ||
val response = DownloaderImpl.getInstance().get(NEWPIPE_API_URL) | ||
handleResponse(response) | ||
} | ||
|
||
private fun handleResponse(response: Response) { | ||
val prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext) | ||
try { | ||
// Store a timestamp which needs to be exceeded, | ||
// before a new request to the API is made. | ||
val newExpiry = coerceUpdateCheckExpiry(response.getHeader("expires")) | ||
prefs.edit { | ||
putLong(applicationContext.getString(R.string.update_expiry_key), newExpiry) | ||
} | ||
} catch (e: Exception) { | ||
if (DEBUG) { | ||
Log.w(TAG, "Could not extract and save new expiry date", e) | ||
} | ||
} | ||
|
||
// Parse the json from the response. | ||
try { | ||
val githubStableObject = JsonParser.`object`() | ||
.from(response.responseBody()).getObject("flavors") | ||
.getObject("github").getObject("stable") | ||
|
||
val versionName = githubStableObject.getString("version") | ||
val versionCode = githubStableObject.getInt("version_code") | ||
val apkLocationUrl = githubStableObject.getString("apk") | ||
compareAppVersionAndShowNotification(versionName, apkLocationUrl, versionCode) | ||
} catch (e: JsonParserException) { | ||
// Most likely something is wrong in data received from NEWPIPE_API_URL. | ||
// Do not alarm user and fail silently. | ||
if (DEBUG) { | ||
Log.w(TAG, "Could not get NewPipe API: invalid json", e) | ||
} | ||
} | ||
} | ||
|
||
override fun doWork(): Result { | ||
try { | ||
checkNewVersion() | ||
} catch (e: IOException) { | ||
Log.w(TAG, "Could not fetch NewPipe API: probably network problem", e) | ||
return Result.failure() | ||
} catch (e: ReCaptchaException) { | ||
Log.e(TAG, "ReCaptchaException should never happen here.", e) | ||
return Result.failure() | ||
} | ||
return Result.success() | ||
} | ||
|
||
companion object { | ||
private val DEBUG = MainActivity.DEBUG | ||
private val TAG = NewVersionWorker::class.java.simpleName | ||
private const val NEWPIPE_API_URL = "https://newpipe.net/api/data.json" | ||
|
||
/** | ||
* Start a new worker which | ||
* checks if all conditions for performing a version check are met, | ||
* fetches the API endpoint [.NEWPIPE_API_URL] containing info | ||
* about the latest NewPipe version | ||
* and displays a notification about ana available update. | ||
* <br></br> | ||
* Following conditions need to be met, before data is request from the server: | ||
* | ||
* * The app is signed with the correct signing key (by TeamNewPipe / schabi). | ||
* If the signing key differs from the one used upstream, the update cannot be installed. | ||
* * The user enabled searching for and notifying about updates in the settings. | ||
* * The app did not recently check for updates. | ||
* We do not want to make unnecessary connections and DOS our servers. | ||
* | ||
*/ | ||
@JvmStatic | ||
fun enqueueNewVersionCheckingWork(context: Context) { | ||
val workRequest: WorkRequest = | ||
OneTimeWorkRequest.Builder(NewVersionWorker::class.java).build() | ||
WorkManager.getInstance(context).enqueue(workRequest) | ||
} | ||
} | ||
} |
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