Android Open Source - EBrowser Weave Sync Task






From Project

Back to project page EBrowser.

License

The source code is released under:

GNU General Public License

If you think the Android project EBrowser 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 org.zirco.sync;
/*from  w  w w  .j av a 2 s.c o  m*/
import java.io.IOException;
import java.net.URI;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.emergent.android.weave.client.QueryParams;
import org.emergent.android.weave.client.QueryResult;
import org.emergent.android.weave.client.UserWeave;
import org.emergent.android.weave.client.WeaveAccountInfo;
import org.emergent.android.weave.client.WeaveBasicObject;
import org.emergent.android.weave.client.WeaveException;
import org.emergent.android.weave.client.WeaveFactory;
import org.json.JSONException;
import org.json.JSONObject;
import org.zirco.providers.BookmarksProviderWrapper;
import org.zirco.providers.WeaveColumns;
import org.zirco.utils.Constants;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.os.AsyncTask;
import android.preference.PreferenceManager;

public class WeaveSyncTask extends AsyncTask<WeaveAccountInfo, Integer, Throwable> {

  private static final String WEAVE_PATH = "/storage/bookmarks";
  
  private static final String WEAVE_HEADER_TYPE = "type";
  
  private static final String WEAVE_VALUE_BOOKMARK = "bookmark";
  private static final String WEAVE_VALUE_FOLDER = "folder";
  private static final String WEAVE_VALUE_ITEM = "item";
  private static final String WEAVE_VALUE_ID = "id";
  private static final String WEAVE_VALUE_PARENT_ID = "parentid";
  private static final String WEAVE_VALUE_TITLE = "title";
  private static final String WEAVE_VALUE_URI = "bmkUri";
  private static final String WEAVE_VALUE_DELETED = "deleted";
  
  private static WeaveFactory mWeaveFactory = null;
  
  private static WeaveFactory getWeaveFactory() {
    if (mWeaveFactory == null) {
      mWeaveFactory = new WeaveFactory(true);
    }
    
    return mWeaveFactory;
  }
  
  private Context mContext;
  private ContentResolver mContentResolver;
  private ISyncListener mListener;
  
  private boolean mFullSync = false;
  
  public WeaveSyncTask(Context context, ISyncListener listener) {
    mContext = context;
    mContentResolver = context.getContentResolver();
    mListener = listener;
  }
  
  @Override
  protected Throwable doInBackground(WeaveAccountInfo... arg0) {
    Throwable result = null;    
    
    try {      
      publishProgress(0, 0, 0);
      
      WeaveAccountInfo accountInfo = arg0[0];
      UserWeave userWeave = getWeaveFactory().createUserWeave(accountInfo.getServer(), accountInfo.getUsername(), accountInfo.getPassword());            
      
      long lastModifiedDate = getLastModified(userWeave).getTime();      
      long lastSyncDate = PreferenceManager.getDefaultSharedPreferences(mContext).getLong(Constants.PREFERENCE_WEAVE_LAST_SYNC_DATE, -1);
      
      if (lastModifiedDate > lastSyncDate) {    
      
        publishProgress(1, 0, 0);
        
        mFullSync = lastSyncDate <= 0;
        
        QueryResult<List<WeaveBasicObject>> queryResult;
        
        QueryParams parms = null;
        if (!mFullSync) {
          parms = new QueryParams();
          parms.setFull(false);
          parms.setNewer(new Date(lastSyncDate));
        } else {
          BookmarksProviderWrapper.clearWeaveBookmarks(mContentResolver);
        }
        
        queryResult = getCollection(userWeave, WEAVE_PATH, parms);
        List<WeaveBasicObject> wboList = queryResult.getValue();

        if (mFullSync) {
          doSync(accountInfo, userWeave, wboList);
        } else {
          doSyncByDelta(accountInfo, userWeave, wboList);
        }
      }
      
    } catch (WeaveException e) {
      e.printStackTrace();
      result = e;
    } catch (JSONException e) {
      e.printStackTrace();
      result = e;
    } catch (IOException e) {
      e.printStackTrace();
      result = e;
    } catch (GeneralSecurityException e) {
      e.printStackTrace();
      result = e;
    }
    
    return result;
  }
  
