|
@@ -0,0 +1,261 @@
|
|
|
+package com.bkgrands.fpmu.activities;
|
|
|
+
|
|
|
+import android.Manifest;
|
|
|
+import android.content.Context;
|
|
|
+import android.content.DialogInterface;
|
|
|
+import android.content.Intent;
|
|
|
+import android.database.Cursor;
|
|
|
+import android.graphics.Bitmap;
|
|
|
+import android.graphics.BitmapFactory;
|
|
|
+import android.graphics.Color;
|
|
|
+import android.graphics.drawable.BitmapDrawable;
|
|
|
+import android.graphics.drawable.Drawable;
|
|
|
+import android.media.MediaScannerConnection;
|
|
|
+import android.net.Uri;
|
|
|
+import android.os.Environment;
|
|
|
+import android.provider.MediaStore;
|
|
|
+import android.support.v7.app.AlertDialog;
|
|
|
+import android.support.v7.app.AppCompatActivity;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.util.Log;
|
|
|
+import android.view.KeyEvent;
|
|
|
+import android.view.MotionEvent;
|
|
|
+import android.view.View;
|
|
|
+import android.webkit.PermissionRequest;
|
|
|
+import android.widget.Button;
|
|
|
+import android.widget.ImageButton;
|
|
|
+import android.widget.ImageView;
|
|
|
+import android.widget.Toast;
|
|
|
+
|
|
|
+import com.bkgrands.fpmu.R;
|
|
|
+import com.karumi.dexter.Dexter;
|
|
|
+import com.karumi.dexter.MultiplePermissionsReport;
|
|
|
+import com.karumi.dexter.PermissionToken;
|
|
|
+import com.karumi.dexter.listener.DexterError;
|
|
|
+import com.karumi.dexter.listener.PermissionRequestErrorListener;
|
|
|
+import com.karumi.dexter.listener.multi.MultiplePermissionsListener;
|
|
|
+
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+public class ProgressActivity extends AppCompatActivity {
|
|
|
+ Button wait, start, done;
|
|
|
+ ImageButton capture;
|
|
|
+ ImageView shot;
|
|
|
+ private Context mContext;
|
|
|
+ private int GALLERY = 1, CAMERA = 2;
|
|
|
+ private static final String IMAGE_DIRECTORY = "/YourDirectName";
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onCreate(Bundle savedInstanceState) {
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
+ setContentView(R.layout.activity_progress);
|
|
|
+ wait = (Button) findViewById(R.id.waiting);
|
|
|
+ start = (Button) findViewById(R.id.notyet);
|
|
|
+ done = (Button) findViewById(R.id.done);
|
|
|
+ capture=(ImageButton)findViewById(R.id.capture);
|
|
|
+ shot=(ImageView)findViewById(R.id.shot);
|
|
|
+
|
|
|
+
|
|
|
+ requestMultiplePermissions();
|
|
|
+
|
|
|
+ start.setOnTouchListener(new View.OnTouchListener() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean onTouch(View v, MotionEvent event) {
|
|
|
+ // TODO Auto-generated method stub
|
|
|
+ if (event.getAction() == KeyEvent.ACTION_DOWN) {
|
|
|
+ start.setBackgroundColor(Color.RED);
|
|
|
+
|
|
|
+ } else if (event.getAction() == KeyEvent.ACTION_UP) {
|
|
|
+ wait.setTextColor(Color.BLACK);
|
|
|
+ wait.setBackground(getResources().getDrawable(R.drawable.yellow));
|
|
|
+ done.setTextColor(Color.BLACK);
|
|
|
+ done.setBackground(getResources().getDrawable(R.drawable.green));
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ //Change the button background color from black to red on touch and red to black on release
|
|
|
+
|
|
|
+ wait.setOnTouchListener(new View.OnTouchListener() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean onTouch(View v, MotionEvent event) {
|
|
|
+ // TODO Auto-generated method stub
|
|
|
+ if (event.getAction() == KeyEvent.ACTION_DOWN) {
|
|
|
+ wait.setBackgroundColor(Color.YELLOW);
|
|
|
+
|
|
|
+ } else if (event.getAction() == KeyEvent.ACTION_UP) {
|
|
|
+ start.setTextColor(Color.BLACK);
|
|
|
+ start.setBackground(getResources().getDrawable(R.drawable.red));
|
|
|
+ done.setTextColor(Color.BLACK);
|
|
|
+ done.setBackground(getResources().getDrawable(R.drawable.green));
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+ done.setOnTouchListener(new View.OnTouchListener() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean onTouch(View v, MotionEvent event) {
|
|
|
+ // TODO Auto-generated method stub
|
|
|
+ if (event.getAction() == KeyEvent.ACTION_DOWN) {
|
|
|
+ done.setBackgroundColor(Color.GREEN);
|
|
|
+
|
|
|
+ } else if (event.getAction() == KeyEvent.ACTION_UP) {
|
|
|
+ start.setTextColor(Color.BLACK);
|
|
|
+ start.setBackground(getResources().getDrawable(R.drawable.red));
|
|
|
+ wait.setTextColor(Color.BLACK);
|
|
|
+ wait.setBackground(getResources().getDrawable(R.drawable.yellow));
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ capture.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View v) {
|
|
|
+ showPictureDialog();
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void showPictureDialog() {
|
|
|
+ AlertDialog.Builder pictureDialog = new AlertDialog.Builder(this);
|
|
|
+ pictureDialog.setTitle("Select Action");
|
|
|
+ String[] pictureDialogItems = {"Select photo from gallery", "Capture photo from camera"};
|
|
|
+ pictureDialog.setItems(pictureDialogItems,
|
|
|
+ new DialogInterface.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(DialogInterface dialog, int which) {
|
|
|
+ switch (which) {
|
|
|
+ case 0:
|
|
|
+ choosePhotoFromGallary();
|
|
|
+ break;
|
|
|
+ case 1:
|
|
|
+ takePhotoFromCamera();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ pictureDialog.show();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void choosePhotoFromGallary() {
|
|
|
+ Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
|
|
|
+ startActivityForResult(galleryIntent, GALLERY);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void takePhotoFromCamera() {
|
|
|
+ Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
|
|
|
+ startActivityForResult(intent, CAMERA);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
+
|
|
|
+ super.onActivityResult(requestCode, resultCode, data);
|
|
|
+ if (resultCode == this.RESULT_CANCELED) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (requestCode == GALLERY) {
|
|
|
+ if (data != null) {
|
|
|
+ Uri contentURI = data.getData();
|
|
|
+ try {
|
|
|
+ Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), contentURI);
|
|
|
+ String path = saveImage(bitmap);
|
|
|
+ Toast.makeText(getApplicationContext(), "Image Saved!", Toast.LENGTH_SHORT).show();
|
|
|
+ shot.setImageBitmap(bitmap);
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ Toast.makeText(getApplicationContext(), "Failed!", Toast.LENGTH_SHORT).show();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } else if (requestCode == CAMERA) {
|
|
|
+ Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
|
|
|
+ shot.setImageBitmap(thumbnail);
|
|
|
+ saveImage(thumbnail);
|
|
|
+ Toast.makeText(getApplicationContext(), "Image Saved!", Toast.LENGTH_SHORT).show();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public String saveImage(Bitmap myBitmap) {
|
|
|
+ ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
|
|
+ myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
|
|
|
+ File wallpaperDirectory = new File(Environment.getExternalStorageDirectory() + IMAGE_DIRECTORY);
|
|
|
+ if (!wallpaperDirectory.exists()) { // have the object build the directory structure, if needed.
|
|
|
+ wallpaperDirectory.mkdirs();
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ File f = new File(wallpaperDirectory, Calendar.getInstance().getTimeInMillis() + ".jpg");
|
|
|
+ f.createNewFile();
|
|
|
+ FileOutputStream fo = new FileOutputStream(f);
|
|
|
+ fo.write(bytes.toByteArray());
|
|
|
+ MediaScannerConnection.scanFile(this,
|
|
|
+ new String[]{f.getPath()},
|
|
|
+ new String[]{"image/jpeg"}, null);
|
|
|
+ fo.close();
|
|
|
+ Log.d("TAG", "File Saved::--->" + f.getAbsolutePath());
|
|
|
+
|
|
|
+ return f.getAbsolutePath();
|
|
|
+ } catch (IOException e1) {
|
|
|
+ e1.printStackTrace();
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ private void requestMultiplePermissions() {
|
|
|
+ Dexter.withActivity(this)
|
|
|
+ .withPermissions(
|
|
|
+ Manifest.permission.CAMERA,
|
|
|
+ Manifest.permission.WRITE_EXTERNAL_STORAGE,
|
|
|
+ Manifest.permission.READ_EXTERNAL_STORAGE)
|
|
|
+ .withListener(new MultiplePermissionsListener() {
|
|
|
+ @Override
|
|
|
+ public void onPermissionsChecked(MultiplePermissionsReport report) {
|
|
|
+ if (report.areAllPermissionsGranted()) { // check if all permissions are granted
|
|
|
+ Toast.makeText(getApplicationContext(), "All permissions are granted by user!", Toast.LENGTH_SHORT).show();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (report.isAnyPermissionPermanentlyDenied()) { // check for permanent denial of any permission
|
|
|
+ // show alert dialog navigating to Settings
|
|
|
+ //openSettingsDialog();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onPermissionRationaleShouldBeShown(List<com.karumi.dexter.listener.PermissionRequest> permissions, PermissionToken token) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+// @Override
|
|
|
+// public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
|
|
|
+// token.continuePermissionRequest();
|
|
|
+// }
|
|
|
+ }).
|
|
|
+ withErrorListener(new PermissionRequestErrorListener() {
|
|
|
+ @Override
|
|
|
+ public void onError(DexterError error) {
|
|
|
+ Toast.makeText(getApplicationContext(), "Some Error! ", Toast.LENGTH_SHORT).show();
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .onSameThread()
|
|
|
+ .check();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|