diff --git a/src/main/java/com/todoroo/astrid/actfm/ActFmCameraModule.java b/src/main/java/com/todoroo/astrid/actfm/ActFmCameraModule.java index 431b9fdf3..1659516ab 100644 --- a/src/main/java/com/todoroo/astrid/actfm/ActFmCameraModule.java +++ b/src/main/java/com/todoroo/astrid/actfm/ActFmCameraModule.java @@ -21,6 +21,7 @@ import android.widget.Toast; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.tasks.R; +import org.tasks.preferences.DeviceInfo; import org.tasks.preferences.Preferences; import java.io.File; @@ -28,6 +29,8 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; +import java.util.List; +import java.util.Map; import java.util.concurrent.atomic.AtomicReference; import javax.inject.Inject; @@ -43,64 +46,85 @@ public class ActFmCameraModule { private final Fragment fragment; private final Preferences preferences; + private DeviceInfo deviceInfo; public interface ClearImageCallback { void clearImage(); } @Inject - public ActFmCameraModule(Fragment fragment, Preferences preferences) { + public ActFmCameraModule(Fragment fragment, Preferences preferences, DeviceInfo deviceInfo) { this.fragment = fragment; this.preferences = preferences; + this.deviceInfo = deviceInfo; } public void showPictureLauncher(final ClearImageCallback clearImageOption) { - ArrayList options = new ArrayList<>(); - - final Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); - PackageManager pm = fragment.getActivity().getPackageManager(); - final boolean cameraAvailable = pm.queryIntentActivities(cameraIntent, 0).size() > 0; - if(cameraAvailable) { - options.add(fragment.getString(R.string.actfm_picture_camera)); - } - - options.add(fragment.getString(R.string.actfm_picture_gallery)); - - if (clearImageOption != null) { - options.add(fragment.getString(R.string.actfm_picture_clear)); - } - - ArrayAdapter adapter = new ArrayAdapter<>(fragment.getActivity(), - android.R.layout.simple_spinner_dropdown_item, options.toArray(new String[options.size()])); - - DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface d, int which) { - if(which == 0 && cameraAvailable) { + final List runnables = new ArrayList<>(); + List options = new ArrayList<>(); + + final boolean cameraAvailable = deviceInfo.hasCamera(); + if (cameraAvailable) { + runnables.add(new Runnable() { + @Override + public void run() { lastTempFile = getFilename(".jpeg"); if (lastTempFile == null) { Toast.makeText(fragment.getActivity(), R.string.external_storage_unavailable, Toast.LENGTH_LONG).show(); - d.dismiss(); } else { + final Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(lastTempFile)); fragment.startActivityForResult(cameraIntent, REQUEST_CODE_CAMERA); } - } else if ((which == 1 && cameraAvailable) || (which == 0 && !cameraAvailable)) { - Intent intent = new Intent(Intent.ACTION_GET_CONTENT); - intent.setType("image/*"); - fragment.startActivityForResult(new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI), REQUEST_CODE_PICTURE); - } else { - if (clearImageOption != null) { - clearImageOption.clearImage(); + } + }); + options.add(fragment.getString(R.string.actfm_picture_camera)); + } + + if (deviceInfo.hasGallery()) { + runnables.add(new Runnable() { + @Override + public void run() { + Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI) {{ + setType("image/*"); + }}; + if (intent.resolveActivity(fragment.getActivity().getPackageManager()) != null) { + fragment.startActivityForResult(intent, REQUEST_CODE_PICTURE); } } - } - }; + }); + options.add(fragment.getString(R.string.actfm_picture_gallery)); + } + + if (clearImageOption != null) { + runnables.add(new Runnable() { + @Override + public void run() { + clearImageOption.clearImage(); + } + }); + options.add(fragment.getString(R.string.actfm_picture_clear)); + } - // show a menu of available options - new AlertDialog.Builder(fragment.getActivity()) - .setAdapter(adapter, listener) - .show().setOwnerActivity(fragment.getActivity()); + if (runnables.size() == 1) { + runnables.get(0).run(); + } else { + ArrayAdapter adapter = new ArrayAdapter<>(fragment.getActivity(), + android.R.layout.simple_spinner_dropdown_item, options.toArray(new String[options.size()])); + + DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface d, int which) { + runnables.get(which).run(); + d.dismiss(); + } + }; + + // show a menu of available options + new AlertDialog.Builder(fragment.getActivity()) + .setAdapter(adapter, listener) + .show().setOwnerActivity(fragment.getActivity()); + } } private File getFilename(String extension) { diff --git a/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.java b/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.java index b4911b5d5..2fc8eb4eb 100755 --- a/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.java +++ b/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.java @@ -82,6 +82,7 @@ import org.tasks.injection.InjectingFragment; import org.tasks.location.GeofenceService; import org.tasks.notifications.NotificationManager; import org.tasks.preferences.ActivityPreferences; +import org.tasks.preferences.DeviceInfo; import org.tasks.preferences.ResourceResolver; import java.io.File; @@ -174,6 +175,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener { @Inject ActFmCameraModule actFmCameraModule; @Inject GeofenceService geofenceService; @Inject ResourceResolver resourceResolver; + @Inject DeviceInfo deviceInfo; // --- UI components @@ -712,31 +714,48 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener { R.string.DLG_confirm_title).setMessage( R.string.DLG_delete_this_task_question).setIcon( android.R.drawable.ic_dialog_alert).setPositiveButton( - android.R.string.ok, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - TimerPlugin.updateTimer(notificationManager, taskService, getActivity(), model, false); - taskDeleter.delete(model); - shouldSaveState = false; - - Activity a = getActivity(); - if (a instanceof TaskEditActivity) { - getActivity().setResult(Activity.RESULT_OK); - getActivity().onBackPressed(); - } else if (a instanceof TaskListActivity) { - discardButtonClick(); - TaskListFragment tlf = ((TaskListActivity) a).getTaskListFragment(); - if (tlf != null) { - tlf.refresh(); - } - } - } - }).setNegativeButton(android.R.string.cancel, null).show(); + android.R.string.ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + TimerPlugin.updateTimer(notificationManager, taskService, getActivity(), model, false); + taskDeleter.delete(model); + shouldSaveState = false; + + Activity a = getActivity(); + if (a instanceof TaskEditActivity) { + getActivity().setResult(Activity.RESULT_OK); + getActivity().onBackPressed(); + } else if (a instanceof TaskListActivity) { + discardButtonClick(); + TaskListFragment tlf = ((TaskListActivity) a).getTaskListFragment(); + if (tlf != null) { + tlf.refresh(); + } + } + } + }).setNegativeButton(android.R.string.cancel, null).show(); } private void startAttachFile() { - ArrayList options = new ArrayList<>(); - options.add(getString(R.string.file_add_picture)); + final List runnables = new ArrayList<>(); + List options = new ArrayList<>(); + + if (deviceInfo.hasCamera() || deviceInfo.hasGallery()) { + runnables.add(new Runnable() { + @Override + public void run() { + actFmCameraModule.showPictureLauncher(null); + } + }); + options.add(getString(R.string.file_add_picture)); + } + runnables.add(new Runnable() { + @Override + public void run() { + Intent attachFile = new Intent(getActivity(), FileExplore.class); + startActivityForResult(attachFile, REQUEST_CODE_ATTACH_FILE); + } + }); options.add(getString(R.string.file_add_sdcard)); ArrayAdapter adapter = new ArrayAdapter<>(getActivity(), @@ -745,12 +764,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener { DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface d, int which) { - if(which == 0) { - actFmCameraModule.showPictureLauncher(null); - } else if (which == 1) { - Intent attachFile = new Intent(getActivity(), FileExplore.class); - startActivityForResult(attachFile, REQUEST_CODE_ATTACH_FILE); - } + runnables.get(which).run(); } }; diff --git a/src/main/java/org/tasks/preferences/DeviceInfo.java b/src/main/java/org/tasks/preferences/DeviceInfo.java index 33b3c9b4f..e4926a210 100644 --- a/src/main/java/org/tasks/preferences/DeviceInfo.java +++ b/src/main/java/org/tasks/preferences/DeviceInfo.java @@ -1,9 +1,11 @@ package org.tasks.preferences; import android.content.Context; +import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.os.Build; +import android.provider.MediaStore; import com.google.api.client.repackaged.com.google.common.base.Joiner; @@ -42,6 +44,16 @@ public class DeviceInfo { return isPlayStoreAvailable; } + public boolean hasCamera() { + return context.getPackageManager().queryIntentActivities(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), 0).size() > 0; + } + + public boolean hasGallery() { + return new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI) {{ + setType("image/*"); + }}.resolveActivity(context.getPackageManager()) != null; + } + public boolean supportsBilling() { return context.getResources().getBoolean(R.bool.billing_enabled) && isPlayStoreAvailable(); }