Skip to content

Commit ccca89d

Browse files
Merge pull request #12585 from Isira-Seneviratne/Merge-dev-to-refactor
Merge dev to refactor
2 parents 10c5a5d + 9bf23ab commit ccca89d

File tree

9 files changed

+106
-29
lines changed

9 files changed

+106
-29
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@
435435
</activity>
436436
<service
437437
android:name=".RouterActivity$FetcherService"
438+
android:foregroundServiceType="dataSync"
438439
android:exported="false" />
439440

440441
<!-- opting out of sending metrics to Google in Android System WebView -->

app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationWorker.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.schabi.newpipe.local.feed.notifications
22

33
import android.content.Context
4+
import android.content.pm.ServiceInfo
5+
import android.os.Build
46
import android.util.Log
57
import androidx.core.app.NotificationCompat
68
import androidx.work.Constraints
@@ -83,7 +85,9 @@ class NotificationWorker(
8385
.setPriority(NotificationCompat.PRIORITY_LOW)
8486
.setContentTitle(applicationContext.getString(R.string.feed_notification_loading))
8587
.build()
86-
setForegroundAsync(ForegroundInfo(FeedLoadService.NOTIFICATION_ID, notification))
88+
// ServiceInfo constants are not used below Android Q, so 0 is set here
89+
val serviceType = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC else 0
90+
setForegroundAsync(ForegroundInfo(FeedLoadService.NOTIFICATION_ID, notification, serviceType))
8791
}
8892

8993
companion object {

app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,17 @@ public boolean shouldUpdateBufferingSlot() {
167167
&& notificationBuilder.mActions.get(2).actionIntent != null);
168168
}
169169

170-
171170
public void createNotificationAndStartForeground() {
172171
if (notificationBuilder == null) {
173172
notificationBuilder = createNotification();
174173
}
175174
updateNotification();
176175

177-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
178-
player.getService().startForeground(NOTIFICATION_ID, notificationBuilder.build(),
179-
ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK);
180-
} else {
181-
player.getService().startForeground(NOTIFICATION_ID, notificationBuilder.build());
182-
}
176+
// ServiceInfo constants are not used below Android Q, so 0 is set here
177+
final int serviceType = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q
178+
? ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK : 0;
179+
ServiceCompat.startForeground(player.getService(), NOTIFICATION_ID,
180+
notificationBuilder.build(), serviceType);
183181
}
184182

185183
public void cancelNotificationAndStopForeground() {

app/src/main/java/org/schabi/newpipe/util/PermissionHelper.java

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
import android.net.Uri;
1010
import android.os.Build;
1111
import android.provider.Settings;
12+
import android.text.Html;
1213
import android.widget.Toast;
1314

1415
import androidx.annotation.RequiresApi;
16+
import androidx.appcompat.app.AlertDialog;
1517
import androidx.core.app.ActivityCompat;
1618
import androidx.core.content.ContextCompat;
1719

@@ -113,14 +115,47 @@ public static boolean checkPostNotificationsPermission(final Activity activity,
113115
@RequiresApi(api = Build.VERSION_CODES.M)
114116
public static boolean checkSystemAlertWindowPermission(final Context context) {
115117
if (!Settings.canDrawOverlays(context)) {
116-
final Intent i = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
117-
Uri.parse("package:" + context.getPackageName()));
118-
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
119-
try {
120-
context.startActivity(i);
121-
} catch (final ActivityNotFoundException ignored) {
118+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
119+
final Intent i = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
120+
Uri.parse("package:" + context.getPackageName()));
121+
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
122+
try {
123+
context.startActivity(i);
124+
} catch (final ActivityNotFoundException ignored) {
125+
}
126+
return false;
127+
// from Android R the ACTION_MANAGE_OVERLAY_PERMISSION will only point to the menu,
128+
// so let’s add a dialog that points the user to the right setting.
129+
} else {
130+
final String appName = context.getApplicationInfo()
131+
.loadLabel(context.getPackageManager()).toString();
132+
final String title = context.getString(R.string.permission_display_over_apps);
133+
final String permissionName =
134+
context.getString(R.string.permission_display_over_apps_permission_name);
135+
final String appNameItalic = "<i>" + appName + "</i>";
136+
final String permissionNameItalic = "<i>" + permissionName + "</i>";
137+
final String message =
138+
context.getString(R.string.permission_display_over_apps_message,
139+
appNameItalic,
140+
permissionNameItalic
141+
);
142+
new AlertDialog.Builder(context)
143+
.setTitle(title)
144+
.setMessage(Html.fromHtml(message, Html.FROM_HTML_MODE_COMPACT))
145+
.setPositiveButton("OK", (dialog, which) -> {
146+
// we don’t need the package name here, since it won’t do anything on >R
147+
final Intent intent =
148+
new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
149+
try {
150+
context.startActivity(intent);
151+
} catch (final ActivityNotFoundException ignored) {
152+
}
153+
})
154+
.setCancelable(true)
155+
.show();
156+
return false;
122157
}
123-
return false;
158+
124159
} else {
125160
return true;
126161
}

app/src/main/java/us/shandian/giga/service/DownloadManager.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public void pauseMission(DownloadMission mission) {
265265
}
266266
}
267267

