|
| 1 | +package org.schabi.newpipe |
| 2 | + |
| 3 | +import android.app.PendingIntent |
| 4 | +import android.content.Context |
| 5 | +import android.content.Intent |
| 6 | +import android.util.Log |
| 7 | +import androidx.core.app.NotificationCompat |
| 8 | +import androidx.core.app.NotificationManagerCompat |
| 9 | +import androidx.core.content.edit |
| 10 | +import androidx.core.net.toUri |
| 11 | +import androidx.preference.PreferenceManager |
| 12 | +import androidx.work.OneTimeWorkRequest |
| 13 | +import androidx.work.WorkManager |
| 14 | +import androidx.work.WorkRequest |
| 15 | +import androidx.work.Worker |
| 16 | +import androidx.work.WorkerParameters |
| 17 | +import com.grack.nanojson.JsonParser |
| 18 | +import com.grack.nanojson.JsonParserException |
| 19 | +import org.schabi.newpipe.extractor.downloader.Response |
| 20 | +import org.schabi.newpipe.extractor.exceptions.ReCaptchaException |
| 21 | +import org.schabi.newpipe.util.ReleaseVersionUtil.coerceUpdateCheckExpiry |
| 22 | +import org.schabi.newpipe.util.ReleaseVersionUtil.isLastUpdateCheckExpired |
| 23 | +import org.schabi.newpipe.util.ReleaseVersionUtil.isReleaseApk |
| 24 | +import java.io.IOException |
| 25 | + |
| 26 | +class NewVersionWorker( |
| 27 | + context: Context, |
| 28 | + workerParams: WorkerParameters |
| 29 | +) : Worker(context, workerParams) { |
| 30 | + |
| 31 | + /** |
| 32 | + * Method to compare the current and latest available app version. |
| 33 | + * If a newer version is available, we show the update notification. |
| 34 | + * |
| 35 | + * @param versionName Name of new version |
| 36 | + * @param apkLocationUrl Url with the new apk |
| 37 | + * @param versionCode Code of new version |
| 38 | + */ |
| 39 | + private fun compareAppVersionAndShowNotification( |
| 40 | + versionName: String, |
| 41 | + apkLocationUrl: String?, |
| 42 | + versionCode: Int |
| 43 | + ) { |
| 44 | + if (BuildConfig.VERSION_CODE >= versionCode) { |
| 45 | + return |
| 46 | + } |
| 47 | + val app = App.getApp() |
| 48 | + |
| 49 | + // A pending intent to open the apk location url in the browser. |
| 50 | + val intent = Intent(Intent.ACTION_VIEW, apkLocationUrl?.toUri()) |
| 51 | + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
| 52 | + val pendingIntent = PendingIntent.getActivity(app, 0, intent, 0) |
| 53 | + val channelId = app.getString(R.string.app_update_notification_channel_id) |
| 54 | + val notificationBuilder = NotificationCompat.Builder(app, channelId) |
| 55 | + .setSmallIcon(R.drawable.ic_newpipe_update) |
| 56 | + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) |
| 57 | + .setContentIntent(pendingIntent) |
| 58 | + .setAutoCancel(true) |
| 59 | + .setContentTitle(app.getString(R.string.app_update_notification_content_title)) |
| 60 | + .setContentText( |
| 61 | + app.getString(R.string.app_update_notification_content_text) + |
| 62 | + " " + versionName |
| 63 | + ) |
| 64 | + val notificationManager = NotificationManagerCompat.from(app) |
| 65 | + notificationManager.notify(2000, notificationBuilder.build()) |
| 66 | + } |
| 67 | + |
| 68 | + @Throws(IOException::class, ReCaptchaException::class) |
| 69 | + private fun checkNewVersion() { |
| 70 | + // Check if the current apk is a github one or not. |
| 71 | + if (!isReleaseApk()) { |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + val prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext) |
| 76 | + // Check if the last request has happened a certain time ago |
| 77 | + // to reduce the number of API requests. |
| 78 | + val expiry = prefs.getLong(applicationContext.getString(R.string.update_expiry_key), 0) |
| 79 | + if (!isLastUpdateCheckExpired(expiry)) { |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + // Make a network request to get latest NewPipe data. |
| 84 | + val response = DownloaderImpl.getInstance().get(NEWPIPE_API_URL) |
| 85 | + handleResponse(response) |
| 86 | + } |
| 87 | + |
| 88 | + private fun handleResponse(response: Response) { |
| 89 | + val prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext) |
| 90 | + try { |
| 91 | + // Store a timestamp which needs to be exceeded, |
| 92 | + // before a new request to the API is made. |
| 93 | + val newExpiry = coerceUpdateCheckExpiry(response.getHeader("expires")) |
| 94 | + prefs.edit { |
| 95 | + putLong(applicationContext.getString(R.string.update_expiry_key), newExpiry) |
| 96 | + } |
| 97 | + } catch (e: Exception) { |
| 98 | + if (DEBUG) { |
| 99 | + Log.w(TAG, "Could not extract and save new expiry date", e) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // Parse the json from the response. |
| 104 | + try { |
| 105 | + val githubStableObject = JsonParser.`object`() |
| 106 | + .from(response.responseBody()).getObject("flavors") |
| 107 | + .getObject("github").getObject("stable") |
| 108 | + |
| 109 | + val versionName = githubStableObject.getString("version") |
| 110 | + val versionCode = githubStableObject.getInt("version_code") |
| 111 | + val apkLocationUrl = githubStableObject.getString("apk") |
| 112 | + compareAppVersionAndShowNotification(versionName, apkLocationUrl, versionCode) |
| 113 | + } catch (e: JsonParserException) { |
| 114 | + // Most likely something is wrong in data received from NEWPIPE_API_URL. |
| 115 | + // Do not alarm user and fail silently. |
| 116 | + if (DEBUG) { |
| 117 | + Log.w(TAG, "Could not get NewPipe API: invalid json", e) |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + override fun doWork(): Result { |
| 123 | + try { |
| 124 | + checkNewVersion() |
| 125 | + } catch (e: IOException) { |
| 126 | + Log.w(TAG, "Could not fetch NewPipe API: probably network problem", e) |
| 127 | + return Result.failure() |
| 128 | + } catch (e: ReCaptchaException) { |
| 129 | + Log.e(TAG, "ReCaptchaException should never happen here.", e) |
| 130 | + return Result.failure() |
| 131 | + } |
| 132 | + return Result.success() |
| 133 | + } |
| 134 | + |
| 135 | + companion object { |
| 136 | + private val DEBUG = MainActivity.DEBUG |
| 137 | + private val TAG = NewVersionWorker::class.java.simpleName |
| 138 | + private const val NEWPIPE_API_URL = "https://newpipe.net/api/data.json" |
| 139 | + |
| 140 | + /** |
| 141 | + * Start a new worker which |
| 142 | + * checks if all conditions for performing a version check are met, |
| 143 | + * fetches the API endpoint [.NEWPIPE_API_URL] containing info |
| 144 | + * about the latest NewPipe version |
| 145 | + * and displays a notification about ana available update. |
| 146 | + * <br></br> |
| 147 | + * Following conditions need to be met, before data is request from the server: |
| 148 | + * |
| 149 | + * * The app is signed with the correct signing key (by TeamNewPipe / schabi). |
| 150 | + * If the signing key differs from the one used upstream, the update cannot be installed. |
| 151 | + * * The user enabled searching for and notifying about updates in the settings. |
| 152 | + * * The app did not recently check for updates. |
| 153 | + * We do not want to make unnecessary connections and DOS our servers. |
| 154 | + * |
| 155 | + */ |
| 156 | + @JvmStatic |
| 157 | + fun enqueueNewVersionCheckingWork(context: Context) { |
| 158 | + val workRequest: WorkRequest = |
| 159 | + OneTimeWorkRequest.Builder(NewVersionWorker::class.java).build() |
| 160 | + WorkManager.getInstance(context).enqueue(workRequest) |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments