Search in sources :

Example 1 with Node

use of net.micode.notes.gtask.data.Node in project Notes by MiCode.

the class GTaskManager method refreshLocalSyncId.

private void refreshLocalSyncId() throws NetworkFailureException {
    if (mCancelled) {
        return;
    }
    // get the latest gtask list
    mGTaskHashMap.clear();
    mGTaskListHashMap.clear();
    mMetaHashMap.clear();
    initGTaskList();
    Cursor c = null;
    try {
        c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, "(type<>? AND parent_id<>?)", new String[] { String.valueOf(Notes.TYPE_SYSTEM), String.valueOf(Notes.ID_TRASH_FOLER) }, NoteColumns.TYPE + " DESC");
        if (c != null) {
            while (c.moveToNext()) {
                String gid = c.getString(SqlNote.GTASK_ID_COLUMN);
                Node node = mGTaskHashMap.get(gid);
                if (node != null) {
                    mGTaskHashMap.remove(gid);
                    ContentValues values = new ContentValues();
                    values.put(NoteColumns.SYNC_ID, node.getLastModified());
                    mContentResolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(SqlNote.ID_COLUMN)), values, null, null);
                } else {
                    Log.e(TAG, "something is missed");
                    throw new ActionFailureException("some local items don't have gid after sync");
                }
            }
        } else {
            Log.w(TAG, "failed to query local note to refresh sync id");
        }
    } finally {
        if (c != null) {
            c.close();
            c = null;
        }
    }
}
Also used : ContentValues(android.content.ContentValues) ActionFailureException(net.micode.notes.gtask.exception.ActionFailureException) Node(net.micode.notes.gtask.data.Node) Cursor(android.database.Cursor)

Example 2 with Node

use of net.micode.notes.gtask.data.Node in project Notes by MiCode.

the class GTaskManager method addRemoteNode.

private void addRemoteNode(Node node, Cursor c) throws NetworkFailureException {
    if (mCancelled) {
        return;
    }
    SqlNote sqlNote = new SqlNote(mContext, c);
    Node n;
    // update remotely
    if (sqlNote.isNoteType()) {
        Task task = new Task();
        task.setContentByLocalJSON(sqlNote.getContent());
        String parentGid = mNidToGid.get(sqlNote.getParentId());
        if (parentGid == null) {
            Log.e(TAG, "cannot find task's parent tasklist");
            throw new ActionFailureException("cannot add remote task");
        }
        mGTaskListHashMap.get(parentGid).addChildTask(task);
        GTaskClient.getInstance().createTask(task);
        n = (Node) task;
        // add meta
        updateRemoteMeta(task.getGid(), sqlNote);
    } else {
        TaskList tasklist = null;
        // we need to skip folder if it has already existed
        String folderName = GTaskStringUtils.MIUI_FOLDER_PREFFIX;
        if (sqlNote.getId() == Notes.ID_ROOT_FOLDER)
            folderName += GTaskStringUtils.FOLDER_DEFAULT;
        else if (sqlNote.getId() == Notes.ID_CALL_RECORD_FOLDER)
            folderName += GTaskStringUtils.FOLDER_CALL_NOTE;
        else
            folderName += sqlNote.getSnippet();
        Iterator<Map.Entry<String, TaskList>> iter = mGTaskListHashMap.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry<String, TaskList> entry = iter.next();
            String gid = entry.getKey();
            TaskList list = entry.getValue();
            if (list.getName().equals(folderName)) {
                tasklist = list;
                if (mGTaskHashMap.containsKey(gid)) {
                    mGTaskHashMap.remove(gid);
                }
                break;
            }
        }
        // no match we can add now
        if (tasklist == null) {
            tasklist = new TaskList();
            tasklist.setContentByLocalJSON(sqlNote.getContent());
            GTaskClient.getInstance().createTaskList(tasklist);
            mGTaskListHashMap.put(tasklist.getGid(), tasklist);
        }
        n = (Node) tasklist;
    }
    // update local note
    sqlNote.setGtaskId(n.getGid());
    sqlNote.commit(false);
    sqlNote.resetLocalModified();
    sqlNote.commit(true);
    // gid-id mapping
    mGidToNid.put(n.getGid(), sqlNote.getId());
    mNidToGid.put(sqlNote.getId(), n.getGid());
}
Also used : Task(net.micode.notes.gtask.data.Task) ActionFailureException(net.micode.notes.gtask.exception.ActionFailureException) Node(net.micode.notes.gtask.data.Node) TaskList(net.micode.notes.gtask.data.TaskList) HashMap(java.util.HashMap) Map(java.util.Map) SqlNote(net.micode.notes.gtask.data.SqlNote)