  private void doSync(WeaveAccountInfo accountInfo, UserWeave userWeave, List<WeaveBasicObject> wboList)
    throws WeaveException, JSONException, IOException, GeneralSecurityException {
    
    int i = 0;
    int count = wboList.size();
    
    List<ContentValues> values = new ArrayList<ContentValues>();
    
    mContext.getContentResolver().delete(WeaveColumns.CONTENT_URI, null, null);
    
    for (WeaveBasicObject wbo : wboList) {
      JSONObject decryptedPayload = wbo.getEncryptedPayload(userWeave, accountInfo.getSecret());
      
      i++;
      
      //if (i > 30) break;
      //Log.d("Decrypted:", decryptedPayload.toString());

      if (decryptedPayload.has(WEAVE_HEADER_TYPE) &&
          ((decryptedPayload.getString(WEAVE_HEADER_TYPE).equals(WEAVE_VALUE_BOOKMARK)) ||
              (decryptedPayload.getString(WEAVE_HEADER_TYPE).equals(WEAVE_VALUE_FOLDER)))) {

        if (decryptedPayload.has(WEAVE_VALUE_TITLE)) {          
          
          boolean isFolder = decryptedPayload.getString(WEAVE_HEADER_TYPE).equals(WEAVE_VALUE_FOLDER);

          String title = decryptedPayload.getString(WEAVE_VALUE_TITLE);              
          String weaveId = decryptedPayload.has(WEAVE_VALUE_ID) ? decryptedPayload.getString(WEAVE_VALUE_ID) : null;
          String parentId = decryptedPayload.has(WEAVE_VALUE_PARENT_ID) ? decryptedPayload.getString(WEAVE_VALUE_PARENT_ID) : null;

          if ((title != null) && (title.length() > 0)) {

            ContentValues value = new ContentValues();
            value.put(WeaveColumns.WEAVE_BOOKMARKS_TITLE, title);
            value.put(WeaveColumns.WEAVE_BOOKMARKS_WEAVE_ID, weaveId);
            value.put(WeaveColumns.WEAVE_BOOKMARKS_WEAVE_PARENT_ID, parentId);

            if (isFolder) {
              value.put(WeaveColumns.WEAVE_BOOKMARKS_FOLDER, true);
            } else {
              String url = decryptedPayload.getString(WEAVE_VALUE_URI);

              value.put(WeaveColumns.WEAVE_BOOKMARKS_FOLDER, false);
              value.put(WeaveColumns.WEAVE_BOOKMARKS_URL, url);
            }

            values.add(value);                
          }
        }
      }

      publishProgress(2, i, count);

      if (isCancelled()) {
        break;
      }
    }
    
    int j = 0;
    ContentValues[] valuesArray = new ContentValues[values.size()];
    for (ContentValues value : values) {
      valuesArray[j++] = value;
    }
    
    publishProgress(3, 0, 0);
    mContext.getContentResolver().bulkInsert(WeaveColumns.CONTENT_URI, valuesArray);
  }
  
