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
|
package de.danoeh.antennapod.dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import de.danoeh.antennapod.core.util.ShareUtils;
import de.danoeh.antennapod.databinding.ShareEpisodeDialogBinding;
import de.danoeh.antennapod.model.feed.FeedItem;
public class ShareDialog extends BottomSheetDialogFragment {
private static final String ARGUMENT_FEED_ITEM = "feedItem";
private static final String PREF_NAME = "ShareDialog";
private static final String PREF_SHARE_EPISODE_START_AT = "prefShareEpisodeStartAt";
private static final String PREF_SHARE_EPISODE_TYPE = "prefShareEpisodeType";
private Context ctx;
private FeedItem item;
private SharedPreferences prefs;
ShareEpisodeDialogBinding viewBinding;
public ShareDialog() {
// Empty constructor required for DialogFragment
}
public static ShareDialog newInstance(FeedItem item) {
Bundle arguments = new Bundle();
arguments.putSerializable(ARGUMENT_FEED_ITEM, item);
ShareDialog dialog = new ShareDialog();
dialog.setArguments(arguments);
return dialog;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
if (getArguments() != null) {
ctx = getActivity();
item = (FeedItem) getArguments().getSerializable(ARGUMENT_FEED_ITEM);
prefs = getActivity().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
}
viewBinding = ShareEpisodeDialogBinding.inflate(inflater);
viewBinding.shareDialogRadioGroup.setOnCheckedChangeListener((group, checkedId) ->
viewBinding.sharePositionCheckbox.setEnabled(checkedId == viewBinding.shareSocialRadio.getId()));
setupOptions();
viewBinding.shareButton.setOnClickListener((v) -> {
boolean includePlaybackPosition = viewBinding.sharePositionCheckbox.isChecked();
int position;
if (viewBinding.shareSocialRadio.isChecked()) {
ShareUtils.shareFeedItemLinkWithDownloadLink(ctx, item, includePlaybackPosition);
position = 1;
} else if (viewBinding.shareMediaReceiverRadio.isChecked()) {
ShareUtils.shareMediaDownloadLink(ctx, item.getMedia());
position = 2;
} else if (viewBinding.shareMediaFileRadio.isChecked()) {
ShareUtils.shareFeedItemFile(ctx, item.getMedia());
position = 3;
} else {
throw new IllegalStateException("Unknown share method");
}
prefs.edit()
.putBoolean(PREF_SHARE_EPISODE_START_AT, includePlaybackPosition)
.putInt(PREF_SHARE_EPISODE_TYPE, position)
.apply();
dismiss();
});
return viewBinding.getRoot();
}
private void setupOptions() {
final boolean hasMedia = item.getMedia() != null;
boolean downloaded = hasMedia && item.getMedia().isDownloaded();
viewBinding.shareMediaFileRadio.setVisibility(downloaded ? View.VISIBLE : View.GONE);
boolean hasDownloadUrl = hasMedia && item.getMedia().getDownload_url() != null;
if (!hasDownloadUrl) {
viewBinding.shareMediaReceiverRadio.setVisibility(View.GONE);
}
int type = prefs.getInt(PREF_SHARE_EPISODE_TYPE, 1);
if ((type == 2 && !hasDownloadUrl) || (type == 3 && !downloaded)) {
type = 1;
}
viewBinding.shareSocialRadio.setChecked(type == 1);
viewBinding.shareMediaReceiverRadio.setChecked(type == 2);
viewBinding.shareMediaFileRadio.setChecked(type == 3);
boolean switchIsChecked = prefs.getBoolean(PREF_SHARE_EPISODE_START_AT, false);
viewBinding.sharePositionCheckbox.setChecked(switchIsChecked);
}
}
|