Fix bug: when installing the app for the first time, app crashes
[pub/Android/ownCloud.git] / src / com / owncloud / android / providers / FileContentProvider.java
index 30783ba..63a6501 100644 (file)
@@ -23,6 +23,7 @@
 package com.owncloud.android.providers;
 
 import java.io.File;
+import java.security.Provider;
 import java.util.ArrayList;
 import java.util.HashMap;
 
@@ -843,32 +844,34 @@ public class FileContentProvider extends ContentProvider {
         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;
+            // get accounts from AccountManager ( we cann't know if they are updated or not because
+            // of synchronicity problems)
+                       Account[] accounts = AccountManager.get(getContext()).getAccountsByType(
+                    MainApp.getAccountType());
+            String serverUrl, username, oldAccountName, newAccountName;
                        for (Account account : accounts) {
                 // build old 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);
+                newAccountName = AccountUtils.buildAccountName(Uri.parse(serverUrl), username);
 
                 // update values in database
                 db.beginTransaction();
                 try{
                     ContentValues cv = new ContentValues();
-                    cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, account.name);
+                    cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, newAccountName);
                     int num = db.update(ProviderTableMeta.FILE_TABLE_NAME,
                             cv,
                             ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
                             new String[]{ oldAccountName });
                     upgraded = true;
-                    db.setTransactionSuccessful();
 
-                     Log_OC.d("SQL", "Updated account in database: old name == " + oldAccountName +
-                             ", new name == " + account.name + " (" + num + " rows updated )");
+                    Log_OC.d("SQL", "Updated account in database: old name == " + oldAccountName +
+                             ", new name == " + newAccountName + " (" + num + " rows updated )");
 
                     // update path for downloaded files
-                    upgraded = updateDownloadedFiles(db, account.name, oldAccountName);
+                    upgraded = updateDownloadedFiles(db, newAccountName, oldAccountName);
 
                 } catch (SQLException e){
                     upgraded = false;
@@ -890,12 +893,15 @@ public class FileContentProvider extends ContentProvider {
         boolean upgraded = false;
         boolean renamed = false;
 
-        String selectQuery = "SELECT " + ProviderTableMeta._ID + " FROM " +
-                ProviderTableMeta.FILE_TABLE_NAME +" WHERE " +
-                ProviderTableMeta.FILE_ACCOUNT_OWNER +"=? AND " +
-                ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL;";
+        String whereClause = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " +
+                ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL";
+
+        Cursor c = db.query(ProviderTableMeta.FILE_TABLE_NAME,
+                null,
+                whereClause,
+                new String[] { newAccountName },
+                null, null, null);
 
-        Cursor c = db.rawQuery(selectQuery, new String[]{newAccountName});
         if (c.moveToFirst()) {
             // create storage path
             String oldAccountPath = FileStorageUtils.getSavePath(oldAccountName);
@@ -925,7 +931,6 @@ public class FileContentProvider extends ContentProvider {
                                 ProviderTableMeta.FILE_STORAGE_PATH + "=?",
                                 new String[]{oldPath});
                         upgraded = true;
-                        db.setTransactionSuccessful();
 
                         Log_OC.d("SQL", "Updated downloaded files: old file name == " + oldPath +
                                 ", new file name == " + newPath);
@@ -936,26 +941,7 @@ public class FileContentProvider extends ContentProvider {
                     }
                     upgradedResult = upgraded && upgradedResult;
 
-//                    // move file
-//                    File oldFile = new File(oldPath);
-//                    renamed = false;
-//                    if (oldFile.exists()) {
-//                        File newFile = new File(newPath);
-//                        File newFolder = newFile.getParentFile();
-//                        if (!newFolder.exists()) {
-//                            newFolder.mkdirs();
-//                        }
-//                        renamed = oldFile.renameTo(newFile);
-//                    }
                 } while (c.moveToNext());
-
-//                // remove old folder
-//                if (renamed && upgradedResult) {
-//                    File oldAccountFolder = new File(oldAccountPath);
-//                    if (oldAccountFolder.exists()) {
-//                        oldAccountFolder.delete();
-//                    }
-//                }
             }
         }
         c.close();