Fixed crash, error notified instead
authorDavid A. Velasco <dvelasco@solidgear.es>
Tue, 18 Jun 2013 12:04:51 +0000 (14:04 +0200)
committerDavid A. Velasco <dvelasco@solidgear.es>
Tue, 18 Jun 2013 12:04:51 +0000 (14:04 +0200)
src/com/owncloud/android/authentication/AccountUtils.java
src/com/owncloud/android/network/OwnCloudClientUtils.java
src/com/owncloud/android/operations/RemoteOperationResult.java
src/com/owncloud/android/syncadapter/AbstractOwnCloudSyncAdapter.java
src/com/owncloud/android/ui/adapter/FileListActionListAdapter.java [deleted file]
src/com/owncloud/android/ui/preview/PreviewVideoActivity.java

index 10f7771..cfa00e3 100644 (file)
@@ -22,6 +22,7 @@ import com.owncloud.android.utils.OwnCloudVersion;
 \r
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
+import android.accounts.AccountsException;\r
 import android.content.Context;\r
 import android.content.SharedPreferences;\r
 import android.preference.PreferenceManager;\r
@@ -135,21 +136,36 @@ public class AccountUtils {
      * @param context\r
      * @param account\r
      * @return url or null on failure\r
+     * @throws AccountNotFoundException     When 'account' is unknown for the AccountManager\r
      */\r
-    public static String constructFullURLForAccount(Context context, Account account) {\r
-        try {\r
-            AccountManager ama = AccountManager.get(context);\r
-            String baseurl = ama.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL);\r
-            String strver  = ama.getUserData(account, AccountAuthenticator.KEY_OC_VERSION);\r
-            boolean supportsOAuth = (ama.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2) != null);\r
-            OwnCloudVersion ver = new OwnCloudVersion(strver);\r
-            String webdavpath = getWebdavPath(ver, supportsOAuth);\r
+    public static String constructFullURLForAccount(Context context, Account account) throws AccountNotFoundException {\r
+        AccountManager ama = AccountManager.get(context);\r
+        String baseurl = ama.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL);\r
+        String strver  = ama.getUserData(account, AccountAuthenticator.KEY_OC_VERSION);\r
+        boolean supportsOAuth = (ama.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2) != null);\r
+        OwnCloudVersion ver = new OwnCloudVersion(strver);\r
+        String webdavpath = getWebdavPath(ver, supportsOAuth);\r
 \r
-            if (webdavpath == null) return null;\r
-            return baseurl + webdavpath;\r
-        } catch (Exception e) {\r
-            e.printStackTrace();\r
-            return null;\r
+        if (baseurl == null || webdavpath == null) \r
+            throw new AccountNotFoundException(account, "Account not found", null);\r
+        \r
+        return baseurl + webdavpath;\r
+    }\r
+    \r
+    \r
+    public static class AccountNotFoundException extends AccountsException {\r
+        \r
+        private static final long serialVersionUID = 4276870654168776992L;\r
+        \r
+        private Account mFailedAccount; \r
+                \r
+        public AccountNotFoundException(Account failedAccount, String message, Throwable cause) {\r
+            super(message, cause);\r
+            mFailedAccount = failedAccount;\r
+        }\r
+        \r
+        public Account getFailedAccount() {\r
+            return mFailedAccount;\r
         }\r
     }\r
 \r
index 80be2e1..04fb7ac 100644 (file)
@@ -38,6 +38,7 @@ import org.apache.http.conn.ssl.X509HostnameVerifier;
 
 import com.owncloud.android.authentication.AccountAuthenticator;
 import com.owncloud.android.authentication.AccountUtils;
+import com.owncloud.android.authentication.AccountUtils.AccountNotFoundException;
 import com.owncloud.android.Log_OC;
 
 import eu.alefzero.webdav.WebdavClient;
