Skip to content

Commit

Permalink
Added Options for custom path to change image locations
Browse files Browse the repository at this point in the history
  • Loading branch information
akshay2211 committed Jan 5, 2019
1 parent 6e6dec2 commit 9f317d6
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 80 deletions.
11 changes: 4 additions & 7 deletions app/src/main/java/com/fxn/pixsample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.widget.Toast;

import com.fxn.adapters.MyAdapter;
import com.fxn.pix.Options;
import com.fxn.pix.Pix;
import com.fxn.utility.PermUtil;

Expand All @@ -29,9 +30,8 @@ protected void onCreate(Bundle savedInstanceState) {
recyclerView.setLayoutManager(new LinearLayoutManager(this));
myAdapter = new MyAdapter(this);
recyclerView.setAdapter(myAdapter);
findViewById(R.id.fab).setOnClickListener((View view) -> {
Pix.start(MainActivity.this, 100, 5);
});
findViewById(R.id.fab).setOnClickListener((View view) ->
Pix.start(MainActivity.this, Options.init().setRequestCode(100).setCount(6)));

}

Expand Down Expand Up @@ -59,15 +59,12 @@ public void onRequestPermissionsResult(int requestCode, String permissions[], in
case PermUtil.REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Pix.start(MainActivity.this, 100, 5);
Pix.start(MainActivity.this, Options.init().setRequestCode(100).setCount(1));
} else {
Toast.makeText(MainActivity.this, "Approve permissions to open Pix ImagePicker", Toast.LENGTH_LONG).show();
}
return;
}

// other 'case' lines to check for other
// permissions this app might request.
}
}
}
58 changes: 58 additions & 0 deletions pix/src/main/java/com/fxn/pix/Options.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.fxn.pix;

import java.io.Serializable;

