2017-01-23 06:19:30 +01:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
2017-04-10 02:12:31 +02:00
|
|
|
* This file is a part of Tusky.
|
2017-01-23 06:19:30 +01:00
|
|
|
*
|
2017-04-10 02:12:31 +02:00
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2017-01-23 06:19:30 +01:00
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
2017-04-10 02:12:31 +02:00
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
2017-01-23 06:19:30 +01:00
|
|
|
*
|
2017-04-10 02:12:31 +02:00
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
2017-01-23 06:19:30 +01:00
|
|
|
|
2017-05-05 00:55:34 +02:00
|
|
|
package com.keylesspalace.tusky.fragment;
|
2017-01-23 06:19:30 +01:00
|
|
|
|
2019-03-23 12:49:36 +01:00
|
|
|
import android.Manifest;
|
|
|
|
import android.app.DownloadManager;
|
2017-09-25 20:15:04 +02:00
|
|
|
import android.content.ClipData;
|
|
|
|
import android.content.ClipboardManager;
|
|
|
|
import android.content.Context;
|
2017-01-23 06:19:30 +01:00
|
|
|
import android.content.Intent;
|
2019-03-23 12:49:36 +01:00
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Environment;
|
2019-04-21 15:16:39 +02:00
|
|
|
import android.text.SpannableStringBuilder;
|
2017-02-27 06:21:46 +01:00
|
|
|
import android.text.Spanned;
|
2019-07-08 12:57:53 +02:00
|
|
|
import android.text.TextUtils;
|
2019-04-21 15:16:39 +02:00
|
|
|
import android.text.style.URLSpan;
|
2019-07-08 12:57:53 +02:00
|
|
|
import android.util.Log;
|
2018-05-28 21:29:06 +02:00
|
|
|
import android.view.Menu;
|
2019-01-16 20:50:09 +01:00
|
|
|
import android.view.MenuItem;
|
2017-01-23 06:19:30 +01:00
|
|
|
import android.view.View;
|
2019-03-23 12:49:36 +01:00
|
|
|
import android.widget.Toast;
|
2017-01-23 06:19:30 +01:00
|
|
|
|
2019-04-21 15:16:39 +02:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.appcompat.app.AlertDialog;
|
|
|
|
import androidx.appcompat.widget.PopupMenu;
|
|
|
|
import androidx.core.app.ActivityOptionsCompat;
|
|
|
|
import androidx.core.view.ViewCompat;
|
|
|
|
|
2019-01-16 20:50:09 +01:00
|
|
|
import com.keylesspalace.tusky.BaseActivity;
|
2018-05-06 22:05:54 +02:00
|
|
|
import com.keylesspalace.tusky.BottomSheetActivity;
|
2017-05-05 00:55:34 +02:00
|
|
|
import com.keylesspalace.tusky.ComposeActivity;
|
2019-01-16 20:50:09 +01:00
|
|
|
import com.keylesspalace.tusky.MainActivity;
|
2017-05-05 00:55:34 +02:00
|
|
|
import com.keylesspalace.tusky.R;
|
2017-06-25 07:07:41 +02:00
|
|
|
import com.keylesspalace.tusky.ViewMediaActivity;
|
2017-05-05 00:55:34 +02:00
|
|
|
import com.keylesspalace.tusky.ViewTagActivity;
|
2019-06-09 16:55:34 +02:00
|
|
|
import com.keylesspalace.tusky.components.report.ReportActivity;
|
2018-02-03 22:45:14 +01:00
|
|
|
import com.keylesspalace.tusky.db.AccountEntity;
|
2018-03-09 22:02:32 +01:00
|
|
|
import com.keylesspalace.tusky.db.AccountManager;
|
2019-03-11 17:25:11 +01:00
|
|
|
import com.keylesspalace.tusky.di.Injectable;
|
2017-11-30 20:12:09 +01:00
|
|
|
import com.keylesspalace.tusky.entity.Attachment;
|
2019-07-08 12:57:53 +02:00
|
|
|
import com.keylesspalace.tusky.entity.Filter;
|
2017-03-09 00:27:37 +01:00
|
|
|
import com.keylesspalace.tusky.entity.Status;
|
2018-04-25 20:04:55 +02:00
|
|
|
import com.keylesspalace.tusky.network.MastodonApi;
|
2018-03-27 19:47:00 +02:00
|
|
|
import com.keylesspalace.tusky.network.TimelineCases;
|
2018-05-10 20:13:25 +02:00
|
|
|
import com.keylesspalace.tusky.viewdata.AttachmentViewData;
|
2017-01-23 06:19:30 +01:00
|
|
|
|
2019-07-08 12:57:53 +02:00
|
|
|
import java.util.ArrayList;
|
2018-04-17 19:06:28 +02:00
|
|
|
import java.util.LinkedHashSet;
|
2018-05-10 20:13:25 +02:00
|
|
|
import java.util.List;
|
2018-04-17 19:06:28 +02:00
|
|
|
import java.util.Set;
|
2019-07-08 12:57:53 +02:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2017-01-23 06:19:30 +01:00
|
|
|
|
2018-04-25 20:04:55 +02:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2019-07-08 12:57:53 +02:00
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
|
|
|
import retrofit2.Response;
|
|
|
|
|
2017-01-23 06:19:30 +01:00
|
|
|
/* Note from Andrew on Jan. 22, 2017: This class is a design problem for me, so I left it with an
|
|
|
|
* awkward name. TimelineFragment and NotificationFragment have significant overlap but the nature
|
|
|
|
* of that is complicated by how they're coupled with Status and Notification and the corresponding
|
|
|
|
* adapters. I feel like the profile pages and thread viewer, which I haven't made yet, will also
|
|
|
|
* overlap functionality. So, I'm momentarily leaving it and hopefully working on those will clear
|
|
|
|
* up what needs to be where. */
|
2019-03-11 17:25:11 +01:00
|
|
|
public abstract class SFragment extends BaseFragment implements Injectable {
|
2018-10-03 21:27:52 +02:00
|
|
|
|
2018-05-27 10:22:12 +02:00
|
|
|
protected abstract void removeItem(int position);
|
2018-05-06 22:05:54 +02:00
|
|
|
|
2018-05-28 21:29:06 +02:00
|
|
|
protected abstract void onReblog(final boolean reblog, final int position);
|
|
|
|
|
2018-05-06 22:05:54 +02:00
|
|
|
private BottomSheetActivity bottomSheetActivity;
|
2018-04-25 20:04:55 +02:00
|
|
|
|
2019-07-08 12:57:53 +02:00
|
|
|
private static List<Filter> filters;
|
|
|
|
private boolean filterRemoveRegex;
|
|
|
|
private Matcher filterRemoveRegexMatcher;
|
|
|
|
|
2018-04-25 20:04:55 +02:00
|
|
|
@Inject
|
2018-05-27 10:22:12 +02:00
|
|
|
public MastodonApi mastodonApi;
|
2018-07-23 21:59:10 +02:00
|
|
|
@Inject
|
|
|
|
public AccountManager accountManager;
|
2019-03-11 17:25:11 +01:00
|
|
|
@Inject
|
|
|
|
public TimelineCases timelineCases;
|
2017-01-23 06:19:30 +01:00
|
|
|
|
2019-07-08 12:57:53 +02:00
|
|
|
private static final String TAG = "SFragment";
|
|
|
|
|
2017-06-30 08:31:58 +02:00
|
|
|
@Override
|
|
|
|
public void startActivity(Intent intent) {
|
|
|
|
super.startActivity(intent);
|
|
|
|
getActivity().overridePendingTransition(R.anim.slide_from_right, R.anim.slide_to_left);
|
2017-01-23 06:19:30 +01:00
|
|
|
}
|
|
|
|
|
2018-05-06 22:05:54 +02:00
|
|
|
@Override
|
2019-03-11 17:25:11 +01:00
|
|
|
public void onAttach(@NonNull Context context) {
|
2018-05-06 22:05:54 +02:00
|
|
|
super.onAttach(context);
|
2018-10-03 21:27:52 +02:00
|
|
|
if (context instanceof BottomSheetActivity) {
|
|
|
|
bottomSheetActivity = (BottomSheetActivity) context;
|
2018-05-06 22:05:54 +02:00
|
|
|
} else {
|
|
|
|
throw new IllegalStateException("Fragment must be attached to a BottomSheetActivity!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-27 19:47:00 +02:00
|
|
|
protected void openReblog(@Nullable final Status status) {
|
|
|
|
if (status == null) return;
|
2018-05-06 22:05:54 +02:00
|
|
|
bottomSheetActivity.viewAccount(status.getAccount().getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void viewThread(Status status) {
|
2019-02-17 19:09:30 +01:00
|
|
|
Status actionableStatus = status.getActionableStatus();
|
|
|
|
bottomSheetActivity.viewThread(actionableStatus.getId(), actionableStatus.getUrl());
|
2018-05-06 22:05:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void viewAccount(String accountId) {
|
|
|
|
bottomSheetActivity.viewAccount(accountId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onViewUrl(String url) {
|
|
|
|
bottomSheetActivity.viewUrl(url);
|
2018-03-27 19:47:00 +02:00
|
|
|
}
|
|
|
|
|
2017-01-23 06:19:30 +01:00
|
|
|
protected void reply(Status status) {
|
2017-03-09 00:27:37 +01:00
|
|
|
String inReplyToId = status.getActionableId();
|
2017-03-31 21:47:41 +02:00
|
|
|
Status actionableStatus = status.getActionableStatus();
|
2017-11-16 19:18:11 +01:00
|
|
|
Status.Visibility replyVisibility = actionableStatus.getVisibility();
|
2018-03-03 13:24:03 +01:00
|
|
|
String contentWarning = actionableStatus.getSpoilerText();
|
|
|
|
Status.Mention[] mentions = actionableStatus.getMentions();
|
2018-04-17 19:06:28 +02:00
|
|
|
Set<String> mentionedUsernames = new LinkedHashSet<>();
|
2018-03-03 13:24:03 +01:00
|
|
|
mentionedUsernames.add(actionableStatus.getAccount().getUsername());
|
2019-01-11 22:07:25 +01:00
|
|
|
String loggedInUsername = null;
|
|
|
|
AccountEntity activeAccount = accountManager.getActiveAccount();
|
|
|
|
if(activeAccount != null) {
|
|
|
|
loggedInUsername = activeAccount.getUsername();
|
|
|
|
}
|
2017-01-31 05:51:02 +01:00
|
|
|
for (Status.Mention mention : mentions) {
|
2018-03-03 13:24:03 +01:00
|
|
|
mentionedUsernames.add(mention.getUsername());
|
2017-01-23 06:19:30 +01:00
|
|
|
}
|
|
|
|
mentionedUsernames.remove(loggedInUsername);
|
2017-11-01 20:59:29 +01:00
|
|
|
Intent intent = new ComposeActivity.IntentBuilder()
|
|
|
|
.inReplyToId(inReplyToId)
|
|
|
|
.replyVisibility(replyVisibility)
|
|
|
|
.contentWarning(contentWarning)
|
|
|
|
.mentionedUsernames(mentionedUsernames)
|
2019-01-14 21:57:22 +01:00
|
|
|
.replyingStatusAuthor(actionableStatus.getAccount().getLocalUsername())
|
2018-03-03 13:24:03 +01:00
|
|
|
.replyingStatusContent(actionableStatus.getContent().toString())
|
2017-11-01 20:59:29 +01:00
|
|
|
.build(getContext());
|
2018-08-23 20:57:51 +02:00
|
|
|
getActivity().startActivity(intent);
|
2017-04-15 19:44:29 +02:00
|
|
|
}
|
|
|
|
|
2018-10-03 21:27:52 +02:00
|
|
|
protected void more(@NonNull final Status status, View view, final int position) {
|
2017-03-09 00:27:37 +01:00
|
|
|
final String id = status.getActionableId();
|
2018-03-03 13:24:03 +01:00
|
|
|
final String accountId = status.getActionableStatus().getAccount().getId();
|
2018-11-06 18:03:10 +01:00
|
|
|
final String accountUsername = status.getActionableStatus().getAccount().getUsername();
|
2018-03-03 13:24:03 +01:00
|
|
|
final Spanned content = status.getActionableStatus().getContent();
|
|
|
|
final String statusUrl = status.getActionableStatus().getUrl();
|
2019-01-16 20:50:09 +01:00
|
|
|
List<AccountEntity> accounts = accountManager.getAllAccountsOrderedByActive();
|
|
|
|
String openAsTitle = null;
|
|
|
|
|
2019-01-11 22:07:25 +01:00
|
|
|
String loggedInAccountId = null;
|
|
|
|
AccountEntity activeAccount = accountManager.getActiveAccount();
|
|
|
|
if(activeAccount != null) {
|
|
|
|
loggedInAccountId = activeAccount.getAccountId();
|
|
|
|
}
|
|
|
|
|
2017-01-23 06:19:30 +01:00
|
|
|
PopupMenu popup = new PopupMenu(getContext(), view);
|
|
|
|
// Give a different menu depending on whether this is the user's own toot or not.
|
|
|
|
if (loggedInAccountId == null || !loggedInAccountId.equals(accountId)) {
|
|
|
|
popup.inflate(R.menu.status_more);
|
2019-03-23 12:49:36 +01:00
|
|
|
Menu menu = popup.getMenu();
|
|
|
|
menu.findItem(R.id.status_download_media).setVisible(!status.getAttachments().isEmpty());
|
2017-01-23 06:19:30 +01:00
|
|
|
} else {
|
|
|
|
popup.inflate(R.menu.status_more_for_user);
|
2018-05-28 21:29:06 +02:00
|
|
|
Menu menu = popup.getMenu();
|
2018-11-06 18:10:07 +01:00
|
|
|
switch (status.getVisibility()) {
|
|
|
|
case PUBLIC:
|
|
|
|
case UNLISTED: {
|
|
|
|
final String textId =
|
|
|
|
getString(status.isPinned() ? R.string.unpin_action : R.string.pin_action);
|
|
|
|
menu.add(0, R.id.pin, 1, textId);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PRIVATE: {
|
|
|
|
boolean reblogged = status.getReblogged();
|
|
|
|
if (status.getReblog() != null) reblogged = status.getReblog().getReblogged();
|
|
|
|
menu.findItem(R.id.status_reblog_private).setVisible(!reblogged);
|
|
|
|
menu.findItem(R.id.status_unreblog_private).setVisible(reblogged);
|
|
|
|
break;
|
|
|
|
}
|
2018-05-28 21:29:06 +02:00
|
|
|
}
|
2017-01-23 06:19:30 +01:00
|
|
|
}
|
2019-01-16 20:50:09 +01:00
|
|
|
|
|
|
|
Menu menu = popup.getMenu();
|
|
|
|
MenuItem openAsItem = menu.findItem(R.id.status_open_as);
|
|
|
|
switch(accounts.size()) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
openAsItem.setVisible(false);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
for (AccountEntity account : accounts) {
|
|
|
|
if (account != activeAccount) {
|
|
|
|
openAsTitle = String.format(getString(R.string.action_open_as), account.getFullName());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
openAsTitle = String.format(getString(R.string.action_open_as), "…");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
openAsItem.setTitle(openAsTitle);
|
|
|
|
|
2018-03-27 19:47:00 +02:00
|
|
|
popup.setOnMenuItemClickListener(item -> {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.status_share_content: {
|
2018-12-03 15:22:51 +01:00
|
|
|
Status statusToShare = status;
|
|
|
|
if(statusToShare.getReblog() != null) statusToShare = statusToShare.getReblog();
|
2018-03-27 19:47:00 +02:00
|
|
|
|
|
|
|
Intent sendIntent = new Intent();
|
|
|
|
sendIntent.setAction(Intent.ACTION_SEND);
|
2018-12-03 15:22:51 +01:00
|
|
|
|
|
|
|
String stringToShare = statusToShare.getAccount().getUsername() +
|
|
|
|
" - " +
|
|
|
|
statusToShare.getContent().toString();
|
|
|
|
sendIntent.putExtra(Intent.EXTRA_TEXT, stringToShare);
|
2018-03-27 19:47:00 +02:00
|
|
|
sendIntent.setType("text/plain");
|
|
|
|
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_status_content_to)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case R.id.status_share_link: {
|
|
|
|
Intent sendIntent = new Intent();
|
|
|
|
sendIntent.setAction(Intent.ACTION_SEND);
|
|
|
|
sendIntent.putExtra(Intent.EXTRA_TEXT, statusUrl);
|
|
|
|
sendIntent.setType("text/plain");
|
|
|
|
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_status_link_to)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case R.id.status_copy_link: {
|
|
|
|
ClipboardManager clipboard = (ClipboardManager)
|
|
|
|
getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
|
|
|
|
ClipData clip = ClipData.newPlainText(null, statusUrl);
|
|
|
|
clipboard.setPrimaryClip(clip);
|
|
|
|
return true;
|
|
|
|
}
|
2019-01-16 20:50:09 +01:00
|
|
|
case R.id.status_open_as: {
|
|
|
|
showOpenAsDialog(statusUrl, item.getTitle());
|
|
|
|
return true;
|
|
|
|
}
|
2019-03-23 12:49:36 +01:00
|
|
|
case R.id.status_download_media: {
|
|
|
|
requestDownloadAllMedia(status);
|
|
|
|
return true;
|
|
|
|
}
|
2018-03-27 19:47:00 +02:00
|
|
|
case R.id.status_mute: {
|
2019-03-11 17:25:11 +01:00
|
|
|
timelineCases.mute(accountId);
|
2018-03-27 19:47:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case R.id.status_block: {
|
2019-03-11 17:25:11 +01:00
|
|
|
timelineCases.block(accountId);
|
2018-03-27 19:47:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case R.id.status_report: {
|
2018-11-06 18:03:10 +01:00
|
|
|
openReportPage(accountId, accountUsername, id, content);
|
2018-03-27 19:47:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
2018-05-28 21:29:06 +02:00
|
|
|
case R.id.status_unreblog_private: {
|
|
|
|
onReblog(false, position);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case R.id.status_reblog_private: {
|
|
|
|
onReblog(true, position);
|
|
|
|
return true;
|
|
|
|
}
|
2018-03-27 19:47:00 +02:00
|
|
|
case R.id.status_delete: {
|
2018-10-01 08:50:48 +02:00
|
|
|
showConfirmDeleteDialog(id, position);
|
2018-03-27 19:47:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
2019-04-21 15:16:39 +02:00
|
|
|
case R.id.status_delete_and_redraft: {
|
|
|
|
showConfirmEditDialog(id, position, status);
|
|
|
|
return true;
|
|
|
|
}
|
2018-10-03 21:27:52 +02:00
|
|
|
case R.id.pin: {
|
2019-03-11 17:25:11 +01:00
|
|
|
timelineCases.pin(status, !status.isPinned());
|
2018-10-03 21:27:52 +02:00
|
|
|
return true;
|
|
|
|
}
|
2018-03-27 19:47:00 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
2017-01-23 06:19:30 +01:00
|
|
|
popup.show();
|
|
|
|
}
|
|
|
|
|
2018-05-10 20:13:25 +02:00
|
|
|
protected void viewMedia(int urlIndex, Status status, @Nullable View view) {
|
|
|
|
final Status actionable = status.getActionableStatus();
|
|
|
|
final Attachment active = actionable.getAttachments().get(urlIndex);
|
|
|
|
Attachment.Type type = active.getType();
|
2017-01-23 06:19:30 +01:00
|
|
|
switch (type) {
|
2018-10-15 19:56:11 +02:00
|
|
|
case GIFV:
|
|
|
|
case VIDEO:
|
2017-01-23 06:19:30 +01:00
|
|
|
case IMAGE: {
|
2018-05-10 20:13:25 +02:00
|
|
|
final List<AttachmentViewData> attachments = AttachmentViewData.list(actionable);
|
|
|
|
final Intent intent = ViewMediaActivity.newIntent(getContext(), attachments,
|
|
|
|
urlIndex);
|
2017-07-14 07:26:58 +02:00
|
|
|
if (view != null) {
|
2018-05-10 20:13:25 +02:00
|
|
|
String url = active.getUrl();
|
2017-07-14 07:26:58 +02:00
|
|
|
ViewCompat.setTransitionName(view, url);
|
|
|
|
ActivityOptionsCompat options =
|
|
|
|
ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity(),
|
2018-03-09 22:02:32 +01:00
|
|
|
view, url);
|
2017-07-14 07:26:58 +02:00
|
|
|
startActivity(intent, options.toBundle());
|
|
|
|
} else {
|
|
|
|
startActivity(intent);
|
|
|
|
}
|
2017-01-23 06:19:30 +01:00
|
|
|
break;
|
|
|
|
}
|
2017-03-06 01:58:36 +01:00
|
|
|
case UNKNOWN: {
|
|
|
|
/* Intentionally do nothing. This case is here is to handle when new attachment
|
|
|
|
* types are added to the API before code is added here to handle them. So, the
|
|
|
|
* best fallback is to just show the preview and ignore requests to view them. */
|
|
|
|
break;
|
|
|
|
}
|
2017-01-23 06:19:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-27 01:34:32 +01:00
|
|
|
protected void viewTag(String tag) {
|
|
|
|
Intent intent = new Intent(getContext(), ViewTagActivity.class);
|
|
|
|
intent.putExtra("hashtag", tag);
|
|
|
|
startActivity(intent);
|
|
|
|
}
|
2017-01-28 04:33:43 +01:00
|
|
|
|
2017-03-03 01:25:35 +01:00
|
|
|
protected void openReportPage(String accountId, String accountUsername, String statusId,
|
2018-03-09 22:02:32 +01:00
|
|
|
Spanned statusContent) {
|
2019-06-09 16:55:34 +02:00
|
|
|
startActivity(ReportActivity.getIntent(requireContext(),accountId,accountUsername,statusId,statusContent));
|
2017-02-27 06:21:46 +01:00
|
|
|
}
|
2018-04-25 20:04:55 +02:00
|
|
|
|
2018-10-01 08:50:48 +02:00
|
|
|
protected void showConfirmDeleteDialog(final String id, final int position) {
|
|
|
|
new AlertDialog.Builder(getActivity())
|
2018-10-03 21:27:52 +02:00
|
|
|
.setMessage(R.string.dialog_delete_toot_warning)
|
|
|
|
.setPositiveButton(android.R.string.ok, (dialogInterface, i) -> {
|
2019-03-11 17:25:11 +01:00
|
|
|
timelineCases.delete(id);
|
2018-10-03 21:27:52 +02:00
|
|
|
removeItem(position);
|
|
|
|
})
|
|
|
|
.setNegativeButton(android.R.string.cancel, null)
|
|
|
|
.show();
|
2018-10-01 08:50:48 +02:00
|
|
|
}
|
2019-01-16 20:50:09 +01:00
|
|
|
|
2019-04-21 15:16:39 +02:00
|
|
|
private void showConfirmEditDialog(final String id, final int position, Status status) {
|
|
|
|
if (getActivity() == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
new AlertDialog.Builder(getActivity())
|
|
|
|
.setMessage(R.string.dialog_redraft_toot_warning)
|
|
|
|
.setPositiveButton(android.R.string.ok, (dialogInterface, i) -> {
|
|
|
|
timelineCases.delete(id);
|
|
|
|
removeItem(position);
|
|
|
|
|
|
|
|
Intent intent = new ComposeActivity.IntentBuilder()
|
|
|
|
.tootText(getEditableText(status.getContent(), status.getMentions()))
|
|
|
|
.inReplyToId(status.getInReplyToId())
|
|
|
|
.visibility(status.getVisibility())
|
|
|
|
.contentWarning(status.getSpoilerText())
|
|
|
|
.mediaAttachments(status.getAttachments())
|
|
|
|
.sensitive(status.getSensitive())
|
|
|
|
.build(getContext());
|
|
|
|
startActivity(intent);
|
|
|
|
})
|
|
|
|
.setNegativeButton(android.R.string.cancel, null)
|
|
|
|
.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getEditableText(Spanned content, Status.Mention[] mentions) {
|
|
|
|
SpannableStringBuilder builder = new SpannableStringBuilder(content);
|
|
|
|
for (URLSpan span : content.getSpans(0, content.length(), URLSpan.class)) {
|
|
|
|
String url = span.getURL();
|
|
|
|
for (Status.Mention mention : mentions) {
|
|
|
|
if (url.equals(mention.getUrl())) {
|
|
|
|
int start = builder.getSpanStart(span);
|
|
|
|
int end = builder.getSpanEnd(span);
|
|
|
|
builder.replace(start, end, '@' + mention.getUsername());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return builder.toString();
|
|
|
|
}
|
|
|
|
|
2019-01-16 20:50:09 +01:00
|
|
|
private void openAsAccount(String statusUrl, AccountEntity account) {
|
|
|
|
accountManager.setActiveAccount(account);
|
|
|
|
Intent intent = new Intent(getContext(), MainActivity.class);
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
|
|
|
intent.putExtra(MainActivity.STATUS_URL, statusUrl);
|
|
|
|
startActivity(intent);
|
|
|
|
((BaseActivity)getActivity()).finishWithoutSlideOutAnimation();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void showOpenAsDialog(String statusUrl, CharSequence dialogTitle) {
|
2019-02-06 10:23:02 +01:00
|
|
|
BaseActivity activity = (BaseActivity)getActivity();
|
|
|
|
activity.showAccountChooserDialog(dialogTitle, false, account -> openAsAccount(statusUrl, account));
|
2019-01-16 20:50:09 +01:00
|
|
|
}
|
2019-03-23 12:49:36 +01:00
|
|
|
|
|
|
|
private void downloadAllMedia(Status status) {
|
|
|
|
Toast.makeText(getContext(), R.string.downloading_media, Toast.LENGTH_SHORT).show();
|
|
|
|
for(Attachment attachment: status.getAttachments()) {
|
|
|
|
String url = attachment.getUrl();
|
|
|
|
Uri uri = Uri.parse(url);
|
|
|
|
String filename = uri.getLastPathSegment();
|
|
|
|
|
|
|
|
DownloadManager downloadManager = (DownloadManager)getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
|
|
|
|
DownloadManager.Request request = new DownloadManager.Request(uri);
|
|
|
|
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
|
|
|
|
downloadManager.enqueue(request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void requestDownloadAllMedia(Status status) {
|
|
|
|
String[] permissions = new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE };
|
2019-04-21 22:59:49 +02:00
|
|
|
((BaseActivity)getActivity()).requestPermissions(permissions, (permissions1, grantResults) -> {
|
2019-03-23 12:49:36 +01:00
|
|
|
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
downloadAllMedia(status);
|
|
|
|
} else {
|
|
|
|
Toast.makeText(getContext(), R.string.error_media_download_permission, Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-07-08 12:57:53 +02:00
|
|
|
|
|
|
|
void reloadFilters(boolean forceRefresh) {
|
|
|
|
if (filters != null && !forceRefresh) {
|
|
|
|
applyFilters(forceRefresh);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mastodonApi.getFilters().enqueue(new Callback<List<Filter>>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<List<Filter>> call, @NonNull Response<List<Filter>> response) {
|
|
|
|
filters = response.body();
|
|
|
|
if (response.isSuccessful() && filters != null) {
|
|
|
|
applyFilters(forceRefresh);
|
|
|
|
} else {
|
|
|
|
Log.e(TAG, "Error getting filters from server");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<List<Filter>> call, @NonNull Throwable t) {
|
|
|
|
Log.e(TAG, "Error getting filters from server", t);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
protected boolean filterIsRelevant(Filter filter) {
|
|
|
|
// Called when building local filter expression
|
|
|
|
// Override to select relevant filters for your fragment
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void refreshAfterApplyingFilters() {
|
|
|
|
// Called after filters are updated
|
|
|
|
// Override to refresh your fragment
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean shouldFilterStatus(Status status) {
|
|
|
|
return (filterRemoveRegex && (filterRemoveRegexMatcher.reset(status.getActionableStatus().getContent()).find()
|
|
|
|
|| (!status.getSpoilerText().isEmpty() && filterRemoveRegexMatcher.reset(status.getActionableStatus().getSpoilerText()).find())));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void applyFilters(boolean refresh) {
|
|
|
|
List<String> tokens = new ArrayList<>();
|
|
|
|
for (Filter filter : filters) {
|
|
|
|
if (filterIsRelevant(filter)) {
|
|
|
|
tokens.add(filterToRegexToken(filter));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
filterRemoveRegex = !tokens.isEmpty();
|
|
|
|
if (filterRemoveRegex) {
|
|
|
|
filterRemoveRegexMatcher = Pattern.compile(TextUtils.join("|", tokens), Pattern.CASE_INSENSITIVE).matcher("");
|
|
|
|
}
|
|
|
|
if (refresh) {
|
|
|
|
refreshAfterApplyingFilters();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String filterToRegexToken(Filter filter) {
|
|
|
|
String phrase = Pattern.quote(filter.getPhrase());
|
|
|
|
return filter.getWholeWord() ? String.format("(^|\\W)%s($|\\W)", phrase) : phrase;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void flushFilters() {
|
|
|
|
filters = null;
|
|
|
|
}
|
2017-01-23 06:19:30 +01:00
|
|
|
}
|