Experiment: image editor

pull/217/head
M66B 8 months ago
parent ca11e6d9fe
commit 2bdd42ee54

@ -58,3 +58,4 @@ FairEmail uses parts or all of:
* [commonmark-java](https://github.com/commonmark/commonmark-java). Copyright (c) 2015, Atlassian Pty Ltd. All rights reserved. [BSD-2-Clause license](https://github.com/commonmark/commonmark-java/blob/main/LICENSE.txt).
* [flexmark-java](https://github.com/vsch/flexmark-java). Copyright (c) 2016-2018, Vladimir Schneider. All rights reserved. [BSD-2-Clause license](https://github.com/vsch/flexmark-java/blob/master/LICENSE.txt).
* [EvalEx](https://github.com/ezylang/EvalEx). Copyright 2012-2022 Udo Klimaschewski. [Apache License 2.0](https://github.com/ezylang/EvalEx/blob/main/LICENSE).
* [Android Image Cropper](https://github.com/CanHub/Android-Image-Cropper). Copyright 2016, Arthur Teplitzki, 2013, Edmodo, Inc. [Apache License 2.0](https://github.com/CanHub/Android-Image-Cropper/blob/main/LICENSE.txt).

@ -617,6 +617,7 @@ dependencies {
def tinylog_version = "2.7.0"
def zxing_version = "3.5.3"
def evalex_version = "3.3.0"
def image_cropper_version = "4.6.0"
// https://mvnrepository.com/artifact/com.android.tools/desugar_jdk_libs?repo=google
coreLibraryDesugaring "com.android.tools:desugar_jdk_libs:$desugar_version"
@ -900,4 +901,7 @@ dependencies {
// https://github.com/ezylang/EvalEx
implementation "com.ezylang:EvalEx:$evalex_version"
// https://github.com/CanHub/Android-Image-Cropper
implementation "com.vanniktech:android-image-cropper:$image_cropper_version"
}

@ -58,3 +58,4 @@ FairEmail uses parts or all of:
* [commonmark-java](https://github.com/commonmark/commonmark-java). Copyright (c) 2015, Atlassian Pty Ltd. All rights reserved. [BSD-2-Clause license](https://github.com/commonmark/commonmark-java/blob/main/LICENSE.txt).
* [flexmark-java](https://github.com/vsch/flexmark-java). Copyright (c) 2016-2018, Vladimir Schneider. All rights reserved. [BSD-2-Clause license](https://github.com/vsch/flexmark-java/blob/master/LICENSE.txt).
* [EvalEx](https://github.com/ezylang/EvalEx). Copyright 2012-2022 Udo Klimaschewski. [Apache License 2.0](https://github.com/ezylang/EvalEx/blob/main/LICENSE).
* [Android Image Cropper](https://github.com/CanHub/Android-Image-Cropper). Copyright 2016, Arthur Teplitzki, 2013, Edmodo, Inc. [Apache License 2.0](https://github.com/CanHub/Android-Image-Cropper/blob/main/LICENSE.txt).

@ -26,6 +26,7 @@ import static android.view.inputmethod.EditorInfo.IME_FLAG_NO_FULLSCREEN;
import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.app.PendingIntent;
import android.content.ClipData;
import android.content.ClipboardManager;
@ -79,6 +80,7 @@ import android.text.style.RelativeSizeSpan;
import android.text.style.URLSpan;
import android.util.Pair;
import android.util.TypedValue;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.HapticFeedbackConstants;
import android.view.KeyEvent;
@ -135,6 +137,7 @@ import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.canhub.cropper.CropImageView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.bottomnavigation.LabelVisibilityMode;
import com.google.android.material.snackbar.Snackbar;
@ -819,6 +822,175 @@ public class FragmentCompose extends FragmentBase {
}
});
if (BuildConfig.DEBUG)
etBody.setOnTouchListener(new View.OnTouchListener() {
private final GestureDetector gestureDetector = new GestureDetector(getContext(),
new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapConfirmed(@NonNull MotionEvent event) {
try {
Editable buffer = etBody.getText();
if (buffer == null)
return false;
int off = Helper.getOffset(etBody, buffer, event);
ImageSpan[] image = buffer.getSpans(off, off, ImageSpan.class);
if (image == null || image.length == 0)
return false;
String source = image[0].getSource();
if (source == null || !source.startsWith("cid:"))
return false;
long id = Long.parseLong(source.substring(source.lastIndexOf('.') + 1));
int start = buffer.getSpanStart(image[0]);
int end = buffer.getSpanEnd(image[0]);
Bundle args = new Bundle();
args.putLong("id", id);
args.putString("source", source);
args.putInt("start", start);
args.putInt("end", end);
args.putInt("zoom", zoom);
args.putLong("working", working);
new SimpleTask<EntityAttachment>() {
@Override
protected EntityAttachment onExecute(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(context);
return db.attachment().getAttachment(id);
}
@Override
protected void onExecuted(Bundle args, EntityAttachment attachment) {
if (attachment == null)
return;
Context context = etBody.getContext();
View dview = LayoutInflater.from(context).inflate(R.layout.dialog_edit_image, null);
ImageButton ibRotate = dview.findViewById(R.id.ibRotate);
ImageButton ibFlip = dview.findViewById(R.id.ibFlip);
ImageButton ibCancel = dview.findViewById(R.id.ibCancel);
ImageButton ibSave = dview.findViewById(R.id.ibSave);
CropImageView civ = dview.findViewById(R.id.civ);
Dialog dialog = new AlertDialog.Builder(context)
.setView(dview)
.create();
ibRotate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
civ.rotateImage(90);
}
});
ibFlip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
civ.flipImageHorizontally();
}
});
ibCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
ibSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
new SimpleTask<Drawable>() {
@Override
protected Drawable onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
String source = args.getString("source");
int zoom = args.getInt("zoom");
long working = args.getLong("working");
DB db = DB.getInstance(context);
EntityAttachment attachment = db.attachment().getAttachment(id);
if (attachment == null)
return null;
Bitmap bm = civ.getCroppedImage();
if (bm == null)
return null;
File file = attachment.getFile(context);
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
bm.compress(Bitmap.CompressFormat.PNG, 90, os);
}
db.attachment().setName(id, attachment.name, "image/png", file.length());
return ImageHelper.decodeImage(context, working, source, true, zoom, 1.0f, etBody);
}
@Override
protected void onExecuted(Bundle args, Drawable d) {
if (d == null)
return;
String source = args.getString("source");
int start = args.getInt("start");
int end = args.getInt("end");
Editable buffer = etBody.getText();
if (buffer == null)
return;
ImageSpan[] image = buffer.getSpans(start, end, ImageSpan.class);
if (image == null || image.length == 0)
return;
int flags = buffer.getSpanFlags(image[0]);
buffer.removeSpan(image[0]);
buffer.setSpan(new ImageSpan(d, source), start, end, flags);
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragment(), ex);
}
}.execute(FragmentCompose.this, args, "save:image");
}
});
civ.setImageUriAsync(attachment.getUri(context));
dialog.show();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(FragmentCompose.this, args, "edit:image");
return true;
} catch (Throwable ex) {
Log.e(ex);
}
return false;
}
});
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
if (compose_color != Color.TRANSPARENT)
tvSignature.setTextColor(compose_color);
tvSignature.setTypeface(StyleHelper.getTypeface(compose_font, getContext()));

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M19,7h2v2h-2zM19,21c1.1,0 2,-0.9 2,-2h-2v2zM19,15h2v2h-2zM19,11h2v2h-2zM9,5L9,3L5,3c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h4v-2L5,19L5,5h4zM19,3v2h2c0,-1.1 -0.9,-2 -2,-2zM11,1h2v22h-2zM15,3h2v2h-2zM15,19h2v2h-2z"/>
</vector>

