Android Open Source - inotes Sync Adapter






From Project

Back to project page inotes.

License

The source code is released under:

GNU General Public License

If you think the Android project inotes listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.codeminders.inotes.sync;
//  w  ww . j a  v  a 2s. c  o  m
import java.util.*;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.*;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;

import com.codeminders.inotes.Constants;
import com.codeminders.inotes.db.DBManager;
import com.codeminders.inotes.imap.ImapService;
import com.codeminders.inotes.model.AccountInfo;
import com.codeminders.inotes.model.Note;

public class SyncAdapter extends AbstractThreadedSyncAdapter {
    private final AccountManager accountManager;
    private final Context context;
    private final int[] times = new int[]{
            300, 900, 1800, 3600, 7200, 14400, 28800, 43200, 86440
    };

    public SyncAdapter(Context context, boolean autoInitialize) {
        super(context, autoInitialize);
        this.context = context;
        accountManager = AccountManager.get(context);
    }

    @Override
    public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
        SharedPreferences prefs = context.getSharedPreferences(Constants.PREFS_NAME, Context.MODE_PRIVATE);
        addPeriodicallySync(prefs, account, authority);

        if (prefs.getBoolean(Constants.SYNC_TYPE, false)) {
            NetworkInfo info = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
            if (info == null || info.getType() != 1) {
                return;
            }
        }

        try {
            DBManager dbManager = new DBManager(context);
            long time = dbManager.getLastSyncTime(account.name);
            long currentTime = new Date().getTime();
            ImapService imapService = new ImapService(getAccountInfo(account), context);
            List<Note> newNotes;
            if (time == 0) {
                newNotes = imapService.getNotes();
                imapService.addNotes(dbManager.getNotesByDate(account.name, time, currentTime));
            } else {
                imapService.deleteNotes(dbManager.getNotesIdentifiersToDelete(account.name, currentTime));
                newNotes = getNewNotes(imapService.getNotes(), time, currentTime);
                imapService.addNotes(dbManager.getNotesByDate(account.name, time, currentTime));
            }
            writeNewNotes(dbManager, newNotes);
            imapService.checkForServersDeletedNotes(context, account.name);
            dbManager.setLastSyncTime(account.name, new Date(currentTime));
        } catch (Exception e) {
            Log.e(Constants.TAG, e.toString());
        }

    }

    private void writeNewNotes(DBManager dbManager, List<Note> notes) {
        for (Note note : notes) {
            dbManager.writeNote(note);
        }
    }

    private List<Note> getNewNotes(List<Note> notes, long lastSync, long currentSync) {
        List<Note> newNotes = new ArrayList<Note>();
        for (Note note : notes) {
            if (currentSync > note.getDate().getTime() && note.getDate().getTime() > lastSync) {
                newNotes.add(note);
            }
        }
        return newNotes;
    }

    private AccountInfo getAccountInfo(Account account) {
        AccountInfo accountInfo = new AccountInfo();
        accountInfo.setEmail(account.name);
        accountInfo.setHost(accountManager.getUserData(account, "host"));
        accountInfo.setPassword(accountManager.getPassword(account));
        accountInfo.setPort(accountManager.getUserData(account, "port"));
        accountInfo.setUseSSL(accountManager.getUserData(account, "useSSL").equals("true"));

        return accountInfo;
    }

    private void addPeriodicallySync(SharedPreferences prefs, Account account, String authority) {
        int itemPosition = prefs.getInt(Constants.SYNC_INTERVAL, 4);
        ContentResolver.addPeriodicSync(account, authority, new Bundle(), times[itemPosition]);
    }


}




Java Source Code List

com.codeminders.inotes.AccountReceiver.java
com.codeminders.inotes.Constants.java
com.codeminders.inotes.Utils.java
com.codeminders.inotes.auth.AuthenticationService.java
com.codeminders.inotes.auth.AuthenticatorActivity.java
com.codeminders.inotes.auth.Authenticator.java
com.codeminders.inotes.auth.ConfigurationActivity.java
com.codeminders.inotes.db.DBCreator.java
com.codeminders.inotes.db.DBManager.java
com.codeminders.inotes.db.DBUpgradeHelper.java
com.codeminders.inotes.imap.HeaderUtils.java
com.codeminders.inotes.imap.ImapService.java
com.codeminders.inotes.imap.ImapSession.java
com.codeminders.inotes.model.AccountInfo.java
com.codeminders.inotes.model.Note.java
com.codeminders.inotes.sync.NotesProvider.java
com.codeminders.inotes.sync.NotesSyncService.java
com.codeminders.inotes.sync.SyncAccountInfo.java
com.codeminders.inotes.sync.SyncAdapter.java
com.codeminders.inotes.sync.SyncAllAccountsInfo.java
com.codeminders.inotes.sync.SyncHelper.java
com.codeminders.inotes.sync.SyncInfo.java
com.codeminders.inotes.ui.AccountsListActivity.java
com.codeminders.inotes.ui.LinedEditText.java
com.codeminders.inotes.ui.NoteEditorActivity.java
com.codeminders.inotes.ui.NotesListActivity.java
com.codeminders.inotes.ui.SettingsActivity.java