Refresh authorization token on failed downloads / uploads when notification error...
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / FileDetailFragment.java
index 8740f83..3668ff3 100644 (file)
@@ -1,9 +1,10 @@
 /* ownCloud Android client application\r
  *   Copyright (C) 2011  Bartek Przybylski\r
+ *   Copyright (C) 2012-2013 ownCloud Inc.\r
  *\r
  *   This program is free software: you can redistribute it and/or modify\r
  *   it under the terms of the GNU General Public License as published by\r
- *   the Free Software Foundation, either version 3 of the License, or\r
+ *   the Free Software Foundation, either version 2 of the License, or\r
  *   (at your option) any later version.\r
  *\r
  *   This program is distributed in the hope that it will be useful,\r
@@ -20,7 +21,6 @@ package com.owncloud.android.ui.fragment;
 import java.io.File;\r
 \r
 import org.apache.commons.httpclient.Credentials;\r
-import org.apache.commons.httpclient.UsernamePasswordCredentials;\r
 \r
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
@@ -65,7 +65,6 @@ import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;\r
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;\r
 import com.owncloud.android.network.BearerCredentials;\r
-import com.owncloud.android.network.OwnCloudClientUtils;\r
 import com.owncloud.android.operations.OnRemoteOperationListener;\r
 import com.owncloud.android.operations.RemoteOperation;\r
 import com.owncloud.android.operations.RemoteOperationResult;\r
@@ -142,10 +141,6 @@ public class FileDetailFragment extends SherlockFragment implements
         mAccount = ocAccount;\r
         mStorageManager = null; // we need a context to init this; the container activity is not available yet at this moment \r
         mLayout = R.layout.file_details_empty;\r
-        \r
-        if(fileToDetail != null && ocAccount != null) {\r
-            mLayout = R.layout.file_details_fragment;\r
-        }\r
     }\r
     \r
     \r
@@ -166,6 +161,10 @@ public class FileDetailFragment extends SherlockFragment implements
             mAccount = savedInstanceState.getParcelable(FileDetailFragment.EXTRA_ACCOUNT);\r
         }\r
         \r
+        if(mFile != null && mAccount != null) {\r
+            mLayout = R.layout.file_details_fragment;\r
+        }\r
+        \r
         View view = null;\r
         view = inflater.inflate(mLayout, container, false);\r
         mView = view;\r
@@ -410,7 +409,6 @@ public class FileDetailFragment extends SherlockFragment implements
                                                                 true, \r
                                                                 mStorageManager);\r
                 mLastRemoteOperation.execute(mAccount, getSherlockActivity(), this, mHandler, getSherlockActivity());\r
-                \r
                 boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;\r
                 getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);\r
             }\r
@@ -446,7 +444,7 @@ public class FileDetailFragment extends SherlockFragment implements
      * @return  True when the fragment was created with the empty layout.\r
      */\r
     public boolean isEmpty() {\r
-        return mLayout == R.layout.file_details_empty;\r
+        return (mLayout == R.layout.file_details_empty || mFile == null || mAccount == null);\r
     }\r
 \r
     \r
@@ -492,8 +490,7 @@ public class FileDetailFragment extends SherlockFragment implements
             \r
             // set file details\r
             setFilename(mFile.getFileName());\r
