Skip to content
This repository has been archived by the owner on Mar 17, 2023. It is now read-only.

Return canceled on back #74

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ You can receive the new processed image path and it's edit status like this-
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == PHOTO_EDITOR_REQUEST_CODE) { // same code you used while starting
String newFilePath = data.getStringExtra(EditImageActivity.OUTPUT_PATH);
String newFilePath = data.getStringExtra(ImageEditorIntentBuilder.OUTPUT_PATH);
boolean isImageEdit = data.getBooleanExtra(EditImageActivity.IMAGE_IS_EDIT, false);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
Expand Down Expand Up @@ -79,6 +80,7 @@ public class EditImageActivity extends BaseActivity implements OnLoadingDialogLi
Manifest.permission.WRITE_EXTERNAL_STORAGE
};

public Uri sourceUri;
public String sourceFilePath;
public String outputFilePath;
public String editorTitle;
Expand Down Expand Up @@ -116,7 +118,10 @@ public class EditImageActivity extends BaseActivity implements OnLoadingDialogLi
private CompositeDisposable compositeDisposable = new CompositeDisposable();

public static void start(Activity activity, Intent intent, int requestCode) {
if (TextUtils.isEmpty(intent.getStringExtra(ImageEditorIntentBuilder.SOURCE_PATH))) {
String sourcePath = intent.getStringExtra(ImageEditorIntentBuilder.SOURCE_PATH);
String sourceUriStr = intent.getStringExtra(ImageEditorIntentBuilder.SOURCE_URI);

if (TextUtils.isEmpty(sourcePath) && TextUtils.isEmpty(sourceUriStr)) {
Toast.makeText(activity, R.string.iamutkarshtiwari_github_io_ananas_not_selected, Toast.LENGTH_SHORT).show();
return;
}
Expand Down Expand Up @@ -151,6 +156,11 @@ private void getData() {
isPortraitForced = getIntent().getBooleanExtra(ImageEditorIntentBuilder.FORCE_PORTRAIT, false);
isSupportActionBarEnabled = getIntent().getBooleanExtra(ImageEditorIntentBuilder.SUPPORT_ACTION_BAR_VISIBILITY, false);

String sourceUriStr = getIntent().getStringExtra(ImageEditorIntentBuilder.SOURCE_URI);
if (!TextUtils.isEmpty(sourceUriStr)) {
sourceUri = Uri.parse(sourceUriStr);
}

sourceFilePath = getIntent().getStringExtra(ImageEditorIntentBuilder.SOURCE_PATH);
outputFilePath = getIntent().getStringExtra(ImageEditorIntentBuilder.OUTPUT_PATH);
editorTitle = getIntent().getStringExtra(ImageEditorIntentBuilder.EDITOR_TITLE);
Expand Down Expand Up @@ -228,7 +238,11 @@ private void initView() {
ActivityCompat.requestPermissions(this, requiredPermissions, PERMISSIONS_REQUEST_CODE);
}

loadImageFromFile(sourceFilePath);
if (!TextUtils.isEmpty(sourceFilePath)) {
loadImageFromFile(sourceFilePath);
} else {
loadImageFromUri(sourceUri);
}
}

private void setOnMainBitmapChangeListener(OnMainBitmapChangeListener listener) {
Expand All @@ -243,6 +257,7 @@ public void onRequestPermissionsResult(int requestCode,
// If request is cancelled, the result arrays are empty.
if (!(grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
setResult(RESULT_CANCELED);
finish();
}
break;
Expand Down Expand Up @@ -303,11 +318,17 @@ public void onBackPressed() {
break;
default:
if (canAutoExit()) {
onSaveTaskDone();
setResult(RESULT_CANCELED);
finish();
} else {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage(R.string.iamutkarshtiwari_github_io_ananas_exit_without_save)
.setCancelable(false).setPositiveButton(R.string.iamutkarshtiwari_github_io_ananas_confirm, (dialog, id) -> finish()).setNegativeButton(R.string.iamutkarshtiwari_github_io_ananas_cancel, (dialog, id) -> dialog.cancel());
.setCancelable(false)
.setPositiveButton(R.string.iamutkarshtiwari_github_io_ananas_confirm, (dialog, id) -> {
setResult(RESULT_CANCELED);
finish();
})
.setNegativeButton(R.string.iamutkarshtiwari_github_io_ananas_cancel, (dialog, id) -> dialog.cancel());

AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
Expand Down Expand Up @@ -337,6 +358,11 @@ public void changeMainBitmap(Bitmap newBit, boolean needPushUndoStack) {

protected void onSaveTaskDone() {
Intent returnIntent = new Intent();

if (sourceUri != null) {
returnIntent.putExtra(ImageEditorIntentBuilder.SOURCE_URI, sourceUri.toString());
}

returnIntent.putExtra(ImageEditorIntentBuilder.SOURCE_PATH, sourceFilePath);
returnIntent.putExtra(ImageEditorIntentBuilder.OUTPUT_PATH, outputFilePath);
returnIntent.putExtra(IS_IMAGE_EDITED, numberOfOperations > 0);
Expand Down Expand Up @@ -377,6 +403,19 @@ private Single<Boolean> saveImage(Bitmap finalBitmap) {
});
}

private void loadImageFromUri(Uri uri) {
compositeDisposable.clear();

Disposable loadImageDisposable = loadImage(uri)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe(subscriber -> loadingDialog.show())
.doFinally(() -> loadingDialog.dismiss())
.subscribe(processedBitmap -> changeMainBitmap(processedBitmap, false), e -> showToast(R.string.iamutkarshtiwari_github_io_ananas_load_error));

compositeDisposable.add(loadImageDisposable);
}

private void loadImageFromFile(String filePath) {
compositeDisposable.clear();

Expand All @@ -395,6 +434,11 @@ private Single<Bitmap> loadImage(String filePath) {
imageHeight));
}

private Single<Bitmap> loadImage(Uri uri) {
return Single.fromCallable(() -> BitmapUtils.decodeSampledBitmap(this, uri,
imageWidth, imageHeight));
}

private void showToast(@StringRes int resId) {
Toast.makeText(this, resId, Toast.LENGTH_SHORT).show();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package iamutkarshtiwari.github.io.ananas.editimage

import android.content.Context
import android.content.Intent
import android.net.Uri

class ImageEditorIntentBuilder @JvmOverloads constructor(private val context: Context,
private val sourcePath: String?,
Expand All @@ -11,6 +12,17 @@ class ImageEditorIntentBuilder @JvmOverloads constructor(private val context: Co
EditImageActivity::class.java
)
) {
private var sourceUri: Uri? = null

@JvmOverloads constructor(context: Context,
sourceUri: Uri,
outputPath: String?,
intent: Intent = Intent(
context,
EditImageActivity::class.java
)) : this(context, null, outputPath, intent) {
this.sourceUri = sourceUri
}

fun withAddText(): ImageEditorIntentBuilder {
intent.putExtra(ADD_TEXT_FEATURE, true)
Expand Down Expand Up @@ -62,8 +74,16 @@ class ImageEditorIntentBuilder @JvmOverloads constructor(private val context: Co
return this
}

fun withSourceUri(sourceUri: Uri): ImageEditorIntentBuilder {
this.sourceUri = sourceUri
intent.putExtra(SOURCE_URI, sourceUri.toString())
intent.removeExtra(SOURCE_PATH)
return this
}

fun withSourcePath(sourcePath: String): ImageEditorIntentBuilder {
intent.putExtra(SOURCE_PATH, sourcePath)
intent.removeExtra(SOURCE_URI)
return this
}

Expand All @@ -85,10 +105,14 @@ class ImageEditorIntentBuilder @JvmOverloads constructor(private val context: Co
@Throws(Exception::class)
fun build(): Intent {

if (sourcePath.isNullOrBlank()) {
throw Exception("Output image path required. Use withOutputPath(path) to provide the output image path.")
} else {
if (sourcePath.isNullOrBlank() && sourceUri == null) {
throw Exception("Source image required. Use withSourcePath(path) or withSourceUri(uri) to provide the source.")
} else if (!sourcePath.isNullOrBlank() && sourceUri != null) {
throw Exception("Multiple source images specified. Use either withSourcePath(path) or withSourceUri(uri) to provide the source.")
} else if (!sourcePath.isNullOrBlank()) {
intent.putExtra(SOURCE_PATH, sourcePath)
} else {
intent.putExtra(SOURCE_URI, sourceUri.toString())
}

if (outputPath.isNullOrBlank()) {
Expand All @@ -111,6 +135,7 @@ class ImageEditorIntentBuilder @JvmOverloads constructor(private val context: Co
const val BEAUTY_FEATURE = "beauty_feature"
const val STICKER_FEATURE = "sticker_feature"

const val SOURCE_URI = "source_uri"
const val SOURCE_PATH = "source_path"
const val OUTPUT_PATH = "output_path"
const val FORCE_PORTRAIT = "force_portrait"
Expand Down
Loading