Example 3 with Node

use of net.micode.notes.gtask.data.Node in project Notes by MiCode.

the class GTaskManager method syncFolder.

private void syncFolder() throws NetworkFailureException {
    Cursor c = null;
    String gid;
    Node node;
    int syncType;
    if (mCancelled) {
        return;
    }
    // for root folder
    try {
        c = mContentResolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, Notes.ID_ROOT_FOLDER), SqlNote.PROJECTION_NOTE, null, null, null);
        if (c != null) {
            c.moveToNext();
            gid = c.getString(SqlNote.GTASK_ID_COLUMN);
            node = mGTaskHashMap.get(gid);
            if (node != null) {
                mGTaskHashMap.remove(gid);
                mGidToNid.put(gid, (long) Notes.ID_ROOT_FOLDER);
                mNidToGid.put((long) Notes.ID_ROOT_FOLDER, gid);
                // for system folder, only update remote name if necessary
                if (!node.getName().equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_DEFAULT))
                    doContentSync(Node.SYNC_ACTION_UPDATE_REMOTE, node, c);
            } else {
                doContentSync(Node.SYNC_ACTION_ADD_REMOTE, node, c);
            }
        } else {
            Log.w(TAG, "failed to query root folder");
        }
    } finally {
        if (c != null) {
            c.close();
            c = null;
        }
    }
    // for call-note folder
    try {
        c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, "(_id=?)", new String[] { String.valueOf(Notes.ID_CALL_RECORD_FOLDER) }, null);
        if (c != null) {
            if (c.moveToNext()) {
                gid = c.getString(SqlNote.GTASK_ID_COLUMN);
                node = mGTaskHashMap.get(gid);
                if (node != null) {
                    mGTaskHashMap.remove(gid);
                    mGidToNid.put(gid, (long) Notes.ID_CALL_RECORD_FOLDER);
                    mNidToGid.put((long) Notes.ID_CALL_RECORD_FOLDER, gid);
                    // necessary
                    if (!node.getName().equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_CALL_NOTE))
                        doContentSync(Node.SYNC_ACTION_UPDATE_REMOTE, node, c);
                } else {
                    doContentSync(Node.SYNC_ACTION_ADD_REMOTE, node, c);
                }
            }
        } else {
            Log.w(TAG, "failed to query call note folder");
        }
    } finally {
        if (c != null) {
            c.close();
            c = null;
        }
    }
    // for local existing folders
    try {
        c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, "(type=? AND parent_id<>?)", new String[] { String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER) }, NoteColumns.TYPE + " DESC");
        if (c != null) {
            while (c.moveToNext()) {
                gid = c.getString(SqlNote.GTASK_ID_COLUMN);
                node = mGTaskHashMap.get(gid);
                if (node != null) {
                    mGTaskHashMap.remove(gid);
                    mGidToNid.put(gid, c.getLong(SqlNote.ID_COLUMN));
                    mNidToGid.put(c.getLong(SqlNote.ID_COLUMN), gid);
                    syncType = node.getSyncAction(c);
                } else {
                    if (c.getString(SqlNote.GTASK_ID_COLUMN).trim().length() == 0) {
                        // local add
                        syncType = Node.SYNC_ACTION_ADD_REMOTE;
                    } else {
                        // remote delete
                        syncType = Node.SYNC_ACTION_DEL_LOCAL;
                    }
                }
                doContentSync(syncType, node, c);
            }
        } else {
            Log.w(TAG, "failed to query existing folder");
        }
    } finally {
        if (c != null) {
            c.close();
            c = null;
        }
    }
    // for remote add folders
    Iterator<Map.Entry<String, TaskList>> iter = mGTaskListHashMap.entrySet().iterator();
    while (iter.hasNext()) {
        Map.Entry<String, TaskList> entry = iter.next();
        gid = entry.getKey();
        node = entry.getValue();
        if (mGTaskHashMap.containsKey(gid)) {
            mGTaskHashMap.remove(gid);
            doContentSync(Node.SYNC_ACTION_ADD_LOCAL, node, null);
        }
    }
    if (!mCancelled)
        GTaskClient.getInstance().commitUpdate();
}
Also used : Node(net.micode.notes.gtask.data.Node) TaskList(net.micode.notes.gtask.data.TaskList) Cursor(android.database.Cursor) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with Node

