diff --git a/app/src/main/java/com/fxn/pixsample/MainActivity.java b/app/src/main/java/com/fxn/pixsample/MainActivity.java index 645933d..5655cfe 100644 --- a/app/src/main/java/com/fxn/pixsample/MainActivity.java +++ b/app/src/main/java/com/fxn/pixsample/MainActivity.java @@ -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; @@ -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))); } @@ -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. } } } diff --git a/pix/src/main/java/com/fxn/pix/Options.java b/pix/src/main/java/com/fxn/pix/Options.java new file mode 100644 index 0000000..3fc6966 --- /dev/null +++ b/pix/src/main/java/com/fxn/pix/Options.java @@ -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; + } + +} diff --git a/pix/src/main/java/com/fxn/pix/Pix.java b/pix/src/main/java/com/fxn/pix/Pix.java index 9338743..6c8f4da 100644 --- a/pix/src/main/java/com/fxn/pix/Pix.java +++ b/pix/src/main/java/com/fxn/pix/Pix.java @@ -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; @@ -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; @@ -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 @@ -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; } @@ -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; @@ -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() { @@ -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(); } @@ -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); @@ -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(), "")); @@ -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) { @@ -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); diff --git a/pix/src/main/java/com/fxn/utility/PermUtil.java b/pix/src/main/java/com/fxn/utility/PermUtil.java index 563b3d0..daf4ddb 100644 --- a/pix/src/main/java/com/fxn/utility/PermUtil.java +++ b/pix/src/main/java/com/fxn/utility/PermUtil.java @@ -50,35 +50,43 @@ private static boolean addPermission(List permissionsList, String permis return true; } - @RequiresApi(api = Build.VERSION_CODES.M) + public static void checkForCamaraWritePermissions(final FragmentActivity activity, WorkFinish workFinish) { - List permissionsNeeded = new ArrayList(); - final List permissionsList = new ArrayList(); - 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 permissionsNeeded = new ArrayList(); + final List permissionsList = new ArrayList(); + 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 permissionsNeeded = new ArrayList(); - final List permissionsList = new ArrayList(); - 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 permissionsNeeded = new ArrayList(); + final List permissionsList = new ArrayList(); + 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); + } } } diff --git a/pix/src/main/java/com/fxn/utility/Utility.java b/pix/src/main/java/com/fxn/utility/Utility.java index ac219a9..3f32ebe 100644 --- a/pix/src/main/java/com/fxn/utility/Utility.java +++ b/pix/src/main/java/com/fxn/utility/Utility.java @@ -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");