2017-01-20 09:09:10 +01:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
|
|
|
* This file is part of Tusky.
|
|
|
|
*
|
|
|
|
* Tusky 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.
|
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>. */
|
|
|
|
|
2017-01-07 23:24:02 +01:00
|
|
|
package com.keylesspalace.tusky;
|
|
|
|
|
2017-01-16 19:15:42 +01:00
|
|
|
import android.Manifest;
|
|
|
|
import android.app.ProgressDialog;
|
|
|
|
import android.content.ContentResolver;
|
2017-01-07 23:24:02 +01:00
|
|
|
import android.content.Context;
|
2017-01-18 19:35:07 +01:00
|
|
|
import android.content.DialogInterface;
|
2017-01-16 19:15:42 +01:00
|
|
|
import android.content.Intent;
|
2017-01-07 23:24:02 +01:00
|
|
|
import android.content.SharedPreferences;
|
2017-01-16 19:15:42 +01:00
|
|
|
import android.content.pm.PackageManager;
|
2017-03-04 02:44:44 +01:00
|
|
|
import android.content.res.AssetFileDescriptor;
|
2017-01-16 19:15:42 +01:00
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
2017-02-17 23:56:31 +01:00
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
2017-01-16 19:15:42 +01:00
|
|
|
import android.media.MediaMetadataRetriever;
|
|
|
|
import android.media.ThumbnailUtils;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Build;
|
2017-01-07 23:24:02 +01:00
|
|
|
import android.os.Bundle;
|
2017-02-04 01:53:33 +01:00
|
|
|
import android.os.Parcel;
|
2017-02-17 23:56:31 +01:00
|
|
|
import android.os.Parcelable;
|
2017-01-16 19:15:42 +01:00
|
|
|
import android.provider.OpenableColumns;
|
2017-02-27 06:21:46 +01:00
|
|
|
import android.support.annotation.DrawableRes;
|
2017-01-16 19:15:42 +01:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
2017-02-22 20:13:51 +01:00
|
|
|
import android.support.annotation.StringRes;
|
2017-03-07 14:09:33 +01:00
|
|
|
import android.support.design.widget.FloatingActionButton;
|
2017-01-16 19:15:42 +01:00
|
|
|
import android.support.design.widget.Snackbar;
|
2017-03-04 02:44:44 +01:00
|
|
|
import android.support.v13.view.inputmethod.EditorInfoCompat;
|
|
|
|
import android.support.v13.view.inputmethod.InputConnectionCompat;
|
|
|
|
import android.support.v13.view.inputmethod.InputContentInfoCompat;
|
2017-01-16 19:15:42 +01:00
|
|
|
import android.support.v4.app.ActivityCompat;
|
|
|
|
import android.support.v4.content.ContextCompat;
|
2017-03-07 14:09:33 +01:00
|
|
|
import android.support.v7.app.ActionBar;
|
|
|
|
import android.support.v7.widget.Toolbar;
|
2017-01-07 23:24:02 +01:00
|
|
|
import android.text.Editable;
|
2017-03-04 02:44:44 +01:00
|
|
|
import android.text.InputType;
|
2017-01-20 05:59:21 +01:00
|
|
|
import android.text.Spannable;
|
|
|
|
import android.text.Spanned;
|
2017-01-07 23:24:02 +01:00
|
|
|
import android.text.TextWatcher;
|
2017-01-20 05:59:21 +01:00
|
|
|
import android.text.style.ForegroundColorSpan;
|
2017-03-04 02:44:44 +01:00
|
|
|
import android.view.Gravity;
|
2017-03-07 14:09:33 +01:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
2017-01-07 23:24:02 +01:00
|
|
|
import android.view.View;
|
2017-03-04 02:44:44 +01:00
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.view.inputmethod.EditorInfo;
|
|
|
|
import android.view.inputmethod.InputConnection;
|
2017-01-16 19:15:42 +01:00
|
|
|
import android.webkit.MimeTypeMap;
|
2017-01-07 23:24:02 +01:00
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.EditText;
|
2017-01-16 19:15:42 +01:00
|
|
|
import android.widget.ImageButton;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.LinearLayout;
|
2017-03-04 02:44:44 +01:00
|
|
|
import android.widget.RelativeLayout;
|
2017-01-07 23:24:02 +01:00
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import com.android.volley.AuthFailureError;
|
|
|
|
import com.android.volley.Request;
|
|
|
|
import com.android.volley.Response;
|
|
|
|
import com.android.volley.VolleyError;
|
|
|
|
import com.android.volley.toolbox.JsonObjectRequest;
|
|
|
|
|
2017-01-16 19:15:42 +01:00
|
|
|
import org.json.JSONArray;
|
2017-01-07 23:24:02 +01:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2017-01-16 19:15:42 +01:00
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.ArrayList;
|
2017-03-04 02:44:44 +01:00
|
|
|
import java.util.Arrays;
|
2017-01-16 19:15:42 +01:00
|
|
|
import java.util.Date;
|
2017-01-07 23:24:02 +01:00
|
|
|
import java.util.HashMap;
|
2017-01-18 19:35:07 +01:00
|
|
|
import java.util.Iterator;
|
2017-01-16 19:15:42 +01:00
|
|
|
import java.util.List;
|
2017-03-06 01:58:36 +01:00
|
|
|
import java.util.Locale;
|
2017-01-07 23:24:02 +01:00
|
|
|
import java.util.Map;
|
2017-01-16 19:15:42 +01:00
|
|
|
import java.util.Random;
|
2017-01-07 23:24:02 +01:00
|
|
|
|
2017-02-16 19:52:55 +01:00
|
|
|
public class ComposeActivity extends BaseActivity {
|
2017-02-17 23:56:31 +01:00
|
|
|
private static final String TAG = "ComposeActivity"; // logging tag, and volley request tag
|
2017-01-16 19:15:42 +01:00
|
|
|
private static final int STATUS_CHARACTER_LIMIT = 500;
|
|
|
|
private static final int STATUS_MEDIA_SIZE_LIMIT = 4000000; // 4MB
|
|
|
|
private static final int MEDIA_PICK_RESULT = 1;
|
|
|
|
private static final int PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1;
|
2017-03-04 02:44:44 +01:00
|
|
|
private static final int MEDIA_SIZE_UNKNOWN = -1;
|
2017-01-07 23:24:02 +01:00
|
|
|
|
2017-01-20 05:59:21 +01:00
|
|
|
private String inReplyToId;
|
2017-01-07 23:24:02 +01:00
|
|
|
private String domain;
|
|
|
|
private String accessToken;
|
|
|
|
private EditText textEditor;
|
2017-01-16 19:15:42 +01:00
|
|
|
private LinearLayout mediaPreviewBar;
|
2017-02-17 23:56:31 +01:00
|
|
|
private ArrayList<QueuedMedia> mediaQueued;
|
2017-01-16 19:15:42 +01:00
|
|
|
private CountUpDownLatch waitForMediaLatch;
|
2017-02-04 01:53:33 +01:00
|
|
|
private boolean showMarkSensitive;
|
|
|
|
private String statusVisibility; // The current values of the options that will be applied
|
|
|
|
private boolean statusMarkSensitive; // to the status being composed.
|
|
|
|
private boolean statusHideText; //
|
|
|
|
private View contentWarningBar;
|
2017-03-02 07:05:02 +01:00
|
|
|
private boolean statusAlreadyInFlight; // to prevent duplicate sends by mashing the send button
|
2017-03-04 02:44:44 +01:00
|
|
|
private InputContentInfoCompat currentInputContentInfo;
|
|
|
|
private int currentFlags;
|
|
|
|
private ProgressDialog finishingUploadDialog;
|
2017-03-07 14:09:33 +01:00
|
|
|
private EditText contentWarningEditor;
|
|
|
|
private boolean mediaPickEnabled;
|
2017-01-16 19:15:42 +01:00
|
|
|
|
|
|
|
private static class QueuedMedia {
|
2017-02-16 19:52:55 +01:00
|
|
|
enum Type {
|
2017-01-16 19:15:42 +01:00
|
|
|
IMAGE,
|
|
|
|
VIDEO
|
|
|
|
}
|
|
|
|
|
2017-02-16 19:52:55 +01:00
|
|
|
enum ReadyStage {
|
2017-01-16 19:15:42 +01:00
|
|
|
DOWNSIZING,
|
2017-02-17 23:56:31 +01:00
|
|
|
UPLOADING
|
2017-01-16 19:15:42 +01:00
|
|
|
}
|
|
|
|
|
2017-02-16 19:52:55 +01:00
|
|
|
Type type;
|
|
|
|
ImageView preview;
|
|
|
|
Uri uri;
|
|
|
|
String id;
|
2017-02-17 23:56:31 +01:00
|
|
|
Request uploadRequest;
|
2017-02-16 19:52:55 +01:00
|
|
|
ReadyStage readyStage;
|
|
|
|
byte[] content;
|
2017-02-17 23:56:31 +01:00
|
|
|
long mediaSize;
|
2017-01-16 19:15:42 +01:00
|
|
|
|
2017-02-17 23:56:31 +01:00
|
|
|
QueuedMedia(Type type, Uri uri, ImageView preview, long mediaSize) {
|
2017-01-16 19:15:42 +01:00
|
|
|
this.type = type;
|
|
|
|
this.uri = uri;
|
|
|
|
this.preview = preview;
|
2017-02-17 23:56:31 +01:00
|
|
|
this.mediaSize = mediaSize;
|
2017-01-16 19:15:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-17 23:56:31 +01:00
|
|
|
/**This saves enough information to re-enqueue an attachment when restoring the activity. */
|
|
|
|
private static class SavedQueuedMedia implements Parcelable {
|
|
|
|
QueuedMedia.Type type;
|
|
|
|
Uri uri;
|
|
|
|
Bitmap preview;
|
|
|
|
long mediaSize;
|
|
|
|
|
|
|
|
SavedQueuedMedia(QueuedMedia.Type type, Uri uri, ImageView view, long mediaSize) {
|
|
|
|
this.type = type;
|
|
|
|
this.uri = uri;
|
|
|
|
this.preview = ((BitmapDrawable) view.getDrawable()).getBitmap();
|
|
|
|
this.mediaSize = mediaSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
SavedQueuedMedia(Parcel parcel) {
|
|
|
|
type = (QueuedMedia.Type) parcel.readSerializable();
|
|
|
|
uri = parcel.readParcelable(Uri.class.getClassLoader());
|
|
|
|
preview = parcel.readParcelable(Bitmap.class.getClassLoader());
|
|
|
|
mediaSize = parcel.readLong();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int describeContents() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeToParcel(Parcel dest, int flags) {
|
|
|
|
dest.writeSerializable(type);
|
|
|
|
dest.writeParcelable(uri, flags);
|
|
|
|
dest.writeParcelable(preview, flags);
|
|
|
|
dest.writeLong(mediaSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final Parcelable.Creator<SavedQueuedMedia> CREATOR
|
|
|
|
= new Parcelable.Creator<SavedQueuedMedia>() {
|
|
|
|
public SavedQueuedMedia createFromParcel(Parcel in) {
|
|
|
|
return new SavedQueuedMedia(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
public SavedQueuedMedia[] newArray(int size) {
|
|
|
|
return new SavedQueuedMedia[size];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-02-22 20:13:51 +01:00
|
|
|
private void doErrorDialog(@StringRes int descriptionId, @StringRes int actionId,
|
|
|
|
View.OnClickListener listener) {
|
2017-01-16 19:15:42 +01:00
|
|
|
Snackbar bar = Snackbar.make(findViewById(R.id.activity_compose), getString(descriptionId),
|
|
|
|
Snackbar.LENGTH_SHORT);
|
|
|
|
bar.setAction(actionId, listener);
|
|
|
|
bar.show();
|
|
|
|
}
|
|
|
|
|
2017-02-22 20:13:51 +01:00
|
|
|
private void displayTransientError(@StringRes int stringId) {
|
2017-01-16 19:15:42 +01:00
|
|
|
Snackbar.make(findViewById(R.id.activity_compose), stringId, Snackbar.LENGTH_LONG).show();
|
|
|
|
}
|
2017-01-07 23:24:02 +01:00
|
|
|
|
2017-02-14 04:39:48 +01:00
|
|
|
private static class FindCharsResult {
|
2017-02-16 19:52:55 +01:00
|
|
|
int charIndex;
|
|
|
|
int stringIndex;
|
2017-02-14 04:39:48 +01:00
|
|
|
|
2017-02-16 19:52:55 +01:00
|
|
|
FindCharsResult() {
|
2017-02-14 04:39:48 +01:00
|
|
|
charIndex = -1;
|
|
|
|
stringIndex = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static FindCharsResult findChars(String string, int fromIndex, char[] chars) {
|
|
|
|
FindCharsResult result = new FindCharsResult();
|
|
|
|
final int length = string.length();
|
|
|
|
for (int i = fromIndex; i < length; i++) {
|
|
|
|
char c = string.charAt(i);
|
|
|
|
for (int j = 0; j < chars.length; j++) {
|
|
|
|
if (chars[j] == c) {
|
|
|
|
result.charIndex = j;
|
|
|
|
result.stringIndex = i;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static FindCharsResult findStart(String string, int fromIndex, char[] chars) {
|
2017-02-14 03:46:25 +01:00
|
|
|
final int length = string.length();
|
|
|
|
while (fromIndex < length) {
|
2017-02-14 04:39:48 +01:00
|
|
|
FindCharsResult found = findChars(string, fromIndex, chars);
|
|
|
|
int i = found.stringIndex;
|
|
|
|
if (i < 0) {
|
2017-02-14 03:46:25 +01:00
|
|
|
break;
|
2017-02-14 04:39:48 +01:00
|
|
|
} else if (i == 0 || i >= 1 && Character.isWhitespace(string.codePointBefore(i))) {
|
|
|
|
return found;
|
2017-02-14 03:46:25 +01:00
|
|
|
} else {
|
2017-02-14 04:39:48 +01:00
|
|
|
fromIndex = i + 1;
|
2017-02-14 03:46:25 +01:00
|
|
|
}
|
|
|
|
}
|
2017-02-14 04:39:48 +01:00
|
|
|
return new FindCharsResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int findEndOfHashtag(String string, int fromIndex) {
|
|
|
|
final int length = string.length();
|
|
|
|
for (int i = fromIndex + 1; i < length;) {
|
|
|
|
int codepoint = string.codePointAt(i);
|
|
|
|
if (Character.isWhitespace(codepoint)) {
|
|
|
|
return i;
|
|
|
|
} else if (codepoint == '#') {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
i += Character.charCount(codepoint);
|
|
|
|
}
|
|
|
|
return length;
|
2017-02-14 03:46:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static int findEndOfMention(String string, int fromIndex) {
|
|
|
|
int atCount = 0;
|
|
|
|
final int length = string.length();
|
2017-02-14 04:39:48 +01:00
|
|
|
for (int i = fromIndex + 1; i < length;) {
|
2017-02-14 03:46:25 +01:00
|
|
|
int codepoint = string.codePointAt(i);
|
|
|
|
if (Character.isWhitespace(codepoint)) {
|
|
|
|
return i;
|
|
|
|
} else if (codepoint == '@') {
|
|
|
|
atCount += 1;
|
2017-02-14 04:39:48 +01:00
|
|
|
if (atCount >= 2) {
|
2017-02-14 03:46:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i += Character.charCount(codepoint);
|
|
|
|
}
|
|
|
|
return length;
|
2017-01-07 23:24:02 +01:00
|
|
|
}
|
|
|
|
|
2017-02-14 04:39:48 +01:00
|
|
|
private static void highlightSpans(Spannable text, int colour) {
|
2017-01-20 05:59:21 +01:00
|
|
|
// Strip all existing colour spans.
|
|
|
|
int n = text.length();
|
|
|
|
ForegroundColorSpan[] oldSpans = text.getSpans(0, n, ForegroundColorSpan.class);
|
|
|
|
for (int i = oldSpans.length - 1; i >= 0; i--) {
|
|
|
|
text.removeSpan(oldSpans[i]);
|
2017-01-07 23:24:02 +01:00
|
|
|
}
|
2017-02-14 04:39:48 +01:00
|
|
|
// Colour the mentions and hashtags.
|
2017-02-14 03:46:25 +01:00
|
|
|
String string = text.toString();
|
|
|
|
int start;
|
|
|
|
int end = 0;
|
|
|
|
while (end < n) {
|
2017-02-14 04:39:48 +01:00
|
|
|
char[] chars = { '#', '@' };
|
|
|
|
FindCharsResult found = findStart(string, end, chars);
|
|
|
|
start = found.stringIndex;
|
|
|
|
if (start < 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (found.charIndex == 0) {
|
|
|
|
end = findEndOfHashtag(string, start);
|
|
|
|
} else if (found.charIndex == 1) {
|
|
|
|
end = findEndOfMention(string, start);
|
|
|
|
} else {
|
2017-02-14 03:46:25 +01:00
|
|
|
break;
|
2017-01-16 19:15:42 +01:00
|
|
|
}
|
2017-02-14 03:46:25 +01:00
|
|
|
if (end < 0) {
|
|
|
|
break;
|
2017-01-16 19:15:42 +01:00
|
|
|
}
|
2017-02-14 03:46:25 +01:00
|
|
|
text.setSpan(new ForegroundColorSpan(colour), start, end,
|
2017-01-20 05:59:21 +01:00
|
|
|
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
|
|
|
|
}
|
2017-01-16 19:15:42 +01:00
|
|
|
}
|
|
|
|
|
2017-01-07 23:24:02 +01:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_compose);
|
|
|
|
|
2017-03-07 14:09:33 +01:00
|
|
|
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
|
|
|
setSupportActionBar(toolbar);
|
|
|
|
|
|
|
|
ActionBar actionBar = getSupportActionBar();
|
|
|
|
|
|
|
|
if (actionBar != null) {
|
|
|
|
actionBar.setTitle(null);
|
|
|
|
actionBar.setDisplayHomeAsUpEnabled(true);
|
|
|
|
actionBar.setDisplayShowHomeEnabled(true);
|
|
|
|
}
|
|
|
|
|
2017-02-17 23:56:31 +01:00
|
|
|
SharedPreferences preferences = getSharedPreferences(
|
|
|
|
getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
|
|
|
|
|
2017-03-07 14:09:33 +01:00
|
|
|
mediaPickEnabled = true;
|
|
|
|
|
|
|
|
FloatingActionButton floatingBtn = (FloatingActionButton) findViewById(R.id.floating_btn);
|
|
|
|
floatingBtn.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
sendStatus();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-02-17 23:56:31 +01:00
|
|
|
ArrayList<SavedQueuedMedia> savedMediaQueued = null;
|
|
|
|
if (savedInstanceState != null) {
|
|
|
|
showMarkSensitive = savedInstanceState.getBoolean("showMarkSensitive");
|
|
|
|
statusVisibility = savedInstanceState.getString("statusVisibility");
|
|
|
|
statusMarkSensitive = savedInstanceState.getBoolean("statusMarkSensitive");
|
|
|
|
statusHideText = savedInstanceState.getBoolean("statusHideText");
|
|
|
|
// Keep these until everything needed to put them in the queue is finished initializing.
|
|
|
|
savedMediaQueued = savedInstanceState.getParcelableArrayList("savedMediaQueued");
|
2017-03-04 02:44:44 +01:00
|
|
|
// These are for restoring an in-progress commit content operation.
|
|
|
|
InputContentInfoCompat previousInputContentInfo = InputContentInfoCompat.wrap(
|
|
|
|
savedInstanceState.getParcelable("commitContentInputContentInfo"));
|
|
|
|
int previousFlags = savedInstanceState.getInt("commitContentFlags");
|
|
|
|
if (previousInputContentInfo != null) {
|
|
|
|
onCommitContentInternal(previousInputContentInfo, previousFlags);
|
|
|
|
}
|
2017-02-17 23:56:31 +01:00
|
|
|
} else {
|
|
|
|
showMarkSensitive = false;
|
|
|
|
statusVisibility = preferences.getString("rememberedVisibility", "public");
|
|
|
|
statusMarkSensitive = false;
|
|
|
|
statusHideText = false;
|
|
|
|
}
|
|
|
|
|
2017-01-20 05:59:21 +01:00
|
|
|
Intent intent = getIntent();
|
|
|
|
String[] mentionedUsernames = null;
|
|
|
|
if (intent != null) {
|
|
|
|
inReplyToId = intent.getStringExtra("in_reply_to_id");
|
2017-03-06 01:58:36 +01:00
|
|
|
String replyVisibility = intent.getStringExtra("reply_visibility");
|
|
|
|
if (replyVisibility != null) {
|
|
|
|
/* Override any remembered visibilty and instead adopt the visibility of the status
|
|
|
|
* to which this replies. */
|
|
|
|
statusVisibility = replyVisibility;
|
|
|
|
}
|
2017-01-20 05:59:21 +01:00
|
|
|
mentionedUsernames = intent.getStringArrayExtra("mentioned_usernames");
|
|
|
|
}
|
|
|
|
|
2017-01-07 23:24:02 +01:00
|
|
|
domain = preferences.getString("domain", null);
|
|
|
|
accessToken = preferences.getString("accessToken", null);
|
|
|
|
|
2017-03-04 02:44:44 +01:00
|
|
|
textEditor = createEditText(null); // new String[] { "image/gif", "image/webp" }
|
|
|
|
if (savedInstanceState != null) {
|
|
|
|
textEditor.onRestoreInstanceState(savedInstanceState.getParcelable("textEditorState"));
|
|
|
|
}
|
|
|
|
RelativeLayout editArea = (RelativeLayout) findViewById(R.id.compose_edit_area);
|
|
|
|
editArea.addView(textEditor);
|
2017-01-07 23:24:02 +01:00
|
|
|
final TextView charactersLeft = (TextView) findViewById(R.id.characters_left);
|
2017-02-17 03:11:05 +01:00
|
|
|
final int mentionColour = ThemeUtils.getColor(this, R.attr.compose_mention_color);
|
2017-01-07 23:24:02 +01:00
|
|
|
TextWatcher textEditorWatcher = new TextWatcher() {
|
|
|
|
@Override
|
|
|
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
|
|
|
int left = STATUS_CHARACTER_LIMIT - s.length();
|
2017-03-06 01:58:36 +01:00
|
|
|
charactersLeft.setText(String.format(Locale.getDefault(), "%d", left));
|
2017-01-07 23:24:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
|
|
|
|
|
|
|
@Override
|
2017-01-20 05:59:21 +01:00
|
|
|
public void afterTextChanged(Editable editable) {
|
2017-02-14 04:39:48 +01:00
|
|
|
highlightSpans(editable, mentionColour);
|
2017-01-20 05:59:21 +01:00
|
|
|
}
|
2017-01-07 23:24:02 +01:00
|
|
|
};
|
|
|
|
textEditor.addTextChangedListener(textEditorWatcher);
|
|
|
|
|
2017-01-20 05:59:21 +01:00
|
|
|
if (mentionedUsernames != null) {
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
for (String name : mentionedUsernames) {
|
|
|
|
builder.append('@');
|
|
|
|
builder.append(name);
|
|
|
|
builder.append(' ');
|
|
|
|
}
|
|
|
|
textEditor.setText(builder);
|
|
|
|
textEditor.setSelection(textEditor.length());
|
|
|
|
}
|
|
|
|
|
2017-01-16 19:15:42 +01:00
|
|
|
mediaPreviewBar = (LinearLayout) findViewById(R.id.compose_media_preview_bar);
|
|
|
|
mediaQueued = new ArrayList<>();
|
|
|
|
waitForMediaLatch = new CountUpDownLatch();
|
|
|
|
|
2017-02-04 01:53:33 +01:00
|
|
|
contentWarningBar = findViewById(R.id.compose_content_warning_bar);
|
2017-03-07 14:09:33 +01:00
|
|
|
contentWarningEditor = (EditText) findViewById(R.id.field_content_warning);
|
2017-02-04 01:53:33 +01:00
|
|
|
showContentWarning(false);
|
|
|
|
|
2017-03-02 07:05:02 +01:00
|
|
|
statusAlreadyInFlight = false;
|
2017-01-16 19:15:42 +01:00
|
|
|
|
2017-03-07 14:09:33 +01:00
|
|
|
// These can only be added after everything affected by the media queue is initialized.
|
|
|
|
if (savedMediaQueued != null) {
|
|
|
|
for (SavedQueuedMedia item : savedMediaQueued) {
|
|
|
|
addMediaToQueue(item.type, item.preview, item.uri, item.mediaSize);
|
2017-01-16 19:15:42 +01:00
|
|
|
}
|
2017-03-07 14:09:33 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-16 19:15:42 +01:00
|
|
|
|
2017-03-07 14:09:33 +01:00
|
|
|
private void showComposeOptions() {
|
|
|
|
ComposeOptionsFragment fragment = ComposeOptionsFragment.newInstance(
|
|
|
|
statusVisibility, statusMarkSensitive, statusHideText,
|
|
|
|
showMarkSensitive, inReplyToId != null,
|
|
|
|
new ComposeOptionsFragment.Listener() {
|
|
|
|
@Override
|
|
|
|
public int describeContents() {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-01-20 05:59:21 +01:00
|
|
|
|
2017-03-07 14:09:33 +01:00
|
|
|
@Override
|
|
|
|
public void writeToParcel(Parcel dest, int flags) {}
|
2017-02-04 01:53:33 +01:00
|
|
|
|
2017-03-07 14:09:33 +01:00
|
|
|
@Override
|
|
|
|
public void onVisibilityChanged(String visibility) {
|
|
|
|
statusVisibility = visibility;
|
|
|
|
}
|
2017-02-04 01:53:33 +01:00
|
|
|
|
2017-03-07 14:09:33 +01:00
|
|
|
@Override
|
|
|
|
public void onMarkSensitiveChanged(boolean markSensitive) {
|
|
|
|
statusMarkSensitive = markSensitive;
|
|
|
|
}
|
2017-02-04 01:53:33 +01:00
|
|
|
|
2017-03-07 14:09:33 +01:00
|
|
|
@Override
|
|
|
|
public void onContentWarningChanged(boolean hideText) {
|
|
|
|
showContentWarning(hideText);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
fragment.show(getSupportFragmentManager(), null);
|
|
|
|
}
|
2017-02-17 23:56:31 +01:00
|
|
|
|
2017-03-07 14:09:33 +01:00
|
|
|
private void sendStatus() {
|
|
|
|
if (statusAlreadyInFlight) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Editable editable = textEditor.getText();
|
|
|
|
if (editable.length() <= STATUS_CHARACTER_LIMIT) {
|
|
|
|
statusAlreadyInFlight = true;
|
|
|
|
String spoilerText = "";
|
|
|
|
if (statusHideText) {
|
|
|
|
spoilerText = contentWarningEditor.getText().toString();
|
2017-02-17 23:56:31 +01:00
|
|
|
}
|
2017-03-07 14:09:33 +01:00
|
|
|
readyStatus(editable.toString(), statusVisibility, statusMarkSensitive,
|
|
|
|
spoilerText);
|
|
|
|
} else {
|
|
|
|
textEditor.setError(getString(R.string.error_compose_character_limit));
|
2017-02-17 23:56:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onSaveInstanceState(Bundle outState) {
|
|
|
|
ArrayList<SavedQueuedMedia> savedMediaQueued = new ArrayList<>();
|
|
|
|
for (QueuedMedia item : mediaQueued) {
|
|
|
|
savedMediaQueued.add(new SavedQueuedMedia(item.type, item.uri, item.preview,
|
|
|
|
item.mediaSize));
|
|
|
|
}
|
|
|
|
outState.putParcelableArrayList("savedMediaQueued", savedMediaQueued);
|
|
|
|
outState.putBoolean("showMarkSensitive", showMarkSensitive);
|
|
|
|
outState.putString("statusVisibility", statusVisibility);
|
|
|
|
outState.putBoolean("statusMarkSensitive", statusMarkSensitive);
|
|
|
|
outState.putBoolean("statusHideText", statusHideText);
|
2017-03-04 02:44:44 +01:00
|
|
|
outState.putParcelable("textEditorState", textEditor.onSaveInstanceState());
|
|
|
|
if (currentInputContentInfo != null) {
|
|
|
|
outState.putParcelable("commitContentInputContentInfo",
|
|
|
|
(Parcelable) currentInputContentInfo.unwrap());
|
|
|
|
outState.putInt("commitContentFlags", currentFlags);
|
|
|
|
}
|
|
|
|
currentInputContentInfo = null;
|
|
|
|
currentFlags = 0;
|
2017-02-17 23:56:31 +01:00
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPause() {
|
|
|
|
super.onPause();
|
2017-03-06 01:58:36 +01:00
|
|
|
if (inReplyToId != null) {
|
|
|
|
/* Don't save the visibility setting for replies because they adopt the visibility of
|
|
|
|
* the status they reply to and that behaviour needs to be kept separate. */
|
|
|
|
return;
|
|
|
|
}
|
2017-02-17 23:56:31 +01:00
|
|
|
SharedPreferences preferences = getSharedPreferences(
|
|
|
|
getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
|
|
|
|
SharedPreferences.Editor editor = preferences.edit();
|
|
|
|
editor.putString("rememberedVisibility", statusVisibility);
|
|
|
|
editor.apply();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
VolleySingleton.getInstance(this).cancelAll(TAG);
|
2017-01-20 05:59:21 +01:00
|
|
|
}
|
|
|
|
|
2017-03-04 02:44:44 +01:00
|
|
|
private EditText createEditText(String[] contentMimeTypes) {
|
|
|
|
final String[] mimeTypes;
|
|
|
|
if (contentMimeTypes == null || contentMimeTypes.length == 0) {
|
|
|
|
mimeTypes = new String[0];
|
|
|
|
} else {
|
|
|
|
mimeTypes = Arrays.copyOf(contentMimeTypes, contentMimeTypes.length);
|
|
|
|
}
|
2017-03-07 14:09:33 +01:00
|
|
|
EditText editText = new android.support.v7.widget.AppCompatEditText(this) {
|
2017-03-04 02:44:44 +01:00
|
|
|
@Override
|
|
|
|
public InputConnection onCreateInputConnection(EditorInfo editorInfo) {
|
|
|
|
final InputConnection ic = super.onCreateInputConnection(editorInfo);
|
|
|
|
EditorInfoCompat.setContentMimeTypes(editorInfo, mimeTypes);
|
|
|
|
final InputConnectionCompat.OnCommitContentListener callback =
|
|
|
|
new InputConnectionCompat.OnCommitContentListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onCommitContent(InputContentInfoCompat inputContentInfo,
|
|
|
|
int flags, Bundle opts) {
|
|
|
|
return ComposeActivity.this.onCommitContent(inputContentInfo, flags,
|
|
|
|
mimeTypes);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return InputConnectionCompat.createWrapper(ic, editorInfo, callback);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
|
|
|
|
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
|
|
|
|
editText.setLayoutParams(layoutParams);
|
|
|
|
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
|
|
|
|
editText.setEms(10);
|
|
|
|
editText.setGravity(Gravity.START | Gravity.TOP);
|
|
|
|
editText.setHint(R.string.hint_compose);
|
|
|
|
return editText;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean onCommitContent(InputContentInfoCompat inputContentInfo, int flags,
|
|
|
|
String[] mimeTypes) {
|
|
|
|
try {
|
|
|
|
if (currentInputContentInfo != null) {
|
|
|
|
currentInputContentInfo.releasePermission();
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(TAG, "InputContentInfoCompat#releasePermission() failed." + e.getMessage());
|
|
|
|
} finally {
|
|
|
|
currentInputContentInfo = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the returned content's type is actually in the list of MIME types requested.
|
|
|
|
boolean supported = false;
|
|
|
|
for (final String mimeType : mimeTypes) {
|
|
|
|
if (inputContentInfo.getDescription().hasMimeType(mimeType)) {
|
|
|
|
supported = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return supported && onCommitContentInternal(inputContentInfo, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean onCommitContentInternal(InputContentInfoCompat inputContentInfo, int flags) {
|
|
|
|
if ((flags & InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0) {
|
|
|
|
try {
|
|
|
|
inputContentInfo.requestPermission();
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(TAG, "InputContentInfoCompat#requestPermission() failed." + e.getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the file size before putting handing it off to be put in the queue.
|
|
|
|
Uri uri = inputContentInfo.getContentUri();
|
|
|
|
long mediaSize;
|
|
|
|
AssetFileDescriptor descriptor = null;
|
|
|
|
try {
|
|
|
|
descriptor = getContentResolver().openAssetFileDescriptor(uri, "r");
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
// Eat this exception, having the descriptor be null is sufficient.
|
|
|
|
}
|
|
|
|
if (descriptor != null) {
|
|
|
|
mediaSize = descriptor.getLength();
|
|
|
|
try {
|
|
|
|
descriptor.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
// Just eat this exception.
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mediaSize = MEDIA_SIZE_UNKNOWN;
|
|
|
|
}
|
|
|
|
pickMedia(uri, mediaSize);
|
|
|
|
|
|
|
|
currentInputContentInfo = inputContentInfo;
|
|
|
|
currentFlags = flags;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-04 01:53:33 +01:00
|
|
|
private void sendStatus(String content, String visibility, boolean sensitive,
|
2017-02-17 23:56:31 +01:00
|
|
|
String spoilerText) {
|
2017-01-20 05:59:21 +01:00
|
|
|
String endpoint = getString(R.string.endpoint_status);
|
|
|
|
String url = "https://" + domain + endpoint;
|
|
|
|
JSONObject parameters = new JSONObject();
|
|
|
|
try {
|
|
|
|
parameters.put("status", content);
|
|
|
|
parameters.put("visibility", visibility);
|
|
|
|
parameters.put("sensitive", sensitive);
|
2017-02-04 01:53:33 +01:00
|
|
|
parameters.put("spoiler_text", spoilerText);
|
2017-01-20 05:59:21 +01:00
|
|
|
if (inReplyToId != null) {
|
|
|
|
parameters.put("in_reply_to_id", inReplyToId);
|
|
|
|
}
|
2017-02-17 23:56:31 +01:00
|
|
|
JSONArray mediaIds = new JSONArray();
|
2017-01-20 05:59:21 +01:00
|
|
|
for (QueuedMedia item : mediaQueued) {
|
2017-02-17 23:56:31 +01:00
|
|
|
mediaIds.put(item.id);
|
2017-01-20 05:59:21 +01:00
|
|
|
}
|
2017-02-17 23:56:31 +01:00
|
|
|
if (mediaIds.length() > 0) {
|
|
|
|
parameters.put("media_ids", mediaIds);
|
2017-01-20 05:59:21 +01:00
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
2017-02-04 01:53:33 +01:00
|
|
|
onSendFailure();
|
2017-01-20 05:59:21 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, parameters,
|
|
|
|
new Response.Listener<JSONObject>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(JSONObject response) {
|
|
|
|
onSendSuccess();
|
|
|
|
}
|
2017-02-16 19:52:55 +01:00
|
|
|
},
|
|
|
|
new Response.ErrorListener() {
|
|
|
|
@Override
|
|
|
|
public void onErrorResponse(VolleyError error) {
|
|
|
|
onSendFailure();
|
|
|
|
}
|
|
|
|
}) {
|
2017-01-20 05:59:21 +01:00
|
|
|
@Override
|
|
|
|
public Map<String, String> getHeaders() throws AuthFailureError {
|
|
|
|
Map<String, String> headers = new HashMap<>();
|
|
|
|
headers.put("Authorization", "Bearer " + accessToken);
|
|
|
|
return headers;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
VolleySingleton.getInstance(this).addToRequestQueue(request);
|
|
|
|
}
|
|
|
|
|
2017-02-04 01:53:33 +01:00
|
|
|
private void onSendSuccess() {
|
|
|
|
Toast.makeText(this, getString(R.string.confirmation_send), Toast.LENGTH_SHORT).show();
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onSendFailure() {
|
|
|
|
textEditor.setError(getString(R.string.error_sending_status));
|
2017-03-02 07:05:02 +01:00
|
|
|
statusAlreadyInFlight = false;
|
2017-02-04 01:53:33 +01:00
|
|
|
}
|
|
|
|
|
2017-02-16 19:52:55 +01:00
|
|
|
private void readyStatus(final String content, final String visibility, final boolean sensitive,
|
|
|
|
final String spoilerText) {
|
2017-03-04 02:44:44 +01:00
|
|
|
finishingUploadDialog = ProgressDialog.show(
|
2017-02-07 08:05:50 +01:00
|
|
|
this, getString(R.string.dialog_title_finishing_media_upload),
|
|
|
|
getString(R.string.dialog_message_uploading_media), true, true);
|
2017-01-20 05:59:21 +01:00
|
|
|
final AsyncTask<Void, Void, Boolean> waitForMediaTask =
|
|
|
|
new AsyncTask<Void, Void, Boolean>() {
|
|
|
|
@Override
|
|
|
|
protected Boolean doInBackground(Void... params) {
|
|
|
|
try {
|
|
|
|
waitForMediaLatch.await();
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Boolean successful) {
|
|
|
|
super.onPostExecute(successful);
|
2017-03-04 02:44:44 +01:00
|
|
|
finishingUploadDialog.dismiss();
|
|
|
|
finishingUploadDialog = null;
|
2017-01-20 05:59:21 +01:00
|
|
|
if (successful) {
|
2017-02-04 01:53:33 +01:00
|
|
|
sendStatus(content, visibility, sensitive, spoilerText);
|
2017-01-20 05:59:21 +01:00
|
|
|
} else {
|
2017-02-04 01:53:33 +01:00
|
|
|
onReadyFailure(content, visibility, sensitive, spoilerText);
|
2017-01-20 05:59:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCancelled() {
|
|
|
|
removeAllMediaFromQueue();
|
2017-03-02 07:05:02 +01:00
|
|
|
statusAlreadyInFlight = false;
|
2017-01-20 05:59:21 +01:00
|
|
|
super.onCancelled();
|
|
|
|
}
|
|
|
|
};
|
2017-03-04 02:44:44 +01:00
|
|
|
finishingUploadDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
|
2017-01-20 05:59:21 +01:00
|
|
|
@Override
|
|
|
|
public void onCancel(DialogInterface dialog) {
|
|
|
|
/* Generating an interrupt by passing true here is important because an interrupt
|
|
|
|
* exception is the only thing that will kick the latch out of its waiting loop
|
|
|
|
* early. */
|
|
|
|
waitForMediaTask.cancel(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
waitForMediaTask.execute();
|
|
|
|
}
|
|
|
|
|
2017-02-04 01:53:33 +01:00
|
|
|
private void onReadyFailure(final String content, final String visibility,
|
|
|
|
final boolean sensitive, final String spoilerText) {
|
2017-01-20 05:59:21 +01:00
|
|
|
doErrorDialog(R.string.error_media_upload_sending, R.string.action_retry,
|
|
|
|
new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2017-02-04 01:53:33 +01:00
|
|
|
readyStatus(content, visibility, sensitive, spoilerText);
|
2017-01-20 05:59:21 +01:00
|
|
|
}
|
|
|
|
});
|
2017-03-02 07:05:02 +01:00
|
|
|
statusAlreadyInFlight = false;
|
2017-01-20 05:59:21 +01:00
|
|
|
}
|
|
|
|
|
2017-01-16 19:15:42 +01:00
|
|
|
private void onMediaPick() {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN &&
|
|
|
|
ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
|
|
|
|
!= PackageManager.PERMISSION_GRANTED) {
|
|
|
|
ActivityCompat.requestPermissions(this,
|
|
|
|
new String[] { Manifest.permission.READ_EXTERNAL_STORAGE },
|
|
|
|
PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
|
|
|
|
} else {
|
|
|
|
initiateMediaPicking();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[],
|
|
|
|
@NonNull int[] grantResults) {
|
|
|
|
switch (requestCode) {
|
|
|
|
case PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: {
|
|
|
|
if (grantResults.length > 0
|
|
|
|
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
initiateMediaPicking();
|
|
|
|
} else {
|
|
|
|
doErrorDialog(R.string.error_media_upload_permission, R.string.action_retry,
|
|
|
|
new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
onMediaPick();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initiateMediaPicking() {
|
|
|
|
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
|
|
|
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
|
|
|
|
intent.setType("image/* video/*");
|
|
|
|
} else {
|
|
|
|
String[] mimeTypes = new String[] { "image/*", "video/*" };
|
|
|
|
intent.setType("*/*");
|
|
|
|
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
|
|
|
|
}
|
|
|
|
startActivityForResult(intent, MEDIA_PICK_RESULT);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void enableMediaPicking() {
|
2017-03-07 14:09:33 +01:00
|
|
|
mediaPickEnabled = true;
|
|
|
|
invalidateOptionsMenu();
|
2017-01-16 19:15:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void disableMediaPicking() {
|
2017-03-07 14:09:33 +01:00
|
|
|
mediaPickEnabled = false;
|
|
|
|
invalidateOptionsMenu();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPrepareOptionsMenu(Menu menu) {
|
|
|
|
if (mediaPickEnabled) {
|
|
|
|
menu.findItem(R.id.compose_photo_pick).setEnabled(true);
|
|
|
|
} else {
|
|
|
|
menu.findItem(R.id.compose_photo_pick).setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.onPrepareOptionsMenu(menu);
|
2017-01-16 19:15:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void addMediaToQueue(QueuedMedia.Type type, Bitmap preview, Uri uri, long mediaSize) {
|
2017-02-17 23:56:31 +01:00
|
|
|
final QueuedMedia item = new QueuedMedia(type, uri, new ImageView(this), mediaSize);
|
2017-02-16 19:52:55 +01:00
|
|
|
ImageView view = item.preview;
|
2017-01-16 19:15:42 +01:00
|
|
|
Resources resources = getResources();
|
|
|
|
int side = resources.getDimensionPixelSize(R.dimen.compose_media_preview_side);
|
|
|
|
int margin = resources.getDimensionPixelSize(R.dimen.compose_media_preview_margin);
|
|
|
|
int marginBottom = resources.getDimensionPixelSize(
|
|
|
|
R.dimen.compose_media_preview_margin_bottom);
|
|
|
|
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(side, side);
|
|
|
|
layoutParams.setMargins(margin, margin, margin, marginBottom);
|
|
|
|
view.setLayoutParams(layoutParams);
|
|
|
|
view.setImageBitmap(preview);
|
|
|
|
view.setScaleType(ImageView.ScaleType.CENTER_CROP);
|
|
|
|
view.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
removeMediaFromQueue(item);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
mediaPreviewBar.addView(view);
|
|
|
|
mediaQueued.add(item);
|
|
|
|
int queuedCount = mediaQueued.size();
|
|
|
|
if (queuedCount == 1) {
|
|
|
|
/* The media preview bar is actually not inset in the EditText, it just overlays it and
|
|
|
|
* is aligned to the bottom. But, so that text doesn't get hidden under it, extra
|
|
|
|
* padding is added at the bottom of the EditText. */
|
|
|
|
int totalHeight = side + margin + marginBottom;
|
2017-02-07 22:05:55 +01:00
|
|
|
textEditor.setPadding(textEditor.getPaddingLeft(), textEditor.getPaddingTop(),
|
|
|
|
textEditor.getPaddingRight(), totalHeight);
|
2017-01-16 19:15:42 +01:00
|
|
|
// If there's one video in the queue it is full, so disable the button to queue more.
|
2017-02-16 19:52:55 +01:00
|
|
|
if (item.type == QueuedMedia.Type.VIDEO) {
|
2017-01-16 19:15:42 +01:00
|
|
|
disableMediaPicking();
|
|
|
|
}
|
|
|
|
} else if (queuedCount >= Status.MAX_MEDIA_ATTACHMENTS) {
|
|
|
|
// Limit the total media attachments, also.
|
|
|
|
disableMediaPicking();
|
|
|
|
}
|
|
|
|
if (queuedCount >= 1) {
|
2017-02-04 01:53:33 +01:00
|
|
|
showMarkSensitive(true);
|
2017-01-16 19:15:42 +01:00
|
|
|
}
|
|
|
|
waitForMediaLatch.countUp();
|
|
|
|
if (mediaSize > STATUS_MEDIA_SIZE_LIMIT && type == QueuedMedia.Type.IMAGE) {
|
|
|
|
downsizeMedia(item);
|
|
|
|
} else {
|
|
|
|
uploadMedia(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void removeMediaFromQueue(QueuedMedia item) {
|
2017-02-16 19:52:55 +01:00
|
|
|
mediaPreviewBar.removeView(item.preview);
|
2017-01-16 19:15:42 +01:00
|
|
|
mediaQueued.remove(item);
|
|
|
|
if (mediaQueued.size() == 0) {
|
2017-02-04 01:53:33 +01:00
|
|
|
showMarkSensitive(false);
|
2017-01-16 19:15:42 +01:00
|
|
|
/* If there are no image previews to show, the extra padding that was added to the
|
|
|
|
* EditText can be removed so there isn't unnecessary empty space. */
|
2017-02-07 22:05:55 +01:00
|
|
|
textEditor.setPadding(textEditor.getPaddingLeft(), textEditor.getPaddingTop(),
|
|
|
|
textEditor.getPaddingRight(), 0);
|
2017-01-16 19:15:42 +01:00
|
|
|
}
|
|
|
|
enableMediaPicking();
|
|
|
|
cancelReadyingMedia(item);
|
|
|
|
}
|
|
|
|
|
2017-01-18 19:35:07 +01:00
|
|
|
private void removeAllMediaFromQueue() {
|
|
|
|
for (Iterator<QueuedMedia> it = mediaQueued.iterator(); it.hasNext();) {
|
|
|
|
QueuedMedia item = it.next();
|
|
|
|
it.remove();
|
|
|
|
removeMediaFromQueue(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-16 19:15:42 +01:00
|
|
|
private void downsizeMedia(final QueuedMedia item) {
|
2017-02-16 19:52:55 +01:00
|
|
|
item.readyStage = QueuedMedia.ReadyStage.DOWNSIZING;
|
2017-01-20 05:59:21 +01:00
|
|
|
InputStream stream;
|
2017-01-16 19:15:42 +01:00
|
|
|
try {
|
2017-02-16 19:52:55 +01:00
|
|
|
stream = getContentResolver().openInputStream(item.uri);
|
2017-01-16 19:15:42 +01:00
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
onMediaDownsizeFailure(item);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Bitmap bitmap = BitmapFactory.decodeStream(stream);
|
2017-01-18 19:35:07 +01:00
|
|
|
IOUtils.closeQuietly(stream);
|
2017-01-16 19:15:42 +01:00
|
|
|
new DownsizeImageTask(STATUS_MEDIA_SIZE_LIMIT, new DownsizeImageTask.Listener() {
|
|
|
|
@Override
|
|
|
|
public void onSuccess(List<byte[]> contentList) {
|
2017-02-16 19:52:55 +01:00
|
|
|
item.content = contentList.get(0);
|
2017-01-16 19:15:42 +01:00
|
|
|
uploadMedia(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure() {
|
|
|
|
onMediaDownsizeFailure(item);
|
|
|
|
}
|
|
|
|
}).execute(bitmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onMediaDownsizeFailure(QueuedMedia item) {
|
|
|
|
displayTransientError(R.string.error_media_upload_size);
|
|
|
|
removeMediaFromQueue(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String randomAlphanumericString(int count) {
|
|
|
|
char[] chars = new char[count];
|
|
|
|
Random random = new Random();
|
|
|
|
final String POSSIBLE_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
chars[i] = POSSIBLE_CHARS.charAt(random.nextInt(POSSIBLE_CHARS.length()));
|
|
|
|
}
|
|
|
|
return new String(chars);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
private static byte[] inputStreamGetBytes(InputStream stream) {
|
|
|
|
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
|
|
|
int read;
|
|
|
|
byte[] data = new byte[16384];
|
|
|
|
try {
|
|
|
|
while ((read = stream.read(data, 0, data.length)) != -1) {
|
|
|
|
buffer.write(data, 0, read);
|
|
|
|
}
|
|
|
|
buffer.flush();
|
|
|
|
} catch (IOException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return buffer.toByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void uploadMedia(final QueuedMedia item) {
|
2017-02-16 19:52:55 +01:00
|
|
|
item.readyStage = QueuedMedia.ReadyStage.UPLOADING;
|
2017-01-16 19:15:42 +01:00
|
|
|
|
|
|
|
String endpoint = getString(R.string.endpoint_media);
|
|
|
|
String url = "https://" + domain + endpoint;
|
|
|
|
|
|
|
|
final String mimeType = getContentResolver().getType(item.uri);
|
|
|
|
MimeTypeMap map = MimeTypeMap.getSingleton();
|
|
|
|
String fileExtension = map.getExtensionFromMimeType(mimeType);
|
|
|
|
final String filename = String.format("%s_%s_%s.%s",
|
|
|
|
getString(R.string.app_name),
|
|
|
|
String.valueOf(new Date().getTime()),
|
|
|
|
randomAlphanumericString(10),
|
|
|
|
fileExtension);
|
|
|
|
|
|
|
|
MultipartRequest request = new MultipartRequest(Request.Method.POST, url, null,
|
|
|
|
new Response.Listener<JSONObject>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(JSONObject response) {
|
|
|
|
try {
|
2017-02-16 19:52:55 +01:00
|
|
|
item.id = response.getString("id");
|
2017-01-16 19:15:42 +01:00
|
|
|
} catch (JSONException e) {
|
2017-02-04 01:53:33 +01:00
|
|
|
onUploadFailure(item);
|
2017-01-16 19:15:42 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
waitForMediaLatch.countDown();
|
|
|
|
}
|
|
|
|
}, new Response.ErrorListener() {
|
|
|
|
@Override
|
|
|
|
public void onErrorResponse(VolleyError error) {
|
2017-02-04 01:53:33 +01:00
|
|
|
onUploadFailure(item);
|
2017-01-16 19:15:42 +01:00
|
|
|
}
|
|
|
|
}) {
|
|
|
|
@Override
|
|
|
|
public Map<String, String> getHeaders() throws AuthFailureError {
|
|
|
|
Map<String, String> headers = new HashMap<>();
|
|
|
|
headers.put("Authorization", "Bearer " + accessToken);
|
|
|
|
return headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public DataItem getData() {
|
2017-02-16 19:52:55 +01:00
|
|
|
byte[] content = item.content;
|
2017-01-16 19:15:42 +01:00
|
|
|
if (content == null) {
|
2017-01-20 05:59:21 +01:00
|
|
|
InputStream stream;
|
2017-01-16 19:15:42 +01:00
|
|
|
try {
|
2017-02-16 19:52:55 +01:00
|
|
|
stream = getContentResolver().openInputStream(item.uri);
|
2017-01-16 19:15:42 +01:00
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
content = inputStreamGetBytes(stream);
|
2017-01-18 19:35:07 +01:00
|
|
|
IOUtils.closeQuietly(stream);
|
2017-01-16 19:15:42 +01:00
|
|
|
if (content == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DataItem data = new DataItem();
|
|
|
|
data.name = "file";
|
|
|
|
data.filename = filename;
|
|
|
|
data.mimeType = mimeType;
|
|
|
|
data.content = content;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
};
|
2017-02-17 23:56:31 +01:00
|
|
|
request.setTag(TAG);
|
|
|
|
item.uploadRequest = request;
|
2017-01-16 19:15:42 +01:00
|
|
|
VolleySingleton.getInstance(this).addToRequestQueue(request);
|
|
|
|
}
|
|
|
|
|
2017-02-04 01:53:33 +01:00
|
|
|
private void onUploadFailure(QueuedMedia item) {
|
2017-01-16 19:15:42 +01:00
|
|
|
displayTransientError(R.string.error_media_upload_sending);
|
2017-03-04 02:44:44 +01:00
|
|
|
if (finishingUploadDialog != null) {
|
|
|
|
finishingUploadDialog.cancel();
|
|
|
|
}
|
2017-01-16 19:15:42 +01:00
|
|
|
removeMediaFromQueue(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void cancelReadyingMedia(QueuedMedia item) {
|
2017-02-16 19:52:55 +01:00
|
|
|
if (item.readyStage == QueuedMedia.ReadyStage.UPLOADING) {
|
2017-02-17 23:56:31 +01:00
|
|
|
item.uploadRequest.cancel();
|
|
|
|
}
|
|
|
|
if (item.id == null) {
|
|
|
|
/* The presence of an upload id is used to detect if it finished uploading or not, to
|
|
|
|
* prevent counting down twice on the same media item. */
|
|
|
|
waitForMediaLatch.countDown();
|
2017-01-16 19:15:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
if (requestCode == MEDIA_PICK_RESULT && resultCode == RESULT_OK && data != null) {
|
|
|
|
Uri uri = data.getData();
|
2017-03-04 02:44:44 +01:00
|
|
|
long mediaSize;
|
2017-01-16 19:15:42 +01:00
|
|
|
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
|
2017-03-04 02:44:44 +01:00
|
|
|
if (cursor != null) {
|
|
|
|
int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
|
|
|
|
cursor.moveToFirst();
|
|
|
|
mediaSize = cursor.getLong(sizeIndex);
|
|
|
|
cursor.close();
|
|
|
|
} else {
|
|
|
|
mediaSize = MEDIA_SIZE_UNKNOWN;
|
|
|
|
}
|
|
|
|
pickMedia(uri, mediaSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void pickMedia(Uri uri, long mediaSize) {
|
|
|
|
ContentResolver contentResolver = getContentResolver();
|
|
|
|
if (mediaSize == MEDIA_SIZE_UNKNOWN) {
|
|
|
|
displayTransientError(R.string.error_media_upload_opening);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
String mimeType = contentResolver.getType(uri);
|
|
|
|
if (mimeType != null) {
|
|
|
|
String topLevelType = mimeType.substring(0, mimeType.indexOf('/'));
|
|
|
|
switch (topLevelType) {
|
|
|
|
case "video": {
|
|
|
|
if (mediaSize > STATUS_MEDIA_SIZE_LIMIT) {
|
|
|
|
displayTransientError(R.string.error_media_upload_size);
|
|
|
|
return;
|
2017-01-16 19:15:42 +01:00
|
|
|
}
|
2017-03-04 02:44:44 +01:00
|
|
|
if (mediaQueued.size() > 0
|
|
|
|
&& mediaQueued.get(0).type == QueuedMedia.Type.IMAGE) {
|
|
|
|
displayTransientError(R.string.error_media_upload_image_or_video);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
|
|
|
|
retriever.setDataSource(this, uri);
|
|
|
|
Bitmap source = retriever.getFrameAtTime();
|
|
|
|
Bitmap bitmap = ThumbnailUtils.extractThumbnail(source, 96, 96);
|
|
|
|
source.recycle();
|
|
|
|
addMediaToQueue(QueuedMedia.Type.VIDEO, bitmap, uri, mediaSize);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "image": {
|
|
|
|
InputStream stream;
|
|
|
|
try {
|
|
|
|
stream = contentResolver.openInputStream(uri);
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
displayTransientError(R.string.error_media_upload_opening);
|
|
|
|
return;
|
2017-01-16 19:15:42 +01:00
|
|
|
}
|
2017-03-04 02:44:44 +01:00
|
|
|
Bitmap source = BitmapFactory.decodeStream(stream);
|
|
|
|
Bitmap bitmap = ThumbnailUtils.extractThumbnail(source, 96, 96);
|
|
|
|
source.recycle();
|
|
|
|
try {
|
|
|
|
if (stream != null) {
|
|
|
|
stream.close();
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
bitmap.recycle();
|
|
|
|
displayTransientError(R.string.error_media_upload_opening);
|
|
|
|
return;
|
2017-01-16 19:15:42 +01:00
|
|
|
}
|
2017-03-04 02:44:44 +01:00
|
|
|
addMediaToQueue(QueuedMedia.Type.IMAGE, bitmap, uri, mediaSize);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
displayTransientError(R.string.error_media_upload_type);
|
|
|
|
break;
|
2017-01-16 19:15:42 +01:00
|
|
|
}
|
|
|
|
}
|
2017-03-04 02:44:44 +01:00
|
|
|
} else {
|
|
|
|
displayTransientError(R.string.error_media_upload_type);
|
2017-01-16 19:15:42 +01:00
|
|
|
}
|
2017-01-07 23:24:02 +01:00
|
|
|
}
|
2017-02-04 01:53:33 +01:00
|
|
|
|
|
|
|
void showMarkSensitive(boolean show) {
|
|
|
|
showMarkSensitive = show;
|
|
|
|
if(!showMarkSensitive) {
|
|
|
|
statusMarkSensitive = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void showContentWarning(boolean show) {
|
|
|
|
statusHideText = show;
|
|
|
|
if (show) {
|
|
|
|
contentWarningBar.setVisibility(View.VISIBLE);
|
|
|
|
} else {
|
|
|
|
contentWarningBar.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
}
|
2017-03-07 14:09:33 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
getMenuInflater().inflate(R.menu.compose_toolbar, menu);
|
|
|
|
return super.onCreateOptionsMenu(menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home: {
|
|
|
|
onBackPressed();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case R.id.compose_photo_pick: {
|
|
|
|
onMediaPick();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case R.id.compose_options: {
|
|
|
|
showComposeOptions();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
2017-01-07 23:24:02 +01:00
|
|
|
}
|