11package org .schabi .newpipe .info_list ;
22
3- import static org .schabi .newpipe .extractor .utils .Utils .isNullOrEmpty ;
4-
53import android .app .Activity ;
4+ import android .content .Context ;
65import android .content .DialogInterface ;
76import android .view .View ;
87import android .widget .TextView ;
2423import 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 */
3030public 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 }
0 commit comments