Browse Source

Reviewed integration with sychronization framework to fix problems in Android 4.4

David A. Velasco 11 years ago
parent
commit
cc39a76a90

+ 3 - 2
AndroidManifest.xml

@@ -38,7 +38,7 @@
     
     <uses-sdk
         android:minSdkVersion="8"
-        android:targetSdkVersion="13" />
+        android:targetSdkVersion="19" />
 
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
     </uses-permission>
@@ -112,7 +112,8 @@
         </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>

+ 1 - 1
project.properties

@@ -8,5 +8,5 @@
 # project structure.
 
 # Project target.
-target=android-17
+target=android-19
 android.library.reference.1=actionbarsherlock/library

+ 2 - 2
res/values/setup.xml

@@ -2,8 +2,8 @@
 <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>

+ 3 - 0
res/xml/syncadapter_files.xml

@@ -25,4 +25,7 @@
     android:contentAuthority="@string/authority"
     android:accountType="@string/account_type"
     android:supportsUploading="true"
+    android:userVisible="true"
+    android:allowParallelSyncs="true"
+    android:isAlwaysSyncable="true"
 />

+ 0 - 9
src/com/owncloud/android/authentication/AccountAuthenticator.java

@@ -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.
      */
-    /* 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";

+ 0 - 1
src/com/owncloud/android/authentication/AccountAuthenticatorService.java

@@ -25,7 +25,6 @@ import android.os.IBinder;
 public class AccountAuthenticatorService extends Service {
 
     private AccountAuthenticator mAuthenticator;
-    //static final public String ACCOUNT_TYPE = "owncloud";
 
     @Override
     public void onCreate() {

+ 6 - 4
src/com/owncloud/android/datamodel/FileDataStorageManager.java

@@ -391,14 +391,16 @@ public class FileDataStorageManager {
         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 {
-                    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());

+ 6 - 0
src/com/owncloud/android/media/MediaServiceBinder.java

@@ -176,6 +176,12 @@ public class MediaServiceBinder extends Binder implements MediaController.MediaP
         return (currentState == MediaService.State.PLAYING || currentState == MediaService.State.PAUSED);
     }
 
+
+    @Override
+    public int getAudioSessionId() {
+        return 1; // not really used
+    }
+
 }
 
 

+ 10 - 5
src/com/owncloud/android/syncadapter/AbstractOwnCloudSyncAdapter.java

@@ -53,7 +53,7 @@ public abstract class AbstractOwnCloudSyncAdapter extends
 
     private AccountManager accountManager;
     private Account account;
-    private ContentProviderClient contentProvider;
+    private ContentProviderClient mContentProviderClient;
     private FileDataStorageManager mStoreManager;
 
     private WebdavClient mClient = null;
@@ -63,6 +63,11 @@ public abstract class AbstractOwnCloudSyncAdapter extends
         this.setAccountManager(AccountManager.get(context));
     }
 
+    public AbstractOwnCloudSyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs) {
+        super(context, autoInitialize, allowParallelSyncs);
+        this.setAccountManager(AccountManager.get(context));
+    }
+
     public AccountManager getAccountManager() {
         return accountManager;
     }
@@ -79,12 +84,12 @@ public abstract class AbstractOwnCloudSyncAdapter extends
         this.account = account;
     }
 
-    public ContentProviderClient getContentProvider() {
-        return contentProvider;
+    public ContentProviderClient getContentProviderClient() {
+        return mContentProviderClient;
     }
 
-    public void setContentProvider(ContentProviderClient contentProvider) {
-        this.contentProvider = contentProvider;
+    public void setContentProviderClient(ContentProviderClient contentProvider) {
+        this.mContentProviderClient = contentProvider;
     }
 
     public void setStorageManager(FileDataStorageManager storage_manager) {

+ 1 - 1
src/com/owncloud/android/syncadapter/ContactSyncAdapter.java

@@ -53,7 +53,7 @@ public class ContactSyncAdapter extends AbstractOwnCloudSyncAdapter {
     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 {

+ 14 - 4
src/com/owncloud/android/syncadapter/FileSyncAdapter.java

@@ -98,7 +98,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
     
     
     /**
-     * Creates an {@link FileSyncAdapter}
+     * Creates a {@link FileSyncAdapter}
      *
      * {@inheritDoc}
      */
@@ -107,12 +107,22 @@ 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,
-            String authority, ContentProviderClient provider,
+            String authority, ContentProviderClient providerClient,
             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);
-        this.setContentProvider(provider);
-        this.setStorageManager(new FileDataStorageManager(account, provider));
+        this.setContentProviderClient(providerClient);
+        this.setStorageManager(new FileDataStorageManager(account, providerClient));
         try {
             this.initClientForCurrentAccount();
         } catch (IOException e) {

+ 13 - 1
src/com/owncloud/android/syncadapter/FileSyncService.java

@@ -17,6 +17,8 @@
  */
 package com.owncloud.android.syncadapter;
 
+import com.owncloud.android.Log_OC;
+
 import android.app.Service;
 import android.content.Intent;
 import android.os.IBinder;
@@ -37,6 +39,11 @@ public class FileSyncService extends Service {
     public static final String ACCOUNT_NAME = "ACCOUNT_NAME";
     public static final String SYNC_RESULT = "SYNC_RESULT";
 
+    // Storage for an instance of the sync adapter
+    private static FileSyncAdapter sSyncAdapter = null;
+    // Object to use as a thread-safe lock
+    private static final Object sSyncAdapterLock = new Object();
+    
     public static String getSyncMessage(){
         return FileSyncService.class.getName().toString() + SYNC_MESSAGE;
     }
@@ -45,6 +52,11 @@ public class FileSyncService extends Service {
      */
     @Override
     public void onCreate() {
+        synchronized (sSyncAdapterLock) {
+            if (sSyncAdapter == null) {
+                sSyncAdapter = new FileSyncAdapter(getApplicationContext(), true);
+            }
+        }
     }
 
     /*
@@ -52,7 +64,7 @@ public class FileSyncService extends Service {
      */
     @Override
     public IBinder onBind(Intent intent) {
-       return new FileSyncAdapter(getApplicationContext(), true).getSyncAdapterBinder();
+       return sSyncAdapter.getSyncAdapterBinder();
     }
     
 }

+ 23 - 6
src/com/owncloud/android/ui/activity/FileDisplayActivity.java

@@ -31,8 +31,10 @@ import android.content.Context;
 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.SyncRequest;
 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() {
-        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);
+        }
     }