Skip to content

Commit 962fe9c

Browse files
committed
Use Context instead of Activity
Improve docs
1 parent 50e2385 commit 962fe9c

8 files changed

Lines changed: 138 additions & 40 deletions

File tree

app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ protected void showStreamDialog(final StreamInfoItem item) {
409409
return;
410410
}
411411

412-
new InfoItemDialog.Builder(activity, this, item).create().show();
412+
new InfoItemDialog.Builder(activity, context, this, item).create().show();
413413
}
414414

415415
/*//////////////////////////////////////////////////////////////////////////

app/src/main/java/org/schabi/newpipe/fragments/list/playlist/PlaylistFragment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ protected void showStreamDialog(final StreamInfoItem item) {
142142
return;
143143
}
144144

145-
final InfoItemDialog.Builder dialogBuilder = new InfoItemDialog.Builder(
146-
activity, this, item);
145+
final InfoItemDialog.Builder dialogBuilder =
146+
new InfoItemDialog.Builder(activity, context, this, item);
147147

148148
dialogBuilder.setAction(StreamDialogDefaultEntry.START_HERE_ON_BACKGROUND,
149149
(fragment, infoItem) -> NavigationHelper.playOnBackgroundPlayer(

app/src/main/java/org/schabi/newpipe/info_list/InfoItemDialog.java

Lines changed: 106 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package org.schabi.newpipe.info_list;
22

3-
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
4-
53
import android.app.Activity;
4+
import android.content.Context;
65
import android.content.DialogInterface;
76
import android.view.View;
87
import android.widget.TextView;
@@ -24,10 +23,18 @@
2423
import java.util.List;
2524

2625
/**
27-
* Dialog with actions for a {@link StreamInfoItem}.
26+
* Dialog for a {@link StreamInfoItem}.
27+
* The dialog'S content are actions that can be performed on the {@link StreamInfoItem}.
2828
* This dialog is mostly used for longpress context menus.
2929
*/
3030
public final class InfoItemDialog {
31+
/**
32+
* Ideally, {@link InfoItemDialog} would extend {@link AlertDialog}.
33+
* However, extending {@link AlertDialog} requires many additional lines
34+
* and brings more complexity to this class, especially the constructor.
35+
* To circumvent this, an {@link AlertDialog.Builder} is used in the constructor.
36+
* Its result is stored in this class variable to allow access via the {@link #show()} method.
37+
*/
3138
private final AlertDialog dialog;
3239

3340
private InfoItemDialog(@NonNull final Activity activity,
@@ -76,38 +83,100 @@ public void show() {
7683
*/
7784
public static class Builder {
7885
@NonNull private final Activity activity;
86+
@NonNull private final Context context;
7987
@NonNull private final StreamInfoItem infoItem;
8088
@NonNull private final Fragment fragment;
8189
@NonNull private final List<StreamDialogEntry> entries = new ArrayList<>();
8290
private final boolean addDefaultEntriesAutomatically;
8391

92+
/**
93+
* <p>Create a Builder instance that automatically adds the some default entries
94+
* at the top and bottom of the dialog.</p>
95+
* The dialog has the following structure:
96+
* <pre>
97+
* + - - - - - - - - - - - - - - - - - - - - - -+
98+
* | ENQUEUE |
99+
* | ENQUEUE_HERE |
100+
* | START_ON_BACKGROUND |
101+
* | START_ON_POPUP |
102+
* + - - - - - - - - - - - - - - - - - - - - - -+
103+
* | entries added manually with |
104+
* | addEntry() and addAllEntries() |
105+
* + - - - - - - - - - - - - - - - - - - - - - -+
106+
* | APPEND_PLAYLIST |
107+
* | SHARE |
108+
* | OPEN_IN_BROWSER |
109+
* | PLAY_WITH_KODI |
110+
* | MARK_AS_WATCHED |
111+
* | SHOW_CHANNEL_DETAILS |
112+
* + - - - - - - - - - - - - - - - - - - - - - -+
113+
* </pre>
114+
* Please note that some entries are not added depending on the user's preferences,
115+
* the item's {@link StreamType} and the current player state.
116+
*
117+
* @param activity
118+
* @param context
119+
* @param fragment
120+
* @param infoItem the item for this dialog; all entries and their actions work with
121+
* this {@link org.schabi.newpipe.extractor.InfoItem}
122+
*/
84123
public Builder(@NonNull final Activity activity,
124+
@NonNull final Context context,
85125
@NonNull final Fragment fragment,
86126
@NonNull final StreamInfoItem infoItem) {
87-
this(activity, fragment, infoItem, true);
127+
this(activity, context, fragment, infoItem, true);
88128
}
89129

90130
/**
91-
* <p>Create an instance of this Builder</p>
131+
* <p>Create an instance of this Builder.</p>
132+
* <p>If {@code addDefaultEntriesAutomatically} is set to {@code true},
133+
* some default entries are added to the top and bottom of the dialog.</p>
134+
* The dialog has the following structure:
135+
* <pre>
136+
* + - - - - - - - - - - - - - - - - - - - - - -+
137+
* | ENQUEUE |
138+
* | ENQUEUE_HERE |
139+
* | START_ON_BACKGROUND |
140+
* | START_ON_POPUP |
141+
* + - - - - - - - - - - - - - - - - - - - - - -+
142+
* | entries added manually with |
143+
* | addEntry() and addAllEntries() |
144+
* + - - - - - - - - - - - - - - - - - - - - - -+
145+
* | APPEND_PLAYLIST |
146+
* | SHARE |
147+
* | OPEN_IN_BROWSER |
148+
* | PLAY_WITH_KODI |
149+
* | MARK_AS_WATCHED |
150+
* | SHOW_CHANNEL_DETAILS |
151+
* + - - - - - - - - - - - - - - - - - - - - - -+
152+
* </pre>
153+
* Please note that some entries are not added depending on the user's preferences,
154+
* the item's {@link StreamType} and the current player state.
155+
*
92156
* @param activity
157+
* @param context
93158
* @param fragment
94159
* @param infoItem
95-
* @param addDefaultEntriesAutomatically whether default entries added with
96-
* {@link #addDefaultEntriesAtBeginning()} and
97-
* {@link #addDefaultEntriesAtEnd()}
98-
* are added automatically when generating
99-
* the {@link InfoItemDialog}.
160+
* @param addDefaultEntriesAutomatically
161+
* whether default entries added with {@link #addDefaultBeginningEntries()}
162+
* and {@link #addDefaultEndEntries()} are added automatically when generating
163+
* the {@link InfoItemDialog}.
164+
* <br/>
165+
* Entries added with {@link #addEntry(StreamDialogDefaultEntry)} and
166+
* {@link #addAllEntries(StreamDialogDefaultEntry...)} are added in between.
100167
*/
101168
public Builder(@NonNull final Activity activity,
169+
@NonNull final Context context,
102170
@NonNull final Fragment fragment,
103171
@NonNull final StreamInfoItem infoItem,
104172
final boolean addDefaultEntriesAutomatically) {
105173
this.activity = activity;
174+
this.context = context;
106175
this.fragment = fragment;
107176
this.infoItem = infoItem;
108177
this.addDefaultEntriesAutomatically = addDefaultEntriesAutomatically;
109178
if (addDefaultEntriesAutomatically) {
110-
addDefaultEntriesAtBeginning();
179+
addDefaultBeginningEntries();
111180
}
112181
}
113182

@@ -139,12 +208,11 @@ public void setAction(@NonNull final StreamDialogDefaultEntry entry,
139208
}
140209
}
141210

142-
public void addChannelDetailsEntryIfPossible() {
143-
if (!isNullOrEmpty(infoItem.getUploaderUrl())) {
144-
addEntry(StreamDialogDefaultEntry.SHOW_CHANNEL_DETAILS);
145-
}
146-
}
147-
211+
/**
212+
* Adds {@link StreamDialogDefaultEntry#ENQUEUE} if the player is open and
213+
* {@link StreamDialogDefaultEntry#ENQUEUE_NEXT} if there are multiple streams
214+
* in the play queue.
215+
*/
148216
public void addEnqueueEntriesIfNeeded() {
149217
if (PlayerHolder.getInstance().isPlayerOpen()) {
150218
addEntry(StreamDialogDefaultEntry.ENQUEUE);
@@ -155,6 +223,11 @@ public void addEnqueueEntriesIfNeeded() {
155223
}
156224
}
157225

226+
/**
227+
* Adds the {@link StreamDialogDefaultEntry#START_HERE_ON_BACKGROUND}.
228+
* If the {@link #infoItem} is not a pure audio (live) stream,
229+
* {@link StreamDialogDefaultEntry#START_HERE_ON_POPUP} is added, too.
230+
*/
158231
public void addStartHereEntries() {
159232
addEntry(StreamDialogDefaultEntry.START_HERE_ON_BACKGROUND);
160233
if (infoItem.getStreamType() != StreamType.AUDIO_STREAM
@@ -169,8 +242,8 @@ public void addStartHereEntries() {
169242
*/
170243
public void addMarkAsWatchedEntryIfNeeded() {
171244
final boolean isWatchHistoryEnabled = PreferenceManager
172-
.getDefaultSharedPreferences(activity)
173-
.getBoolean(activity.getString(R.string.enable_watch_history_key), false);
245+
.getDefaultSharedPreferences(context)
246+
.getBoolean(context.getString(R.string.enable_watch_history_key), false);
174247
if (isWatchHistoryEnabled
175248
&& infoItem.getStreamType() != StreamType.LIVE_STREAM
176249
&& infoItem.getStreamType() != StreamType.AUDIO_LIVE_STREAM) {
@@ -179,25 +252,34 @@ public void addMarkAsWatchedEntryIfNeeded() {
179252
}
180253

181254
public void addPlayWithKodiEntryIfNeeded() {
182-
if (KoreUtils.shouldShowPlayWithKodi(activity, infoItem.getServiceId())) {
255+
if (KoreUtils.shouldShowPlayWithKodi(context, infoItem.getServiceId())) {
183256
addEntry(StreamDialogDefaultEntry.PLAY_WITH_KODI);
184257
}
185258
}
186259

187-
public void addDefaultEntriesAtBeginning() {
260+
/**
261+
* Add the entries which are usually at the top of the action list.
262+
* <br/>
263+
* This method adds the "enqueue" (see {@link #addEnqueueEntriesIfNeeded()})
264+
* and "start here" (see {@link #addStartHereEntries()} entries.
265+
*/
266+
public void addDefaultBeginningEntries() {
188267
addEnqueueEntriesIfNeeded();
189268
addStartHereEntries();
190269
}
191270

192-
public void addDefaultEntriesAtEnd() {
271+
/**
272+
* Add the entries which are usually at the bottom of the action list.
273+
*/
274+
public void addDefaultEndEntries() {
193275
addAllEntries(
194276
StreamDialogDefaultEntry.APPEND_PLAYLIST,
195277
StreamDialogDefaultEntry.SHARE,
196278
StreamDialogDefaultEntry.OPEN_IN_BROWSER
197279
);
198280
addPlayWithKodiEntryIfNeeded();
199281
addMarkAsWatchedEntryIfNeeded();
200-
addChannelDetailsEntryIfPossible();
282+
addEntry(StreamDialogDefaultEntry.SHOW_CHANNEL_DETAILS);
201283
}
202284

203285
/**
@@ -206,7 +288,7 @@ public void addDefaultEntriesAtEnd() {
206288
*/
207289
public InfoItemDialog create() {
208290
if (addDefaultEntriesAutomatically) {
209-
addDefaultEntriesAtEnd();
291+
addDefaultEndEntries();
210292
}
211293
return new InfoItemDialog(this.activity, this.fragment, this.infoItem, this.entries);
212294
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ class FeedFragment : BaseStateFragment<FeedState>() {
357357
val activity: Activity? = getActivity()
358358
if (context == null || context.resources == null || activity == null) return
359359

360-
InfoItemDialog.Builder(activity, this, item).create().show()
360+
InfoItemDialog.Builder(activity, context, this, item).create().show()
361361
}
362362

363363
private val listenerStreamItem = object : OnItemClickListener, OnItemLongClickListener {

app/src/main/java/org/schabi/newpipe/local/history/StatisticsPlaylistFragment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,8 @@ private void showStreamDialog(final StreamStatisticsEntry item) {
332332
}
333333
final StreamInfoItem infoItem = item.toStreamInfoItem();
334334

335-
final InfoItemDialog.Builder dialogBuilder = new InfoItemDialog.Builder(
336-
activity, this, infoItem);
335+
final InfoItemDialog.Builder dialogBuilder =
336+
new InfoItemDialog.Builder(activity, context, this, infoItem);
337337

338338
// set entries in the middle; the others are added automatically
339339
dialogBuilder.addEntry(StreamDialogDefaultEntry.DELETE);

app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -747,8 +747,8 @@ protected void showStreamItemDialog(final PlaylistStreamEntry item) {
747747
}
748748
final StreamInfoItem infoItem = item.toStreamInfoItem();
749749

750-
final InfoItemDialog.Builder dialogBuilder = new InfoItemDialog.Builder(
751-
activity, this, infoItem);
750+
final InfoItemDialog.Builder dialogBuilder =
751+
new InfoItemDialog.Builder(activity, context, this, infoItem);
752752

753753
// add entries in the middle
754754
dialogBuilder.addAllEntries(

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import org.schabi.newpipe.NewPipeDatabase;
1313
import org.schabi.newpipe.R;
1414
import org.schabi.newpipe.database.stream.model.StreamEntity;
15+
import org.schabi.newpipe.error.ErrorInfo;
16+
import org.schabi.newpipe.error.ErrorUtil;
17+
import org.schabi.newpipe.error.UserAction;
1518
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
1619
import org.schabi.newpipe.local.dialog.PlaylistAppendDialog;
1720
import org.schabi.newpipe.local.dialog.PlaylistDialog;
@@ -25,11 +28,23 @@
2528
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
2629
import io.reactivex.rxjava3.schedulers.Schedulers;
2730

31+
/**
32+
* <p>
33+
* This enum provides entries that are accepted
34+
* by the {@link org.schabi.newpipe.info_list.InfoItemDialog.Builder}.
35+
* </p>
36+
* <p>
37+
* These entries contain a String {@link #resource} which is displayed in the dialog and
38+
* a default {@link #action} that is executed
39+
* when the entry is selected (via <code>onClick()</code>).
40+
* <br/>
41+
* They action can be overridden by using the Builder's
42+
* {@link org.schabi.newpipe.info_list.InfoItemDialog.Builder#setAction(
43+
* StreamDialogDefaultEntry, StreamDialogEntry.StreamDialogEntryAction)}
44+
* method.
45+
* </p>
46+
*/
2847
public enum StreamDialogDefaultEntry {
29-
//////////////////////////////////////
30-
// enum values with DEFAULT actions //
31-
//////////////////////////////////////
32-
3348
SHOW_CHANNEL_DETAILS(R.string.show_channel_details, (fragment, item) -> {
3449
if (isNullOrEmpty(item.getUploaderUrl())) {
3550
final int serviceId = item.getServiceId();
@@ -125,6 +140,7 @@ public enum StreamDialogDefaultEntry {
125140
public final int resource;
126141
@NonNull
127142
public final StreamDialogEntry.StreamDialogEntryAction action;
143+
128144
StreamDialogDefaultEntry(@StringRes final int resource,
129145
@NonNull final StreamDialogEntry.StreamDialogEntryAction action) {
130146
this.resource = resource;
@@ -136,10 +152,6 @@ public StreamDialogEntry toStreamDialogEntry() {
136152
return new StreamDialogEntry(resource, action);
137153
}
138154

139-
/////////////////////////////////////////////
140-
// private method to open channel fragment //
141-
/////////////////////////////////////////////
142-
143155
private static void openChannelFragment(@NonNull final Fragment fragment,
144156
@NonNull final StreamInfoItem item,
145157
final String uploaderUrl) {

app/src/main/java/org/schabi/newpipe/util/external_communication/KoreUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
import org.schabi.newpipe.extractor.ServiceList;
1111
import org.schabi.newpipe.util.NavigationHelper;
1212

13+
/**
14+
* Util class that provides methods which are related to the Kodi Media Center and its Kore app.
15+
* @see <a href="https://kodi.tv/">Kodi website</a>
16+
*/
1317
public final class KoreUtils {
1418
private KoreUtils() { }
1519

0 commit comments

Comments
 (0)