Here is my code:

DriveSyncAdapter.java:
public class DriveSyncAdapter extends AbstractThreadedSyncAdapter {

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

    @Override
    public void onPerformSync(Account account, Bundle extras, String 
authority, ContentProviderClient provider,
            SyncResult syncResult) { 
    }
}

DriveSyncService.java
public class DriveSyncService extends Service {

    private static final Object sSyncAdapterLock = new Object();
    private static DriveSyncAdapter sSyncAdapter = null;

    @Override
    public void onCreate() {
        synchronized (sSyncAdapterLock) {
            if (sSyncAdapter == null) {
                sSyncAdapter = new 
DriveSyncAdapter(getApplicationContext(), true);
            }
        }
    }
    
    @Override
    public IBinder onBind(Intent intent) {
        return sSyncAdapter.getSyncAdapterBinder();
    }
}

DummyContentProvider.java
public class DummyContentProvider extends ContentProvider {

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }

    @Override
    public boolean onCreate() {
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, 
String[] selectionArgs, String sortOrder) {
        return null;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, 
String[] selectionArgs) {
        return 0;
    }
}

AuthenticatorAsyncTask.java (this task is execute from my main Activity)
public class AuthenticatorAsyncTask extends AsyncTask<Void, Void, Boolean> {
    
    private final Context context;
    private final String accountname;
    private Account account;
    private static final String AUTHORITY = "com.test.testapp";
    private static long FOUR_HOURS = 4 * 60 * 60; 

    protected AuthenticatorAsyncTask(Context context, String accountname) {
        this.context = context;
        this.accountname = accountname;
        AccountManager mgr = AccountManager.get(context);
        Account accounts[] = mgr.getAccounts();
        for(int i=0; i<accounts.length; i++) {
            if(accounts[i].name.equals(accountname)) {
                account = accounts[i];
            }
        }
    }
    
    @Override
    protected Boolean doInBackground(Void... params) {
        if(getAccessToken(context, accountname) == null) {
            return false;
        }
        return true;
    }
    
    @Override 
    protected void onPostExecute(Boolean result) {
        ContentResolver.setSyncAutomatically(account, AUTHORITY, true);
        ContentResolver.addPeriodicSync(account, AUTHORITY, new Bundle(), 
FOUR_HOURS);
        ContentResolver.requestSync(account, AUTHORITY, new Bundle());
    }
    
    private String getAccessToken(Context context, String accountname) {
        try {
            return GoogleAuthUtil.getToken(context, accountname, "oauth2:" 
+ DriveScopes.DRIVE_READONLY);
        } catch(UserRecoverableAuthException e) {
            context.startActivity(e.getIntent());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (GoogleAuthException e) {
            e.printStackTrace();
        }
        
        return null;
    }
    
    public static void authenticate(Context context, String accountname) {
        AuthenticatorAsyncTask task = new AuthenticatorAsyncTask(context, 
accountname);
        task.execute();
    }
}

AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android";
    package="com.test.testapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_SYNC_STATS" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />   
 

    <application
        android:allowBackup="false"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:taskAffinity="" 
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>                   
        </activity>
                
    <service android:name=".DriveSyncService" android:exported="true"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.content.SyncAdapter" />
        </intent-filter>
        <meta-data
            android:name="android.content.SyncAdapter"
            android:resource="@xml/syncadapter" />
    </service>   
    
    <provider android:name=".DummyContentProvider"
         android:syncable="true"
         android:authorities="com.test.testapp.dummy">
        
    </provider>
    </application> 

</manifest>

syncadapter.xml :
<?xml version="1.0" encoding="utf-8"?>
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android";
        android:contentAuthority="com.test.testapp" 
        android:accountType="com.google" />

On Tuesday, December 11, 2012 1:58:38 PM UTC+11, rukiman wrote:
>
> I'm very confused with this. I'm trying to sync some files and folders in 
> google drive with Android. I came across a Google IO presentation but there 
> was no code attached to it. Using parts of it and what I can google around 
> (there is lots of confusing thoughts about AbstractThreadedSyncAdapter on 
> the net). i.e needs content provider for it to work others say it doesn't 
> etc. I have implemented it by piecing all the information I have worked 
> out. My application makes use of Google play services to call 
> GoogleAuthUtil.getToken() which displays an activity the first time asking 
> the user to confirm permissions to the application. I have a dummy content 
> provider with no functions implemented. However my application does not 
> appear in the Accounts & Sync android settings and the onPerformSync() is 
> not called. 
>
> Can anyone point me to a working example of hopefully using Google drive 
> to sync OR an example of  AbstractThreadedSyncAdapter with a custom 
> provider where the sync is working correctly? 
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to