public class Options implements Serializable {
private static Options options;
private int count = 1;
private int requestCode = 0;
private String path = "/DCIM/Camera";

private Options() {
}

public static Options init() {
Options.options = new Options();
return Options.options;
}

private static void check() {
if (Options.options == null) {
throw new NullPointerException("call init() method to initialise Options class");
}
}

public int getCount() {
return count;
}

public Options setCount(int count) {
check();
Options.options.count = count;
return Options.options;
}

public int getRequestCode() {
if (requestCode == 0) {
throw new NullPointerException("requestCode in Options class is null");
}
return requestCode;
}

public Options setRequestCode(int requestcode) {
check();
Options.options.requestCode = requestcode;
return Options.options;
}

public String getPath() {
return path;
}

public static Options setPath(String path) {
check();
Options.options.path = path;
return Options.options;
}

}
78 changes: 29 additions & 49 deletions pix/src/main/java/com/fxn/pix/Pix.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
Expand Down Expand Up @@ -69,7 +68,7 @@ public class Pix extends AppCompatActivity implements View.OnTouchListener {

private static final int sBubbleAnimDuration = 1000;
private static final int sScrollbarHideDelay = 1000;
private static final String SELECTION = "selection";
private static final String OPTIONS = "options";
private static final int sTrackSnapRange = 5;
public static String IMAGE_RESULTS = "image_results";
public static float TOPBAR_HEIGHT;
Expand Down Expand Up @@ -99,7 +98,7 @@ public void run() {
private float mViewHeight;
private boolean mHideScrollbar = true;
private boolean LongSelection = false;
private int SelectionCount = 1;
private Options options = null;
private RecyclerView.OnScrollListener mScrollListener = new RecyclerView.OnScrollListener() {

@Override
Expand Down Expand Up @@ -144,7 +143,7 @@ public void onClick(Img img, View view, int position) {
initaliseadapter.select(false, position);
mainImageAdapter.select(false, position);
} else {
if (SelectionCount <= selectionList.size()) {
if (options.getCount() <= selectionList.size()) {
Toast.makeText(Pix.this, String.format(getResources().getString(R.string.selection_limiter_pix), selectionList.size()), Toast.LENGTH_SHORT).show();
return;
}
Expand Down Expand Up @@ -209,7 +208,7 @@ public void onAnimationRepeat(Animation animation) {

@Override
public void onLongClick(Img img, View view, int position) {
if (SelectionCount > 1) {
if (options.getCount() > 1) {
Utility.vibe(Pix.this, 50);
//Log.e("onLongClick", "onLongClick");
LongSelection = true;
Expand Down Expand Up @@ -279,47 +278,35 @@ public boolean onTouch(View v, MotionEvent event) {
}
};

public static void start(final Fragment context, final int requestCode, final int selectionCount) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PermUtil.checkForCamaraWritePermissions(context, new WorkFinish() {
@Override
public void onWorkFinish(Boolean check) {
Intent i = new Intent(context.getActivity(), Pix.class);
i.putExtra(SELECTION, selectionCount);
context.startActivityForResult(i, requestCode);
}
});
} else {
Intent i = new Intent(context.getActivity(), Pix.class);
i.putExtra(SELECTION, selectionCount);
context.startActivityForResult(i, requestCode);
}

public static void start(final Fragment context, final Options options) {
PermUtil.checkForCamaraWritePermissions(context, new WorkFinish() {
@Override
public void onWorkFinish(Boolean check) {
Intent i = new Intent(context.getActivity(), Pix.class);
i.putExtra(OPTIONS, options);
context.startActivityForResult(i, options.getRequestCode());
}
});
}

public static void start(Fragment context, int requestCode) {
start(context, requestCode, 1);
start(context, Options.init().setRequestCode(requestCode).setCount(1));
}

public static void start(final FragmentActivity context, final int requestCode, final int selectionCount) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PermUtil.checkForCamaraWritePermissions(context, new WorkFinish() {
@Override
public void onWorkFinish(Boolean check) {
Intent i = new Intent(context, Pix.class);
i.putExtra(SELECTION, selectionCount);
context.startActivityForResult(i, requestCode);
}
});
} else {
Intent i = new Intent(context, Pix.class);
i.putExtra(SELECTION, selectionCount);
context.startActivityForResult(i, requestCode);
}
public static void start(final FragmentActivity context, final Options options) {
PermUtil.checkForCamaraWritePermissions(context, new WorkFinish() {
@Override
public void onWorkFinish(Boolean check) {
Intent i = new Intent(context, Pix.class);
i.putExtra(OPTIONS, options);
context.startActivityForResult(i, options.getRequestCode());
}
});
}

public static void start(final FragmentActivity context, int requestCode) {
start(context, requestCode, 1);
start(context, Options.init().setRequestCode(requestCode).setCount(1));
}

private void hideScrollbar() {
Expand Down Expand Up @@ -389,7 +376,7 @@ private void initialize() {
getSupportActionBar().hide();
}
try {
SelectionCount = getIntent().getIntExtra(SELECTION, 1);
options = (Options) getIntent().getSerializableExtra(OPTIONS);
} catch (Exception e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -422,7 +409,7 @@ private void initialize() {
selection_count = findViewById(R.id.selection_count);
selection_back = findViewById(R.id.selection_back);
selection_check = findViewById(R.id.selection_check);
selection_check.setVisibility((SelectionCount > 1) ? View.VISIBLE : View.GONE);
selection_check.setVisibility((options.getCount() > 1) ? View.VISIBLE : View.GONE);
sendButton = findViewById(R.id.sendButton);
img_count = findViewById(R.id.img_count);
mBubbleView = findViewById(R.id.fastscroll_bubble);
Expand Down Expand Up @@ -496,7 +483,7 @@ public Unit invoke(Bitmap bitmap) {
if (bitmap != null) {
Log.e("my pick", bitmap.toString());
synchronized (bitmap) {
File photo = Utility.writeImage(bitmap);
File photo = Utility.writeImage(bitmap, options.getPath());
Log.e("my pick saved", bitmap.toString() + " -> " + photo.length() / 1024);
selectionList.clear();
selectionList.add(new Img("", "", photo.getAbsolutePath(), ""));
Expand Down Expand Up @@ -641,14 +628,7 @@ private void setBottomSheetBehavior() {
mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
mBottomSheetBehavior.setPeekHeight((int) (Utility.convertDpToPixel(194, this)));
mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
/**
* Called when the bottom sheet changes its state.
*
* @param bottomSheet The bottom sheet view.
* @param newState The new state. This will be one of {@link #STATE_DRAGGING},
* {@link #STATE_SETTLING}, {@link #STATE_EXPANDED},
* {@link #STATE_COLLAPSED}, or {@link #STATE_HIDDEN}.
*/

@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {

Expand Down Expand Up @@ -819,7 +799,7 @@ public void onBackPressed() {
initaliseadapter.notifyItemChanged(img.getPosition());
}
LongSelection = false;
if (SelectionCount > 1) {
if (options.getCount() > 1) {
selection_check.setVisibility(View.VISIBLE);
}
DrawableCompat.setTint(selection_back.getDrawable(), colorPrimaryDark);
Expand Down
52 changes: 30 additions & 22 deletions pix/src/main/java/com/fxn/utility/PermUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,35 +50,43 @@ private static boolean addPermission(List<String> permissionsList, String permis
return true;
}

@RequiresApi(api = Build.VERSION_CODES.M)

public static void checkForCamaraWritePermissions(final FragmentActivity activity, WorkFinish workFinish) {
List<String> permissionsNeeded = new ArrayList<String>();
final List<String> permissionsList = new ArrayList<String>();
if (!addPermission(permissionsList, Manifest.permission.CAMERA, activity))
permissionsNeeded.add("CAMERA");
if (!addPermission(permissionsList, Manifest.permission.WRITE_EXTERNAL_STORAGE, activity))
permissionsNeeded.add("WRITE_EXTERNAL_STORAGE");
if (permissionsList.size() > 0) {
activity.requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
} else {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
workFinish.onWorkFinish(true);
} else {
List<String> permissionsNeeded = new ArrayList<String>();
final List<String> permissionsList = new ArrayList<String>();
if (!addPermission(permissionsList, Manifest.permission.CAMERA, activity))
permissionsNeeded.add("CAMERA");
if (!addPermission(permissionsList, Manifest.permission.WRITE_EXTERNAL_STORAGE, activity))
permissionsNeeded.add("WRITE_EXTERNAL_STORAGE");
if (permissionsList.size() > 0) {
activity.requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
} else {
workFinish.onWorkFinish(true);
}
}
}

@RequiresApi(api = Build.VERSION_CODES.M)

public static void checkForCamaraWritePermissions(final Fragment fragment, WorkFinish workFinish) {
List<String> permissionsNeeded = new ArrayList<String>();
final List<String> permissionsList = new ArrayList<String>();
if (!addPermission(permissionsList, Manifest.permission.CAMERA, fragment.getActivity()))
permissionsNeeded.add("CAMERA");
if (!addPermission(permissionsList, Manifest.permission.WRITE_EXTERNAL_STORAGE, fragment.getActivity()))
permissionsNeeded.add("WRITE_EXTERNAL_STORAGE");
if (permissionsList.size() > 0) {
fragment.requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
} else {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
workFinish.onWorkFinish(true);
} else {
List<String> permissionsNeeded = new ArrayList<String>();
final List<String> permissionsList = new ArrayList<String>();
if (!addPermission(permissionsList, Manifest.permission.CAMERA, fragment.getActivity()))
permissionsNeeded.add("CAMERA");
if (!addPermission(permissionsList, Manifest.permission.WRITE_EXTERNAL_STORAGE, fragment.getActivity()))
permissionsNeeded.add("WRITE_EXTERNAL_STORAGE");
if (permissionsList.size() > 0) {
fragment.requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
} else {
workFinish.onWorkFinish(true);
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions pix/src/main/java/com/fxn/utility/Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ public static void vibe(Context c, long l) {
((Vibrator) c.getSystemService(Context.VIBRATOR_SERVICE)).vibrate(l);
}

public static File writeImage(Bitmap bitmap) {
File dir = new File(Environment.getExternalStorageDirectory(), "/DCIM/Camera");
public static File writeImage(Bitmap bitmap, String path) {
File dir = new File(Environment.getExternalStorageDirectory(), path);
if (!dir.exists())
dir.mkdir();
File photo = new File(dir, "IMG_" + new SimpleDateFormat("yyyyMMdd_HHmmSS", Locale.ENGLISH).format(new Date()) + ".jpg");
Expand Down

0 comments on commit 9f317d6

Please sign in to comment.