Added MediaController user interface to handle audio files in the MediaService
authorDavid A. Velasco <dvelasco@solidgear.es>
Wed, 6 Feb 2013 13:14:55 +0000 (14:14 +0100)
committerDavid A. Velasco <dvelasco@solidgear.es>
Wed, 6 Feb 2013 13:14:55 +0000 (14:14 +0100)
res/layout/audio_player.xml [new file with mode: 0644]
res/layout/file_details_fragment.xml
src/com/owncloud/android/datamodel/OCFile.java
src/com/owncloud/android/media/MediaService.java
src/com/owncloud/android/media/MediaServiceBinder.java [new file with mode: 0644]
src/com/owncloud/android/ui/activity/FileDisplayActivity.java
src/com/owncloud/android/ui/fragment/FileDetailFragment.java

diff --git a/res/layout/audio_player.xml b/res/layout/audio_player.xml
new file mode 100644 (file)
index 0000000..bee0f87
--- /dev/null
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+              android:id="@+id/main_audio_view"
+              android:layout_width="fill_parent"
+              android:layout_height="wrap_content"
+              android:layout_gravity="center"
+              android:orientation="vertical">
+  <TextView
+    android:layout_width="wrap_content"
+    android:layout_height="wrap_content"
+    android:layout_gravity="center"
+    android:text="Now playing:"
+    android:textSize="25sp"
+    android:textStyle="bold"
+    />
+  <TextView
+    android:id="@+id/now_playing_text"
+    android:layout_width="wrap_content"
+    android:layout_height="wrap_content"
+    android:layout_marginTop="20dip"
+    android:layout_marginLeft="10dip"
+    android:layout_marginRight="10dip"
+    android:layout_gravity="center"
+    android:text="Now playing.."
+    android:textSize="16sp"
+    android:textStyle="italic"
+    />
+</LinearLayout>
\ No newline at end of file
index 3903677..cee45a9 100644 (file)
@@ -57,7 +57,7 @@
                 android:id="@+id/fdDetailsContainer"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
-                android:layout_below="@+id/fdFileHeaderContainer" >
+                android:layout_below="@id/fdFileHeaderContainer" >
 
                 <RelativeLayout
                     android:id="@+id/fdLabelContainer"
index b136129..0b581eb 100644 (file)
@@ -454,4 +454,8 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
         return 0;
     }
 
+    public boolean isAudio() {
+        return (mMimeType.startsWith("audio/"));
+    }
+
 }
index 8020f12..cccbaa7 100644 (file)
@@ -37,10 +37,8 @@ import android.net.wifi.WifiManager.WifiLock;
 import android.os.IBinder;
 import android.os.PowerManager;
 import android.util.Log;
-import android.widget.RemoteViews;
 import android.widget.Toast;
 
-import java.io.File;
 import java.io.IOException;
 
 import com.owncloud.android.AccountUtils;
@@ -156,6 +154,8 @@ public class MediaService extends Service implements OnCompletionListener, OnPre
     
     private OCFile mFile;
     private Account mAccount;
+
+    private IBinder mBinder;
     
     
 
@@ -173,7 +173,7 @@ public class MediaService extends Service implements OnCompletionListener, OnPre
 
         mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
         mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
-
+        mBinder = new MediaServiceBinder(this);
     }
 
     
@@ -206,6 +206,8 @@ public class MediaService extends Service implements OnCompletionListener, OnPre
     /**
      * Processes a request to play a media file received as a parameter
      * 
+     * TODO If a new request is received when a file is being prepared, it is ignored. Is this what we want? 
+     * 
      * @param intent    Intent received in the request with the data to identify the file to play. 
      */
     private void processPlayFileRequest(Intent intent) {
@@ -215,14 +217,13 @@ public class MediaService extends Service implements OnCompletionListener, OnPre
             tryToGetAudioFocus();
             playMedia();
         }
-        // TODO think what happens if mState == State.PREPARING
     }
 
     
     /**
      * Processes a request to play a media file.
      */
