package com.axelby.podax;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Service;
import android.content.AbstractThreadedSyncAdapter;
import android.content.ContentProviderClient;
import android.content.Context;
import android.content.Intent;
import android.content.SyncResult;
import android.database.Cursor;
import android.os.Bundle;
import android.os.IBinder;

public class GPodderSyncService extends Service {
    private static final Object _syncAdapterLock = new Object();
    private static GPodderSyncAdapter _syncAdapter = null;

    @Override
    public void onCreate() {
        synchronized (_syncAdapterLock) {
            if (_syncAdapter == null) {
                _syncAdapter = new GPodderSyncAdapter(getApplicationContext(), true);
            }
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return _syncAdapter.getSyncAdapterBinder();
    }

	private static class GPodderSyncAdapter extends AbstractThreadedSyncAdapter {
	
		private Context _context;
	
		public GPodderSyncAdapter(Context context, boolean autoInitialize) {
	        super(context, autoInitialize);
	        _context = context;
	    }
	
		@Override
		public void onPerformSync(Account account, Bundle extras, String authority,
				ContentProviderClient provider, SyncResult syncResult) {
			// find the last timestamp
			String[] projection = { SubscriptionProvider.COLUMN_GPODDER_SYNCTIME };
			Cursor c = _context.getContentResolver().query(SubscriptionProvider.URI, projection, null, null, SubscriptionProvider.COLUMN_GPODDER_SYNCTIME + " DESC");
			if (!c.moveToNext())
				return;
			int lastTimestamp = c.getInt(0);
			c.close();

			AccountManager accountManager = AccountManager.get(_context);
			GPodderClient client = new GPodderClient(_context, account.name, accountManager.getPassword(account));
			if (client.authenticate()) {
				client.getSubscriptionChanges(lastTimestamp);
				client.sendSubscriptions();
			}
		}
	
	}
}
package com.axelby.podax;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Service;
import android.content.AbstractThreadedSyncAdapter;
import android.content.ContentProviderClient;
import android.content.Context;
import android.content.Intent;
import android.content.SyncResult;
import android.database.Cursor;
import android.os.Bundle;
import android.os.IBinder;

public class GPodderSyncService extends Service {
    private static final Object _syncAdapterLock = new Object();
    private static GPodderSyncAdapter _syncAdapter = null;

    @Override
    public void onCreate() {
        synchronized (_syncAdapterLock) {
            if (_syncAdapter == null) {
                _syncAdapter = new GPodderSyncAdapter(getApplicationContext(), true);
            }
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return _syncAdapter.getSyncAdapterBinder();
    }

	private static class GPodderSyncAdapter extends AbstractThreadedSyncAdapter {
	
		private Context _context;
	
		public GPodderSyncAdapter(Context context, boolean autoInitialize) {
	        super(context, autoInitialize);
	        _context = context;
	    }
	
		@Override
		public void onPerformSync(Account account, Bundle extras, String authority,
				ContentProviderClient provider, SyncResult syncResult) {
			AccountManager accountManager = AccountManager.get(_context);
			GPodderClient client = new GPodderClient(_context, account.name, accountManager.getPassword(account));
			if (!client.authenticate())
				return;

			// find the last time we updated
			String[] projection = { SubscriptionProvider.COLUMN_GPODDER_SYNCTIME };
			Cursor c = _context.getContentResolver().query(SubscriptionProvider.URI, projection, null, null, SubscriptionProvider.COLUMN_GPODDER_SYNCTIME + " DESC");
			if (!c.moveToNext())
				return;
			int lastTimestamp = c.getInt(0);
			c.close();

			// update gpodder with new subscriptions
			client.sendSubscriptions();

			client.getSubscriptionChanges(lastTimestamp);
		}
	
	}
}
reorder gpodder sync operations