From 2e53a99361cdc9f380ccd7ace8dd21ed8aa321f4 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Tue, 6 Feb 2024 05:11:13 +0530 Subject: [PATCH] Convert isReleaseApk to lazy value --- .../main/java/org/schabi/newpipe/NewVersionWorker.kt | 10 ++++------ .../schabi/newpipe/settings/MainSettingsFragment.java | 2 +- .../org/schabi/newpipe/settings/SettingsActivity.java | 2 +- .../java/org/schabi/newpipe/util/ReleaseVersionUtil.kt | 5 ++--- 4 files changed, 8 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/NewVersionWorker.kt b/app/src/main/java/org/schabi/newpipe/NewVersionWorker.kt index 39d8e90dc..000b83953 100644 --- a/app/src/main/java/org/schabi/newpipe/NewVersionWorker.kt +++ b/app/src/main/java/org/schabi/newpipe/NewVersionWorker.kt @@ -20,9 +20,7 @@ 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.isLastUpdateCheckExpired -import org.schabi.newpipe.util.ReleaseVersionUtil.isReleaseApk +import org.schabi.newpipe.util.ReleaseVersionUtil import java.io.IOException class NewVersionWorker( @@ -84,7 +82,7 @@ class NewVersionWorker( @Throws(IOException::class, ReCaptchaException::class) private fun checkNewVersion() { // Check if the current apk is a github one or not. - if (!isReleaseApk()) { + if (!ReleaseVersionUtil.isReleaseApk) { return } @@ -93,7 +91,7 @@ class NewVersionWorker( // 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)) { + if (!ReleaseVersionUtil.isLastUpdateCheckExpired(expiry)) { return } } @@ -108,7 +106,7 @@ class NewVersionWorker( try { // Store a timestamp which needs to be exceeded, // before a new request to the API is made. - val newExpiry = coerceUpdateCheckExpiry(response.getHeader("expires")) + val newExpiry = ReleaseVersionUtil.coerceUpdateCheckExpiry(response.getHeader("expires")) prefs.edit { putLong(applicationContext.getString(R.string.update_expiry_key), newExpiry) } diff --git a/app/src/main/java/org/schabi/newpipe/settings/MainSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/MainSettingsFragment.java index 3776d78f6..32e33d55b 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/MainSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/MainSettingsFragment.java @@ -23,7 +23,7 @@ public class MainSettingsFragment extends BasePreferenceFragment { setHasOptionsMenu(true); // Otherwise onCreateOptionsMenu is not called // Check if the app is updatable - if (!ReleaseVersionUtil.isReleaseApk()) { + if (!ReleaseVersionUtil.INSTANCE.isReleaseApk()) { getPreferenceScreen().removePreference( findPreference(getString(R.string.update_pref_screen_key))); diff --git a/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java b/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java index 3ee6668bf..529e53442 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java +++ b/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java @@ -266,7 +266,7 @@ public class SettingsActivity extends AppCompatActivity implements */ private void ensureSearchRepresentsApplicationState() { // Check if the update settings are available - if (!ReleaseVersionUtil.isReleaseApk()) { + if (!ReleaseVersionUtil.INSTANCE.isReleaseApk()) { SettingsResourceRegistry.getInstance() .getEntryByPreferencesResId(R.xml.update_settings) .setSearchable(false); diff --git a/app/src/main/java/org/schabi/newpipe/util/ReleaseVersionUtil.kt b/app/src/main/java/org/schabi/newpipe/util/ReleaseVersionUtil.kt index fc31a4d94..fc1a7d8cc 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ReleaseVersionUtil.kt +++ b/app/src/main/java/org/schabi/newpipe/util/ReleaseVersionUtil.kt @@ -15,14 +15,13 @@ object ReleaseVersionUtil { private const val RELEASE_CERT_PUBLIC_KEY_SHA256 = "cb84069bd68116bafae5ee4ee5b08a567aa6d898404e7cb12f9e756df5cf5cab" - @JvmStatic - fun isReleaseApk(): Boolean { + val isReleaseApk by lazy { @Suppress("NewApi") val certificates = mapOf( RELEASE_CERT_PUBLIC_KEY_SHA256.toByteArray() to PackageManager.CERT_INPUT_SHA256 ) val app = App.getApp() - return try { + try { PackageInfoCompat.hasSignatures(app.packageManager, app.packageName, certificates, false) } catch (e: PackageManager.NameNotFoundException) { createNotification(