fedilab-Android-App/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java

2609 lines
129 KiB
Java
Raw Normal View History

2017-05-05 16:36:04 +02:00
/* Copyright 2017 Thomas Schneider
*
2017-07-10 10:33:24 +02:00
* This file is a part of Mastalab
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.
*
2017-07-10 10:33:24 +02:00
* Mastalab 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.
*
2017-08-04 11:11:27 +02:00
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
2017-05-05 16:36:04 +02:00
* see <http://www.gnu.org/licenses>. */
package fr.gouv.etalab.mastodon.activities;
import android.Manifest;
2017-08-26 10:57:08 +02:00
import android.annotation.SuppressLint;
2017-05-05 16:36:04 +02:00
import android.app.Activity;
import android.content.ActivityNotFoundException;
2018-11-14 14:07:25 +01:00
import android.content.ContentResolver;
2017-05-05 16:36:04 +02:00
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
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;
2018-11-14 14:07:25 +01:00
import android.graphics.BitmapFactory;
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;
import android.provider.MediaStore;
import android.speech.RecognizerIntent;
2018-11-14 14:07:25 +01:00
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.ActionBar;
2018-11-14 14:07:25 +01:00
import android.support.v7.app.AlertDialog;
2018-11-03 12:06:44 +01:00
import android.support.v7.widget.Toolbar;
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;
2017-05-05 16:36:04 +02:00
import android.text.TextWatcher;
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.ViewTreeObserver;
2017-08-02 12:17:43 +02:00
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
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;
import android.widget.CompoundButton;
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;
import android.widget.ProgressBar;
2017-10-29 16:53:32 +01:00
import android.widget.RelativeLayout;
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;
2017-12-02 11:02:25 +01:00
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
2017-11-05 17:53:16 +01:00
import com.github.stom79.localepicker.CountryPicker;
import com.github.stom79.localepicker.CountryPickerListener;
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;
2017-12-02 11:02:25 +01:00
2017-09-01 19:14:42 +02:00
import java.io.ByteArrayInputStream;
2017-09-16 12:20:06 +02:00
import java.io.ByteArrayOutputStream;
import java.io.File;
2017-05-05 16:36:04 +02:00
import java.io.FileNotFoundException;
import java.io.IOException;
2017-05-05 16:36:04 +02:00
import java.io.InputStream;
import java.lang.ref.WeakReference;
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;
2017-05-05 16:36:04 +02:00
import java.util.List;
import java.util.Locale;
2019-01-20 19:07:24 +01:00
import java.util.TimeZone;
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
2018-11-25 10:45:16 +01:00
import es.dmoral.toasty.Toasty;
2018-11-14 14:07:25 +01:00
import fr.gouv.etalab.mastodon.R;
2019-01-20 15:20:07 +01:00
import fr.gouv.etalab.mastodon.asynctasks.PostActionAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.PostStatusAsyncTask;
2017-10-25 07:53:31 +02:00
import fr.gouv.etalab.mastodon.asynctasks.RetrieveAccountsForReplyAsyncTask;
2017-11-01 19:19:37 +01:00
import fr.gouv.etalab.mastodon.asynctasks.RetrieveEmojiAsyncTask;
2017-05-26 17:20:36 +02:00
import fr.gouv.etalab.mastodon.asynctasks.RetrieveSearchAccountsAsyncTask;
2017-10-09 18:35:43 +02:00
import fr.gouv.etalab.mastodon.asynctasks.RetrieveSearchAsyncTask;
2017-10-27 11:15:47 +02:00
import fr.gouv.etalab.mastodon.asynctasks.UpdateDescriptionAttachmentAsyncTask;
2019-01-20 15:20:07 +01:00
import fr.gouv.etalab.mastodon.client.API;
2017-06-03 18:18:27 +02:00
import fr.gouv.etalab.mastodon.client.APIResponse;
2017-05-05 16:36:04 +02:00
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Attachment;
2017-11-01 19:19:37 +01:00
import fr.gouv.etalab.mastodon.client.Entities.Emojis;
import fr.gouv.etalab.mastodon.client.Entities.Error;
import fr.gouv.etalab.mastodon.client.Entities.Mention;
2019-01-02 14:22:57 +01:00
import fr.gouv.etalab.mastodon.client.Entities.Notification;
2017-10-09 18:35:43 +02:00
import fr.gouv.etalab.mastodon.client.Entities.Results;
2017-05-05 16:36:04 +02:00
import fr.gouv.etalab.mastodon.client.Entities.Status;
2017-07-15 14:59:09 +02:00
import fr.gouv.etalab.mastodon.client.Entities.StoredStatus;
2018-11-14 14:07:25 +01:00
import fr.gouv.etalab.mastodon.client.Entities.Version;
2017-11-18 11:25:04 +01:00
import fr.gouv.etalab.mastodon.client.HttpsConnection;
2017-10-25 10:51:36 +02:00
import fr.gouv.etalab.mastodon.drawers.AccountsReplyAdapter;
2017-05-05 16:36:04 +02:00
import fr.gouv.etalab.mastodon.drawers.AccountsSearchAdapter;
2018-11-14 14:07:25 +01:00
import fr.gouv.etalab.mastodon.drawers.CustomEmojiAdapter;
2017-07-15 14:59:09 +02:00
import fr.gouv.etalab.mastodon.drawers.DraftsListAdapter;
2018-11-14 14:07:25 +01:00
import fr.gouv.etalab.mastodon.drawers.EmojisSearchAdapter;
2017-10-09 18:35:43 +02:00
import fr.gouv.etalab.mastodon.drawers.TagsSearchAdapter;
2017-05-05 16:36:04 +02:00
import fr.gouv.etalab.mastodon.helper.Helper;
2018-11-14 14:07:25 +01:00
import fr.gouv.etalab.mastodon.helper.MastalabAutoCompleteTextView;
import fr.gouv.etalab.mastodon.interfaces.OnDownloadInterface;
2019-01-20 15:20:07 +01:00
import fr.gouv.etalab.mastodon.interfaces.OnPostActionInterface;
import fr.gouv.etalab.mastodon.interfaces.OnPostStatusActionInterface;
2017-10-25 07:53:31 +02:00
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveAccountsReplyInterface;
2017-05-05 16:36:04 +02:00
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveAttachmentInterface;
2018-11-14 14:07:25 +01:00
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveEmojiInterface;
2017-05-26 17:20:36 +02:00
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveSearcAccountshInterface;
2017-10-09 18:35:43 +02:00
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveSearchInterface;
2017-07-16 17:09:35 +02:00
import fr.gouv.etalab.mastodon.jobs.ScheduledTootsSyncJob;
2017-05-05 16:36:04 +02:00
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
2018-11-14 14:07:25 +01:00
import fr.gouv.etalab.mastodon.sqlite.CustomEmojiDAO;
2017-05-05 16:36:04 +02:00
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
2017-08-06 17:58:55 +02:00
import fr.gouv.etalab.mastodon.sqlite.StatusStoredDAO;
2017-05-05 16:36:04 +02:00
import static fr.gouv.etalab.mastodon.helper.Helper.HOME_TIMELINE_INTENT;
import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_ACTION;
import static fr.gouv.etalab.mastodon.helper.Helper.THEME_LIGHT;
2017-07-01 15:24:28 +02:00
import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor;
import static fr.gouv.etalab.mastodon.helper.Helper.convertDpToPixel;
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-01-20 15:20:07 +01:00
public class TootActivity extends BaseActivity implements OnPostActionInterface, OnRetrieveSearcAccountshInterface, OnRetrieveAttachmentInterface, OnPostStatusActionInterface, OnRetrieveSearchInterface, OnRetrieveAccountsReplyInterface, OnRetrieveEmojiInterface, OnDownloadInterface {
2017-05-05 16:36:04 +02:00
private String visibility;
private final int PICK_IMAGE = 56556;
private final int TAKE_PHOTO = 56532;
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 fileMention = null;
private String sharedContent, sharedSubject, sharedContentIni;
2017-06-07 15:47:05 +02:00
private CheckBox toot_sensitive;
2017-07-15 14:59:09 +02:00
public long currentToId;
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;
2017-08-02 17:44:36 +02:00
private LinearLayout drawer_layout;
private HorizontalScrollView picture_scrollview;
2017-08-09 18:27:03 +02:00
private int currentCursorPosition, searchLength;
2017-08-18 16:54:43 +02:00
private TextView toot_space_left;
private String initialContent;
2019-01-25 11:41:39 +01:00
public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 754;
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;
2017-12-28 17:25:36 +01:00
private String userId, instance;
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-09-01 18:24:01 +02:00
static boolean active = false;
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;
2017-05-05 16:36:04 +02:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2017-06-30 17:09:07 +02:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
2017-12-28 17:25:36 +01:00
userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
instance = sharedpreferences.getString(Helper.PREF_INSTANCE, Helper.getLiveInstance(getApplicationContext()));
final int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
2018-05-11 11:28:05 +02:00
switch (theme){
case Helper.THEME_LIGHT:
setTheme(R.style.AppTheme);
break;
case Helper.THEME_DARK:
setTheme(R.style.AppThemeDark);
break;
case Helper.THEME_BLACK:
setTheme(R.style.AppThemeBlack);
break;
default:
setTheme(R.style.AppThemeDark);
2017-06-30 17:09:07 +02:00
}
2018-11-03 14:45:55 +01:00
if (theme == Helper.THEME_DARK) {
style = R.style.DialogDark;
} else if (theme == Helper.THEME_BLACK){
style = R.style.DialogBlack;
}else {
style = R.style.Dialog;
}
2017-05-05 16:36:04 +02:00
setContentView(R.layout.activity_toot);
2017-07-21 21:09:32 +02:00
ActionBar actionBar = getSupportActionBar();
2018-11-03 12:06:44 +01:00
if( actionBar != null ) {
2017-07-21 21:09:32 +02:00
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2017-10-27 15:34:53 +02:00
assert inflater != null;
2017-08-26 10:57:08 +02:00
@SuppressLint("InflateParams") View view = inflater.inflate(R.layout.toot_action_bar, null);
2017-07-21 21:09:32 +02:00
actionBar.setCustomView(view, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
2017-10-25 09:46:05 +02:00
ImageView close_toot = actionBar.getCustomView().findViewById(R.id.close_toot);
2018-11-03 12:06:44 +01:00
2017-07-21 21:09:32 +02:00
close_toot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
2018-11-03 12:06:44 +01:00
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
2017-10-27 15:34:53 +02:00
assert inputMethodManager != null;
2017-08-02 12:17:43 +02:00
inputMethodManager.hideSoftInputFromWindow(toot_content.getWindowToken(), 0);
2017-07-21 21:09:32 +02:00
finish();
}
});
2018-11-03 12:06:44 +01:00
if (theme == THEME_LIGHT){
Toolbar toolbar = actionBar.getCustomView().findViewById(R.id.toolbar);
Helper.colorizeToolbar(toolbar, R.color.black, TootActivity.this);
}
2017-10-25 09:46:05 +02: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);
2017-07-21 21:09:32 +02:00
}
2017-10-28 14:32:18 +02:00
changeColor();
2017-07-15 14:59:09 +02:00
//By default the toot is not restored so the id -1 is defined
currentToId = -1;
2018-08-19 10:26:17 +02:00
restoredScheduled = false;
2017-12-02 11:02:25 +01:00
2019-01-23 19:23:26 +01:00
checkedValues = new ArrayList<>();
contacts = new ArrayList<>();
2017-10-25 09:46:05 +02:00
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);
2017-10-25 09:46:05 +02:00
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);
2018-11-03 12:06:44 +01:00
ImageButton toot_emoji = findViewById(R.id.toot_emoji);
2019-01-20 15:20:07 +01:00
isScheduled = false;
2018-10-28 11:32:23 +01:00
if( sharedpreferences.getBoolean(Helper.SET_DISPLAY_EMOJI, true)) {
final EmojiPopup emojiPopup = EmojiPopup.Builder.fromRootView(drawer_layout).build(toot_content);
2018-10-28 11:32:23 +01:00
toot_emoji.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
emojiPopup.toggle(); // Toggles visibility of the Popup.
}
});
}else {
toot_emoji.setVisibility(View.GONE);
}
drawer_layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int heightDiff = drawer_layout.getRootView().getHeight() - drawer_layout.getHeight();
if (heightDiff > Helper.convertDpToPixel(200, getApplicationContext())) {
ViewGroup.LayoutParams params = toot_picture_container.getLayoutParams();
params.height = (int) Helper.convertDpToPixel(50, getApplicationContext());
params.width = (int) Helper.convertDpToPixel(50, getApplicationContext());
toot_picture_container.setLayoutParams(params);
}else {
ViewGroup.LayoutParams params = toot_picture_container.getLayoutParams();
params.height = (int) Helper.convertDpToPixel(100, getApplicationContext());
params.width = (int) Helper.convertDpToPixel(100, getApplicationContext());
toot_picture_container.setLayoutParams(params);
}
}
});
2017-05-05 16:36:04 +02:00
Bundle b = getIntent().getExtras();
2017-08-26 10:57:08 +02:00
ArrayList<Uri> sharedUri = new ArrayList<>();
2018-11-04 10:01:16 +01:00
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2017-07-16 19:04:53 +02:00
restored = -1;
if(b != null) {
tootReply = b.getParcelable("tootReply");
2019-01-20 15:20:07 +01:00
scheduledstatus = b.getParcelable("storedStatus");
2018-11-04 10:01:16 +01:00
String accountReplyToken = b.getString("accountReplyToken", null);
accountReply = null;
if( accountReplyToken != null){
accountReply = new AccountDAO(getApplicationContext(),db).getAccountByToken(accountReplyToken);
}
tootMention = b.getString("tootMention", null);
urlMention = b.getString("urlMention", null);
fileMention = b.getString("fileMention", null);
sharedContent = b.getString("sharedContent", null);
sharedContentIni = b.getString("sharedContent", null);
sharedSubject = b.getString("sharedSubject", null);
mentionAccount = b.getString("mentionAccount", null);
2018-12-08 17:03:27 +01: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);
// 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
2017-12-02 08:39:01 +01: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-01-20 15:20:07 +01:00
if( scheduledstatus != null)
toot_it.setText(R.string.modify);
if(restoredScheduled){
2017-07-16 19:04:53 +02:00
toot_it.setVisibility(View.GONE);
invalidateOptionsMenu();
}
2018-11-04 10:01:16 +01:00
2018-02-18 19:08:24 +01:00
String userIdReply;
if( accountReply == null)
userIdReply = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
else
userIdReply = accountReply.getId();
if( accountReply == null)
account = new AccountDAO(getApplicationContext(),db).getAccountByID(userIdReply);
else
account = accountReply;
if( tootReply != null) {
tootReply();
2017-05-05 16:36:04 +02:00
}else {
2017-07-21 21:09:32 +02:00
if( title != null)
title.setText(getString(R.string.toot_title));
else
setTitle(R.string.toot_title);
2017-05-05 16:36:04 +02:00
}
2018-02-18 19:08:24 +01:00
2017-10-04 15:57:53 +02:00
if( mentionAccount != null){
toot_content.setText(String.format("@%s\n", mentionAccount));
toot_content.setSelection(toot_content.getText().length());
toot_space_left.setText(String.valueOf(toot_content.length()));
}
if( tootMention != null && urlMention != null && fileMention != null) {
Bitmap pictureMention = BitmapFactory.decodeFile(getCacheDir() + "/" + fileMention);
if (pictureMention != null) {
2017-09-16 12:20:06 +02:00
ByteArrayOutputStream bos = new ByteArrayOutputStream();
pictureMention.compress(Bitmap.CompressFormat.PNG, 0, bos);
byte[] bitmapdata = bos.toByteArray();
ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);
toot_picture_container.setVisibility(View.VISIBLE);
picture_scrollview.setVisibility(View.VISIBLE);
2017-09-16 12:20:06 +02:00
toot_picture.setEnabled(false);
toot_it.setEnabled(false);
2018-08-16 12:08:38 +02:00
new HttpsConnection(TootActivity.this).upload(bs, fileMention, accountReply!=null?accountReply.getToken():null, TootActivity.this);
2017-09-16 12:20:06 +02:00
}
toot_content.setText(String.format("\n\nvia @%s\n\n%s\n\n", tootMention, urlMention));
toot_space_left.setText(String.valueOf(toot_content.length()));
2017-09-16 12:20:06 +02:00
}
initialContent = toot_content.getText().toString();
2018-02-18 19:08:24 +01:00
2017-09-16 12:20:06 +02:00
String url = account.getAvatar();
if( url.startsWith("/") ){
2018-01-24 15:56:33 +01:00
url = Helper.getLiveInstanceWithProtocol(getApplicationContext()) + account.getAvatar();
}
2018-11-14 14:07:25 +01:00
Helper.loadGiF(getApplicationContext(), url, pp_actionBar);
if( sharedContent != null ){ //Shared content
if( sharedSubject != null){
sharedContent = sharedSubject + "\n\n" + sharedContent;
}
2018-08-16 12:08:38 +02:00
if( b != null) {
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);
toot_space_left.setText(String.valueOf(toot_content.length()));
}
2018-08-16 12:08:38 +02:00
if (image != null) {
new HttpsConnection(TootActivity.this).download(image, TootActivity.this);
}
int selectionBefore = toot_content.getSelectionStart();
toot_content.setText(String.format("\n%s", sharedContent));
if (selectionBefore >= 0 && selectionBefore < toot_content.length())
toot_content.setSelection(selectionBefore);
toot_space_left.setText(String.valueOf(toot_content.length()));
}
}
2017-05-05 16:36:04 +02:00
attachments = new ArrayList<>();
2017-08-02 12:17:43 +02:00
int charsInCw = 0;
int charsInToot = 0;
if (!sharedUri.isEmpty()) {
uploadSharedImage(sharedUri);
}
2018-02-18 19:08:24 +01:00
if( tootReply == null) {
2018-09-05 17:08:57 +02:00
if( visibility == null) {
String defaultVisibility = account.isLocked() ? "private" : "public";
visibility = sharedpreferences.getString(Helper.SET_TOOT_VISIBILITY + "@" + account.getAcct() + "@" + account.getInstance(), defaultVisibility);
}
2018-02-18 19:08:24 +01:00
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;
}
2017-05-05 16:36:04 +02:00
}
2017-06-07 15:47:05 +02:00
toot_sensitive.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
isSensitive = isChecked;
}
});
2017-05-05 16:36:04 +02:00
2017-08-18 16:54:43 +02:00
toot_space_left.setText(String.valueOf(charsInToot + charsInCw));
2017-05-05 16:36:04 +02:00
toot_cw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View 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();
}
}
});
toot_visibility.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tootVisibilityDialog();
}
});
2017-05-05 16:36:04 +02:00
toot_it.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
2019-01-19 19:41:55 +01:00
sendToot(null);
2017-05-05 16:36:04 +02:00
}
});
toot_picture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
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 (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
intent.setType("*/*");
String[] mimetypes = {"image/*", "video/*"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
startActivityForResult(intent, PICK_IMAGE);
}else {
intent.setType("image/* video/*");
Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.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);
}
2017-05-05 16:36:04 +02:00
}
});
String pattern = "^(.|\\s)*(@([a-zA-Z0-9_]{2,}))$";
final Pattern sPattern = Pattern.compile(pattern);
2017-08-09 18:27:03 +02:00
2017-10-29 17:25:45 +01:00
String patternTag = "^(.|\\s)*(#([\\w-]{2,}))$";
2017-10-09 18:35:43 +02:00
final Pattern tPattern = Pattern.compile(patternTag);
2017-11-18 11:25:04 +01:00
String patternEmoji = "^(.|\\s)*(:([\\w_]+))$";
2017-11-01 19:19:37 +01:00
final Pattern ePattern = Pattern.compile(patternEmoji);
2017-10-27 15:34:53 +02:00
toot_cw_content.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
int totalChar = toot_cw_content.length() + toot_content.length();
toot_space_left.setText(String.valueOf(totalChar));
}
});
2017-05-05 16:36:04 +02:00
toot_content.addTextChangedListener(new TextWatcher() {
@Override
2017-08-10 09:03:42 +02:00
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
2017-05-05 16:36:04 +02:00
@Override
2017-08-10 09:03:42 +02:00
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
2017-05-05 16:36:04 +02:00
@Override
public void afterTextChanged(Editable s) {
if( toot_content.getSelectionStart() != 0)
currentCursorPosition = toot_content.getSelectionStart();
if( s.toString().length() == 0 )
currentCursorPosition = 0;
//Only check last 15 characters before cursor position to avoid lags
if( currentCursorPosition < 15 ){ //Less than 15 characters are written before the cursor position
searchLength = currentCursorPosition;
}else {
searchLength = 15;
}
int totalChar = toot_cw_content.length() + toot_content.length();
toot_space_left.setText(String.valueOf(totalChar));
2017-08-10 09:03:42 +02:00
if( currentCursorPosition- (searchLength-1) < 0 || currentCursorPosition == 0 || currentCursorPosition > s.toString().length())
return;
2017-10-09 18:35:43 +02:00
Matcher m, mt;
if( s.toString().charAt(0) == '@')
m = sPattern.matcher(s.toString().substring(currentCursorPosition- searchLength, currentCursorPosition));
else
m = sPattern.matcher(s.toString().substring(currentCursorPosition- (searchLength-1), currentCursorPosition));
2017-05-05 16:36:04 +02:00
if(m.matches()) {
2017-08-09 18:27:03 +02:00
String search = m.group(3);
if (pp_progress != null && pp_actionBar != null) {
2017-08-02 12:17:43 +02:00
pp_progress.setVisibility(View.VISIBLE);
pp_actionBar.setVisibility(View.GONE);
}
2017-05-26 17:20:36 +02:00
new RetrieveSearchAccountsAsyncTask(getApplicationContext(),search,TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2017-10-09 18:41:16 +02:00
}else{
if( s.toString().charAt(0) == '#')
mt = tPattern.matcher(s.toString().substring(currentCursorPosition- searchLength, currentCursorPosition));
else
mt = tPattern.matcher(s.toString().substring(currentCursorPosition- (searchLength-1), currentCursorPosition));
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(getApplicationContext(),search,true, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2017-11-01 19:19:37 +01:00
}else{
if( s.toString().charAt(0) == ':')
mt = ePattern.matcher(s.toString().substring(currentCursorPosition- searchLength, currentCursorPosition));
else
mt = ePattern.matcher(s.toString().substring(currentCursorPosition- (searchLength-1), currentCursorPosition));
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(getApplicationContext(),shortcode,TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}else {
toot_content.dismissDropDown();
}
}
2017-10-09 18:41:16 +02:00
}
2017-10-09 18:35:43 +02:00
totalChar = toot_cw_content.length() + toot_content.length();
2017-08-18 16:54:43 +02:00
toot_space_left.setText(String.valueOf(totalChar));
2017-05-05 16:36:04 +02:00
}
});
2017-10-27 15:34:53 +02:00
2019-01-20 15:20:07 +01:00
if( scheduledstatus != null)
restoreServerSchedule(scheduledstatus.getStatus());
2017-10-27 15:34:53 +02:00
2017-07-16 19:04:53 +02:00
if( restored != -1 ){
restoreToot(restored);
}
2018-10-26 17:00:38 +02:00
2017-05-05 16:36:04 +02:00
}
@Override
public void onDestroy(){
super.onDestroy();
}
@Override
public void onRequestPermissionsResult(int requestCode,
@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;
}
}
}
public void showAToast (String message){
if (mToast != null) {
mToast.cancel();
}
2018-11-25 10:45:16 +01:00
mToast = Toasty.error(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;
for(Uri fileUri: uri) {
if (fileUri != null) {
2018-01-10 16:00:57 +01:00
if (count == 4) {
break;
}
picture_scrollview.setVisibility(View.VISIBLE);
try {
2018-08-16 12:08:38 +02:00
new asyncPicture(TootActivity.this, accountReply, fileUri).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
count++;
2018-01-10 16:00:57 +01:00
} catch (Exception e) {
e.printStackTrace();
2018-11-25 10:45:16 +01:00
Toasty.error(getApplicationContext(),getString(R.string.toot_select_image_error),Toast.LENGTH_LONG).show();
toot_picture.setEnabled(true);
toot_it.setEnabled(true);
}
} else {
2018-11-25 10:45:16 +01:00
Toasty.error(getApplicationContext(),getString(R.string.toot_select_image_error),Toast.LENGTH_LONG).show();
}
}
}
}
String mCurrentPhotoPath;
File photoFile = null;
Uri photoFileUri = null;
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) {
Toasty.error(getApplicationContext(),getString(R.string.toot_select_image_error),Toast.LENGTH_LONG).show();}
// Continue only if the File was successfully created
if (photoFile != null) {
photoFileUri = FileProvider.getUriForFile(this,
"fr.gouv.etalab.mastodon.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);
if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK) {
2017-08-02 17:44:36 +02:00
picture_scrollview.setVisibility(View.VISIBLE);
if (data == null || data.getData() == null) {
2018-11-25 10:45:16 +01:00
Toasty.error(getApplicationContext(),getString(R.string.toot_select_image_error),Toast.LENGTH_LONG).show();
2017-05-05 16:36:04 +02:00
return;
}
try {
String filename = Helper.getFileName(TootActivity.this, data.getData());
ContentResolver cr = getContentResolver();
String mime = cr.getType(data.getData());
2018-01-30 11:10:42 +01:00
if(mime != null && (mime.toLowerCase().contains("video") || mime.toLowerCase().contains("gif")) ) {
InputStream inputStream = getContentResolver().openInputStream(data.getData());
2018-08-16 12:08:38 +02:00
new HttpsConnection(TootActivity.this).upload(inputStream, filename, accountReply!=null?accountReply.getToken():null, TootActivity.this);
} else if(mime != null && mime.toLowerCase().contains("image")) {
2018-08-16 12:08:38 +02:00
new asyncPicture(TootActivity.this, accountReply, data.getData()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}else {
2018-11-25 10:45:16 +01:00
Toasty.error(getApplicationContext(),getString(R.string.toot_select_image_error),Toast.LENGTH_LONG).show();
}
2017-05-05 16:36:04 +02:00
} catch (FileNotFoundException e) {
2018-11-25 10:45:16 +01:00
Toasty.error(getApplicationContext(),getString(R.string.toot_select_image_error),Toast.LENGTH_LONG).show();
2017-05-05 16:36:04 +02:00
toot_picture.setEnabled(true);
toot_it.setEnabled(true);
2017-05-05 16:36:04 +02:00
}
}else if(requestCode == Helper.REQ_CODE_SPEECH_INPUT && resultCode == Activity.RESULT_OK){
if (null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
toot_content.setText(result.get(0));
toot_content.setSelection(toot_content.getText().length());
}
}else if (requestCode == TAKE_PHOTO && resultCode == RESULT_OK) {
2018-08-16 12:08:38 +02:00
new asyncPicture(TootActivity.this, accountReply, photoFileUri).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) {
if( error != null){
Toasty.error(getApplicationContext(),getString(R.string.toast_error),Toast.LENGTH_LONG).show();
}else {
Toasty.success(getApplicationContext(),getString(R.string.toot_scheduled),Toast.LENGTH_LONG).show();
resetForNextToot();
}
}
2019-01-23 19:23:26 +01:00
2019-01-26 11:59:37 +01:00
static class asyncPicture extends AsyncTask<Void, Void, Void> {
ByteArrayInputStream bs;
WeakReference<Activity> activityWeakReference;
2018-04-17 18:53:18 +02:00
android.net.Uri uriFile;
2018-08-16 12:08:38 +02:00
Account accountReply;
2019-01-26 11:59:37 +01:00
boolean error = false;
2018-08-16 12:08:38 +02:00
asyncPicture(Activity activity, Account accountReply, android.net.Uri uri){
this.activityWeakReference = new WeakReference<>(activity);
2018-04-17 18:53:18 +02:00
this.uriFile = uri;
2018-08-16 12:08:38 +02:00
this.accountReply = accountReply;
}
@Override
2019-01-26 11:59:37 +01:00
protected void onPreExecute(){
if( uriFile == null) {
Toasty.error(activityWeakReference.get(), activityWeakReference.get().getString(R.string.toast_error), Toast.LENGTH_SHORT).show();
error = true;
}
}
2019-01-26 11:59:37 +01:00
@Override
protected Void doInBackground(Void... voids) {
if( error)
return null;
2018-04-22 13:16:52 +02:00
bs = Helper.compressImage(activityWeakReference.get(), uriFile, Helper.MediaType.MEDIA);
return null;
}
@Override
protected void onPostExecute(Void result) {
2019-01-26 11:59:37 +01:00
if( !error) {
if (bs == null)
return;
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);
toot_picture_container.setVisibility(View.VISIBLE);
toot_picture.setEnabled(false);
toot_it.setEnabled(false);
String filename = Helper.getFileName(this.activityWeakReference.get(), uriFile);
new HttpsConnection(this.activityWeakReference.get()).upload(bs, filename, accountReply != null ? accountReply.getToken() : null, (TootActivity) this.activityWeakReference.get());
}
2017-05-05 16:36:04 +02:00
}
}
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"));
picture_scrollview.setVisibility(View.VISIBLE);
if( imageUri == null) {
2018-11-25 10:45:16 +01:00
Toasty.error(getApplicationContext(),getString(R.string.toot_select_image_error),Toast.LENGTH_LONG).show();
2018-09-01 18:24:01 +02:00
return;
}
try {
String filename = Helper.getFileName(TootActivity.this, imageUri);
ContentResolver cr = getContentResolver();
String mime = cr.getType(imageUri);
if(mime != null && (mime.toLowerCase().contains("video") || mime.toLowerCase().contains("gif")) ) {
InputStream inputStream = getContentResolver().openInputStream(imageUri);
new HttpsConnection(TootActivity.this).upload(inputStream, filename, accountReply!=null?accountReply.getToken():null, TootActivity.this);
} else if(mime != null && mime.toLowerCase().contains("image")) {
new asyncPicture(TootActivity.this, accountReply, intent.getData()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}else {
2018-11-25 10:45:16 +01:00
Toasty.error(getApplicationContext(),getString(R.string.toot_select_image_error),Toast.LENGTH_LONG).show();
2018-09-01 18:24:01 +02:00
}
} catch (FileNotFoundException e) {
2018-11-25 10:45:16 +01:00
Toasty.error(getApplicationContext(),getString(R.string.toot_select_image_error),Toast.LENGTH_LONG).show();
2018-09-01 18:24:01 +02:00
toot_picture.setEnabled(true);
toot_it.setEnabled(true);
}
}
}
2017-05-05 16:36:04 +02:00
@Override
public boolean onOptionsItemSelected(MenuItem item) {
2017-07-15 14:59:09 +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;
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
if (theme == Helper.THEME_DARK) {
style = R.style.DialogDark;
} else if (theme == Helper.THEME_BLACK){
style = R.style.DialogBlack;
}else {
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();
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
//noinspection deprecation
input.setText(Html.fromHtml(content));
alert.setPositiveButton(R.string.close, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
2017-10-25 09:04:34 +02:00
alert.setNegativeButton(R.string.accounts, new DialogInterface.OnClickListener() {
2017-10-25 07:53:31 +02:00
public void onClick(DialogInterface dialog, int whichButton) {
2017-10-25 09:04:34 +02:00
new RetrieveAccountsForReplyAsyncTask(getApplicationContext(), tootReply.getReblog() != null?tootReply.getReblog():tootReply, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2017-10-25 07:53:31 +02:00
dialog.dismiss();
}
});
alert.show();
2017-10-31 12:47:51 +01:00
return true;
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
2017-10-31 18:29:04 +01:00
if( theme == Helper.THEME_LIGHT){
picker.setStyle(R.style.AppTheme, R.style.AlertDialog);
}else {
picker.setStyle(R.style.AppThemeDark, R.style.AlertDialogDark);
}
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);
if( dateString != null){
2017-11-01 13:58:12 +01:00
Date dateCompare = Helper.stringToDate(getApplicationContext(), dateString);
Date date = new Date();
if( date.before(dateCompare)) {
2018-11-25 10:45:16 +01:00
Toasty.info(getApplicationContext(), 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
}
2017-10-31 12:47:51 +01:00
picker.setListener(new CountryPickerListener() {
2017-10-31 15:57:19 +01:00
@SuppressLint("InflateParams")
2017-10-31 12:47:51 +01:00
@Override
2017-11-05 17:53:16 +01:00
public void onSelectCountry(String name, String locale, int flagDrawableResID) {
2017-10-31 12:47:51 +01:00
picker.dismiss();
2018-11-03 14:45:55 +01:00
AlertDialog.Builder transAlert = new AlertDialog.Builder(TootActivity.this, style);
2017-10-31 12:47:51 +01:00
transAlert.setTitle(R.string.translate_toot);
2017-10-31 15:57:19 +01:00
popup_trans = getLayoutInflater().inflate( R.layout.popup_translate, null );
transAlert.setView(popup_trans);
2017-11-01 13:36:25 +01:00
SharedPreferences.Editor editor = sharedpreferences.edit();
2018-04-28 16:54:06 +02:00
editor.putString(Helper.LAST_TRANSLATION_TIME, Helper.dateToString(new Date( System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(Helper.SECONDES_BETWEEN_TRANSLATE))));
2017-11-01 13:36:25 +01:00
editor.apply();
2017-11-01 13:58:12 +01:00
TextView yandex_translate = popup_trans.findViewById(R.id.yandex_translate);
yandex_translate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://translate.yandex.com/"));
startActivity(browserIntent);
}
});
2017-11-29 15:06:30 +01:00
MyTransL myTransL = MyTransL.getInstance(MyTransL.translatorEngine.YANDEX);
myTransL.setYandexAPIKey(Helper.YANDEX_KEY);
myTransL.setObfuscation(true);
myTransL.setTimeout(60);
2018-08-23 09:41:01 +02:00
if( toot_cw_content.getText().toString().length() > 0)
myTransL.translate(toot_cw_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_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());
}
}else {
2018-11-25 10:45:16 +01:00
Toasty.error(getApplicationContext(),getString(R.string.toast_error_translate),Toast.LENGTH_LONG).show();
2017-11-29 15:06:30 +01:00
}
2018-08-23 09:41:01 +02: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
}
2018-08-23 09:41:01 +02:00
} catch (IllegalArgumentException e) {
2018-11-25 10:45:16 +01:00
Toasty.error(getApplicationContext(),getString(R.string.toast_error_translate),Toast.LENGTH_LONG).show();
2017-11-29 15:06:30 +01:00
}
2018-08-23 09:41:01 +02:00
}
2017-11-29 15:06:30 +01:00
2018-08-23 09:41:01 +02:00
@Override
public void onFail(HttpsConnectionException e) {
e.printStackTrace();
}
});
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 {
2018-11-25 10:45:16 +01:00
Toasty.error(getApplicationContext(),getString(R.string.toast_error_translate),Toast.LENGTH_LONG).show();
2017-11-29 15:06:30 +01:00
}
2018-08-23 09:41:01 +02: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
}
2018-08-23 09:41:01 +02:00
} catch (IllegalArgumentException e) {
2018-11-25 10:45:16 +01:00
Toasty.error(getApplicationContext(),getString(R.string.toast_error_translate),Toast.LENGTH_LONG).show();
2017-11-29 15:06:30 +01:00
}
}
2018-08-23 09:41:01 +02:00
@Override
public void onFail(HttpsConnectionException e) {
e.printStackTrace();
}
});
2017-11-29 15:06:30 +01:00
2017-10-31 12:47:51 +01:00
transAlert.setPositiveButton(R.string.close, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
transAlert.setNegativeButton(R.string.validate, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
2017-10-31 15:57:19 +01:00
TextView toot_trans = popup_trans.findViewById(R.id.toot_trans);
TextView cw_trans = popup_trans.findViewById(R.id.cw_trans);
2017-10-31 18:06:01 +01:00
if( toot_trans != null) {
toot_content.setText(toot_trans.getText().toString());
2017-10-31 18:06:01 +01:00
toot_content.setSelection(toot_content.getText().length());
}
if( cw_trans != null)
toot_cw_content.setText(cw_trans.getText().toString());
2017-10-31 12:47:51 +01:00
dialog.dismiss();
}
});
2017-10-31 15:57:19 +01:00
dialogTrans = transAlert.create();
transAlert.show();
dialogTrans.setOnShowListener(new DialogInterface.OnShowListener() {
2017-10-31 12:47:51 +01:00
@Override
public void onShow(DialogInterface dialog) {
Button negativeButton = ((AlertDialog) dialog)
.getButton(AlertDialog.BUTTON_NEGATIVE);
if( negativeButton != null)
negativeButton.setEnabled(false);
}
});
2017-10-31 15:57:19 +01:00
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:
final List<Emojis> emojis = new CustomEmojiDAO(getApplicationContext(), db).getAllEmojis();
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;
int paddingDp = (int)(paddingPixel * density);
builder.setNeutralButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setTitle(R.string.insert_emoji);
if( emojis != null && emojis.size() > 0) {
GridView gridView = new GridView(TootActivity.this);
gridView.setAdapter(new CustomEmojiAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, emojis));
gridView.setNumColumns(5);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
toot_content.getText().insert(toot_content.getSelectionStart(), " :" + emojis.get(position).getShortcode()+": ");
alertDialogEmoji.dismiss();
}
});
gridView.setPadding(paddingDp,paddingDp,paddingDp,paddingDp);
builder.setView(gridView);
}else{
TextView textView = new TextView(TootActivity.this);
textView.setText(getString(R.string.no_emoji));
textView.setPadding(paddingDp,paddingDp,paddingDp,paddingDp);
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();
@SuppressLint("InflateParams") View dialogView = inflater.inflate(R.layout.popup_contact, null);
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);
new RetrieveSearchAccountsAsyncTask(TootActivity.this, "a", true,TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
search_account.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@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);
}else{
search_account.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_search, 0);
}
}
@Override
public void afterTextChanged(Editable s) {
if( s != null && s.length() > 0){
new RetrieveSearchAccountsAsyncTask(TootActivity.this, s.toString(), true,TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
});
search_account.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent 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("");
2019-01-23 19:35:08 +01:00
new RetrieveSearchAccountsAsyncTask(TootActivity.this, "a", true,TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2019-01-23 19:23:26 +01:00
}
}
return false;
}
});
builderSingle.setView(dialogView);
builderSingle.setNegativeButton(R.string.validate, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
toot_content.setSelection(toot_content.getText().length());
}
});
builderSingle.show();
return true;
case R.id.action_microphone:
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(intent, Helper.REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
2018-11-25 10:45:16 +01:00
Toasty.info(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
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:
try{
final List<StoredStatus> drafts = new StatusStoredDAO(TootActivity.this, db).getAllDrafts();
if( drafts == null || drafts.size() == 0){
2018-11-25 10:45:16 +01:00
Toasty.info(getApplicationContext(), 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));
final DraftsListAdapter draftsListAdapter = new DraftsListAdapter(TootActivity.this, drafts);
final int[] ids = new int[drafts.size()];
int i = 0;
for(StoredStatus draft: drafts){
ids[i] = draft.getId();
i++;
}
builderSingle.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builderSingle.setPositiveButton(R.string.delete_all, new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, int which) {
2018-11-03 14:45:55 +01:00
AlertDialog.Builder builder = new AlertDialog.Builder(TootActivity.this, style);
2017-07-15 14:59:09 +02:00
builder.setTitle(R.string.delete_all);
builder.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogConfirm, int which) {
new StatusStoredDAO(getApplicationContext(), db).removeAllDrafts();
dialogConfirm.dismiss();
dialog.dismiss();
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogConfirm, int which) {
dialogConfirm.dismiss();
}
})
.show();
}
});
builderSingle.setAdapter(draftsListAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int id = ids[which];
2017-07-16 19:04:53 +02:00
restoreToot(id);
2017-07-15 14:59:09 +02:00
dialog.dismiss();
}
});
builderSingle.show();
}catch (Exception e){
2018-11-25 10:45:16 +01:00
Toasty.error(getApplicationContext(), 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:
2017-07-15 14:59:09 +02:00
if(toot_content.getText().toString().trim().length() == 0 ){
2018-11-25 10:45:16 +01:00
Toasty.error(getApplicationContext(),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();
dialogView = inflater.inflate(R.layout.datetime_picker, null);
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);
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
date_time_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
date_time_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View 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);
}
});
date_time_previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View 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);
}
});
date_time_set.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int hour, minute;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
hour = timePicker.getHour();
minute = timePicker.getMinute();
}else {
//noinspection deprecation
hour = timePicker.getCurrentHour();
//noinspection deprecation
minute = timePicker.getCurrentMinute();
}
Calendar calendar = new GregorianCalendar(datePicker.getYear(),
datePicker.getMonth(),
datePicker.getDayOfMonth(),
hour,
minute);
2019-01-20 09:45:48 +01:00
final long[] time = {calendar.getTimeInMillis()};
2019-01-20 19:07:24 +01:00
2019-01-20 09:45:48 +01:00
if( (time[0] - new Date().getTime()) < 60000 ){
2018-11-25 10:45:16 +01:00
Toasty.warning(getApplicationContext(), getString(R.string.toot_scheduled_date), Toast.LENGTH_LONG).show();
2017-07-16 17:09:35 +02:00
}else {
2019-01-19 19:41:55 +01:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
String instanceVersion = sharedpreferences.getString(Helper.INSTANCE_VERSION + userId + instance, null);
Version currentVersion = new Version(instanceVersion);
Version minVersion = new Version("2.7");
if (currentVersion.compareTo(minVersion) == 1 || currentVersion.equals(minVersion)) {
AlertDialog.Builder builderSingle = new AlertDialog.Builder(TootActivity.this, style);
builderSingle.setTitle(getString(R.string.choose_schedule));
builderSingle.setNegativeButton(R.string.device_schedule, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
2019-01-20 09:45:48 +01:00
deviceSchedule(time[0]);
2019-01-19 19:41:55 +01:00
dialog.dismiss();
}
});
builderSingle.setPositiveButton(R.string.server_schedule, new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, int which) {
2019-01-20 19:07:24 +01:00
int offset = TimeZone.getDefault().getRawOffset();
calendar.add(Calendar.MILLISECOND, -offset);
final String date = Helper.dateToString(new Date(calendar.getTimeInMillis()));
2019-01-20 09:45:48 +01:00
serverSchedule(date);
2019-01-19 19:41:55 +01:00
}
});
builderSingle.show();
} else {
2019-01-20 09:45:48 +01:00
deviceSchedule(time[0]);
2017-07-16 17:09:35 +02:00
}
2019-01-19 19:41:55 +01:00
2017-07-16 17:09:35 +02:00
alertDialog.dismiss();
}
}
});
alertDialog.show();
return true;
2017-05-05 16:36:04 +02:00
default:
return super.onOptionsItemSelected(item);
}
}
2019-01-19 19:41:55 +01:00
private void sendToot(String timestamp){
toot_it.setEnabled(false);
if(toot_content.getText().toString().trim().length() == 0 && attachments.size() == 0){
2019-01-20 15:20:07 +01:00
Toasty.error(getApplicationContext(),getString(R.string.toot_error_no_content),Toast.LENGTH_LONG).show();
2019-01-19 19:41:55 +01:00
toot_it.setEnabled(true);
return;
}
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
boolean split_toot = sharedpreferences.getBoolean(Helper.SET_AUTOMATICALLY_SPLIT_TOOTS, false);
int split_toot_size = sharedpreferences.getInt(Helper.SET_AUTOMATICALLY_SPLIT_TOOTS_SIZE, Helper.SPLIT_TOOT_SIZE);
String tootContent;
if( toot_cw_content.getText() != null && toot_cw_content.getText().toString().trim().length() > 0 )
split_toot_size -= toot_cw_content.getText().toString().trim().length();
if( !split_toot || (toot_content.getText().toString().trim().length() < split_toot_size)){
tootContent = toot_content.getText().toString().trim();
}else{
splitToot = Helper.splitToots(toot_content.getText().toString().trim(), split_toot_size);
tootContent = splitToot.get(0);
stepSpliToot = 1;
}
Status toot = new Status();
toot.setSensitive(isSensitive);
toot.setMedia_attachments(attachments);
if( toot_cw_content.getText().toString().trim().length() > 0)
toot.setSpoiler_text(toot_cw_content.getText().toString().trim());
toot.setVisibility(visibility);
if( tootReply != null)
toot.setIn_reply_to_id(tootReply.getId());
toot.setContent(tootContent);
2019-01-20 09:45:48 +01:00
if( timestamp == null)
2019-01-20 15:20:07 +01:00
if( scheduledstatus == null)
new PostStatusAsyncTask(getApplicationContext(), accountReply, toot, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else {
toot.setScheduled_at(Helper.dateToString(scheduledstatus.getScheduled_date()));
scheduledstatus.setStatus(toot);
isScheduled = true;
new PostActionAsyncTask(getApplicationContext(), API.StatusAction.DELETESCHEDULED, scheduledstatus, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new PostStatusAsyncTask(getApplicationContext(), accountReply, toot, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
2019-01-20 09:45:48 +01:00
else {
2019-01-19 19:41:55 +01:00
toot.setScheduled_at(timestamp);
2019-01-20 09:45:48 +01:00
new PostStatusAsyncTask(getApplicationContext(), accountReply, toot, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2019-01-19 19:41:55 +01:00
}
}
2019-01-20 09:45:48 +01:00
private void serverSchedule(String time){
sendToot(time);
2019-01-20 15:20:07 +01:00
isScheduled = true;
2019-01-19 19:41:55 +01:00
resetForNextToot();
}
private void deviceSchedule(long time){
//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
ScheduledTootsSyncJob.schedule(getApplicationContext(), currentToId, time);
resetForNextToot();
}
private void resetForNextToot(){
//Clear content
toot_content.setText("");
toot_cw_content.setText("");
toot_space_left.setText("0");
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);
}
List<Attachment> tmp_attachment = new ArrayList<>();
tmp_attachment.addAll(attachments);
attachments.removeAll(tmp_attachment);
tmp_attachment.clear();
}
isSensitive = false;
toot_sensitive.setVisibility(View.GONE);
currentToId = -1;
Toasty.info(TootActivity.this,getString(R.string.toot_scheduled), Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_toot, menu);
2017-07-16 19:04:53 +02:00
if( restored != -1 ){
MenuItem itemRestore = menu.findItem(R.id.action_restore);
if( itemRestore != null)
itemRestore.setVisible(false);
MenuItem itemSchedule = menu.findItem(R.id.action_schedule);
2018-08-19 10:26:17 +02:00
if( restoredScheduled )
2017-07-16 19:04:53 +02:00
itemSchedule.setVisible(false);
}
MenuItem itemViewReply = menu.findItem(R.id.action_view_reply);
if( tootReply == null){
if( itemViewReply != null)
itemViewReply.setVisible(false);
}
2018-11-03 12:06:44 +01:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
if( theme == THEME_LIGHT)
Helper.colorizeIconMenu(menu, R.color.black);
changeColor();
2017-11-03 15:20:50 +01:00
String instanceVersion = sharedpreferences.getString(Helper.INSTANCE_VERSION + userId + instance, null);
2017-11-03 13:55:18 +01:00
Version currentVersion = new Version(instanceVersion);
Version minVersion = new Version("2.0");
MenuItem itemEmoji = menu.findItem(R.id.action_emoji);
2017-11-24 07:25:04 +01:00
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
final List<Emojis> emojis = new CustomEmojiDAO(getApplicationContext(), db).getAllEmojis();
//Displays button only if custom emojis
if (emojis != null && emojis.size() > 0 && (currentVersion.compareTo(minVersion) == 1 || currentVersion.equals(minVersion))) {
2017-11-03 13:55:18 +01:00
itemEmoji.setVisible(true);
}else{
itemEmoji.setVisible(false);
}
if( accountReply != null){
MenuItem itemRestore = menu.findItem(R.id.action_restore);
if( itemRestore != null)
itemRestore.setVisible(false);
MenuItem itemSchedule = menu.findItem(R.id.action_schedule);
if( itemSchedule != null)
itemSchedule.setVisible(false);
MenuItem itemStore= menu.findItem(R.id.action_store);
if( itemStore != null)
itemStore.setVisible(false);
}
return true;
}
2017-05-05 16:36:04 +02:00
@Override
public void onRetrieveAttachment(final Attachment attachment, Error error) {
2017-11-20 16:05:42 +01:00
if( error != null || attachment == null){
2018-09-09 13:58:29 +02:00
if( error != null)
2018-11-25 10:45:16 +01:00
Toasty.error(getApplicationContext(), error.getError(), Toast.LENGTH_LONG).show();
2018-09-09 13:58:29 +02:00
else
2018-11-25 10:45:16 +01:00
Toasty.error(getApplicationContext(), getString(R.string.toast_error), Toast.LENGTH_LONG).show();
2017-08-02 17:44:36 +02:00
if( attachments.size() == 0 )
toot_picture_container.setVisibility(View.GONE);
toot_picture.setEnabled(true);
toot_it.setEnabled(true);
return;
}
2017-10-27 11:15:47 +02:00
boolean alreadyAdded = false;
int index = 0;
for(Attachment attach_: this.attachments){
if( attach_.getId().equals(attachment.getId())){
alreadyAdded = true;
break;
}
2017-10-27 11:15:47 +02:00
index++;
}
if( !alreadyAdded){
toot_picture_container.setVisibility(View.VISIBLE);
2017-11-20 16:05:42 +01:00
String url = attachment.getPreview_url();
if (url == null || url.trim().equals(""))
url = attachment.getUrl();
final ImageView imageView = new ImageView(getApplicationContext());
imageView.setId(Integer.parseInt(attachment.getId()));
2017-12-02 16:54:59 +01:00
2017-12-02 15:05:54 +01:00
Glide.with(imageView.getContext())
2017-12-02 16:54:59 +01:00
.asBitmap()
2017-12-02 11:02:25 +01:00
.load(url)
2017-12-02 16:54:59 +01:00
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
2017-12-02 16:54:59 +01:00
imageView.setImageBitmap(resource);
}
});
2017-11-20 16:05:42 +01:00
LinearLayout.LayoutParams imParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
imParams.setMargins(20, 5, 20, 5);
imParams.height = (int) Helper.convertDpToPixel(100, getApplicationContext());
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
2018-09-09 13:58:29 +02:00
boolean show_media_urls = sharedpreferences.getBoolean(Helper.SET_MEDIA_URLS, false);
2017-11-20 16:05:42 +01:00
if (show_media_urls) {
//Adds the shorter text_url of attachment at the end of the toot
int selectionBefore = toot_content.getSelectionStart();
toot_content.setText(String.format("%s\n\n%s",toot_content.getText().toString(), attachment.getText_url()));
2017-11-20 16:05:42 +01:00
toot_space_left.setText(String.valueOf(toot_content.length()));
//Moves the cursor
toot_content.setSelection(selectionBefore);
2017-11-20 16:05:42 +01:00
}
imageView.setTag(attachment.getId());
2017-11-20 16:05:42 +01:00
toot_picture_container.addView(imageView, attachments.size(), imParams);
imageView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
showRemove(imageView.getId());
return false;
2017-05-05 16:36:04 +02:00
}
2017-11-20 16:05:42 +01:00
});
String instanceVersion = sharedpreferences.getString(Helper.INSTANCE_VERSION + userId + instance, null);
if (instanceVersion != null) {
Version currentVersion = new Version(instanceVersion);
Version minVersion = new Version("2.0");
if (currentVersion.compareTo(minVersion) == 1 || currentVersion.equals(minVersion)) {
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
2018-01-10 16:00:57 +01:00
showAddDescription(attachment);
2017-11-20 16:05:42 +01:00
}
});
2017-10-27 11:15:47 +02:00
}
2017-10-27 11:45:37 +02:00
}
2017-11-20 16:05:42 +01:00
attachments.add(attachment);
addBorder();
2017-11-20 16:05:42 +01:00
if (attachments.size() < 4)
toot_picture.setEnabled(true);
toot_it.setEnabled(true);
2017-11-20 16:05:42 +01:00
toot_sensitive.setVisibility(View.VISIBLE);
picture_scrollview.setVisibility(View.VISIBLE);
2017-10-27 11:45:37 +02:00
}else {
if( attachments.size() > index && attachment.getDescription() != null) {
attachments.get(index).setDescription(attachment.getDescription());
}
2017-05-05 16:36:04 +02:00
}
}
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) {
2017-11-18 12:22:41 +01:00
picture_scrollview.setVisibility(View.VISIBLE);
Bitmap pictureMention = BitmapFactory.decodeFile(pathToFile);
2018-01-11 07:26:10 +01:00
if( pictureMention != null) {
String filename = pathToFile.substring(pathToFile.lastIndexOf("/") + 1);
2018-01-11 07:26:10 +01:00
ByteArrayOutputStream bos = new ByteArrayOutputStream();
if( filename.contains(".png") || filename.contains(".PNG"))
pictureMention.compress(Bitmap.CompressFormat.PNG, 0, bos);
else
pictureMention.compress(Bitmap.CompressFormat.JPEG, 80, bos);
2018-01-11 07:26:10 +01:00
byte[] bitmapdata = bos.toByteArray();
ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);
toot_picture_container.setVisibility(View.VISIBLE);
toot_picture.setEnabled(false);
toot_it.setEnabled(false);
2018-08-16 12:08:38 +02:00
new HttpsConnection(TootActivity.this).upload(bs, filename, accountReply!=null?accountReply.getToken():null, TootActivity.this);
2018-01-11 07:26:10 +01:00
}
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);
if( progress <= 100) {
progressBar.setScaleY(3f);
progress_bar_container.setVisibility(View.VISIBLE);
progressBar.setProgress(progress);
toolbar_text.setText(String.format("%s%%", progress));
}else{
progress_bar_container.setVisibility(View.GONE);
}
}
2018-01-10 16:00:57 +01: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);
@SuppressLint("InflateParams") View popup_media_description = getLayoutInflater().inflate( R.layout.popup_media_description, null );
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);
2018-12-16 14:29:23 +01:00
Glide.with(getApplicationContext())
.asBitmap()
.load(attachment.getUrl())
.into(new SimpleTarget<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);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
media_picture.setImageAlpha(60);
}else {
media_picture.setAlpha(60);
}
}
});
2017-11-15 14:05:41 +01:00
builderInner.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
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
}
builderInner.setPositiveButton(R.string.validate, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
2018-11-21 11:14:33 +01:00
new UpdateDescriptionAttachmentAsyncTask(getApplicationContext(), attachment.getId(), input.getText().toString(), accountReply,TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
attachment.setDescription(input.getText().toString());
addBorder();
2017-11-15 14:05:41 +01:00
dialog.dismiss();
}
});
AlertDialog alertDialog = builderInner.create();
alertDialog.show();
}
2017-05-05 16:36:04 +02:00
/**
* Removes a media
* @param viewId String
*/
private void showRemove(final int viewId){
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);
dialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
dialog.dismiss();
}
});
dialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
View namebar = findViewById(viewId);
for(Attachment attachment: attachments){
if( Integer.valueOf(attachment.getId()) == viewId){
attachments.remove(attachment);
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
2018-09-09 13:58:29 +02:00
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(toot_content.length()));
//Moves the cursor
2017-10-14 16:45:18 +02:00
if (selectionBefore >= 0 && selectionBefore < toot_content.length())
toot_content.setSelection(selectionBefore);
}
((ViewGroup) namebar.getParent()).removeView(namebar);
break;
}
}
2017-05-05 16:36:04 +02:00
dialog.dismiss();
2017-06-07 15:47:05 +02:00
if( attachments.size() == 0 ) {
toot_sensitive.setVisibility(View.GONE);
isSensitive = false;
toot_sensitive.setChecked(false);
2017-08-02 17:44:36 +02:00
picture_scrollview.setVisibility(View.GONE);
2017-06-07 15:47:05 +02:00
}
toot_picture.setEnabled(true);
2017-05-05 16:36:04 +02:00
}
});
dialog.show();
}
private void tootVisibilityDialog(){
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);
dialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int position) {
dialog.dismiss();
}
});
dialog.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int position) {
switch (position){
case 0:
visibility = "public";
2017-10-29 07:15:13 +01:00
toot_visibility.setImageResource(R.drawable.ic_public_toot);
2017-05-05 16:36:04 +02:00
break;
case 1:
visibility = "unlisted";
2017-10-29 07:15:13 +01:00
toot_visibility.setImageResource(R.drawable.ic_lock_open_toot);
2017-05-05 16:36:04 +02:00
break;
case 2:
visibility = "private";
2017-10-29 07:15:13 +01:00
toot_visibility.setImageResource(R.drawable.ic_lock_outline_toot);
2017-05-05 16:36:04 +02:00
break;
case 3:
visibility = "direct";
2017-10-29 07:15:13 +01:00
toot_visibility.setImageResource(R.drawable.ic_mail_outline_toot);
2017-05-05 16:36:04 +02:00
break;
}
2017-10-28 14:32:18 +02:00
2017-05-05 16:36:04 +02:00
dialog.dismiss();
}
});
dialog.show();
}
2017-07-15 14:59:09 +02:00
@Override
public void onPause(){
super.onPause();
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean storeToot = sharedpreferences.getBoolean(Helper.SET_AUTO_STORE, true);
if( storeToot && accountReply == null)
2017-11-15 14:14:41 +01:00
storeToot(true, false);
else if( storeToot)
storeToot(false, false);
2017-07-15 14:59:09 +02:00
}
2017-05-05 16:36:04 +02:00
@Override
public void onPostStatusAction(APIResponse apiResponse) {
2018-10-06 15:37:44 +02:00
if( apiResponse.getError() != null ){
2017-08-18 16:54:43 +02:00
toot_it.setEnabled(true);
if( apiResponse.getError().getError().contains("422")){
showAToast(getString(R.string.toast_error_char_limit));
2019-01-25 16:54:48 +01:00
return;
}else if( apiResponse.getError().getStatusCode() == -33){
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-01-25 16:54:48 +01:00
}
2018-08-15 11:24:57 +02:00
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean split_toot = sharedpreferences.getBoolean(Helper.SET_AUTOMATICALLY_SPLIT_TOOTS, false);
int split_toot_size = sharedpreferences.getInt(Helper.SET_AUTOMATICALLY_SPLIT_TOOTS_SIZE, Helper.SPLIT_TOOT_SIZE);
2018-09-07 07:58:20 +02:00
int cwSize = toot_cw_content.getText().toString().trim().length();
int size = toot_content.getText().toString().trim().length() + cwSize;
2018-08-15 11:24:57 +02:00
2018-09-07 07:58:20 +02:00
if( split_toot && (size >= split_toot_size) && 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);
if( toot_cw_content.getText().toString().trim().length() > 0)
toot.setSpoiler_text(toot_cw_content.getText().toString().trim());
toot.setVisibility(visibility);
2018-10-06 15:37:44 +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());
toot.setContent(tootContent);
new PostStatusAsyncTask(getApplicationContext(), accountReply, toot, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
return;
}
if(restored != -1){
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
new StatusStoredDAO(getApplicationContext(), db).remove(restored);
}
//Clear the toot
toot_content.setText("");
toot_cw_content.setText("");
toot_space_left.setText("0");
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);
}
List<Attachment> tmp_attachment = new ArrayList<>();
tmp_attachment.addAll(attachments);
attachments.removeAll(tmp_attachment);
tmp_attachment.clear();
}
isSensitive = false;
toot_sensitive.setVisibility(View.GONE);
currentToId = -1;
2019-01-25 16:54:48 +01:00
if(apiResponse.getError() == null) {
if (scheduledstatus == null && !isScheduled)
Toasty.success(TootActivity.this, getString(R.string.toot_sent), Toast.LENGTH_LONG).show();
else
Toasty.success(TootActivity.this, getString(R.string.toot_scheduled), Toast.LENGTH_LONG).show();
}else {
if(apiResponse.getError().getStatusCode() == -33)
Toasty.info(TootActivity.this, getString(R.string.toast_toot_saved_error), Toast.LENGTH_LONG).show();
}
toot_it.setEnabled(true);
//It's a reply, so the user will be redirect to its answer
if( tootReply != null){
List<Status> statuses = apiResponse.getStatuses();
if( statuses != null && statuses.size() > 0 ){
Status status = statuses.get(0);
if( status != null ) {
Intent intent = new Intent(getApplicationContext(), ShowConversationActivity.class);
Bundle b = new Bundle();
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
}
}
}else {
2018-11-03 19:25:36 +01:00
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra(INTENT_ACTION, HOME_TIMELINE_INTENT);
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) {
2017-08-02 12:17:43 +02:00
if( pp_progress != null && pp_actionBar != null) {
pp_progress.setVisibility(View.GONE);
pp_actionBar.setVisibility(View.VISIBLE);
}
if( apiResponse.getError() != null)
return;
final List<Account> accounts = apiResponse.getAccounts();
2017-05-26 17:20:36 +02:00
if( accounts != null && accounts.size() > 0){
AccountsSearchAdapter accountsListAdapter = new AccountsSearchAdapter(TootActivity.this, accounts);
toot_content.setThreshold(1);
2017-08-02 17:44:36 +02:00
toot_content.setAdapter(accountsListAdapter);
2017-08-09 18:27:03 +02:00
final String oldContent = toot_content.getText().toString();
2017-10-18 14:25:51 +02:00
if( oldContent.length() >= currentCursorPosition) {
String[] searchA = oldContent.substring(0, currentCursorPosition).split("@");
if (searchA.length > 0) {
final String search = searchA[searchA.length - 1];
toot_content.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Account account = accounts.get(position);
String deltaSearch = "";
if (currentCursorPosition - searchLength > 0 && currentCursorPosition < oldContent.length())
deltaSearch = oldContent.substring(currentCursorPosition - searchLength, currentCursorPosition);
else {
if (currentCursorPosition >= oldContent.length())
deltaSearch = oldContent.substring(currentCursorPosition - searchLength, oldContent.length());
}
2017-10-18 14:25:51 +02:00
if (!search.equals(""))
deltaSearch = deltaSearch.replace("@" + search, "");
String newContent = oldContent.substring(0, currentCursorPosition - searchLength);
newContent += deltaSearch;
newContent += "@" + account.getAcct() + " ";
int newPosition = newContent.length();
2017-12-02 17:18:38 +01:00
if (currentCursorPosition < oldContent.length() )
newContent += oldContent.substring(currentCursorPosition, oldContent.length());
2017-10-18 14:25:51 +02:00
toot_content.setText(newContent);
toot_space_left.setText(String.valueOf(toot_content.length()));
toot_content.setSelection(newPosition);
AccountsSearchAdapter accountsListAdapter = new AccountsSearchAdapter(TootActivity.this, new ArrayList<>());
2017-10-18 14:25:51 +02:00
toot_content.setThreshold(1);
toot_content.setAdapter(accountsListAdapter);
}
});
}
}
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-01-23 19:35:08 +01: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-01-23 19:23:26 +01: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);
AccountsReplyAdapter contactAdapter = new AccountsReplyAdapter(TootActivity.this, this.contacts, this.checkedValues);
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) {
2017-11-01 19:19:37 +01:00
if( pp_progress != null && pp_actionBar != null) {
pp_progress.setVisibility(View.GONE);
pp_actionBar.setVisibility(View.VISIBLE);
}
if( emojis != null && emojis.size() > 0){
2017-11-01 19:34:53 +01:00
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(":");
final String search = searchA[searchA.length-1];
toot_content.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String shortcode = emojis.get(position).getShortcode();
String deltaSearch = "";
if( currentCursorPosition-searchLength > 0 && currentCursorPosition < oldContent.length() )
deltaSearch = oldContent.substring(currentCursorPosition-searchLength, currentCursorPosition);
else {
if( currentCursorPosition >= oldContent.length() )
deltaSearch = oldContent.substring(currentCursorPosition-searchLength, oldContent.length());
}
if( !search.equals(""))
deltaSearch = deltaSearch.replace(":"+search,"");
String newContent = oldContent.substring(0,currentCursorPosition-searchLength);
newContent += deltaSearch;
newContent += ":" + shortcode + ": ";
int newPosition = newContent.length();
2017-12-02 17:18:38 +01:00
if( currentCursorPosition < oldContent.length() )
2017-11-01 19:34:53 +01:00
newContent += oldContent.substring(currentCursorPosition, oldContent.length()-1);
toot_content.setText(newContent);
toot_space_left.setText(String.valueOf(toot_content.length()));
toot_content.setSelection(newPosition);
EmojisSearchAdapter emojisSearchAdapter = new EmojisSearchAdapter(TootActivity.this, new ArrayList<>());
2017-11-01 19:34:53 +01:00
toot_content.setThreshold(1);
toot_content.setAdapter(emojisSearchAdapter);
}
});
2017-11-01 19:19:37 +01:00
}
}
2017-10-09 18:35:43 +02:00
@Override
public void onRetrieveSearch(Results results, Error error) {
if( pp_progress != null && pp_actionBar != null) {
pp_progress.setVisibility(View.GONE);
pp_actionBar.setVisibility(View.VISIBLE);
}
if( results == null)
2017-10-09 18:35:43 +02:00
return;
2017-10-09 18:35:43 +02:00
final List<String> tags = results.getHashtags();
if( tags != null && tags.size() > 0){
TagsSearchAdapter tagsSearchAdapter = new TagsSearchAdapter(TootActivity.this, tags);
toot_content.setThreshold(1);
toot_content.setAdapter(tagsSearchAdapter);
final String oldContent = toot_content.getText().toString();
2018-09-19 08:50:10 +02:00
if( oldContent.length() < currentCursorPosition)
return;
String[] searchA = oldContent.substring(0,currentCursorPosition).split("#");
2018-11-29 18:48:08 +01:00
if( searchA.length < 1)
return;
2017-10-09 18:35:43 +02:00
final String search = searchA[searchA.length-1];
toot_content.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
2018-01-10 14:39:01 +01:00
if( position >= tags.size() )
return;
2017-10-09 18:35:43 +02:00
String tag = tags.get(position);
String deltaSearch = "";
if( currentCursorPosition-searchLength > 0 && currentCursorPosition < oldContent.length() )
deltaSearch = oldContent.substring(currentCursorPosition-searchLength, currentCursorPosition);
else {
if( currentCursorPosition >= oldContent.length() )
deltaSearch = oldContent.substring(currentCursorPosition-searchLength, oldContent.length());
}
if( !search.equals(""))
deltaSearch = deltaSearch.replace("#"+search,"");
String newContent = oldContent.substring(0,currentCursorPosition-searchLength);
newContent += deltaSearch;
newContent += "#" + tag + " ";
int newPosition = newContent.length();
2017-12-02 17:18:38 +01:00
if( currentCursorPosition < oldContent.length() )
2017-10-09 18:35:43 +02:00
newContent += oldContent.substring(currentCursorPosition, oldContent.length()-1);
toot_content.setText(newContent);
toot_space_left.setText(String.valueOf(toot_content.length()));
toot_content.setSelection(newPosition);
TagsSearchAdapter tagsSearchAdapter = new TagsSearchAdapter(TootActivity.this, new ArrayList<>());
2017-10-09 18:35:43 +02:00
toot_content.setThreshold(1);
toot_content.setAdapter(tagsSearchAdapter);
}
});
}
2017-05-05 16:36:04 +02:00
}
2017-07-16 19:04:53 +02:00
private void restoreToot(long id){
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
StoredStatus draft = new StatusStoredDAO(TootActivity.this, db).getStatus(id);
2019-01-08 16:24:02 +01:00
if( draft == null)
return;
2017-07-16 19:04:53 +02:00
Status status = draft.getStatus();
//Retrieves attachments
2018-08-18 20:12:37 +02:00
if( removed ){
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<>();
if( childCount > 0 ){
for(int i = 0 ; i < childCount ; i++){
if( toot_picture_container.getChildAt(i) instanceof ImageView)
2017-10-25 09:46:05 +02:00
toRemove.add((ImageView)toot_picture_container.getChildAt(i));
2017-08-06 17:03:15 +02:00
}
if( toRemove.size() > 0){
for(ImageView imageView: toRemove)
toot_picture_container.removeView(imageView);
}
toRemove.clear();
}
2018-10-16 18:02:52 +02:00
String content = status.getContent();
2018-09-22 19:13:55 +02: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
}
2018-10-16 18:02:52 +02:00
if( removed ) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
content = Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY).toString();
else
//noinspection deprecation
content = Html.fromHtml(content).toString();
}
2017-07-16 19:04:53 +02:00
if( attachments != null && attachments.size() > 0){
toot_picture_container.setVisibility(View.VISIBLE);
2017-08-02 17:44:36 +02:00
picture_scrollview.setVisibility(View.VISIBLE);
2017-07-16 19:04:53 +02:00
int i = 0 ;
2017-11-15 14:05:41 +01:00
for(final Attachment attachment: attachments){
2017-07-16 19:04:53 +02:00
String url = attachment.getPreview_url();
if( url == null || url.trim().equals(""))
url = attachment.getUrl();
final ImageView imageView = new ImageView(getApplicationContext());
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);
imParams.height = (int) Helper.convertDpToPixel(100, getApplicationContext());
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)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
2017-12-02 16:54:59 +01:00
imageView.setImageBitmap(resource);
}
});
imageView.setTag(attachment.getId());
2017-07-16 19:04:53 +02:00
imageView.setOnClickListener(new View.OnClickListener() {
@Override
2017-11-15 14:05:41 +01:00
public void onClick(View view) {
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
String instanceVersion = sharedpreferences.getString(Helper.INSTANCE_VERSION + userId + instance, null);
if (instanceVersion != null) {
Version currentVersion = new Version(instanceVersion);
Version minVersion = new Version("2.0");
if (currentVersion.compareTo(minVersion) == 1 || currentVersion.equals(minVersion)) {
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
2018-01-10 16:00:57 +01:00
showAddDescription(attachment);
2017-11-15 14:05:41 +01:00
}
});
}
}
}
});
imageView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
2017-07-16 19:04:53 +02:00
showRemove(imageView.getId());
2017-11-15 14:05:41 +01:00
return false;
2017-07-16 19:04:53 +02:00
}
});
addBorder();
2017-07-16 19:04:53 +02:00
if( attachments.size() < 4)
toot_picture.setEnabled(true);
toot_sensitive.setVisibility(View.VISIBLE);
i++;
}
}else {
toot_picture_container.setVisibility(View.GONE);
}
//Sensitive content
toot_sensitive.setChecked(status.isSensitive());
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);
}else {
toot_cw_content.setText("");
toot_cw_content.setVisibility(View.GONE);
}
2018-08-18 14:41:02 +02:00
2018-10-12 17:53:06 +02:00
toot_content.setText(content);
toot_space_left.setText(String.valueOf(toot_content.length()));
2017-07-16 19:04:53 +02:00
toot_content.setSelection(toot_content.getText().length());
switch (status.getVisibility()){
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();
if( tootReply != null) {
tootReply();
2017-10-01 19:01:15 +02:00
}else {
2017-07-21 21:09:32 +02:00
if( title != null)
title.setText(getString(R.string.toot_title));
else
setTitle(R.string.toot_title);
}
2017-10-01 19:01:15 +02:00
invalidateOptionsMenu();
initialContent = toot_content.getText().toString();
2017-08-18 16:54:43 +02:00
toot_space_left.setText(String.valueOf(toot_content.getText().length() + toot_cw_content.getText().length()));
2017-07-16 19:04:53 +02:00
}
2017-08-02 17:44:36 +02:00
2019-01-20 15:20:07 +01:00
private void restoreServerSchedule(Status status){
attachments = status.getMedia_attachments();
int childCount = toot_picture_container.getChildCount();
ArrayList<ImageView> toRemove = new ArrayList<>();
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));
}
if( toRemove.size() > 0){
for(ImageView imageView: toRemove)
toot_picture_container.removeView(imageView);
}
toRemove.clear();
}
String content = status.getContent();
Pattern mentionLink = Pattern.compile("(<\\s?a\\s?href=\"https?:\\/\\/([\\da-z\\.-]+\\.[a-z\\.]{2,10})\\/(@[\\/\\w._-]*)\"\\s?[^.]*<\\s?\\/\\s?a\\s?>)");
Matcher matcher = mentionLink.matcher(content);
if (matcher.find()) {
content = matcher.replaceAll("$3@$2");
}
if( removed ) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
content = Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY).toString();
else
//noinspection deprecation
content = Html.fromHtml(content).toString();
}
if( attachments != null && attachments.size() > 0){
toot_picture_container.setVisibility(View.VISIBLE);
picture_scrollview.setVisibility(View.VISIBLE);
int i = 0 ;
for(final Attachment attachment: attachments){
String url = attachment.getPreview_url();
if( url == null || url.trim().equals(""))
url = attachment.getUrl();
final ImageView imageView = new ImageView(getApplicationContext());
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);
imParams.height = (int) Helper.convertDpToPixel(100, getApplicationContext());
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
toot_picture_container.addView(imageView, i, imParams);
Glide.with(imageView.getContext())
.asBitmap()
.load(url)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
imageView.setImageBitmap(resource);
}
});
imageView.setTag(attachment.getId());
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
String instanceVersion = sharedpreferences.getString(Helper.INSTANCE_VERSION + userId + instance, null);
if (instanceVersion != null) {
Version currentVersion = new Version(instanceVersion);
Version minVersion = new Version("2.0");
if (currentVersion.compareTo(minVersion) == 1 || currentVersion.equals(minVersion)) {
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showAddDescription(attachment);
}
});
}
}
}
});
imageView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
showRemove(imageView.getId());
return false;
}
});
addBorder();
if( attachments.size() < 4)
toot_picture.setEnabled(true);
toot_sensitive.setVisibility(View.VISIBLE);
i++;
}
}else {
toot_picture_container.setVisibility(View.GONE);
}
//Sensitive content
toot_sensitive.setChecked(status.isSensitive());
if( status.getSpoiler_text() != null && status.getSpoiler_text().length() > 0 ){
toot_cw_content.setText(status.getSpoiler_text());
toot_cw_content.setVisibility(View.VISIBLE);
}else {
toot_cw_content.setText("");
toot_cw_content.setVisibility(View.GONE);
}
toot_content.setText(content);
toot_space_left.setText(String.valueOf(toot_content.length()));
toot_content.setSelection(toot_content.getText().length());
switch (status.getVisibility()){
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;
}
if( title != null)
title.setText(getString(R.string.toot_title));
else
setTitle(R.string.toot_title);
invalidateOptionsMenu();
initialContent = toot_content.getText().toString();
toot_space_left.setText(String.valueOf(toot_content.getText().length() + toot_cw_content.getText().length()));
}
private void tootReply(){
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
2017-07-21 21:09:32 +02:00
if( title != null)
title.setText(getString(R.string.toot_title_reply));
else
setTitle(R.string.toot_title_reply);
2017-12-28 17:25:36 +01:00
String userIdReply;
2017-10-04 15:57:53 +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
if( restored == -1 ){
//Gets the default visibility, will be used if not set in settings
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;
switch (tootReply.getVisibility()){
case "public":
initialTootVisibility = 4;
break;
case "unlisted":
initialTootVisibility = 3;
break;
case "private":
visibility = "private";
initialTootVisibility = 2;
break;
case "direct":
visibility = "direct";
initialTootVisibility = 1;
break;
}
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;
if( ownerTootVisibility >= initialTootVisibility){
tootVisibility = initialTootVisibility;
}else {
tootVisibility = ownerTootVisibility;
}
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;
}
2017-11-07 16:08:31 +01:00
if( tootReply.getSpoiler_text() != null && tootReply.getSpoiler_text().length() > 0) {
toot_cw_content.setText(tootReply.getSpoiler_text());
toot_cw_content.setVisibility(View.VISIBLE);
}
//Retrieves mentioned accounts + OP and adds them at the beginin of the toot
ArrayList<String> mentionedAccountsAdded = new ArrayList<>();
int cursorReply = 0;
2017-12-28 17:25:36 +01:00
if( tootReply.getAccount() != null && tootReply.getAccount().getAcct() != null && !tootReply.getAccount().getId().equals(userIdReply)) {
toot_content.setText(String.format("@%s", tootReply.getAccount().getAcct()));
mentionedAccountsAdded.add(tootReply.getAccount().getAcct());
//Evaluate the cursor position => mention length + 1 char for carriage return
cursorReply = toot_content.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)
toot_content.setText(String.format("%s", (toot_content.getText().toString() + "\n\n")));
else
toot_content.setText(String.format("%s", (toot_content.getText().toString() + " ")));
for(Mention mention : tootReply.getMentions()){
2017-12-28 17:25:36 +01:00
if( mention.getAcct() != null && !mention.getId().equals(userIdReply) && !mentionedAccountsAdded.contains(mention.getAcct())) {
mentionedAccountsAdded.add(mention.getAcct());
String tootTemp = String.format("@%s ", mention.getAcct());
toot_content.setText(String.format("%s ", (toot_content.getText().toString() + tootTemp.trim())));
}
}
2017-12-26 17:10:30 +01:00
toot_content.setText(toot_content.getText().toString().trim());
2018-01-06 09:41:16 +01:00
if (toot_content.getText().toString().startsWith("@") ) {
if( capitalize )
toot_content.append("\n");
else
toot_content.append(" ");
2017-12-26 17:10:30 +01:00
}
toot_space_left.setText(String.valueOf(toot_content.length()));
toot_content.requestFocus();
if( capitalize) {
if (mentionedAccountsAdded.size() == 1) {
toot_content.setSelection(toot_content.getText().length()); //Put cursor at the end
} else {
if (cursorReply > 0 && cursorReply < toot_content.getText().length())
toot_content.setSelection(cursorReply);
else
toot_content.setSelection(toot_content.getText().length()); //Put cursor at the end
}
}else {
toot_content.setSelection(toot_content.getText().length()); //Put cursor at the end
}
}
}
initialContent = toot_content.getText().toString();
}
2017-11-15 14:14:41 +01:00
private void storeToot(boolean message, boolean forced){
2017-07-15 14:59:09 +02:00
//Nothing to store here....
2017-11-15 14:14:41 +01:00
if( !forced) {
if (toot_content.getText().toString().trim().length() == 0 && (attachments == null || attachments.size() < 1) && toot_cw_content.getText().toString().trim().length() == 0)
return;
if (initialContent.trim().equals(toot_content.getText().toString().trim()))
return;
}
2017-07-15 14:59:09 +02:00
Status toot = new Status();
toot.setSensitive(isSensitive);
toot.setMedia_attachments(attachments);
if( toot_cw_content.getText().toString().trim().length() > 0)
toot.setSpoiler_text(toot_cw_content.getText().toString().trim());
toot.setVisibility(visibility);
toot.setContent(toot_content.getText().toString().trim());
2017-09-22 14:44:00 +02:00
2017-07-15 14:59:09 +02:00
if( tootReply != null)
toot.setIn_reply_to_id(tootReply.getId());
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
try{
if( currentToId == -1 ) {
currentToId = new StatusStoredDAO(TootActivity.this, db).insertStatus(toot, tootReply);
2017-07-15 14:59:09 +02:00
}else{
StoredStatus storedStatus = new StatusStoredDAO(TootActivity.this, db).getStatus(currentToId);
if( storedStatus != null ){
new StatusStoredDAO(TootActivity.this, db).updateStatus(currentToId, toot);
}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
}
}
2017-07-16 17:09:35 +02:00
if( message )
2018-11-25 10:45:16 +01:00
Toasty.success(getApplicationContext(), getString(R.string.toast_toot_saved), Toast.LENGTH_LONG).show();
2017-07-15 14:59:09 +02:00
}catch (Exception e){
2017-07-16 17:09:35 +02:00
if( message)
2018-11-25 10:45:16 +01:00
Toasty.error(getApplicationContext(), getString(R.string.toast_error), Toast.LENGTH_LONG).show();
2017-07-15 14:59:09 +02:00
}
}
private void changeColor(){
2017-08-13 18:31:44 +02:00
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
if( theme == Helper.THEME_DARK || theme == Helper.THEME_BLACK) {
2017-10-29 07:15:13 +01:00
changeDrawableColor(TootActivity.this, R.drawable.ic_public_toot, R.color.dark_text);
changeDrawableColor(TootActivity.this, R.drawable.ic_lock_open_toot, R.color.dark_text);
changeDrawableColor(TootActivity.this, R.drawable.ic_lock_outline_toot, R.color.dark_text);
changeDrawableColor(TootActivity.this, R.drawable.ic_mail_outline_toot, R.color.dark_text);
2017-10-11 14:09:28 +02:00
changeDrawableColor(TootActivity.this, R.drawable.ic_insert_photo, R.color.dark_text);
2017-08-13 18:31:44 +02:00
changeDrawableColor(TootActivity.this, R.drawable.ic_skip_previous, R.color.dark_text);
changeDrawableColor(TootActivity.this, R.drawable.ic_skip_next, R.color.dark_text);
changeDrawableColor(TootActivity.this, R.drawable.ic_check, R.color.dark_text);
changeDrawableColor(TootActivity.this, R.drawable.emoji_one_category_smileysandpeople, R.color.dark_text);
}else {
2017-10-29 07:15:13 +01:00
changeDrawableColor(TootActivity.this, R.drawable.ic_public_toot, R.color.white);
changeDrawableColor(TootActivity.this, R.drawable.ic_lock_open_toot, R.color.white);
changeDrawableColor(TootActivity.this, R.drawable.ic_lock_outline_toot, R.color.white);
changeDrawableColor(TootActivity.this, R.drawable.ic_mail_outline_toot, R.color.white);
2017-10-11 14:09:28 +02:00
changeDrawableColor(TootActivity.this, R.drawable.ic_insert_photo, R.color.white);
2017-08-13 18:31:44 +02:00
changeDrawableColor(TootActivity.this, R.drawable.ic_skip_previous, R.color.white);
changeDrawableColor(TootActivity.this, R.drawable.ic_skip_next, R.color.white);
changeDrawableColor(TootActivity.this, R.drawable.ic_check, R.color.white);
changeDrawableColor(TootActivity.this, R.drawable.emoji_one_category_smileysandpeople, R.color.black);
}
}
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;
2017-10-25 10:51:36 +02:00
for(Account account: accounts) {
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);
2017-10-25 10:51:36 +02:00
AccountsReplyAdapter accountsReplyAdapter = new AccountsReplyAdapter(TootActivity.this, accounts, checkedValues);
2017-10-25 11:00:04 +02:00
builderSingle.setTitle(getString(R.string.select_accounts)).setAdapter(accountsReplyAdapter, null);
2017-10-25 09:04:34 +02:00
builderSingle.setNegativeButton(R.string.validate, new DialogInterface.OnClickListener() {
2017-10-25 07:53:31 +02:00
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
2017-10-25 09:43:38 +02:00
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
public void changeAccountReply(boolean isChecked, String acct){
if (isChecked) {
if( !toot_content.getText().toString().contains(acct))
2017-10-29 17:25:45 +01:00
toot_content.setText(String.format("%s %s",acct, toot_content.getText()));
2017-10-25 10:51:36 +02:00
} else {
toot_content.setText(toot_content.getText().toString().replaceAll("\\s*" +acct, ""));
}
}
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
private void addBorder(){
for (int i = 0; i < toot_picture_container.getChildCount(); i++) {
View v = toot_picture_container.getChildAt(i);
if (v instanceof ImageView) {
for(Attachment attachment: attachments){
2018-12-27 12:13:10 +01:00
if(attachment.getType().toLowerCase().equals("image"))
if( v.getTag().toString().trim().equals(attachment.getId().trim())){
int borderSize = (int)convertDpToPixel(1, TootActivity.this);
int borderSizeTop = (int)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
2017-05-05 16:36:04 +02:00
}