Reviewed integration with sychronization framework to fix problems in Android 4.4
authorDavid A. Velasco <dvelasco@solidgear.es>
Mon, 11 Nov 2013 09:08:16 +0000 (10:08 +0100)
committerDavid A. Velasco <dvelasco@solidgear.es>
Mon, 11 Nov 2013 09:08:16 +0000 (10:08 +0100)
13 files changed:
AndroidManifest.xml
project.properties
res/values/setup.xml
res/xml/syncadapter_files.xml
src/com/owncloud/android/authentication/AccountAuthenticator.java
src/com/owncloud/android/authentication/AccountAuthenticatorService.java
src/com/owncloud/android/datamodel/FileDataStorageManager.java
src/com/owncloud/android/media/MediaServiceBinder.java
src/com/owncloud/android/syncadapter/AbstractOwnCloudSyncAdapter.java
src/com/owncloud/android/syncadapter/ContactSyncAdapter.java
src/com/owncloud/android/syncadapter/FileSyncAdapter.java
src/com/owncloud/android/syncadapter/FileSyncService.java
src/com/owncloud/android/ui/activity/FileDisplayActivity.java

index 2035a9d..767b171 100644 (file)
@@ -38,7 +38,7 @@
     
     <uses-sdk
         android:minSdkVersion="8"
     
     <uses-sdk
         android:minSdkVersion="8"
-        android:targetSdkVersion="13" />
+        android:targetSdkVersion="19" />
 
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
     </uses-permission>
 
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
     </uses-permission>
         </service>
         <service
             android:name=".syncadapter.FileSyncService"
         </service>
         <service
             android:name=".syncadapter.FileSyncService"
-            android:exported="true" >
+            android:exported="true" 
+            android:process=":sync">
             <intent-filter>
                 <action android:name="android.content.SyncAdapter" />
             </intent-filter>
             <intent-filter>
                 <action android:name="android.content.SyncAdapter" />
             </intent-filter>
index 716944a..c9e5840 100644 (file)
@@ -8,5 +8,5 @@
 # project structure.
 
 # Project target.
 # project structure.
 
 # Project target.
-target=android-17
+target=android-19
 android.library.reference.1=actionbarsherlock/library
 android.library.reference.1=actionbarsherlock/library
index a2fb403..226b618 100644 (file)
@@ -2,8 +2,8 @@
 <resources>
     <!-- App name  and other strings-->
     <string name="app_name">ownCloud</string>
 <resources>
     <!-- App name  and other strings-->
     <string name="app_name">ownCloud</string>
-    <string name="account_type">owncloud</string>
-    <string name="authority">org.owncloud</string>
+    <string name="account_type">owncloud</string>      <!-- better if was a domain name; but changing it now would require migrate accounts when the app is updated -->
+    <string name="authority">org.owncloud</string>     <!-- better if was the app package with ".provider" appended ; it identifies the provider -->
     <string name ="db_file">owncloud.db</string>
     <string name ="db_name">ownCloud</string>
     <string name ="data_folder">owncloud</string>
     <string name ="db_file">owncloud.db</string>
     <string name ="db_name">ownCloud</string>
     <string name ="data_folder">owncloud</string>
index 25b018c..8c8d8b3 100644 (file)
@@ -25,4 +25,7 @@
     android:contentAuthority="@string/authority"
     android:accountType="@string/account_type"
     android:supportsUploading="true"
     android:contentAuthority="@string/authority"
     android:accountType="@string/account_type"
     android:supportsUploading="true"
+    android:userVisible="true"
+    android:allowParallelSyncs="true"
+    android:isAlwaysSyncable="true"
 />
 />
