Back to project page groceryviewer.
The source code is released under:
GNU General Public License
If you think the Android project groceryviewer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.github.knrajago.groceryviewer.syncadapters; //w w w .j a v a2s . com import static com.github.knrajago.groceryviewer.constants.GroceryViewerConstants.APPLICATION_TAG; import static com.github.knrajago.groceryviewer.constants.GroceryViewerConstants.CONTENT_PROVIDER_URI; import static com.github.knrajago.groceryviewer.constants.GroceryViewerConstants.ITEM_COL; import static com.github.knrajago.groceryviewer.constants.GroceryViewerConstants.LAST_UPDATE_DATE_TIME_KEY; import static com.github.knrajago.groceryviewer.constants.GroceryViewerConstants.MALIGAI_LIST_XLS_TITLE; import static com.github.knrajago.groceryviewer.constants.GroceryViewerConstants.PREF_NAME; import static com.github.knrajago.groceryviewer.constants.GroceryViewerConstants.SPREADSHEETS_API_URL; import static com.github.knrajago.groceryviewer.constants.GroceryViewerConstants.TOKEN_KEY; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.List; import android.accounts.Account; import android.content.AbstractThreadedSyncAdapter; import android.content.ContentProviderClient; import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.content.SyncResult; import android.net.Uri; import android.os.Bundle; import android.util.Log; import com.google.gdata.client.spreadsheet.SpreadsheetQuery; import com.google.gdata.client.spreadsheet.SpreadsheetService; import com.google.gdata.data.DateTime; import com.google.gdata.data.spreadsheet.CellEntry; import com.google.gdata.data.spreadsheet.CellFeed; import com.google.gdata.data.spreadsheet.SpreadsheetEntry; import com.google.gdata.data.spreadsheet.SpreadsheetFeed; import com.google.gdata.data.spreadsheet.WorksheetEntry; import com.google.gdata.util.AuthenticationException; import com.google.gdata.util.ServiceException; public class GoogleSpreadsheetRefresher extends AbstractThreadedSyncAdapter { public GoogleSpreadsheetRefresher(Context context, boolean autoInitialize) { super(context, autoInitialize); } public GoogleSpreadsheetRefresher(Context context, boolean autoInitialize, boolean allowParallelSyncs) { super(context, autoInitialize, allowParallelSyncs); } @Override public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) { Log.i("Groceryviewer", "onPerformSync() - start"); SpreadsheetService service = getSpreadsheetService(); ContentResolver resolver = this.getContext().getContentResolver(); Uri uri = Uri.parse(CONTENT_PROVIDER_URI); resolver.delete(uri, "1", null); String token = extras.getString(TOKEN_KEY); try { Log.i(APPLICATION_TAG, account.name); service.setAuthSubToken(token); //service.setUserToken(token); URL spreadSheetUrl = new URL(SPREADSHEETS_API_URL); SpreadsheetQuery query = new SpreadsheetQuery(spreadSheetUrl); query.setTitleQuery(MALIGAI_LIST_XLS_TITLE); query.setTitleExact(true); SpreadsheetFeed spreadSheetFeed = service.getFeed(query, SpreadsheetFeed.class); Log.i("Groceryviewer", "Spreadsheet feed got"); DateTime updDtTm = spreadSheetFeed.getUpdated(); long updDtTmLong = updDtTm.getValue(); long storedLstEditedDtTm = getLastUpdateTime(this.getContext()); if (storedLstEditedDtTm < updDtTmLong) { setLastUpdateTime(this.getContext(), updDtTmLong); List<SpreadsheetEntry> entryList = spreadSheetFeed.getEntries(); if (entryList == null) { // } else { resolver.delete(uri, "1", null); Log.i(APPLICATION_TAG, "Deleted all rows."); for (SpreadsheetEntry oneSheetEntry : entryList) { Log.i("Groceryviewer", "sheets"); List<WorksheetEntry> wrkSheetList = oneSheetEntry.getWorksheets(); for (WorksheetEntry oneWrk : wrkSheetList) { Log.i("Groceryviewer", "Sheet Name : " + oneWrk.getTitle().getPlainText()); } WorksheetEntry wrkShtEntry = wrkSheetList.get(0); URL cellFeedUrl= wrkShtEntry.getCellFeedUrl(); CellFeed celFeed = service.getFeed (cellFeedUrl, CellFeed.class); List<CellEntry> cellEntryList = celFeed.getEntries(); String item = null; for (CellEntry oneCell : cellEntryList) { Log.i("Groceryviewer","cell"); item = oneCell.getCell().getValue(); ContentValues values = new ContentValues(); values.put(ITEM_COL, item); Log.i("GroceryViewer", "Trying to insert - " + item); resolver.insert(uri, values); } } } } else { Log.i("GroceryViewer", "The last update time stored is the latest. So, no need to update"); } } catch (AuthenticationException e) { Log.e("GroceryViewer", "Refresh Data from Google XLS - AuthenticationException", e); } catch (MalformedURLException e) { Log.e("GroceryViewer", "Refresh Data from Google XLS - MalformedURLException", e); } catch (IOException e) { Log.e("GroceryViewer", "Refresh Data from Google XLS - IOException", e); } catch (ServiceException e) { Log.e("GroceryViewer", "Refresh Data from Google XLS - ServiceException", e); } resolver.notifyChange(uri, null); } private SpreadsheetService getSpreadsheetService () { final SpreadsheetService service = new SpreadsheetService("MySpreadsheetIntegration"); service.setProtocolVersion(SpreadsheetService.Versions.V3); return service; } private void setLastUpdateTime(Context pContext, long pLastUpdateTime) { SharedPreferences lastUpdPrefrence = pContext.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); Editor edit = lastUpdPrefrence.edit(); edit.clear(); edit.putLong(LAST_UPDATE_DATE_TIME_KEY, pLastUpdateTime); edit.commit(); } private long getLastUpdateTime(Context pContext) { SharedPreferences lastUpdPrefrence = pContext.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); return lastUpdPrefrence.getLong(LAST_UPDATE_DATE_TIME_KEY, -1); } }