diff --git a/astrid/src/main/java/com/todoroo/astrid/actfm/ActFmCameraModule.java b/astrid/src/main/java/com/todoroo/astrid/actfm/ActFmCameraModule.java index e4fe5312d..068700413 100644 --- a/astrid/src/main/java/com/todoroo/astrid/actfm/ActFmCameraModule.java +++ b/astrid/src/main/java/com/todoroo/astrid/actfm/ActFmCameraModule.java @@ -29,6 +29,8 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.concurrent.atomic.AtomicReference; +import javax.inject.Inject; + import static com.todoroo.astrid.files.FileUtilities.getNewAttachmentPath; import static org.tasks.files.FileHelper.copyFile; @@ -41,11 +43,20 @@ public class ActFmCameraModule { private static File lastTempFile = null; + private final Fragment fragment; + private final Preferences preferences; + public interface ClearImageCallback { public void clearImage(); } - public static void showPictureLauncher(final Fragment fragment, final Preferences preferences, final ClearImageCallback clearImageOption) { + @Inject + public ActFmCameraModule(Fragment fragment, Preferences preferences) { + this.fragment = fragment; + this.preferences = preferences; + } + + public void showPictureLauncher(final ClearImageCallback clearImageOption) { ArrayList options = new ArrayList<>(); final Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); @@ -68,7 +79,7 @@ public class ActFmCameraModule { @Override public void onClick(DialogInterface d, int which) { if(which == 0 && cameraAvailable) { - lastTempFile = getFilename(fragment.getActivity(), preferences, ".jpeg"); + lastTempFile = getFilename(".jpeg"); if (lastTempFile == null) { Toast.makeText(fragment.getActivity(), R.string.external_storage_unavailable, Toast.LENGTH_LONG).show(); d.dismiss(); @@ -94,13 +105,13 @@ public class ActFmCameraModule { .show().setOwnerActivity(fragment.getActivity()); } - private static File getFilename(Activity activity, Preferences preferences, String extension) { + private File getFilename(String extension) { AtomicReference nameRef = new AtomicReference<>(); if (!extension.startsWith(".")) { extension = "." + extension; } try { - String path = getNewAttachmentPath(preferences, activity, extension, nameRef); + String path = getNewAttachmentPath(preferences, fragment.getActivity(), extension, nameRef); File file = new File(path); file.getParentFile().mkdirs(); if (!file.createNewFile()) { @@ -117,30 +128,30 @@ public class ActFmCameraModule { public void handleCameraResult(Uri uri); } - public static boolean activityResult(Activity activity, Preferences preferences, int requestCode, int resultCode, Intent data, + public boolean activityResult(int requestCode, int resultCode, Intent data, CameraResultCallback cameraResult) { if(requestCode == ActFmCameraModule.REQUEST_CODE_CAMERA && resultCode == Activity.RESULT_OK) { if (lastTempFile != null) { Uri uri = Uri.fromFile(lastTempFile); lastTempFile = null; - activity.setResult(Activity.RESULT_OK); + fragment.getActivity().setResult(Activity.RESULT_OK); cameraResult.handleCameraResult(uri); } return true; } else if(requestCode == ActFmCameraModule.REQUEST_CODE_PICTURE && resultCode == Activity.RESULT_OK) { Uri uri = data.getData(); - ContentResolver contentResolver = activity.getContentResolver(); + ContentResolver contentResolver = fragment.getActivity().getContentResolver(); MimeTypeMap mime = MimeTypeMap.getSingleton(); String extension = mime.getExtensionFromMimeType(contentResolver.getType(uri)); - File tempFile = getFilename(activity, preferences, extension); + File tempFile = getFilename(extension); log.debug("Writing {} to {}", uri, tempFile); try { - InputStream inputStream = activity.getContentResolver().openInputStream(uri); + InputStream inputStream = fragment.getActivity().getContentResolver().openInputStream(uri); copyFile(inputStream, tempFile.getPath()); } catch (IOException e) { throw new RuntimeException(e); } - activity.setResult(Activity.RESULT_OK); + fragment.getActivity().setResult(Activity.RESULT_OK); cameraResult.handleCameraResult(Uri.fromFile(tempFile)); return true; } diff --git a/astrid/src/main/java/com/todoroo/astrid/actfm/TagSettingsActivity.java b/astrid/src/main/java/com/todoroo/astrid/actfm/TagSettingsActivity.java index 239e7957f..2e7db8261 100644 --- a/astrid/src/main/java/com/todoroo/astrid/actfm/TagSettingsActivity.java +++ b/astrid/src/main/java/com/todoroo/astrid/actfm/TagSettingsActivity.java @@ -5,10 +5,8 @@ */ package com.todoroo.astrid.actfm; -import android.app.Activity; import android.content.Context; import android.content.Intent; -import android.net.Uri; import android.os.Bundle; import android.support.v7.app.ActionBar; import android.text.TextUtils; @@ -23,7 +21,6 @@ import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import com.todoroo.andlib.utility.AndroidUtilities; -import com.todoroo.astrid.actfm.ActFmCameraModule.CameraResultCallback; import com.todoroo.astrid.dao.TagDataDao; import com.todoroo.astrid.data.TagData; import com.todoroo.astrid.helper.UUIDHelper; @@ -50,8 +47,6 @@ public class TagSettingsActivity extends InjectingActionBarActivity { private static final int MENU_SAVE_ID = R.string.TEA_menu_save; private static final int MENU_DISCARD_ID = R.string.TEA_menu_discard_changes; - public static final int REQUEST_ACTFM_LOGIN = 3; - public static final String TOKEN_AUTOPOPULATE_MEMBERS = "autopopulateMembers"; //$NON-NLS-1$ public static final String TOKEN_AUTOPOPULATE_NAME = "autopopulateName"; //$NON-NLS-1$ @@ -217,23 +212,6 @@ public class TagSettingsActivity extends InjectingActionBarActivity { } } - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - CameraResultCallback callback = new CameraResultCallback() { - @Override - public void handleCameraResult(Uri uri) { - log.error("Not expecting this"); - } - }; - if (!ActFmCameraModule.activityResult(this, preferences, requestCode, resultCode, data, callback)) { - if(requestCode == REQUEST_ACTFM_LOGIN && resultCode == Activity.RESULT_OK) { - saveSettings(); - } else { - super.onActivityResult(requestCode, resultCode, data); - } - } - } - @Override public boolean onCreateOptionsMenu(Menu menu) { MenuItem item = menu.add(Menu.NONE, MENU_DISCARD_ID, 0, R.string.TEA_menu_discard_changes); diff --git a/astrid/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.java b/astrid/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.java index bb0ce880b..ede2c8a5e 100755 --- a/astrid/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.java +++ b/astrid/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.java @@ -86,7 +86,6 @@ import com.todoroo.astrid.voice.VoiceRecognizer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.tasks.R; -import org.tasks.injection.ForApplication; import org.tasks.injection.InjectingFragment; import org.tasks.notifications.NotificationManager; import org.tasks.preferences.ActivityPreferences; @@ -176,7 +175,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener { @Inject ActivityPreferences preferences; @Inject DateChangedAlerts dateChangedAlerts; @Inject TagDataDao tagDataDao; - @Inject @ForApplication Context context; + @Inject ActFmCameraModule actFmCameraModule; // --- UI components @@ -278,7 +277,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener { private void instantiateEditNotes() { if (showEditComments) { long idParam = getActivity().getIntent().getLongExtra(TOKEN_ID, -1L); - editNotes = new EditNoteActivity(preferences, metadataDao, userActivityDao, + editNotes = new EditNoteActivity(actFmCameraModule, preferences, metadataDao, userActivityDao, taskService, this, getView(), idParam); editNotes.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); @@ -820,7 +819,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener { @Override public void onClick(DialogInterface d, int which) { if(which == 0) { - ActFmCameraModule.showPictureLauncher(TaskEditFragment.this, preferences, null); + actFmCameraModule.showPictureLauncher(null); } else if (which == 1) { Intent attachFile = new Intent(getActivity(), FileExplore.class); startActivityForResult(attachFile, REQUEST_CODE_ATTACH_FILE); @@ -980,7 +979,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener { return; } - ActFmCameraModule.activityResult(getActivity(), preferences, requestCode, resultCode, data, new CameraResultCallback() { + actFmCameraModule.activityResult(requestCode, resultCode, data, new CameraResultCallback() { @Override public void handleCameraResult(Uri uri) { attachImage(uri); diff --git a/astrid/src/main/java/com/todoroo/astrid/notes/EditNoteActivity.java b/astrid/src/main/java/com/todoroo/astrid/notes/EditNoteActivity.java index 809b907c9..3ee9724bd 100644 --- a/astrid/src/main/java/com/todoroo/astrid/notes/EditNoteActivity.java +++ b/astrid/src/main/java/com/todoroo/astrid/notes/EditNoteActivity.java @@ -66,6 +66,7 @@ public class EditNoteActivity extends LinearLayout implements TimerActionListene private Task task; + private ActFmCameraModule actFmCameraModule; private final Preferences preferences; private final MetadataDao metadataDao; private final UserActivityDao userActivityDao; @@ -96,6 +97,7 @@ public class EditNoteActivity extends LinearLayout implements TimerActionListene } public EditNoteActivity( + ActFmCameraModule actFmCameraModule, Preferences preferences, MetadataDao metadataDao, UserActivityDao userActivityDao, @@ -104,6 +106,7 @@ public class EditNoteActivity extends LinearLayout implements TimerActionListene View parent, long t) { super(fragment.getActivity()); + this.actFmCameraModule = actFmCameraModule; this.preferences = preferences; this.metadataDao = metadataDao; this.userActivityDao = userActivityDao; @@ -229,9 +232,9 @@ public class EditNoteActivity extends LinearLayout implements TimerActionListene @Override public void onClick(View v) { if (pendingCommentPicture != null) { - ActFmCameraModule.showPictureLauncher(fragment, preferences, clearImage); + actFmCameraModule.showPictureLauncher(clearImage); } else { - ActFmCameraModule.showPictureLauncher(fragment, preferences, null); + actFmCameraModule.showPictureLauncher(null); } respondToPicture = true; } @@ -492,8 +495,7 @@ public class EditNoteActivity extends LinearLayout implements TimerActionListene } }; - return (ActFmCameraModule.activityResult((Activity)getContext(), preferences, - requestCode, resultCode, data, callback)); + return actFmCameraModule.activityResult(requestCode, resultCode, data, callback); } else { return false; } diff --git a/astrid/src/main/java/org/tasks/injection/FragmentModule.java b/astrid/src/main/java/org/tasks/injection/FragmentModule.java index 07e9e2f6b..b462c6e0c 100644 --- a/astrid/src/main/java/org/tasks/injection/FragmentModule.java +++ b/astrid/src/main/java/org/tasks/injection/FragmentModule.java @@ -2,6 +2,7 @@ package org.tasks.injection; import android.app.Activity; import android.content.Context; +import android.support.v4.app.Fragment; import com.todoroo.astrid.actfm.TagViewFragment; import com.todoroo.astrid.activity.FilterListFragment; @@ -40,11 +41,11 @@ import dagger.Provides; }) public class FragmentModule { - private final Activity activity; - private Injector injector; + private final Fragment fragment; + private final Injector injector; - public FragmentModule(Activity activity, Injector injector) { - this.activity = activity; + public FragmentModule(Fragment fragment, Injector injector) { + this.fragment = fragment; this.injector = injector; } @@ -54,16 +55,20 @@ public class FragmentModule { return injector; } - @Singleton @Provides @ForActivity public Context getContext() { - return activity; + return fragment.getActivity(); } - @Singleton @Provides public Activity getActivity() { - return activity; + return fragment.getActivity(); + } + + @Singleton + @Provides + public Fragment getFragment() { + return fragment; } } diff --git a/astrid/src/main/java/org/tasks/injection/InjectingFragment.java b/astrid/src/main/java/org/tasks/injection/InjectingFragment.java index b001cbcb6..f68c19ea3 100644 --- a/astrid/src/main/java/org/tasks/injection/InjectingFragment.java +++ b/astrid/src/main/java/org/tasks/injection/InjectingFragment.java @@ -15,7 +15,7 @@ public class InjectingFragment extends Fragment implements Injector { super.onAttach(activity); if (!injected) { - objectGraph = ((Injector) activity).getObjectGraph().plus(new FragmentModule(activity, this)); + objectGraph = ((Injector) activity).getObjectGraph().plus(new FragmentModule(this, this)); inject(this); injected = true; } diff --git a/astrid/src/main/java/org/tasks/injection/InjectingListFragment.java b/astrid/src/main/java/org/tasks/injection/InjectingListFragment.java index 6d9b89541..ae73746f0 100644 --- a/astrid/src/main/java/org/tasks/injection/InjectingListFragment.java +++ b/astrid/src/main/java/org/tasks/injection/InjectingListFragment.java @@ -15,7 +15,7 @@ public class InjectingListFragment extends ListFragment implements Injector { super.onAttach(activity); if (!injected) { - objectGraph = ((Injector) activity).getObjectGraph().plus(new FragmentModule(activity, this)); + objectGraph = ((Injector) activity).getObjectGraph().plus(new FragmentModule(this, this)); inject(this); injected = true; }