Merge branch 'develop' into upgrading_account_manager
authorDavid A. Velasco <dvelasco@solidgear.es>
Wed, 15 Apr 2015 10:16:24 +0000 (12:16 +0200)
committerDavid A. Velasco <dvelasco@solidgear.es>
Wed, 15 Apr 2015 10:16:24 +0000 (12:16 +0200)
owncloud-android-library
src/com/owncloud/android/authentication/AccountUtils.java
src/com/owncloud/android/authentication/AuthenticatorActivity.java
src/com/owncloud/android/db/ProviderMeta.java
src/com/owncloud/android/files/InstantUploadBroadcastReceiver.java
src/com/owncloud/android/operations/GetServerInfoOperation.java
src/com/owncloud/android/providers/FileContentProvider.java
src/com/owncloud/android/syncadapter/ContactSyncAdapter.java [deleted file]
src/com/owncloud/android/syncadapter/ContactSyncService.java [deleted file]

index 9e76138..2e993f9 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 9e761387a0b406402684571f28c36c2d6d2b6301
+Subproject commit 2e993f9893739ca4bf0883b881b125030a9cad26
index bd3a8e7..bb2315d 100644 (file)
@@ -33,13 +33,9 @@ import android.content.SharedPreferences;
 import android.preference.PreferenceManager;\r
 \r
 public class AccountUtils {\r
-    public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php";\r
-    public static final String WEBDAV_PATH_2_0 = "/files/webdav.php";\r
-    public static final String WEBDAV_PATH_4_0 = "/remote.php/webdav";\r
+    public static final String WEBDAV_PATH_4_0_AND_LATER = "/remote.php/webdav";\r
     private static final String ODAV_PATH = "/remote.php/odav";\r
     private static final String SAML_SSO_PATH = "/remote.php/webdav";\r
-    public static final String CARDDAV_PATH_2_0 = "/apps/contacts/carddav.php";\r
-    public static final String CARDDAV_PATH_4_0 = "/remote/carddav.php";\r
     public static final String STATUS_PATH = "/status.php";\r
 \r
     /**\r
@@ -105,19 +101,6 @@ public class AccountUtils {
     }\r
     \r
 \r
-    /**\r
-     * Checks, whether or not there are any ownCloud accounts setup.\r
-     * \r
-     * @return true, if there is at least one account.\r
-     */\r
-    public static boolean accountsAreSetup(Context context) {\r
-        AccountManager accMan = AccountManager.get(context);\r
-        Account[] accounts = accMan\r
-                .getAccountsByType(MainApp.getAccountType());\r
-        return accounts.length > 0;\r
-    }\r
-    \r
-    \r
     public static boolean setCurrentOwnCloudAccount(Context context, String accountName) {\r
         boolean result = false;\r
         if (accountName != null) {\r
@@ -145,8 +128,10 @@ public class AccountUtils {
      * according to its version and the authorization method used.\r
      * \r
      * @param   version         Version of ownCloud server.\r
-     * @param   authTokenType   Authorization token type, matching some of the AUTH_TOKEN_TYPE_* constants in {@link AccountAuthenticator}. \r
-     * @return                  WebDAV path for given OC version and authorization method, null if OC version is unknown.\r
+     * @param   authTokenType   Authorization token type, matching some of the AUTH_TOKEN_TYPE_* constants in\r
+     *                          {@link AccountAuthenticator}.\r
+     * @return                  WebDAV path for given OC version and authorization method, null if OC version\r
+     *                          is unknown; versions prior to ownCloud 4 are not supported anymore\r
      */\r
     public static String getWebdavPath(OwnCloudVersion version, String authTokenType) {\r
         if (version != null) {\r
@@ -156,13 +141,7 @@ public class AccountUtils {
             if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(authTokenType)) {\r
                 return SAML_SSO_PATH;\r
             }\r
-            if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)\r
-                return WEBDAV_PATH_4_0;\r
-            if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0\r
-                    || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)\r
-                return WEBDAV_PATH_2_0;\r
-            if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)\r
-                return WEBDAV_PATH_1_2;\r
+            return WEBDAV_PATH_4_0_AND_LATER;\r
         }\r
         return null;\r
     }\r
index 1e4e8b4..6514ef7 100644 (file)
@@ -1119,13 +1119,9 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
 \r
     // TODO remove, if possible\r
     private String trimUrlWebdav(String url){       \r
-        if(url.toLowerCase().endsWith(AccountUtils.WEBDAV_PATH_4_0)){\r
-            url = url.substring(0, url.length() - AccountUtils.WEBDAV_PATH_4_0.length());             \r
-        } else if(url.toLowerCase().endsWith(AccountUtils.WEBDAV_PATH_2_0)){\r
-            url = url.substring(0, url.length() - AccountUtils.WEBDAV_PATH_2_0.length());             \r
-        } else if (url.toLowerCase().endsWith(AccountUtils.WEBDAV_PATH_1_2)){\r
-            url = url.substring(0, url.length() - AccountUtils.WEBDAV_PATH_1_2.length());             \r
-        } \r
+        if(url.toLowerCase().endsWith(AccountUtils.WEBDAV_PATH_4_0_AND_LATER)){\r
+            url = url.substring(0, url.length() - AccountUtils.WEBDAV_PATH_4_0_AND_LATER.length());\r
+        }\r
         return (url != null ? url : "");\r
     }\r
 \r
index 1f789d1..25e8fbd 100644 (file)
@@ -31,7 +31,7 @@ import com.owncloud.android.MainApp;
 public class ProviderMeta {\r
 \r
     public static final String DB_NAME = "filelist";\r
-    public static final int DB_VERSION = 9;\r
+    public static final int DB_VERSION = 10;\r
 \r
     private ProviderMeta() {\r
     }\r
index b52c36d..47f7127 100644 (file)
@@ -89,7 +89,7 @@ public class InstantUploadBroadcastReceiver extends BroadcastReceiver {
 
         Account account = AccountUtils.getCurrentOwnCloudAccount(context);
         if (account == null) {
-            Log_OC.w(TAG, "No owncloud account found for instant upload, aborting");
+            Log_OC.w(TAG, "No ownCloud account found for instant upload, aborting");
             return;
         }
 
index 8e91100..1b4b7a9 100644 (file)
@@ -23,7 +23,6 @@ package com.owncloud.android.operations;
 
 import java.util.ArrayList;
 
-import com.owncloud.android.MainApp;
 import com.owncloud.android.authentication.AccountUtils;
 import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.common.operations.RemoteOperation;
@@ -118,12 +117,8 @@ public class GetServerInfoOperation extends RemoteOperation {
             if (url.endsWith("/")) {
                 url = url.substring(0, url.length() - 1);
             }
-            if(url.toLowerCase().endsWith(AccountUtils.WEBDAV_PATH_4_0)){
-                url = url.substring(0, url.length() - AccountUtils.WEBDAV_PATH_4_0.length());
-            } else if(url.toLowerCase().endsWith(AccountUtils.WEBDAV_PATH_2_0)){
-                url = url.substring(0, url.length() - AccountUtils.WEBDAV_PATH_2_0.length());
-            } else if (url.toLowerCase().endsWith(AccountUtils.WEBDAV_PATH_1_2)){
-                url = url.substring(0, url.length() - AccountUtils.WEBDAV_PATH_1_2.length());
+            if(url.toLowerCase().endsWith(AccountUtils.WEBDAV_PATH_4_0_AND_LATER)){
+                url = url.substring(0, url.length() - AccountUtils.WEBDAV_PATH_4_0_AND_LATER.length());
             }
         }
         return url;
index 737c664..b50609a 100644 (file)
@@ -25,12 +25,16 @@ package com.owncloud.android.providers;
 import java.util.ArrayList;
 import java.util.HashMap;
 
+import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
 import com.owncloud.android.db.ProviderMeta;
 import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
+import com.owncloud.android.lib.common.accounts.AccountUtils;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.lib.resources.shares.ShareType;
 
+import android.accounts.Account;
+import android.accounts.AccountManager;
 import android.content.ContentProvider;
 import android.content.ContentProviderOperation;
 import android.content.ContentProviderResult;
@@ -684,9 +688,9 @@ public class FileContentProvider extends ContentProvider {
                 Log_OC.i("SQL", "Entering in the #3 ADD in onUpgrade");
                 db.beginTransaction();
                 try {
-                    db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
-                           " ADD COLUMN " + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA +
-                           " INTEGER " + " DEFAULT 0");
+                    db.execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
+                            " ADD COLUMN " + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA +
+                            " INTEGER " + " DEFAULT 0");
                 
                     db.execSQL("UPDATE " + ProviderTableMeta.FILE_TABLE_NAME + 
                            " SET " + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA + " = " +
@@ -707,7 +711,7 @@ public class FileContentProvider extends ContentProvider {
                 Log_OC.i("SQL", "Entering in the #4 ADD in onUpgrade");
                 db.beginTransaction();
                 try {
-                    db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
+                    db.execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
                             " ADD COLUMN " + ProviderTableMeta.FILE_ETAG + " TEXT " +
                             " DEFAULT NULL");
                     
@@ -765,11 +769,11 @@ public class FileContentProvider extends ContentProvider {
                 Log_OC.i("SQL", "Entering in the #7 ADD in onUpgrade");
                 db.beginTransaction();
                 try {
-                    db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
+                    db.execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
                             " ADD COLUMN " + ProviderTableMeta.FILE_PERMISSIONS + " TEXT " +
                             " DEFAULT NULL");
 
-                    db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
+                    db.execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
                             " ADD COLUMN " + ProviderTableMeta.FILE_REMOTE_ID + " TEXT " +
                             " DEFAULT NULL");
                     
@@ -787,7 +791,7 @@ public class FileContentProvider extends ContentProvider {
                 Log_OC.i("SQL", "Entering in the #8 ADD in onUpgrade");
                 db.beginTransaction();
                 try {
-                    db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
+                    db.execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
                             " ADD COLUMN " + ProviderTableMeta.FILE_UPDATE_THUMBNAIL + " INTEGER " +
                             " DEFAULT 0");
 
@@ -818,7 +822,57 @@ public class FileContentProvider extends ContentProvider {
             if (!upgraded)
                 Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion +
                         ", newVersion == " + newVersion);
+
+            if (oldVersion < 10 && newVersion >= 10) {
+                Log_OC.i("SQL", "Entering in the #10 ADD in onUpgrade");
+                upgraded = updateAccountName(db);
+            }
+             if (!upgraded)
+                Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion +
+                        ", newVersion == " + newVersion);
         }
     }
 
+
+    private boolean updateAccountName(SQLiteDatabase db){
+        AccountManager ama = AccountManager.get(getContext());
+        boolean upgradedResult = true;
+        boolean upgraded = false;
+        try {
+            // get accounts ALREADY UPDATED from AccountManager
+                       Account[] accounts = AccountManager.get(getContext()).getAccountsByType(MainApp.getAccountType());
+            String serverUrl, username, oldAccountName;
+                       for (Account account : accounts) {
+                // build new account name
+                serverUrl = ama.getUserData(account, AccountUtils.Constants.KEY_OC_BASE_URL);
+                username = account.name.substring(0, account.name.lastIndexOf('@'));
+                oldAccountName = AccountUtils.buildAccountNameOld(Uri.parse(serverUrl), username);
+
+                // update values in database
+                db.beginTransaction();
+                try{
+                    db.execSQL("UPDATE " + ProviderTableMeta.FILE_TABLE_NAME +
+                            " SET " + ProviderTableMeta.FILE_ACCOUNT_OWNER + " ='" +
+                            account.name + "' " +
+                            " WHERE " + ProviderTableMeta.FILE_ACCOUNT_OWNER + " ='" +
+                            oldAccountName + "' " );
+                    upgraded = true;
+                    db.setTransactionSuccessful();
+
+                    Log_OC.i("SQL", "Updated account in database: old name == " + oldAccountName +
+                            ", new name == " + account.name);
+
+                } catch (SQLException e){
+                    upgraded = false;
+                } finally {
+                    db.endTransaction();
+                }
+                upgradedResult = upgraded && upgradedResult;
+                       }
+               } catch (Exception e) {
+                       Log_OC.i("Exception", "Exception:" + e);
+               }
+
+        return upgradedResult;
+    }
 }
diff --git a/src/com/owncloud/android/syncadapter/ContactSyncAdapter.java b/src/com/owncloud/android/syncadapter/ContactSyncAdapter.java
deleted file mode 100644 (file)
index d3ab06c..0000000
+++ /dev/null
@@ -1,125 +0,0 @@
-/**
- *   ownCloud Android client application
- *
- *   Copyright (C) 2012 Bartek Przybylski
- *   Copyright (C) 2015 ownCloud Inc.
- *
- *   This program is free software: you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License version 2,
- *   as published by the Free Software Foundation.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-package com.owncloud.android.syncadapter;
-
-import java.io.FileInputStream;
-import java.io.IOException;
-
-import org.apache.http.client.methods.HttpPut;
-import org.apache.http.entity.ByteArrayEntity;
-
-import com.owncloud.android.authentication.AccountUtils;
-import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
-
-
-import android.accounts.Account;
-import android.accounts.AccountManager;
-import android.accounts.AuthenticatorException;
-import android.accounts.OperationCanceledException;
-import android.content.ContentProviderClient;
-import android.content.Context;
-import android.content.SyncResult;
-import android.content.res.AssetFileDescriptor;
-import android.database.Cursor;
-import android.net.Uri;
-import android.os.Bundle;
-import android.provider.ContactsContract;
-
-public class ContactSyncAdapter extends AbstractOwnCloudSyncAdapter {
-    private String mAddrBookUri;
-
-    public ContactSyncAdapter(Context context, boolean autoInitialize) {
-        super(context, autoInitialize);
-        mAddrBookUri = null;
-    }
-
-    @Override
-    public void onPerformSync(Account account, Bundle extras, String authority,
-            ContentProviderClient provider, SyncResult syncResult) {
-        setAccount(account);
-        setContentProviderClient(provider);
-        Cursor c = getLocalContacts(false);
-        if (c.moveToFirst()) {
-            do {
-                String lookup = c.getString(c
-                        .getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
-                String a = getAddressBookUri();
-                String uri = a + lookup + ".vcf";
-                FileInputStream f;
-                try {
-                    f = getContactVcard(lookup);
-                    HttpPut query = new HttpPut(uri);
-                    byte[] b = new byte[f.available()];
-                    f.read(b);
-                    query.setEntity(new ByteArrayEntity(b));
-                    fireRawRequest(query);
-                } catch (IOException e) {
-                    e.printStackTrace();
-                    return;
-                } catch (OperationCanceledException e) {
-                    // TODO Auto-generated catch block
-                    e.printStackTrace();
-                } catch (AuthenticatorException e) {
-                    // TODO Auto-generated catch block
-                    e.printStackTrace();
-                }
-            } while (c.moveToNext());
-            // } while (c.moveToNext());
-        }
-
-    }
-
-    private String getAddressBookUri() {
-        if (mAddrBookUri != null)
-            return mAddrBookUri;
-
-        AccountManager am = getAccountManager();
-        @SuppressWarnings("deprecation")
-        String uri = am.getUserData(getAccount(),
-                Constants.KEY_OC_URL).replace(
-                AccountUtils.WEBDAV_PATH_2_0, AccountUtils.CARDDAV_PATH_2_0);
-        uri += "/addressbooks/"
-                + getAccount().name.substring(0,
-                        getAccount().name.lastIndexOf('@')) + "/default/";
-        mAddrBookUri = uri;
-        return uri;
-    }
-
-    private FileInputStream getContactVcard(String lookupKey)
-            throws IOException {
-        Uri uri = Uri.withAppendedPath(
-                ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
-        AssetFileDescriptor fd = getContext().getContentResolver()
-                .openAssetFileDescriptor(uri, "r");
-        return fd.createInputStream();
-    }
-
-    private Cursor getLocalContacts(boolean include_hidden_contacts) {
-        return getContext().getContentResolver().query(
-                ContactsContract.Contacts.CONTENT_URI,
-                new String[] { ContactsContract.Contacts._ID,
-                        ContactsContract.Contacts.LOOKUP_KEY },
-                ContactsContract.Contacts.IN_VISIBLE_GROUP + " = ?",
-                new String[] { (include_hidden_contacts ? "0" : "1") },
-                ContactsContract.Contacts._ID + " DESC");
-    }
-
-}
diff --git a/src/com/owncloud/android/syncadapter/ContactSyncService.java b/src/com/owncloud/android/syncadapter/ContactSyncService.java
deleted file mode 100644 (file)
index d907bb4..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-/**
- *   ownCloud Android client application
- *
- *   Copyright (C) 2012 Bartek Przybylski
- *   Copyright (C) 2015 ownCloud Inc.
- *
- *   This program is free software: you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License version 2,
- *   as published by the Free Software Foundation.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-package com.owncloud.android.syncadapter;
-
-import android.app.Service;
-import android.content.Intent;
-import android.os.IBinder;
-
-public class ContactSyncService extends Service {
-    private static final Object syncAdapterLock = new Object();
-    private static AbstractOwnCloudSyncAdapter mSyncAdapter = null;
-
-    @Override
-    public void onCreate() {
-        synchronized (syncAdapterLock) {
-            if (mSyncAdapter == null) {
-                mSyncAdapter = new ContactSyncAdapter(getApplicationContext(),
-                        true);
-            }
-        }
-    }
-
-    @Override
-    public IBinder onBind(Intent arg0) {
-        return mSyncAdapter.getSyncAdapterBinder();
-    }
-
-}