-
Notifications
You must be signed in to change notification settings - Fork 37
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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, | ||
|
@@ -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(); | ||
|
||
} | ||
}); | ||
|
||
|
@@ -196,24 +233,13 @@ private void showStepsList(long taskId) { | |
transaction.commit(); | ||
} | ||
|
||
|
||
@Override | ||
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); | ||
} | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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/*"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please check if your ifs match our convention ->
|
||
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe rename it to PremissionService There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of adding dots in code, import this constants.
|
||
{ | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this 'elses' are required? |
||
return true; | ||
} | ||
} else { | ||
return true; | ||
} | ||
} | ||
} |
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 |
There was a problem hiding this comment.
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.