@@ -83,8 +84,9 @@ public class OwnCloudClientUtils {
      * @throws AuthenticatorException       If the authenticator failed to get the authorization token for the account.
      * @throws OperationCanceledException   If the authenticator operation was cancelled while getting the authorization token for the account. 
      * @throws IOException                  If there was some I/O error while getting the authorization token for the account.
+     * @throws AccountNotFoundException     If 'account' is unknown for the AccountManager
      */
-    public static WebdavClient createOwnCloudClient (Account account, Context appContext) throws OperationCanceledException, AuthenticatorException, IOException {
+    public static WebdavClient createOwnCloudClient (Account account, Context appContext) throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException {
         //Log_OC.d(TAG, "Creating WebdavClient associated to " + account.name);
        
         Uri uri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account));
@@ -105,7 +107,7 @@ public class OwnCloudClientUtils {
     }
     
     
-    public static WebdavClient createOwnCloudClient (Account account, Context appContext, Activity currentActivity) throws OperationCanceledException, AuthenticatorException, IOException {
+    public static WebdavClient createOwnCloudClient (Account account, Context appContext, Activity currentActivity) throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException {
         Uri uri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account));
         WebdavClient client = createOwnCloudClient(uri, appContext);
         AccountManager am = AccountManager.get(appContext);
index 16b4779..6f4f291 100644 (file)
@@ -32,7 +32,11 @@ import org.apache.commons.httpclient.HttpException;
 import org.apache.commons.httpclient.HttpStatus;
 import org.apache.jackrabbit.webdav.DavException;
 
+import android.accounts.Account;
+import android.accounts.AccountsException;
+
 import com.owncloud.android.Log_OC;
+import com.owncloud.android.authentication.AccountUtils.AccountNotFoundException;
 import com.owncloud.android.network.CertificateCombinedException;
 
 /**
@@ -47,6 +51,7 @@ public class RemoteOperationResult implements Serializable {
 
     /** Generated - should be refreshed every time the class changes!! */
     private static final long serialVersionUID = -7805531062432602444L;
+
     
     private static final String TAG = "RemoteOperationResult";
     
@@ -77,7 +82,9 @@ public class RemoteOperationResult implements Serializable {
         LOCAL_STORAGE_NOT_MOVED, 
         LOCAL_STORAGE_NOT_COPIED, 
         OAUTH2_ERROR_ACCESS_DENIED,
-        QUOTA_EXCEEDED
+        QUOTA_EXCEEDED, 
+        ACCOUNT_NOT_FOUND, 
+        ACCOUNT_EXCEPTION
     }
 
     private boolean mSuccess = false;
@@ -142,6 +149,12 @@ public class RemoteOperationResult implements Serializable {
         } else if (e instanceof UnknownHostException) {
             mCode = ResultCode.HOST_NOT_AVAILABLE;
 
+        } else if (e instanceof AccountNotFoundException) {
+            mCode = ResultCode.ACCOUNT_NOT_FOUND;
+            
+        } else if (e instanceof AccountsException) {
+            mCode = ResultCode.ACCOUNT_EXCEPTION;
+            
         } else if (e instanceof SSLException || e instanceof RuntimeException) {
             CertificateCombinedException se = getCertificateCombinedException(e);
             if (se != null) {
@@ -242,6 +255,13 @@ public class RemoteOperationResult implements Serializable {
             } else if (mException instanceof IOException) {
                 return "Unrecovered transport exception";
 
+            } else if (mException instanceof AccountNotFoundException) {
+                Account failedAccount = ((AccountNotFoundException)mException).getFailedAccount();
+                return mException.getMessage() + " (" + (failedAccount != null ? failedAccount.name : "NULL") + ")";
+                
+            } else if (mException instanceof AccountsException) {
+                return "Exception while using account";
+                
             } else {
                 return "Unexpected exception";
             }
index 267eaba..e9f7a9b 100644 (file)
@@ -29,6 +29,7 @@ import org.apache.http.conn.ConnectionKeepAliveStrategy;
 import org.apache.http.protocol.HttpContext;\r
 \r
 import com.owncloud.android.authentication.AccountUtils;\r
+import com.owncloud.android.authentication.AccountUtils.AccountNotFoundException;\r
 import com.owncloud.android.datamodel.DataStorageManager;\r
 import com.owncloud.android.network.OwnCloudClientUtils;\r
 \r
@@ -142,10 +143,8 @@ public abstract class AbstractOwnCloudSyncAdapter extends
         return null;\r
     }\r
 \r
-    protected void initClientForCurrentAccount() throws OperationCanceledException, AuthenticatorException, IOException {\r
-        if (AccountUtils.constructFullURLForAccount(getContext(), account) == null) {\r
-            throw new UnknownHostException();\r
-        }\r
+    protected void initClientForCurrentAccount() throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException {\r
+        AccountUtils.constructFullURLForAccount(getContext(), account);\r
         mClient = OwnCloudClientUtils.createOwnCloudClient(account, getContext());\r
     }\r
     \r
diff --git a/src/com/owncloud/android/ui/adapter/FileListActionListAdapter.java b/src/com/owncloud/android/ui/adapter/FileListActionListAdapter.java
deleted file mode 100644 (file)
index 54202e4..0000000
+++ /dev/null
@@ -1,159 +0,0 @@
-/* ownCloud Android client application
- *   Copyright (C) 2011  Bartek Przybylski
- *   Copyright (C) 2012-2013 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.ui.adapter;
-
-import java.io.File;
-
-import com.owncloud.android.authentication.AccountUtils;
-import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
-
-import com.owncloud.android.R;
-import eu.alefzero.webdav.WebdavUtils;
-import android.accounts.Account;
-import android.accounts.AccountManager;
-import android.content.Context;
-import android.content.Intent;
-import android.database.Cursor;
-import android.database.DataSetObserver;
-import android.net.Uri;
-import android.text.TextUtils;
-import android.view.LayoutInflater;
-import android.view.View;
-import android.view.ViewGroup;
-import android.widget.ImageView;
-import android.widget.ListAdapter;
-import android.widget.TextView;
-
-public class FileListActionListAdapter implements ListAdapter {
-
-    private Context mContext;
-    private Account mAccount;
-    private String mFilename, mFileType, mFilePath, mFileStoragePath;
-
-    private final int ITEM_DOWNLOAD = 0;
-
-    // private final int ITEM_SHARE = 1;
-
-    public FileListActionListAdapter(Cursor c, Context co, Account account) {
-        mContext = co;
-        mFilename = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_NAME));
-        mFileType = c.getString(c
-                .getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE));
-        mFilePath = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH));
-        mFileStoragePath = c.getString(c
-                .getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH));
-        // mItemId = c.getString(c.getColumnIndex(ProviderTableMeta._ID));
-        mAccount = account;
-    }
-
-    public boolean areAllItemsEnabled() {
-        return true;
-    }
-
-    public boolean isEnabled(int position) {
-        return true;
-    }
-
-    public int getCount() {
-        return 1;
-    }
-
-    public Object getItem(int position) {
-        if (position == 0) {
-            Intent intent = new Intent(Intent.ACTION_VIEW);
-            if (TextUtils.isEmpty(mFileStoragePath)) {
-                intent.putExtra("toDownload", true);
-                AccountManager accm = (AccountManager) mContext
-                        .getSystemService(Context.ACCOUNT_SERVICE);
-                String ocurl = accm.getUserData(mAccount,
-                        AccountUtils.constructFullURLForAccount(mContext, mAccount));
-                ocurl += WebdavUtils.encodePath(mFilePath + mFilename);
-                intent.setData(Uri.parse(ocurl));
-            } else {
-                intent.putExtra("toDownload", false);
-                intent.setDataAndType(Uri.fromFile(new File(mFileStoragePath)),
-                        mFileType);
-            }
-            return intent;
-        }
-        return null;
-    }
-
-    public long getItemId(int position) {
-        return 0;
-    }
-
-    public int getItemViewType(int position) {
-        return 0;
-    }
-
-    public View getView(int position, View convertView, ViewGroup parent) {
-        View v = convertView;
-        if (v == null) {
-            LayoutInflater vi = (LayoutInflater) mContext
-                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
-            v = vi.inflate(R.layout.file_display_action_list_element, null);
-        }
-
-        TextView tv;
-        ImageView iv;
-        switch (position) {
-        case ITEM_DOWNLOAD:
-            tv = (TextView) v.findViewById(R.id.textView1);
-            if (mFileStoragePath == null) {
-                tv.setText("Download");
-            } else {
-                setActionName(tv);
-            }
-            iv = (ImageView) v.findViewById(R.id.imageView1);
-            iv.setImageResource(R.drawable.download);
-            break;
-        }
-
-        return v;
-    }
-
-    public int getViewTypeCount() {
-        return 2;
-    }
-
-    public boolean hasStableIds() {
-        return false;
-    }
-
-    public boolean isEmpty() {
-        return false;
-    }
-
-    public void registerDataSetObserver(DataSetObserver observer) { }
-
-    public void unregisterDataSetObserver(DataSetObserver observer) { }
-
-    private void setActionName(TextView tv) {
-        if (mFileType.matches("image/.*")) {
-            tv.setText("View");
-        } else if (mFileType.matches("audio/.*")
-                || mFileType.matches("video/.*")) {
-            tv.setText("Play");
-        } else {
-            tv.setText("Open");
-        }
-    }
-
-}
index 0d25bf3..c4b6dce 100644 (file)
@@ -35,6 +35,7 @@ import android.widget.VideoView;
 import com.owncloud.android.Log_OC;
 import com.owncloud.android.R;
 import com.owncloud.android.authentication.AccountUtils;
+import com.owncloud.android.authentication.AccountUtils.AccountNotFoundException;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.media.MediaService;
 
@@ -117,9 +118,14 @@ public class PreviewVideoActivity extends Activity implements OnCompletionListen
                 mVideoPlayer.setVideoPath(mFile.getStoragePath());
                 
             } else if (mAccount != null) {
-                // not working now
-                String url = AccountUtils.constructFullURLForAccount(this, mAccount) + mFile.getRemotePath();
-                mVideoPlayer.setVideoURI(Uri.parse(url));
+                // not working yet
+                String url;
+                try {
+                    url = AccountUtils.constructFullURLForAccount(this, mAccount) + mFile.getRemotePath();
+                    mVideoPlayer.setVideoURI(Uri.parse(url));
+                } catch (AccountNotFoundException e) {
+                    onError(null, MediaService.OC_MEDIA_ERROR, R.string.media_err_no_account);
+                }
                 
             } else {
                 onError(null, MediaService.OC_MEDIA_ERROR, R.string.media_err_no_account);