Skip to content

Commit c0f7b12

Browse files
authored
Merge pull request #7296 from vhouriet/vhouriet_feature_issue6049
Add "Check for updates" button in update settings
2 parents e9e2afa + 403154b commit c0f7b12

File tree

7 files changed

+80
-56
lines changed

7 files changed

+80
-56
lines changed

app/src/main/java/org/schabi/newpipe/CheckForNewAppVersion.java

+31-39
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@
77
import android.content.SharedPreferences;
88
import android.content.pm.PackageManager;
99
import android.content.pm.Signature;
10-
import android.net.ConnectivityManager;
1110
import android.net.Uri;
1211
import android.util.Log;
1312

1413
import androidx.annotation.NonNull;
1514
import androidx.annotation.Nullable;
1615
import androidx.core.app.NotificationCompat;
1716
import androidx.core.app.NotificationManagerCompat;
18-
import androidx.core.content.ContextCompat;
1917
import androidx.core.content.pm.PackageInfoCompat;
2018
import androidx.preference.PreferenceManager;
2119

@@ -48,7 +46,8 @@ public CheckForNewAppVersion() {
4846
private static final boolean DEBUG = MainActivity.DEBUG;
4947
private static final String TAG = CheckForNewAppVersion.class.getSimpleName();
5048

51-
private static final String GITHUB_APK_SHA1
49+
// Public key of the certificate that is used in NewPipe release versions
50+
private static final String RELEASE_CERT_PUBLIC_KEY_SHA1
5251
= "B0:2E:90:7C:1C:D6:FC:57:C3:35:F0:88:D0:8F:50:5F:94:E4:D2:15";
5352
private static final String NEWPIPE_API_URL = "https://newpipe.net/api/data.json";
5453

@@ -129,44 +128,37 @@ private static void compareAppVersionAndShowNotification(@NonNull final Applicat
129128
final String versionName,
130129
final String apkLocationUrl,
131130
final int versionCode) {
132-
final int notificationId = 2000;
133-
134-
if (BuildConfig.VERSION_CODE < versionCode) {
135-
// A pending intent to open the apk location url in the browser.
136-
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(apkLocationUrl));
137-
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
138-
final PendingIntent pendingIntent
139-
= PendingIntent.getActivity(application, 0, intent, 0);
131+
if (BuildConfig.VERSION_CODE >= versionCode) {
132+
return;
133+
}
140134

141-
final String channelId = application
142-
.getString(R.string.app_update_notification_channel_id);
143-
final NotificationCompat.Builder notificationBuilder
144-
= new NotificationCompat.Builder(application, channelId)
145-
.setSmallIcon(R.drawable.ic_newpipe_update)
146-
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
147-
.setContentIntent(pendingIntent)
148-
.setAutoCancel(true)
149-
.setContentTitle(application
150-
.getString(R.string.app_update_notification_content_title))
151-
.setContentText(application
152-
.getString(R.string.app_update_notification_content_text)
153-
+ " " + versionName);
135+
// A pending intent to open the apk location url in the browser.
136+
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(apkLocationUrl));
137+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
138+
final PendingIntent pendingIntent
139+
= PendingIntent.getActivity(application, 0, intent, 0);
154140

155-
final NotificationManagerCompat notificationManager
156-
= NotificationManagerCompat.from(application);
157-
notificationManager.notify(notificationId, notificationBuilder.build());
158-
}
159-
}
141+
final String channelId = application
142+
.getString(R.string.app_update_notification_channel_id);
143+
final NotificationCompat.Builder notificationBuilder
144+
= new NotificationCompat.Builder(application, channelId)
145+
.setSmallIcon(R.drawable.ic_newpipe_update)
146+
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
147+
.setContentIntent(pendingIntent)
148+
.setAutoCancel(true)
149+
.setContentTitle(application
150+
.getString(R.string.app_update_notification_content_title))
151+
.setContentText(application
152+
.getString(R.string.app_update_notification_content_text)
153+
+ " " + versionName);
160154