use of net.micode.notes.gtask.data.Node in project Notes by MiCode.

the class GTaskManager method syncContent.

private void syncContent() throws NetworkFailureException {
    int syncType;
    Cursor c = null;
    String gid;
    Node node;
    mLocalDeleteIdMap.clear();
    if (mCancelled) {
        return;
    }
    // for local deleted note
    try {
        c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, "(type<>? AND parent_id=?)", new String[] { String.valueOf(Notes.TYPE_SYSTEM), String.valueOf(Notes.ID_TRASH_FOLER) }, null);
        if (c != null) {
            while (c.moveToNext()) {
                gid = c.getString(SqlNote.GTASK_ID_COLUMN);
                node = mGTaskHashMap.get(gid);
                if (node != null) {
                    mGTaskHashMap.remove(gid);
                    doContentSync(Node.SYNC_ACTION_DEL_REMOTE, node, c);
                }
                mLocalDeleteIdMap.add(c.getLong(SqlNote.ID_COLUMN));
            }
        } else {
            Log.w(TAG, "failed to query trash folder");
        }
    } finally {
        if (c != null) {
            c.close();
            c = null;
        }
    }
    // sync folder first
    syncFolder();
    // for note existing in database
    try {
        c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, "(type=? AND parent_id<>?)", new String[] { String.valueOf(Notes.TYPE_NOTE), String.valueOf(Notes.ID_TRASH_FOLER) }, NoteColumns.TYPE + " DESC");
        if (c != null) {
            while (c.moveToNext()) {
                gid = c.getString(SqlNote.GTASK_ID_COLUMN);
                node = mGTaskHashMap.get(gid);
                if (node != null) {
                    mGTaskHashMap.remove(gid);
                    mGidToNid.put(gid, c.getLong(SqlNote.ID_COLUMN));
                    mNidToGid.put(c.getLong(SqlNote.ID_COLUMN), gid);
                    syncType = node.getSyncAction(c);
                } else {
                    if (c.getString(SqlNote.GTASK_ID_COLUMN).trim().length() == 0) {
                        // local add
                        syncType = Node.SYNC_ACTION_ADD_REMOTE;
                    } else {
                        // remote delete
                        syncType = Node.SYNC_ACTION_DEL_LOCAL;
                    }
                }
                doContentSync(syncType, node, c);
            }
        } else {
            Log.w(TAG, "failed to query existing note in database");
        }
    } finally {
        if (c != null) {
            c.close();
            c = null;
        }
    }
    // go through remaining items
    Iterator<Map.Entry<String, Node>> iter = mGTaskHashMap.entrySet().iterator();
    while (iter.hasNext()) {
        Map.Entry<String, Node> entry = iter.next();
        node = entry.getValue();
        doContentSync(Node.SYNC_ACTION_ADD_LOCAL, node, null);
    }
    // clear local delete table
    if (!mCancelled) {
        if (!DataUtils.batchDeleteNotes(mContentResolver, mLocalDeleteIdMap)) {
            throw new ActionFailureException("failed to batch-delete local deleted notes");
        }
    }
    // refresh local sync id
    if (!mCancelled) {
        GTaskClient.getInstance().commitUpdate();
        refreshLocalSyncId();
    }
}
Also used : ActionFailureException(net.micode.notes.gtask.exception.ActionFailureException) Node(net.micode.notes.gtask.data.Node) Cursor(android.database.Cursor) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Node (net.micode.notes.gtask.data.Node)4 Cursor (android.database.Cursor)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ActionFailureException (net.micode.notes.gtask.exception.ActionFailureException)3 TaskList (net.micode.notes.gtask.data.TaskList)2 ContentValues (android.content.ContentValues)1 SqlNote (net.micode.notes.gtask.data.SqlNote)1 Task (net.micode.notes.gtask.data.Task)1