-            setFiletype(DisplayUtils.convertMIMEtoPrettyPrint(mFile\r
-                    .getMimetype()));\r
+            setFiletype(mFile.getMimetype());\r
             setFilesize(mFile.getFileLength());\r
             if(ocVersionSupportsTimeCreated()){\r
                 setTimeCreated(mFile.getCreationTimestamp());\r
@@ -545,8 +542,14 @@ public class FileDetailFragment extends SherlockFragment implements
      */\r
     private void setFiletype(String mimetype) {\r
         TextView tv = (TextView) getView().findViewById(R.id.fdType);\r
-        if (tv != null)\r
-            tv.setText(mimetype);\r
+        if (tv != null) {\r
+            String printableMimetype = DisplayUtils.convertMIMEtoPrettyPrint(mimetype);;        \r
+            tv.setText(printableMimetype);\r
+        }\r
+        ImageView iv = (ImageView) getView().findViewById(R.id.fdIcon);\r
+        if (iv != null) {\r
+            iv.setImageResource(DisplayUtils.getResourceId(mimetype));\r
+        }\r
     }\r
 \r
     /**\r
@@ -721,14 +724,14 @@ public class FileDetailFragment extends SherlockFragment implements
                 if (mFile.getRemotePath().equals(uploadRemotePath) ||\r
                     renamedInUpload) {\r
                     if (uploadWasFine) {\r
-                        mFile = mStorageManager.getFileByPath(mFile.getRemotePath());\r
+                       mFile = mStorageManager.getFileByPath(uploadRemotePath);\r
                     }\r
                     if (renamedInUpload) {\r
                         String newName = (new File(uploadRemotePath)).getName();\r
                         Toast msg = Toast.makeText(getActivity().getApplicationContext(), String.format(getString(R.string.filedetails_renamed_in_upload_msg), newName), Toast.LENGTH_LONG);\r
                         msg.show();\r
-                        getSherlockActivity().removeStickyBroadcast(intent);    // not the best place to do this; a small refactorization of BroadcastReceivers should be done\r
                     }\r
+                    getSherlockActivity().removeStickyBroadcast(intent);    // not the best place to do this; a small refactorization of BroadcastReceivers should be done\r
                     updateFileDetails(false);    // it updates the buttons; must be called although !uploadWasFine; interrupted uploads still leave an incomplete file in the server\r
                 }\r
             }\r
@@ -936,28 +939,14 @@ public class FileDetailFragment extends SherlockFragment implements
      */\r
     @Override\r
     public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {\r
-        if (!result.isSuccess() && result.getCode() == ResultCode.UNAUTHORIZED) {\r
-            AccountManager am = AccountManager.get(getSherlockActivity());\r
-            //am.invalidateAuthToken(AccountAuthenticator.ACCOUNT_TYPE, OwnCloudClientUtils.getAuthorizationTokenType(operation.getClient().getCredentials()));\r
-            Credentials cred = operation.getClient().getCredentials();\r
-            if (cred instanceof BearerCredentials) {\r
-                am.invalidateAuthToken(AccountAuthenticator.ACCOUNT_TYPE, ((BearerCredentials)cred).getAccessToken());\r
-            } else {\r
-                am.clearPassword(mAccount);\r
-            }\r
-            operation.execute(mAccount, getSherlockActivity(), this, mHandler, getSherlockActivity());  // need a new client instance, so avoid retry()\r
-                // TODO si el usuario no se autoriza de nuevo, esto genera un bucle infinito; o un error en la creación del objecto cliente\r
-            \r
-        } else {\r
-            if (operation instanceof RemoveFileOperation) {\r
-                onRemoveFileOperationFinish((RemoveFileOperation)operation, result);\r
+        if (operation instanceof RemoveFileOperation) {\r
+            onRemoveFileOperationFinish((RemoveFileOperation)operation, result);\r
                 \r
-            } else if (operation instanceof RenameFileOperation) {\r
-                onRenameFileOperationFinish((RenameFileOperation)operation, result);\r
+        } else if (operation instanceof RenameFileOperation) {\r
+            onRenameFileOperationFinish((RenameFileOperation)operation, result);\r
                 \r
-            } else if (operation instanceof SynchronizeFileOperation) {\r
-                onSynchronizeFileOperationFinish((SynchronizeFileOperation)operation, result);\r
-            }\r
+        } else if (operation instanceof SynchronizeFileOperation) {\r
+            onSynchronizeFileOperationFinish((SynchronizeFileOperation)operation, result);\r
         }\r
     }\r
     \r