|
10 | 10 | import android.net.ConnectivityManager; |
11 | 11 | import android.net.Uri; |
12 | 12 | import android.util.Log; |
13 | | - |
14 | 13 | import androidx.annotation.NonNull; |
15 | 14 | import androidx.annotation.Nullable; |
16 | 15 | import androidx.core.app.NotificationCompat; |
17 | 16 | import androidx.core.app.NotificationManagerCompat; |
18 | 17 | import androidx.core.content.ContextCompat; |
19 | 18 | import androidx.preference.PreferenceManager; |
20 | | - |
21 | 19 | import com.grack.nanojson.JsonObject; |
22 | 20 | import com.grack.nanojson.JsonParser; |
23 | 21 | import com.grack.nanojson.JsonParserException; |
24 | | - |
25 | | -import org.schabi.newpipe.report.ErrorActivity; |
26 | | -import org.schabi.newpipe.report.ErrorInfo; |
27 | | -import org.schabi.newpipe.report.UserAction; |
28 | | - |
| 22 | +import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers; |
| 23 | +import io.reactivex.rxjava3.core.Maybe; |
| 24 | +import io.reactivex.rxjava3.disposables.Disposable; |
| 25 | +import io.reactivex.rxjava3.schedulers.Schedulers; |
29 | 26 | import java.io.ByteArrayInputStream; |
30 | 27 | import java.io.InputStream; |
31 | 28 | import java.security.MessageDigest; |
|
34 | 31 | import java.security.cert.CertificateException; |
35 | 32 | import java.security.cert.CertificateFactory; |
36 | 33 | import java.security.cert.X509Certificate; |
37 | | - |
38 | | -import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers; |
39 | | -import io.reactivex.rxjava3.core.Maybe; |
40 | | -import io.reactivex.rxjava3.disposables.Disposable; |
41 | | -import io.reactivex.rxjava3.schedulers.Schedulers; |
| 34 | +import org.schabi.newpipe.report.ErrorActivity; |
| 35 | +import org.schabi.newpipe.report.ErrorInfo; |
| 36 | +import org.schabi.newpipe.report.UserAction; |
42 | 37 |
|
43 | 38 | public final class CheckForNewAppVersion { |
44 | 39 | private CheckForNewAppVersion() { } |
@@ -176,38 +171,60 @@ public static boolean isGithubApk(@NonNull final App app) { |
176 | 171 | @Nullable |
177 | 172 | public static Disposable checkNewVersion(@NonNull final App app) { |
178 | 173 | final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(app); |
| 174 | + final NewVersionManager manager = new NewVersionManager(); |
179 | 175 |
|
180 | 176 | // Check if user has enabled/disabled update checking |
181 | 177 | // and if the current apk is a github one or not. |
182 | 178 | if (!prefs.getBoolean(app.getString(R.string.update_app_key), true) || !isGithubApk(app)) { |
183 | 179 | return null; |
184 | 180 | } |
185 | 181 |
|
| 182 | + // Check if the last request has happened a certain time ago |
| 183 | + // to reduce the number of API requests. |
| 184 | + final long expiry = prefs.getLong(app.getString(R.string.update_expiry_key), 0); |
| 185 | + if (!manager.isExpired(expiry)) { |
| 186 | + return null; |
| 187 | + } |
| 188 | + |
186 | 189 | return Maybe |
187 | | - .fromCallable(() -> { |
188 | | - if (!isConnected(app)) { |
189 | | - return null; |
190 | | - } |
191 | | - |
192 | | - // Make a network request to get latest NewPipe data. |
193 | | - return DownloaderImpl.getInstance().get(NEWPIPE_API_URL).responseBody(); |
194 | | - }) |
| 190 | + .fromCallable(() -> { |
| 191 | + if (!isConnected(app)) { |
| 192 | + return null; |
| 193 | + } |
| 194 | + |
| 195 | + // Make a network request to get latest NewPipe data. |
| 196 | + return DownloaderImpl.getInstance().get(NEWPIPE_API_URL); |
| 197 | + }) |
195 | 198 | .subscribeOn(Schedulers.io()) |
196 | 199 | .observeOn(AndroidSchedulers.mainThread()) |
197 | 200 | .subscribe( |
198 | 201 | response -> { |
| 202 | + try { |
| 203 | + // Store a timestamp which needs to be exceeded, |
| 204 | + // before a new request to the API is made. |
| 205 | + final long newExpiry = manager |
| 206 | + .coerceExpiry(response.getHeader("expires")); |
| 207 | + prefs.edit() |
| 208 | + .putLong(app.getString(R.string.update_expiry_key), newExpiry) |
| 209 | + .apply(); |
| 210 | + } catch (final Exception e) { |
| 211 | + if (DEBUG) { |
| 212 | + Log.w(TAG, "Could not extract and save new expiry date", e); |
| 213 | + } |
| 214 | + } |
| 215 | + |
199 | 216 | // Parse the json from the response. |
200 | 217 | try { |
201 | 218 | final JsonObject githubStableObject = JsonParser.object() |
202 | | - .from(response).getObject("flavors").getObject("github") |
203 | | - .getObject("stable"); |
| 219 | + .from(response.responseBody()).getObject("flavors") |
| 220 | + .getObject("github").getObject("stable"); |
204 | 221 |
|
205 | 222 | final String versionName = githubStableObject |
206 | | - .getString("version"); |
| 223 | + .getString("version"); |
207 | 224 | final int versionCode = githubStableObject |
208 | | - .getInt("version_code"); |
| 225 | + .getInt("version_code"); |
209 | 226 | final String apkLocationUrl = githubStableObject |
210 | | - .getString("apk"); |
| 227 | + .getString("apk"); |
211 | 228 |
|
212 | 229 | compareAppVersionAndShowNotification(app, versionName, |
213 | 230 | apkLocationUrl, versionCode); |
|
0 commit comments