268-
public void deleteMission(Mission mission) {
268+
public void deleteMission(Mission mission, boolean alsoDeleteFile) {
269269
synchronized (this) {
270270
if (mission instanceof DownloadMission) {
271271
mMissionsPending.remove(mission);
@@ -274,7 +274,9 @@ public void deleteMission(Mission mission) {
274274
mFinishedMissionStore.deleteMission(mission);
275275
}
276276

277-
mission.delete();
277+
if (alsoDeleteFile) {
278+
mission.delete();
279+
}
278280
}
279281
}
280282

app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ private void deleteFinishedDownloads() {
614614
while (i.hasNext()) {
615615
Mission mission = i.next();
616616
if (mission != null) {
617-
mDownloadManager.deleteMission(mission);
617+
mDownloadManager.deleteMission(mission, true);
618618
mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mission.storage.getUri()));
619619
}
620620
i.remove();
@@ -667,7 +667,14 @@ private boolean handlePopupItem(@NonNull ViewHolderItem h, @NonNull MenuItem opt
667667
shareFile(h.item.mission);
668668
return true;
669669
case R.id.delete:
670-
mDeleter.append(h.item.mission);
670+
// delete the entry and the file
671+
mDeleter.append(h.item.mission, true);
672+
applyChanges();
673+
checkMasterButtonsVisibility();
674+
return true;
675+
case R.id.delete_entry:
676+
// just delete the entry
677+
mDeleter.append(h.item.mission, false);
671678
applyChanges();
672679
checkMasterButtonsVisibility();
673680
return true;
@@ -676,7 +683,7 @@ private boolean handlePopupItem(@NonNull ViewHolderItem h, @NonNull MenuItem opt
676683
final StoredFileHelper storage = h.item.mission.storage;
677684
if (!storage.existsAsFile()) {
678685
Toast.makeText(mContext, R.string.missing_file, Toast.LENGTH_SHORT).show();
679-
mDeleter.append(h.item.mission);
686+
mDeleter.append(h.item.mission, true);
680687
applyChanges();
681688
return true;
682689
}

app/src/main/java/us/shandian/giga/ui/common/Deleter.java

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
import org.schabi.newpipe.R;
1414

1515
import java.util.ArrayList;
16+
import java.util.Optional;
1617

18+
import kotlin.Pair;
1719
import us.shandian.giga.get.FinishedMission;
1820
import us.shandian.giga.get.Mission;
1921
import us.shandian.giga.service.DownloadManager;
@@ -30,7 +32,8 @@ public class Deleter {
3032
private static final int DELAY_RESUME = 400;// ms
3133

3234
private Snackbar snackbar;
33-
private ArrayList<Mission> items;
35+
// list of missions to be deleted, and whether to also delete the corresponding file
36+
private ArrayList<Pair<Mission, Boolean>> items;
3437
private boolean running = true;
3538

3639
private final Context mContext;
@@ -51,7 +54,7 @@ public Deleter(View v, Context c, MissionAdapter a, DownloadManager d, MissionIt
5154
items = new ArrayList<>(2);
5255
}
5356

54-
public void append(Mission item) {
57+
public void append(Mission item, boolean alsoDeleteFile) {
5558
/* If a mission is removed from the list while the Snackbar for a previously
5659
* removed item is still showing, commit the action for the previous item
5760
* immediately. This prevents Snackbars from stacking up in reverse order.
@@ -60,13 +63,13 @@ public void append(Mission item) {
6063
commit();
6164

6265
mIterator.hide(item);
63-
items.add(0, item);
66+
items.add(0, new Pair<>(item, alsoDeleteFile));
6467

6568
show();
6669
}
6770

6871
private void forget() {
69-
mIterator.unHide(items.remove(0));
72+
mIterator.unHide(items.remove(0).getFirst());
7073
mAdapter.applyChanges();
7174

7275
show();
@@ -84,7 +87,19 @@ private void show() {
8487
private void next() {
8588
if (items.size() < 1) return;
8689

87-
String msg = mContext.getString(R.string.file_deleted).concat(":\n").concat(items.get(0).storage.getName());
90+
final Optional<String> fileToBeDeleted = items.stream()
91+
.filter(Pair::getSecond)
92+
.map(p -> p.getFirst().storage.getName())
93+
.findFirst();
94+
95+
String msg;
96+
if (fileToBeDeleted.isPresent()) {
97+
msg = mContext.getString(R.string.file_deleted)
98+
.concat(":\n")
99+
.concat(fileToBeDeleted.get());
100+
} else {
101+
msg = mContext.getString(R.string.entry_deleted);
102+
}
88103

89104
snackbar = Snackbar.make(mView, msg, Snackbar.LENGTH_INDEFINITE);
90105
snackbar.setAction(R.string.undo, s -> forget());
@@ -98,11 +113,13 @@ private void commit() {
98113
if (items.size() < 1) return;
99114

100115
while (items.size() > 0) {
101-
Mission mission = items.remove(0);
116+
Pair<Mission, Boolean> missionAndAlsoDeleteFile = items.remove(0);
117+
Mission mission = missionAndAlsoDeleteFile.getFirst();
118+
boolean alsoDeleteFile = missionAndAlsoDeleteFile.getSecond();
102119
if (mission.deleted) continue;
103120

104121
mIterator.unHide(mission);
105-
mDownloadManager.deleteMission(mission);
122+
mDownloadManager.deleteMission(mission, alsoDeleteFile);
106123

107124
if (mission instanceof FinishedMission) {
108125
mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mission.storage.getUri()));
@@ -137,7 +154,11 @@ public void dispose() {
137154

138155
pause();
139156

140-
for (Mission mission : items) mDownloadManager.deleteMission(mission);
157+
for (Pair<Mission, Boolean> missionAndAlsoDeleteFile : items) {
158+
Mission mission = missionAndAlsoDeleteFile.getFirst();
159+
boolean alsoDeleteFile = missionAndAlsoDeleteFile.getSecond();
160+
mDownloadManager.deleteMission(mission, alsoDeleteFile);
161+
}
141162
items = null;
142163
}
143164
}

app/src/main/res/menu/mission.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727

2828
<item
2929
android:id="@+id/delete"
30-
android:title="@string/delete" />
30+
android:title="@string/delete_file" />
31+
32+
<item
33+
android:id="@+id/delete_entry"
34+
android:title="@string/delete_entry" />
3135

3236
<item
3337
android:id="@+id/error_message_view"

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@
257257
<string name="restore_defaults">Restore defaults</string>
258258
<string name="restore_defaults_confirmation">Do you want to restore defaults?</string>
259259
<string name="permission_display_over_apps">Give permission to display over other apps</string>
260+
<string name="permission_display_over_apps_message">In order to use the Popup Player, please select %1$s in the following Android settings menu and enable %2$s.</string>
261+
<string name="permission_display_over_apps_permission_name">“Allow display over other apps”</string>
260262
<!-- error activity -->
261263
<string name="error_report_notification_title">NewPipe encountered an error, tap to report</string>
262264
<string name="error_report_notification_toast">An error occurred, see the notification</string>
@@ -333,6 +335,8 @@
333335
<string name="pause">Pause</string>
334336
<string name="create">Create</string>
335337
<string name="delete">Delete</string>
338+
<string name="delete_file">Delete file</string>
339+
<string name="delete_entry">Delete entry</string>
336340
<string name="checksum">Checksum</string>
337341
<string name="dismiss">Dismiss</string>
338342
<string name="rename">Rename</string>
@@ -889,4 +893,5 @@
889893
<string name="trending_podcasts">Trending podcasts</string>
890894
<string name="trending_movies">Trending movies and shows</string>
891895
<string name="trending_music">Trending music</string>
896+
<string name="entry_deleted">Entry deleted</string>
892897
</resources>

0 commit comments

Comments
 (0)