changed how new versions are found

This commit is contained in:
polymorphicshade 2024-01-21 13:40:19 -07:00
parent d45a981452
commit 660ffe21dd
5 changed files with 96 additions and 59 deletions

View file

@ -22,7 +22,7 @@ import org.schabi.newpipe.extractor.downloader.Response
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException import org.schabi.newpipe.extractor.exceptions.ReCaptchaException
import org.schabi.newpipe.util.ReleaseVersionUtil.coerceUpdateCheckExpiry import org.schabi.newpipe.util.ReleaseVersionUtil.coerceUpdateCheckExpiry
import org.schabi.newpipe.util.ReleaseVersionUtil.isLastUpdateCheckExpired import org.schabi.newpipe.util.ReleaseVersionUtil.isLastUpdateCheckExpired
import org.schabi.newpipe.util.ReleaseVersionUtil.isReleaseApk import org.schabi.newpipe.util.Version
import java.io.IOException import java.io.IOException
class NewVersionWorker( class NewVersionWorker(
@ -30,20 +30,15 @@ class NewVersionWorker(
workerParams: WorkerParameters workerParams: WorkerParameters
) : Worker(context, workerParams) { ) : 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( private fun compareAppVersionAndShowNotification(
versionName: String, versionName: String,
apkLocationUrl: String?, apkLocationUrl: String?
versionCode: Int
) { ) {
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)) { if (inputData.getBoolean(IS_MANUAL, false)) {
// Show toast stating that the app is up-to-date if the update check was manual. // Show toast stating that the app is up-to-date if the update check was manual.
ContextCompat.getMainExecutor(applicationContext).execute { ContextCompat.getMainExecutor(applicationContext).execute {
@ -83,11 +78,6 @@ class NewVersionWorker(
@Throws(IOException::class, ReCaptchaException::class) @Throws(IOException::class, ReCaptchaException::class)
private fun checkNewVersion() { private fun checkNewVersion() {
// Check if the current apk is a github one or not.
if (!isReleaseApk()) {
return
}
if (!inputData.getBoolean(IS_MANUAL, false)) { if (!inputData.getBoolean(IS_MANUAL, false)) {
val prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext) val prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext)
// Check if the last request has happened a certain time ago // Check if the last request has happened a certain time ago
@ -120,19 +110,16 @@ class NewVersionWorker(
// Parse the json from the response. // Parse the json from the response.
try { try {
val newpipeVersionInfo = JsonParser.`object`() val jObj = JsonParser.`object`().from(response.responseBody())
.from(response.responseBody()).getObject("flavors") val versionName = jObj.getString("tag_name")
.getObject("newpipe") val apkLocationUrl = jObj
.getArray("assets")
val versionName = newpipeVersionInfo.getString("version") .getObject(0)
val versionCode = newpipeVersionInfo.getInt("version_code") .getString("browser_download_url")
val apkLocationUrl = newpipeVersionInfo.getString("apk") compareAppVersionAndShowNotification(versionName, apkLocationUrl)
compareAppVersionAndShowNotification(versionName, apkLocationUrl, versionCode)
} catch (e: JsonParserException) { } catch (e: JsonParserException) {
// Most likely something is wrong in data received from NEWPIPE_API_URL.
// Do not alarm user and fail silently.
if (DEBUG) { if (DEBUG) {
Log.w(TAG, "Could not get NewPipe API: invalid json", e) Log.w(TAG, "Invalid json", e)
} }
} }
} }
@ -153,22 +140,9 @@ class NewVersionWorker(
companion object { companion object {
private val DEBUG = MainActivity.DEBUG private val DEBUG = MainActivity.DEBUG
private val TAG = NewVersionWorker::class.java.simpleName 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" 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 @JvmStatic
fun enqueueNewVersionCheckingWork(context: Context, isManual: Boolean) { fun enqueueNewVersionCheckingWork(context: Context, isManual: Boolean) {
val workRequest = OneTimeWorkRequestBuilder<NewVersionWorker>() val workRequest = OneTimeWorkRequestBuilder<NewVersionWorker>()

View file

@ -9,7 +9,6 @@ import androidx.annotation.NonNull;
import org.schabi.newpipe.MainActivity; import org.schabi.newpipe.MainActivity;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
import org.schabi.newpipe.util.ReleaseVersionUtil;
public class MainSettingsFragment extends BasePreferenceFragment { public class MainSettingsFragment extends BasePreferenceFragment {
public static final boolean DEBUG = MainActivity.DEBUG; public static final boolean DEBUG = MainActivity.DEBUG;
@ -22,14 +21,6 @@ public class MainSettingsFragment extends BasePreferenceFragment {
setHasOptionsMenu(true); // Otherwise onCreateOptionsMenu is not called 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 // Hide debug preferences in RELEASE build variant
if (!DEBUG) { if (!DEBUG) {
getPreferenceScreen().removePreference( getPreferenceScreen().removePreference(

View file

@ -35,7 +35,6 @@ import org.schabi.newpipe.settings.preferencesearch.PreferenceSearchResultListen
import org.schabi.newpipe.settings.preferencesearch.PreferenceSearcher; import org.schabi.newpipe.settings.preferencesearch.PreferenceSearcher;
import org.schabi.newpipe.util.DeviceUtils; import org.schabi.newpipe.util.DeviceUtils;
import org.schabi.newpipe.util.KeyboardUtil; import org.schabi.newpipe.util.KeyboardUtil;
import org.schabi.newpipe.util.ReleaseVersionUtil;
import org.schabi.newpipe.util.ThemeHelper; import org.schabi.newpipe.util.ThemeHelper;
import org.schabi.newpipe.views.FocusOverlayView; import org.schabi.newpipe.views.FocusOverlayView;
@ -265,13 +264,6 @@ public class SettingsActivity extends AppCompatActivity implements
* be found when searching inside a release. * be found when searching inside a release.
*/ */
private void ensureSearchRepresentsApplicationState() { 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 // Hide debug preferences in RELEASE build variant
if (DEBUG) { if (DEBUG) {
SettingsResourceRegistry.getInstance() SettingsResourceRegistry.getInstance()

View file

@ -22,6 +22,7 @@ object ReleaseVersionUtil {
private const val RELEASE_CERT_PUBLIC_KEY_SHA1 = 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" "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 @JvmStatic
fun isReleaseApk(): Boolean { fun isReleaseApk(): Boolean {
return certificateSHA1Fingerprint == RELEASE_CERT_PUBLIC_KEY_SHA1 return certificateSHA1Fingerprint == RELEASE_CERT_PUBLIC_KEY_SHA1

View file

@ -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;
}
}