Allow taking photos directly from the app (closes #139)

This commit is contained in:
Raphael Michel 2017-04-15 17:43:26 +02:00
parent a33c259760
commit 15088fa42a
6 changed files with 118 additions and 8 deletions

View File

@ -6,6 +6,8 @@
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
<application <application
android:allowBackup="true" android:allowBackup="true"
@ -89,7 +91,17 @@
<intent-filter> <intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE"/> <action android:name="android.service.quicksettings.action.QS_TILE"/>
</intent-filter> </intent-filter>
</service> </service>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.keylesspalace.tusky.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
</application> </application>
</manifest> </manifest>

View File

@ -36,8 +36,10 @@ import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import android.provider.MediaStore;
import android.provider.OpenableColumns; import android.provider.OpenableColumns;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
@ -48,6 +50,7 @@ import android.support.v13.view.inputmethod.InputConnectionCompat;
import android.support.v13.view.inputmethod.InputContentInfoCompat; import android.support.v13.view.inputmethod.InputContentInfoCompat;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar;
import android.support.v7.content.res.AppCompatResources; import android.support.v7.content.res.AppCompatResources;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
@ -77,9 +80,11 @@ import com.keylesspalace.tusky.entity.Media;
import com.keylesspalace.tusky.entity.Status; import com.keylesspalace.tusky.entity.Status;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
@ -99,9 +104,11 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFrag
private static final int STATUS_CHARACTER_LIMIT = 500; private static final int STATUS_CHARACTER_LIMIT = 500;
private static final int STATUS_MEDIA_SIZE_LIMIT = 4000000; // 4MB private static final int STATUS_MEDIA_SIZE_LIMIT = 4000000; // 4MB
private static final int MEDIA_PICK_RESULT = 1; private static final int MEDIA_PICK_RESULT = 1;
private static final int MEDIA_TAKE_PHOTO_RESULT = 2;
private static final int PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1; private static final int PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1;
private static final int MEDIA_SIZE_UNKNOWN = -1; private static final int MEDIA_SIZE_UNKNOWN = -1;
private static final int COMPOSE_SUCCESS = -1; private static final int COMPOSE_SUCCESS = -1;
private static final int THUMBNAIL_SIZE = 128;
private String inReplyToId; private String inReplyToId;
private EditText textEditor; private EditText textEditor;
@ -121,9 +128,11 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFrag
private TextView charactersLeft; private TextView charactersLeft;
private Button floatingBtn; private Button floatingBtn;
private ImageButton pickBtn; private ImageButton pickBtn;
private ImageButton takeBtn;
private Button nsfwBtn; private Button nsfwBtn;
private ProgressBar postProgress; private ProgressBar postProgress;
private ImageButton visibilityBtn; private ImageButton visibilityBtn;
private Uri photoUploadUri;
private static class QueuedMedia { private static class QueuedMedia {
enum Type { enum Type {
@ -342,6 +351,7 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFrag
floatingBtn = (Button) findViewById(R.id.floating_btn); floatingBtn = (Button) findViewById(R.id.floating_btn);
pickBtn = (ImageButton) findViewById(R.id.compose_photo_pick); pickBtn = (ImageButton) findViewById(R.id.compose_photo_pick);
takeBtn = (ImageButton) findViewById(R.id.compose_photo_take);
nsfwBtn = (Button) findViewById(R.id.action_toggle_nsfw); nsfwBtn = (Button) findViewById(R.id.action_toggle_nsfw);
visibilityBtn = (ImageButton) findViewById(R.id.action_toggle_visibility); visibilityBtn = (ImageButton) findViewById(R.id.action_toggle_visibility);
@ -357,6 +367,12 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFrag
onMediaPick(); onMediaPick();
} }
}); });
takeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
initiateCameraApp();
}
});
nsfwBtn.setOnClickListener(new View.OnClickListener() { nsfwBtn.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
@ -565,6 +581,7 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFrag
private void disableButtons() { private void disableButtons() {
pickBtn.setClickable(false); pickBtn.setClickable(false);
takeBtn.setClickable(false);
nsfwBtn.setClickable(false); nsfwBtn.setClickable(false);
visibilityBtn.setClickable(false); visibilityBtn.setClickable(false);
floatingBtn.setEnabled(false); floatingBtn.setEnabled(false);
@ -572,6 +589,7 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFrag
private void enableButtons() { private void enableButtons() {
pickBtn.setClickable(true); pickBtn.setClickable(true);
takeBtn.setClickable(true);
nsfwBtn.setClickable(true); nsfwBtn.setClickable(true);
visibilityBtn.setClickable(true); visibilityBtn.setClickable(true);
floatingBtn.setEnabled(true); floatingBtn.setEnabled(true);
@ -944,6 +962,42 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFrag
} }
} }
private File createNewImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
String imageFileName = "Tusky_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
return image;
}
private void initiateCameraApp() {
// We don't need to ask for permission in this case, because the used calls require
// android.permission.WRITE_EXTERNAL_STORAGE only on SDKs *older* than Kitkat, which was
// way before permission dialogues have been introduced.
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createNewImageFile();
} catch (IOException ex) {
displayTransientError(R.string.error_media_upload_opening);
}
// Continue only if the File was successfully created
if (photoFile != null) {
photoUploadUri = FileProvider.getUriForFile(this,
"com.keylesspalace.tusky.fileprovider",
photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUploadUri);
startActivityForResult(intent, MEDIA_TAKE_PHOTO_RESULT);
}
}
}
private void initiateMediaPicking() { private void initiateMediaPicking() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE); intent.addCategory(Intent.CATEGORY_OPENABLE);
@ -957,16 +1011,22 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFrag
startActivityForResult(intent, MEDIA_PICK_RESULT); startActivityForResult(intent, MEDIA_PICK_RESULT);
} }
private void enableMediaPicking() { private void enableMediaButtons() {
pickBtn.setEnabled(true); pickBtn.setEnabled(true);
ThemeUtils.setDrawableTint(this, pickBtn.getDrawable(), ThemeUtils.setDrawableTint(this, pickBtn.getDrawable(),
R.attr.compose_media_button_tint); R.attr.compose_media_button_tint);
takeBtn.setEnabled(true);
ThemeUtils.setDrawableTint(this, takeBtn.getDrawable(),
R.attr.compose_media_button_tint);
} }
private void disableMediaPicking() { private void disableMediaButtons() {
pickBtn.setEnabled(false); pickBtn.setEnabled(false);
ThemeUtils.setDrawableTint(this, pickBtn.getDrawable(), ThemeUtils.setDrawableTint(this, pickBtn.getDrawable(),
R.attr.compose_media_button_disabled_tint); R.attr.compose_media_button_disabled_tint);
takeBtn.setEnabled(false);
ThemeUtils.setDrawableTint(this, takeBtn.getDrawable(),
R.attr.compose_media_button_disabled_tint);
} }
private void addMediaToQueue(QueuedMedia.Type type, Bitmap preview, Uri uri, long mediaSize) { private void addMediaToQueue(QueuedMedia.Type type, Bitmap preview, Uri uri, long mediaSize) {
@ -1001,11 +1061,11 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFrag
textEditor.getPaddingRight(), totalHeight); textEditor.getPaddingRight(), totalHeight);
// If there's one video in the queue it is full, so disable the button to queue more. // If there's one video in the queue it is full, so disable the button to queue more.
if (item.type == QueuedMedia.Type.VIDEO) { if (item.type == QueuedMedia.Type.VIDEO) {
disableMediaPicking(); disableMediaButtons();
} }
} else if (queuedCount >= Status.MAX_MEDIA_ATTACHMENTS) { } else if (queuedCount >= Status.MAX_MEDIA_ATTACHMENTS) {
// Limit the total media attachments, also. // Limit the total media attachments, also.
disableMediaPicking(); disableMediaButtons();
} }
if (queuedCount >= 1) { if (queuedCount >= 1) {
showMarkSensitive(true); showMarkSensitive(true);
@ -1028,7 +1088,7 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFrag
textEditor.setPadding(textEditor.getPaddingLeft(), textEditor.getPaddingTop(), textEditor.setPadding(textEditor.getPaddingLeft(), textEditor.getPaddingTop(),
textEditor.getPaddingRight(), 0); textEditor.getPaddingRight(), 0);
} }
enableMediaPicking(); enableMediaButtons();
cancelReadyingMedia(item); cancelReadyingMedia(item);
} }
@ -1189,9 +1249,20 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFrag
Uri uri = data.getData(); Uri uri = data.getData();
long mediaSize = getMediaSize(getContentResolver(), uri); long mediaSize = getMediaSize(getContentResolver(), uri);
pickMedia(uri, mediaSize); pickMedia(uri, mediaSize);
} else if (requestCode == MEDIA_TAKE_PHOTO_RESULT && resultCode == RESULT_OK) {
queueCameraResult();
} }
} }
private void queueCameraResult() {
ContentResolver contentResolver = getContentResolver();
Cursor returnCursor = contentResolver.query(photoUploadUri, null, null, null, null);
int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
returnCursor.moveToFirst();
pickMedia(photoUploadUri, returnCursor.getLong(sizeIndex));
}
private void pickMedia(Uri uri, long mediaSize) { private void pickMedia(Uri uri, long mediaSize) {
ContentResolver contentResolver = getContentResolver(); ContentResolver contentResolver = getContentResolver();
if (mediaSize == MEDIA_SIZE_UNKNOWN) { if (mediaSize == MEDIA_SIZE_UNKNOWN) {
@ -1215,7 +1286,7 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFrag
MediaMetadataRetriever retriever = new MediaMetadataRetriever(); MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(this, uri); retriever.setDataSource(this, uri);
Bitmap source = retriever.getFrameAtTime(); Bitmap source = retriever.getFrameAtTime();
Bitmap bitmap = ThumbnailUtils.extractThumbnail(source, 128, 128); Bitmap bitmap = ThumbnailUtils.extractThumbnail(source, THUMBNAIL_SIZE, THUMBNAIL_SIZE);
source.recycle(); source.recycle();
addMediaToQueue(QueuedMedia.Type.VIDEO, bitmap, uri, mediaSize); addMediaToQueue(QueuedMedia.Type.VIDEO, bitmap, uri, mediaSize);
break; break;
@ -1228,8 +1299,9 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFrag
displayTransientError(R.string.error_media_upload_opening); displayTransientError(R.string.error_media_upload_opening);
return; return;
} }
Bitmap source = BitmapFactory.decodeStream(stream); Bitmap source = BitmapFactory.decodeStream(stream);
Bitmap bitmap = ThumbnailUtils.extractThumbnail(source, 128, 128); Bitmap bitmap = ThumbnailUtils.extractThumbnail(source, THUMBNAIL_SIZE, THUMBNAIL_SIZE);
source.recycle(); source.recycle();
try { try {
if (stream != null) { if (stream != null) {

View File

@ -0,0 +1,12 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="@color/toolbar_icon_dark"
android:pathData="M12,12m-3.2,0a3.2,3.2 0,1 1,6.4 0a3.2,3.2 0,1 1,-6.4 0"/>
<path
android:fillColor="@color/toolbar_icon_dark"
android:pathData="M9,2L7.17,4L4,4c-1.1,0 -2,0.9 -2,2v12c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2h-3.17L15,2L9,2zM12,17c-2.76,0 -5,-2.24 -5,-5s2.24,-5 5,-5 5,2.24 5,5 -2.24,5 -5,5z"/>
</vector>

View File

@ -91,6 +91,15 @@
android:paddingRight="16dp" android:paddingRight="16dp"
android:paddingTop="4dp"> android:paddingTop="4dp">
<ImageButton
android:id="@+id/compose_photo_take"
style="?attr/image_button_style"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginRight="8dp"
app:srcCompat="@drawable/ic_camera_24dp"
android:contentDescription="@string/action_photo_take" />
<ImageButton <ImageButton
android:id="@+id/compose_photo_pick" android:id="@+id/compose_photo_pick"
style="?attr/image_button_style" style="?attr/image_button_style"

View File

@ -79,6 +79,7 @@
<string name="action_open_in_web">Open in browser</string> <string name="action_open_in_web">Open in browser</string>
<string name="action_submit">Submit</string> <string name="action_submit">Submit</string>
<string name="action_photo_pick">Add media</string> <string name="action_photo_pick">Add media</string>
<string name="action_photo_take">Take photo</string>
<string name="action_share">Share</string> <string name="action_share">Share</string>
<string name="action_mute">Mute</string> <string name="action_mute">Mute</string>
<string name="action_unmute">Unmute</string> <string name="action_unmute">Unmute</string>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" />
</paths>