Skip to content

Commit

Permalink
changed how new versions are found
Browse files Browse the repository at this point in the history
  • Loading branch information
polymorphicshade committed Jan 21, 2024
1 parent d45a981 commit 660ffe2
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 59 deletions.
58 changes: 16 additions & 42 deletions app/src/main/java/org/schabi/newpipe/NewVersionWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,23 @@ 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.isLastUpdateCheckExpired
import org.schabi.newpipe.util.ReleaseVersionUtil.isReleaseApk
import org.schabi.newpipe.util.Version
import java.io.IOException

class NewVersionWorker(
context: Context,
workerParams: WorkerParameters
) : Worker(context, workerParams) {

/**
* Method to compare the current and latest available app version.
* If a newer version is available, we show the update notification.
*
* @param versionName Name of new version
* @param apkLocationUrl Url with the new apk
* @param versionCode Code of new version
*/
private fun compareAppVersionAndShowNotification(
versionName: String,
apkLocationUrl: String?,
versionCode: Int
apkLocationUrl: String?
) {
if (BuildConfig.VERSION_CODE >= versionCode) {
val ourVersion = Version.fromString(BuildConfig.VERSION_NAME)
val theirVersion = Version.fromString(versionName)

// abort if source version is the same or newer than target version
if (ourVersion >= theirVersion) {
if (inputData.getBoolean(IS_MANUAL, false)) {
// Show toast stating that the app is up-to-date if the update check was manual.
ContextCompat.getMainExecutor(applicationContext).execute {
Expand Down Expand Up @@ -83,11 +78,6 @@ class NewVersionWorker(

@Throws(IOException::class, ReCaptchaException::class)
private fun checkNewVersion() {
// Check if the current apk is a github one or not.
if (!isReleaseApk()) {
return
}

if (!inputData.getBoolean(IS_MANUAL, false)) {
val prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext)
// Check if the last request has happened a certain time ago
Expand Down Expand Up @@ -120,19 +110,16 @@ class NewVersionWorker(

// Parse the json from the response.
try {
val newpipeVersionInfo = JsonParser.`object`()
.from(response.responseBody()).getObject("flavors")
.getObject("newpipe")

val versionName = newpipeVersionInfo.getString("version")
val versionCode = newpipeVersionInfo.getInt("version_code")
val apkLocationUrl = newpipeVersionInfo.getString("apk")
compareAppVersionAndShowNotification(versionName, apkLocationUrl, versionCode)
val jObj = JsonParser.`object`().from(response.responseBody())
val versionName = jObj.getString("tag_name")
val apkLocationUrl = jObj
.getArray("assets")
.getObject(0)
.getString("browser_download_url")
compareAppVersionAndShowNotification(versionName, apkLocationUrl)
} 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)
Log.w(TAG, "Invalid json", e)
}
}
}
Expand All @@ -153,22 +140,9 @@ class NewVersionWorker(
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"
private const val NEWPIPE_API_URL = "https://api.github.com/repos/polymorphicshade/Tubular/releases/latest"
private const val IS_MANUAL = "isManual"

/**
* 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 an available update if one is available.
* <br></br>
* Following conditions need to be met, before data is requested 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, isManual: Boolean) {
val workRequest = OneTimeWorkRequestBuilder<NewVersionWorker>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import org.schabi.newpipe.MainActivity;
import org.schabi.newpipe.R;
import org.schabi.newpipe.util.ReleaseVersionUtil;

public class MainSettingsFragment extends BasePreferenceFragment {
public static final boolean DEBUG = MainActivity.DEBUG;
Expand All @@ -22,14 +21,6 @@ public void onCreatePreferences(final Bundle savedInstanceState, final String ro

setHasOptionsMenu(true); // Otherwise onCreateOptionsMenu is not called

// Check if the app is updatable
if (!ReleaseVersionUtil.isReleaseApk()) {
getPreferenceScreen().removePreference(
findPreference(getString(R.string.update_pref_screen_key)));

defaultPreferences.edit().putBoolean(getString(R.string.update_app_key), false).apply();
}

// Hide debug preferences in RELEASE build variant
if (!DEBUG) {
getPreferenceScreen().removePreference(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.schabi.newpipe.settings.preferencesearch.PreferenceSearcher;
import org.schabi.newpipe.util.DeviceUtils;
import org.schabi.newpipe.util.KeyboardUtil;
import org.schabi.newpipe.util.ReleaseVersionUtil;
import org.schabi.newpipe.util.ThemeHelper;
import org.schabi.newpipe.views.FocusOverlayView;

Expand Down Expand Up @@ -265,13 +264,6 @@ private void initSearch(
* be found when searching inside a release.
*/
private void ensureSearchRepresentsApplicationState() {
// Check if the update settings are available
if (!ReleaseVersionUtil.isReleaseApk()) {
SettingsResourceRegistry.getInstance()
.getEntryByPreferencesResId(R.xml.update_settings)
.setSearchable(false);
}

// Hide debug preferences in RELEASE build variant
if (DEBUG) {
SettingsResourceRegistry.getInstance()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ object ReleaseVersionUtil {
private const val RELEASE_CERT_PUBLIC_KEY_SHA1 =
"B0:2E:90:7C:1C:D6:FC:57:C3:35:F0:88:D0:8F:50:5F:94:E4:D2:15"

// TODO: no longer using this since this checks Github now... do we still need this?
@JvmStatic
fun isReleaseApk(): Boolean {
return certificateSHA1Fingerprint == RELEASE_CERT_PUBLIC_KEY_SHA1
Expand Down
79 changes: 79 additions & 0 deletions app/src/main/java/org/schabi/newpipe/util/Version.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.schabi.newpipe.util;

import android.util.Log;

public final class Version implements Comparable<Version> {
private static final String TAG = Version.class.getSimpleName();
private final int major;
private final int minor;
private final int build;
private final int rev;

public Version(final int major, final int minor, final int build, final int rev) {
this.major = major;
this.minor = minor;
this.build = build;
this.rev = rev;
}

public static Version fromString(final String str) {
// examples of valid version strings:
// - 0.1
// - v0.1.0.4
// - 0.20.6
// - v0.20.6_r2
try {
// example: v0.20.6_r2 -> v0.20.6.r2 -> 0.20.6.2
final String[] split = str
.replaceAll("_", ".")
.replaceAll("[^0-9.]", "")
.split("[^\\d]");

final int major = Integer.parseInt(split[0]);
final int minor = split.length > 1
? Integer.parseInt(split[1])
: 0;
final int build = split.length > 2
? Integer.parseInt(split[2])
: 0;
final int rev = split.length > 3
? Integer.parseInt(split[3])
: 0;

return new Version(major, minor, build, rev);
} catch (final Exception e) {
Log.e(TAG, "Could not successfully parse version string.", e);
return new Version(0, 0, 0, 0);
}
}

public int getMajor() {
return major;
}

public int getMinor() {
return minor;
}

public int getBuild() {
return build;
}

public int getRev() {
return rev;
}

@Override
public int compareTo(final Version that) {
if (this.getMajor() != that.getMajor()) {
return this.getMajor() < that.getMajor() ? -1 : 1;
} else if (this.getMinor() != that.getMinor()) {
return this.getMinor() < that.getMinor() ? -1 : 1;
} else if (this.getBuild() != that.getBuild()) {
return this.getBuild() < that.getBuild() ? -1 : 1;
} else if (this.getRev() != that.getRev()) {
return this.getRev() < that.getRev() ? -1 : 1;
}
return 0;
}
}

0 comments on commit 660ffe2

Please sign in to comment.