@ -0,0 +1,11 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal"
android:autoMirrored="true">
<path
android:fillColor="@android:color/white"
android:pathData="M19.93,11c-0.17,-1.39 -0.72,-2.73 -1.62,-3.89l-1.42,1.42c0.54,0.75 0.88,1.6 1.02,2.47h2.02zM11,1v3.07C7.06,4.56 4,7.92 4,12s3.05,7.44 7,7.93v-2.02c-2.84,-0.48 -5,-2.94 -5,-5.91s2.16,-5.43 5,-5.91L11,10l4.55,-4.45L11,1zM15.46,16.87c-0.75,0.54 -1.59,0.89 -2.46,1.03v2.02c1.39,-0.17 2.74,-0.71 3.9,-1.61l-1.44,-1.44zM18.31,16.89c0.9,-1.16 1.45,-2.5 1.62,-3.89h-2.02c-0.14,0.87 -0.48,1.72 -1.02,2.48l1.42,1.41z"/>
</vector>

Binary file not shown.

After

Width:  |  Height:  |  Size: 230 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 250 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 269 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 412 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 509 B

@ -0,0 +1,77 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp">
<com.canhub.cropper.CropImageView
android:id="@+id/civ"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/ibRotate"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="@+id/ibRotate"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginTop="12dp"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/title_info"
android:scaleType="fitCenter"
android:tooltipText="@string/title_info"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/civ"
app:srcCompat="@drawable/twotone_rotate_right_24"
app:tint="?attr/colorAccent" />
<ImageButton
android:id="@+id/ibFlip"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginStart="24dp"
android:layout_marginTop="12dp"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/title_info"
android:scaleType="fitCenter"
android:tooltipText="@string/title_info"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/ibRotate"
app:layout_constraintTop_toBottomOf="@id/civ"
app:srcCompat="@drawable/twotone_flip_24"
app:tint="?attr/colorAccent" />
<ImageButton
android:id="@+id/ibCancel"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginStart="24dp"
android:layout_marginTop="12dp"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/title_save"
android:scaleType="fitCenter"
android:tooltipText="@string/title_save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/ibFlip"
app:layout_constraintTop_toBottomOf="@id/civ"
app:srcCompat="@drawable/twotone_cancel_24" />
<ImageButton
android:id="@+id/ibSave"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginTop="12dp"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/title_save"
android:scaleType="fitCenter"
android:tooltipText="@string/title_save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/civ"
app:srcCompat="@drawable/twotone_save_alt_24"
app:tint="?attr/colorAccent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Loading…
Cancel
Save