161-
private static boolean isConnected(@NonNull final App app) {
162-
final ConnectivityManager connectivityManager =
163-
ContextCompat.getSystemService(app, ConnectivityManager.class);
164-
return connectivityManager != null && connectivityManager.getActiveNetworkInfo() != null
165-
&& connectivityManager.getActiveNetworkInfo().isConnected();
155+
final NotificationManagerCompat notificationManager
156+
= NotificationManagerCompat.from(application);
157+
notificationManager.notify(2000, notificationBuilder.build());
166158
}
167159

168-
public static boolean isGithubApk(@NonNull final App app) {
169-
return getCertificateSHA1Fingerprint(app).equals(GITHUB_APK_SHA1);
160+
public static boolean isReleaseApk(@NonNull final App app) {
161+
return getCertificateSHA1Fingerprint(app).equals(RELEASE_CERT_PUBLIC_KEY_SHA1);
170162
}
171163

172164
private void checkNewVersion() throws IOException, ReCaptchaException {
@@ -175,9 +167,8 @@ private void checkNewVersion() throws IOException, ReCaptchaException {
175167
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(app);
176168
final NewVersionManager manager = new NewVersionManager();
177169

178-
// Check if user has enabled/disabled update checking
179-
// and if the current apk is a github one or not.
180-
if (!prefs.getBoolean(app.getString(R.string.update_app_key), true) || !isGithubApk(app)) {
170+
// Check if the current apk is a github one or not.
171+
if (!isReleaseApk(app)) {
181172
return;
182173
}
183174

@@ -213,6 +204,7 @@ private void handleResponse(@NonNull final Response response,
213204

214205
// Parse the json from the response.
215206
try {
207+
216208
final JsonObject githubStableObject = JsonParser.object()
217209
.from(response.responseBody()).getObject("flavors")
218210
.getObject("github").getObject("stable");

app/src/main/java/org/schabi/newpipe/MainActivity.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,16 @@ protected void onCreate(final Bundle savedInstanceState) {
169169
@Override
170170
protected void onPostCreate(final Bundle savedInstanceState) {
171171
super.onPostCreate(savedInstanceState);
172-
// Start the service which is checking all conditions
173-
// and eventually searching for a new version.
174-
// The service searching for a new NewPipe version must not be started in background.
175-
startNewVersionCheckService();
172+
173+
final App app = App.getApp();
174+
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(app);
175+
176+
if (prefs.getBoolean(app.getString(R.string.update_app_key), true)) {
177+
// Start the service which is checking all conditions
178+
// and eventually searching for a new version.
179+
// The service searching for a new NewPipe version must not be started in background.
180+
startNewVersionCheckService();
181+
}
176182
}
177183

178184
private void setupDrawer() throws ExtractionException {

app/src/main/java/org/schabi/newpipe/settings/MainSettingsFragment.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ public class MainSettingsFragment extends BasePreferenceFragment {
1616
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
1717
addPreferencesFromResource(R.xml.main_settings);
1818

19-
if (!CheckForNewAppVersion.isGithubApk(App.getApp())) {
20-
final Preference update = findPreference(getString(R.string.update_pref_screen_key));
19+
if (!CheckForNewAppVersion.isReleaseApk(App.getApp())) {
20+
final Preference update
21+
= findPreference(getString(R.string.update_pref_screen_key));
2122
getPreferenceScreen().removePreference(update);
2223

2324
defaultPreferences.edit().putBoolean(getString(R.string.update_app_key), false).apply();
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,48 @@
11
package org.schabi.newpipe.settings;
22

3+
import static org.schabi.newpipe.CheckForNewAppVersion.startNewVersionCheckService;
4+
35
import android.os.Bundle;
6+
import android.widget.Toast;
47

58
import androidx.preference.Preference;
69

710
import org.schabi.newpipe.R;
811

9-
import static org.schabi.newpipe.CheckForNewAppVersion.startNewVersionCheckService;
10-
1112
public class UpdateSettingsFragment extends BasePreferenceFragment {
1213
private final Preference.OnPreferenceChangeListener updatePreferenceChange
1314
= (preference, checkForUpdates) -> {
1415
defaultPreferences.edit()
1516
.putBoolean(getString(R.string.update_app_key), (boolean) checkForUpdates).apply();
1617

17-
if ((boolean) checkForUpdates) {
18-
// Search for updates immediately when update checks are enabled.
19-
// Reset the expire time. This is necessary to check for an update immediately.
20-
defaultPreferences.edit()
21-
.putLong(getString(R.string.update_expiry_key), 0).apply();
22-
startNewVersionCheckService();
23-
}
18+
if ((boolean) checkForUpdates) {
19+
checkNewVersionNow();
20+
}
21+
return true;
22+
};
23+
24+
private final Preference.OnPreferenceClickListener manualUpdateClick
25+
= preference -> {
26+
Toast.makeText(getContext(), R.string.checking_updates_toast, Toast.LENGTH_SHORT).show();
27+
checkNewVersionNow();
2428
return true;
2529
};
2630

31+
private void checkNewVersionNow() {
32+
// Search for updates immediately when update checks are enabled.
33+
// Reset the expire time. This is necessary to check for an update immediately.
34+
defaultPreferences.edit()
35+
.putLong(getString(R.string.update_expiry_key), 0).apply();
36+
startNewVersionCheckService();
37+
}
38+
2739
@Override
2840
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
2941
addPreferencesFromResource(R.xml.update_settings);
3042

31-
final String updateToggleKey = getString(R.string.update_app_key);
32-
findPreference(updateToggleKey).setOnPreferenceChangeListener(updatePreferenceChange);
43+
findPreference(getString(R.string.update_app_key))
44+
.setOnPreferenceChangeListener(updatePreferenceChange);
45+
findPreference(getString(R.string.manual_update_key))
46+
.setOnPreferenceClickListener(manualUpdateClick);
3347
}
3448
}

app/src/main/res/values/settings_keys.xml

+1
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@
383383

384384
<!-- Updates -->
385385
<string name="update_app_key" translatable="false">update_app_key</string>
386+
<string name="manual_update_key">manual_update_key</string>
386387
<string name="update_pref_screen_key" translatable="false">update_pref_screen_key</string>
387388
<string name="update_expiry_key" translatable="false">update_expiry_key</string>
388389

app/src/main/res/values/strings.xml

+3
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,8 @@
517517
<!-- Updates Settings -->
518518
<string name="updates_setting_title">Updates</string>
519519
<string name="updates_setting_description">Show a notification to prompt app update when a new version is available</string>
520+
<string name="manual_update_title">Check for updates</string>
521+
<string name="manual_update_description">Manually check for new versions</string>
520522
<!-- Minimize to exit action -->
521523
<string name="minimize_on_exit_title">Minimize on app switch</string>
522524
<string name="minimize_on_exit_summary">Action when switching to other app from main video player — %s</string>
@@ -547,6 +549,7 @@
547549
<string name="recovering">recovering</string>
548550
<string name="enqueue">Queue</string>
549551
<string name="permission_denied">Action denied by the system</string>
552+
<string name="checking_updates_toast">Checking for updates…</string>
550553
<!-- download notifications -->
551554
<string name="download_failed">Download failed</string>
552555
<plurals name="download_finished_notification">

app/src/main/res/xml/update_settings.xml

+7
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,11 @@
1212
app:singleLineTitle="false"
1313
app:iconSpaceReserved="false" />
1414

15+
<Preference
16+
android:key="@string/manual_update_key"
17+
android:summary="@string/manual_update_description"
18+
android:title="@string/manual_update_title"
19+
app:singleLineTitle="false"
20+
app:iconSpaceReserved="false" />
21+
1522
</PreferenceScreen>

0 commit comments

Comments
 (0)