Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert Pictures https://github.com/autyzm-pg/friendly-plans/issues/145 #189

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Friendly-plans/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
}
}

android {
compileSdkVersion 24
buildToolsVersion '24.0.2'
buildToolsVersion '25.0.0'

defaultConfig {
applicationId "pg.autyzm.friendly_plans"
Expand Down Expand Up @@ -51,7 +51,7 @@ greendao {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'org.greenrobot:greendao:3.2.0'
compile 'org.greenrobot:greendao:3.2.2'
compile 'com.android.support:appcompat-v7:24.2.0'
compile "com.google.dagger:dagger:2.9"
compile 'com.nbsp:library:1.2'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package pg.autyzm.friendly_plans;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
Expand All @@ -21,7 +28,11 @@
import com.squareup.picasso.Picasso;
import database.repository.AssetRepository;
import database.repository.TaskTemplateRepository;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.inject.Inject;
import pg.autyzm.friendly_plans.asset.AssetType;
Expand All @@ -31,10 +42,13 @@
import pg.autyzm.friendly_plans.validation.TaskValidation;
import pg.autyzm.friendly_plans.validation.Utils;


public class TaskCreateFragment extends Fragment {

private static final String REGEX_TRIM_NAME = "_([\\d]*)(?=\\.)";
private static final String TASK_ID = "task_id";
private static final int REQUEST_CAMERA =0 ;
private static final int SELECT_FILE = 1;

@Inject
public FilePickerProxy filePickerProxy;
Expand Down Expand Up @@ -65,6 +79,7 @@ public class TaskCreateFragment extends Fragment {
private ImageView playSoundIcon;
private Long pictureId;
private Long soundId;
String userChoosenTask;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Expand Down Expand Up @@ -108,7 +123,29 @@ private void registerViews(View view) {

selectPicture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
filePickerProxy.openFilePicker(TaskCreateFragment.this, AssetType.PICTURE);
final CharSequence[] items = {"Take Photo", "Choose from Library",
"Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
boolean result = pg.autyzm.friendly_plans.Utils.checkPermission(getActivity());
if (items[item].equals("Take Photo")) {
userChoosenTask="Take Photo";
if (result)
cameraIntent();
} else if (items[item].equals("Choose from Library")) {
userChoosenTask="Choose from Library";
if (result)
galleryIntent();
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();

}
});

Expand Down Expand Up @@ -196,24 +233,13 @@ private void showStepsList(long taskId) {
transaction.commit();
}


@Override
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the issue why build is failing. Override is not needed here.

private void showMainMenu() {
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (filePickerProxy.isFilePicked(resultCode)) {
if (filePickerProxy.isPickFileRequested(requestCode, AssetType.PICTURE)) {
handleAssetSelecting(data, AssetType.PICTURE);
} else if (filePickerProxy.isPickFileRequested(requestCode, AssetType.SOUND)) {
handleAssetSelecting(data, AssetType.SOUND);
}
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handleAssetSelecting is not used now, but it was responsible for storing asset path in db.

private void handleAssetSelecting(Intent data, AssetType assetType) {
Context context = getActivity().getApplicationContext();
String filePath = filePickerProxy.getFilePath(data);
Expand Down Expand Up @@ -312,5 +338,88 @@ private void showToastMessage(int resourceStringId) {
resourceStringId,
getActivity().getApplicationContext());
}

private void cameraIntent()
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
}

private void galleryIntent()
{
Intent intent = new Intent();
intent.setType("image/*");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please extract strings to final static.

intent.setAction(Intent.ACTION_GET_CONTENT);//
startActivityForResult(Intent.createChooser(intent, "Select File"),SELECT_FILE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == Activity.RESULT_OK) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check if your ifs match our convention ->

 if () { 
...
}

if (requestCode == SELECT_FILE)
onSelectFromGalleryResult(data);
else if (requestCode == REQUEST_CAMERA)
onCaptureImageResult(data);
}


}

private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");

FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is good to show some alert, go back to previous state, etc. if something went wrong.

} catch (IOException e) {
e.printStackTrace();
}

picturePreview.setImageBitmap(thumbnail);
}

@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {

Bitmap bm=null;
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if something went wrong it would be worth to display alert - image not saved or something like this.

}
}

picturePreview.setImageBitmap(bm);
}


@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case pg.autyzm.friendly_plans.Utils.MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if(userChoosenTask.equals("Take Photo"))
cameraIntent();
else if(userChoosenTask.equals("Choose from Library"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be worth to change strings in userChoosenTask to be enum. Maybe enum with { CAMERA , GALLERY }

galleryIntent();
} else {
//code for deny
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for that moment else can be removed.

}
break;
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package pg.autyzm.friendly_plans;

/**
* Created by shreya on 24/10/17.
*/

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;

/**
* Created by shreya on 24/10/17.
*/

public class Utils {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename it to PremissionService

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is one more class named this way.

public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 123;
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static boolean checkPermission(final Context context)
{
int currentAPIVersion = Build.VERSION.SDK_INT;
if(currentAPIVersion>=android.os.Build.VERSION_CODES.M)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding dots in code, import this constants.

import android.os.Build.VERSION_CODES;
 ....
VERSION_CODES.M

{
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.READ_EXTERNAL_STORAGE)) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe extract this dialog builder to ( line 32 - 43 ) to separated private method name what this is doing -> showPermissionAlert() ?

alertBuilder.setCancelable(true);
alertBuilder.setTitle("Permission necessary");
alertBuilder.setMessage("External storage permission is necessary");
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
} else {
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
}
return false;
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this 'elses' are required?

return true;
}
} else {
return true;
}
}
}
4 changes: 3 additions & 1 deletion Friendly-plans/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions Friendly-plans/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Fri Mar 17 09:06:03 CET 2017
#Mon Oct 23 18:28:38 IST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip