1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
package ml.docilealligator.infinityforreddit.bottomsheetfragments;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.account.Account;
import ml.docilealligator.infinityforreddit.activities.BaseActivity;
import ml.docilealligator.infinityforreddit.activities.CommentActivity;
import ml.docilealligator.infinityforreddit.activities.PostFilterPreferenceActivity;
import ml.docilealligator.infinityforreddit.activities.ReportActivity;
import ml.docilealligator.infinityforreddit.activities.SubmitCrosspostActivity;
import ml.docilealligator.infinityforreddit.customviews.LandscapeExpandedRoundedBottomSheetDialogFragment;
import ml.docilealligator.infinityforreddit.databinding.FragmentPostOptionsBottomSheetBinding;
import ml.docilealligator.infinityforreddit.post.Post;
import ml.docilealligator.infinityforreddit.services.DownloadMediaService;
import ml.docilealligator.infinityforreddit.services.DownloadRedditVideoService;
/**
* A simple {@link Fragment} subclass.
* Use the {@link PostOptionsBottomSheetFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class PostOptionsBottomSheetFragment extends LandscapeExpandedRoundedBottomSheetDialogFragment {
private static final String EXTRA_POST = "EP";
private static final String EXTRA_GALLERY_INDEX = "EGI";
private BaseActivity mBaseActivity;
private Post mPost;
private FragmentPostOptionsBottomSheetBinding binding;
public PostOptionsBottomSheetFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param post Post
* @return A new instance of fragment PostOptionsBottomSheetFragment.
*/
public static PostOptionsBottomSheetFragment newInstance(Post post, int galleryIndex) {
PostOptionsBottomSheetFragment fragment = new PostOptionsBottomSheetFragment();
Bundle args = new Bundle();
args.putParcelable(EXTRA_POST, post);
args.putInt(EXTRA_GALLERY_INDEX, galleryIndex);
fragment.setArguments(args);
return fragment;
}
public static PostOptionsBottomSheetFragment newInstance(Post post) {
PostOptionsBottomSheetFragment fragment = new PostOptionsBottomSheetFragment();
Bundle args = new Bundle();
args.putParcelable(EXTRA_POST, post);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mPost = getArguments().getParcelable(EXTRA_POST);
} else {
dismiss();
}
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
binding = FragmentPostOptionsBottomSheetBinding.inflate(inflater, container, false);
if (mPost != null) {
switch (mPost.getPostType()) {
case Post.IMAGE_TYPE:
case Post.GALLERY_TYPE:
binding.downloadTextViewPostOptionsBottomSheetFragment.setVisibility(View.VISIBLE);
binding.downloadTextViewPostOptionsBottomSheetFragment.setText(R.string.download_image);
break;
case Post.GIF_TYPE:
binding.downloadTextViewPostOptionsBottomSheetFragment.setVisibility(View.VISIBLE);
binding.downloadTextViewPostOptionsBottomSheetFragment.setText(R.string.download_gif);
break;
case Post.VIDEO_TYPE:
binding.downloadTextViewPostOptionsBottomSheetFragment.setVisibility(View.VISIBLE);
binding.downloadTextViewPostOptionsBottomSheetFragment.setText(R.string.download_video);
break;
}
if (binding.downloadTextViewPostOptionsBottomSheetFragment.getVisibility() == View.VISIBLE) {
binding.downloadTextViewPostOptionsBottomSheetFragment.setOnClickListener(view -> {
Toast.makeText(mBaseActivity, R.string.download_started, Toast.LENGTH_SHORT).show();
if (mPost.getPostType() == Post.VIDEO_TYPE) {
if (!mPost.isRedgifs() && !mPost.isStreamable() && !mPost.isImgur()) {
PersistableBundle extras = new PersistableBundle();
extras.putString(DownloadRedditVideoService.EXTRA_VIDEO_URL, mPost.getVideoDownloadUrl());
extras.putString(DownloadRedditVideoService.EXTRA_POST_ID, mPost.getId());
extras.putString(DownloadRedditVideoService.EXTRA_SUBREDDIT, mPost.getSubredditName());
extras.putInt(DownloadRedditVideoService.EXTRA_IS_NSFW, mPost.isNSFW() ? 1 : 0);
//TODO: contentEstimatedBytes
JobInfo jobInfo = DownloadRedditVideoService.constructJobInfo(mBaseActivity, 5000000, extras);
((JobScheduler) mBaseActivity.getSystemService(Context.JOB_SCHEDULER_SERVICE)).schedule(jobInfo);
dismiss();
return;
}
}
JobInfo jobInfo = DownloadMediaService.constructJobInfo(mBaseActivity, 5000000, mPost, getArguments().getInt(EXTRA_GALLERY_INDEX, 0));
((JobScheduler) mBaseActivity.getSystemService(Context.JOB_SCHEDULER_SERVICE)).schedule(jobInfo);
dismiss();
});
}
if (mPost.getPostType() == Post.GALLERY_TYPE) {
binding.downloadAllTextViewPostOptionsBottomSheetFragment.setVisibility(View.VISIBLE);
binding.downloadAllTextViewPostOptionsBottomSheetFragment.setOnClickListener(view -> {
JobInfo jobInfo = DownloadMediaService.constructGalleryDownloadAllMediaJobInfo(mBaseActivity, 5000000, mPost);
((JobScheduler) mBaseActivity.getSystemService(Context.JOB_SCHEDULER_SERVICE)).schedule(jobInfo);
dismiss();
});
}
binding.addToPostFilterTextViewPostOptionsBottomSheetFragment.setOnClickListener(view -> {
Intent intent = new Intent(mBaseActivity, PostFilterPreferenceActivity.class);
intent.putExtra(PostFilterPreferenceActivity.EXTRA_POST, mPost);
startActivity(intent);
dismiss();
});
if (mBaseActivity.accountName.equals(Account.ANONYMOUS_ACCOUNT)) {
binding.commentTextViewPostOptionsBottomSheetFragment.setVisibility(View.GONE);
binding.hidePostTextViewPostOptionsBottomSheetFragment.setVisibility(View.GONE);
binding.crosspostTextViewPostOptionsBottomSheetFragment.setVisibility(View.GONE);
binding.reportTextViewPostOptionsBottomSheetFragment.setVisibility(View.GONE);
} else {
binding.commentTextViewPostOptionsBottomSheetFragment.setOnClickListener(view -> {
Intent intent = new Intent(mBaseActivity, CommentActivity.class);
intent.putExtra(CommentActivity.EXTRA_PARENT_FULLNAME_KEY, mPost.getFullName());
intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_TITLE_KEY, mPost.getTitle());
intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_BODY_MARKDOWN_KEY, mPost.getSelfText());
intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_BODY_KEY, mPost.getSelfTextPlain());
intent.putExtra(CommentActivity.EXTRA_SUBREDDIT_NAME_KEY, mPost.getSubredditName());
intent.putExtra(CommentActivity.EXTRA_IS_REPLYING_KEY, false);
intent.putExtra(CommentActivity.EXTRA_PARENT_DEPTH_KEY, 0);
mBaseActivity.startActivity(intent);
dismiss();
});
if (mPost.isHidden()) {
binding.hidePostTextViewPostOptionsBottomSheetFragment.setText(R.string.action_unhide_post);
} else {
binding.hidePostTextViewPostOptionsBottomSheetFragment.setText(R.string.action_hide_post);
}
binding.hidePostTextViewPostOptionsBottomSheetFragment.setOnClickListener(view -> {
if (mBaseActivity instanceof PostOptionsCallback) {
((PostOptionsCallback) mBaseActivity).onOptionClicked(mPost, mPost.isHidden() ? POST_OPTION.UNHIDE_POST : POST_OPTION.HIDE_POST);
}
dismiss();
});
binding.crosspostTextViewPostOptionsBottomSheetFragment.setOnClickListener(view -> {
Intent submitCrosspostIntent = new Intent(mBaseActivity, SubmitCrosspostActivity.class);
submitCrosspostIntent.putExtra(SubmitCrosspostActivity.EXTRA_POST, mPost);
startActivity(submitCrosspostIntent);
dismiss();
});
binding.reportTextViewPostOptionsBottomSheetFragment.setOnClickListener(view -> {
Intent intent = new Intent(mBaseActivity, ReportActivity.class);
intent.putExtra(ReportActivity.EXTRA_SUBREDDIT_NAME, mPost.getSubredditName());
intent.putExtra(ReportActivity.EXTRA_THING_FULLNAME, mPost.getFullName());
startActivity(intent);
});
}
}
return binding.getRoot();
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
mBaseActivity = (BaseActivity) context;
}
public interface PostOptionsCallback {
void onOptionClicked(Post post, @POST_OPTION int option);
}
@IntDef({POST_OPTION.HIDE_POST, POST_OPTION.UNHIDE_POST})
@Retention(RetentionPolicy.SOURCE)
public @interface POST_OPTION {
int HIDE_POST = 0;
int UNHIDE_POST = 1;
int DOWNLOAD_MEDIA = 2;
}
}
|