Updating attachment dialogs

pull/281/head
Alex Baker 9 years ago
parent 1583a7f12f
commit bbc4bf538d

@ -21,6 +21,7 @@ import android.widget.Toast;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.tasks.R; import org.tasks.R;
import org.tasks.preferences.DeviceInfo;
import org.tasks.preferences.Preferences; import org.tasks.preferences.Preferences;
import java.io.File; import java.io.File;
@ -28,6 +29,8 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import javax.inject.Inject; import javax.inject.Inject;
@ -43,64 +46,85 @@ public class ActFmCameraModule {
private final Fragment fragment; private final Fragment fragment;
private final Preferences preferences; private final Preferences preferences;
private DeviceInfo deviceInfo;
public interface ClearImageCallback { public interface ClearImageCallback {
void clearImage(); void clearImage();
} }
@Inject @Inject
public ActFmCameraModule(Fragment fragment, Preferences preferences) { public ActFmCameraModule(Fragment fragment, Preferences preferences, DeviceInfo deviceInfo) {
this.fragment = fragment; this.fragment = fragment;
this.preferences = preferences; this.preferences = preferences;
this.deviceInfo = deviceInfo;
} }
public void showPictureLauncher(final ClearImageCallback clearImageOption) { public void showPictureLauncher(final ClearImageCallback clearImageOption) {
ArrayList<String> options = new ArrayList<>(); final List<Runnable> runnables = new ArrayList<>();
List<String> options = new ArrayList<>();
final Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
PackageManager pm = fragment.getActivity().getPackageManager(); final boolean cameraAvailable = deviceInfo.hasCamera();
final boolean cameraAvailable = pm.queryIntentActivities(cameraIntent, 0).size() > 0; if (cameraAvailable) {
if(cameraAvailable) { runnables.add(new Runnable() {
options.add(fragment.getString(R.string.actfm_picture_camera)); @Override
} public void run() {
options.add(fragment.getString(R.string.actfm_picture_gallery));
if (clearImageOption != null) {
options.add(fragment.getString(R.string.actfm_picture_clear));
}
ArrayAdapter<String> 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) {
lastTempFile = getFilename(".jpeg"); lastTempFile = getFilename(".jpeg");
if (lastTempFile == null) { if (lastTempFile == null) {
Toast.makeText(fragment.getActivity(), R.string.external_storage_unavailable, Toast.LENGTH_LONG).show(); Toast.makeText(fragment.getActivity(), R.string.external_storage_unavailable, Toast.LENGTH_LONG).show();
d.dismiss();
} else { } else {
final Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(lastTempFile)); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(lastTempFile));
fragment.startActivityForResult(cameraIntent, REQUEST_CODE_CAMERA); 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/*"); options.add(fragment.getString(R.string.actfm_picture_camera));
fragment.startActivityForResult(new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI), REQUEST_CODE_PICTURE); }
} else {
if (clearImageOption != null) { if (deviceInfo.hasGallery()) {
clearImageOption.clearImage(); 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 if (runnables.size() == 1) {
new AlertDialog.Builder(fragment.getActivity()) runnables.get(0).run();
.setAdapter(adapter, listener) } else {
.show().setOwnerActivity(fragment.getActivity()); ArrayAdapter<String> 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) { private File getFilename(String extension) {

@ -82,6 +82,7 @@ import org.tasks.injection.InjectingFragment;
import org.tasks.location.GeofenceService; import org.tasks.location.GeofenceService;
import org.tasks.notifications.NotificationManager; import org.tasks.notifications.NotificationManager;
import org.tasks.preferences.ActivityPreferences; import org.tasks.preferences.ActivityPreferences;
import org.tasks.preferences.DeviceInfo;
import org.tasks.preferences.ResourceResolver; import org.tasks.preferences.ResourceResolver;
import java.io.File; import java.io.File;
@ -174,6 +175,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener {
@Inject ActFmCameraModule actFmCameraModule; @Inject ActFmCameraModule actFmCameraModule;
@Inject GeofenceService geofenceService; @Inject GeofenceService geofenceService;
@Inject ResourceResolver resourceResolver; @Inject ResourceResolver resourceResolver;
@Inject DeviceInfo deviceInfo;
// --- UI components // --- UI components
@ -712,31 +714,48 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener {
R.string.DLG_confirm_title).setMessage( R.string.DLG_confirm_title).setMessage(
R.string.DLG_delete_this_task_question).setIcon( R.string.DLG_delete_this_task_question).setIcon(
android.R.drawable.ic_dialog_alert).setPositiveButton( android.R.drawable.ic_dialog_alert).setPositiveButton(
android.R.string.ok, new DialogInterface.OnClickListener() { android.R.string.ok, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
TimerPlugin.updateTimer(notificationManager, taskService, getActivity(), model, false); TimerPlugin.updateTimer(notificationManager, taskService, getActivity(), model, false);
taskDeleter.delete(model); taskDeleter.delete(model);
shouldSaveState = false; shouldSaveState = false;
Activity a = getActivity(); Activity a = getActivity();
if (a instanceof TaskEditActivity) { if (a instanceof TaskEditActivity) {
getActivity().setResult(Activity.RESULT_OK); getActivity().setResult(Activity.RESULT_OK);
getActivity().onBackPressed(); getActivity().onBackPressed();
} else if (a instanceof TaskListActivity) { } else if (a instanceof TaskListActivity) {
discardButtonClick(); discardButtonClick();
TaskListFragment tlf = ((TaskListActivity) a).getTaskListFragment(); TaskListFragment tlf = ((TaskListActivity) a).getTaskListFragment();
if (tlf != null) { if (tlf != null) {
tlf.refresh(); tlf.refresh();
} }
} }
} }
}).setNegativeButton(android.R.string.cancel, null).show(); }).setNegativeButton(android.R.string.cancel, null).show();
} }
private void startAttachFile() { private void startAttachFile() {
ArrayList<String> options = new ArrayList<>(); final List<Runnable> runnables = new ArrayList<>();
options.add(getString(R.string.file_add_picture)); List<String> 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)); options.add(getString(R.string.file_add_sdcard));
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
@ -745,12 +764,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener {
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface d, int which) { public void onClick(DialogInterface d, int which) {
if(which == 0) { runnables.get(which).run();
actFmCameraModule.showPictureLauncher(null);
} else if (which == 1) {
Intent attachFile = new Intent(getActivity(), FileExplore.class);
startActivityForResult(attachFile, REQUEST_CODE_ATTACH_FILE);
}
} }
}; };

@ -1,9 +1,11 @@
package org.tasks.preferences; package org.tasks.preferences;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Build; import android.os.Build;
import android.provider.MediaStore;
import com.google.api.client.repackaged.com.google.common.base.Joiner; import com.google.api.client.repackaged.com.google.common.base.Joiner;
@ -42,6 +44,16 @@ public class DeviceInfo {
return isPlayStoreAvailable; 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() { public boolean supportsBilling() {
return context.getResources().getBoolean(R.bool.billing_enabled) && isPlayStoreAvailable(); return context.getResources().getBoolean(R.bool.billing_enabled) && isPlayStoreAvailable();
} }

Loading…
Cancel
Save