Inject more contexts

pull/189/head
Alex Baker 11 years ago
parent 5467e448db
commit 7b3be55752

@ -12,7 +12,11 @@ import com.todoroo.astrid.tags.TagService;
import com.todoroo.astrid.test.DatabaseTestCase;
import com.todoroo.astrid.utility.TitleParser;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.tasks.Freeze;
import org.tasks.R;
import org.tasks.Snippet;
import org.tasks.preferences.Preferences;
import java.util.ArrayList;
@ -36,6 +40,28 @@ public class TitleParserTest extends DatabaseTestCase {
preferences.setStringFromInteger(R.string.p_default_urgency_key, 0);
}
public void testTrimParenthesisWithDate() {
Freeze.freezeAt(new DateTime(2014, 6, 5, 14, 10, 14)).thawAfter(new Snippet() {{
Task task = new Task() {{
setTitle("Call mom (tomorrow)");
}};
TitleParser.parse(tagService, task, new ArrayList<String>());
assertEquals("Call mom", task.getTitle());
assertEquals(new DateTime(2014, 6, 6, 12, 0, 0, DateTimeZone.UTC).getMillis(), (long) task.getDueDate());
}});
}
public void disabled_testTrimParenthesisWithDateAndTime() {
Freeze.freezeAt(new DateTime(2014, 6, 5, 14, 10, 14)).thawAfter(new Snippet() {{
Task task = new Task() {{
setTitle("Call mom (tomorrow 9am)");
}};
TitleParser.parse(tagService, task, new ArrayList<String>());
assertEquals("Call mom", task.getTitle());
assertEquals(new DateTime(2014, 6, 6, 14, 0, 0, DateTimeZone.UTC).getMillis(), (long) task.getDueDate());
}});
}
/** test that completing a task w/ no regular expressions creates a simple task with no date, no repeat, no lists*/
public void testNoRegexes() throws Exception{
Task task = new Task();

@ -3,8 +3,6 @@ package com.todoroo.astrid.utility;
import android.annotation.SuppressLint;
import android.test.AndroidTestCase;
import com.todoroo.andlib.service.ContextManager;
import org.tasks.R;
import org.tasks.preferences.Preferences;
@ -37,7 +35,7 @@ public class AstridDefaultPreferenceSpecTest extends AndroidTestCase {
}
public void testQuietHoursDisabledAfterMigration() {
setString(R.string.p_rmd_quietStart_old, "");
preferences.setString(getContext().getString(R.string.p_rmd_quietStart_old), "");
spec.migrateToNewQuietHours();
@ -125,11 +123,4 @@ public class AstridDefaultPreferenceSpecTest extends AndroidTestCase {
private int newReminderTime() {
return preferences.getInt(R.string.p_rmd_time);
}
/**
* Sets string preference
*/
private void setString(int resourceId, String value) {
preferences.setString(ContextManager.getContext().getString(resourceId), value);
}
}

@ -30,6 +30,7 @@ import com.todoroo.astrid.service.ThemeService;
import com.todoroo.astrid.tags.TaskToTagMetadata;
import org.tasks.R;
import org.tasks.injection.ForApplication;
import org.tasks.injection.InjectingBroadcastReceiver;
import org.tasks.injection.Injector;
import org.tasks.preferences.Preferences;
@ -48,21 +49,22 @@ import javax.inject.Inject;
public final class CoreFilterExposer extends InjectingBroadcastReceiver implements AstridFilterExposer {
@Inject Preferences preferences;
@Inject @ForApplication Context context;
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Resources r = context.getResources();
ContextManager.setContext(context);
FilterListItem[] list = prepareFilters(r);
FilterListItem[] list = prepareFilters();
Intent broadcastIntent = new Intent(AstridApiConstants.BROADCAST_SEND_FILTERS);
broadcastIntent.putExtra(AstridApiConstants.EXTRAS_RESPONSE, list);
context.sendBroadcast(broadcastIntent, AstridApiConstants.PERMISSION_READ);
}
private FilterListItem[] prepareFilters(Resources r) {
private FilterListItem[] prepareFilters() {
Resources r = context.getResources();
// core filters
List<FilterListItem> filters = new ArrayList<>(3);
@ -122,14 +124,9 @@ public final class CoreFilterExposer extends InjectingBroadcastReceiver implemen
@Override
public FilterListItem[] getFilters(Injector injector) {
if (ContextManager.getContext() == null || ContextManager.getContext().getResources() == null) {
return null;
}
injector.inject(this);
Resources r = ContextManager.getContext().getResources();
return prepareFilters(r);
return prepareFilters();
}
}

@ -13,7 +13,6 @@ import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.sql.Order;
import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.sql.QueryTemplate;
@ -30,6 +29,7 @@ import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.service.ThemeService;
import org.tasks.R;
import org.tasks.injection.ForApplication;
import org.tasks.injection.InjectingActivity;
import org.tasks.injection.InjectingBroadcastReceiver;
import org.tasks.injection.Injector;
@ -52,12 +52,13 @@ public final class CustomFilterExposer extends InjectingBroadcastReceiver implem
@Inject StoreObjectDao storeObjectDao;
@Inject Preferences preferences;
@Inject @ForApplication Context context;
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
FilterListItem[] list = prepareFilters(context);
FilterListItem[] list = prepareFilters();
// transmit filter list
Intent broadcastIntent = new Intent(AstridApiConstants.BROADCAST_SEND_FILTERS);
@ -65,7 +66,7 @@ public final class CustomFilterExposer extends InjectingBroadcastReceiver implem
context.sendBroadcast(broadcastIntent, AstridApiConstants.PERMISSION_READ);
}
private FilterListItem[] prepareFilters(Context context) {
private FilterListItem[] prepareFilters() {
Resources r = context.getResources();
return buildSavedFilters(context, r);
@ -163,13 +164,9 @@ public final class CustomFilterExposer extends InjectingBroadcastReceiver implem
@Override
public FilterListItem[] getFilters(Injector injector) {
if (ContextManager.getContext() == null) {
return null;
}
injector.inject(this);
return prepareFilters(ContextManager.getContext());
return prepareFilters();
}
}

@ -7,6 +7,7 @@ package com.todoroo.astrid.gcal;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.CursorIndexOutOfBoundsException;
import android.net.Uri;
@ -14,13 +15,13 @@ import android.text.TextUtils;
import android.text.format.Time;
import android.util.Log;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.service.TaskService;
import org.tasks.R;
import org.tasks.injection.ForApplication;
import org.tasks.preferences.Preferences;
import java.util.TimeZone;
@ -33,11 +34,13 @@ public class GCalHelper {
public static final String CALENDAR_ID_COLUMN = "calendar_id"; //$NON-NLS-1$
private final Context context;
private final TaskService taskService;
private final Preferences preferences;
@Inject
public GCalHelper(TaskService taskService, Preferences preferences) {
public GCalHelper(@ForApplication Context context, TaskService taskService, Preferences preferences) {
this.context = context;
this.taskService = taskService;
this.preferences = preferences;
}
@ -68,7 +71,7 @@ public class GCalHelper {
boolean gcalCreateEventEnabled = preferences.getStringValue(R.string.gcal_p_default) != null
&& !preferences.getStringValue(R.string.gcal_p_default).equals("-1"); //$NON-NLS-1$
if (gcalCreateEventEnabled) {
ContentResolver cr = ContextManager.getContext().getContentResolver();
ContentResolver cr = context.getContentResolver();
Uri calendarUri = createTaskEvent(t, cr, new ContentValues(), deleteEventIfExists);
if (calendarUri != null) {
t.setCalendarUri(calendarUri.toString());
@ -170,7 +173,7 @@ public class GCalHelper {
Uri calendarUri = Uri.parse(uri);
// try to load calendar
ContentResolver cr = ContextManager.getContext().getContentResolver();
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(calendarUri, new String[] { "dtstart" }, null, null, null); //$NON-NLS-1$
try {
boolean alreadydeleted = cursor.getCount() == 0;

@ -34,6 +34,7 @@ import com.todoroo.astrid.data.StoreObject;
import com.todoroo.astrid.data.Task;
import org.tasks.R;
import org.tasks.injection.ForApplication;
import org.tasks.injection.InjectingBroadcastReceiver;
import org.tasks.injection.Injector;
@ -49,6 +50,7 @@ public class GtasksFilterExposer extends InjectingBroadcastReceiver implements A
@Inject GtasksListService gtasksListService;
@Inject GtasksPreferenceService gtasksPreferenceService;
@Inject @ForApplication Context context;
private StoreObject[] lists;
@ -81,14 +83,14 @@ public class GtasksFilterExposer extends InjectingBroadcastReceiver implements A
super.onReceive(context, intent);
ContextManager.setContext(context);
FilterListItem[] list = prepareFilters(context);
FilterListItem[] list = prepareFilters();
Intent broadcastIntent = new Intent(AstridApiConstants.BROADCAST_SEND_FILTERS);
broadcastIntent.putExtra(AstridApiConstants.EXTRAS_ADDON, GtasksPreferenceService.IDENTIFIER);
broadcastIntent.putExtra(AstridApiConstants.EXTRAS_RESPONSE, list);
context.sendBroadcast(broadcastIntent, AstridApiConstants.PERMISSION_READ);
}
private FilterListItem[] prepareFilters(Context context) {
private FilterListItem[] prepareFilters() {
// if we aren't logged in (or we are logged in to astrid.com), don't expose features
if(!gtasksPreferenceService.isLoggedIn()) {
return null;
@ -123,13 +125,9 @@ public class GtasksFilterExposer extends InjectingBroadcastReceiver implements A
@Override
public FilterListItem[] getFilters(Injector injector) {
if (ContextManager.getContext() == null) {
return null;
}
injector.inject(this);
return prepareFilters(ContextManager.getContext());
return prepareFilters();
}
}

@ -1,15 +1,16 @@
package com.todoroo.astrid.tags;
import android.content.Context;
import android.content.Intent;
import android.widget.EditText;
import android.widget.Toast;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.utility.DialogUtilities;
import com.todoroo.astrid.actfm.TagViewFragment;
import com.todoroo.astrid.api.AstridApiConstants;
import org.tasks.R;
import org.tasks.injection.ForApplication;
import javax.inject.Inject;
@ -18,6 +19,7 @@ public class RenameTagActivity extends TagActivity {
private EditText editor;
@Inject TagService tagService;
@Inject @ForApplication Context context;
@Override
protected void showDialog() {
@ -42,7 +44,7 @@ public class RenameTagActivity extends TagActivity {
if (renamed > 0) {
Intent intent = new Intent(AstridApiConstants.BROADCAST_EVENT_TAG_RENAMED);
intent.putExtra(TagViewFragment.EXTRA_TAG_UUID, uuid);
ContextManager.getContext().sendBroadcast(intent);
context.sendBroadcast(intent);
return intent;
}
return null;

@ -18,7 +18,6 @@ import android.text.TextUtils;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.sql.QueryTemplate;
import com.todoroo.andlib.utility.DialogUtilities;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.actfm.TagViewFragment;
import com.todoroo.astrid.api.AstridApiConstants;
@ -36,6 +35,7 @@ import com.todoroo.astrid.service.ThemeService;
import com.todoroo.astrid.tags.TagService.Tag;
import org.tasks.R;
import org.tasks.injection.ForApplication;
import org.tasks.injection.InjectingBroadcastReceiver;
import org.tasks.injection.Injector;
@ -55,6 +55,7 @@ public class TagFilterExposer extends InjectingBroadcastReceiver implements Astr
public static final String TAG = "tag"; //$NON-NLS-1$
@Inject TagService tagService;
@Inject @ForApplication Context context;
/** Create filter from new tag object */
public static FilterWithCustomIntent filterFromTag(Context context, Tag tag, Criterion criterion) {
@ -113,7 +114,7 @@ public class TagFilterExposer extends InjectingBroadcastReceiver implements Astr
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
FilterListItem[] listAsArray = prepareFilters(context);
FilterListItem[] listAsArray = prepareFilters();
Intent broadcastIntent = new Intent(AstridApiConstants.BROADCAST_SEND_FILTERS);
broadcastIntent.putExtra(AstridApiConstants.EXTRAS_RESPONSE, listAsArray);
@ -121,7 +122,7 @@ public class TagFilterExposer extends InjectingBroadcastReceiver implements Astr
context.sendBroadcast(broadcastIntent, AstridApiConstants.PERMISSION_READ);
}
private FilterListItem[] prepareFilters(Context context) {
private FilterListItem[] prepareFilters() {
ContextManager.setContext(context);
ArrayList<FilterListItem> list = new ArrayList<>();
@ -142,7 +143,6 @@ public class TagFilterExposer extends InjectingBroadcastReceiver implements Astr
ArrayList<Filter> filters = new ArrayList<>(tags.length);
Context context = ContextManager.getContext();
Resources r = context.getResources();
int themeFlags = ThemeService.getFilterThemeFlags();
@ -173,13 +173,9 @@ public class TagFilterExposer extends InjectingBroadcastReceiver implements Astr
@Override
public FilterListItem[] getFilters(Injector injector) {
if (ContextManager.getContext() == null) {
return null;
}
injector.inject(this);
return prepareFilters(ContextManager.getContext());
return prepareFilters();
}
}

@ -23,6 +23,7 @@ import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.service.TaskService;
import org.tasks.R;
import org.tasks.injection.ForApplication;
import org.tasks.injection.InjectingBroadcastReceiver;
import org.tasks.injection.Injector;
@ -37,20 +38,21 @@ import javax.inject.Inject;
public final class TimerFilterExposer extends InjectingBroadcastReceiver implements AstridFilterExposer {
@Inject TaskService taskService;
@Inject @ForApplication Context context;
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
ContextManager.setContext(context);
FilterListItem[] list = prepareFilters(context);
FilterListItem[] list = prepareFilters();
Intent broadcastIntent = new Intent(AstridApiConstants.BROADCAST_SEND_FILTERS);
broadcastIntent.putExtra(AstridApiConstants.EXTRAS_RESPONSE, list);
context.sendBroadcast(broadcastIntent, AstridApiConstants.PERMISSION_READ);
}
private FilterListItem[] prepareFilters(Context context) {
private FilterListItem[] prepareFilters() {
if(taskService.count(Query.select(Task.ID).
where(Task.TIMER_START.gt(0))) == 0) {
return null;
@ -78,12 +80,8 @@ public final class TimerFilterExposer extends InjectingBroadcastReceiver impleme
@Override
public FilterListItem[] getFilters(Injector injector) {
if (ContextManager.getContext() == null) {
return null;
}
injector.inject(this);
return prepareFilters(ContextManager.getContext());
return prepareFilters();
}
}

@ -5,7 +5,6 @@ import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.astrid.utility.AstridDefaultPreferenceSpec;
import org.tasks.injection.ForApplication;
@ -87,7 +86,7 @@ public class Preferences {
}
public void setString(int resourceId, String value) {
setString(ContextManager.getContext().getString(resourceId), value);
setString(context.getString(resourceId), value);
}
public void setString(String key, String newValue) {
@ -133,7 +132,7 @@ public class Preferences {
}
public int getInt(int resourceId, int defValue) {
return getInt(ContextManager.getContext().getString(resourceId), defValue);
return getInt(context.getString(resourceId), defValue);
}
public int getInt(String key, int defValue) {
@ -141,7 +140,7 @@ public class Preferences {
}
public void setInt(int resourceId, int value) {
setInt(ContextManager.getContext().getString(resourceId), value);
setInt(context.getString(resourceId), value);
}
public void setInt(String key, int value) {

Loading…
Cancel
Save