@ -1,13 +1,16 @@
package com.todoroo.astrid.subtasks ;
package com.todoroo.astrid.subtasks ;
import java.util.ArrayList ;
import java.util.ArrayList ;
import java.util.HashMap ;
import android.content.SharedPreferences ;
import android.content.SharedPreferences ;
import android.text.TextUtils ;
import android.text.TextUtils ;
import android.util.Log ;
import android.util.Log ;
import com.todoroo.andlib.data.TodorooCursor ;
import com.todoroo.andlib.service.ContextManager ;
import com.todoroo.andlib.service.ContextManager ;
import com.todoroo.andlib.sql.Criterion ;
import com.todoroo.andlib.sql.Criterion ;
import com.todoroo.andlib.sql.Query ;
import com.todoroo.andlib.utility.Preferences ;
import com.todoroo.andlib.utility.Preferences ;
import com.todoroo.astrid.actfm.TagViewFragment ;
import com.todoroo.astrid.actfm.TagViewFragment ;
import com.todoroo.astrid.api.Filter ;
import com.todoroo.astrid.api.Filter ;
@ -18,6 +21,7 @@ import com.todoroo.astrid.core.SortHelper;
import com.todoroo.astrid.dao.TaskDao.TaskCriteria ;
import com.todoroo.astrid.dao.TaskDao.TaskCriteria ;
import com.todoroo.astrid.data.TagData ;
import com.todoroo.astrid.data.TagData ;
import com.todoroo.astrid.data.Task ;
import com.todoroo.astrid.data.Task ;
import com.todoroo.astrid.subtasks.AstridOrderedListUpdater.Node ;
import com.todoroo.astrid.utility.AstridPreferences ;
import com.todoroo.astrid.utility.AstridPreferences ;
public class SubtasksHelper {
public class SubtasksHelper {
@ -75,12 +79,11 @@ public class SubtasksHelper {
else
else
serialized = Preferences . getStringValue ( SubtasksUpdater . ACTIVE_TASKS_ORDER ) ;
serialized = Preferences . getStringValue ( SubtasksUpdater . ACTIVE_TASKS_ORDER ) ;
ArrayList < Long > ids = getIdArray ( serialized ) ;
return AstridOrderedListUpdater . buildOrderString ( getIdArray ( serialized ) ) ;
return AstridOrderedListUpdater . buildOrderString ( ids . toArray ( new Long [ ids . size ( ) ] ) ) ;
}
}
@SuppressWarnings ( "nls" )
@SuppressWarnings ( "nls" )
p rivate static ArrayList < Long > getIdArray ( String serializedTree ) {
p ublic static Long [ ] getIdArray ( String serializedTree ) {
ArrayList < Long > ids = new ArrayList < Long > ( ) ;
ArrayList < Long > ids = new ArrayList < Long > ( ) ;
String [ ] digitsOnly = serializedTree . split ( "\\D+" ) ;
String [ ] digitsOnly = serializedTree . split ( "\\D+" ) ;
for ( String idString : digitsOnly ) {
for ( String idString : digitsOnly ) {
@ -91,7 +94,74 @@ public class SubtasksHelper {
Log . e ( "widget-subtasks" , "error parsing id " + idString , e ) ;
Log . e ( "widget-subtasks" , "error parsing id " + idString , e ) ;
}
}
}
}
return ids ;
return ids . toArray ( new Long [ ids . size ( ) ] ) ;
}
public static String convertTreeToRemoteIds ( String localTree ) {
return convertIdTree ( localTree , true ) ;
}
public static String convertTreeToLocalIds ( String remoteTree ) {
return convertIdTree ( remoteTree , false ) ;
}
private static String convertIdTree ( String treeString , boolean localToRemote ) {
Long [ ] ids = getIdArray ( treeString ) ;
HashMap < Long , Long > idMap = buildIdMap ( ids , localToRemote ) ;
Node tree = AstridOrderedListUpdater . buildTreeModel ( treeString , null ) ;
remapTree ( tree , idMap ) ;
return AstridOrderedListUpdater . serializeTree ( tree ) ;
}
private static void remapTree ( Node root , HashMap < Long , Long > idMap ) {
ArrayList < Node > children = root . children ;
for ( int i = 0 ; i < children . size ( ) ; i + + ) {
Node child = children . get ( i ) ;
Long remoteId = idMap . get ( child . taskId ) ;
if ( remoteId = = null | | remoteId < = 0 ) {
children . remove ( i ) ;
i - - ;
} else {
child . taskId = remoteId ;
remapTree ( child , idMap ) ;
}
}
}
private static HashMap < Long , Long > buildIdMap ( Long [ ] localIds , boolean localToRemote ) { // If localToRemote is true, keys are local ids. If false. keys are remtoe ids
HashMap < Long , Long > map = new HashMap < Long , Long > ( ) ;
Criterion criterion ;
if ( localToRemote )
criterion = Task . ID . in ( localIds ) ;
else
criterion = Task . REMOTE_ID . in ( localIds ) ;
TodorooCursor < Task > tasks = PluginServices . getTaskService ( ) . query ( Query . select ( Task . ID , Task . REMOTE_ID ) . where ( criterion ) ) ;
try {
Task t = new Task ( ) ;
for ( tasks . moveToFirst ( ) ; ! tasks . isAfterLast ( ) ; tasks . moveToNext ( ) ) {
t . clear ( ) ;
t . readFromCursor ( tasks ) ;
if ( t . containsNonNullValue ( Task . REMOTE_ID ) ) {
Long key ;
Long value ;
if ( localToRemote ) {
key = t . getId ( ) ;
value = t . getValue ( Task . REMOTE_ID ) ;
} else {
key = t . getValue ( Task . REMOTE_ID ) ;
value = t . getId ( ) ;
}
map . put ( key , value ) ;
}
}
} finally {
tasks . close ( ) ;
}
return map ;
}
}
}
}