fedilab-Android-App/app/src/main/java/app/fedilab/android/activities/TootActivity.java

3920 lines
188 KiB
Java
Raw Normal View History

2017-05-05 16:36:04 +02:00
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2017-05-05 16:36:04 +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.
*
2019-05-18 11:10:30 +02:00
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2017-05-05 16:36:04 +02:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
2017-05-05 16:36:04 +02:00
* see <http://www.gnu.org/licenses>. */
2019-05-18 11:10:30 +02:00
package app.fedilab.android.activities;
2017-05-05 16:36:04 +02:00
2019-06-11 11:49:23 +02:00
2019-11-15 16:32:25 +01:00
import android.Manifest;
2020-03-08 10:29:06 +01:00
import android.annotation.SuppressLint;
2017-05-05 16:36:04 +02:00
import android.app.Activity;
2019-05-30 16:37:49 +02:00
import android.content.BroadcastReceiver;
import android.content.ClipData;
2018-11-14 14:07:25 +01:00
import android.content.ContentResolver;
2019-05-30 16:37:49 +02:00
import android.content.Context;
2017-05-05 16:36:04 +02:00
import android.content.DialogInterface;
import android.content.Intent;
2019-05-30 16:37:49 +02:00
import android.content.IntentFilter;
2017-05-05 16:36:04 +02:00
import android.content.SharedPreferences;
2018-11-14 14:07:25 +01:00
import android.content.pm.PackageManager;
2017-05-05 16:36:04 +02:00
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
2019-07-20 17:44:44 +02:00
import android.graphics.Color;
2019-11-13 12:54:01 +01:00
import android.graphics.drawable.ColorDrawable;
2020-04-09 14:59:36 +02:00
import android.graphics.drawable.Drawable;
2018-11-14 14:07:25 +01:00
import android.net.Uri;
2017-05-05 16:36:04 +02:00
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
2018-11-14 14:07:25 +01:00
import android.os.Environment;
2019-06-27 17:13:26 +02:00
import android.os.Handler;
import android.os.Looper;
2018-11-14 14:07:25 +01:00
import android.provider.MediaStore;
2017-05-05 16:36:04 +02:00
import android.text.Editable;
import android.text.Html;
2017-10-27 11:15:47 +02:00
import android.text.InputFilter;
import android.text.InputType;
2020-02-21 09:53:16 +01:00
import android.text.SpannableString;
2017-05-05 16:36:04 +02:00
import android.text.TextWatcher;
2020-02-21 09:53:16 +01:00
import android.text.style.ForegroundColorSpan;
import android.util.Patterns;
2017-07-16 17:09:35 +02:00
import android.view.LayoutInflater;
import android.view.Menu;
2017-05-05 16:36:04 +02:00
import android.view.MenuItem;
2019-01-23 19:23:26 +01:00
import android.view.MotionEvent;
2017-05-05 16:36:04 +02:00
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
2017-08-02 12:17:43 +02:00
import android.view.inputmethod.InputMethodManager;
2017-05-05 16:36:04 +02:00
import android.widget.ArrayAdapter;
import android.widget.Button;
2017-06-07 15:47:05 +02:00
import android.widget.CheckBox;
2017-07-16 17:09:35 +02:00
import android.widget.DatePicker;
2017-05-05 16:36:04 +02:00
import android.widget.EditText;
2017-11-03 13:55:18 +01:00
import android.widget.GridView;
2017-08-02 17:44:36 +02:00
import android.widget.HorizontalScrollView;
2017-05-05 16:36:04 +02:00
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
2019-01-23 19:23:26 +01:00
import android.widget.ListView;
2019-01-26 20:01:11 +01:00
import android.widget.PopupMenu;
import android.widget.ProgressBar;
2017-10-29 16:53:32 +01:00
import android.widget.RelativeLayout;
2019-07-20 10:02:44 +02:00
import android.widget.ScrollView;
2019-03-24 16:38:12 +01:00
import android.widget.Spinner;
2017-05-05 16:36:04 +02:00
import android.widget.TextView;
2017-07-16 17:09:35 +02:00
import android.widget.TimePicker;
2017-05-05 16:36:04 +02:00
import android.widget.Toast;
2019-11-15 16:32:25 +01:00
import androidx.annotation.NonNull;
2020-04-09 14:59:36 +02:00
import androidx.annotation.Nullable;
2019-11-15 16:32:25 +01:00
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.SwitchCompat;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;
import androidx.fragment.app.DialogFragment;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
2017-12-02 11:02:25 +01:00
import com.bumptech.glide.Glide;
2020-04-09 14:59:36 +02:00
import com.bumptech.glide.request.target.CustomTarget;
2017-12-02 11:02:25 +01:00
import com.bumptech.glide.request.transition.Transition;
2019-07-20 15:15:47 +02:00
import com.github.irshulx.Editor;
import com.github.irshulx.EditorListener;
import com.github.irshulx.models.EditorTextStyle;
2017-11-05 17:53:16 +01:00
import com.github.stom79.localepicker.CountryPicker;
2017-11-29 15:06:30 +01:00
import com.github.stom79.mytransl.MyTransL;
import com.github.stom79.mytransl.client.HttpsConnectionException;
import com.github.stom79.mytransl.translate.Translate;
import com.vanniktech.emoji.EmojiPopup;
2019-07-20 17:44:44 +02:00
2019-06-05 18:23:44 +02:00
import net.gotev.uploadservice.MultipartUploadRequest;
import net.gotev.uploadservice.ServerResponse;
import net.gotev.uploadservice.UploadInfo;
import net.gotev.uploadservice.UploadNotificationConfig;
2019-06-06 15:42:24 +02:00
import net.gotev.uploadservice.UploadServiceSingleBroadcastReceiver;
2019-06-05 18:23:44 +02:00
import net.gotev.uploadservice.UploadStatusDelegate;
2019-06-14 11:37:20 +02:00
import org.apache.poi.util.IOUtils;
2019-11-13 12:54:01 +01:00
import org.jetbrains.annotations.NotNull;
2019-06-05 18:23:44 +02:00
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
2017-05-05 16:36:04 +02:00
import java.io.FileNotFoundException;
2019-06-14 11:37:20 +02:00
import java.io.FileOutputStream;
import java.io.IOException;
2019-06-14 11:37:20 +02:00
import java.io.InputStream;
import java.lang.ref.WeakReference;
2019-06-05 18:23:44 +02:00
import java.net.MalformedURLException;
2019-08-19 19:25:42 +02:00
import java.text.Normalizer;
import java.text.SimpleDateFormat;
2017-05-05 16:36:04 +02:00
import java.util.ArrayList;
2017-07-16 17:09:35 +02:00
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
2019-02-26 18:41:21 +01:00
import java.util.HashMap;
2019-07-21 17:22:39 +02:00
import java.util.Iterator;
2017-05-05 16:36:04 +02:00
import java.util.List;
import java.util.Locale;
2019-07-20 15:15:47 +02:00
import java.util.Map;
2020-02-21 09:53:16 +01:00
import java.util.Objects;
2019-01-20 19:07:24 +01:00
import java.util.TimeZone;
2019-06-06 15:42:24 +02:00
import java.util.UUID;
2017-11-01 13:27:40 +01:00
import java.util.concurrent.TimeUnit;
2017-05-05 16:36:04 +02:00
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2018-11-14 14:07:25 +01:00
2019-06-12 10:04:52 +02:00
import app.fedilab.android.BuildConfig;
2019-06-11 11:49:23 +02:00
import app.fedilab.android.R;
import app.fedilab.android.asynctasks.PostActionAsyncTask;
import app.fedilab.android.asynctasks.PostStatusAsyncTask;
import app.fedilab.android.asynctasks.RetrieveAccountsForReplyAsyncTask;
import app.fedilab.android.asynctasks.RetrieveEmojiAsyncTask;
import app.fedilab.android.asynctasks.RetrieveRelationshipAsyncTask;
2019-06-11 11:49:23 +02:00
import app.fedilab.android.asynctasks.RetrieveSearchAccountsAsyncTask;
import app.fedilab.android.asynctasks.RetrieveSearchAsyncTask;
import app.fedilab.android.asynctasks.UpdateAccountInfoAsyncTask;
import app.fedilab.android.asynctasks.UpdateDescriptionAttachmentAsyncTask;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.API;
import app.fedilab.android.client.APIResponse;
import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.client.Entities.Attachment;
import app.fedilab.android.client.Entities.Emojis;
import app.fedilab.android.client.Entities.Error;
import app.fedilab.android.client.Entities.Mention;
import app.fedilab.android.client.Entities.Notification;
import app.fedilab.android.client.Entities.Poll;
import app.fedilab.android.client.Entities.PollOptions;
import app.fedilab.android.client.Entities.Relationship;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.Entities.Results;
import app.fedilab.android.client.Entities.Status;
import app.fedilab.android.client.Entities.StoredStatus;
2019-07-20 15:15:47 +02:00
import app.fedilab.android.client.Entities.Suggestion;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.Entities.Tag;
2019-06-05 18:23:44 +02:00
import app.fedilab.android.client.GNUAPI;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.HttpsConnection;
import app.fedilab.android.drawers.AccountsReplyAdapter;
import app.fedilab.android.drawers.AccountsSearchAdapter;
import app.fedilab.android.drawers.CustomEmojiAdapter;
import app.fedilab.android.drawers.DraftsListAdapter;
import app.fedilab.android.drawers.EmojisSearchAdapter;
2019-07-20 15:15:47 +02:00
import app.fedilab.android.drawers.SuggestionsAdapter;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.drawers.TagsSearchAdapter;
2019-06-05 18:23:44 +02:00
import app.fedilab.android.helper.FileNameCleaner;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.helper.Helper;
import app.fedilab.android.helper.MastalabAutoCompleteTextView;
2019-07-20 15:15:47 +02:00
import app.fedilab.android.helper.RecyclerItemClickListener;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.interfaces.OnDownloadInterface;
import app.fedilab.android.interfaces.OnPostActionInterface;
import app.fedilab.android.interfaces.OnPostStatusActionInterface;
import app.fedilab.android.interfaces.OnRetrieveAccountsReplyInterface;
import app.fedilab.android.interfaces.OnRetrieveAttachmentInterface;
import app.fedilab.android.interfaces.OnRetrieveEmojiInterface;
import app.fedilab.android.interfaces.OnRetrieveRelationshipInterface;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.interfaces.OnRetrieveSearcAccountshInterface;
import app.fedilab.android.interfaces.OnRetrieveSearchInterface;
2019-06-11 11:49:23 +02:00
import app.fedilab.android.jobs.ScheduledTootsSyncJob;
import app.fedilab.android.sqlite.AccountDAO;
import app.fedilab.android.sqlite.CustomEmojiDAO;
import app.fedilab.android.sqlite.Sqlite;
import app.fedilab.android.sqlite.StatusStoredDAO;
2019-06-26 11:00:59 +02:00
import cafe.adriel.androidaudiorecorder.AndroidAudioRecorder;
import cafe.adriel.androidaudiorecorder.model.AudioChannel;
import cafe.adriel.androidaudiorecorder.model.AudioSampleRate;
import cafe.adriel.androidaudiorecorder.model.AudioSource;
2019-06-11 11:49:23 +02:00
import es.dmoral.toasty.Toasty;
2019-07-20 17:44:44 +02:00
import top.defaults.colorpicker.ColorPickerPopup;
2019-05-30 15:19:02 +02:00
2019-08-19 19:25:42 +02:00
import static app.fedilab.android.helper.Helper.ALPHA;
import static app.fedilab.android.helper.Helper.MORSE;
2019-07-20 15:46:38 +02:00
import static app.fedilab.android.helper.Helper.THEME_BLACK;
import static app.fedilab.android.helper.Helper.THEME_DARK;
import static app.fedilab.android.helper.Helper.THEME_LIGHT;
2019-05-28 15:54:02 +02:00
import static app.fedilab.android.helper.Helper.countWithEmoji;
2020-05-23 17:29:12 +02:00
import static app.fedilab.android.helper.Helper.isValidContextForGlide;
2019-07-20 17:44:44 +02:00
2017-07-01 15:24:28 +02:00
2017-05-05 16:36:04 +02:00
/**
* Created by Thomas on 01/05/2017.
* Toot activity class
*/
2019-07-20 15:15:47 +02:00
public class TootActivity extends BaseActivity implements UploadStatusDelegate, OnPostActionInterface, OnRetrieveSearcAccountshInterface, OnPostStatusActionInterface, OnRetrieveSearchInterface, OnRetrieveAccountsReplyInterface, OnRetrieveEmojiInterface, OnDownloadInterface, OnRetrieveAttachmentInterface, OnRetrieveRelationshipInterface {
2017-05-05 16:36:04 +02:00
2019-11-15 16:32:25 +01:00
public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 754;
public static final int REQUEST_CAMERA_PERMISSION_RESULT = 1653;
public static final int SEND_VOICE_MESSAGE = 1423;
public static HashMap<String, Uri> filesMap;
public static boolean autocomplete;
public static Uri photoFileUri = null;
static boolean active = false;
private static String instance;
private static int searchDeep = 15;
2017-05-05 16:36:04 +02:00
private final int PICK_IMAGE = 56556;
private final int TAKE_PHOTO = 56532;
2019-11-15 16:32:25 +01:00
public long currentToId;
List<Emojis> emojis;
String mCurrentPhotoPath;
File photoFile = null;
private String visibility;
2017-05-05 16:36:04 +02:00
private ImageButton toot_picture;
private LinearLayout toot_picture_container;
private ArrayList<Attachment> attachments;
2017-06-07 15:47:05 +02:00
private boolean isSensitive = false;
2017-05-05 16:36:04 +02:00
private ImageButton toot_visibility;
private Button toot_it;
private MastalabAutoCompleteTextView toot_content;
2017-08-02 17:44:36 +02:00
private EditText toot_cw_content;
private Status tootReply = null;
private String tootMention = null;
private String urlMention = null;
private String sharedContent, sharedSubject, sharedContentIni;
2017-06-07 15:47:05 +02:00
private CheckBox toot_sensitive;
2017-07-16 19:04:53 +02:00
private long restored;
2017-07-21 21:09:32 +02:00
private TextView title;
private ImageView pp_actionBar;
2017-08-02 12:17:43 +02:00
private ProgressBar pp_progress;
private Toast mToast;
2020-01-18 14:54:34 +01:00
private LinearLayout drawer_layout;
2017-08-02 17:44:36 +02:00
private HorizontalScrollView picture_scrollview;
2017-08-18 16:54:43 +02:00
private TextView toot_space_left;
private String initialContent;
2017-10-04 15:57:53 +02:00
private Account accountReply;
2017-10-31 15:57:19 +01:00
private View popup_trans;
private AlertDialog dialogTrans;
2017-11-03 13:55:18 +01:00
private AlertDialog alertDialogEmoji;
private String mentionAccount;
2018-12-08 17:03:27 +01:00
private Status idRedirect;
2019-05-23 07:46:34 +02:00
private String userId;
2018-02-18 19:08:24 +01:00
private Account account;
2018-08-15 11:24:57 +02:00
private ArrayList<String> splitToot;
private int stepSpliToot;
2018-08-18 20:12:37 +02:00
private boolean removed;
2018-08-19 10:26:17 +02:00
private boolean restoredScheduled;
2018-11-03 14:45:55 +01:00
private int style;
2019-01-20 15:20:07 +01:00
private StoredStatus scheduledstatus;
private boolean isScheduled;
2019-01-23 19:23:26 +01:00
private List<Boolean> checkedValues;
private List<Account> contacts;
private ListView lv_accounts_search;
private RelativeLayout loader;
2019-01-26 20:01:11 +01:00
private String contentType;
private int max_media_count;
2019-03-24 16:38:12 +01:00
private Poll poll;
2019-03-24 19:06:03 +01:00
private ImageButton poll_action;
2019-06-04 11:02:15 +02:00
private int pollCountItem;
2019-06-06 15:42:24 +02:00
private UploadServiceSingleBroadcastReceiver uploadReceiver;
2019-06-27 17:13:26 +02:00
private String quickmessagecontent, quickmessagevisibility;
private TextView warning_message;
2019-07-20 15:15:47 +02:00
private Editor wysiwyg;
private EditText wysiwygEditText;
2019-07-21 16:29:23 +02:00
private String url_for_media;
2019-07-31 19:14:42 +02:00
private UpdateAccountInfoAsyncTask.SOCIAL social;
2019-11-15 16:32:25 +01:00
private BroadcastReceiver imageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String imgpath = intent.getStringExtra("imgpath");
if (imgpath != null) {
prepareUpload(TootActivity.this, Uri.fromFile(new File(imgpath)), null, uploadReceiver);
}
2018-11-03 14:45:55 +01:00
}
2019-11-15 16:32:25 +01:00
};
private BroadcastReceiver add_new_media = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
2019-07-31 19:14:42 +02:00
2019-11-15 16:32:25 +01:00
JSONObject response = null;
2020-03-08 10:29:06 +01:00
ArrayList<String> successfullyUploadedFiles;
2019-11-15 16:32:25 +01:00
try {
2020-02-21 09:53:16 +01:00
response = new JSONObject(Objects.requireNonNull(intent.getStringExtra("response")));
2019-11-15 16:32:25 +01:00
} catch (JSONException e) {
e.printStackTrace();
}
successfullyUploadedFiles = intent.getStringArrayListExtra("uploadInfo");
addNewMedia(response, successfullyUploadedFiles);
}
};
2019-11-09 18:07:33 +01:00
2019-11-15 16:32:25 +01:00
public static TextWatcher initializeTextWatcher(Context context, UpdateAccountInfoAsyncTask.SOCIAL social, Status status,
MastalabAutoCompleteTextView toot_content, EditText toot_cw_content, TextView toot_space_left,
ImageView pp_actionBar, ProgressBar pp_progress,
OnRetrieveSearchInterface listener, OnRetrieveSearcAccountshInterface listenerAccount, OnRetrieveEmojiInterface listenerEmoji
) {
2018-11-03 12:06:44 +01:00
2019-11-15 16:32:25 +01:00
String pattern = "(.|\\s)*(@[\\w_-]+@[a-z0-9.\\-]+|@[\\w_-]+)";
final Pattern sPattern = Pattern.compile(pattern);
2019-06-17 14:24:50 +02:00
2019-11-15 16:32:25 +01:00
String patternTag = "^(.|\\s)*(#([\\w-]{2,}))$";
final Pattern tPattern = Pattern.compile(patternTag);
2019-11-09 16:02:19 +01:00
2019-11-15 16:32:25 +01:00
String patternEmoji = "^(.|\\s)*(:([\\w_]+))$";
final Pattern ePattern = Pattern.compile(patternEmoji);
final int[] currentCursorPosition = {toot_content.getSelectionStart()};
final String[] newContent = {null};
final int[] searchLength = {searchDeep};
TextWatcher textw = null;
TextWatcher finalTextw = textw;
textw = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
2017-07-21 21:09:32 +02:00
2019-11-15 16:32:25 +01:00
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
2019-07-20 10:02:44 +02:00
2019-11-15 16:32:25 +01:00
}
2019-07-31 19:14:42 +02:00
2019-11-15 16:32:25 +01:00
@Override
public void afterTextChanged(Editable s) {
2019-11-23 11:00:23 +01:00
2019-11-15 16:32:25 +01:00
if (status != null) {
status.setQuickReplyContent(s.toString());
}
if (autocomplete) {
toot_content.removeTextChangedListener(finalTextw);
Thread thread = new Thread() {
@Override
public void run() {
String fedilabHugsTrigger = ":fedilab_hugs:";
String fedilabMorseTrigger = ":fedilab_morse:";
2019-07-20 15:15:47 +02:00
2019-11-15 16:32:25 +01:00
if (s.toString().contains(fedilabHugsTrigger)) {
newContent[0] = s.toString().replaceAll(fedilabHugsTrigger, "");
int currentLength = countLength(social, toot_content, toot_cw_content);
int toFill = 500 - currentLength;
if (toFill <= 0) {
return;
}
2019-07-20 15:15:47 +02:00
2019-11-11 15:19:02 +01:00
2019-11-15 16:32:25 +01:00
StringBuilder hugs = new StringBuilder();
for (int i = 0; i < toFill; i++) {
hugs.append(new String(Character.toChars(0x1F917)));
}
2019-11-15 16:32:25 +01:00
Handler mainHandler = new Handler(Looper.getMainLooper());
2020-03-08 10:29:06 +01:00
Runnable myRunnable = () -> {
newContent[0] = newContent[0] + hugs.toString();
toot_content.setText(newContent[0]);
toot_content.setSelection(toot_content.getText().length());
autocomplete = false;
toot_space_left.setText(String.valueOf(countLength(social, toot_content, toot_cw_content)));
2019-11-15 16:32:25 +01:00
};
mainHandler.post(myRunnable);
} else if (s.toString().contains(fedilabMorseTrigger)) {
newContent[0] = s.toString().replaceAll(fedilabMorseTrigger, "").trim();
List<String> mentions = new ArrayList<>();
2020-03-08 10:29:06 +01:00
String mentionPattern = "@[a-z0-9_]+(@[a-z0-9.\\-]+[a-z0-9]+)?";
2019-11-15 16:32:25 +01:00
final Pattern mPattern = Pattern.compile(mentionPattern, Pattern.CASE_INSENSITIVE);
Matcher matcherMentions = mPattern.matcher(newContent[0]);
while (matcherMentions.find()) {
mentions.add(matcherMentions.group());
}
for (String mention : mentions) {
newContent[0] = newContent[0].replace(mention, "");
}
newContent[0] = Normalizer.normalize(newContent[0], Normalizer.Form.NFD);
newContent[0] = newContent[0].replaceAll("[^\\p{ASCII}]", "");
2019-07-20 15:32:17 +02:00
2019-11-15 16:32:25 +01:00
HashMap<String, String> ALPHA_TO_MORSE = new HashMap<>();
for (int i = 0; i < ALPHA.length && i < MORSE.length; i++) {
ALPHA_TO_MORSE.put(ALPHA[i], MORSE[i]);
}
StringBuilder builder = new StringBuilder();
String[] words = newContent[0].trim().split(" ");
2019-07-20 15:46:38 +02:00
2019-11-15 16:32:25 +01:00
for (String word : words) {
for (int i = 0; i < word.length(); i++) {
String morse = ALPHA_TO_MORSE.get(word.substring(i, i + 1).toLowerCase());
builder.append(morse).append(" ");
}
2019-07-20 15:46:38 +02:00
2019-11-15 16:32:25 +01:00
builder.append(" ");
}
newContent[0] = "";
for (String mention : mentions) {
newContent[0] += mention + " ";
}
newContent[0] += builder.toString();
2019-07-20 15:15:47 +02:00
2019-11-15 16:32:25 +01:00
Handler mainHandler = new Handler(Looper.getMainLooper());
2019-07-20 15:15:47 +02:00
2020-03-08 10:29:06 +01:00
Runnable myRunnable = () -> {
toot_content.setText(newContent[0]);
toot_content.setSelection(toot_content.getText().length());
autocomplete = false;
toot_space_left.setText(String.valueOf(countLength(social, toot_content, toot_cw_content)));
2019-11-15 16:32:25 +01:00
};
mainHandler.post(myRunnable);
}
}
};
thread.start();
return;
}
2019-07-20 15:15:47 +02:00
2019-11-15 16:32:25 +01:00
if (toot_content.getSelectionStart() != 0)
currentCursorPosition[0] = toot_content.getSelectionStart();
if (s.toString().length() == 0)
currentCursorPosition[0] = 0;
//Only check last 15 characters before cursor position to avoid lags
if (currentCursorPosition[0] < searchDeep) { //Less than 15 characters are written before the cursor position
searchLength[0] = currentCursorPosition[0];
} else {
searchLength[0] = searchDeep;
}
int totalChar = countLength(social, toot_content, toot_cw_content);
toot_space_left.setText(String.valueOf(totalChar));
if (currentCursorPosition[0] - (searchLength[0] - 1) < 0 || currentCursorPosition[0] == 0 || currentCursorPosition[0] > s.toString().length())
return;
String patternh = "^(.|\\s)*(:fedilab_hugs:)$";
final Pattern hPattern = Pattern.compile(patternh);
Matcher mh = hPattern.matcher((s.toString().substring(currentCursorPosition[0] - searchLength[0], currentCursorPosition[0])));
if (mh.matches()) {
autocomplete = true;
return;
}
String patternM = "^(.|\\s)*(:fedilab_morse:)$";
final Pattern mPattern = Pattern.compile(patternM);
Matcher mm = mPattern.matcher((s.toString().substring(currentCursorPosition[0] - searchLength[0], currentCursorPosition[0])));
if (mm.matches()) {
autocomplete = true;
return;
}
String[] searchInArray = (s.toString().substring(currentCursorPosition[0] - searchLength[0], currentCursorPosition[0])).split("\\s");
if (searchInArray.length < 1) {
return;
}
String searchIn = searchInArray[searchInArray.length - 1];
Matcher m, mt;
m = sPattern.matcher(searchIn);
if (m.matches()) {
String search = m.group();
if (pp_progress != null && pp_actionBar != null) {
pp_progress.setVisibility(View.VISIBLE);
pp_actionBar.setVisibility(View.GONE);
}
new RetrieveSearchAccountsAsyncTask(context, search, listenerAccount).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
mt = tPattern.matcher(searchIn);
if (mt.matches()) {
String search = mt.group(3);
2019-07-20 15:15:47 +02:00
if (pp_progress != null && pp_actionBar != null) {
pp_progress.setVisibility(View.VISIBLE);
pp_actionBar.setVisibility(View.GONE);
}
2019-11-15 16:32:25 +01:00
new RetrieveSearchAsyncTask(context, search, true, listener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2019-07-20 15:15:47 +02:00
} else {
2019-11-15 16:32:25 +01:00
mt = ePattern.matcher(searchIn);
2019-07-20 15:15:47 +02:00
if (mt.matches()) {
2019-11-15 16:32:25 +01:00
String shortcode = mt.group(3);
2019-07-20 15:15:47 +02:00
if (pp_progress != null && pp_actionBar != null) {
pp_progress.setVisibility(View.VISIBLE);
pp_actionBar.setVisibility(View.GONE);
}
2019-11-15 16:32:25 +01:00
new RetrieveEmojiAsyncTask(context, shortcode, listenerEmoji).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2019-07-20 15:15:47 +02:00
} else {
2019-11-15 16:32:25 +01:00
toot_content.dismissDropDown();
2019-07-20 15:15:47 +02:00
}
}
}
2019-09-06 17:55:14 +02:00
2019-07-20 15:15:47 +02:00
2019-11-15 16:32:25 +01:00
totalChar = countLength(social, toot_content, toot_cw_content);
toot_space_left.setText(String.valueOf(totalChar));
}
};
return textw;
}
2019-07-20 15:15:47 +02:00
2019-11-15 16:32:25 +01:00
static private void upload(Activity activity, Account account, UpdateAccountInfoAsyncTask.SOCIAL social, Uri inUri, String fname, UploadServiceSingleBroadcastReceiver uploadReceiver) {
String uploadId = UUID.randomUUID().toString();
if (uploadReceiver != null) {
uploadReceiver.setUploadID(uploadId);
}
Uri uri;
InputStream tempInput = null;
FileOutputStream tempOut = null;
String filename = inUri.toString().substring(inUri.toString().lastIndexOf("/"));
int suffixPosition = filename.lastIndexOf(".");
String suffix = "";
if (suffixPosition > 0) suffix = filename.substring(suffixPosition);
try {
File file;
tempInput = activity.getContentResolver().openInputStream(inUri);
if (fname.startsWith("fedilabins_")) {
file = File.createTempFile("fedilabins_randomTemp1", suffix, activity.getCacheDir());
} else {
file = File.createTempFile("randomTemp1", suffix, activity.getCacheDir());
}
2019-11-15 16:32:25 +01:00
filesMap.put(file.getAbsolutePath(), inUri);
tempOut = new FileOutputStream(file.getAbsoluteFile());
byte[] buff = new byte[1024];
int read;
assert tempInput != null;
while ((read = tempInput.read(buff)) > 0) {
tempOut.write(buff, 0, read);
2018-11-04 10:01:16 +01:00
}
2019-11-15 16:32:25 +01:00
if (BuildConfig.DONATIONS) {
uri = FileProvider.getUriForFile(activity,
"fr.gouv.etalab.mastodon.fileProvider",
file);
} else {
uri = FileProvider.getUriForFile(activity,
"app.fedilab.android.fileProvider",
file);
}
tempInput.close();
tempOut.close();
} catch (IOException e) {
e.printStackTrace();
uri = inUri;
} finally {
IOUtils.closeQuietly(tempInput);
IOUtils.closeQuietly(tempOut);
}
try {
final String fileName = FileNameCleaner.cleanFileName(fname);
SharedPreferences sharedpreferences = activity.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String scheme = sharedpreferences.getString(Helper.SET_ONION_SCHEME + account.getInstance(), "https");
String token = account.getToken();
int maxUploadRetryTimes = sharedpreferences.getInt(Helper.MAX_UPLOAD_IMG_RETRY_TIMES, 3);
String url;
if (social != UpdateAccountInfoAsyncTask.SOCIAL.GNU && social != UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA) {
url = scheme + "://" + account.getInstance() + "/api/v1/media";
} else {
url = scheme + "://" + account.getInstance() + "/api/media/upload.json";
}
UploadNotificationConfig uploadConfig = new UploadNotificationConfig();
uploadConfig
.setClearOnActionForAllStatuses(true);
uploadConfig.getProgress().message = activity.getString(R.string.uploading);
uploadConfig.getCompleted().autoClear = true;
MultipartUploadRequest request = new MultipartUploadRequest(activity, uploadId, url);
if (token != null && !token.startsWith("Basic "))
request.addHeader("Authorization", "Bearer " + token);
else if (token != null && token.startsWith("Basic "))
request.addHeader("Authorization", token);
request.setNotificationConfig(uploadConfig);
if (social != UpdateAccountInfoAsyncTask.SOCIAL.GNU && social != UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA) {
request.addFileToUpload(uri.toString().replace("file://", ""), "file");
} else {
request.addFileToUpload(uri.toString().replace("file://", ""), "media");
}
request.addParameter("filename", fileName).setMaxRetries(maxUploadRetryTimes)
.startUpload();
} catch (MalformedURLException | FileNotFoundException e) {
e.printStackTrace();
}
}
public static void manageMentions(Context context, UpdateAccountInfoAsyncTask.SOCIAL social, String userIdReply, MastalabAutoCompleteTextView contentView, EditText CWView, TextView counterView, Status tootReply) {
//Retrieves mentioned accounts + OP and adds them at the beginin of the toot
ArrayList<String> mentionedAccountsAdded = new ArrayList<>();
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
int cursorReply = 0;
if (tootReply.getAccount() != null && tootReply.getAccount().getAcct() != null && !tootReply.getAccount().getId().equals(userIdReply)) {
contentView.setText(String.format("@%s", tootReply.getAccount().getAcct()));
mentionedAccountsAdded.add(tootReply.getAccount().getAcct());
//Evaluate the cursor position => mention length + 1 char for carriage return
cursorReply = contentView.getText().toString().length() + 1;
}
if (tootReply.getMentions() != null) {
//Put other accounts mentioned at the bottom
boolean capitalize = sharedpreferences.getBoolean(Helper.SET_CAPITALIZE, true);
if (capitalize)
contentView.setText(String.format("%s", (contentView.getText().toString() + "\n\n")));
else
contentView.setText(String.format("%s", (contentView.getText().toString() + " ")));
for (Mention mention : tootReply.getMentions()) {
if (mention.getAcct() != null && !mention.getId().equals(userIdReply) && !mentionedAccountsAdded.contains(mention.getAcct())) {
mentionedAccountsAdded.add(mention.getAcct());
String tootTemp = String.format("@%s ", mention.getAcct());
contentView.setText(String.format("%s ", (contentView.getText().toString() + tootTemp.trim())));
}
}
contentView.setText(contentView.getText().toString().trim());
if (contentView.getText().toString().startsWith("@")) {
if (capitalize)
contentView.append("\n");
else
contentView.append(" ");
}
counterView.setText(String.valueOf(countLength(social, contentView, CWView)));
contentView.requestFocus();
if (capitalize) {
if (mentionedAccountsAdded.size() == 1) {
contentView.setSelection(contentView.getText().length()); //Put cursor at the end
} else {
if (cursorReply > 0 && cursorReply < contentView.getText().length())
contentView.setSelection(cursorReply);
else
contentView.setSelection(contentView.getText().length()); //Put cursor at the end
}
} else {
contentView.setSelection(contentView.getText().length()); //Put cursor at the end
}
}
}
2020-05-23 17:29:12 +02:00
public static String manageMentions(String userIdReply, Status tootReply) {
2019-11-15 16:32:25 +01:00
String contentView = "";
//Retrieves mentioned accounts + OP and adds them at the beginin of the toot
ArrayList<String> mentionedAccountsAdded = new ArrayList<>();
if (tootReply.getAccount() != null && tootReply.getAccount().getAcct() != null && !tootReply.getAccount().getId().equals(userIdReply)) {
contentView = String.format("@%s", tootReply.getAccount().getAcct());
mentionedAccountsAdded.add(tootReply.getAccount().getAcct());
//Evaluate the cursor position => mention length + 1 char for carriage return
}
if (tootReply.getMentions() != null) {
//Put other accounts mentioned at the bottom
contentView = String.format("%s", (contentView + " "));
for (Mention mention : tootReply.getMentions()) {
if (mention.getAcct() != null && !mention.getId().equals(userIdReply) && !mentionedAccountsAdded.contains(mention.getAcct())) {
mentionedAccountsAdded.add(mention.getAcct());
String tootTemp = String.format("@%s ", mention.getAcct());
contentView = String.format("%s ", (contentView + tootTemp.trim()));
}
}
contentView = contentView.trim();
if (contentView.startsWith("@")) {
contentView += " ";
}
}
return contentView;
}
public static int countLength(UpdateAccountInfoAsyncTask.SOCIAL social, MastalabAutoCompleteTextView toot_content, EditText toot_cw_content) {
if (toot_content == null || toot_cw_content == null) {
return -1;
}
String content = toot_content.getText().toString();
String cwContent = toot_cw_content.getText().toString();
String contentCount = content;
if (social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON) {
2020-03-08 10:29:06 +01:00
contentCount = contentCount.replaceAll("(?i)(^|[^/\\w])@(([a-z0-9_]+)@[a-z0-9.-]+[a-z0-9]+)", "$1@$3");
2019-11-15 16:32:25 +01:00
Matcher matcherALink = Patterns.WEB_URL.matcher(contentCount);
while (matcherALink.find()) {
final String url = matcherALink.group(1);
2020-02-21 09:53:16 +01:00
assert url != null;
2019-11-15 16:32:25 +01:00
contentCount = contentCount.replace(url, "abcdefghijklmnopkrstuvw");
}
}
int contentLength = contentCount.length() - countWithEmoji(content);
int cwLength = cwContent.length() - countWithEmoji(cwContent);
return cwLength + contentLength;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
2020-04-08 12:42:15 +02:00
instance = sharedpreferences.getString(Helper.PREF_INSTANCE, Helper.getLiveInstance(TootActivity.this));
2019-11-15 16:32:25 +01:00
final int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
switch (theme) {
case Helper.THEME_LIGHT:
setTheme(R.style.AppTheme_Fedilab);
break;
case Helper.THEME_BLACK:
setTheme(R.style.AppThemeBlack);
break;
default:
setTheme(R.style.AppThemeDark);
}
if (theme == Helper.THEME_DARK) {
style = R.style.DialogDark;
} else if (theme == Helper.THEME_BLACK) {
style = R.style.DialogBlack;
} else {
style = R.style.Dialog;
}
filesMap = new HashMap<>();
social = MainActivity.social;
autocomplete = false;
2019-11-20 19:07:46 +01:00
setContentView(R.layout.activity_toot);
2019-11-15 16:32:25 +01:00
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
assert inflater != null;
2020-04-08 12:42:15 +02:00
View view = inflater.inflate(R.layout.toot_action_bar, new LinearLayout(TootActivity.this), false);
2019-11-15 19:36:39 +01:00
view.setBackground(new ColorDrawable(ContextCompat.getColor(TootActivity.this, R.color.cyanea_primary)));
2019-11-15 16:32:25 +01:00
actionBar.setCustomView(view, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
ImageView close_toot = actionBar.getCustomView().findViewById(R.id.close_toot);
2020-02-21 09:53:16 +01:00
close_toot.setOnClickListener(v -> {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
assert inputMethodManager != null;
inputMethodManager.hideSoftInputFromWindow(toot_content.getWindowToken(), 0);
boolean storeToot = sharedpreferences.getBoolean(Helper.SET_AUTO_STORE, true);
if (!storeToot) {
if (toot_content.getText().toString().trim().length() == 0 && (attachments == null || attachments.size() < 1) && toot_cw_content.getText().toString().trim().length() == 0) {
finish();
} else if (!displayWYSIWYG() && initialContent.trim().equals(toot_content.getText().toString().trim())) {
finish();
} else if (displayWYSIWYG() && initialContent.trim().equals(wysiwyg.getContentAsHTML().trim())) {
finish();
} else {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(TootActivity.this, style);
dialogBuilder.setMessage(R.string.save_draft);
dialogBuilder.setPositiveButton(R.string.save, (dialog, id) -> {
if (accountReply == null) {
storeToot(true, false);
} else {
storeToot(false, false);
}
dialog.dismiss();
2019-11-15 16:32:25 +01:00
finish();
2020-02-21 09:53:16 +01:00
});
dialogBuilder.setNegativeButton(R.string.discard, (dialog, id) -> {
dialog.dismiss();
2019-11-15 16:32:25 +01:00
finish();
2020-02-21 09:53:16 +01:00
});
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.setCancelable(false);
alertDialog.show();
2019-11-15 16:32:25 +01:00
}
2020-02-21 09:53:16 +01:00
} else {
finish();
2019-11-15 16:32:25 +01:00
}
});
title = actionBar.getCustomView().findViewById(R.id.toolbar_title);
pp_actionBar = actionBar.getCustomView().findViewById(R.id.pp_actionBar);
pp_progress = actionBar.getCustomView().findViewById(R.id.pp_progress);
}
2019-11-20 19:07:46 +01:00
2019-11-15 16:32:25 +01:00
//By default the toot is not restored so the id -1 is defined
currentToId = -1;
restoredScheduled = false;
contentType = null;
checkedValues = new ArrayList<>();
contacts = new ArrayList<>();
toot_it = findViewById(R.id.toot_it);
Button toot_cw = findViewById(R.id.toot_cw);
toot_space_left = findViewById(R.id.toot_space_left);
toot_visibility = findViewById(R.id.toot_visibility);
toot_picture = findViewById(R.id.toot_picture);
toot_picture_container = findViewById(R.id.toot_picture_container);
toot_content = findViewById(R.id.toot_content);
int newInputType = toot_content.getInputType() & (toot_content.getInputType() ^ InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE);
toot_content.setInputType(newInputType);
toot_cw_content = findViewById(R.id.toot_cw_content);
picture_scrollview = findViewById(R.id.picture_scrollview);
toot_sensitive = findViewById(R.id.toot_sensitive);
drawer_layout = findViewById(R.id.drawer_layout);
ImageButton toot_emoji = findViewById(R.id.toot_emoji);
warning_message = findViewById(R.id.warning_message);
poll_action = findViewById(R.id.poll_action);
2020-04-11 18:14:20 +02:00
toot_content.setKeyBoardInputCallbackListener((inputContentInfo, flags, opts) -> {
SharedPreferences sharedpreferences1 = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
boolean photo_editor = sharedpreferences1.getBoolean(Helper.SET_PHOTO_EDITOR, true);
2020-04-16 14:34:45 +02:00
if (inputContentInfo != null) {
2020-04-11 18:14:20 +02:00
Uri uri = inputContentInfo.getContentUri();
String filename = Helper.getFileName(TootActivity.this, uri);
ContentResolver cr = getContentResolver();
String mime = cr.getType(uri);
if (mime != null && (mime.toLowerCase().contains("video") || mime.toLowerCase().contains("gif"))) {
prepareUpload(TootActivity.this, uri, filename, uploadReceiver);
} else if (mime != null && mime.toLowerCase().contains("image")) {
if (photo_editor) {
Intent intent = new Intent(TootActivity.this, PhotoEditorActivity.class);
Bundle b = new Bundle();
intent.putExtra("imageUri", uri.toString());
intent.putExtras(b);
startActivity(intent);
} else {
prepareUpload(TootActivity.this, uri, filename, uploadReceiver);
}
} else if (mime != null && mime.toLowerCase().contains("audio")) {
prepareUpload(TootActivity.this, uri, filename, uploadReceiver);
} else {
Toasty.error(TootActivity.this, getString(R.string.toot_select_image_error), Toast.LENGTH_LONG).show();
}
}
});
2020-04-08 12:42:15 +02:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(TootActivity.this);
2019-11-15 16:32:25 +01:00
int iconColor = prefs.getInt("theme_icons_color", -1);
if (iconColor != -1) {
2020-04-08 12:42:15 +02:00
Helper.changeDrawableColor(TootActivity.this, toot_emoji, iconColor);
Helper.changeDrawableColor(TootActivity.this, toot_visibility, iconColor);
Helper.changeDrawableColor(TootActivity.this, poll_action, iconColor);
Helper.changeDrawableColor(TootActivity.this, toot_picture, iconColor);
2019-11-15 16:32:25 +01:00
}
isScheduled = false;
2020-03-15 17:12:53 +01:00
if (sharedpreferences.getBoolean(Helper.SET_DISPLAY_EMOJI, false)) {
2019-11-15 16:32:25 +01:00
final EmojiPopup emojiPopup = EmojiPopup.Builder.fromRootView(drawer_layout).build(toot_content);
2020-03-08 10:29:06 +01:00
toot_emoji.setOnClickListener(v -> {
emojiPopup.toggle(); // Toggles visibility of the Popup.
2019-11-15 16:32:25 +01:00
});
} else {
toot_emoji.setVisibility(View.GONE);
}
ScrollView composer_container = findViewById(R.id.composer_container);
ScrollView wysiwyg_container = findViewById(R.id.wysiwyg_container);
wysiwyg = findViewById(R.id.editor);
switch (theme) {
case THEME_LIGHT:
wysiwyg.setEditorTextColor("#000000");
break;
case THEME_BLACK:
case THEME_DARK:
wysiwyg.setEditorTextColor("#f3f3f3");
break;
}
if (displayWYSIWYG()) {
wysiwyg_container.setVisibility(View.VISIBLE);
composer_container.setVisibility(View.GONE);
HorizontalScrollView toolbar_scrollview = findViewById(R.id.toolbar_scrollview);
toolbar_scrollview.setVisibility(View.VISIBLE);
renderEditor();
wysiwyg.setEditorListener(new EditorListener() {
@Override
public void onTextChanged(EditText editText, Editable s) {
wysiwygEditText = editText;
String pattern = "^(.|\\s)*(@[\\w_-]+@[a-z0-9.\\-]+|@[\\w_-]+)$";
final Pattern sPattern = Pattern.compile(pattern);
String patternTag = "^(.|\\s)*(#([\\w-]{2,}))$";
final Pattern tPattern = Pattern.compile(patternTag);
String patternEmoji = "^(.|\\s)*(:([\\w_]+))$";
final Pattern ePattern = Pattern.compile(patternEmoji);
int currentCursorPosition = editText.getSelectionStart();
if (editText.getSelectionStart() != 0)
currentCursorPosition = editText.getSelectionStart();
if (s.toString().length() == 0)
currentCursorPosition = 0;
//Only check last 15 characters before cursor position to avoid lags
int searchLength;
if (currentCursorPosition < searchDeep) { //Less than 15 characters are written before the cursor position
searchLength = currentCursorPosition;
} else {
searchLength = searchDeep;
}
int totalChar = countLength(wysiwyg, toot_cw_content);
toot_space_left.setText(String.valueOf(totalChar));
if (currentCursorPosition - (searchLength - 1) < 0 || currentCursorPosition == 0 || currentCursorPosition > s.toString().length())
return;
String[] searchInArray = (s.toString().substring(currentCursorPosition - searchLength, currentCursorPosition)).split("\\s");
String searchIn = searchInArray[searchInArray.length - 1];
Matcher m, mt;
m = sPattern.matcher(searchIn);
if (m.matches()) {
String search = m.group(1);
if (pp_progress != null && pp_actionBar != null) {
pp_progress.setVisibility(View.VISIBLE);
pp_actionBar.setVisibility(View.GONE);
}
2020-04-08 12:42:15 +02:00
new RetrieveSearchAccountsAsyncTask(TootActivity.this, search, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2019-11-15 16:32:25 +01:00
} else {
mt = tPattern.matcher(searchIn);
if (mt.matches()) {
String search = mt.group(3);
if (pp_progress != null && pp_actionBar != null) {
pp_progress.setVisibility(View.VISIBLE);
pp_actionBar.setVisibility(View.GONE);
}
new RetrieveSearchAsyncTask(TootActivity.this, search, true, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
mt = ePattern.matcher(searchIn);
if (mt.matches()) {
String shortcode = mt.group(3);
if (pp_progress != null && pp_actionBar != null) {
pp_progress.setVisibility(View.VISIBLE);
pp_actionBar.setVisibility(View.GONE);
}
new RetrieveEmojiAsyncTask(TootActivity.this, shortcode, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
}
totalChar = countLength(wysiwyg, toot_cw_content);
toot_space_left.setText(String.valueOf(totalChar));
}
@Override
public void onUpload(Bitmap image, String uuid) {
if (url_for_media != null) {
wysiwyg.onImageUploadComplete(url_for_media, uuid);
}
}
@Override
public View onRenderMacro(String name, Map<String, Object> props, int index) {
return null;
}
});
}
2020-03-08 10:29:06 +01:00
drawer_layout.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
int heightDiff = drawer_layout.getRootView().getHeight() - drawer_layout.getHeight();
2020-04-08 12:42:15 +02:00
if (heightDiff > Helper.convertDpToPixel(200, TootActivity.this)) {
2020-03-08 10:29:06 +01:00
ViewGroup.LayoutParams params = toot_picture_container.getLayoutParams();
2020-04-08 12:42:15 +02:00
params.height = (int) Helper.convertDpToPixel(50, TootActivity.this);
params.width = (int) Helper.convertDpToPixel(50, TootActivity.this);
2020-03-08 10:29:06 +01:00
toot_picture_container.setLayoutParams(params);
} else {
ViewGroup.LayoutParams params = toot_picture_container.getLayoutParams();
2020-04-08 12:42:15 +02:00
params.height = (int) Helper.convertDpToPixel(100, TootActivity.this);
params.width = (int) Helper.convertDpToPixel(100, TootActivity.this);
2020-03-08 10:29:06 +01:00
toot_picture_container.setLayoutParams(params);
2019-11-15 16:32:25 +01:00
}
});
Bundle b = getIntent().getExtras();
ArrayList<Uri> sharedUri = new ArrayList<>();
2020-04-09 18:57:12 +02:00
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2019-11-15 16:32:25 +01:00
restored = -1;
if (b != null) {
tootReply = b.getParcelable("tootReply");
scheduledstatus = b.getParcelable("storedStatus");
String accountReplyToken = b.getString("accountReplyToken", null);
accountReply = null;
if (accountReplyToken != null) {
String[] val = accountReplyToken.split("\\|");
if (val.length == 2) {
2020-04-08 12:42:15 +02:00
accountReply = new AccountDAO(TootActivity.this, db).getUniqAccount(val[0], val[1]);
2019-11-15 16:32:25 +01:00
}
}
tootMention = b.getString("tootMention", null);
urlMention = b.getString("urlMention", null);
sharedContent = b.getString("sharedContent", null);
sharedContentIni = b.getString("sharedContent", null);
sharedSubject = b.getString("sharedSubject", null);
mentionAccount = b.getString("mentionAccount", null);
2019-05-30 15:19:02 +02:00
idRedirect = b.getParcelable("idRedirect");
2018-08-18 20:12:37 +02:00
removed = b.getBoolean("removed");
2018-09-05 17:08:57 +02:00
visibility = b.getString("visibility", null);
restoredScheduled = b.getBoolean("restoredScheduled", false);
2019-06-27 16:00:27 +02:00
quickmessagecontent = b.getString("quickmessagecontent", null);
2019-09-06 17:55:14 +02:00
quickmessagevisibility = b.getString("quickmessagevisibility", null);
// ACTION_SEND route
2017-12-02 08:39:01 +01:00
if (b.getInt("uriNumberMast", 0) == 1) {
Uri fileUri = b.getParcelable("sharedUri");
2017-09-16 12:20:06 +02:00
if (fileUri != null) {
sharedUri.add(fileUri);
}
}
// ACTION_SEND_MULTIPLE route
2019-05-30 15:19:02 +02:00
else if (b.getInt("uriNumberMast", 0) > 1) {
ArrayList<Uri> fileUri = b.getParcelableArrayList("sharedUri");
if (fileUri != null) {
sharedUri.addAll(fileUri);
}
}
2017-07-16 19:04:53 +02:00
restored = b.getLong("restored", -1);
}
2019-11-21 18:50:30 +01:00
if (tootReply != null && tootReply.getAccount() != null) {
2019-10-04 19:14:40 +02:00
if (tootReply.getAccount() != null && tootReply.getAccount().getMoved_to_account() != null) {
warning_message.setVisibility(View.VISIBLE);
}
2020-04-08 12:42:15 +02:00
new RetrieveRelationshipAsyncTask(TootActivity.this, tootReply.getAccount().getId(), TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
2019-05-30 15:19:02 +02:00
if (scheduledstatus != null)
2019-01-20 15:20:07 +01:00
toot_it.setText(R.string.modify);
2019-05-30 15:19:02 +02:00
if (restoredScheduled) {
2017-07-16 19:04:53 +02:00
toot_it.setVisibility(View.GONE);
invalidateOptionsMenu();
}
2019-06-05 14:35:42 +02:00
String userIdReply, instanceReply;
if (accountReply == null) {
2018-02-18 19:08:24 +01:00
userIdReply = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
2019-06-05 14:35:42 +02:00
instanceReply = sharedpreferences.getString(Helper.PREF_INSTANCE, null);
2019-09-06 17:55:14 +02:00
} else {
2018-02-18 19:08:24 +01:00
userIdReply = accountReply.getId();
2019-06-05 14:35:42 +02:00
instanceReply = accountReply.getInstance();
}
2019-05-30 15:19:02 +02:00
if (accountReply == null)
2020-04-08 12:42:15 +02:00
account = new AccountDAO(TootActivity.this, db).getUniqAccount(userIdReply, instanceReply);
2018-02-18 19:08:24 +01:00
else
account = accountReply;
2019-07-31 19:14:42 +02:00
if (social == null || accountReply != null) {
2019-03-02 18:48:25 +01:00
//Update the static variable which manages account type
2019-11-03 09:49:35 +01:00
social = Helper.setSoftware(account.getSocial(), false);
2019-03-02 18:48:25 +01:00
}
2019-07-31 19:14:42 +02:00
switch (social) {
2019-03-02 18:48:25 +01:00
case GNU:
toot_it.setText(getText(R.string.queet_it));
break;
case PLEROMA:
toot_it.setText(getText(R.string.submit));
break;
case FRIENDICA:
toot_it.setText(getText(R.string.share));
break;
default:
toot_it.setText(getText(R.string.toot_it));
}
2019-07-31 19:14:42 +02:00
if (social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA) {
max_media_count = 9999;
} else {
max_media_count = 4;
}
if (social == UpdateAccountInfoAsyncTask.SOCIAL.GNU || social == UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA)
toot_visibility.setVisibility(View.GONE);
2019-03-02 18:48:25 +01:00
2019-05-30 15:19:02 +02:00
if (tootReply != null) {
tootReply();
2019-05-30 15:19:02 +02:00
} else {
if (title != null) {
2019-07-31 19:14:42 +02:00
if (social == UpdateAccountInfoAsyncTask.SOCIAL.GNU)
2019-02-21 20:04:16 +01:00
title.setText(getString(R.string.queet_title));
else
title.setText(getString(R.string.toot_title));
2019-05-30 15:19:02 +02:00
} else {
2019-07-31 19:14:42 +02:00
if (social == UpdateAccountInfoAsyncTask.SOCIAL.GNU)
2019-02-21 20:04:16 +01:00
setTitle(R.string.queet_title);
else
setTitle(R.string.toot_title);
}
2017-05-05 16:36:04 +02:00
}
2018-02-18 19:08:24 +01:00
toot_content.requestFocus();
2019-09-06 17:55:14 +02:00
if (quickmessagecontent == null) {
2019-06-27 16:00:27 +02:00
if (mentionAccount != null) {
toot_content.setText(String.format("@%s\n", mentionAccount));
toot_content.setSelection(toot_content.getText().length());
2019-07-31 19:14:42 +02:00
toot_space_left.setText(String.valueOf(countLength(social, toot_content, toot_cw_content)));
2019-06-27 16:00:27 +02:00
}
if (tootMention != null && urlMention != null) {
toot_content.setText(String.format("\n\nvia @%s\n\n%s\n\n", tootMention, urlMention));
2019-07-31 19:14:42 +02:00
toot_space_left.setText(String.valueOf(countLength(social, toot_content, toot_cw_content)));
2019-06-27 16:00:27 +02:00
}
2019-09-06 17:55:14 +02:00
} else {
2019-06-27 16:00:27 +02:00
toot_content.setText(quickmessagecontent);
2019-07-31 19:14:42 +02:00
toot_space_left.setText(String.valueOf(countLength(social, toot_content, toot_cw_content)));
toot_content.setSelection(toot_content.getText().length());
2017-09-16 12:20:06 +02:00
}
2019-05-13 18:15:45 +02:00
2019-09-06 17:55:14 +02:00
initialContent = displayWYSIWYG() ? wysiwyg.getContentAsHTML() : toot_content.getText().toString();
2018-02-18 19:08:24 +01:00
2019-07-30 11:48:37 +02:00
2020-04-08 12:42:15 +02:00
Helper.loadGiF(TootActivity.this, account, pp_actionBar);
2019-05-30 15:19:02 +02:00
if (sharedContent != null) { //Shared content
2019-05-30 15:19:02 +02:00
if (sharedSubject != null) {
sharedContent = sharedSubject + "\n\n" + sharedContent;
}
2019-05-30 15:19:02 +02:00
if (b != null) {
2018-08-16 12:08:38 +02:00
final String image = b.getString("image");
String title = b.getString("title");
String description = b.getString("description");
if (description != null && description.length() > 0) {
if (sharedContentIni.startsWith("www."))
sharedContentIni = "http://" + sharedContentIni;
if (title != null && title.length() > 0)
sharedContent = title + "\n\n" + description + "\n\n" + sharedContentIni;
else
sharedContent = description + "\n\n" + sharedContentIni;
int selectionBefore = toot_content.getSelectionStart();
toot_content.setText(sharedContent);
if (selectionBefore >= 0 && selectionBefore < toot_content.length())
toot_content.setSelection(selectionBefore);
2019-07-31 19:14:42 +02:00
toot_space_left.setText(String.valueOf(countLength(social, toot_content, toot_cw_content)));
}
2018-08-16 12:08:38 +02:00
if (image != null) {
2019-05-23 07:46:34 +02:00
new HttpsConnection(TootActivity.this, instance).download(image, TootActivity.this);
2018-08-16 12:08:38 +02:00
}
int selectionBefore = toot_content.getSelectionStart();
toot_content.setText(String.format("\n%s", sharedContent));
2019-11-15 16:32:25 +01:00
if (selectionBefore >= 0 && selectionBefore < toot_content.length())
toot_content.setSelection(selectionBefore);
2019-07-31 19:14:42 +02:00
toot_space_left.setText(String.valueOf(countLength(social, toot_content, toot_cw_content)));
2017-10-27 15:34:53 +02:00
}
2019-11-15 16:32:25 +01:00
}
2017-10-27 15:34:53 +02:00
2019-06-27 15:17:18 +02:00
2019-11-15 16:32:25 +01:00
attachments = new ArrayList<>();
2019-06-27 15:17:18 +02:00
2019-11-15 16:32:25 +01:00
if (!sharedUri.isEmpty()) {
uploadSharedImage(sharedUri);
}
2019-06-27 15:17:18 +02:00
2019-11-15 16:32:25 +01:00
if (tootReply == null) {
if (visibility == null) {
String defaultVisibility = account.isLocked() ? "private" : "public";
visibility = sharedpreferences.getString(Helper.SET_TOOT_VISIBILITY + "@" + account.getAcct() + "@" + account.getInstance(), defaultVisibility);
}
switch (visibility) {
case "public":
toot_visibility.setImageResource(R.drawable.ic_public_toot);
break;
case "unlisted":
toot_visibility.setImageResource(R.drawable.ic_lock_open_toot);
break;
case "private":
toot_visibility.setImageResource(R.drawable.ic_lock_outline_toot);
break;
case "direct":
toot_visibility.setImageResource(R.drawable.ic_mail_outline_toot);
break;
}
2019-06-27 15:17:18 +02:00
}
2020-03-08 10:29:06 +01:00
toot_sensitive.setOnCheckedChangeListener((buttonView, isChecked) -> isSensitive = isChecked);
2017-10-27 15:34:53 +02:00
2019-07-31 19:14:42 +02:00
toot_space_left.setText(String.valueOf(countLength(social, toot_content, toot_cw_content)));
2020-03-08 10:29:06 +01:00
toot_cw.setOnClickListener(v -> {
if (toot_cw_content.getVisibility() == View.GONE) {
toot_cw_content.setVisibility(View.VISIBLE);
toot_cw_content.requestFocus();
} else {
toot_cw_content.setVisibility(View.GONE);
toot_cw_content.setText("");
toot_content.requestFocus();
2019-05-28 19:41:41 +02:00
}
2019-11-15 16:32:25 +01:00
});
2019-05-28 19:41:41 +02:00
2020-03-08 10:29:06 +01:00
toot_visibility.setOnClickListener(v -> tootVisibilityDialog());
2019-05-28 19:41:41 +02:00
2020-03-08 10:29:06 +01:00
toot_it.setOnClickListener(v -> {
if (!displayWYSIWYG()) {
sendToot(null, null);
} else {
sendToot(null, "text/html");
2019-11-15 16:32:25 +01:00
}
});
2019-08-19 19:25:42 +02:00
2019-11-15 16:32:25 +01:00
if (social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA && !displayWYSIWYG())
2020-03-08 10:29:06 +01:00
toot_it.setOnLongClickListener(v -> {
PopupMenu popup = new PopupMenu(TootActivity.this, toot_it);
popup.getMenuInflater()
.inflate(R.menu.main_content_type, popup.getMenu());
popup.setOnMenuItemClickListener(item -> {
switch (item.getItemId()) {
case R.id.action_plain_text:
contentType = "text/plain";
break;
case R.id.action_html:
contentType = "text/html";
break;
case R.id.action_markdown:
contentType = "text/markdown";
break;
case R.id.action_bbcode:
contentType = "text/bbcode";
break;
}
popup.dismiss();
sendToot(null, contentType);
2019-11-15 16:32:25 +01:00
return false;
2020-03-08 10:29:06 +01:00
});
popup.show();
return false;
2019-11-15 16:32:25 +01:00
});
2019-05-28 19:41:41 +02:00
2020-03-08 10:29:06 +01:00
toot_picture.setOnClickListener(v -> {
2019-05-28 19:41:41 +02:00
2020-03-08 10:29:06 +01:00
if (ContextCompat.checkSelfPermission(TootActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(TootActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
return;
}
Intent intent;
intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
String[] mimetypes = {"image/*", "video/*", "audio/mpeg", "audio/opus", "audio/flac", "audio/wav", "audio/ogg"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
startActivityForResult(intent, PICK_IMAGE);
} else {
intent.setType("image/* video/* audio/mpeg audio/opus audio/flac audio/wav audio/ogg");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
Intent pickIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Intent chooserIntent = Intent.createChooser(intent, getString(R.string.toot_select_image));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{pickIntent});
startActivityForResult(chooserIntent, PICK_IMAGE);
2019-05-28 19:41:41 +02:00
}
2020-03-08 10:29:06 +01:00
2019-11-15 16:32:25 +01:00
});
2017-05-05 16:36:04 +02:00
2019-05-30 16:37:49 +02:00
2019-11-15 16:32:25 +01:00
toot_cw_content.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
2019-07-22 15:54:24 +02:00
}
2019-06-06 15:42:24 +02:00
2019-11-15 16:32:25 +01:00
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
2019-07-24 18:54:57 +02:00
2019-11-15 16:32:25 +01:00
@Override
public void afterTextChanged(Editable s) {
toot_space_left.setText(String.valueOf(countLength(social, toot_content, toot_cw_content)));
2019-07-24 18:54:57 +02:00
}
2019-11-15 16:32:25 +01:00
});
2020-04-08 12:42:15 +02:00
TextWatcher textWatcher = initializeTextWatcher(TootActivity.this, social, null, toot_content, toot_cw_content, toot_space_left, pp_actionBar, pp_progress, TootActivity.this, TootActivity.this, TootActivity.this);
2019-11-15 16:32:25 +01:00
if (social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON || social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA)
toot_content.addTextChangedListener(textWatcher);
if (scheduledstatus != null)
restoreServerSchedule(scheduledstatus.getStatus());
if (restored != -1) {
restoreToot(restored);
2019-07-24 18:54:57 +02:00
}
2020-03-08 10:29:06 +01:00
poll_action.setOnClickListener(v -> displayPollPopup());
2019-11-15 16:32:25 +01:00
toot_space_left.setText(String.valueOf(countLength(social, toot_content, toot_cw_content)));
LocalBroadcastManager.getInstance(this)
.registerReceiver(imageReceiver,
new IntentFilter(Helper.INTENT_SEND_MODIFIED_IMAGE));
uploadReceiver = new UploadServiceSingleBroadcastReceiver(this);
uploadReceiver.register(this);
LocalBroadcastManager.getInstance(this)
.registerReceiver(add_new_media,
new IntentFilter(Helper.INTENT_ADD_UPLOADED_MEDIA));
}
2019-07-24 18:54:57 +02:00
2019-09-06 17:55:14 +02:00
private void addNewMedia(JSONObject response, ArrayList<String> successfullyUploadedFiles) {
2019-07-24 18:54:57 +02:00
Attachment attachment;
//response = new JSONObject(serverResponse.getBodyAsString());
2020-03-08 10:29:06 +01:00
if (response == null) {
2019-11-26 18:29:20 +01:00
Toasty.error(TootActivity.this, getString(R.string.toast_error), Toasty.LENGTH_SHORT).show();
return;
}
if (social != UpdateAccountInfoAsyncTask.SOCIAL.GNU && social != UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA) {
2019-07-24 18:54:57 +02:00
attachment = API.parseAttachmentResponse(response);
2019-11-26 18:29:20 +01:00
} else {
2019-07-24 18:54:57 +02:00
attachment = GNUAPI.parseUploadedAttachmentResponse(response);
2019-11-26 18:29:20 +01:00
}
2019-07-24 18:54:57 +02:00
boolean alreadyAdded = false;
int index = 0;
2019-11-15 16:32:25 +01:00
if (attachments == null) {
2019-10-06 20:27:30 +02:00
attachments = new ArrayList<>();
}
2019-09-06 17:55:14 +02:00
for (Attachment attach_ : attachments) {
if (attach_.getId().equals(attachment.getId())) {
2019-07-24 18:54:57 +02:00
alreadyAdded = true;
break;
}
index++;
}
File audioFile = new File(getCacheDir() + "/fedilab_recorded_audio.wav");
2020-03-08 10:29:06 +01:00
//noinspection ResultOfMethodCallIgnored
2019-07-24 18:54:57 +02:00
audioFile.delete();
2019-09-06 17:55:14 +02:00
if (!alreadyAdded) {
2019-07-24 18:54:57 +02:00
toot_picture_container.setVisibility(View.VISIBLE);
String url = attachment.getPreview_url();
if (url == null || url.trim().equals(""))
url = attachment.getUrl();
2020-04-08 12:42:15 +02:00
final ImageView imageView = new ImageView(TootActivity.this);
2019-07-24 18:54:57 +02:00
imageView.setId(Integer.parseInt(attachment.getId()));
2019-09-06 17:55:14 +02:00
if (social == UpdateAccountInfoAsyncTask.SOCIAL.GNU || social == UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA) {
if (successfullyUploadedFiles != null && successfullyUploadedFiles.size() > 0) {
2019-07-24 18:54:57 +02:00
Iterator it = filesMap.entrySet().iterator();
Uri fileName = null;
while (it.hasNext()) {
2019-09-06 17:55:14 +02:00
Map.Entry pair = (Map.Entry) it.next();
2019-07-24 18:54:57 +02:00
fileName = (Uri) pair.getValue();
it.remove();
}
2019-09-06 17:55:14 +02:00
if (fileName != null) {
2020-05-23 17:29:12 +02:00
if (isValidContextForGlide(imageView.getContext())) {
Glide.with(imageView.getContext())
.asBitmap()
.load(fileName)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
imageView.setImageBitmap(resource);
}
2020-04-09 14:59:36 +02:00
2020-05-23 17:29:12 +02:00
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
2020-04-09 14:59:36 +02:00
2020-05-23 17:29:12 +02:00
}
});
}
2019-07-24 18:54:57 +02:00
}
}
2019-09-06 17:55:14 +02:00
} else {
2020-05-23 17:29:12 +02:00
if (isValidContextForGlide(imageView.getContext())) {
String finalUrl = url;
Glide.with(imageView.getContext())
.asBitmap()
.load(url)
.error(Glide.with(imageView).asBitmap().load(R.drawable.ic_audio_wave))
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
imageView.setImageBitmap(resource);
if (displayWYSIWYG()) {
url_for_media = finalUrl;
Iterator it = filesMap.entrySet().iterator();
String fileName = null;
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
fileName = (String) pair.getKey();
it.remove();
}
if (fileName != null && fileName.contains("fedilabins_")) {
wysiwyg.insertImage(resource);
}
2019-07-24 18:54:57 +02:00
}
}
2020-04-09 14:59:36 +02:00
2020-05-23 17:29:12 +02:00
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
2020-04-09 14:59:36 +02:00
2020-05-23 17:29:12 +02:00
}
});
}
2019-07-24 18:54:57 +02:00
}
2019-07-24 18:54:57 +02:00
LinearLayout.LayoutParams imParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
imParams.setMargins(20, 5, 20, 5);
2020-04-08 12:42:15 +02:00
imParams.height = (int) Helper.convertDpToPixel(100, TootActivity.this);
2019-07-24 18:54:57 +02:00
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
boolean show_media_urls = sharedpreferences.getBoolean(Helper.SET_MEDIA_URLS, false);
if (show_media_urls && !displayWYSIWYG()) {
//Adds the shorter text_url of attachment at the end of the toot
int selectionBefore = toot_content.getSelectionStart();
2019-09-06 17:55:14 +02:00
toot_content.setText(String.format("%s\n\n%s", toot_content.getText().toString(), attachment.getText_url()));
2019-07-31 19:14:42 +02:00
toot_space_left.setText(String.valueOf(countLength(social, toot_content, toot_cw_content)));
2019-07-24 18:54:57 +02:00
//Moves the cursor
toot_content.setSelection(selectionBefore);
}
imageView.setTag(attachment.getId());
toot_picture_container.addView(imageView, attachments.size(), imParams);
2020-03-08 10:29:06 +01:00
imageView.setOnLongClickListener(view -> {
showRemove(imageView.getId());
return false;
2019-07-24 18:54:57 +02:00
});
2020-03-12 18:09:41 +01:00
imageView.setOnClickListener(view -> showAddDescription(attachment));
2019-07-24 18:54:57 +02:00
attachments.add(attachment);
addBorder();
if (attachments.size() < max_media_count)
toot_picture.setEnabled(true);
toot_it.setEnabled(true);
toot_sensitive.setVisibility(View.VISIBLE);
2019-09-06 17:55:14 +02:00
if (account.isSensitive()) {
2019-07-24 18:54:57 +02:00
toot_sensitive.setChecked(true);
}
picture_scrollview.setVisibility(View.VISIBLE);
2019-09-06 17:55:14 +02:00
} else {
if (attachments.size() > index && attachment.getDescription() != null) {
2019-07-24 18:54:57 +02:00
attachments.get(index).setDescription(attachment.getDescription());
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
2019-11-15 16:32:25 +01:00
@NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// We have the permission.
toot_picture.callOnClick();
}
break;
}
2019-06-26 11:00:59 +02:00
case REQUEST_CAMERA_PERMISSION_RESULT: {
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
recordAudio();
}
}
}
}
2019-05-30 15:19:02 +02:00
public void showAToast(String message) {
if (mToast != null) {
mToast.cancel();
}
2020-04-08 12:42:15 +02:00
mToast = Toasty.error(TootActivity.this, message, Toast.LENGTH_SHORT);
mToast.show();
}
2017-05-05 16:36:04 +02:00
// Handles uploading shared images
2018-01-10 16:00:57 +01:00
public void uploadSharedImage(ArrayList<Uri> uri) {
if (!uri.isEmpty()) {
int count = 0;
2019-05-30 15:19:02 +02:00
for (Uri fileUri : uri) {
if (fileUri != null) {
if (count == max_media_count) {
break;
}
picture_scrollview.setVisibility(View.VISIBLE);
try {
2019-07-22 15:54:24 +02:00
prepareUpload(TootActivity.this, fileUri, null, uploadReceiver);
count++;
2018-01-10 16:00:57 +01:00
} catch (Exception e) {
e.printStackTrace();
2020-04-08 12:42:15 +02:00
Toasty.error(TootActivity.this, getString(R.string.toot_select_image_error), Toast.LENGTH_LONG).show();
toot_picture.setEnabled(true);
toot_it.setEnabled(true);
}
} else {
2020-04-08 12:42:15 +02:00
Toasty.error(TootActivity.this, getString(R.string.toot_select_image_error), Toast.LENGTH_LONG).show();
}
}
}
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
try {
photoFile = createImageFile();
2018-11-25 10:45:16 +01:00
} catch (IOException ignored) {
2020-04-08 12:42:15 +02:00
Toasty.error(TootActivity.this, getString(R.string.toot_select_image_error), Toast.LENGTH_LONG).show();
2019-05-30 15:19:02 +02:00
}
// Continue only if the File was successfully created
if (photoFile != null) {
2019-09-06 17:55:14 +02:00
if (BuildConfig.DONATIONS) {
photoFileUri = FileProvider.getUriForFile(this,
"fr.gouv.etalab.mastodon.fileProvider",
photoFile);
2019-09-06 17:55:14 +02:00
} else {
photoFileUri = FileProvider.getUriForFile(this,
"app.fedilab.android.fileProvider",
photoFile);
}
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoFileUri);
startActivityForResult(takePictureIntent, TAKE_PHOTO);
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
2017-05-05 16:36:04 +02:00
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
2019-05-31 18:20:54 +02:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
boolean photo_editor = sharedpreferences.getBoolean(Helper.SET_PHOTO_EDITOR, true);
2019-05-18 11:10:30 +02:00
if (requestCode == PICK_IMAGE && resultCode == RESULT_OK) {
2019-05-30 15:19:02 +02:00
if (data == null) {
2020-04-08 12:42:15 +02:00
Toasty.error(TootActivity.this, getString(R.string.toot_select_image_error), Toast.LENGTH_LONG).show();
2017-05-05 16:36:04 +02:00
return;
}
ClipData clipData = data.getClipData();
if (data.getData() == null && clipData == null) {
2020-04-08 12:42:15 +02:00
Toasty.error(TootActivity.this, getString(R.string.toot_select_image_error), Toast.LENGTH_LONG).show();
return;
}
2020-04-11 18:14:20 +02:00
2019-05-30 15:19:02 +02:00
if (clipData != null) {
ArrayList<Uri> mArrayUri = new ArrayList<>();
for (int i = 0; i < clipData.getItemCount(); i++) {
ClipData.Item item = clipData.getItemAt(i);
Uri uri = item.getUri();
mArrayUri.add(uri);
}
uploadSharedImage(mArrayUri);
2019-05-30 15:19:02 +02:00
} else {
2019-06-05 18:23:44 +02:00
String filename = Helper.getFileName(TootActivity.this, data.getData());
ContentResolver cr = getContentResolver();
String mime = cr.getType(data.getData());
if (mime != null && (mime.toLowerCase().contains("video") || mime.toLowerCase().contains("gif"))) {
2019-09-06 17:55:14 +02:00
prepareUpload(TootActivity.this, data.getData(), filename, uploadReceiver);
2019-06-05 18:23:44 +02:00
} else if (mime != null && mime.toLowerCase().contains("image")) {
2019-09-06 17:55:14 +02:00
if (photo_editor) {
2019-06-05 18:23:44 +02:00
Intent intent = new Intent(TootActivity.this, PhotoEditorActivity.class);
Bundle b = new Bundle();
intent.putExtra("imageUri", data.getData().toString());
intent.putExtras(b);
startActivity(intent);
2019-09-06 17:55:14 +02:00
} else {
prepareUpload(TootActivity.this, data.getData(), filename, uploadReceiver);
}
2019-09-06 17:55:14 +02:00
} else if (mime != null && mime.toLowerCase().contains("audio")) {
prepareUpload(TootActivity.this, data.getData(), filename, uploadReceiver);
} else {
2020-04-08 12:42:15 +02:00
Toasty.error(TootActivity.this, getString(R.string.toot_select_image_error), Toast.LENGTH_LONG).show();
}
2017-05-05 16:36:04 +02:00
}
2019-11-15 16:32:25 +01:00
} else if (requestCode == SEND_VOICE_MESSAGE && resultCode == RESULT_OK) {
Uri uri = Uri.fromFile(new File(getCacheDir() + "/fedilab_recorded_audio.wav"));
prepareUpload(TootActivity.this, uri, "fedilab_recorded_audio.wav", uploadReceiver);
} else if (requestCode == TAKE_PHOTO && resultCode == RESULT_OK) {
if (photo_editor) {
Intent intent = new Intent(TootActivity.this, PhotoEditorActivity.class);
Bundle b = new Bundle();
intent.putExtra("imageUri", photoFileUri.toString());
intent.putExtras(b);
startActivity(intent);
2019-09-06 17:55:14 +02:00
} else {
2019-11-15 16:32:25 +01:00
prepareUpload(TootActivity.this, photoFileUri, null, uploadReceiver);
2019-07-22 15:54:24 +02:00
}
2019-11-15 16:32:25 +01:00
} else if (requestCode == wysiwyg.PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
String filename = Helper.getFileName(TootActivity.this, data.getData());
prepareUpload(TootActivity.this, data.getData(), "fedilabins_" + filename, uploadReceiver);
2019-07-22 15:54:24 +02:00
}
2019-11-15 16:32:25 +01:00
}
2019-07-22 15:54:24 +02:00
2019-11-15 16:32:25 +01:00
private void prepareUpload(Activity activity, android.net.Uri uri, String filename, UploadServiceSingleBroadcastReceiver uploadReceiver) {
if (uploadReceiver == null) {
uploadReceiver = new UploadServiceSingleBroadcastReceiver(TootActivity.this);
uploadReceiver.register(this);
}
2019-11-15 16:32:25 +01:00
new asyncPicture(activity, account, social, uri, filename, uploadReceiver).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
2019-01-20 15:20:07 +01:00
@Override
public void onPostAction(int statusCode, API.StatusAction statusAction, String userId, Error error) {
2019-09-06 17:55:14 +02:00
if (error != null) {
2020-04-08 12:42:15 +02:00
Toasty.error(TootActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show();
2019-09-06 17:55:14 +02:00
} else {
2020-04-08 12:42:15 +02:00
Toasty.success(TootActivity.this, getString(R.string.toot_scheduled), Toast.LENGTH_LONG).show();
2019-01-20 15:20:07 +01:00
resetForNextToot();
}
}
2019-06-06 15:42:24 +02:00
@Override
public void onProgress(Context context, UploadInfo uploadInfo) {
// your code here
}
@Override
public void onError(Context context, UploadInfo uploadInfo, ServerResponse serverResponse,
Exception exception) {
2020-04-08 12:42:15 +02:00
Toasty.error(TootActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show();
2019-09-06 17:55:14 +02:00
if (attachments.size() == 0)
2019-06-06 15:42:24 +02:00
toot_picture_container.setVisibility(View.GONE);
toot_picture.setEnabled(true);
toot_it.setEnabled(true);
}
@Override
public void onCompleted(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) {
JSONObject response = null;
try {
response = new JSONObject(serverResponse.getBodyAsString());
} catch (JSONException e) {
e.printStackTrace();
}
2019-07-24 18:54:57 +02:00
addNewMedia(response, uploadInfo.getSuccessfullyUploadedFiles());
2019-06-06 15:42:24 +02:00
}
@Override
public void onCancelled(Context context, UploadInfo uploadInfo) {
// your code here
}
@Override
public void onRetrieveRelationship(Relationship relationship, Error error) {
2019-09-06 17:55:14 +02:00
if (error != null) {
return;
}
2019-09-06 17:55:14 +02:00
if (relationship != null && relationship.isBlocked_by()) {
warning_message.setVisibility(View.VISIBLE);
}
}
2018-09-01 18:24:01 +02:00
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Bundle extras = intent.getExtras();
2018-09-02 11:31:12 +02:00
if (extras != null && extras.getString("imageUri") != null) {
2018-09-01 18:24:01 +02:00
Uri imageUri = Uri.parse(extras.getString("imageUri"));
2019-09-06 17:55:14 +02:00
if (imageUri == null) {
2020-04-08 12:42:15 +02:00
Toasty.error(TootActivity.this, getString(R.string.toot_select_image_error), Toast.LENGTH_LONG).show();
2018-09-01 18:24:01 +02:00
return;
}
2019-09-06 17:55:14 +02:00
String filename = Helper.getFileName(TootActivity.this, imageUri);
2019-07-22 15:54:24 +02:00
2019-09-06 17:55:14 +02:00
prepareUpload(TootActivity.this, imageUri, filename, uploadReceiver);
2018-09-01 18:24:01 +02:00
}
}
2020-03-08 10:29:06 +01:00
@SuppressLint("ClickableViewAccessibility")
2017-05-05 16:36:04 +02:00
@Override
2019-11-20 16:25:44 +01:00
public boolean onOptionsItemSelected(@NotNull MenuItem item) {
2020-04-09 18:57:12 +02:00
final SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2019-01-23 19:23:26 +01:00
int style;
2019-05-18 11:10:30 +02:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
2019-01-23 19:23:26 +01:00
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
if (theme == Helper.THEME_DARK) {
style = R.style.DialogDark;
2019-09-06 17:55:14 +02:00
} else if (theme == Helper.THEME_BLACK) {
2019-01-23 19:23:26 +01:00
style = R.style.DialogBlack;
2019-09-06 17:55:14 +02:00
} else {
2019-01-23 19:23:26 +01:00
style = R.style.Dialog;
}
2017-05-05 16:36:04 +02:00
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
case R.id.action_view_reply:
2018-11-03 14:45:55 +01:00
AlertDialog.Builder alert = new AlertDialog.Builder(TootActivity.this, style);
alert.setTitle(R.string.toot_reply_content_title);
final TextView input = new TextView(TootActivity.this);
//Set the padding
input.setPadding(30, 30, 30, 30);
alert.setView(input);
String content = tootReply.getContent();
2019-09-06 17:55:14 +02:00
if (tootReply.getReblog() != null)
content = tootReply.getReblog().getContent();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
input.setText(Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY));
else
input.setText(Html.fromHtml(content));
2020-03-08 10:29:06 +01:00
alert.setPositiveButton(R.string.close, (dialog, whichButton) -> dialog.dismiss());
alert.setNegativeButton(R.string.accounts, (dialog, whichButton) -> {
2020-04-08 12:42:15 +02:00
new RetrieveAccountsForReplyAsyncTask(TootActivity.this, tootReply.getReblog() != null ? tootReply.getReblog() : tootReply, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2020-03-08 10:29:06 +01:00
dialog.dismiss();
2017-10-25 07:53:31 +02:00
});
alert.show();
2017-10-31 12:47:51 +01:00
return true;
2019-03-23 17:23:02 +01:00
case R.id.action_poll:
2019-03-24 19:06:03 +01:00
displayPollPopup();
2019-03-24 16:38:12 +01:00
return false;
2017-10-31 12:47:51 +01:00
case R.id.action_translate:
2018-08-23 09:41:01 +02:00
final CountryPicker picker = CountryPicker.newInstance(getString(R.string.which_language)); // dialog title
2019-09-06 17:55:14 +02:00
if (theme == Helper.THEME_LIGHT) {
2019-11-07 20:23:07 +01:00
picker.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.Dialog);
2019-09-06 17:55:14 +02:00
} else {
2019-11-07 20:23:07 +01:00
picker.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogDark);
2017-10-31 18:29:04 +01:00
}
2019-09-06 17:55:14 +02:00
if (toot_content.getText().length() == 0 && toot_cw_content.getText().length() == 0)
return true;
2017-11-01 13:27:40 +01:00
String dateString = sharedpreferences.getString(Helper.LAST_TRANSLATION_TIME, null);
2019-09-06 17:55:14 +02:00
if (dateString != null) {
2020-04-08 12:42:15 +02:00
Date dateCompare = Helper.stringToDate(TootActivity.this, dateString);
2017-11-01 13:58:12 +01:00
Date date = new Date();
2019-09-06 17:55:14 +02:00
if (date.before(dateCompare)) {
2020-04-08 12:42:15 +02:00
Toasty.info(TootActivity.this, getString(R.string.please_wait), Toast.LENGTH_SHORT).show();
2017-11-01 13:27:40 +01:00
return true;
2017-11-01 13:58:12 +01:00
}
2017-11-01 13:27:40 +01:00
}
2020-03-08 10:29:06 +01:00
picker.setListener((name, locale, flagDrawableResID) -> {
picker.dismiss();
AlertDialog.Builder transAlert = new AlertDialog.Builder(TootActivity.this, style);
transAlert.setTitle(R.string.translate_toot);
2020-04-08 12:42:15 +02:00
popup_trans = getLayoutInflater().inflate(R.layout.popup_translate, new LinearLayout(TootActivity.this), false);
2020-03-08 10:29:06 +01:00
transAlert.setView(popup_trans);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_TRANSLATION_TIME, Helper.dateToString(new Date(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(Helper.SECONDES_BETWEEN_TRANSLATE))));
editor.apply();
TextView yandex_translate = popup_trans.findViewById(R.id.yandex_translate);
yandex_translate.setOnClickListener(v -> {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://translate.yandex.com/"));
startActivity(browserIntent);
});
MyTransL myTransL = MyTransL.getInstance(MyTransL.translatorEngine.YANDEX);
myTransL.setYandexAPIKey(Helper.YANDEX_KEY);
myTransL.setObfuscation(true);
myTransL.setTimeout(60);
if (toot_cw_content.getText().toString().length() > 0)
myTransL.translate(toot_cw_content.getText().toString(), locale, new com.github.stom79.mytransl.client.Results() {
2017-11-01 13:58:12 +01:00
@Override
2020-03-08 10:29:06 +01:00
public void onSuccess(Translate translate) {
try {
if (translate.getTranslatedContent() == null)
return;
if (popup_trans != null) {
ProgressBar trans_progress_cw = popup_trans.findViewById(R.id.trans_progress_cw);
ProgressBar trans_progress_toot = popup_trans.findViewById(R.id.trans_progress_toot);
if (trans_progress_cw != null)
trans_progress_cw.setVisibility(View.GONE);
LinearLayout trans_container = popup_trans.findViewById(R.id.trans_container);
if (trans_container != null) {
TextView cw_trans = popup_trans.findViewById(R.id.cw_trans);
if (cw_trans != null) {
cw_trans.setVisibility(View.VISIBLE);
cw_trans.setText(translate.getTranslatedContent());
2017-11-29 15:06:30 +01:00
}
2020-03-08 10:29:06 +01:00
} else {
2020-04-08 12:42:15 +02:00
Toasty.error(TootActivity.this, getString(R.string.toast_error_translate), Toast.LENGTH_LONG).show();
2017-11-29 15:06:30 +01:00
}
2020-03-08 10:29:06 +01:00
if (trans_progress_cw != null && trans_progress_toot != null && trans_progress_cw.getVisibility() == View.GONE && trans_progress_toot.getVisibility() == View.GONE)
if (dialogTrans.getButton(DialogInterface.BUTTON_NEGATIVE) != null)
dialogTrans.getButton(DialogInterface.BUTTON_NEGATIVE).setEnabled(true);
2017-11-29 15:06:30 +01:00
}
2020-03-08 10:29:06 +01:00
} catch (IllegalArgumentException e) {
2020-04-08 12:42:15 +02:00
Toasty.error(TootActivity.this, getString(R.string.toast_error_translate), Toast.LENGTH_LONG).show();
2017-11-29 15:06:30 +01:00
}
2020-03-08 10:29:06 +01:00
}
2017-11-29 15:06:30 +01:00
2020-03-08 10:29:06 +01:00
@Override
public void onFail(HttpsConnectionException e) {
e.printStackTrace();
2017-10-31 12:47:51 +01:00
}
});
2020-03-08 10:29:06 +01:00
else {
ProgressBar trans_progress_cw = popup_trans.findViewById(R.id.trans_progress_cw);
trans_progress_cw.setVisibility(View.GONE);
}
if (toot_content.getText().toString().length() > 0)
myTransL.translate(toot_content.getText().toString(), locale, new com.github.stom79.mytransl.client.Results() {
@Override
public void onSuccess(Translate translate) {
try {
if (translate.getTranslatedContent() == null)
return;
if (popup_trans != null) {
ProgressBar trans_progress_cw = popup_trans.findViewById(R.id.trans_progress_cw);
ProgressBar trans_progress_toot = popup_trans.findViewById(R.id.trans_progress_toot);
if (trans_progress_toot != null)
trans_progress_toot.setVisibility(View.GONE);
LinearLayout trans_container = popup_trans.findViewById(R.id.trans_container);
if (trans_container != null) {
TextView toot_trans = popup_trans.findViewById(R.id.toot_trans);
if (toot_trans != null) {
toot_trans.setVisibility(View.VISIBLE);
toot_trans.setText(translate.getTranslatedContent());
}
} else {
2020-04-08 12:42:15 +02:00
Toasty.error(TootActivity.this, getString(R.string.toast_error_translate), Toast.LENGTH_LONG).show();
2020-03-08 10:29:06 +01:00
}
if (trans_progress_cw != null && trans_progress_toot != null && trans_progress_cw.getVisibility() == View.GONE && trans_progress_toot.getVisibility() == View.GONE)
if (dialogTrans.getButton(DialogInterface.BUTTON_NEGATIVE) != null)
dialogTrans.getButton(DialogInterface.BUTTON_NEGATIVE).setEnabled(true);
}
} catch (IllegalArgumentException e) {
2020-04-08 12:42:15 +02:00
Toasty.error(TootActivity.this, getString(R.string.toast_error_translate), Toast.LENGTH_LONG).show();
2017-10-31 18:06:01 +01:00
}
2017-10-31 12:47:51 +01:00
}
2017-10-31 12:47:51 +01:00
@Override
2020-03-08 10:29:06 +01:00
public void onFail(HttpsConnectionException e) {
e.printStackTrace();
2017-10-31 12:47:51 +01:00
}
});
2017-10-31 15:57:19 +01:00
2020-03-08 10:29:06 +01:00
transAlert.setPositiveButton(R.string.close, (dialog, whichButton) -> dialog.dismiss());
transAlert.setNegativeButton(R.string.validate, (dialog, whichButton) -> {
TextView toot_trans = popup_trans.findViewById(R.id.toot_trans);
TextView cw_trans = popup_trans.findViewById(R.id.cw_trans);
if (toot_trans != null) {
toot_content.setText(toot_trans.getText().toString());
toot_content.setSelection(toot_content.getText().length());
}
if (cw_trans != null)
toot_cw_content.setText(cw_trans.getText().toString());
dialog.dismiss();
});
dialogTrans = transAlert.create();
transAlert.show();
dialogTrans.setOnShowListener(dialog -> {
Button negativeButton = ((AlertDialog) dialog)
.getButton(AlertDialog.BUTTON_NEGATIVE);
if (negativeButton != null)
negativeButton.setEnabled(false);
});
2017-10-31 12:47:51 +01:00
});
picker.show(getSupportFragmentManager(), "COUNTRY_PICKER");
2017-11-03 13:55:18 +01:00
return true;
case R.id.action_emoji:
2020-03-08 10:29:06 +01:00
if (emojis != null) {
2019-11-16 18:36:08 +01:00
emojis.clear();
emojis = null;
}
2020-04-08 12:42:15 +02:00
emojis = new CustomEmojiDAO(TootActivity.this, db).getAllEmojis(account.getInstance());
2018-11-03 14:45:55 +01:00
final AlertDialog.Builder builder = new AlertDialog.Builder(this, style);
2017-11-03 13:55:18 +01:00
int paddingPixel = 15;
float density = getResources().getDisplayMetrics().density;
2019-09-06 17:55:14 +02:00
int paddingDp = (int) (paddingPixel * density);
2020-03-08 10:29:06 +01:00
builder.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss());
2017-11-03 13:55:18 +01:00
builder.setTitle(R.string.insert_emoji);
2019-09-06 17:55:14 +02:00
if (emojis != null && emojis.size() > 0) {
2017-11-03 13:55:18 +01:00
GridView gridView = new GridView(TootActivity.this);
2019-11-06 11:56:23 +01:00
gridView.setAdapter(new CustomEmojiAdapter(emojis));
2017-11-03 13:55:18 +01:00
gridView.setNumColumns(5);
2020-03-08 10:29:06 +01:00
gridView.setOnItemClickListener((parent, view, position, id) -> {
toot_content.getText().insert(toot_content.getSelectionStart(), " :" + emojis.get(position).getShortcode() + ": ");
alertDialogEmoji.dismiss();
2017-11-03 13:55:18 +01:00
});
2019-09-06 17:55:14 +02:00
gridView.setPadding(paddingDp, paddingDp, paddingDp, paddingDp);
2017-11-03 13:55:18 +01:00
builder.setView(gridView);
2019-09-06 17:55:14 +02:00
} else {
2017-11-03 13:55:18 +01:00
TextView textView = new TextView(TootActivity.this);
textView.setText(getString(R.string.no_emoji));
2019-09-06 17:55:14 +02:00
textView.setPadding(paddingDp, paddingDp, paddingDp, paddingDp);
2017-11-03 13:55:18 +01:00
builder.setView(textView);
}
alertDialogEmoji = builder.show();
return true;
case R.id.action_photo_camera:
dispatchTakePictureIntent();
2019-01-23 19:23:26 +01:00
return true;
case R.id.action_contacts:
AlertDialog.Builder builderSingle = new AlertDialog.Builder(TootActivity.this, style);
builderSingle.setTitle(getString(R.string.select_accounts));
LayoutInflater inflater = getLayoutInflater();
2020-04-08 12:42:15 +02:00
View dialogView = inflater.inflate(R.layout.popup_contact, new LinearLayout(TootActivity.this), false);
2019-01-23 19:23:26 +01:00
loader = dialogView.findViewById(R.id.loader);
EditText search_account = dialogView.findViewById(R.id.search_account);
lv_accounts_search = dialogView.findViewById(R.id.lv_accounts_search);
loader.setVisibility(View.VISIBLE);
2019-09-06 17:55:14 +02:00
new RetrieveSearchAccountsAsyncTask(TootActivity.this, "a", true, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2019-01-23 19:23:26 +01:00
search_account.addTextChangedListener(new TextWatcher() {
@Override
2019-09-06 17:55:14 +02:00
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
2019-01-23 19:23:26 +01:00
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count > 0) {
search_account.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_close, 0);
2019-09-06 17:55:14 +02:00
} else {
2019-01-23 19:23:26 +01:00
search_account.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_search, 0);
}
}
2019-09-06 17:55:14 +02:00
2019-01-23 19:23:26 +01:00
@Override
public void afterTextChanged(Editable s) {
2019-09-06 17:55:14 +02:00
if (s != null && s.length() > 0) {
new RetrieveSearchAccountsAsyncTask(TootActivity.this, s.toString(), true, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2019-01-23 19:23:26 +01:00
}
}
});
2020-03-08 10:29:06 +01:00
search_account.setOnTouchListener((v, event) -> {
final int DRAWABLE_RIGHT = 2;
if (event.getAction() == MotionEvent.ACTION_UP) {
if (search_account.length() > 0 && event.getRawX() >= (search_account.getRight() - search_account.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
search_account.setText("");
new RetrieveSearchAccountsAsyncTask(TootActivity.this, "a", true, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2019-01-23 19:23:26 +01:00
}
}
2020-03-08 10:29:06 +01:00
return false;
2019-01-23 19:23:26 +01:00
});
builderSingle.setView(dialogView);
2020-03-08 10:29:06 +01:00
builderSingle.setNegativeButton(R.string.validate, (dialog, which) -> {
dialog.dismiss();
toot_content.setSelection(toot_content.getText().length());
2019-01-23 19:23:26 +01:00
});
builderSingle.show();
return true;
case R.id.action_microphone:
2019-06-26 11:00:59 +02:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) ==
PackageManager.PERMISSION_GRANTED) {
recordAudio();
} else {
if (shouldShowRequestPermissionRationale(Manifest.permission.RECORD_AUDIO)) {
Toast.makeText(this,
getString(R.string.audio), Toast.LENGTH_SHORT).show();
}
requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO
}, REQUEST_CAMERA_PERMISSION_RESULT);
}
} else {
recordAudio();
}
2019-06-26 11:00:59 +02:00
return true;
2017-07-15 14:59:09 +02:00
case R.id.action_store:
2017-11-15 14:14:41 +01:00
storeToot(true, true);
2017-07-15 14:59:09 +02:00
return true;
case R.id.action_tags:
Intent intentTags = new Intent(TootActivity.this, TagCacheActivity.class);
intentTags.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intentTags);
return true;
2017-07-15 14:59:09 +02:00
case R.id.action_restore:
2019-09-06 17:55:14 +02:00
try {
2017-07-15 14:59:09 +02:00
final List<StoredStatus> drafts = new StatusStoredDAO(TootActivity.this, db).getAllDrafts();
2019-09-06 17:55:14 +02:00
if (drafts == null || drafts.size() == 0) {
2020-04-08 12:42:15 +02:00
Toasty.info(TootActivity.this, getString(R.string.no_draft), Toast.LENGTH_LONG).show();
2017-07-15 14:59:09 +02:00
return true;
}
2019-01-23 19:23:26 +01:00
builderSingle = new AlertDialog.Builder(TootActivity.this, style);
2017-07-15 14:59:09 +02:00
builderSingle.setTitle(getString(R.string.choose_toot));
2019-08-19 09:44:15 +02:00
final DraftsListAdapter draftsListAdapter = new DraftsListAdapter(drafts);
2017-07-15 14:59:09 +02:00
final int[] ids = new int[drafts.size()];
int i = 0;
2019-09-06 17:55:14 +02:00
for (StoredStatus draft : drafts) {
2017-07-15 14:59:09 +02:00
ids[i] = draft.getId();
i++;
}
2020-03-08 10:29:06 +01:00
builderSingle.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss());
builderSingle.setPositiveButton(R.string.delete_all, (dialog, which) -> {
AlertDialog.Builder builder1 = new AlertDialog.Builder(TootActivity.this, style);
builder1.setTitle(R.string.delete_all);
builder1.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(R.string.yes, (dialogConfirm, which1) -> {
2020-04-08 12:42:15 +02:00
new StatusStoredDAO(TootActivity.this, db).removeAllDrafts();
2020-03-08 10:29:06 +01:00
dialogConfirm.dismiss();
dialog.dismiss();
})
.setNegativeButton(R.string.no, (dialogConfirm, which12) -> dialogConfirm.dismiss())
.show();
2017-07-15 14:59:09 +02:00
});
2020-03-08 10:29:06 +01:00
builderSingle.setAdapter(draftsListAdapter,
(dialog, which) -> {
int id = ids[which];
restoreToot(id);
dialog.dismiss();
});
2017-07-15 14:59:09 +02:00
builderSingle.show();
2019-09-06 17:55:14 +02:00
} catch (Exception e) {
2020-04-08 12:42:15 +02:00
Toasty.error(TootActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show();
2017-07-15 14:59:09 +02:00
}
return true;
2017-07-16 17:09:35 +02:00
case R.id.action_schedule:
2019-09-06 17:55:14 +02:00
if (toot_content.getText().toString().trim().length() == 0) {
2020-04-08 12:42:15 +02:00
Toasty.error(TootActivity.this, getString(R.string.toot_error_no_content), Toast.LENGTH_LONG).show();
2017-07-15 14:59:09 +02:00
return true;
}
2018-11-03 14:45:55 +01:00
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(TootActivity.this, style);
2019-01-23 19:23:26 +01:00
inflater = this.getLayoutInflater();
2020-04-08 12:42:15 +02:00
dialogView = inflater.inflate(R.layout.datetime_picker, new LinearLayout(TootActivity.this), false);
2017-07-16 17:09:35 +02:00
dialogBuilder.setView(dialogView);
final AlertDialog alertDialog = dialogBuilder.create();
2017-10-25 09:46:05 +02:00
final DatePicker datePicker = dialogView.findViewById(R.id.date_picker);
final TimePicker timePicker = dialogView.findViewById(R.id.time_picker);
2020-04-08 12:42:15 +02:00
if (android.text.format.DateFormat.is24HourFormat(TootActivity.this))
2019-05-24 15:10:43 +02:00
timePicker.setIs24HourView(true);
2017-10-25 09:46:05 +02:00
Button date_time_cancel = dialogView.findViewById(R.id.date_time_cancel);
final ImageButton date_time_previous = dialogView.findViewById(R.id.date_time_previous);
final ImageButton date_time_next = dialogView.findViewById(R.id.date_time_next);
final ImageButton date_time_set = dialogView.findViewById(R.id.date_time_set);
2017-07-16 17:09:35 +02:00
//Buttons management
2020-03-08 10:29:06 +01:00
date_time_cancel.setOnClickListener(v -> alertDialog.dismiss());
date_time_next.setOnClickListener(v -> {
datePicker.setVisibility(View.GONE);
timePicker.setVisibility(View.VISIBLE);
date_time_previous.setVisibility(View.VISIBLE);
date_time_next.setVisibility(View.GONE);
date_time_set.setVisibility(View.VISIBLE);
2017-07-16 17:09:35 +02:00
});
2020-03-08 10:29:06 +01:00
date_time_previous.setOnClickListener(v -> {
datePicker.setVisibility(View.VISIBLE);
timePicker.setVisibility(View.GONE);
date_time_previous.setVisibility(View.GONE);
date_time_next.setVisibility(View.VISIBLE);
date_time_set.setVisibility(View.GONE);
2017-07-16 17:09:35 +02:00
});
2020-03-08 10:29:06 +01:00
date_time_set.setOnClickListener(v -> {
int hour, minute;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
hour = timePicker.getHour();
minute = timePicker.getMinute();
} else {
hour = timePicker.getCurrentHour();
minute = timePicker.getCurrentMinute();
2017-07-16 17:09:35 +02:00
}
2020-03-08 10:29:06 +01:00
Calendar calendar = new GregorianCalendar(datePicker.getYear(),
datePicker.getMonth(),
datePicker.getDayOfMonth(),
hour,
minute);
final long[] time = {calendar.getTimeInMillis()};
if ((time[0] - new Date().getTime()) < 60000) {
2020-04-08 12:42:15 +02:00
Toasty.warning(TootActivity.this, getString(R.string.toot_scheduled_date), Toast.LENGTH_LONG).show();
2020-03-08 10:29:06 +01:00
} else {
2020-03-12 18:09:41 +01:00
AlertDialog.Builder builderSingle1 = new AlertDialog.Builder(TootActivity.this, style);
builderSingle1.setTitle(getString(R.string.choose_schedule));
builderSingle1.setNegativeButton(R.string.device_schedule, (dialog, which) -> {
2020-03-08 10:29:06 +01:00
deviceSchedule(time[0]);
2020-03-12 18:09:41 +01:00
dialog.dismiss();
});
builderSingle1.setPositiveButton(R.string.server_schedule, (dialog, which) -> {
int offset = TimeZone.getDefault().getRawOffset();
calendar.add(Calendar.MILLISECOND, -offset);
final String date = Helper.dateToString(new Date(calendar.getTimeInMillis()));
serverSchedule(date);
});
builderSingle1.show();
2020-03-08 10:29:06 +01:00
alertDialog.dismiss();
2017-07-16 17:09:35 +02:00
}
});
alertDialog.show();
return true;
2017-05-05 16:36:04 +02:00
default:
return super.onOptionsItemSelected(item);
}
}
2019-09-06 17:55:14 +02:00
private void sendToot(String timestamp, String content_type) {
2019-01-19 19:41:55 +01:00
toot_it.setEnabled(false);
2019-09-06 17:55:14 +02:00
if (!displayWYSIWYG() && toot_content.getText().toString().trim().length() == 0 && attachments.size() == 0) {
2020-04-08 12:42:15 +02:00
Toasty.error(TootActivity.this, getString(R.string.toot_error_no_content), Toast.LENGTH_LONG).show();
2019-07-20 17:44:44 +02:00
toot_it.setEnabled(true);
return;
}
2019-09-06 17:55:14 +02:00
if (displayWYSIWYG() && wysiwyg.getContent().toString().trim().length() == 0 && attachments.size() == 0) {
2020-04-08 12:42:15 +02:00
Toasty.error(TootActivity.this, getString(R.string.toot_error_no_content), Toast.LENGTH_LONG).show();
2019-01-19 19:41:55 +01:00
toot_it.setEnabled(true);
return;
}
2019-05-18 11:10:30 +02:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
2019-09-06 17:55:14 +02:00
boolean split_toot = sharedpreferences.getBoolean(Helper.SET_AUTOMATICALLY_SPLIT_TOOTS + userId + instance, false);
int split_toot_size = sharedpreferences.getInt(Helper.SET_AUTOMATICALLY_SPLIT_TOOTS_SIZE + userId + instance, Helper.SPLIT_TOOT_SIZE);
2019-09-06 17:55:14 +02:00
if (displayWYSIWYG()) {
2019-07-20 17:44:44 +02:00
split_toot = false;
}
2019-01-19 19:41:55 +01:00
String tootContent;
2019-09-06 17:55:14 +02:00
if (toot_cw_content.getText() != null && toot_cw_content.getText().toString().trim().length() > 0)
2019-01-19 19:41:55 +01:00
split_toot_size -= toot_cw_content.getText().toString().trim().length();
2019-09-06 17:55:14 +02:00
if (social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA || !split_toot || (countLength(social, toot_content, toot_cw_content) < split_toot_size)) {
if (!displayWYSIWYG()) {
2019-07-20 17:44:44 +02:00
tootContent = toot_content.getText().toString().trim();
2019-09-06 17:55:14 +02:00
} else {
2019-07-20 17:44:44 +02:00
tootContent = wysiwyg.getContentAsHTML();
}
2019-10-06 12:27:50 +02:00
createAndSendToot(tootContent, content_type, timestamp);
2019-09-06 17:55:14 +02:00
} else {
2019-10-06 16:35:08 +02:00
splitToot = Helper.splitToots(toot_content.getText().toString().trim(), split_toot_size, true);
2019-11-15 16:32:25 +01:00
if (splitToot.size() > 0) {
2019-10-22 18:10:20 +02:00
tootContent = splitToot.get(0);
stepSpliToot = 1;
2019-10-06 16:35:08 +02:00
2019-10-22 18:10:20 +02:00
AlertDialog.Builder builderInner = new AlertDialog.Builder(TootActivity.this, style);
builderInner.setTitle(R.string.message_preview);
2019-10-06 16:35:08 +02:00
2020-04-08 12:42:15 +02:00
View preview = getLayoutInflater().inflate(R.layout.popup_message_preview, new LinearLayout(TootActivity.this), false);
2019-10-22 18:10:20 +02:00
builderInner.setView(preview);
2019-10-06 16:35:08 +02:00
2019-10-22 18:10:20 +02:00
//Text for report
final TextView textView = preview.findViewById(R.id.preview);
textView.setText("");
final SwitchCompat report_mention = preview.findViewById(R.id.report_mention);
int finalSplit_toot_size = split_toot_size;
2020-03-08 10:29:06 +01:00
report_mention.setOnCheckedChangeListener((buttonView, isChecked) -> {
splitToot = Helper.splitToots(toot_content.getText().toString().trim(), finalSplit_toot_size, isChecked);
textView.setText("");
int inc = 0;
for (String prev : splitToot) {
if (inc < splitToot.size() - 1) {
String contentStr = textView.getText() + prev + "\n----------\n";
textView.setText(contentStr);
2019-10-06 16:35:08 +02:00
}
}
2019-10-22 18:10:20 +02:00
});
int inc = 0;
for (String prev : splitToot) {
if (inc < splitToot.size() - 1) {
2020-03-08 10:29:06 +01:00
String textStr = textView.getText() + prev + "\n----------\n";
textView.setText(textStr);
2019-10-22 18:10:20 +02:00
}
2019-10-06 16:35:08 +02:00
}
2020-03-08 10:29:06 +01:00
builderInner.setNegativeButton(R.string.cancel, (dialog, which) -> {
toot_it.setEnabled(true);
dialog.dismiss();
2019-10-22 18:10:20 +02:00
});
2020-03-08 10:29:06 +01:00
builderInner.setPositiveButton(R.string.validate, (dialog, which) -> {
createAndSendToot(tootContent, content_type, timestamp);
dialog.dismiss();
2019-10-22 18:10:20 +02:00
});
AlertDialog alertDialog = builderInner.create();
alertDialog.show();
2019-11-15 16:32:25 +01:00
} else { //Failed to split the toot.
2019-10-22 18:10:20 +02:00
if (!displayWYSIWYG()) {
tootContent = toot_content.getText().toString().trim();
} else {
tootContent = wysiwyg.getContentAsHTML();
2019-10-06 16:35:08 +02:00
}
2019-10-22 18:10:20 +02:00
createAndSendToot(tootContent, content_type, timestamp);
}
2019-01-19 19:41:55 +01:00
}
2019-10-06 12:27:50 +02:00
}
2019-11-15 16:32:25 +01:00
private void createAndSendToot(String tootContent, String content_type, String timestamp) {
2019-01-19 19:41:55 +01:00
Status toot = new Status();
2019-09-06 17:55:14 +02:00
if (content_type != null)
2019-01-26 20:01:11 +01:00
toot.setContentType(content_type);
2019-01-19 19:41:55 +01:00
toot.setSensitive(isSensitive);
2019-03-24 19:06:03 +01:00
2019-09-06 17:55:14 +02:00
if (toot_cw_content.getText().toString().trim().length() > 0)
2019-01-19 19:41:55 +01:00
toot.setSpoiler_text(toot_cw_content.getText().toString().trim());
toot.setVisibility(visibility);
2019-09-06 17:55:14 +02:00
if (tootReply != null)
2019-01-19 19:41:55 +01:00
toot.setIn_reply_to_id(tootReply.getId());
2019-11-30 08:53:37 +01:00
toot.setContent(TootActivity.this, tootContent);
2019-09-06 17:55:14 +02:00
if (social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON) {
2019-06-30 18:24:47 +02:00
if (poll != null) {
toot.setPoll(poll);
} else {
toot.setMedia_attachments(attachments);
}
2019-09-06 17:55:14 +02:00
} else {
2019-06-30 18:24:47 +02:00
if (poll != null) {
toot.setPoll(poll);
}
2019-03-24 19:06:03 +01:00
toot.setMedia_attachments(attachments);
}
2019-09-06 17:55:14 +02:00
if (timestamp == null)
if (scheduledstatus == null)
2020-04-08 12:42:15 +02:00
new PostStatusAsyncTask(TootActivity.this, social, account, toot, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2019-01-20 15:20:07 +01:00
else {
toot.setScheduled_at(Helper.dateToString(scheduledstatus.getScheduled_date()));
scheduledstatus.setStatus(toot);
isScheduled = true;
2020-04-08 12:42:15 +02:00
new PostActionAsyncTask(TootActivity.this, API.StatusAction.DELETESCHEDULED, scheduledstatus, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new PostStatusAsyncTask(TootActivity.this, social, account, toot, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2019-01-20 15:20:07 +01:00
}
2019-01-20 09:45:48 +01:00
else {
2019-01-19 19:41:55 +01:00
toot.setScheduled_at(timestamp);
2020-04-08 12:42:15 +02:00
new PostStatusAsyncTask(TootActivity.this, social, account, toot, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2019-01-19 19:41:55 +01:00
}
}
2019-09-06 17:55:14 +02:00
private void serverSchedule(String time) {
2019-01-26 20:01:11 +01:00
sendToot(time, null);
2019-01-20 15:20:07 +01:00
isScheduled = true;
2019-01-19 19:41:55 +01:00
resetForNextToot();
}
2019-09-06 17:55:14 +02:00
private void deviceSchedule(long time) {
2019-01-19 19:41:55 +01:00
//Store the toot as draft first
storeToot(false, false);
2019-01-20 15:20:07 +01:00
isScheduled = true;
2019-01-19 19:41:55 +01:00
//Schedules the toot
2020-04-08 12:42:15 +02:00
ScheduledTootsSyncJob.schedule(TootActivity.this, currentToId, time);
2019-01-19 19:41:55 +01:00
resetForNextToot();
}
2019-09-06 17:55:14 +02:00
private void resetForNextToot() {
2019-01-19 19:41:55 +01:00
//Clear content
toot_content.setText("");
toot_cw_content.setText("");
toot_space_left.setText("0");
2019-09-06 17:55:14 +02:00
if (attachments != null) {
2019-01-19 19:41:55 +01:00
for (Attachment attachment : attachments) {
View namebar = findViewById(Integer.parseInt(attachment.getId()));
if (namebar != null && namebar.getParent() != null)
((ViewGroup) namebar.getParent()).removeView(namebar);
}
2020-05-23 17:29:12 +02:00
List<Attachment> tmp_attachment = new ArrayList<>(attachments);
2019-01-19 19:41:55 +01:00
attachments.removeAll(tmp_attachment);
tmp_attachment.clear();
}
isSensitive = false;
toot_sensitive.setVisibility(View.GONE);
currentToId = -1;
2020-04-08 12:42:15 +02:00
Toasty.info(TootActivity.this, getString(R.string.toot_scheduled), Toast.LENGTH_LONG).show();
2019-01-19 19:41:55 +01:00
}
@Override
2019-11-13 12:54:01 +01:00
public boolean onCreateOptionsMenu(@NotNull Menu menu) {
getMenuInflater().inflate(R.menu.main_toot, menu);
2019-09-06 17:55:14 +02:00
if (restored != -1) {
2017-07-16 19:04:53 +02:00
MenuItem itemRestore = menu.findItem(R.id.action_restore);
2019-09-06 17:55:14 +02:00
if (itemRestore != null)
2017-07-16 19:04:53 +02:00
itemRestore.setVisible(false);
MenuItem itemSchedule = menu.findItem(R.id.action_schedule);
2019-09-06 17:55:14 +02:00
if (restoredScheduled)
2017-07-16 19:04:53 +02:00
itemSchedule.setVisible(false);
}
MenuItem itemViewReply = menu.findItem(R.id.action_view_reply);
2019-09-06 17:55:14 +02:00
if (tootReply == null) {
if (itemViewReply != null)
itemViewReply.setVisible(false);
}
2019-09-06 17:55:14 +02:00
if (social != UpdateAccountInfoAsyncTask.SOCIAL.MASTODON && social != UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA) {
2019-03-23 17:23:02 +01:00
MenuItem itemPoll = menu.findItem(R.id.action_poll);
2019-09-06 17:55:14 +02:00
if (itemPoll != null)
2019-03-23 17:23:02 +01:00
itemPoll.setVisible(false);
}
2019-05-18 11:10:30 +02:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
2017-11-03 13:55:18 +01:00
MenuItem itemEmoji = menu.findItem(R.id.action_emoji);
2020-04-09 18:57:12 +02:00
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2020-04-08 12:42:15 +02:00
final List<Emojis> emojis = new CustomEmojiDAO(TootActivity.this, db).getAllEmojis();
2017-11-24 07:25:04 +01:00
//Displays button only if custom emojis
2020-03-12 18:09:41 +01:00
if (emojis != null && emojis.size() > 0) {
2017-11-03 13:55:18 +01:00
itemEmoji.setVisible(true);
2019-09-06 17:55:14 +02:00
} else {
2017-11-03 13:55:18 +01:00
itemEmoji.setVisible(false);
}
2019-09-06 17:55:14 +02:00
if (accountReply != null) {
MenuItem itemRestore = menu.findItem(R.id.action_restore);
2019-09-06 17:55:14 +02:00
if (itemRestore != null)
itemRestore.setVisible(false);
MenuItem itemSchedule = menu.findItem(R.id.action_schedule);
2019-09-06 17:55:14 +02:00
if (itemSchedule != null)
itemSchedule.setVisible(false);
2019-09-06 17:55:14 +02:00
MenuItem itemStore = menu.findItem(R.id.action_store);
if (itemStore != null)
itemStore.setVisible(false);
}
2019-09-06 17:55:14 +02:00
if (social == UpdateAccountInfoAsyncTask.SOCIAL.GNU || social == UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA) {
2019-02-22 14:30:04 +01:00
MenuItem itemContacts = menu.findItem(R.id.action_contacts);
2019-09-06 17:55:14 +02:00
if (itemContacts != null)
2019-02-22 14:30:04 +01:00
itemContacts.setVisible(false);
}
2020-02-21 09:53:16 +01:00
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
2020-03-08 10:29:06 +01:00
if (theme == THEME_LIGHT) {
2020-02-21 09:53:16 +01:00
//Hack for colors in compose menu
2020-03-08 10:29:06 +01:00
for (int i = 0; i < menu.size(); i++) {
2020-02-21 09:53:16 +01:00
MenuItem item = menu.getItem(i);
SpannableString s = new SpannableString(item.getTitle());
s.setSpan(new ForegroundColorSpan(Color.BLACK), 0, s.length(), 0);
item.setTitle(s);
}
}
return true;
}
2017-11-18 12:22:41 +01:00
@Override
2017-12-07 18:48:18 +01:00
public void onDownloaded(String pathToFile, String url, Error error) {
2019-09-06 17:55:14 +02:00
if (error == null && pathToFile != null) {
picture_scrollview.setVisibility(View.VISIBLE);
Uri uri = Uri.fromFile(new File(pathToFile));
String filename = FileNameCleaner.cleanFileName(url);
toot_picture_container.setVisibility(View.VISIBLE);
toot_picture.setEnabled(false);
toot_it.setEnabled(false);
2019-10-09 10:53:55 +02:00
upload(TootActivity.this, account, social, uri, filename, uploadReceiver);
}
2019-06-05 18:23:44 +02:00
}
@Override
public void onRetrieveAttachment(Attachment attachment, String fileName, Error error) {
2017-11-18 12:22:41 +01:00
}
2017-10-29 16:53:32 +01:00
@Override
public void onUpdateProgress(int progress) {
ProgressBar progressBar = findViewById(R.id.upload_progress);
TextView toolbar_text = findViewById(R.id.toolbar_text);
RelativeLayout progress_bar_container = findViewById(R.id.progress_bar_container);
2019-09-06 17:55:14 +02:00
if (progress <= 100) {
2017-10-29 16:53:32 +01:00
progressBar.setScaleY(3f);
progress_bar_container.setVisibility(View.VISIBLE);
progressBar.setProgress(progress);
toolbar_text.setText(String.format("%s%%", progress));
2019-09-06 17:55:14 +02:00
} else {
2017-10-29 16:53:32 +01:00
progress_bar_container.setVisibility(View.GONE);
}
}
2019-09-06 17:55:14 +02:00
private void showAddDescription(final Attachment attachment) {
2018-11-03 14:45:55 +01:00
AlertDialog.Builder builderInner = new AlertDialog.Builder(TootActivity.this, style);
2017-11-15 14:05:41 +01:00
builderInner.setTitle(R.string.upload_form_description);
2020-04-08 12:42:15 +02:00
View popup_media_description = getLayoutInflater().inflate(R.layout.popup_media_description, new LinearLayout(TootActivity.this), false);
builderInner.setView(popup_media_description);
2017-11-15 14:05:41 +01:00
//Text for report
final EditText input = popup_media_description.findViewById(R.id.media_description);
2017-11-15 14:05:41 +01:00
input.setFilters(new InputFilter[]{new InputFilter.LengthFilter(420)});
final ImageView media_picture = popup_media_description.findViewById(R.id.media_picture);
2020-04-08 12:42:15 +02:00
Glide.with(TootActivity.this)
.asBitmap()
.load(attachment.getUrl())
2020-04-09 14:59:36 +02:00
.into(new CustomTarget<Bitmap>() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
2018-01-10 10:37:55 +01:00
media_picture.setImageBitmap(resource);
2019-11-09 18:07:33 +01:00
media_picture.setImageAlpha(60);
}
2020-04-09 14:59:36 +02:00
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
2020-03-08 10:29:06 +01:00
builderInner.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss());
2017-11-15 14:05:41 +01:00
2019-09-06 17:55:14 +02:00
if (attachment.getDescription() != null && !attachment.getDescription().equals("null")) {
input.setText(attachment.getDescription());
input.setSelection(input.getText().length());
2017-11-15 14:05:41 +01:00
}
2020-03-08 10:29:06 +01:00
builderInner.setPositiveButton(R.string.validate, (dialog, which) -> {
2020-04-08 12:42:15 +02:00
new UpdateDescriptionAttachmentAsyncTask(TootActivity.this, attachment.getId(), input.getText().toString(), account, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2020-03-08 10:29:06 +01:00
attachment.setDescription(input.getText().toString());
addBorder();
dialog.dismiss();
2017-11-15 14:05:41 +01:00
});
AlertDialog alertDialog = builderInner.create();
2020-03-08 10:29:06 +01:00
Objects.requireNonNull(alertDialog.getWindow()).setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
2017-11-15 14:05:41 +01:00
alertDialog.show();
input.requestFocus();
2017-11-15 14:05:41 +01:00
}
2017-05-05 16:36:04 +02:00
/**
* Removes a media
2019-09-06 17:55:14 +02:00
*
2017-05-05 16:36:04 +02:00
* @param viewId String
*/
2019-09-06 17:55:14 +02:00
private void showRemove(final int viewId) {
2017-05-05 16:36:04 +02:00
2018-11-03 14:45:55 +01:00
AlertDialog.Builder dialog = new AlertDialog.Builder(TootActivity.this, style);
2017-05-05 16:36:04 +02:00
dialog.setMessage(R.string.toot_delete_media);
2020-03-08 10:29:06 +01:00
dialog.setNegativeButton(R.string.cancel, (dialog1, which) -> dialog1.dismiss());
dialog.setPositiveButton(R.string.yes, (dialog12, which) -> {
View namebar = findViewById(viewId);
for (Attachment attachment : attachments) {
if (Integer.parseInt(attachment.getId()) == viewId) {
attachments.remove(attachment);
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
boolean show_media_urls = sharedpreferences.getBoolean(Helper.SET_MEDIA_URLS, false);
if (show_media_urls) {
//Clears the text_url at the end of the toot for this attachment
int selectionBefore = toot_content.getSelectionStart();
toot_content.setText(toot_content.getText().toString().replace(attachment.getText_url(), ""));
toot_space_left.setText(String.valueOf(countLength(social, toot_content, toot_cw_content)));
//Moves the cursor
if (selectionBefore >= 0 && selectionBefore < toot_content.length())
toot_content.setSelection(selectionBefore);
}
2020-03-08 10:29:06 +01:00
((ViewGroup) namebar.getParent()).removeView(namebar);
break;
}
2017-05-05 16:36:04 +02:00
}
2020-03-08 10:29:06 +01:00
dialog12.dismiss();
if (attachments.size() == 0) {
toot_sensitive.setVisibility(View.GONE);
isSensitive = false;
toot_sensitive.setChecked(false);
picture_scrollview.setVisibility(View.GONE);
}
toot_picture.setEnabled(true);
2017-05-05 16:36:04 +02:00
});
dialog.show();
}
2019-09-06 17:55:14 +02:00
private void tootVisibilityDialog() {
2017-05-05 16:36:04 +02:00
2018-11-03 14:45:55 +01:00
AlertDialog.Builder dialog = new AlertDialog.Builder(TootActivity.this, style);
2017-05-05 16:36:04 +02:00
dialog.setTitle(R.string.toot_visibility_tilte);
final String[] stringArray = getResources().getStringArray(R.array.toot_visibility);
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(TootActivity.this, android.R.layout.simple_list_item_1, stringArray);
2020-03-08 10:29:06 +01:00
dialog.setNegativeButton(R.string.cancel, (dialog1, position) -> dialog1.dismiss());
dialog.setAdapter(arrayAdapter, (dialog12, position) -> {
switch (position) {
case 0:
visibility = "public";
toot_visibility.setImageResource(R.drawable.ic_public_toot);
break;
case 1:
visibility = "unlisted";
toot_visibility.setImageResource(R.drawable.ic_lock_open_toot);
break;
case 2:
visibility = "private";
toot_visibility.setImageResource(R.drawable.ic_lock_outline_toot);
break;
case 3:
visibility = "direct";
toot_visibility.setImageResource(R.drawable.ic_mail_outline_toot);
break;
2017-05-05 16:36:04 +02:00
}
2017-10-28 14:32:18 +02:00
2020-03-08 10:29:06 +01:00
dialog12.dismiss();
2017-05-05 16:36:04 +02:00
});
dialog.show();
}
2019-06-06 15:42:24 +02:00
@Override
protected void onResume() {
super.onResume();
}
2017-07-15 14:59:09 +02:00
@Override
2019-09-06 17:55:14 +02:00
public void onPause() {
2017-07-15 14:59:09 +02:00
super.onPause();
2019-05-18 11:10:30 +02:00
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
2017-07-15 14:59:09 +02:00
boolean storeToot = sharedpreferences.getBoolean(Helper.SET_AUTO_STORE, true);
2019-09-06 17:55:14 +02:00
if (storeToot && accountReply == null) {
2017-11-15 14:14:41 +01:00
storeToot(true, false);
2019-09-06 17:55:14 +02:00
} else if (storeToot) {
storeToot(false, false);
2019-06-17 14:24:50 +02:00
}
}
@Override
public void onBackPressed() {
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
boolean storeToot = sharedpreferences.getBoolean(Helper.SET_AUTO_STORE, true);
2019-09-06 17:55:14 +02:00
if (!storeToot) {
if (toot_content.getText().toString().trim().length() == 0 && (attachments == null || attachments.size() < 1) && toot_cw_content.getText().toString().trim().length() == 0) {
2019-06-17 14:24:50 +02:00
finish();
2019-09-06 17:55:14 +02:00
} else if (initialContent.trim().equals(toot_content.getText().toString().trim())) {
2019-06-17 14:24:50 +02:00
finish();
2019-09-06 17:55:14 +02:00
} else {
2019-06-17 14:24:50 +02:00
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(TootActivity.this, style);
dialogBuilder.setMessage(R.string.save_draft);
2020-03-08 10:29:06 +01:00
dialogBuilder.setPositiveButton(R.string.save, (dialog, id) -> {
if (accountReply == null) {
storeToot(true, false);
} else {
storeToot(false, false);
2019-06-17 14:24:50 +02:00
}
2020-03-08 10:29:06 +01:00
dialog.dismiss();
finish();
2019-06-17 14:24:50 +02:00
});
2020-03-08 10:29:06 +01:00
dialogBuilder.setNegativeButton(R.string.discard, (dialog, id) -> {
dialog.dismiss();
finish();
2019-06-17 14:24:50 +02:00
});
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.setCancelable(false);
alertDialog.show();
}
2019-09-06 17:55:14 +02:00
} else {
2019-06-17 14:24:50 +02:00
super.onBackPressed();
}
2019-06-06 15:42:24 +02:00
2017-07-15 14:59:09 +02:00
}
2019-06-06 15:42:24 +02:00
@Override
public void onDestroy() {
super.onDestroy();
LocalBroadcastManager.getInstance(this)
.unregisterReceiver(imageReceiver);
2019-07-24 18:54:57 +02:00
LocalBroadcastManager.getInstance(this)
.unregisterReceiver(add_new_media);
2019-06-06 15:42:24 +02:00
uploadReceiver.unregister(this);
}
2017-05-05 16:36:04 +02:00
@Override
public void onPostStatusAction(APIResponse apiResponse) {
2019-09-06 17:55:14 +02:00
if (apiResponse.getError() != null) {
2017-08-18 16:54:43 +02:00
toot_it.setEnabled(true);
2020-01-19 10:29:35 +01:00
if (apiResponse.getError().getStatusCode() == -33) {
2019-01-25 16:54:48 +01:00
storeToot(false, true);
} else {
2017-08-18 16:54:43 +02:00
showAToast(apiResponse.getError().getError());
2019-01-25 16:54:48 +01:00
return;
2017-08-18 16:54:43 +02:00
}
}
2019-05-18 11:10:30 +02:00
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
2019-09-06 17:55:14 +02:00
boolean split_toot = sharedpreferences.getBoolean(Helper.SET_AUTOMATICALLY_SPLIT_TOOTS + userId + instance, false);
2018-08-15 11:24:57 +02:00
2019-09-06 17:55:14 +02:00
if (split_toot && splitToot != null && stepSpliToot < splitToot.size()) {
2018-08-15 11:24:57 +02:00
String tootContent = splitToot.get(stepSpliToot);
stepSpliToot += 1;
Status toot = new Status();
toot.setSensitive(isSensitive);
toot.setMedia_attachments(attachments);
2019-09-06 17:55:14 +02:00
if (toot_cw_content.getText().toString().trim().length() > 0)
2018-08-15 11:24:57 +02:00
toot.setSpoiler_text(toot_cw_content.getText().toString().trim());
toot.setVisibility(visibility);
2019-09-06 17:55:14 +02:00
if (apiResponse.getStatuses() != null && apiResponse.getStatuses().size() > 0)
2018-08-15 11:24:57 +02:00
toot.setIn_reply_to_id(apiResponse.getStatuses().get(0).getId());
2019-11-30 08:53:37 +01:00
toot.setContent(TootActivity.this, tootContent);
2020-04-08 12:42:15 +02:00
new PostStatusAsyncTask(TootActivity.this, social, account, toot, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2018-08-15 11:24:57 +02:00
return;
}
2019-09-06 17:55:14 +02:00
if (apiResponse.getError() == null || apiResponse.getError().getStatusCode() != -33) {
if (restored != -1) {
2020-04-09 18:57:12 +02:00
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2020-04-08 12:42:15 +02:00
new StatusStoredDAO(TootActivity.this, db).remove(restored);
} else if (currentToId != -1) {
2020-04-09 18:57:12 +02:00
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2020-04-08 12:42:15 +02:00
new StatusStoredDAO(TootActivity.this, db).remove(currentToId);
}
}
//Clear the toot
toot_content.setText("");
toot_cw_content.setText("");
toot_space_left.setText("0");
2019-09-06 17:55:14 +02:00
if (attachments != null) {
for (Attachment attachment : attachments) {
View namebar = findViewById(Integer.parseInt(attachment.getId()));
if (namebar != null && namebar.getParent() != null)
((ViewGroup) namebar.getParent()).removeView(namebar);
}
2020-05-23 17:29:12 +02:00
List<Attachment> tmp_attachment = new ArrayList<>(attachments);
attachments.removeAll(tmp_attachment);
tmp_attachment.clear();
}
isSensitive = false;
toot_sensitive.setVisibility(View.GONE);
currentToId = -1;
2019-09-06 17:55:14 +02:00
if (apiResponse.getError() == null) {
if (scheduledstatus == null && !isScheduled) {
boolean display_confirm = sharedpreferences.getBoolean(Helper.SET_DISPLAY_CONFIRM, true);
2019-09-06 17:55:14 +02:00
if (display_confirm) {
2020-04-08 12:42:15 +02:00
Toasty.success(TootActivity.this, getString(R.string.toot_sent), Toast.LENGTH_LONG).show();
}
2019-09-06 17:55:14 +02:00
} else
2020-04-08 12:42:15 +02:00
Toasty.success(TootActivity.this, getString(R.string.toot_scheduled), Toast.LENGTH_LONG).show();
2019-09-06 17:55:14 +02:00
} else {
if (apiResponse.getError().getStatusCode() == -33)
2020-04-08 12:42:15 +02:00
Toasty.info(TootActivity.this, getString(R.string.toast_toot_saved_error), Toast.LENGTH_LONG).show();
2019-01-25 16:54:48 +01:00
}
toot_it.setEnabled(true);
//It's a reply, so the user will be redirect to its answer
2019-09-06 17:55:14 +02:00
if (tootReply != null) {
List<Status> statuses = apiResponse.getStatuses();
2019-09-06 17:55:14 +02:00
if (statuses != null && statuses.size() > 0) {
Status status = statuses.get(0);
2019-09-06 17:55:14 +02:00
if (status != null) {
2020-04-08 12:42:15 +02:00
Intent intent = new Intent(TootActivity.this, ShowConversationActivity.class);
Bundle b = new Bundle();
2019-09-06 17:55:14 +02:00
if (idRedirect == null)
2018-11-16 16:35:25 +01:00
b.putParcelable("status", status);
2018-11-17 15:55:38 +01:00
else {
2018-12-08 17:03:27 +01:00
b.putParcelable("status", idRedirect);
2018-11-17 15:55:38 +01:00
}
intent.putExtras(b);
startActivity(intent);
finish();
2017-05-05 16:36:04 +02:00
}
}
2019-09-06 17:55:14 +02:00
} else {
2020-04-08 12:42:15 +02:00
Intent intent = new Intent(TootActivity.this, MainActivity.class);
2019-05-18 11:10:30 +02:00
intent.putExtra(Helper.INTENT_ACTION, Helper.HOME_TIMELINE_INTENT);
2018-11-03 19:25:36 +01:00
startActivity(intent);
finish();
2017-05-05 16:36:04 +02:00
}
}
2017-05-05 16:36:04 +02:00
@Override
2017-06-03 18:18:27 +02:00
public void onRetrieveSearchAccounts(APIResponse apiResponse) {
2019-09-06 17:55:14 +02:00
if (pp_progress != null && pp_actionBar != null) {
2017-08-02 12:17:43 +02:00
pp_progress.setVisibility(View.GONE);
pp_actionBar.setVisibility(View.VISIBLE);
}
2019-09-06 17:55:14 +02:00
if (apiResponse.getError() != null)
return;
final List<Account> accounts = apiResponse.getAccounts();
2019-09-06 17:55:14 +02:00
if (accounts != null && accounts.size() > 0) {
if (!displayWYSIWYG()) {
2019-07-20 15:15:47 +02:00
int currentCursorPosition = toot_content.getSelectionStart();
AccountsSearchAdapter accountsListAdapter = new AccountsSearchAdapter(TootActivity.this, accounts);
toot_content.setThreshold(1);
toot_content.setAdapter(accountsListAdapter);
final String oldContent = toot_content.getText().toString();
if (oldContent.length() >= currentCursorPosition) {
String[] searchA = oldContent.substring(0, currentCursorPosition).split("@");
if (searchA.length > 0) {
final String search = searchA[searchA.length - 1];
2020-03-08 10:29:06 +01:00
toot_content.setOnItemClickListener((parent, view, position, id) -> {
Account account = accounts.get(position);
String deltaSearch = "";
int searchLength = searchDeep;
if (currentCursorPosition < searchDeep) { //Less than 15 characters are written before the cursor position
searchLength = currentCursorPosition;
}
if (currentCursorPosition - searchLength > 0 && currentCursorPosition < oldContent.length())
deltaSearch = oldContent.substring(currentCursorPosition - searchLength, currentCursorPosition);
else {
if (currentCursorPosition >= oldContent.length())
deltaSearch = oldContent.substring(currentCursorPosition - searchLength);
2019-07-04 19:43:48 +02:00
}
2020-03-08 10:29:06 +01:00
if (!search.equals(""))
deltaSearch = deltaSearch.replace("@" + search, "");
String newContent = oldContent.substring(0, currentCursorPosition - searchLength);
newContent += deltaSearch;
newContent += "@" + account.getAcct() + " ";
int newPosition = newContent.length();
if (currentCursorPosition < oldContent.length())
newContent += oldContent.substring(currentCursorPosition);
toot_content.setText(newContent);
toot_space_left.setText(String.valueOf(countLength(social, toot_content, toot_cw_content)));
toot_content.setSelection(newPosition);
AccountsSearchAdapter accountsListAdapter1 = new AccountsSearchAdapter(TootActivity.this, new ArrayList<>());
toot_content.setThreshold(1);
toot_content.setAdapter(accountsListAdapter1);
2019-07-20 15:15:47 +02:00
});
}
}
2019-09-06 17:55:14 +02:00
} else { //For Pleroma and its wysiwyg
2019-07-20 15:32:17 +02:00
RecyclerView suggestionsRV = findViewById(R.id.suggestions);
2019-07-20 15:15:47 +02:00
suggestionsRV.setLayoutManager(new LinearLayoutManager(this));
List<Suggestion> suggestions = new ArrayList<>();
2019-09-06 17:55:14 +02:00
for (Account account : accounts) {
2019-07-20 15:15:47 +02:00
Suggestion suggestion = new Suggestion();
suggestion.setContent(account.getAcct());
suggestion.setImageUrl(account.getAvatar());
suggestion.setType(Suggestion.suggestionType.ACCOUNT);
suggestions.add(suggestion);
}
2019-08-19 09:18:30 +02:00
SuggestionsAdapter suggestionsAdapter = new SuggestionsAdapter(suggestions);
2019-07-20 15:15:47 +02:00
suggestionsRV.setAdapter(suggestionsAdapter);
suggestionsRV.setVisibility(View.VISIBLE);
final String oldContent = wysiwygEditText.getText().toString();
int currentCursorPosition = wysiwygEditText.getSelectionStart();
if (oldContent.length() >= currentCursorPosition) {
String[] searchA = oldContent.substring(0, currentCursorPosition).split("@");
if (searchA.length > 0) {
final String search = searchA[searchA.length - 1];
2020-03-08 10:29:06 +01:00
suggestionsRV.addOnItemTouchListener(new RecyclerItemClickListener(this, (view, position) -> {
final Suggestion suggestion = suggestionsAdapter.getItem(position);
2019-07-04 19:04:27 +02:00
2019-07-20 15:15:47 +02:00
2020-03-08 10:29:06 +01:00
String deltaSearch = "";
int searchLength = searchDeep;
if (currentCursorPosition < searchDeep) { //Less than 15 characters are written before the cursor position
searchLength = currentCursorPosition;
}
if (currentCursorPosition - searchLength > 0 && currentCursorPosition < oldContent.length())
deltaSearch = oldContent.substring(currentCursorPosition - searchLength, currentCursorPosition);
else {
if (currentCursorPosition >= oldContent.length())
deltaSearch = oldContent.substring(currentCursorPosition - searchLength);
2019-07-20 15:15:47 +02:00
}
2020-03-08 10:29:06 +01:00
if (!search.equals(""))
deltaSearch = deltaSearch.replace("@" + search, "");
String newContent = oldContent.substring(0, currentCursorPosition - searchLength);
newContent += deltaSearch;
newContent += "@" + suggestion.getContent() + " ";
int newPosition = newContent.length();
if (currentCursorPosition < oldContent.length())
newContent += oldContent.substring(currentCursorPosition);
wysiwygEditText.setText(newContent);
toot_space_left.setText(String.valueOf(countLength(wysiwyg, toot_cw_content)));
wysiwygEditText.setSelection(newPosition);
suggestionsRV.setVisibility(View.GONE);
2019-07-20 15:15:47 +02:00
}));
}
2017-10-18 14:25:51 +02:00
}
2019-07-20 15:15:47 +02:00
}
2017-05-05 16:36:04 +02:00
}
2017-10-09 18:35:43 +02:00
}
2019-01-23 19:23:26 +01:00
@Override
public void onRetrieveContact(APIResponse apiResponse) {
2019-09-06 17:55:14 +02:00
if (apiResponse.getError() != null || apiResponse.getAccounts() == null)
2019-01-23 19:23:26 +01:00
return;
2019-01-23 19:35:08 +01:00
this.contacts = new ArrayList<>();
this.checkedValues = new ArrayList<>();
this.contacts.addAll(apiResponse.getAccounts());
2019-09-06 17:55:14 +02:00
for (Account account : contacts) {
2019-01-23 19:35:08 +01:00
this.checkedValues.add(toot_content.getText().toString().contains("@" + account.getAcct()));
2019-01-23 19:23:26 +01:00
}
2019-01-23 19:35:08 +01:00
this.loader.setVisibility(View.GONE);
2019-11-15 16:32:25 +01:00
AccountsReplyAdapter contactAdapter = new AccountsReplyAdapter(new WeakReference<>(TootActivity.this), this.contacts, this.checkedValues);
2019-01-23 19:35:08 +01:00
this.lv_accounts_search.setAdapter(contactAdapter);
2019-01-23 19:23:26 +01:00
}
2017-11-01 19:19:37 +01:00
@Override
2017-12-03 14:17:57 +01:00
public void onRetrieveEmoji(Status status, boolean fromTranslation) {
2017-11-01 19:19:37 +01:00
}
2019-01-02 14:22:57 +01:00
@Override
public void onRetrieveEmoji(Notification notification) {
}
2017-11-01 19:19:37 +01:00
@Override
2017-11-01 19:34:53 +01:00
public void onRetrieveSearchEmoji(final List<Emojis> emojis) {
2019-09-06 17:55:14 +02:00
if (pp_progress != null && pp_actionBar != null) {
2017-11-01 19:19:37 +01:00
pp_progress.setVisibility(View.GONE);
pp_actionBar.setVisibility(View.VISIBLE);
}
2019-07-20 15:32:17 +02:00
if (emojis != null && emojis.size() > 0) {
2019-09-06 17:55:14 +02:00
if (!displayWYSIWYG()) {
int currentCursorPosition = toot_content.getSelectionStart();
EmojisSearchAdapter emojisSearchAdapter = new EmojisSearchAdapter(TootActivity.this, emojis);
toot_content.setThreshold(1);
toot_content.setAdapter(emojisSearchAdapter);
final String oldContent = toot_content.getText().toString();
String[] searchA = oldContent.substring(0, currentCursorPosition).split(":");
if (searchA.length > 0) {
final String search = searchA[searchA.length - 1];
2020-03-08 10:29:06 +01:00
toot_content.setOnItemClickListener((parent, view, position, id) -> {
String shortcode = emojis.get(position).getShortcode();
String deltaSearch = "";
int searchLength = searchDeep;
if (currentCursorPosition < searchDeep) { //Less than 15 characters are written before the cursor position
searchLength = currentCursorPosition;
}
if (currentCursorPosition - searchLength > 0 && currentCursorPosition < oldContent.length())
deltaSearch = oldContent.substring(currentCursorPosition - searchLength, currentCursorPosition);
else {
if (currentCursorPosition >= oldContent.length())
deltaSearch = oldContent.substring(currentCursorPosition - searchLength);
}
2019-07-04 19:43:48 +02:00
2020-03-08 10:29:06 +01:00
if (!search.equals(""))
deltaSearch = deltaSearch.replace(":" + search, "");
String newContent = oldContent.substring(0, currentCursorPosition - searchLength);
newContent += deltaSearch;
newContent += ":" + shortcode + ": ";
int newPosition = newContent.length();
if (currentCursorPosition < oldContent.length())
newContent += oldContent.substring(currentCursorPosition);
toot_content.setText(newContent);
toot_space_left.setText(String.valueOf(countLength(social, toot_content, toot_cw_content)));
toot_content.setSelection(newPosition);
EmojisSearchAdapter emojisSearchAdapter1 = new EmojisSearchAdapter(TootActivity.this, new ArrayList<>());
toot_content.setThreshold(1);
toot_content.setAdapter(emojisSearchAdapter1);
2019-07-04 19:43:48 +02:00
2019-09-06 17:55:14 +02:00
});
}
} else {
2019-07-20 15:32:17 +02:00
int currentCursorPosition = wysiwygEditText.getSelectionStart();
RecyclerView suggestionsRV = findViewById(R.id.suggestions);
suggestionsRV.setLayoutManager(new LinearLayoutManager(this));
List<Suggestion> suggestions = new ArrayList<>();
for (Emojis emoji : emojis) {
Suggestion suggestion = new Suggestion();
suggestion.setContent(emoji.getShortcode());
suggestion.setImageUrl(emoji.getUrl());
suggestion.setType(Suggestion.suggestionType.EMOJI);
suggestions.add(suggestion);
}
2019-08-19 09:18:30 +02:00
SuggestionsAdapter suggestionsAdapter = new SuggestionsAdapter(suggestions);
2019-07-20 15:32:17 +02:00
suggestionsRV.setAdapter(suggestionsAdapter);
suggestionsRV.setVisibility(View.VISIBLE);
final String oldContent = wysiwygEditText.getText().toString();
if (oldContent.length() >= currentCursorPosition) {
String[] searchA = oldContent.substring(0, currentCursorPosition).split(":");
if (searchA.length > 0) {
final String search = searchA[searchA.length - 1];
2020-03-08 10:29:06 +01:00
suggestionsRV.addOnItemTouchListener(new RecyclerItemClickListener(this, (view, position) -> {
final Suggestion suggestion = suggestionsAdapter.getItem(position);
2019-07-20 15:32:17 +02:00
2020-03-08 10:29:06 +01:00
String deltaSearch = "";
int searchLength = searchDeep;
if (currentCursorPosition < searchDeep) { //Less than 15 characters are written before the cursor position
searchLength = currentCursorPosition;
}
if (currentCursorPosition - searchLength > 0 && currentCursorPosition < oldContent.length())
deltaSearch = oldContent.substring(currentCursorPosition - searchLength, currentCursorPosition);
else {
if (currentCursorPosition >= oldContent.length())
deltaSearch = oldContent.substring(currentCursorPosition - searchLength);
}
2019-07-20 15:32:17 +02:00
2020-03-08 10:29:06 +01:00
if (!search.equals(""))
deltaSearch = deltaSearch.replace(":" + search, "");
String newContent = oldContent.substring(0, currentCursorPosition - searchLength);
newContent += deltaSearch;
newContent += ":" + suggestion.getContent() + ": ";
int newPosition = newContent.length();
if (currentCursorPosition < oldContent.length())
newContent += oldContent.substring(currentCursorPosition);
wysiwygEditText.setText(newContent);
toot_space_left.setText(String.valueOf(countLength(wysiwyg, toot_cw_content)));
wysiwygEditText.setSelection(newPosition);
suggestionsRV.setVisibility(View.GONE);
2019-07-20 15:32:17 +02:00
}));
}
}
}
2017-11-01 19:19:37 +01:00
}
}
2017-10-09 18:35:43 +02:00
@Override
2019-03-31 14:25:40 +02:00
public void onRetrieveSearch(APIResponse apiResponse) {
2019-06-27 15:17:18 +02:00
2019-07-20 15:32:17 +02:00
2019-09-06 17:55:14 +02:00
if (pp_progress != null && pp_actionBar != null) {
2017-10-09 18:35:43 +02:00
pp_progress.setVisibility(View.GONE);
pp_actionBar.setVisibility(View.VISIBLE);
}
2019-09-06 17:55:14 +02:00
if (apiResponse == null || apiResponse.getResults() == null)
2017-10-09 18:35:43 +02:00
return;
2019-03-31 14:25:40 +02:00
Results results = apiResponse.getResults();
2017-10-09 18:35:43 +02:00
final List<String> tags = results.getHashtags();
2019-09-06 17:55:14 +02:00
if (tags != null && tags.size() > 0) {
if (!displayWYSIWYG()) {
2019-07-20 15:32:17 +02:00
int currentCursorPosition = toot_content.getSelectionStart();
TagsSearchAdapter tagsSearchAdapter = new TagsSearchAdapter(TootActivity.this, tags);
toot_content.setThreshold(1);
toot_content.setAdapter(tagsSearchAdapter);
final String oldContent = toot_content.getText().toString();
if (oldContent.length() < currentCursorPosition)
return;
String[] searchA = oldContent.substring(0, currentCursorPosition).split("#");
if (searchA.length < 1)
return;
final String search = searchA[searchA.length - 1];
2020-03-08 10:29:06 +01:00
toot_content.setOnItemClickListener((parent, view, position, id) -> {
if (position >= tags.size())
return;
String tag = tags.get(position);
String deltaSearch = "";
int searchLength = searchDeep;
if (currentCursorPosition < searchDeep) { //Less than 15 characters are written before the cursor position
searchLength = currentCursorPosition;
}
if (currentCursorPosition - searchLength > 0 && currentCursorPosition < oldContent.length())
deltaSearch = oldContent.substring(currentCursorPosition - searchLength, currentCursorPosition);
else {
if (currentCursorPosition >= oldContent.length())
deltaSearch = oldContent.substring(currentCursorPosition - searchLength);
}
2019-07-20 15:32:17 +02:00
2020-03-08 10:29:06 +01:00
if (!search.equals(""))
deltaSearch = deltaSearch.replace("#" + search, "");
String newContent = oldContent.substring(0, currentCursorPosition - searchLength);
newContent += deltaSearch;
newContent += "#" + tag + " ";
int newPosition = newContent.length();
if (currentCursorPosition < oldContent.length())
newContent += oldContent.substring(currentCursorPosition);
toot_content.setText(newContent);
toot_space_left.setText(String.valueOf(countLength(social, toot_content, toot_cw_content)));
toot_content.setSelection(newPosition);
TagsSearchAdapter tagsSearchAdapter1 = new TagsSearchAdapter(TootActivity.this, new ArrayList<>());
toot_content.setThreshold(1);
toot_content.setAdapter(tagsSearchAdapter1);
2019-07-20 15:32:17 +02:00
});
2019-09-06 17:55:14 +02:00
} else {
2019-07-20 15:32:17 +02:00
int currentCursorPosition = wysiwygEditText.getSelectionStart();
2019-07-04 19:43:48 +02:00
2019-07-20 15:32:17 +02:00
List<Suggestion> suggestions = new ArrayList<>();
for (String tag : tags) {
Suggestion suggestion = new Suggestion();
suggestion.setContent(tag);
suggestion.setType(Suggestion.suggestionType.TAG);
suggestions.add(suggestion);
2017-10-09 18:35:43 +02:00
}
2019-07-20 15:32:17 +02:00
RecyclerView suggestionsRV = findViewById(R.id.suggestions);
suggestionsRV.setLayoutManager(new LinearLayoutManager(this));
2019-08-19 09:18:30 +02:00
SuggestionsAdapter suggestionsAdapter = new SuggestionsAdapter(suggestions);
2019-07-20 15:32:17 +02:00
suggestionsRV.setAdapter(suggestionsAdapter);
suggestionsRV.setVisibility(View.VISIBLE);
final String oldContent = wysiwygEditText.getText().toString();
if (oldContent.length() >= currentCursorPosition) {
String[] searchA = oldContent.substring(0, currentCursorPosition).split("#");
if (searchA.length > 0) {
final String search = searchA[searchA.length - 1];
2020-03-08 10:29:06 +01:00
suggestionsRV.addOnItemTouchListener(new RecyclerItemClickListener(this, (view, position) -> {
final Suggestion suggestion = suggestionsAdapter.getItem(position);
2019-07-20 15:32:17 +02:00
2020-03-08 10:29:06 +01:00
String deltaSearch = "";
int searchLength = searchDeep;
if (currentCursorPosition < searchDeep) { //Less than 15 characters are written before the cursor position
searchLength = currentCursorPosition;
}
if (currentCursorPosition - searchLength > 0 && currentCursorPosition < oldContent.length())
deltaSearch = oldContent.substring(currentCursorPosition - searchLength, currentCursorPosition);
else {
if (currentCursorPosition >= oldContent.length())
deltaSearch = oldContent.substring(currentCursorPosition - searchLength);
}
2019-07-20 15:32:17 +02:00
2020-03-08 10:29:06 +01:00
if (!search.equals(""))
deltaSearch = deltaSearch.replace("#" + search, "");
String newContent = oldContent.substring(0, currentCursorPosition - searchLength);
newContent += deltaSearch;
newContent += "#" + suggestion.getContent() + " ";
int newPosition = newContent.length();
if (currentCursorPosition < oldContent.length())
newContent += oldContent.substring(currentCursorPosition);
wysiwygEditText.setText(newContent);
toot_space_left.setText(String.valueOf(countLength(wysiwyg, toot_cw_content)));
wysiwygEditText.setSelection(newPosition);
suggestionsRV.setVisibility(View.GONE);
2019-07-20 15:32:17 +02:00
}));
}
}
}
2017-10-09 18:35:43 +02:00
}
2017-05-05 16:36:04 +02:00
}
2019-09-06 17:55:14 +02:00
private void restoreToot(long id) {
2020-04-09 18:57:12 +02:00
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2017-07-16 19:04:53 +02:00
StoredStatus draft = new StatusStoredDAO(TootActivity.this, db).getStatus(id);
2019-09-06 17:55:14 +02:00
if (draft == null)
2019-01-08 16:24:02 +01:00
return;
2017-07-16 19:04:53 +02:00
Status status = draft.getStatus();
//Retrieves attachments
2019-09-06 17:55:14 +02:00
if (removed) {
2018-08-18 20:12:37 +02:00
new StatusStoredDAO(TootActivity.this, db).remove(draft.getId());
}
restored = id;
2017-07-16 19:04:53 +02:00
attachments = status.getMedia_attachments();
2017-08-06 17:03:15 +02:00
int childCount = toot_picture_container.getChildCount();
ArrayList<ImageView> toRemove = new ArrayList<>();
2019-09-06 17:55:14 +02:00
if (childCount > 0) {
for (int i = 0; i < childCount; i++) {
if (toot_picture_container.getChildAt(i) instanceof ImageView)
toRemove.add((ImageView) toot_picture_container.getChildAt(i));
2017-08-06 17:03:15 +02:00
}
2019-09-06 17:55:14 +02:00
if (toRemove.size() > 0) {
for (ImageView imageView : toRemove)
2017-08-06 17:03:15 +02:00
toot_picture_container.removeView(imageView);
}
toRemove.clear();
}
2018-10-16 18:02:52 +02:00
String content = status.getContent();
2020-03-08 10:29:06 +01:00
Pattern mentionLink = Pattern.compile("(<\\s?a\\s?href=\"https?://([\\da-z.-]+\\.[a-z.]{2,10})/(@[/\\w._-]*)\"\\s?[^.]*<\\s?/\\s?a\\s?>)");
2018-08-18 20:12:37 +02:00
Matcher matcher = mentionLink.matcher(content);
if (matcher.find()) {
content = matcher.replaceAll("$3@$2");
2018-08-18 14:41:02 +02:00
}
2019-03-24 19:06:03 +01:00
2020-03-08 10:29:06 +01:00
Pattern mentionLinkPleroma = Pattern.compile("(<\\s?a\\s?href=\"https?://([\\da-z.-]+\\.[a-z.]{2,10})/users/([/\\w._-]*)\"\\s?[^.]*<\\s?/\\s?a\\s?>)");
Matcher matcherPleroma = mentionLinkPleroma.matcher(content);
if (matcherPleroma.find()) {
content = matcherPleroma.replaceAll("@$3@$2");
}
2019-09-06 17:55:14 +02:00
if (removed) {
2018-10-16 18:02:52 +02:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
content = Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY).toString();
else
content = Html.fromHtml(content).toString();
}
2019-09-06 17:55:14 +02:00
if (status.getPoll() != null) {
2019-03-24 19:06:03 +01:00
poll = status.getPoll();
poll_action.setVisibility(View.VISIBLE);
2019-09-06 17:55:14 +02:00
if (social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON) {
2019-06-30 18:24:47 +02:00
toot_picture.setVisibility(View.GONE);
picture_scrollview.setVisibility(View.GONE);
}
2019-03-24 19:06:03 +01:00
}
2019-09-06 17:55:14 +02:00
if (attachments != null && attachments.size() > 0) {
2017-07-16 19:04:53 +02:00
toot_picture_container.setVisibility(View.VISIBLE);
2017-08-02 17:44:36 +02:00
picture_scrollview.setVisibility(View.VISIBLE);
2019-09-06 17:55:14 +02:00
int i = 0;
for (final Attachment attachment : attachments) {
2017-07-16 19:04:53 +02:00
String url = attachment.getPreview_url();
2019-09-06 17:55:14 +02:00
if (url == null || url.trim().equals(""))
2017-07-16 19:04:53 +02:00
url = attachment.getUrl();
2020-04-08 12:42:15 +02:00
final ImageView imageView = new ImageView(TootActivity.this);
2017-07-16 19:04:53 +02:00
imageView.setId(Integer.parseInt(attachment.getId()));
2017-12-02 16:54:59 +01:00
2017-07-16 19:04:53 +02:00
LinearLayout.LayoutParams imParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
imParams.setMargins(20, 5, 20, 5);
2020-04-08 12:42:15 +02:00
imParams.height = (int) Helper.convertDpToPixel(100, TootActivity.this);
2017-07-16 19:04:53 +02:00
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
toot_picture_container.addView(imageView, i, imParams);
2017-12-02 16:54:59 +01:00
Glide.with(imageView.getContext())
.asBitmap()
.load(url)
2020-04-09 14:59:36 +02:00
.into(new CustomTarget<Bitmap>() {
2017-12-02 16:54:59 +01:00
@Override
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
2017-12-02 16:54:59 +01:00
imageView.setImageBitmap(resource);
}
2020-04-09 14:59:36 +02:00
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
2017-12-02 16:54:59 +01:00
});
imageView.setTag(attachment.getId());
2020-05-23 17:29:12 +02:00
imageView.setOnClickListener(view -> imageView.setOnClickListener(view1 -> showAddDescription(attachment)));
2020-03-08 10:29:06 +01:00
imageView.setOnLongClickListener(view -> {
showRemove(imageView.getId());
return false;
2017-07-16 19:04:53 +02:00
});
addBorder();
2019-09-06 17:55:14 +02:00
if (attachments.size() < max_media_count)
2017-07-16 19:04:53 +02:00
toot_picture.setEnabled(true);
toot_sensitive.setVisibility(View.VISIBLE);
i++;
}
2019-09-06 17:55:14 +02:00
} else {
2017-07-16 19:04:53 +02:00
toot_picture_container.setVisibility(View.GONE);
}
//Sensitive content
toot_sensitive.setChecked(status.isSensitive());
2019-09-06 17:55:14 +02:00
if (status.getSpoiler_text() != null && status.getSpoiler_text().length() > 0) {
2018-10-12 17:53:06 +02:00
toot_cw_content.setText(status.getSpoiler_text());
2017-07-16 19:04:53 +02:00
toot_cw_content.setVisibility(View.VISIBLE);
2019-09-06 17:55:14 +02:00
} else {
2017-07-16 19:04:53 +02:00
toot_cw_content.setText("");
toot_cw_content.setVisibility(View.GONE);
}
2019-09-06 17:55:14 +02:00
if (!displayWYSIWYG()) {
2019-07-20 17:44:44 +02:00
toot_content.setText(content);
2019-09-06 17:55:14 +02:00
} else {
2020-03-08 10:29:06 +01:00
content = content.replaceAll("<p[^>]*></p>", "");
2019-07-20 17:44:44 +02:00
wysiwyg.render(content);
}
2019-07-31 19:14:42 +02:00
toot_space_left.setText(String.valueOf(countLength(social, toot_content, toot_cw_content)));
2019-09-06 17:55:14 +02:00
if (!displayWYSIWYG()) {
2019-07-20 17:44:44 +02:00
toot_content.setSelection(toot_content.getText().length());
2019-09-06 17:55:14 +02:00
} else {
if (wysiwygEditText != null) {
2019-07-20 17:44:44 +02:00
wysiwygEditText.setSelection(wysiwygEditText.getText().length());
}
}
2019-09-06 17:55:14 +02:00
switch (status.getVisibility()) {
2017-07-16 19:04:53 +02:00
case "public":
visibility = "public";
2017-10-29 07:15:13 +01:00
toot_visibility.setImageResource(R.drawable.ic_public_toot);
2017-07-16 19:04:53 +02:00
break;
case "unlisted":
visibility = "unlisted";
2017-10-29 07:15:13 +01:00
toot_visibility.setImageResource(R.drawable.ic_lock_open_toot);
2017-07-16 19:04:53 +02:00
break;
case "private":
visibility = "private";
2017-10-29 07:15:13 +01:00
toot_visibility.setImageResource(R.drawable.ic_lock_outline_toot);
2017-07-16 19:04:53 +02:00
break;
case "direct":
visibility = "direct";
2017-10-29 07:15:13 +01:00
toot_visibility.setImageResource(R.drawable.ic_mail_outline_toot);
2017-07-16 19:04:53 +02:00
break;
}
2017-10-28 14:32:18 +02:00
2017-07-16 19:04:53 +02:00
//The current id is set to the draft
currentToId = draft.getId();
tootReply = draft.getStatusReply();
2019-09-06 17:55:14 +02:00
if (tootReply != null) {
tootReply();
2017-10-01 19:01:15 +02:00
2019-09-06 17:55:14 +02:00
} else {
if (title != null) {
if (social == UpdateAccountInfoAsyncTask.SOCIAL.GNU)
2019-02-21 20:04:16 +01:00
title.setText(getString(R.string.queet_title));
else
title.setText(getString(R.string.toot_title));
2019-09-06 17:55:14 +02:00
} else {
2019-07-31 19:14:42 +02:00
if (social == UpdateAccountInfoAsyncTask.SOCIAL.GNU)
2019-02-21 20:04:16 +01:00
setTitle(R.string.queet_title);
else
setTitle(R.string.toot_title);
}
}
2017-10-01 19:01:15 +02:00
invalidateOptionsMenu();
2019-09-06 17:55:14 +02:00
initialContent = displayWYSIWYG() ? wysiwyg.getContentAsHTML() : toot_content.getText().toString();
if (!displayWYSIWYG()) {
2019-07-31 19:14:42 +02:00
toot_space_left.setText(String.valueOf(countLength(social, toot_content, toot_cw_content)));
2019-09-06 17:55:14 +02:00
} else {
2019-07-20 17:44:44 +02:00
toot_space_left.setText(String.valueOf(countLength(wysiwyg, toot_cw_content)));
}
2017-07-16 19:04:53 +02:00
}
2019-09-06 17:55:14 +02:00
private void restoreServerSchedule(Status status) {
2019-01-20 15:20:07 +01:00
attachments = status.getMedia_attachments();
int childCount = toot_picture_container.getChildCount();
ArrayList<ImageView> toRemove = new ArrayList<>();
2019-09-06 17:55:14 +02:00
if (childCount > 0) {
for (int i = 0; i < childCount; i++) {
if (toot_picture_container.getChildAt(i) instanceof ImageView)
toRemove.add((ImageView) toot_picture_container.getChildAt(i));
2019-01-20 15:20:07 +01:00
}
2019-09-06 17:55:14 +02:00
if (toRemove.size() > 0) {
for (ImageView imageView : toRemove)
2019-01-20 15:20:07 +01:00
toot_picture_container.removeView(imageView);
}
toRemove.clear();
}
String content = status.getContent();
2020-03-08 10:29:06 +01:00
Pattern mentionLink = Pattern.compile("(<\\s?a\\s?href=\"https?://([\\da-z.-]+\\.[a-z.]{2,10})/(@[/\\w._-]*)\"\\s?[^.]*<\\s?/\\s?a\\s?>)");
2019-01-20 15:20:07 +01:00
Matcher matcher = mentionLink.matcher(content);
if (matcher.find()) {
content = matcher.replaceAll("$3@$2");
}
2019-09-06 17:55:14 +02:00
if (removed) {
2019-01-20 15:20:07 +01:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
content = Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY).toString();
else
content = Html.fromHtml(content).toString();
}
2019-09-06 17:55:14 +02:00
if (attachments != null && attachments.size() > 0) {
2019-01-20 15:20:07 +01:00
toot_picture_container.setVisibility(View.VISIBLE);
picture_scrollview.setVisibility(View.VISIBLE);
2019-09-06 17:55:14 +02:00
int i = 0;
for (final Attachment attachment : attachments) {
2019-01-20 15:20:07 +01:00
String url = attachment.getPreview_url();
2019-09-06 17:55:14 +02:00
if (url == null || url.trim().equals(""))
2019-01-20 15:20:07 +01:00
url = attachment.getUrl();
2020-04-08 12:42:15 +02:00
final ImageView imageView = new ImageView(TootActivity.this);
2019-01-20 15:20:07 +01:00
imageView.setId(Integer.parseInt(attachment.getId()));
LinearLayout.LayoutParams imParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
imParams.setMargins(20, 5, 20, 5);
2020-04-08 12:42:15 +02:00
imParams.height = (int) Helper.convertDpToPixel(100, TootActivity.this);
2019-01-20 15:20:07 +01:00
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
toot_picture_container.addView(imageView, i, imParams);
Glide.with(imageView.getContext())
.asBitmap()
.load(url)
2020-04-09 14:59:36 +02:00
.into(new CustomTarget<Bitmap>() {
2019-01-20 15:20:07 +01:00
@Override
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
imageView.setImageBitmap(resource);
}
2020-04-09 14:59:36 +02:00
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
2019-01-20 15:20:07 +01:00
});
imageView.setTag(attachment.getId());
2020-03-08 10:29:06 +01:00
imageView.setOnClickListener(view -> {
2020-03-12 18:09:41 +01:00
imageView.setOnClickListener(view1 -> showAddDescription(attachment));
2019-01-20 15:20:07 +01:00
});
2020-03-08 10:29:06 +01:00
imageView.setOnLongClickListener(view -> {
showRemove(imageView.getId());
return false;
2019-01-20 15:20:07 +01:00
});
addBorder();
2019-09-06 17:55:14 +02:00
if (attachments.size() < max_media_count)
2019-01-20 15:20:07 +01:00
toot_picture.setEnabled(true);
toot_sensitive.setVisibility(View.VISIBLE);
i++;
}
2019-09-06 17:55:14 +02:00
} else {
2019-01-20 15:20:07 +01:00
toot_picture_container.setVisibility(View.GONE);
}
//Sensitive content
toot_sensitive.setChecked(status.isSensitive());
2019-09-06 17:55:14 +02:00
if (status.getSpoiler_text() != null && status.getSpoiler_text().length() > 0) {
2019-01-20 15:20:07 +01:00
toot_cw_content.setText(status.getSpoiler_text());
toot_cw_content.setVisibility(View.VISIBLE);
2019-09-06 17:55:14 +02:00
} else {
2019-01-20 15:20:07 +01:00
toot_cw_content.setText("");
toot_cw_content.setVisibility(View.GONE);
}
2020-06-08 17:59:12 +02:00
if( status.getVisibility() == null) {
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
String defaultVisibility = account.isLocked() ? "private" : "public";
String settingsVisibility = sharedpreferences.getString(Helper.SET_TOOT_VISIBILITY + "@" + account.getAcct() + "@" + account.getInstance(), defaultVisibility);
status.setVisibility(settingsVisibility);
}
2019-01-20 15:20:07 +01:00
toot_content.setText(content);
2019-07-31 19:14:42 +02:00
toot_space_left.setText(String.valueOf(countLength(social, toot_content, toot_cw_content)));
2019-01-20 15:20:07 +01:00
toot_content.setSelection(toot_content.getText().length());
2019-09-06 17:55:14 +02:00
switch (status.getVisibility()) {
2019-01-20 15:20:07 +01:00
case "public":
visibility = "public";
toot_visibility.setImageResource(R.drawable.ic_public_toot);
break;
case "unlisted":
visibility = "unlisted";
toot_visibility.setImageResource(R.drawable.ic_lock_open_toot);
break;
case "private":
visibility = "private";
toot_visibility.setImageResource(R.drawable.ic_lock_outline_toot);
break;
case "direct":
visibility = "direct";
toot_visibility.setImageResource(R.drawable.ic_mail_outline_toot);
break;
}
2019-09-06 17:55:14 +02:00
if (title != null) {
2019-07-31 19:14:42 +02:00
if (social == UpdateAccountInfoAsyncTask.SOCIAL.GNU)
2019-02-21 20:04:16 +01:00
title.setText(getString(R.string.queet_title));
else
title.setText(getString(R.string.toot_title));
2019-09-06 17:55:14 +02:00
} else {
2019-07-31 19:14:42 +02:00
if (social == UpdateAccountInfoAsyncTask.SOCIAL.GNU)
2019-02-21 20:04:16 +01:00
setTitle(R.string.queet_title);
else
setTitle(R.string.toot_title);
}
2019-01-20 15:20:07 +01:00
invalidateOptionsMenu();
2019-09-06 17:55:14 +02:00
initialContent = displayWYSIWYG() ? wysiwyg.getContentAsHTML() : toot_content.getText().toString();
2019-07-31 19:14:42 +02:00
toot_space_left.setText(String.valueOf(countLength(social, toot_content, toot_cw_content)));
2019-01-20 15:20:07 +01:00
}
2019-09-06 17:55:14 +02:00
private void tootReply() {
2019-05-18 11:10:30 +02:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
2019-09-06 17:55:14 +02:00
if (title != null) {
2019-07-31 19:14:42 +02:00
if (social == UpdateAccountInfoAsyncTask.SOCIAL.GNU)
2019-02-21 20:04:16 +01:00
title.setText(getString(R.string.queet_title_reply));
else
title.setText(getString(R.string.toot_title_reply));
2019-09-06 17:55:14 +02:00
} else {
2019-07-31 19:14:42 +02:00
if (social == UpdateAccountInfoAsyncTask.SOCIAL.GNU)
2019-02-21 20:04:16 +01:00
setTitle(R.string.queet_title_reply);
else
setTitle(R.string.toot_title_reply);
}
2017-12-28 17:25:36 +01:00
String userIdReply;
2019-09-06 17:55:14 +02:00
if (accountReply == null)
2017-12-28 17:25:36 +01:00
userIdReply = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
2017-10-04 15:57:53 +02:00
else
2017-12-28 17:25:36 +01:00
userIdReply = accountReply.getId();
2017-08-08 18:15:37 +02:00
//If toot is not restored
2019-09-06 17:55:14 +02:00
if (restored == -1) {
//Gets the default visibility, will be used if not set in settings
2019-09-06 17:55:14 +02:00
String defaultVisibility = account.isLocked() ? "private" : "public";
String settingsVisibility = sharedpreferences.getString(Helper.SET_TOOT_VISIBILITY + "@" + account.getAcct() + "@" + account.getInstance(), defaultVisibility);
int initialTootVisibility = 0;
int ownerTootVisibility = 0;
2019-06-27 17:13:26 +02:00
String temp_visibility = tootReply.getVisibility();
2019-09-06 17:55:14 +02:00
if (quickmessagevisibility != null) {
2019-06-27 17:13:26 +02:00
temp_visibility = quickmessagevisibility;
}
2019-09-06 17:55:14 +02:00
switch (temp_visibility) {
case "public":
initialTootVisibility = 4;
break;
case "unlisted":
2019-09-06 17:55:14 +02:00
initialTootVisibility = 3;
break;
case "private":
visibility = "private";
initialTootVisibility = 2;
break;
case "direct":
visibility = "direct";
initialTootVisibility = 1;
break;
}
2019-11-09 18:07:33 +01:00
switch (settingsVisibility) {
case "public":
ownerTootVisibility = 4;
break;
case "unlisted":
ownerTootVisibility = 3;
break;
case "private":
visibility = "private";
ownerTootVisibility = 2;
break;
case "direct":
visibility = "direct";
ownerTootVisibility = 1;
break;
}
int tootVisibility;
2019-09-06 17:55:14 +02:00
if (ownerTootVisibility >= initialTootVisibility) {
tootVisibility = initialTootVisibility;
2019-09-06 17:55:14 +02:00
} else {
tootVisibility = ownerTootVisibility;
}
2019-09-06 17:55:14 +02:00
switch (tootVisibility) {
case 4:
visibility = "public";
toot_visibility.setImageResource(R.drawable.ic_public_toot);
break;
case 3:
visibility = "unlisted";
toot_visibility.setImageResource(R.drawable.ic_lock_open_toot);
break;
case 2:
visibility = "private";
toot_visibility.setImageResource(R.drawable.ic_lock_outline_toot);
break;
case 1:
visibility = "direct";
toot_visibility.setImageResource(R.drawable.ic_mail_outline_toot);
break;
}
2019-09-06 17:55:14 +02:00
if (tootReply.getSpoiler_text() != null && tootReply.getSpoiler_text().length() > 0) {
2017-11-07 16:08:31 +01:00
toot_cw_content.setText(tootReply.getSpoiler_text());
toot_cw_content.setVisibility(View.VISIBLE);
}
2019-09-06 17:55:14 +02:00
if (tootReply != null) {
2020-04-08 12:42:15 +02:00
manageMentions(TootActivity.this, social, userIdReply, toot_content, toot_cw_content, toot_space_left, tootReply);
}
2019-05-13 18:15:45 +02:00
boolean forwardTags = sharedpreferences.getBoolean(Helper.SET_FORWARD_TAGS_IN_REPLY, false);
2019-09-06 17:55:14 +02:00
if (tootReply != null && forwardTags && tootReply.getTags() != null && tootReply.getTags().size() > 0) {
2019-05-13 18:15:45 +02:00
int currentCursorPosition = toot_content.getSelectionStart();
2020-03-08 10:29:06 +01:00
String contentStr = toot_content.getText() + "\n";
toot_content.setText(contentStr);
2019-09-06 17:55:14 +02:00
for (Tag tag : tootReply.getTags()) {
2020-03-08 10:29:06 +01:00
String tempContentStr = toot_content.getText() + " #" + tag.getName();
toot_content.setText(tempContentStr);
2019-05-13 18:15:45 +02:00
}
toot_content.setSelection(currentCursorPosition);
2019-07-31 19:14:42 +02:00
toot_space_left.setText(String.valueOf(countLength(social, toot_content, toot_cw_content)));
2019-05-13 18:15:45 +02:00
}
}
2019-09-06 17:55:14 +02:00
initialContent = displayWYSIWYG() ? wysiwyg.getContentAsHTML() : toot_content.getText().toString();
}
2019-09-06 17:55:14 +02:00
private void displayPollPopup() {
2019-03-24 19:06:03 +01:00
AlertDialog.Builder alertPoll = new AlertDialog.Builder(TootActivity.this, style);
alertPoll.setTitle(R.string.create_poll);
2020-05-23 17:29:12 +02:00
View view = getLayoutInflater().inflate(R.layout.popup_poll, new LinearLayout(TootActivity.this), false);
2019-03-24 19:06:03 +01:00
alertPoll.setView(view);
Spinner poll_choice = view.findViewById(R.id.poll_choice);
Spinner poll_duration = view.findViewById(R.id.poll_duration);
EditText choice_1 = view.findViewById(R.id.choice_1);
EditText choice_2 = view.findViewById(R.id.choice_2);
2019-06-04 11:02:15 +02:00
ImageButton add = view.findViewById(R.id.add_poll_item);
ImageButton remove = view.findViewById(R.id.remove_poll_item);
LinearLayout poll_items_container = view.findViewById(R.id.poll_items_container);
int max_entry = 4;
int max_length = 25;
pollCountItem = 2;
2019-06-04 19:45:27 +02:00
remove.setVisibility(View.GONE);
2019-09-06 17:55:14 +02:00
if (MainActivity.poll_limits != null && MainActivity.poll_limits.containsKey("max_options")) {
max_entry = MainActivity.poll_limits.get("max_options") != null ? MainActivity.poll_limits.get("max_options") : 4;
2019-06-04 11:02:15 +02:00
}
2019-09-06 17:55:14 +02:00
if (MainActivity.poll_limits != null && MainActivity.poll_limits.containsKey("max_option_chars")) {
max_length = MainActivity.poll_limits.get("max_option_chars") != null ? MainActivity.poll_limits.get("max_option_chars") : 25;
2019-06-04 11:02:15 +02:00
}
InputFilter[] fArray = new InputFilter[1];
fArray[0] = new InputFilter.LengthFilter(max_length);
choice_1.setFilters(fArray);
choice_2.setFilters(fArray);
int finalMax_entry = max_entry;
int finalMax_length = max_length;
2020-03-08 10:29:06 +01:00
add.setOnClickListener(v -> {
if (pollCountItem < finalMax_entry) {
EditText poll_item = new EditText(TootActivity.this);
InputFilter[] fArray1 = new InputFilter[1];
fArray1[0] = new InputFilter.LengthFilter(finalMax_length);
poll_item.setFilters(fArray1);
poll_item.setHint(getString(R.string.poll_choice_s, (pollCountItem + 1)));
poll_items_container.addView(poll_item);
}
pollCountItem++;
if (pollCountItem >= finalMax_entry) {
add.setVisibility(View.GONE);
} else {
add.setVisibility(View.VISIBLE);
2019-06-04 11:02:15 +02:00
}
2020-03-08 10:29:06 +01:00
remove.setVisibility(View.VISIBLE);
2019-06-04 11:02:15 +02:00
});
2020-03-08 10:29:06 +01:00
remove.setOnClickListener(v -> {
if (pollCountItem > 2) {
int childCount = poll_items_container.getChildCount();
poll_items_container.removeViewAt(childCount - 1);
}
pollCountItem--;
if (pollCountItem <= 2) {
remove.setVisibility(View.GONE);
} else {
remove.setVisibility(View.VISIBLE);
2019-06-04 11:02:15 +02:00
}
2020-03-08 10:29:06 +01:00
add.setVisibility(View.VISIBLE);
2019-06-04 11:02:15 +02:00
});
2019-03-24 19:06:03 +01:00
ArrayAdapter<CharSequence> pollduration = ArrayAdapter.createFromResource(TootActivity.this,
2019-11-20 11:57:25 +01:00
R.array.poll_duration, android.R.layout.simple_spinner_dropdown_item);
2019-03-24 19:06:03 +01:00
ArrayAdapter<CharSequence> pollchoice = ArrayAdapter.createFromResource(TootActivity.this,
2019-11-20 11:57:25 +01:00
R.array.poll_choice_type, android.R.layout.simple_spinner_dropdown_item);
2019-03-24 19:06:03 +01:00
poll_choice.setAdapter(pollchoice);
poll_duration.setAdapter(pollduration);
poll_duration.setSelection(4);
poll_choice.setSelection(0);
2019-09-06 17:55:14 +02:00
if (poll != null && poll.getOptionsList() != null) {
2019-03-24 19:06:03 +01:00
int i = 1;
2019-09-06 17:55:14 +02:00
for (PollOptions pollOptions : poll.getOptionsList()) {
if (i == 1) {
if (pollOptions.getTitle() != null)
2019-06-04 11:02:15 +02:00
choice_1.setText(pollOptions.getTitle());
2019-09-06 17:55:14 +02:00
} else if (i == 2) {
if (pollOptions.getTitle() != null)
2019-06-04 11:02:15 +02:00
choice_2.setText(pollOptions.getTitle());
2019-09-06 17:55:14 +02:00
} else {
2019-06-04 11:02:15 +02:00
EditText poll_item = new EditText(TootActivity.this);
fArray = new InputFilter[1];
fArray[0] = new InputFilter.LengthFilter(finalMax_length);
poll_item.setFilters(fArray);
2019-09-06 17:55:14 +02:00
poll_item.setHint(getString(R.string.poll_choice_s, (pollCountItem + 1)));
if (pollOptions.getTitle() != null) {
2019-06-04 11:02:15 +02:00
poll_item.setText(pollOptions.getTitle());
}
poll_items_container.addView(poll_item);
pollCountItem++;
2019-03-24 19:06:03 +01:00
}
i++;
}
2019-06-04 19:45:27 +02:00
remove.setVisibility(View.VISIBLE);
add.setVisibility(View.VISIBLE);
2019-09-06 17:55:14 +02:00
if (poll.getOptionsList().size() >= max_entry) {
2019-06-04 19:45:27 +02:00
add.setVisibility(View.GONE);
2019-09-06 17:55:14 +02:00
} else if (poll.getOptionsList().size() <= 2) {
2019-06-04 19:45:27 +02:00
remove.setVisibility(View.GONE);
}
2019-09-06 17:55:14 +02:00
switch (poll.getExpires_in()) {
2019-03-24 19:06:03 +01:00
case 300:
poll_duration.setSelection(0);
break;
case 1800:
poll_duration.setSelection(1);
break;
case 3600:
poll_duration.setSelection(2);
break;
case 21600:
poll_duration.setSelection(3);
break;
case 86400:
poll_duration.setSelection(4);
break;
case 259200:
poll_duration.setSelection(5);
break;
case 604800:
poll_duration.setSelection(6);
break;
}
2019-09-06 17:55:14 +02:00
if (poll.isMultiple())
2019-03-24 19:06:03 +01:00
poll_choice.setSelection(1);
else
poll_choice.setSelection(0);
}
2020-03-08 10:29:06 +01:00
alertPoll.setNegativeButton(R.string.delete, (dialog, whichButton) -> {
if (poll != null)
poll = null;
poll_action.setVisibility(View.GONE);
toot_picture.setVisibility(View.VISIBLE);
if (attachments != null && attachments.size() > 0)
picture_scrollview.setVisibility(View.VISIBLE);
dialog.dismiss();
2019-03-24 19:06:03 +01:00
});
alertPoll.setPositiveButton(R.string.validate, null);
final AlertDialog alertPollDiaslog = alertPoll.create();
2020-03-08 10:29:06 +01:00
alertPollDiaslog.setOnShowListener(dialog -> {
2019-03-24 19:06:03 +01:00
2020-03-08 10:29:06 +01:00
Button b = alertPollDiaslog.getButton(AlertDialog.BUTTON_POSITIVE);
b.setOnClickListener(view1 -> {
int poll_duration_pos = poll_duration.getSelectedItemPosition();
2019-03-24 19:06:03 +01:00
2020-03-08 10:29:06 +01:00
int poll_choice_pos = poll_choice.getSelectedItemPosition();
String choice1 = choice_1.getText().toString().trim();
String choice2 = choice_2.getText().toString().trim();
2019-03-24 19:06:03 +01:00
2020-03-08 10:29:06 +01:00
if (choice1.isEmpty() && choice2.isEmpty()) {
2020-04-08 12:42:15 +02:00
Toasty.error(TootActivity.this, getString(R.string.poll_invalid_choices), Toast.LENGTH_SHORT).show();
2020-03-08 10:29:06 +01:00
} else {
poll = new Poll();
poll.setMultiple(poll_choice_pos != 0);
int expire;
switch (poll_duration_pos) {
case 0:
expire = 300;
break;
case 1:
expire = 1800;
break;
case 2:
expire = 3600;
break;
case 3:
expire = 21600;
break;
case 4:
expire = 86400;
break;
case 5:
expire = 259200;
break;
case 6:
expire = 604800;
break;
default:
expire = 864000;
}
poll.setExpires_in(expire);
2019-03-24 19:06:03 +01:00
2020-03-08 10:29:06 +01:00
List<PollOptions> pollOptions = new ArrayList<>();
PollOptions pollOption1 = new PollOptions();
pollOption1.setTitle(choice1);
pollOptions.add(pollOption1);
2019-03-24 19:06:03 +01:00
2020-03-08 10:29:06 +01:00
PollOptions pollOption2 = new PollOptions();
pollOption2.setTitle(choice2);
pollOptions.add(pollOption2);
int childCount = poll_items_container.getChildCount();
if (childCount > 2) {
for (int i = 2; i < childCount; i++) {
PollOptions pollItem = new PollOptions();
pollItem.setTitle(((EditText) poll_items_container.getChildAt(i)).getText().toString());
pollOptions.add(pollItem);
}
2020-03-08 10:29:06 +01:00
}
List<String> options = new ArrayList<>();
boolean doubleTitle = false;
for (PollOptions po : pollOptions) {
if (!options.contains(po.getTitle().trim())) {
options.add(po.getTitle().trim());
} else {
doubleTitle = true;
2019-06-04 11:02:15 +02:00
}
}
2020-03-08 10:29:06 +01:00
if (!doubleTitle) {
poll.setOptionsList(pollOptions);
dialog.dismiss();
} else {
2020-04-08 12:42:15 +02:00
Toasty.error(TootActivity.this, getString(R.string.poll_duplicated_entry), Toast.LENGTH_SHORT).show();
2020-03-08 10:29:06 +01:00
}
}
poll_action.setVisibility(View.VISIBLE);
if (social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON) {
toot_picture.setVisibility(View.GONE);
picture_scrollview.setVisibility(View.GONE);
}
});
2019-03-24 19:06:03 +01:00
});
alertPollDiaslog.show();
2019-03-24 19:06:03 +01:00
}
2019-09-06 17:55:14 +02:00
private void storeToot(boolean message, boolean forced) {
2017-07-15 14:59:09 +02:00
//Nothing to store here....
2019-07-23 11:15:35 +02:00
String currentContent;
2019-09-06 17:55:14 +02:00
if (displayWYSIWYG()) {
2019-07-23 11:15:35 +02:00
currentContent = wysiwyg.getContentAsHTML().trim();
2019-09-06 17:55:14 +02:00
} else {
2019-07-23 11:15:35 +02:00
currentContent = toot_content.getText().toString().trim();
}
2019-10-13 17:17:46 +02:00
if (!forced && !removed) {
2019-07-23 11:15:35 +02:00
if (currentContent.length() == 0 && (attachments == null || attachments.size() < 1) && toot_cw_content.getText().toString().trim().length() == 0)
2019-07-20 17:44:44 +02:00
return;
2019-07-23 11:15:35 +02:00
if (initialContent.trim().equals(currentContent))
2017-11-15 14:14:41 +01:00
return;
}
2017-07-15 14:59:09 +02:00
Status toot = new Status();
toot.setSensitive(isSensitive);
toot.setMedia_attachments(attachments);
2019-09-06 17:55:14 +02:00
if (toot_cw_content.getText().toString().trim().length() > 0)
2017-07-15 14:59:09 +02:00
toot.setSpoiler_text(toot_cw_content.getText().toString().trim());
toot.setVisibility(visibility);
2019-11-30 08:53:37 +01:00
toot.setContent(TootActivity.this, currentContent);
2017-09-22 14:44:00 +02:00
2019-09-06 17:55:14 +02:00
if (poll != null)
2019-03-24 19:06:03 +01:00
toot.setPoll(poll);
2019-09-06 17:55:14 +02:00
if (tootReply != null)
2017-07-15 14:59:09 +02:00
toot.setIn_reply_to_id(tootReply.getId());
2020-04-09 18:57:12 +02:00
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2019-09-06 17:55:14 +02:00
try {
if (currentToId == -1) {
currentToId = new StatusStoredDAO(TootActivity.this, db).insertStatus(toot, tootReply);
2017-07-15 14:59:09 +02:00
2019-09-06 17:55:14 +02:00
} else {
2017-07-15 14:59:09 +02:00
StoredStatus storedStatus = new StatusStoredDAO(TootActivity.this, db).getStatus(currentToId);
2019-09-06 17:55:14 +02:00
if (storedStatus != null) {
2017-07-15 14:59:09 +02:00
new StatusStoredDAO(TootActivity.this, db).updateStatus(currentToId, toot);
2019-09-06 17:55:14 +02:00
} else { //Might have been deleted, so it needs insertion
new StatusStoredDAO(TootActivity.this, db).insertStatus(toot, tootReply);
2017-07-15 14:59:09 +02:00
}
}
2019-09-06 17:55:14 +02:00
if (message)
2020-04-08 12:42:15 +02:00
Toasty.success(TootActivity.this, getString(R.string.toast_toot_saved), Toast.LENGTH_LONG).show();
2019-09-06 17:55:14 +02:00
} catch (Exception e) {
if (message)
2020-04-08 12:42:15 +02:00
Toasty.error(TootActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show();
2017-07-15 14:59:09 +02:00
}
}
2019-09-06 17:55:14 +02:00
2017-10-09 18:35:43 +02:00
2017-10-25 07:53:31 +02:00
@Override
2017-10-25 10:51:36 +02:00
public void onRetrieveAccountsReply(ArrayList<Account> accounts) {
final boolean[] checkedValues = new boolean[accounts.size()];
2017-10-25 09:04:34 +02:00
int i = 0;
2019-09-06 17:55:14 +02:00
for (Account account : accounts) {
2017-10-25 10:51:36 +02:00
checkedValues[i] = toot_content.getText().toString().contains("@" + account.getAcct());
2017-10-25 09:04:34 +02:00
i++;
2017-10-25 07:53:31 +02:00
}
2018-11-03 14:45:55 +01:00
final AlertDialog.Builder builderSingle = new AlertDialog.Builder(TootActivity.this, style);
2019-10-17 18:20:21 +02:00
AccountsReplyAdapter accountsReplyAdapter = new AccountsReplyAdapter(new WeakReference<>(TootActivity.this), accounts, checkedValues);
2017-10-25 11:00:04 +02:00
builderSingle.setTitle(getString(R.string.select_accounts)).setAdapter(accountsReplyAdapter, null);
2020-03-08 10:29:06 +01:00
builderSingle.setNegativeButton(R.string.validate, (dialog, which) -> {
dialog.dismiss();
toot_content.setSelection(toot_content.getText().length());
2017-10-25 07:53:31 +02:00
});
builderSingle.show();
}
2017-10-25 10:51:36 +02:00
2019-09-06 17:55:14 +02:00
public void changeAccountReply(boolean isChecked, String acct) {
2017-10-25 10:51:36 +02:00
if (isChecked) {
2019-09-06 17:55:14 +02:00
if (!toot_content.getText().toString().contains(acct))
toot_content.setText(String.format("%s %s", acct, toot_content.getText()));
2017-10-25 10:51:36 +02:00
} else {
2019-09-06 17:55:14 +02:00
toot_content.setText(toot_content.getText().toString().replaceAll("\\s*" + acct, ""));
2017-10-25 10:51:36 +02:00
}
}
2018-09-01 18:24:01 +02:00
@Override
public void onStart() {
super.onStart();
active = true;
}
2018-09-01 18:24:01 +02:00
@Override
public void onStop() {
super.onStop();
active = false;
}
2017-11-01 19:19:37 +01:00
2019-09-06 17:55:14 +02:00
private void addBorder() {
for (int i = 0; i < toot_picture_container.getChildCount(); i++) {
View v = toot_picture_container.getChildAt(i);
if (v instanceof ImageView) {
2019-09-06 17:55:14 +02:00
for (Attachment attachment : attachments) {
if (attachment.getType().toLowerCase().equals("image"))
if (v.getTag().toString().trim().equals(attachment.getId().trim())) {
int borderSize = (int) Helper.convertDpToPixel(1, TootActivity.this);
int borderSizeTop = (int) Helper.convertDpToPixel(6, TootActivity.this);
v.setPadding(borderSize, borderSizeTop, borderSize, borderSizeTop);
if (attachment.getDescription() == null || attachment.getDescription().trim().equals("null") || attachment.getDescription().trim().equals("")) {
v.setBackgroundColor(ContextCompat.getColor(TootActivity.this, R.color.red_1));
} else
v.setBackgroundColor(ContextCompat.getColor(TootActivity.this, R.color.green_1));
}
}
}
}
}
2017-11-01 19:19:37 +01:00
2019-09-06 17:55:14 +02:00
int countLength(Editor wysiwyg, EditText toot_cw_content) {
if (wysiwyg == null || toot_cw_content == null) {
2019-07-20 15:15:47 +02:00
return -1;
}
2019-07-20 17:44:44 +02:00
String content = wysiwyg.getContentAsHTML();
2019-07-20 15:15:47 +02:00
String cwContent = toot_cw_content.getText().toString();
int contentLength = content.length() - countWithEmoji(content);
int cwLength = cwContent.length() - countWithEmoji(cwContent);
return cwLength + contentLength;
}
2019-06-26 11:00:59 +02:00
2019-09-06 17:55:14 +02:00
private void recordAudio() {
2019-06-26 11:20:20 +02:00
String filePath = getCacheDir() + "/fedilab_recorded_audio.wav";
2019-06-26 11:00:59 +02:00
int color = getResources().getColor(R.color.mastodonC1);
AndroidAudioRecorder.with(this)
// Required
.setFilePath(filePath)
.setColor(color)
.setRequestCode(SEND_VOICE_MESSAGE)
// Optional
.setSource(AudioSource.MIC)
.setChannel(AudioChannel.STEREO)
.setSampleRate(AudioSampleRate.HZ_44100)
.setAutoStart(true)
.setKeepDisplayOn(true)
// Start recording
.record();
}
2019-07-20 15:15:47 +02:00
2019-09-06 17:55:14 +02:00
private boolean displayWYSIWYG() {
if (social != UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA) {
2019-07-20 17:44:44 +02:00
return false;
}
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
return sharedpreferences.getBoolean(Helper.SET_WYSIWYG, false);
2019-07-20 17:44:44 +02:00
}
2019-09-06 17:55:14 +02:00
private void renderEditor() {
2019-07-20 15:15:47 +02:00
2020-03-08 10:29:06 +01:00
findViewById(R.id.action_h1).setOnClickListener(v -> wysiwyg.updateTextStyle(EditorTextStyle.H1));
2019-07-20 15:15:47 +02:00
2020-03-08 10:29:06 +01:00
findViewById(R.id.action_h2).setOnClickListener(v -> wysiwyg.updateTextStyle(EditorTextStyle.H2));
2019-07-20 15:15:47 +02:00
2020-03-08 10:29:06 +01:00
findViewById(R.id.action_h3).setOnClickListener(v -> wysiwyg.updateTextStyle(EditorTextStyle.H3));
2019-07-20 15:15:47 +02:00
2020-03-08 10:29:06 +01:00
findViewById(R.id.action_bold).setOnClickListener(v -> wysiwyg.updateTextStyle(EditorTextStyle.BOLD));
2019-07-20 15:15:47 +02:00
2020-03-08 10:29:06 +01:00
findViewById(R.id.action_Italic).setOnClickListener(v -> wysiwyg.updateTextStyle(EditorTextStyle.ITALIC));
2019-07-20 15:15:47 +02:00
2020-03-08 10:29:06 +01:00
findViewById(R.id.action_indent).setOnClickListener(v -> wysiwyg.updateTextStyle(EditorTextStyle.INDENT));
2019-07-20 15:15:47 +02:00
2020-03-08 10:29:06 +01:00
findViewById(R.id.action_outdent).setOnClickListener(v -> wysiwyg.updateTextStyle(EditorTextStyle.OUTDENT));
2019-07-20 15:15:47 +02:00
2020-03-08 10:29:06 +01:00
findViewById(R.id.action_bulleted).setOnClickListener(v -> wysiwyg.insertList(false));
2019-07-20 15:15:47 +02:00
2020-04-08 12:42:15 +02:00
findViewById(R.id.action_color).setOnClickListener(v -> new ColorPickerPopup.Builder(TootActivity.this)
2020-03-08 10:29:06 +01:00
.enableAlpha(false)
.okTitle(getString(R.string.validate))
.cancelTitle(getString(R.string.cancel))
.showIndicator(true)
.showValue(true)
.build()
.show(findViewById(android.R.id.content), new ColorPickerPopup.ColorPickerObserver() {
@Override
public void onColorPicked(int color) {
wysiwyg.updateTextColor(colorHex(color));
}
2019-07-20 17:44:44 +02:00
2020-03-08 10:29:06 +01:00
}));
2019-07-21 16:29:23 +02:00
//Remove colours
findViewById(R.id.action_color).setVisibility(View.GONE);
2019-07-20 15:15:47 +02:00
2020-03-08 10:29:06 +01:00
findViewById(R.id.action_unordered_numbered).setOnClickListener(v -> wysiwyg.insertList(true));
2019-07-20 15:15:47 +02:00
2020-03-08 10:29:06 +01:00
findViewById(R.id.action_hr).setOnClickListener(v -> wysiwyg.insertDivider());
2019-07-20 15:15:47 +02:00
2020-03-08 10:29:06 +01:00
findViewById(R.id.action_insert_image).setOnClickListener(v -> wysiwyg.openImagePicker());
2019-07-20 15:15:47 +02:00
2020-03-08 10:29:06 +01:00
findViewById(R.id.action_insert_link).setOnClickListener(v -> wysiwyg.insertLink());
2019-07-20 15:15:47 +02:00
2020-03-08 10:29:06 +01:00
findViewById(R.id.action_erase).setOnClickListener(v -> wysiwyg.clearAllContents());
2019-07-21 16:29:23 +02:00
//Remove eraser
findViewById(R.id.action_erase).setVisibility(View.GONE);
2019-07-20 15:15:47 +02:00
2020-03-08 10:29:06 +01:00
findViewById(R.id.action_blockquote).setOnClickListener(v -> {
wysiwyg.updateTextStyle(EditorTextStyle.BLOCKQUOTE);
wysiwyg.updateTextColor("#000000");
2019-07-20 15:15:47 +02:00
});
wysiwyg.render();
}
2019-07-20 17:44:44 +02:00
private String colorHex(int color) {
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
return String.format(Locale.getDefault(), "#%02X%02X%02X", r, g, b);
}
2019-11-15 16:32:25 +01:00
static class asyncPicture extends AsyncTask<Void, Void, Void> {
String commpressedFilePath;
WeakReference<Activity> activityWeakReference;
android.net.Uri uriFile;
boolean error = false;
UploadServiceSingleBroadcastReceiver uploadReceiver;
String filename;
UpdateAccountInfoAsyncTask.SOCIAL social;
private Account account;
asyncPicture(Activity activity, Account account, UpdateAccountInfoAsyncTask.SOCIAL social, android.net.Uri uri, String filename, UploadServiceSingleBroadcastReceiver uploadReceiver) {
this.activityWeakReference = new WeakReference<>(activity);
this.uriFile = uri;
this.uploadReceiver = uploadReceiver;
this.filename = filename;
this.social = social;
this.account = account;
}
@Override
protected void onPreExecute() {
if (uriFile == null) {
Toasty.error(activityWeakReference.get(), activityWeakReference.get().getString(R.string.toast_error), Toast.LENGTH_SHORT).show();
error = true;
}
if (activityWeakReference.get().findViewById(R.id.compression_loader) != null) {
activityWeakReference.get().findViewById(R.id.compression_loader).setVisibility(View.VISIBLE);
}
}
@Override
protected Void doInBackground(Void... voids) {
if (error) {
return null;
}
commpressedFilePath = Helper.compressImagePath(activityWeakReference.get(), uriFile);
return null;
}
@Override
protected void onPostExecute(Void result) {
2020-03-08 10:29:06 +01:00
if (activityWeakReference.get().findViewById(R.id.compression_loader) != null) {
2019-11-21 18:50:30 +01:00
activityWeakReference.get().findViewById(R.id.compression_loader).setVisibility(View.GONE);
}
2020-03-08 10:29:06 +01:00
if (activityWeakReference.get().findViewById(R.id.picture_scrollview) != null) {
2019-11-21 18:50:30 +01:00
activityWeakReference.get().findViewById(R.id.picture_scrollview).setVisibility(View.VISIBLE);
}
2019-11-15 16:32:25 +01:00
if (!error) {
if (commpressedFilePath != null) {
uriFile = Uri.fromFile(new File(commpressedFilePath));
}
ImageButton toot_picture;
Button toot_it;
LinearLayout toot_picture_container;
toot_picture = this.activityWeakReference.get().findViewById(R.id.toot_picture);
toot_it = this.activityWeakReference.get().findViewById(R.id.toot_it);
toot_picture_container = this.activityWeakReference.get().findViewById(R.id.toot_picture_container);
2020-03-08 10:29:06 +01:00
if (toot_picture_container != null) {
2019-11-21 18:50:30 +01:00
toot_picture_container.setVisibility(View.VISIBLE);
}
2020-03-08 10:29:06 +01:00
if (toot_picture != null) {
2019-11-21 18:50:30 +01:00
toot_picture.setEnabled(false);
}
2020-03-08 10:29:06 +01:00
if (toot_it != null) {
2019-11-21 18:50:30 +01:00
toot_it.setEnabled(false);
}
2019-11-15 16:32:25 +01:00
if (filename == null) {
filename = Helper.getFileName(this.activityWeakReference.get(), uriFile);
}
filesMap.put(filename, uriFile);
upload(activityWeakReference.get(), account, social, uriFile, filename, uploadReceiver);
}
}
}
2017-05-05 16:36:04 +02:00
}