1
0
mirror of https://github.com/TeamNewPipe/NewPipe.git synced 2024-11-22 02:53:09 +01:00

Convert isReleaseApk to lazy value

This commit is contained in:
Isira Seneviratne 2024-02-06 05:11:13 +05:30 committed by Tobi
parent bec18e13d3
commit 2e53a99361
4 changed files with 8 additions and 11 deletions

View File

@ -20,9 +20,7 @@ import com.grack.nanojson.JsonParser
import com.grack.nanojson.JsonParserException import com.grack.nanojson.JsonParserException
import org.schabi.newpipe.extractor.downloader.Response 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
import org.schabi.newpipe.util.ReleaseVersionUtil.isLastUpdateCheckExpired
import org.schabi.newpipe.util.ReleaseVersionUtil.isReleaseApk
import java.io.IOException import java.io.IOException
class NewVersionWorker( class NewVersionWorker(
@ -84,7 +82,7 @@ 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. // Check if the current apk is a github one or not.
if (!isReleaseApk()) { if (!ReleaseVersionUtil.isReleaseApk) {
return return
} }
@ -93,7 +91,7 @@ class NewVersionWorker(
// 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.
val expiry = prefs.getLong(applicationContext.getString(R.string.update_expiry_key), 0) val expiry = prefs.getLong(applicationContext.getString(R.string.update_expiry_key), 0)
if (!isLastUpdateCheckExpired(expiry)) { if (!ReleaseVersionUtil.isLastUpdateCheckExpired(expiry)) {
return return
} }
} }
@ -108,7 +106,7 @@ class NewVersionWorker(
try { try {
// Store a timestamp which needs to be exceeded, // Store a timestamp which needs to be exceeded,
// before a new request to the API is made. // before a new request to the API is made.
val newExpiry = coerceUpdateCheckExpiry(response.getHeader("expires")) val newExpiry = ReleaseVersionUtil.coerceUpdateCheckExpiry(response.getHeader("expires"))
prefs.edit { prefs.edit {
putLong(applicationContext.getString(R.string.update_expiry_key), newExpiry) putLong(applicationContext.getString(R.string.update_expiry_key), newExpiry)
} }

View File

@ -23,7 +23,7 @@ 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 // Check if the app is updatable
if (!ReleaseVersionUtil.isReleaseApk()) { if (!ReleaseVersionUtil.INSTANCE.isReleaseApk()) {
getPreferenceScreen().removePreference( getPreferenceScreen().removePreference(
findPreference(getString(R.string.update_pref_screen_key))); findPreference(getString(R.string.update_pref_screen_key)));

View File

@ -266,7 +266,7 @@ public class SettingsActivity extends AppCompatActivity implements
*/ */
private void ensureSearchRepresentsApplicationState() { private void ensureSearchRepresentsApplicationState() {
// Check if the update settings are available // Check if the update settings are available
if (!ReleaseVersionUtil.isReleaseApk()) { if (!ReleaseVersionUtil.INSTANCE.isReleaseApk()) {
SettingsResourceRegistry.getInstance() SettingsResourceRegistry.getInstance()
.getEntryByPreferencesResId(R.xml.update_settings) .getEntryByPreferencesResId(R.xml.update_settings)
.setSearchable(false); .setSearchable(false);

View File

@ -15,14 +15,13 @@ object ReleaseVersionUtil {
private const val RELEASE_CERT_PUBLIC_KEY_SHA256 = private const val RELEASE_CERT_PUBLIC_KEY_SHA256 =
"cb84069bd68116bafae5ee4ee5b08a567aa6d898404e7cb12f9e756df5cf5cab" "cb84069bd68116bafae5ee4ee5b08a567aa6d898404e7cb12f9e756df5cf5cab"
@JvmStatic val isReleaseApk by lazy {
fun isReleaseApk(): Boolean {
@Suppress("NewApi") @Suppress("NewApi")
val certificates = mapOf( val certificates = mapOf(
RELEASE_CERT_PUBLIC_KEY_SHA256.toByteArray() to PackageManager.CERT_INPUT_SHA256 RELEASE_CERT_PUBLIC_KEY_SHA256.toByteArray() to PackageManager.CERT_INPUT_SHA256
) )
val app = App.getApp() val app = App.getApp()
return try { try {
PackageInfoCompat.hasSignatures(app.packageManager, app.packageName, certificates, false) PackageInfoCompat.hasSignatures(app.packageManager, app.packageName, certificates, false)
} catch (e: PackageManager.NameNotFoundException) { } catch (e: PackageManager.NameNotFoundException) {
createNotification( createNotification(