diff --git a/app/src/main/java/net/alhazmy13/mediapickerexample/ImageFragment.java b/app/src/main/java/net/alhazmy13/mediapickerexample/ImageFragment.java index 01250d7..9ed0032 100644 --- a/app/src/main/java/net/alhazmy13/mediapickerexample/ImageFragment.java +++ b/app/src/main/java/net/alhazmy13/mediapickerexample/ImageFragment.java @@ -15,7 +15,9 @@ import java.util.List; -import io.reactivex.functions.Consumer; +import io.reactivex.Observable; +import io.reactivex.Observer; +import io.reactivex.disposables.Disposable; /** @@ -49,7 +51,7 @@ public void onClick(View v) { private void pickImage() { - new ImagePickerHelper( + Observable> x = new ImagePickerHelper( new ImagePicker.Builder(getActivity()) .mode(ImagePicker.Mode.CAMERA_AND_GALLERY) .allowMultipleImages(true) @@ -60,36 +62,34 @@ private void pickImage() { .scale(600, 600) .allowMultipleImages(true) .enableDebuggingMode(true)) - .getObservable() - .doOnNext(new Consumer>() { - @Override - public void accept(List imagePath) throws Exception { - Log.d(TAG, "onNext() called with: " + "imagePath = [" + imagePath + "]"); - mPath = imagePath; - loadImage(); - } - }) - .doOnError(new Consumer() { - @Override - public void accept(Throwable throwable) throws Exception { - Log.d(TAG, "onError() called with: " + "e = [" + throwable + "]"); - throwable.printStackTrace(); - } - }).subscribe(); + .getObservable(); - } + x.subscribe(new Observer>() { + @Override + public void onSubscribe(Disposable d) { + + } + @Override + public void onNext(List imagePath) { + Log.d(TAG, "onNext() called with: " + "imagePath = [" + imagePath + "]"); + mPath = imagePath; + loadImage(); + } -// @Override -// public void onActivityResult(int requestCode, int resultCode, Intent data) { -// super.onActivityResult(requestCode, resultCode, data); -// Log.d(TAG, "onActivityResult() called with: requestCode = [" + requestCode + "], resultCode = [" + resultCode + "], data = [" + data + "]"); -// if (requestCode == ImagePicker.IMAGE_PICKER_REQUEST_CODE && resultCode == RESULT_OK) { -// mPath = (List) data.getSerializableExtra(ImagePicker.EXTRA_IMAGE_PATH); -// -// loadImage(); -// } -// } + @Override + public void onError(Throwable e) { + Log.d(TAG, "onError() called with: " + "e = [" + e + "]"); + e.printStackTrace(); + } + + @Override + public void onComplete() { + + } + }); + + } private void loadImage() { Log.d(TAG, "loadImage: " + mPath.size()); diff --git a/libary/src/main/java/net/alhazmy13/mediapicker/Image/ImageActivity.java b/libary/src/main/java/net/alhazmy13/mediapicker/Image/ImageActivity.java index f977660..28112da 100644 --- a/libary/src/main/java/net/alhazmy13/mediapicker/Image/ImageActivity.java +++ b/libary/src/main/java/net/alhazmy13/mediapicker/Image/ImageActivity.java @@ -7,7 +7,6 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; -import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; import android.net.Uri; import android.os.AsyncTask; @@ -50,6 +49,7 @@ public class ImageActivity extends AppCompatActivity { private Uri mImageUri; private ImageConfig mImgConfig; private List listOfImgs; + private AlertDialog alertDialog; public static Intent getCallingIntent(Context activity, ImageConfig imageConfig) { Intent intent = new Intent(activity, ImageActivity.class); @@ -60,7 +60,6 @@ public static Intent getCallingIntent(Context activity, ImageConfig imageConfig) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); Intent intent = getIntent(); if (intent != null) { mImgConfig = (ImageConfig) intent.getSerializableExtra(ImageTags.Tags.IMG_CONFIG); @@ -74,6 +73,13 @@ protected void onCreate(Bundle savedInstanceState) { Log.d(ImageTags.Tags.TAG, mImgConfig.toString()); } + @Override + protected void onPause() { + if (alertDialog != null) + alertDialog.dismiss(); + super.onPause(); + } + private void pickImage() { Utility.createFolder(mImgConfig.directory); destination = new File(mImgConfig.directory, Utility.getRandomString() + mImgConfig.extension.getValue()); @@ -96,7 +102,7 @@ private void pickImage() { } private void showFromCameraOrGalleryAlert() { - new AlertDialog.Builder(this) + alertDialog = new AlertDialog.Builder(this) .setTitle(getString(R.string.media_picker_select_from)) .setPositiveButton(getString(R.string.media_picker_camera), new DialogInterface.OnClickListener() { @Override @@ -104,6 +110,7 @@ public void onClick(DialogInterface dialogInterface, int i) { if (mImgConfig.debug) Log.d(ImageTags.Tags.TAG, "Alert Dialog - Start From Camera"); startActivityFromCamera(); + alertDialog.dismiss(); } }) .setNegativeButton(getString(R.string.media_picker_gallery), new DialogInterface.OnClickListener() { @@ -115,6 +122,7 @@ public void onClick(DialogInterface dialogInterface, int i) { startActivityFromGalleryMultiImg(); else startActivityFromGallery(); + alertDialog.dismiss(); } }) .setOnCancelListener(new DialogInterface.OnCancelListener() { @@ -122,10 +130,14 @@ public void onClick(DialogInterface dialogInterface, int i) { public void onCancel(DialogInterface dialogInterface) { if (mImgConfig.debug) Log.d(ImageTags.Tags.TAG, "Alert Dialog - Canceled"); + alertDialog.dismiss(); finish(); } }) - .show(); + .create(); + if (alertDialog != null) + alertDialog.show(); + } private void startActivityFromGallery() { diff --git a/libary/src/main/java/net/alhazmy13/mediapicker/Video/VideoActivity.java b/libary/src/main/java/net/alhazmy13/mediapicker/Video/VideoActivity.java index 749288e..bf18763 100644 --- a/libary/src/main/java/net/alhazmy13/mediapicker/Video/VideoActivity.java +++ b/libary/src/main/java/net/alhazmy13/mediapicker/Video/VideoActivity.java @@ -6,7 +6,6 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; -import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; import android.net.Uri; import android.os.AsyncTask; @@ -43,6 +42,7 @@ public class VideoActivity extends AppCompatActivity { private Uri mVideoUri; private VideoConfig mVideoConfig; private List mListOfVideos; + private AlertDialog alertDialog; public static Intent getCallingIntent(Context activity, VideoConfig videoConfig) { Intent intent = new Intent(activity, VideoActivity.class); @@ -53,8 +53,6 @@ public static Intent getCallingIntent(Context activity, VideoConfig videoConfig) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); - Intent intent = getIntent(); if (intent != null) { mVideoConfig = (VideoConfig) intent.getSerializableExtra(VideoTags.Tags.IMG_CONFIG); @@ -68,6 +66,12 @@ protected void onCreate(Bundle savedInstanceState) { Log.d(VideoTags.Tags.TAG, mVideoConfig.toString()); } + @Override + protected void onPause() { + if (alertDialog != null) + alertDialog.dismiss(); + super.onPause(); + } private void pickVideo() { Utility.createFolder(mVideoConfig.directory); destination = new File(mVideoConfig.directory, Utility.getRandomString() + mVideoConfig.extension.getValue()); @@ -90,7 +94,7 @@ private void pickVideo() { } private void showFromCameraOrGalleryAlert() { - new AlertDialog.Builder(this) + alertDialog = new AlertDialog.Builder(this) .setTitle(getString(R.string.media_picker_select_from)) .setPositiveButton(getString(R.string.media_picker_camera), new DialogInterface.OnClickListener() { @Override @@ -98,6 +102,7 @@ public void onClick(DialogInterface dialogInterface, int i) { if (mVideoConfig.debug) Log.d(VideoTags.Tags.TAG, "Alert Dialog - Start From Camera"); startActivityFromCamera(); + alertDialog.dismiss(); } }) .setNegativeButton(getString(R.string.media_picker_gallery), new DialogInterface.OnClickListener() { @@ -109,6 +114,7 @@ public void onClick(DialogInterface dialogInterface, int i) { startActivityFromGalleryMultiImg(); else startActivityFromGallery(); + alertDialog.dismiss(); } }) .setOnCancelListener(new DialogInterface.OnCancelListener() { @@ -116,10 +122,12 @@ public void onClick(DialogInterface dialogInterface, int i) { public void onCancel(DialogInterface dialogInterface) { if (mVideoConfig.debug) Log.d(VideoTags.Tags.TAG, "Alert Dialog - Canceled"); + alertDialog.dismiss(); finish(); } - }) - .show(); + }).create(); + alertDialog.show(); + } private void startActivityFromGallery() { diff --git a/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/image/ImagePickerHelper.java b/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/image/ImagePickerHelper.java index 20404e4..558c05a 100644 --- a/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/image/ImagePickerHelper.java +++ b/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/image/ImagePickerHelper.java @@ -1,6 +1,5 @@ package net.alhazmy13.mediapicker.rxjava.image; import net.alhazmy13.mediapicker.Image.ImagePicker; -import net.alhazmy13.mediapicker.rxjava.image.observable.ImagePickerObservable; import java.util.List; diff --git a/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/image/ImagePickerObservable.java b/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/image/ImagePickerObservable.java new file mode 100644 index 0000000..d379b64 --- /dev/null +++ b/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/image/ImagePickerObservable.java @@ -0,0 +1,55 @@ +package net.alhazmy13.mediapicker.rxjava.image; + + +import android.content.Context; +import android.content.IntentFilter; + +import net.alhazmy13.mediapicker.Image.ImagePicker; +import net.alhazmy13.mediapicker.Image.ImageTags; + +import java.lang.ref.WeakReference; +import java.util.List; + +import io.reactivex.ObservableEmitter; +import io.reactivex.ObservableOnSubscribe; +import io.reactivex.disposables.Disposable; + +/** + * Created by Alhazmy13 on 8/7/16. + * MediaPicker + */ +public class ImagePickerObservable implements ObservableOnSubscribe>, Disposable { + + private static final String TAG = "ImagePickerObservable"; + private ImagePicker.Builder imagePicker; + private ImagePickerReceiver broadcastReceiver; + private final WeakReference contextWeakReference; + + public ImagePickerObservable(ImagePicker.Builder imagePicker) { + this.imagePicker = imagePicker; + this.contextWeakReference = new WeakReference(imagePicker.getContext()); + + } + + + @Override + public void subscribe(ObservableEmitter> emitter) { + broadcastReceiver = new ImagePickerReceiver(emitter); + imagePicker.build(); + contextWeakReference.get().registerReceiver(broadcastReceiver, new IntentFilter(ImageTags.Action.SERVICE_ACTION)); + } + + @Override + public void dispose() { + if (contextWeakReference != null && contextWeakReference.get() != null && broadcastReceiver != null) { + contextWeakReference.get().unregisterReceiver(broadcastReceiver); + + } + broadcastReceiver = null; + } + + @Override + public boolean isDisposed() { + return broadcastReceiver == null; + } +} diff --git a/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/image/service/ImagePickerReceiver.java b/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/image/ImagePickerReceiver.java similarity index 56% rename from rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/image/service/ImagePickerReceiver.java rename to rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/image/ImagePickerReceiver.java index e38a391..0d3cef1 100644 --- a/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/image/service/ImagePickerReceiver.java +++ b/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/image/ImagePickerReceiver.java @@ -1,4 +1,4 @@ -package net.alhazmy13.mediapicker.rxjava.image.service; +package net.alhazmy13.mediapicker.rxjava.image; import android.content.BroadcastReceiver; import android.content.Context; @@ -9,7 +9,7 @@ import java.util.List; -import io.reactivex.ObservableEmitter; +import io.reactivex.Emitter; /** @@ -19,18 +19,20 @@ public class ImagePickerReceiver extends BroadcastReceiver { private static final String TAG = "VideoPickerReceiver"; - private ObservableEmitter> observer; + private Emitter> emitter; - public ImagePickerReceiver(ObservableEmitter> observer) { - this.observer = observer; + public ImagePickerReceiver(Emitter> observer) { + this.emitter = observer; } public void onReceive(Context context, Intent intent) { Log.d(TAG, "Received message " + intent); List imagePath = (List) intent.getSerializableExtra(ImageTags.Tags.IMAGE_PATH); - if (imagePath != null && imagePath.size() > 0) - observer.onNext(imagePath); - else - observer.onError(new Throwable(intent.getStringExtra(ImageTags.Tags.PICK_ERROR))); + if (imagePath != null && imagePath.size() > 0) { + emitter.onNext(imagePath); + emitter.onComplete(); + } else { + emitter.onError(new Throwable(intent.getStringExtra(ImageTags.Tags.PICK_ERROR))); + } } } diff --git a/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/image/observable/ImagePickerBaseObservable.java b/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/image/observable/ImagePickerBaseObservable.java deleted file mode 100644 index f6581d2..0000000 --- a/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/image/observable/ImagePickerBaseObservable.java +++ /dev/null @@ -1,29 +0,0 @@ -package net.alhazmy13.mediapicker.rxjava.image.observable; - -import android.content.Context; - -import java.util.List; - -import io.reactivex.ObservableEmitter; -import io.reactivex.ObservableOnSubscribe; - -/** - * Created by Alhazmy13 on 8/7/16. - * MediaPicker - */ -abstract class ImagePickerBaseObservable implements ObservableOnSubscribe> { - - private static final String TAG = "ImagePicker"; - public Context context; - - public ImagePickerBaseObservable(Context context) { - this.context = context; - } - - public abstract void registerImagePickerObservable(); - - @Override - public abstract void subscribe(ObservableEmitter> emitter); - - -} \ No newline at end of file diff --git a/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/image/observable/ImagePickerObservable.java b/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/image/observable/ImagePickerObservable.java deleted file mode 100644 index fa72e39..0000000 --- a/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/image/observable/ImagePickerObservable.java +++ /dev/null @@ -1,54 +0,0 @@ -package net.alhazmy13.mediapicker.rxjava.image.observable; - - -import android.content.IntentFilter; - -import net.alhazmy13.mediapicker.Image.ImagePicker; -import net.alhazmy13.mediapicker.Image.ImageTags; -import net.alhazmy13.mediapicker.rxjava.image.service.ImagePickerReceiver; - -import java.util.List; - -import io.reactivex.ObservableEmitter; - -/** - * Created by Alhazmy13 on 8/7/16. - * MediaPicker - */ -public class ImagePickerObservable extends ImagePickerBaseObservable { - - private static final String TAG = "VideoPickerObservable"; - private ImagePicker.Builder imagePicker; - private ImagePickerReceiver receiver; - - public ImagePickerObservable(ImagePicker.Builder imagePicker) { - super(imagePicker.getContext()); - this.imagePicker = imagePicker; - - } - - - @Override - public void registerImagePickerObservable() { - context.registerReceiver(receiver, new IntentFilter(ImageTags.Action.SERVICE_ACTION)); - - } -// -// @Override -// public void onUnsubscribed() { -// Log.d(TAG, "onUnsubscribed() called with: " + ""); -// try { -// context.unregisterReceiver(receiver); -// } catch (IllegalArgumentException ignored) { -// } -// -// } - - - @Override - public void subscribe(ObservableEmitter> emitter) { - receiver = new ImagePickerReceiver(emitter); - imagePicker.build(); - registerImagePickerObservable(); - } -} diff --git a/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/video/VideoPickerHelper.java b/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/video/VideoPickerHelper.java index 394cf06..2d645b2 100644 --- a/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/video/VideoPickerHelper.java +++ b/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/video/VideoPickerHelper.java @@ -1,7 +1,6 @@ package net.alhazmy13.mediapicker.rxjava.video; import net.alhazmy13.mediapicker.Video.VideoPicker; -import net.alhazmy13.mediapicker.rxjava.video.observable.VideoPickerObservable; import java.util.List; diff --git a/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/video/VideoPickerObservable.java b/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/video/VideoPickerObservable.java new file mode 100644 index 0000000..16f357f --- /dev/null +++ b/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/video/VideoPickerObservable.java @@ -0,0 +1,59 @@ +package net.alhazmy13.mediapicker.rxjava.video; + + +import android.content.Context; +import android.content.IntentFilter; + +import net.alhazmy13.mediapicker.Image.ImageTags; +import net.alhazmy13.mediapicker.Video.VideoPicker; +import net.alhazmy13.mediapicker.Video.VideoTags; +import net.alhazmy13.mediapicker.rxjava.image.ImagePickerReceiver; + +import java.lang.ref.WeakReference; +import java.util.List; + +import io.reactivex.ObservableEmitter; +import io.reactivex.ObservableOnSubscribe; +import io.reactivex.disposables.Disposable; + +/** + * Created by Alhazmy13 on 8/7/16. + * MediaPicker + */ +public class VideoPickerObservable implements ObservableOnSubscribe>, Disposable { + + private static final String TAG = "VideoPickerObservable"; + private VideoPicker.Builder mVideoPicker; + private VideoPickerReceiver broadcastReceiver; + private final WeakReference contextWeakReference; + + public VideoPickerObservable(VideoPicker.Builder videoPicker) { + this.mVideoPicker = videoPicker; + this.contextWeakReference = new WeakReference(mVideoPicker.getContext()); + + + } + + + @Override + public void subscribe(ObservableEmitter> emitter) { + broadcastReceiver = new VideoPickerReceiver(emitter); + mVideoPicker.build(); + contextWeakReference.get().registerReceiver(broadcastReceiver, new IntentFilter(VideoTags.Action.SERVICE_ACTION)); + + } + + @Override + public void dispose() { + if (contextWeakReference != null && contextWeakReference.get() != null && broadcastReceiver != null) { + contextWeakReference.get().unregisterReceiver(broadcastReceiver); + + } + broadcastReceiver = null; + } + + @Override + public boolean isDisposed() { + return broadcastReceiver == null; + } +} diff --git a/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/video/service/VideoPickerReceiver.java b/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/video/VideoPickerReceiver.java similarity index 94% rename from rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/video/service/VideoPickerReceiver.java rename to rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/video/VideoPickerReceiver.java index 55bff27..81bc634 100644 --- a/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/video/service/VideoPickerReceiver.java +++ b/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/video/VideoPickerReceiver.java @@ -1,4 +1,4 @@ -package net.alhazmy13.mediapicker.rxjava.video.service; +package net.alhazmy13.mediapicker.rxjava.video; import android.content.BroadcastReceiver; import android.content.Context; diff --git a/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/video/observable/VideoPickerBaseObservable.java b/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/video/observable/VideoPickerBaseObservable.java deleted file mode 100644 index b0e521b..0000000 --- a/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/video/observable/VideoPickerBaseObservable.java +++ /dev/null @@ -1,28 +0,0 @@ -package net.alhazmy13.mediapicker.rxjava.video.observable; - -import android.content.Context; - -import java.util.List; - -import io.reactivex.ObservableEmitter; -import io.reactivex.ObservableOnSubscribe; - - -/** - * Created by Alhazmy13 on 8/7/16. - * MediaPicker - */ -abstract class VideoPickerBaseObservable implements ObservableOnSubscribe> { - - private static final String TAG = "ImagePicker"; - public Context context; - - public VideoPickerBaseObservable(Context context) { - this.context = context; - } - - @Override - public abstract void subscribe(ObservableEmitter> emitter); - - -} \ No newline at end of file diff --git a/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/video/observable/VideoPickerObservable.java b/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/video/observable/VideoPickerObservable.java deleted file mode 100644 index 9c4b8eb..0000000 --- a/rxjava/src/main/java/net/alhazmy13/mediapicker/rxjava/video/observable/VideoPickerObservable.java +++ /dev/null @@ -1,43 +0,0 @@ -package net.alhazmy13.mediapicker.rxjava.video.observable; - - -import android.content.IntentFilter; -import android.util.Log; - -import net.alhazmy13.mediapicker.Video.VideoPicker; -import net.alhazmy13.mediapicker.Video.VideoTags; -import net.alhazmy13.mediapicker.rxjava.video.service.VideoPickerReceiver; - -import org.reactivestreams.Subscriber; - -import java.util.List; - -import io.reactivex.ObservableEmitter; - -/** - * Created by Alhazmy13 on 8/7/16. - * MediaPicker - */ -public class VideoPickerObservable extends VideoPickerBaseObservable { - - private static final String TAG = "VideoPickerObservable"; - private VideoPicker.Builder mVideoPicker; - private VideoPickerReceiver mReceiver; - - public VideoPickerObservable(VideoPicker.Builder videoPicker) { - super(videoPicker.getContext()); - this.mVideoPicker = videoPicker; - - } - - - - - @Override - public void subscribe(ObservableEmitter> emitter) { - context.registerReceiver(mReceiver, new IntentFilter(VideoTags.Action.SERVICE_ACTION)); - mReceiver = new VideoPickerReceiver(emitter); - mVideoPicker.build(); - - } -}