  private void doSyncByDelta(WeaveAccountInfo accountInfo, UserWeave userWeave, List<WeaveBasicObject> wboList)
    throws WeaveException, JSONException, IOException, GeneralSecurityException {
    
    int i = 0;
    int count = wboList.size();
    
    for (WeaveBasicObject wbo : wboList) {
      JSONObject decryptedPayload = wbo.getEncryptedPayload(userWeave, accountInfo.getSecret());
      
      i++;            
      
      if (decryptedPayload.has(WEAVE_HEADER_TYPE)) {
        
        if (decryptedPayload.getString(WEAVE_HEADER_TYPE).equals(WEAVE_VALUE_ITEM) &&
            decryptedPayload.has(WEAVE_VALUE_DELETED) &&
            decryptedPayload.getBoolean(WEAVE_VALUE_DELETED)) {

          String weaveId = decryptedPayload.has(WEAVE_VALUE_ID) ? decryptedPayload.getString(WEAVE_VALUE_ID) : null;
          if ((weaveId != null) &&
              (weaveId.length() > 0)) {
            //mDbAdapter.deleteWeaveBookmarkByWeaveId(weaveId);
            BookmarksProviderWrapper.deleteWeaveBookmarkByWeaveId(mContentResolver, weaveId);
          }
        } else if (decryptedPayload.getString(WEAVE_HEADER_TYPE).equals(WEAVE_VALUE_BOOKMARK) ||
            decryptedPayload.getString(WEAVE_HEADER_TYPE).equals(WEAVE_VALUE_FOLDER)) {
          
          String weaveId = decryptedPayload.has(WEAVE_VALUE_ID) ? decryptedPayload.getString(WEAVE_VALUE_ID) : null;
          if ((weaveId != null) &&
              (weaveId.length() > 0)) {
            
            boolean isFolder = decryptedPayload.getString(WEAVE_HEADER_TYPE).equals(WEAVE_VALUE_FOLDER);
            
            String title = decryptedPayload.getString(WEAVE_VALUE_TITLE);
            String parentId = decryptedPayload.has(WEAVE_VALUE_PARENT_ID) ? decryptedPayload.getString(WEAVE_VALUE_PARENT_ID) : null;
            
            ContentValues values = new ContentValues();
            values.put(WeaveColumns.WEAVE_BOOKMARKS_WEAVE_ID, weaveId);
            values.put(WeaveColumns.WEAVE_BOOKMARKS_WEAVE_PARENT_ID, parentId);
            values.put(WeaveColumns.WEAVE_BOOKMARKS_TITLE, title);            
            
            if (isFolder) {
              values.put(WeaveColumns.WEAVE_BOOKMARKS_FOLDER, true);
            } else {
              String url = decryptedPayload.getString(WEAVE_VALUE_URI);
              
              values.put(WeaveColumns.WEAVE_BOOKMARKS_FOLDER, false);
              values.put(WeaveColumns.WEAVE_BOOKMARKS_URL, url);
            }
            
            long id = BookmarksProviderWrapper.getWeaveBookmarkIdByWeaveId(mContentResolver, weaveId);//mDbAdapter.getIdByWeaveId(weaveId);
            if (id == -1) {
              // Insert.
              BookmarksProviderWrapper.insertWeaveBookmark(mContentResolver, values);
            } else {
              // Update.
              BookmarksProviderWrapper.updateWeaveBookmark(mContentResolver, id, values);
            }            
            
          }
        }
      }
      
      //Log.d("Decrypted:", decryptedPayload.toString());
      
      publishProgress(2, i, count);

      if (isCancelled()) {
        break;
      }
    }
  }
  
  @Override
  protected void onCancelled() {
    mListener.onSyncCancelled();
    super.onCancelled();
  }

  @Override
  protected void onProgressUpdate(Integer... values) {
    mListener.onSyncProgress(values[0], values[1], values[2]);
  }

  @Override
  protected void onPostExecute(Throwable result) {
    mListener.onSyncEnd(result);
  }

  private QueryResult<List<WeaveBasicObject>> getCollection(UserWeave weave, String name, QueryParams params) throws WeaveException {
      if (params == null)
        params = new QueryParams();
      URI uri = weave.buildSyncUriFromSubpath(name + params.toQueryString());
      return weave.getWboCollection(uri);
    }
  
  private Date getLastModified(UserWeave userWeave) throws WeaveException {
    try {
      JSONObject infoCol = userWeave.getNode(UserWeave.HashNode.INFO_COLLECTIONS).getValue();

      if (infoCol.has("bookmarks")) {
        long modLong = infoCol.getLong("bookmarks");
        return new Date(modLong * 1000);
      }

      return null;
    } catch (JSONException e) {
      throw new WeaveException(e);
    }
  }
  
  public boolean isFullSync() {
    return mFullSync;
  }

}




Java Source Code List