-    void processPlayRequest() {
+    protected void processPlayRequest() {
         // request audio focus
         tryToGetAudioFocus();
 
@@ -245,7 +246,7 @@ public class MediaService extends Service implements OnCompletionListener, OnPre
      * Makes sure the media player exists and has been reset. This will create the media player
      * if needed, or reset the existing media player if one already exists.
      */
-    void createMediaPlayerIfNeeded() {
+    protected void createMediaPlayerIfNeeded() {
         if (mPlayer == null) {
             mPlayer = new MediaPlayer();
 
@@ -277,7 +278,7 @@ public class MediaService extends Service implements OnCompletionListener, OnPre
     /**
      * Processes a request to pause the current playback 
      */
-    private void processPauseRequest() {
+    protected void processPauseRequest() {
         if (mState == State.PLAYING) {
             mState = State.PAUSED;
             mPlayer.pause();
@@ -310,9 +311,12 @@ public class MediaService extends Service implements OnCompletionListener, OnPre
      * @param   force       When 'true', the playback is stopped no matter the value of mState
      */
     void processStopRequest(boolean force) {
-        if (mState == State.PLAYING || mState == State.PAUSED || force) {
+        if (mState == State.PLAYING || mState == State.PAUSED || mState == State.STOPPED || force) {
             mState = State.STOPPED;
 
+            mFile = null;
+            mAccount = null;
+            
             releaseResources(true);
             giveUpAudioFocus();
             stopSelf();     // service is no longer necessary
@@ -645,10 +649,59 @@ public class MediaService extends Service implements OnCompletionListener, OnPre
     }
     
 
+    /**
+     * Provides a binder object that clients can use to perform operations on the MediaPlayer managed by the MediaService. 
+     */
     @Override
-    public IBinder onBind(Intent arg0) {
-        // TODO provide a binding API? may we use a service to play VIDEO?
-        return null;
+    public IBinder onBind(Intent arg) {
+        return mBinder;
+    }
+    
+    
+    /**
+     * Called when ALL the bound clients were onbound.
+     * 
+     * The service is destroyed if playback stopped or paused
+     */
+    @Override
+    public boolean onUnbind(Intent intent) {
+        if (mState == State.PAUSED || mState == State.STOPPED)  {
+            Log.d(TAG, "Stopping service due to unbind in pause");
+            processStopRequest(false);
+        }
+        return false;   // not accepting rebinding (default behaviour)
+    }
+
+
+    /**
+     * Accesses the current MediaPlayer instance in the service.
+     * 
+     * To be handled carefully. Visibility is protected to be accessed only 
+     * 
+     * @return Current MediaPlayer instance handled by MediaService.
+     */
+    protected MediaPlayer getPlayer() {
+        return mPlayer;
+    }
+
+
+    /**
+     * Accesses the current OCFile loaded in the service.
+     * 
+     * @return  The current OCFile loaded in the service.
+     */
+    protected OCFile getCurrentFile() {
+        return mFile;
+    }
+
+    
+    /**
+     * Accesses the current {@link State} of the MediaService.
+     * 
+     * @return  The current {@link State} of the MediaService.
+     */
+    public State getState() {
+        return mState;
     }
 
 }
diff --git a/src/com/owncloud/android/media/MediaServiceBinder.java b/src/com/owncloud/android/media/MediaServiceBinder.java
new file mode 100644 (file)
index 0000000..98b506c
--- /dev/null
@@ -0,0 +1,174 @@
+/* ownCloud Android client application
+ *   Copyright (C) 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 as published by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   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.media;
+
+
+import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.media.MediaService.State;
+
+import android.accounts.Account;
+import android.content.Intent;
+import android.media.MediaPlayer;
+import android.os.Binder;
+import android.util.Log;
+import android.widget.MediaController;
+
+
+/**
+ *  Binder allowing client components to perform operations on on the MediaPlayer managed by a MediaService instance.
+ * 
+ *  Provides the operations of {@link MediaController.MediaPlayerControl}, and an extra method to check if
+ *  an {@link OCFile} instance is handled by the MediaService.
+ *  
+ *  @author David A. Velasco
+ */
+public class MediaServiceBinder extends Binder implements MediaController.MediaPlayerControl {
+
+    private static final String TAG = MediaServiceBinder.class.getSimpleName();
+    /**
+     * {@link MediaService} instance to access with the binder
+     */
+    private MediaService mService = null;
+    
+    /**
+     * Public constructor
+     * 
+     * @param service       A {@link MediaService} instance to access with the binder 
+     */
+    public MediaServiceBinder(MediaService service) {
+        if (service == null) {
+            throw new IllegalArgumentException("Argument 'service' can not be null");
+        }
+        mService = service;
+    }
+    
+    
+    public boolean isPlaying(OCFile mFile) {
+        return (mFile != null && mFile.equals(mService.getCurrentFile())); 
+    }
+
+    
+    @Override
+    public boolean canPause() {
+        //Log.e(TAG, TAG + " - canPause -> true");
+        return true;
+    }
+
+    @Override
+    public boolean canSeekBackward() {
+        //Log.e(TAG, TAG + " - canSeekBackward -> true");
+        return true;
+    }
+
+    @Override
+    public boolean canSeekForward() {
+        //Log.e(TAG, TAG + " - canSeekForward -> true");
+        return true;
+    }
+
+    @Override
+    public int getBufferPercentage() {
+        MediaPlayer currentPlayer = mService.getPlayer();
+        if (currentPlayer != null) {
+            //Log.e(TAG, TAG + " - getBufferPercentage -> 100");
+            return 100;
+            // TODO update for streamed playback; add OnBufferUpdateListener in MediaService
+        } else {
+            //Log.e(TAG, TAG + " - getBufferPercentage -> 0");
+            return 0;
+        }
+    }
+
+    @Override
+    public int getCurrentPosition() {
+        MediaPlayer currentPlayer = mService.getPlayer();
+        if (currentPlayer != null) {
+            int pos = currentPlayer.getCurrentPosition();
+            //Log.e(TAG, TAG + " - getCurrentPosition -> " + pos);
+            return pos;
+        } else {
+            //Log.e(TAG, TAG + " - getCurrentPosition -> 0");
+            return 0;
+        }
+    }
+
+    @Override
+    public int getDuration() {
+        MediaPlayer currentPlayer = mService.getPlayer();
+        if (currentPlayer != null) {
+            int dur = currentPlayer.getDuration();
+            //Log.e(TAG, TAG + " - getDuration -> " + dur);
+            return dur;
+        } else {
+            //Log.e(TAG, TAG + " - getDuration -> 0");
+            return 0;
+        }
+    }
+
+    
+    /**
+     * Reports if the MediaService is playing a file or not.
+     * 
+     * Considers that the file is being played when it is in preparation because the expected
+     * client of this method is a {@link MediaController} , and we do not want that the 'play'
+     * button is shown when the file is being prepared by the MediaService.
+     */
+    @Override
+    public boolean isPlaying() {
+        MediaService.State currentState = mService.getState();
+        //Log.e(TAG, TAG + " - isPlaying -> " + (currentState == State.PLAYING || currentState == State.PREPARING));
+        return (currentState == State.PLAYING || currentState == State.PREPARING);
+    }
+
+    
+    @Override
+    public void pause() {
+        Log.d(TAG, "Pausing through binder...");
+        mService.processPauseRequest();
+    }
+
+    @Override
+    public void seekTo(int pos) {
+        Log.d(TAG, "Seeking " + pos + " through binder...");
+        MediaPlayer currentPlayer = mService.getPlayer();
+        MediaService.State currentState = mService.getState();
+        if (currentPlayer != null && currentState != State.PREPARING && currentState != State.STOPPED) {
+            currentPlayer.seekTo(pos);
+        }
+    }
+
+    @Override
+    public void start() {
+        Log.d(TAG, "Starting through binder...");
+        mService.processPlayRequest();  // this will finish the service if there is no file preloaded to play
+    }
+    
+    
+    public void start(Account account, OCFile file) {
+        Log.d(TAG, "Loading and starting through binder...");
+        Intent i = new Intent(mService, MediaService.class);
+        i.putExtra(MediaService.EXTRA_ACCOUNT, account);
+        i.putExtra(MediaService.EXTRA_FILE, file);
+        i.setAction(MediaService.ACTION_PLAY_FILE);
+        mService.startService(i);
+    }
+
+}
+
+
index cfd9ae5..8eafbf7 100644 (file)
@@ -1015,7 +1015,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
      */\r
     @Override\r
     public void onFileClick(OCFile file) {\r
-        \r
+\r
         // If we are on a large device -> update fragment\r
         if (mDualPane) {\r
             // buttons in the details view are problematic when trying to reuse an existing fragment; create always a new one solves some of them, BUT no all; downloads are 'dangerous'\r
index 4d6fb48..0d07c44 100644 (file)
@@ -28,6 +28,7 @@ import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
 import org.apache.http.HttpStatus;\r
 import org.apache.http.NameValuePair;\r
 import org.apache.http.client.utils.URLEncodedUtils;\r
+import org.apache.http.entity.FileEntity;\r
 import org.apache.http.message.BasicNameValuePair;\r
 import org.apache.http.protocol.HTTP;\r
 import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;\r
@@ -39,9 +40,11 @@ import android.annotation.SuppressLint;
 import android.app.Activity;\r
 import android.content.ActivityNotFoundException;\r
 import android.content.BroadcastReceiver;\r
+import android.content.ComponentName;\r
 import android.content.Context;\r
 import android.content.Intent;\r
 import android.content.IntentFilter;\r
+import android.content.ServiceConnection;\r
 import android.graphics.Bitmap;\r
 import android.graphics.BitmapFactory;\r
 import android.graphics.BitmapFactory.Options;\r
@@ -50,18 +53,22 @@ import android.net.Uri;
 import android.os.AsyncTask;\r
 import android.os.Bundle;\r
 import android.os.Handler;\r
+import android.os.IBinder;\r
 import android.support.v4.app.DialogFragment;\r
 import android.support.v4.app.FragmentTransaction;\r
 import android.util.Log;\r
 import android.view.Display;\r
 import android.view.LayoutInflater;\r
+import android.view.MotionEvent;\r
 import android.view.View;\r
 import android.view.View.OnClickListener;\r
+import android.view.View.OnTouchListener;\r
 import android.view.ViewGroup;\r
 import android.webkit.MimeTypeMap;\r
 import android.widget.Button;\r
 import android.widget.CheckBox;\r
 import android.widget.ImageView;\r
+import android.widget.MediaController;\r
 import android.widget.TextView;\r
 import android.widget.Toast;\r
 \r
@@ -77,6 +84,7 @@ 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.media.MediaService;\r
+import com.owncloud.android.media.MediaServiceBinder;\r
 import com.owncloud.android.network.OwnCloudClientUtils;\r
 import com.owncloud.android.operations.OnRemoteOperationListener;\r
 import com.owncloud.android.operations.RemoteOperation;\r
@@ -104,7 +112,8 @@ import eu.alefzero.webdav.WebdavUtils;
  * \r
  */\r
 public class FileDetailFragment extends SherlockFragment implements\r
-        OnClickListener, ConfirmationDialogFragment.ConfirmationDialogFragmentListener, OnRemoteOperationListener, EditNameDialogListener {\r
+        OnClickListener, OnTouchListener, \r
+        ConfirmationDialogFragment.ConfirmationDialogFragmentListener, OnRemoteOperationListener, EditNameDialogListener {\r
 \r
     public static final String EXTRA_FILE = "FILE";\r
     public static final String EXTRA_ACCOUNT = "ACCOUNT";\r
@@ -124,6 +133,9 @@ public class FileDetailFragment extends SherlockFragment implements
     private Handler mHandler;\r
     private RemoteOperation mLastRemoteOperation;\r
     private DialogFragment mCurrentDialog;\r
+    private MediaServiceBinder mMediaServiceBinder = null;\r
+    private MediaController mMediaController = null;\r
+    private MediaServiceConnection mMediaServiceConnection = null;\r
 \r
     private static final String TAG = FileDetailFragment.class.getSimpleName();\r
     public static final String FTAG = "FileDetails"; \r
@@ -192,6 +204,7 @@ public class FileDetailFragment extends SherlockFragment implements
             mView.findViewById(R.id.fdRemoveBtn).setOnClickListener(this);\r
             //mView.findViewById(R.id.fdShareBtn).setOnClickListener(this);\r
             mPreview = (ImageView)mView.findViewById(R.id.fdPreview);\r
+            mPreview.setOnTouchListener(this);\r
         }\r
         \r
         updateFileDetails(false);\r
@@ -234,6 +247,13 @@ public class FileDetailFragment extends SherlockFragment implements
         Log.i(getClass().toString(), "onSaveInstanceState() end");\r
     }\r
 \r
+    @Override\r
+    public void onStart() {\r
+        super.onStart();\r
+        if (mFile != null && mFile.isAudio()) {\r
+            bindMediaService();\r
+        }\r
+    }\r
     \r
     @Override\r
     public void onResume() {\r
@@ -247,10 +267,15 @@ public class FileDetailFragment extends SherlockFragment implements
         mUploadFinishReceiver = new UploadFinishReceiver();\r
         filter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);\r
         getActivity().registerReceiver(mUploadFinishReceiver, filter);\r
+\r
+        mPreview = (ImageView)mView.findViewById(R.id.fdPreview);   // this is here just because it is nullified in onPause()\r
         \r
-        mPreview = (ImageView)mView.findViewById(R.id.fdPreview);\r
+        if (mMediaController != null) {\r
+            mMediaController.show();\r
+        }\r
     }\r
 \r
+\r
     @Override\r
     public void onPause() {\r
         super.onPause();\r
@@ -261,18 +286,34 @@ public class FileDetailFragment extends SherlockFragment implements
         getActivity().unregisterReceiver(mUploadFinishReceiver);\r
         mUploadFinishReceiver = null;\r
         \r
-        if (mPreview != null) {\r
+        if (mPreview != null) { // why?\r
             mPreview = null;\r
         }\r
+        \r
+        if (mMediaController != null) {\r
+            mMediaController.hide();\r
+        }\r
     }\r
 \r
+\r
+    @Override\r
+    public void onStop() {\r
+        super.onStop();\r
+        if (mMediaServiceConnection != null) {\r
+            Log.d(TAG, "Unbinding from MediaService ...");\r
+            getActivity().unbindService(mMediaServiceConnection);\r
+            mMediaServiceBinder = null;\r
+            mMediaController = null;\r
+        }\r
+    }\r
+    \r
+    \r
     @Override\r
     public View getView() {\r
         return super.getView() == null ? mView : super.getView();\r
     }\r
 \r
     \r
-    \r
     @Override\r
     public void onClick(View v) {\r
         switch (v.getId()) {\r
@@ -374,18 +415,84 @@ public class FileDetailFragment extends SherlockFragment implements
         }*/\r
     }\r
     \r
+    \r
+    @Override\r
+    public boolean onTouch(View v, MotionEvent event) {\r
+        if (v == mPreview && event.getAction() == MotionEvent.ACTION_DOWN && mFile != null && mFile.isDown() && mFile.isAudio()) {\r
+            if (!mMediaServiceBinder.isPlaying(mFile)) {\r
+                Log.d(TAG, "starting playback of " + mFile.getStoragePath());\r
+                mMediaServiceBinder.start(mAccount, mFile);\r
+                // this is a patch; need to synchronize this with the onPrepared() coming from MediaPlayer in the MediaService\r
+                mMediaController.postDelayed(new Runnable() {\r
+                    @Override\r
+                    public void run() {\r
+                        mMediaController.show(0);\r
+                    }\r
+                } , 300);\r
+            } else {\r
+                mMediaController.show(0);\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+\r
+    \r
+    private void bindMediaService() {\r
+        Log.d(TAG, "Binding to MediaService...");\r
+        if (mMediaServiceConnection == null) {\r
+            mMediaServiceConnection = new MediaServiceConnection();\r
+        }\r
+        getActivity().bindService(  new Intent(getActivity(), \r
+                                    MediaService.class),\r
+                                    mMediaServiceConnection, \r
+                                    Context.BIND_AUTO_CREATE);\r
+    }\r
+    \r
+    /** Defines callbacks for service binding, passed to bindService() */\r
+    private class MediaServiceConnection implements ServiceConnection {\r
+\r
+        @Override\r
+        public void onServiceConnected(ComponentName component, IBinder service) {\r
+            if (component.equals(new ComponentName(getActivity(), MediaService.class))) {\r
+                Log.d(TAG, "Media service connected");\r
+                mMediaServiceBinder = (MediaServiceBinder) service;\r
+                if (mMediaServiceBinder != null) {\r
+                    if (mMediaController == null) {\r
+                        mMediaController = new MediaController(getSherlockActivity());\r
+                    }\r
+                    mMediaController.setMediaPlayer(mMediaServiceBinder);\r
+                    mMediaController.setAnchorView(mPreview);\r
+                    mMediaController.setEnabled(true);\r
+                    \r
+                    Log.d(TAG, "Successfully bound to MediaService, MediaController ready");\r
+                    \r
+                } else {\r
+                    Log.e(TAG, "Unexpected response from MediaService while binding");\r
+                }\r
+            }\r
+        }\r
+        \r
+        @Override\r
+        public void onServiceDisconnected(ComponentName component) {\r
+            if (component.equals(new ComponentName(getActivity(), MediaService.class))) {\r
+                Log.d(TAG, "Media service suddenly disconnected");\r
+                if (mMediaController != null) {\r
+                    mMediaController.hide();\r
+                    mMediaController.setMediaPlayer(null);  // TODO check this is not an error\r
+                    mMediaController = null;\r
+                }\r
+                mMediaServiceBinder = null;\r
+                mMediaServiceConnection = null;\r
+            }\r
+        }\r
+    }    \r
+\r
+\r
     /**\r
      * Opens mFile.\r
      */\r
     private void openFile() {\r
         \r
-        Intent i = new Intent(getActivity(), MediaService.class);\r
-        i.putExtra(MediaService.EXTRA_ACCOUNT, mAccount);\r
-        i.putExtra(MediaService.EXTRA_FILE, mFile);\r
-        i.setAction(MediaService.ACTION_PLAY_FILE);\r
-        getActivity().startService(i);\r
-\r
-        /*\r
         String storagePath = mFile.getStoragePath();\r
         String encodedStoragePath = WebdavUtils.encodePath(storagePath);\r
         try {\r
@@ -406,7 +513,7 @@ public class FileDetailFragment extends SherlockFragment implements
                         i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mimeType);\r
                     } else {\r
                         // desperate try\r
-                        i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), "*-/*");\r
+                        i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), "*/*");\r
                     }\r
                     i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);\r
                     startActivity(i);\r
@@ -428,7 +535,7 @@ public class FileDetailFragment extends SherlockFragment implements
                 }\r
             }\r
             \r
-        }*/\r
+        }\r
     }\r
 \r
 \r
@@ -512,6 +619,8 @@ public class FileDetailFragment extends SherlockFragment implements
      *\r
      * TODO Remove parameter when the transferring state of files is kept in database. \r
      * \r
+     * TODO REFACTORING! this method called 5 times before every time the fragment is shown! \r
+     * \r
      * @param transferring      Flag signaling if the file should be considered as downloading or uploading, \r
      *                          although {@link FileDownloaderBinder#isDownloading(Account, OCFile)}  and \r
      *                          {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.\r
@@ -519,7 +628,7 @@ public class FileDetailFragment extends SherlockFragment implements
      */\r
     public void updateFileDetails(boolean transferring) {\r
 \r
-        if (mFile != null && mAccount != null && mLayout == R.layout.file_details_fragment) {\r
+        if (readyToShow()) {\r
             \r
             // set file details\r
             setFilename(mFile.getFileName());\r
@@ -560,6 +669,17 @@ public class FileDetailFragment extends SherlockFragment implements
     \r
     \r
     /**\r
+     * Checks if the fragment is ready to show details of a OCFile\r
+     *  \r
+     * @return  'True' when the fragment is ready to show details of a file\r
+     */\r
+    private boolean readyToShow() {\r
+        return (mFile != null && mAccount != null && mLayout == R.layout.file_details_fragment);        \r
+    }\r
+\r
+\r
+\r
+    /**\r
      * Updates the filename in view\r
      * @param filename to set\r
      */\r
@@ -1074,4 +1194,5 @@ public class FileDetailFragment extends SherlockFragment implements
         }\r
     }\r
 \r
+\r
 }\r