index a2a38e8..ff0782d 100644 (file)
@@ -48,15 +48,6 @@ public class AccountAuthenticator extends AbstractAccountAuthenticator {
      * Is used by android system to assign accounts to authenticators. Should be
      * used by application and all extensions.
      */
      * Is used by android system to assign accounts to authenticators. Should be
      * used by application and all extensions.
      */
-    /* These constants are now in MainApp
-         public static final String ACCOUNT_TYPE = "owncloud";
-         public static final String AUTHORITY = "org.owncloud";
-         public static final String AUTH_TOKEN_TYPE = "org.owncloud";
-         public static final String AUTH_TOKEN_TYPE_PASSWORD = "owncloud.password";
-         public static final String AUTH_TOKEN_TYPE_ACCESS_TOKEN = "owncloud.oauth2.access_token";
-         public static final String AUTH_TOKEN_TYPE_REFRESH_TOKEN = "owncloud.oauth2.refresh_token";
-         public static final String AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE = "owncloud.saml.web_sso.session_cookie";
-    */
     public static final String KEY_AUTH_TOKEN_TYPE = "authTokenType";
     public static final String KEY_REQUIRED_FEATURES = "requiredFeatures";
     public static final String KEY_LOGIN_OPTIONS = "loginOptions";
     public static final String KEY_AUTH_TOKEN_TYPE = "authTokenType";
     public static final String KEY_REQUIRED_FEATURES = "requiredFeatures";
     public static final String KEY_LOGIN_OPTIONS = "loginOptions";
index 4c91f6e..c479cea 100644 (file)
@@ -25,7 +25,6 @@ import android.os.IBinder;
 public class AccountAuthenticatorService extends Service {
 
     private AccountAuthenticator mAuthenticator;
 public class AccountAuthenticatorService extends Service {
 
     private AccountAuthenticator mAuthenticator;
-    //static final public String ACCOUNT_TYPE = "owncloud";
 
     @Override
     public void onCreate() {
 
     @Override
     public void onCreate() {
index 635e10c..0ad985e 100644 (file)
@@ -391,14 +391,16 @@ public class FileDataStorageManager {
         if (id > FileDataStorageManager.ROOT_PARENT_ID) {
             Log_OC.d(TAG, "Updating size of " + id);
             if (getContentResolver() != null) {
         if (id > FileDataStorageManager.ROOT_PARENT_ID) {
             Log_OC.d(TAG, "Updating size of " + id);
             if (getContentResolver() != null) {
-                getContentResolver().update(ProviderTableMeta.CONTENT_URI_DIR, null,
+                getContentResolver().update(ProviderTableMeta.CONTENT_URI_DIR, 
+                        new ContentValues(),    // won't be used, but cannot be null; crashes in KLP
                         ProviderTableMeta._ID + "=?",
                         new String[] { String.valueOf(id) });
             } else {
                 try {
                         ProviderTableMeta._ID + "=?",
                         new String[] { String.valueOf(id) });
             } else {
                 try {
-                    getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_DIR, null,
-                        ProviderTableMeta._ID + "=?",
-                        new String[] { String.valueOf(id) });
+                    getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_DIR, 
+                            new ContentValues(),    // won't be used, but cannot be null; crashes in KLP
+                            ProviderTableMeta._ID + "=?",
+                            new String[] { String.valueOf(id) });
                     
                 } catch (RemoteException e) {
                     Log_OC.e(TAG, "Exception in update of folder size through compatibility patch " + e.getMessage());
                     
                 } catch (RemoteException e) {
                     Log_OC.e(TAG, "Exception in update of folder size through compatibility patch " + e.getMessage());
index 4fab8bd..ab018f6 100644 (file)
@@ -176,6 +176,12 @@ public class MediaServiceBinder extends Binder implements MediaController.MediaP
         return (currentState == MediaService.State.PLAYING || currentState == MediaService.State.PAUSED);
     }
 
         return (currentState == MediaService.State.PLAYING || currentState == MediaService.State.PAUSED);
     }
 
+
+    @Override
+    public int getAudioSessionId() {
+        return 1; // not really used
+    }
+
 }
 
 
 }
 
 
index a3e7926..6e81557 100644 (file)
@@ -53,7 +53,7 @@ public abstract class AbstractOwnCloudSyncAdapter extends
 \r
     private AccountManager accountManager;\r
     private Account account;\r
 \r
     private AccountManager accountManager;\r
     private Account account;\r
-    private ContentProviderClient contentProvider;\r
+    private ContentProviderClient mContentProviderClient;\r
     private FileDataStorageManager mStoreManager;\r
 \r
     private WebdavClient mClient = null;\r
     private FileDataStorageManager mStoreManager;\r
 \r
     private WebdavClient mClient = null;\r
@@ -63,6 +63,11 @@ public abstract class AbstractOwnCloudSyncAdapter extends
         this.setAccountManager(AccountManager.get(context));\r
     }\r
 \r
         this.setAccountManager(AccountManager.get(context));\r
     }\r
 \r
+    public AbstractOwnCloudSyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs) {\r
+        super(context, autoInitialize, allowParallelSyncs);\r
+        this.setAccountManager(AccountManager.get(context));\r
+    }\r
+\r
     public AccountManager getAccountManager() {\r
         return accountManager;\r
     }\r
     public AccountManager getAccountManager() {\r
         return accountManager;\r
     }\r
@@ -79,12 +84,12 @@ public abstract class AbstractOwnCloudSyncAdapter extends
         this.account = account;\r
     }\r
 \r
         this.account = account;\r
     }\r
 \r
-    public ContentProviderClient getContentProvider() {\r
-        return contentProvider;\r
+    public ContentProviderClient getContentProviderClient() {\r
+        return mContentProviderClient;\r
     }\r
 \r
     }\r
 \r
-    public void setContentProvider(ContentProviderClient contentProvider) {\r
-        this.contentProvider = contentProvider;\r
+    public void setContentProviderClient(ContentProviderClient contentProvider) {\r
+        this.mContentProviderClient = contentProvider;\r
     }\r
 \r
     public void setStorageManager(FileDataStorageManager storage_manager) {\r
     }\r
 \r
     public void setStorageManager(FileDataStorageManager storage_manager) {\r
index b07c048..662c8a6 100644 (file)
@@ -53,7 +53,7 @@ public class ContactSyncAdapter extends AbstractOwnCloudSyncAdapter {
     public void onPerformSync(Account account, Bundle extras, String authority,
             ContentProviderClient provider, SyncResult syncResult) {
         setAccount(account);
     public void onPerformSync(Account account, Bundle extras, String authority,
             ContentProviderClient provider, SyncResult syncResult) {
         setAccount(account);
-        setContentProvider(provider);
+        setContentProviderClient(provider);
         Cursor c = getLocalContacts(false);
         if (c.moveToFirst()) {
             do {
         Cursor c = getLocalContacts(false);
         if (c.moveToFirst()) {
             do {
index 7a00b2e..5b0376c 100644 (file)
@@ -98,7 +98,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
     
     
     /**
     
     
     /**
-     * Creates an {@link FileSyncAdapter}
+     * Creates a {@link FileSyncAdapter}
      *
      * {@inheritDoc}
      */
      *
      * {@inheritDoc}
      */
@@ -108,11 +108,21 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
 
     
     /**
 
     
     /**
+     * Creates a {@link FileSyncAdapter}
+     *
+     * {@inheritDoc}
+     */
+    public FileSyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs) {
+        super(context, autoInitialize, allowParallelSyncs);
+    }
+
+    
+    /**
      * {@inheritDoc}
      */
     @Override
     public synchronized void onPerformSync(Account account, Bundle extras,
      * {@inheritDoc}
      */
     @Override
     public synchronized void onPerformSync(Account account, Bundle extras,
-            String authority, ContentProviderClient provider,
+            String authority, ContentProviderClient providerClient,
             SyncResult syncResult) {
 
         mCancellation = false;
             SyncResult syncResult) {
 
         mCancellation = false;
@@ -127,8 +137,8 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
         mSyncResult.delayUntil = 60*60*24; // avoid too many automatic synchronizations
 
         this.setAccount(account);
         mSyncResult.delayUntil = 60*60*24; // avoid too many automatic synchronizations
 
         this.setAccount(account);
-        this.setContentProvider(provider);
-        this.setStorageManager(new FileDataStorageManager(account, provider));
+        this.setContentProviderClient(providerClient);
+        this.setStorageManager(new FileDataStorageManager(account, providerClient));
         try {
             this.initClientForCurrentAccount();
         } catch (IOException e) {
         try {
             this.initClientForCurrentAccount();
         } catch (IOException e) {
index 3c99975..dafc771 100644 (file)
@@ -17,6 +17,8 @@
  */\r
 package com.owncloud.android.syncadapter;\r
 \r
  */\r
 package com.owncloud.android.syncadapter;\r
 \r
+import com.owncloud.android.Log_OC;\r
+\r
 import android.app.Service;\r
 import android.content.Intent;\r
 import android.os.IBinder;\r
 import android.app.Service;\r
 import android.content.Intent;\r
 import android.os.IBinder;\r
@@ -37,6 +39,11 @@ public class FileSyncService extends Service {
     public static final String ACCOUNT_NAME = "ACCOUNT_NAME";\r
     public static final String SYNC_RESULT = "SYNC_RESULT";\r
 \r
     public static final String ACCOUNT_NAME = "ACCOUNT_NAME";\r
     public static final String SYNC_RESULT = "SYNC_RESULT";\r
 \r
+    // Storage for an instance of the sync adapter\r
+    private static FileSyncAdapter sSyncAdapter = null;\r
+    // Object to use as a thread-safe lock\r
+    private static final Object sSyncAdapterLock = new Object();\r
+    \r
     public static String getSyncMessage(){\r
         return FileSyncService.class.getName().toString() + SYNC_MESSAGE;\r
     }\r
     public static String getSyncMessage(){\r
         return FileSyncService.class.getName().toString() + SYNC_MESSAGE;\r
     }\r
@@ -45,6 +52,11 @@ public class FileSyncService extends Service {
      */\r
     @Override\r
     public void onCreate() {\r
      */\r
     @Override\r
     public void onCreate() {\r
+        synchronized (sSyncAdapterLock) {\r
+            if (sSyncAdapter == null) {\r
+                sSyncAdapter = new FileSyncAdapter(getApplicationContext(), true);\r
+            }\r
+        }\r
     }\r
 \r
     /*\r
     }\r
 \r
     /*\r
@@ -52,7 +64,7 @@ public class FileSyncService extends Service {
      */\r
     @Override\r
     public IBinder onBind(Intent intent) {\r
      */\r
     @Override\r
     public IBinder onBind(Intent intent) {\r
-       return new FileSyncAdapter(getApplicationContext(), true).getSyncAdapterBinder();\r
+       return sSyncAdapter.getSyncAdapterBinder();\r
     }\r
     \r
 }\r
     }\r
     \r
 }\r
index d58de5e..39b7bd8 100644 (file)
@@ -31,8 +31,10 @@ import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.content.IntentFilter.AuthorityEntry;
 import android.content.ServiceConnection;
 import android.content.SharedPreferences;
 import android.content.ServiceConnection;
 import android.content.SharedPreferences;
+import android.content.SyncRequest;
 import android.content.res.Resources.NotFoundException;
 import android.database.Cursor;
 import android.net.Uri;
 import android.content.res.Resources.NotFoundException;
 import android.database.Cursor;
 import android.net.Uri;
@@ -494,12 +496,27 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa
     }
 
     private void startSynchronization() {
     }
 
     private void startSynchronization() {
-        ContentResolver.cancelSync(null, MainApp.getAuthTokenType());   // cancel the current synchronizations of any ownCloud account
-        Bundle bundle = new Bundle();
-        bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
-        ContentResolver.requestSync(
-                getAccount(),
-                MainApp.getAuthTokenType(), bundle);
+        Log_OC.e(TAG, "Got to start sync");
+        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
+            Log_OC.e(TAG, "Canceling all syncs for " + MainApp.getAuthority());
+            ContentResolver.cancelSync(null, MainApp.getAuthority());   // cancel the current synchronizations of any ownCloud account
+            Bundle bundle = new Bundle();
+            bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
+            bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
+            Log_OC.e(TAG, "Requesting sync for " + getAccount().name + " at " + MainApp.getAuthority());
+            ContentResolver.requestSync(
+                    getAccount(),
+                    MainApp.getAuthority(), bundle);
+        } else {
+            Log_OC.e(TAG, "Requesting sync for " + getAccount().name + " at " + MainApp.getAuthority() + " with new API");
+            SyncRequest.Builder builder = new SyncRequest.Builder();
+            builder.setSyncAdapter(getAccount(), MainApp.getAuthority());
+            builder.setExpedited(true);
+            builder.setManual(true);
+            builder.syncOnce();
+            SyncRequest request = builder.build();
+            ContentResolver.requestSync(request);
+        }
     }
 
 
     }