org.emergent.android.weave.client.Base32.java
org.emergent.android.weave.client.Base64Encoder.java
org.emergent.android.weave.client.Base64.java
org.emergent.android.weave.client.BulkKeyCouplet.java
org.emergent.android.weave.client.Dbg.java
org.emergent.android.weave.client.HexEncoder.java
org.emergent.android.weave.client.Hex.java
org.emergent.android.weave.client.QueryParams.java
org.emergent.android.weave.client.QueryResult.java
org.emergent.android.weave.client.UserWeave.java
org.emergent.android.weave.client.WeaveAccountInfo.java
org.emergent.android.weave.client.WeaveBasicObject.java
org.emergent.android.weave.client.WeaveConstants.java
org.emergent.android.weave.client.WeaveCryptoUtil.java
org.emergent.android.weave.client.WeaveException.java
org.emergent.android.weave.client.WeaveFactory.java
org.emergent.android.weave.client.WeaveHeader.java
org.emergent.android.weave.client.WeaveResponse.java
org.emergent.android.weave.client.WeaveSSLSocketFactory.java
org.emergent.android.weave.client.WeaveTransport.java
org.emergent.android.weave.client.WeaveUtil.java
org.greendroid.QuickActionGrid.java
org.greendroid.QuickActionWidget.java
org.greendroid.QuickAction.java
org.zirco.controllers.Controller.java
org.zirco.events.EventConstants.java
org.zirco.events.EventController.java
org.zirco.events.IDownloadEventsListener.java
org.zirco.model.DbAdapter.java
org.zirco.model.UrlSuggestionItemComparator.java
org.zirco.model.adapters.BookmarksCursorAdapter.java
org.zirco.model.adapters.DownloadListAdapter.java
org.zirco.model.adapters.HistoryExpandableListAdapter.java
org.zirco.model.adapters.UrlSuggestionCursorAdapter.java
org.zirco.model.adapters.WeaveBookmarksCursorAdapter.java
org.zirco.model.items.BookmarkItem.java
org.zirco.model.items.DownloadItem.java
org.zirco.model.items.HistoryItem.java
org.zirco.model.items.UrlSuggestionItem.java
org.zirco.model.items.WeaveBookmarkItem.java
org.zirco.providers.BookmarksProviderWrapper.java
org.zirco.providers.WeaveColumns.java
org.zirco.providers.WeaveContentProvider.java
org.zirco.providers.ZircoBookmarksContentProvider.java
org.zirco.sync.ISyncListener.java
org.zirco.sync.WeaveSyncTask.java
org.zirco.ui.activities.AboutActivity.java
org.zirco.ui.activities.AdBlockerWhiteListActivity.java
org.zirco.ui.activities.BookmarksHistoryActivity.java
org.zirco.ui.activities.BookmarksListActivity.java
org.zirco.ui.activities.ChangelogActivity.java
org.zirco.ui.activities.DownloadsListActivity.java
org.zirco.ui.activities.EditBookmarkActivity.java
org.zirco.ui.activities.HistoryListActivity.java
org.zirco.ui.activities.IToolbarsContainer.java
org.zirco.ui.activities.MainActivity.java
org.zirco.ui.activities.MobileViewListActivity.java
org.zirco.ui.activities.WeaveBookmarksListActivity.java
org.zirco.ui.activities.preferences.BaseSpinnerCustomPreferenceActivity.java
org.zirco.ui.activities.preferences.HomepagePreferenceActivity.java
org.zirco.ui.activities.preferences.PreferencesActivity.java
org.zirco.ui.activities.preferences.SearchUrlPreferenceActivity.java
org.zirco.ui.activities.preferences.UserAgentPreferenceActivity.java
org.zirco.ui.activities.preferences.WeavePreferencesActivity.java
org.zirco.ui.activities.preferences.WeaveServerPreferenceActivity.java
org.zirco.ui.components.CustomWebViewClient.java
org.zirco.ui.components.CustomWebView.java
org.zirco.ui.runnables.DownloadRunnable.java
org.zirco.ui.runnables.FaviconUpdaterRunnable.java
org.zirco.ui.runnables.HideToolbarsRunnable.java
org.zirco.ui.runnables.HistoryUpdater.java
org.zirco.ui.runnables.XmlHistoryBookmarksExporter.java
org.zirco.ui.runnables.XmlHistoryBookmarksImporter.java
org.zirco.utils.AnimationManager.java
org.zirco.utils.ApplicationUtils.java
org.zirco.utils.Constants.java
org.zirco.utils.DateUtils.java
org.zirco.utils.IOUtils.java
org.zirco.utils.ProxyChangeReceiver.java
org.zirco.utils.ProxySettings.java
org.zirco.utils.UrlUtils.java