/* * Copyright 2012 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.android.apps.iosched.sync; import static com.google.android.apps.iosched.util.LogUtils.LOGE; import static com.google.android.apps.iosched.util.LogUtils.LOGI; import static com.google.android.apps.iosched.util.LogUtils.makeLogTag; import java.io.IOException; import java.io.InputStream; import java.util.regex.Pattern; import java.util.zip.GZIPInputStream; import org.apache.http.Header; import org.apache.http.HeaderElement; import org.apache.http.HttpEntity; import org.apache.http.HttpRequest; import org.apache.http.HttpRequestInterceptor; import org.apache.http.HttpResponse; import org.apache.http.HttpResponseInterceptor; import org.apache.http.client.HttpClient; import org.apache.http.entity.HttpEntityWrapper; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.params.HttpProtocolParams; import org.apache.http.protocol.HttpContext; import android.accounts.Account; import android.content.AbstractThreadedSyncAdapter; import android.content.ContentProviderClient; import android.content.ContentResolver; import android.content.Context; import android.content.SyncResult; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Bundle; import android.text.format.DateUtils; import com.google.android.apps.iosched.BuildConfig; import com.google.android.apps.iosched.io.gdocs.LocalExecutor; import com.google.android.apps.iosched.io.gdocs.RemoteExecutor; import com.google.android.apps.iosched.util.AccountUtils; /** * Sync adapter for Google I/O data. Note that this sync adapter makes heavy use of a * "conference API" that is not currently open source. */ public class SyncAdapter extends AbstractThreadedSyncAdapter { private static final String TAG = makeLogTag(SyncAdapter.class); private static final Pattern sSanitizeAccountNamePattern = Pattern.compile("(.).*?(.?)@"); private final Context mContext; private SyncHelper mSyncHelper; public SyncAdapter(Context context, boolean autoInitialize) { super(context, autoInitialize); mContext = context; //noinspection ConstantConditions,PointlessBooleanExpression if (!BuildConfig.DEBUG) { Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread thread, Throwable throwable) { LOGE(TAG, "Uncaught sync exception, suppressing UI in release build.", throwable); } }); } } @Override public void onPerformSync(final Account account, Bundle extras, String authority, final ContentProviderClient provider, final SyncResult syncResult) { final boolean uploadOnly = extras.getBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, false); final boolean manualSync = extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false); final boolean initialize = extras.getBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, false); final String logSanitizedAccountName = sSanitizeAccountNamePattern .matcher(account.name).replaceAll("$1...$2@"); if (uploadOnly) { return; } LOGI(TAG, "Beginning sync for account " + logSanitizedAccountName + "," + " uploadOnly=" + uploadOnly + " manualSync=" + manualSync + " initialize=" + initialize); if (initialize) { String chosenAccountName = AccountUtils.getChosenAccountName(mContext); boolean isChosenAccount = chosenAccountName != null && chosenAccountName.equals(account.name); ContentResolver.setIsSyncable(account, authority, isChosenAccount ? 1 : 0); if (!isChosenAccount) { ++syncResult.stats.numAuthExceptions; return; } } // Perform a sync using SyncHelper if (mSyncHelper == null) { mSyncHelper = new SyncHelper(mContext); } try { mSyncHelper.performSync(syncResult, SyncHelper.FLAG_SYNC_LOCAL | SyncHelper.FLAG_SYNC_REMOTE); } catch (IOException e) { ++syncResult.stats.numIoExceptions; LOGE(TAG, "Error syncing data for I/O 2012.", e); } } }