/*
 * Copyright (C) 2014 Gregory S. Meiste  <http://gregmeiste.com>
 *
 * 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.meiste.tempalarm.sync;

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.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;

import com.meiste.tempalarm.provider.RasPiContract;

import timber.log.Timber;

public class SyncAdapter extends AbstractThreadedSyncAdapter {

    /**
     * Whether there is any network connected.
     */
    private static boolean isNetworkConnected(final Context context) {
        final ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager == null) {
            return false;
        }
        final NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return (activeNetworkInfo != null) && activeNetworkInfo.isConnected();
    }

    public static boolean requestSync(final Context context, final boolean isUserRequested) {
        if (isUserRequested && !isNetworkConnected(context)) {
            return false;
        }

        final Account account = AccountUtils.getAccount(context);
        if (account == null) {
            return false;
        }

        Timber.d("Requesting network synchronization");

        final Bundle b = new Bundle();
        b.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, isUserRequested);
        b.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, isUserRequested);
        ContentResolver.requestSync(account, RasPiContract.CONTENT_AUTHORITY, b);

        return true;
    }

    public SyncAdapter(final Context context, final boolean autoInitialize) {
        super(context, autoInitialize);
    }

    @Override
    public void onPerformSync(final Account account, final Bundle extras, final String authority,
                              final ContentProviderClient provider, final SyncResult syncResult) {
        Timber.d("Beginning network sync for " + account.name);

        // TODO

        Timber.d("Network synchronization complete");
    }
}

@@ -998,16 +998,316 @@
 undle;%0A%0A
+import com.google.api.client.extensions.android.http.AndroidHttp;%0Aimport com.google.api.client.extensions.android.json.AndroidJsonFactory;%0Aimport com.meiste.tempalarm.R;%0Aimport com.meiste.tempalarm.backend.temperature.Temperature;%0Aimport com.meiste.tempalarm.backend.temperature.model.SettingRecord;%0A
 import c
@@ -1351,16 +1351,45 @@
 tract;%0A%0A
+import java.io.IOException;%0A%0A
 import t
@@ -1471,16 +1471,61 @@
 pter %7B%0A%0A
+    private final Temperature mTempService;%0A%0A
     /**%0A
@@ -2870,302 +2870,979 @@
     
-%7D%0A%0A    @Override%0A    public void onPerformSync(final Account account, final Bundle extras, final String authority,%0A                              final ContentProviderClient provider, final SyncResult syncResult) %7B%0A        Timber.d(%22Beginning network sync for %22 + account.name);%0A%0A        // TODO
+    Timber.d(%22Creating sync adapter%22);%0A%0A        mTempService = new Temperature.Builder(AndroidHttp.newCompatibleTransport(),%0A                new AndroidJsonFactory(), AccountUtils.getCredential(context))%0A                .setApplicationName(context.getString(R.string.app_name))%0A                .build();%0A    %7D%0A%0A    @Override%0A    public void onPerformSync(final Account account, final Bundle extras, final String authority,%0A                              final ContentProviderClient provider, final SyncResult syncResult) %7B%0A        Timber.d(%22Beginning network sync for %22 + account.name);%0A%0A        try %7B%0A            // TODO: Download temp records. Downloading report rate for now to prove setup.%0A            final SettingRecord record = mTempService.temperatureEndpoint().getReportRate().execute();%0A            Timber.i(%22Report rate is %25s seconds%22, record.getValue());%0A        %7D catch (final IOException e) %7B%0A            Timber.e(%22Failed to download report rate%22);%0A        %7D
 %0A%0A  

Add TemperatureEndpoint to SyncAdapter