More preference migration

pull/189/head
Alex Baker 10 years ago
parent 5868a41fe4
commit 4fca2ef1eb

@ -24,7 +24,7 @@ public class Preferences {
private static SharedPreferences preferences = null;
/** Get preferences object from the context */
public static SharedPreferences getPrefs(Context context) {
private static SharedPreferences getPrefs(Context context) {
if(preferences != null) {
return preferences;
}
@ -55,15 +55,6 @@ public class Preferences {
return getPrefs(context).getString(key, null);
}
/** Gets an string value from a string preference. Returns null
* if the value is not set
* @return integer value, or null on error
*/
public static String getStringValue(int keyResource) {
Context context = ContextManager.getContext();
return getPrefs(context).getString(context.getResources().getString(keyResource), null);
}
/** Gets an integer value from a string preference. Returns null
* if the value is not set or not an integer.
*

@ -28,7 +28,6 @@ public final class ShareLinkActivity extends TaskListActivity {
@Inject TagService tagService;
@Inject MetadataService metadataService;
@Inject GCalHelper gcalHelper;
@Inject Preferences preferences;
private String subject;
private boolean handled;
@ -53,7 +52,7 @@ public final class ShareLinkActivity extends TaskListActivity {
if (!handled) {
Intent callerIntent = getIntent();
Task task = QuickAddBar.basicQuickAddTask(preferences, gcalHelper, taskService, metadataService, tagService, subject);
Task task = QuickAddBar.basicQuickAddTask(gcalHelper, taskService, metadataService, tagService, subject);
if (task != null) {
task.setNotes(callerIntent.getStringExtra(Intent.EXTRA_TEXT));
taskService.save(task);

@ -10,14 +10,18 @@ import android.content.res.Resources;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.util.Log;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.astrid.gcal.Calendars;
import com.todoroo.astrid.gcal.GCalHelper;
import com.todoroo.astrid.utility.TodorooPreferenceActivity;
import org.tasks.R;
import org.tasks.preferences.Preferences;
import java.util.ArrayList;
import java.util.Arrays;
import javax.inject.Inject;
/**
@ -29,6 +33,7 @@ import javax.inject.Inject;
public class DefaultsPreferences extends TodorooPreferenceActivity {
@Inject Preferences preferences;
@Inject GCalHelper calendarHelper;
@Override
public int getPreferenceResource() {
@ -39,8 +44,7 @@ public class DefaultsPreferences extends TodorooPreferenceActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListPreference defaultCalendarPreference = (ListPreference) findPreference(getString(R.string.gcal_p_default));
Calendars.initCalendarsPreference(this, defaultCalendarPreference);
initCalendarsPreference((ListPreference) findPreference(getString(R.string.gcal_p_default)));
}
@Override
@ -113,4 +117,61 @@ public class DefaultsPreferences extends TodorooPreferenceActivity {
}
}
/**
* Appends all user-modifiable calendars to listPreference.
*
* @param listPreference
* preference to init
*/
private void initCalendarsPreference(ListPreference listPreference) {
Resources r = getResources();
GCalHelper.CalendarResult calendars = calendarHelper.getCalendars();
// Fetch the current setting. Invalid calendar id will
// be changed to default value.
String currentSetting = preferences.getStringValue(R.string.gcal_p_default);
int currentSettingIndex = -1;
ArrayList<CharSequence> entries = new ArrayList<>();
entries.addAll(Arrays.asList(r.getStringArray(R.array.EPr_default_addtocalendar)));
entries.addAll(Arrays.asList(calendars.calendars));
ArrayList<CharSequence> entryValues = new ArrayList<>();
entryValues.addAll(Arrays.asList(r.getStringArray(R.array.EPr_default_addtocalendar_values)));
entryValues.addAll(Arrays.asList(calendars.calendarIds));
listPreference.setEntries(entries.toArray(new CharSequence[entries.size()]));
listPreference.setEntryValues(entryValues.toArray(new CharSequence[entryValues.size()]));
listPreference.setValueIndex(0);
listPreference.setEnabled(true);
if (calendars.calendarIds.length == 0 || calendars.calendars.length == 0) {
// Something went wrong when querying calendars
// Leave the preference at disabled.
return;
}
// Iterate calendars one by one, and fill up the list preference
if (currentSetting != null) {
for (int i=0; i<calendars.calendarIds.length; i++) {
// We found currently selected calendar
if (currentSetting.equals(calendars.calendarIds[i])) {
currentSettingIndex = i+1; // +1 correction for disabled-entry
break;
}
}
}
if(currentSettingIndex == -1 || currentSettingIndex > calendars.calendarIds.length+1) {
// Should not happen!
// Leave the preference at disabled.
Log.d("astrid", "initCalendarsPreference: Unknown calendar.");
currentSettingIndex = 0;
}
listPreference.setValueIndex(currentSettingIndex);
listPreference.setEnabled(true);
}
}

@ -5,24 +5,14 @@
*/
package com.todoroo.astrid.gcal;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.net.Uri;
import android.preference.ListPreference;
import android.provider.CalendarContract;
import android.util.Log;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.andlib.utility.Preferences;
import org.tasks.R;
import java.util.ArrayList;
import java.util.Arrays;
import javax.inject.Singleton;
@Singleton
public class Calendars {
public static final String CALENDAR_CONTENT_CALENDARS = "calendars";
@ -41,19 +31,18 @@ public class Calendars {
public static final String ATTENDEES_NAME_COL = (USE_ICS_NAMES ? CalendarContract.Attendees.ATTENDEE_NAME : "attendeeName");
public static final String ATTENDEES_EMAIL_COL = (USE_ICS_NAMES ? CalendarContract.Attendees.ATTENDEE_EMAIL: "attendeeEmail");
private static final String[] CALENDARS_PROJECTION = new String[] {
public static final String[] CALENDARS_PROJECTION = new String[] {
ID_COLUMN_NAME,
CALENDARS_DISPLAY_COL,
};
// Only show calendars that the user can modify. Access level 500
// corresponds to Calendars.CONTRIBUTOR_ACCESS
private static final String CALENDARS_WHERE = CALENDARS_ACCESS_LEVEL_COL + ">= 500";
public static final String CALENDARS_WHERE = CALENDARS_ACCESS_LEVEL_COL + ">= 500";
private static final String CALENDARS_SORT = CALENDARS_DISPLAY_COL + " ASC";
public static final String CALENDARS_SORT = CALENDARS_DISPLAY_COL + " ASC";
// --- api access
// --- api access
/** Return content uri for calendars
* @param table provider table, something like calendars, events
@ -81,153 +70,4 @@ public class Calendars {
}
return null;
}
// --- helper data structure
/**
* Helper class for working with the results of getCalendars
*/
public static class CalendarResult {
/** calendar names */
public String[] calendars;
/** calendar ids. null entry -> use default */
public String[] calendarIds;
/** default selection index */
public int defaultIndex = -1;
}
/**
* Appends all user-modifiable calendars to listPreference.
*/
public static CalendarResult getCalendars() {
Context context = ContextManager.getContext();
ContentResolver cr = context.getContentResolver();
Resources r = context.getResources();
Cursor c = cr.query(getCalendarContentUri(CALENDAR_CONTENT_CALENDARS), CALENDARS_PROJECTION,
CALENDARS_WHERE, null, CALENDARS_SORT);
try {
// Fetch the current setting. Invalid calendar id will
// be changed to default value.
String defaultSetting = Preferences.getStringValue(R.string.gcal_p_default);
CalendarResult result = new CalendarResult();
if (c == null || c.getCount() == 0) {
// Something went wrong when querying calendars. Only offer them
// the system default choice
result.calendars = new String[] {
r.getString(R.string.gcal_GCP_default) };
result.calendarIds = new String[] { null };
result.defaultIndex = 0;
return result;
}
int calendarCount = c.getCount();
result.calendars = new String[calendarCount];
result.calendarIds = new String[calendarCount];
// Iterate calendars one by one, and fill up the list preference
int row = 0;
int idColumn = c.getColumnIndex(ID_COLUMN_NAME);
int nameColumn = c.getColumnIndex(CALENDARS_DISPLAY_COL);
while (c.moveToNext()) {
String id = c.getString(idColumn);
String name = c.getString(nameColumn);
result.calendars[row] = name;
result.calendarIds[row] = id;
// We found currently selected calendar
if (defaultSetting != null && defaultSetting.equals(id)) {
result.defaultIndex = row;
}
row++;
}
if (result.defaultIndex >= calendarCount) {
result.defaultIndex = 0;
}
return result;
} finally {
if(c != null) {
c.close();
}
}
}
/**
* Appends all user-modifiable calendars to listPreference.
*
* @param context
* context
* @param listPreference
* preference to init
*/
public static void initCalendarsPreference(Context context,
ListPreference listPreference) {
Resources r = context.getResources();
CalendarResult calendars = getCalendars();
// Fetch the current setting. Invalid calendar id will
// be changed to default value.
String currentSetting = Preferences.getStringValue(R.string.gcal_p_default);
int currentSettingIndex = -1;
ArrayList<CharSequence> entries = new ArrayList<>();
entries.addAll(Arrays.asList(r.getStringArray(R.array.EPr_default_addtocalendar)));
entries.addAll(Arrays.asList(calendars.calendars));
ArrayList<CharSequence> entryValues = new ArrayList<>();
entryValues.addAll(Arrays.asList(r.getStringArray(R.array.EPr_default_addtocalendar_values)));
entryValues.addAll(Arrays.asList(calendars.calendarIds));
listPreference.setEntries(entries.toArray(new CharSequence[entries.size()]));
listPreference.setEntryValues(entryValues.toArray(new CharSequence[entryValues.size()]));
listPreference.setValueIndex(0);
listPreference.setEnabled(true);
if (calendars.calendarIds.length == 0 || calendars.calendars.length == 0) {
// Something went wrong when querying calendars
// Leave the preference at disabled.
return;
}
// Iterate calendars one by one, and fill up the list preference
if (currentSetting != null) {
for (int i=0; i<calendars.calendarIds.length; i++) {
// We found currently selected calendar
if (currentSetting.equals(calendars.calendarIds[i])) {
currentSettingIndex = i+1; // +1 correction for disabled-entry
break;
}
}
}
if(currentSettingIndex == -1 || currentSettingIndex > calendars.calendarIds.length+1) {
// Should not happen!
// Leave the preference at disabled.
Log.d("astrid", "initCalendarsPreference: Unknown calendar.");
currentSettingIndex = 0;
}
listPreference.setValueIndex(currentSettingIndex);
listPreference.setEnabled(true);
}
/**
* gets the default calendar for future use
* @return the calendar id for use with the contentresolver
*/
public static String getDefaultCalendar() {
return Preferences.getStringValue(R.string.gcal_p_default);
}
}

@ -24,9 +24,7 @@ import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.gcal.Calendars.CalendarResult;
import com.todoroo.astrid.service.ThemeService;
import com.todoroo.astrid.ui.PopupControlSet;
@ -53,7 +51,7 @@ public class GCalControlSet extends PopupControlSet {
private Uri calendarUri = null;
private final CalendarResult calendars;
private final GCalHelper.CalendarResult calendars;
private boolean hasEvent = false;
private Spinner calendarSelector;
private final int title;
@ -63,7 +61,7 @@ public class GCalControlSet extends PopupControlSet {
super(activity, viewLayout, displayViewLayout, title);
this.gcal = gcal;
this.title = title;
calendars = Calendars.getCalendars();
this.calendars = gcal.getCalendars();
getView(); // Hack to force initialized
image = (ImageView) getDisplayView().findViewById(R.id.display_row_icon);
}
@ -142,8 +140,8 @@ public class GCalControlSet extends PopupControlSet {
return;
}
boolean gcalCreateEventEnabled = Preferences.getStringValue(R.string.gcal_p_default) != null &&
!Preferences.getStringValue(R.string.gcal_p_default).equals("-1");
boolean gcalCreateEventEnabled = gcal.getDefaultCalendar() != null &&
!gcal.getDefaultCalendar().equals("-1");
if ((gcalCreateEventEnabled || calendarSelector.getSelectedItemPosition() != 0) &&
calendarUri == null) {

@ -8,6 +8,7 @@ package com.todoroo.astrid.gcal;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.CursorIndexOutOfBoundsException;
import android.net.Uri;
@ -28,6 +29,8 @@ import java.util.TimeZone;
import javax.inject.Inject;
import static com.todoroo.astrid.gcal.Calendars.getCalendarContentUri;
public class GCalHelper {
/** If task has no estimated time, how early to set a task in calendar (seconds)*/
private static final long DEFAULT_CAL_TIME = DateUtilities.ONE_HOUR;
@ -67,9 +70,13 @@ public class GCalHelper {
createTaskEventIfEnabled(t, true);
}
public String getDefaultCalendar() {
return preferences.getStringValue(R.string.gcal_p_default);
}
private void createTaskEventIfEnabled(Task t, boolean deleteEventIfExists) {
boolean gcalCreateEventEnabled = preferences.getStringValue(R.string.gcal_p_default) != null
&& !preferences.getStringValue(R.string.gcal_p_default).equals("-1"); //$NON-NLS-1$
boolean gcalCreateEventEnabled = getDefaultCalendar() != null
&& !getDefaultCalendar().equals("-1"); //$NON-NLS-1$
if (gcalCreateEventEnabled) {
ContentResolver cr = context.getContentResolver();
Uri calendarUri = createTaskEvent(t, cr, new ContentValues(), deleteEventIfExists);
@ -91,7 +98,7 @@ public class GCalHelper {
}
try{
Uri uri = Calendars.getCalendarContentUri(Calendars.CALENDAR_CONTENT_EVENTS);
Uri uri = getCalendarContentUri(Calendars.CALENDAR_CONTENT_EVENTS);
values.put("title", task.getTitle());
values.put("description", task.getNotes());
values.put("hasAlarm", 0);
@ -102,7 +109,7 @@ public class GCalHelper {
boolean valuesContainCalendarId = (values.containsKey(CALENDAR_ID_COLUMN) &&
!TextUtils.isEmpty(values.getAsString(CALENDAR_ID_COLUMN)));
if (!valuesContainCalendarId) {
String calendarId = Calendars.getDefaultCalendar();
String calendarId = getDefaultCalendar();
if (!TextUtils.isEmpty(calendarId)) {
values.put("calendar_id", calendarId);
}
@ -238,4 +245,76 @@ public class GCalHelper {
}
}
}
public static class CalendarResult {
/** calendar names */
public String[] calendars;
/** calendar ids. null entry -> use default */
public String[] calendarIds;
/** default selection index */
public int defaultIndex = -1;
}
/**
* Appends all user-modifiable calendars to listPreference.
*/
public CalendarResult getCalendars() {
ContentResolver cr = context.getContentResolver();
Resources r = context.getResources();
Cursor c = cr.query(getCalendarContentUri(Calendars.CALENDAR_CONTENT_CALENDARS), Calendars.CALENDARS_PROJECTION,
Calendars.CALENDARS_WHERE, null, Calendars.CALENDARS_SORT);
try {
// Fetch the current setting. Invalid calendar id will
// be changed to default value.
String defaultSetting = getDefaultCalendar();
CalendarResult result = new CalendarResult();
if (c == null || c.getCount() == 0) {
// Something went wrong when querying calendars. Only offer them
// the system default choice
result.calendars = new String[] {
r.getString(R.string.gcal_GCP_default) };
result.calendarIds = new String[] { null };
result.defaultIndex = 0;
return result;
}
int calendarCount = c.getCount();
result.calendars = new String[calendarCount];
result.calendarIds = new String[calendarCount];
// Iterate calendars one by one, and fill up the list preference
int row = 0;
int idColumn = c.getColumnIndex(Calendars.ID_COLUMN_NAME);
int nameColumn = c.getColumnIndex(Calendars.CALENDARS_DISPLAY_COL);
while (c.moveToNext()) {
String id = c.getString(idColumn);
String name = c.getString(nameColumn);
result.calendars[row] = name;
result.calendarIds[row] = id;
// We found currently selected calendar
if (defaultSetting != null && defaultSetting.equals(id)) {
result.defaultIndex = row;
}
row++;
}
if (result.defaultIndex >= calendarCount) {
result.defaultIndex = 0;
}
return result;
} finally {
if(c != null) {
c.close();
}
}
}
}

@ -283,7 +283,7 @@ public class QuickAddBar extends LinearLayout {
resetControlSets();
addToCalendar(preferences, gcalHelper, taskService, task, title);
addToCalendar(gcalHelper, taskService, task, title);
TextView quickAdd = (TextView) findViewById(R.id.quickAddText);
quickAdd.setText(""); //$NON-NLS-1$
@ -307,9 +307,9 @@ public class QuickAddBar extends LinearLayout {
}
}
private static void addToCalendar(Preferences preferences, GCalHelper gcalHelper, TaskService taskService, Task task, String title) {
boolean gcalCreateEventEnabled = preferences.getStringValue(R.string.gcal_p_default) != null
&& !preferences.getStringValue(R.string.gcal_p_default).equals("-1") && task.hasDueDate(); //$NON-NLS-1$
private static void addToCalendar(GCalHelper gcalHelper, TaskService taskService, Task task, String title) {
boolean gcalCreateEventEnabled = gcalHelper.getDefaultCalendar() != null
&& !gcalHelper.getDefaultCalendar().equals("-1") && task.hasDueDate(); //$NON-NLS-1$
if (!TextUtils.isEmpty(title) && gcalCreateEventEnabled && TextUtils.isEmpty(task.getCalendarURI())) {
Uri calendarUri = gcalHelper.createTaskEvent(task,
@ -324,7 +324,7 @@ public class QuickAddBar extends LinearLayout {
* Static method to quickly add tasks without all the control set nonsense.
* Used from the share link activity.
*/
public static Task basicQuickAddTask(Preferences preferences, GCalHelper gcalHelper, TaskService taskService, MetadataService metadataService, TagService tagService, String title) {
public static Task basicQuickAddTask(GCalHelper gcalHelper, TaskService taskService, MetadataService metadataService, TagService tagService, String title) {
if (TextUtils.isEmpty(title)) {
return null;
}
@ -332,7 +332,7 @@ public class QuickAddBar extends LinearLayout {
title = title.trim();
Task task = TaskService.createWithValues(taskService, metadataService, tagService, null, title);
addToCalendar(preferences, gcalHelper, taskService, task, title);
addToCalendar(gcalHelper, taskService, task, title);
return task;
}

@ -23,11 +23,13 @@ import android.preference.PreferenceManager;
import android.preference.RingtonePreference;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.utility.Preferences;
import org.tasks.injection.InjectingPreferenceActivity;
import org.tasks.preferences.Preferences;
import org.tasks.ui.TimePreference;
import javax.inject.Inject;
/**
* Displays a preference screen for users to edit their preferences. Override
* updatePreferences to update the summary with preference values.
@ -49,6 +51,8 @@ abstract public class TodorooPreferenceActivity extends InjectingPreferenceActiv
// --- implementation
@Inject Preferences preferences;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -59,7 +63,7 @@ abstract public class TodorooPreferenceActivity extends InjectingPreferenceActiv
@Override
public SharedPreferences getSharedPreferences(String name, int mode) {
return Preferences.getPrefs(this);
return preferences.getPrefs();
}
protected void initializePreference(Preference preference) {

@ -12,7 +12,6 @@ import com.todoroo.astrid.tags.TagService;
import org.tasks.R;
import org.tasks.injection.InjectingActivity;
import org.tasks.preferences.Preferences;
import javax.inject.Inject;
@ -20,7 +19,6 @@ import static com.todoroo.astrid.ui.QuickAddBar.basicQuickAddTask;
public class VoiceCommandActivity extends InjectingActivity {
@Inject Preferences preferences;
@Inject GCalHelper gcalHelper;
@Inject MetadataService metadataService;
@Inject TagService tagService;
@ -35,7 +33,7 @@ public class VoiceCommandActivity extends InjectingActivity {
switch (intent.getAction()) {
case "com.google.android.gm.action.AUTO_SEND":
final String text = intent.getStringExtra(Intent.EXTRA_TEXT);
basicQuickAddTask(preferences, gcalHelper, taskService, metadataService, tagService, text);
basicQuickAddTask(gcalHelper, taskService, metadataService, tagService, text);
Context context = getApplicationContext();
if (context != null) {
Toast.makeText(context, getString(R.string.voice_command_added_task), Toast.LENGTH_LONG).show();

Loading…
Cancel
Save