restoring fork-specific update functionality

This commit is contained in:
polymorphicshade 2022-04-30 12:41:52 -06:00
parent 29feaeba59
commit 50ba525c5b
2 changed files with 20 additions and 32 deletions

View file

@ -20,7 +20,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(
@ -34,19 +34,21 @@ class NewVersionWorker(
* *
* @param versionName Name of new version * @param versionName Name of new version
* @param apkLocationUrl Url with the new apk * @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 sourceVersion = Version.fromString(BuildConfig.VERSION_NAME)
val targetVersion = Version.fromString(versionName)
// abort if source version is the same or newer than target version
if (sourceVersion >= targetVersion) {
return return
} }
val app = App.getApp() val app = App.getApp()
// A pending intent to open the apk location url in the browser.
val intent = Intent(Intent.ACTION_VIEW, apkLocationUrl?.toUri()) val intent = Intent(Intent.ACTION_VIEW, apkLocationUrl?.toUri())
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
val pendingIntent = PendingIntent.getActivity(app, 0, intent, 0) val pendingIntent = PendingIntent.getActivity(app, 0, intent, 0)
@ -67,11 +69,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
}
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
// to reduce the number of API requests. // to reduce the number of API requests.
@ -81,7 +78,7 @@ class NewVersionWorker(
} }
// Make a network request to get latest NewPipe data. // Make a network request to get latest NewPipe data.
val response = DownloaderImpl.getInstance().get(NEWPIPE_API_URL) val response = DownloaderImpl.getInstance().get(API_URL)
handleResponse(response) handleResponse(response)
} }
@ -102,19 +99,18 @@ class NewVersionWorker(
// Parse the json from the response. // Parse the json from the response.
try { try {
val githubStableObject = JsonParser.`object`() val jObj = JsonParser.`object`().from(response.responseBody())
.from(response.responseBody()).getObject("flavors") val versionName = jObj.getString("tag_name")
.getObject("github").getObject("stable") val apkLocationUrl = jObj
.getArray("assets")
val versionName = githubStableObject.getString("version") .getObject(0)
val versionCode = githubStableObject.getInt("version_code") .getString("browser_download_url")
val apkLocationUrl = githubStableObject.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. // Most likely something is wrong in data received from API_URL.
// Do not alarm user and fail silently. // 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, "Could not get Github API: invalid json", e)
} }
} }
} }
@ -135,7 +131,8 @@ 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 API_URL =
"https://api.github.com/repos/polymorphicshade/NewPipe/releases/latest"
/** /**
* Start a new worker which * Start a new worker which

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(