Skip to content
This repository has been archived by the owner on May 30, 2021. It is now read-only.

Commit

Permalink
* refactor rxjava module, * remove portrait orientation from image an…
Browse files Browse the repository at this point in the history
…d video activity
  • Loading branch information
alhazmy13 committed Apr 17, 2018
1 parent 1e86c26 commit f10daf1
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand Down Expand Up @@ -49,7 +51,7 @@ public void onClick(View v) {


private void pickImage() {
new ImagePickerHelper(
Observable<List<String>> x = new ImagePickerHelper(
new ImagePicker.Builder(getActivity())
.mode(ImagePicker.Mode.CAMERA_AND_GALLERY)
.allowMultipleImages(true)
Expand All @@ -60,36 +62,34 @@ private void pickImage() {
.scale(600, 600)
.allowMultipleImages(true)
.enableDebuggingMode(true))
.getObservable()
.doOnNext(new Consumer<List<String>>() {
@Override
public void accept(List<String> imagePath) throws Exception {
Log.d(TAG, "onNext() called with: " + "imagePath = [" + imagePath + "]");
mPath = imagePath;
loadImage();
}
})
.doOnError(new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Log.d(TAG, "onError() called with: " + "e = [" + throwable + "]");
throwable.printStackTrace();
}
}).subscribe();
.getObservable();

}
x.subscribe(new Observer<List<String>>() {
@Override
public void onSubscribe(Disposable d) {

}

@Override
public void onNext(List<String> 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<String>) 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,6 +49,7 @@ public class ImageActivity extends AppCompatActivity {
private Uri mImageUri;
private ImageConfig mImgConfig;
private List<String> listOfImgs;
private AlertDialog alertDialog;

public static Intent getCallingIntent(Context activity, ImageConfig imageConfig) {
Intent intent = new Intent(activity, ImageActivity.class);
Expand All @@ -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);
Expand All @@ -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());
Expand All @@ -96,14 +102,15 @@ 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
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() {
Expand All @@ -115,17 +122,22 @@ public void onClick(DialogInterface dialogInterface, int i) {
startActivityFromGalleryMultiImg();
else
startActivityFromGallery();
alertDialog.dismiss();
}
})
.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,6 +42,7 @@ public class VideoActivity extends AppCompatActivity {
private Uri mVideoUri;
private VideoConfig mVideoConfig;
private List<String> mListOfVideos;
private AlertDialog alertDialog;

public static Intent getCallingIntent(Context activity, VideoConfig videoConfig) {
Intent intent = new Intent(activity, VideoActivity.class);
Expand All @@ -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);
Expand All @@ -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());
Expand All @@ -90,14 +94,15 @@ 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
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() {
Expand All @@ -109,17 +114,20 @@ public void onClick(DialogInterface dialogInterface, int i) {
startActivityFromGalleryMultiImg();
else
startActivityFromGallery();
alertDialog.dismiss();
}
})
.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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<List<String>>, Disposable {

private static final String TAG = "ImagePickerObservable";
private ImagePicker.Builder imagePicker;
private ImagePickerReceiver broadcastReceiver;
private final WeakReference<Context> contextWeakReference;

public ImagePickerObservable(ImagePicker.Builder imagePicker) {
this.imagePicker = imagePicker;
this.contextWeakReference = new WeakReference<Context>(imagePicker.getContext());

}


@Override
public void subscribe(ObservableEmitter<List<String>> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,7 +9,7 @@

import java.util.List;

import io.reactivex.ObservableEmitter;
import io.reactivex.Emitter;


/**
Expand All @@ -19,18 +19,20 @@
public class ImagePickerReceiver extends BroadcastReceiver {

private static final String TAG = "VideoPickerReceiver";
private ObservableEmitter<List<String>> observer;
private Emitter<? super List<String>> emitter;

public ImagePickerReceiver(ObservableEmitter<List<String>> observer) {
this.observer = observer;
public ImagePickerReceiver(Emitter<? super List<String>> observer) {
this.emitter = observer;
}

public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Received message " + intent);
List<String> imagePath = (List<String>) 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)));
}
}
}

This file was deleted.

Loading

0 comments on commit f10daf1

Please sign in to comment.