Download service refactoring: multiple downloads and cancellation support
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / FileDisplayActivity.java
index 64098f7..50b8361 100644 (file)
@@ -26,12 +26,14 @@ import android.app.ProgressDialog;
 import android.app.AlertDialog.Builder;\r
 import android.app.Dialog;\r
 import android.content.BroadcastReceiver;\r
+import android.content.ComponentName;\r
 import android.content.ContentResolver;\r
 import android.content.Context;\r
 import android.content.DialogInterface;\r
 import android.content.DialogInterface.OnClickListener;\r
 import android.content.Intent;\r
 import android.content.IntentFilter;\r
+import android.content.ServiceConnection;\r
 import android.content.SharedPreferences;\r
 import android.content.pm.PackageInfo;\r
 import android.content.pm.PackageManager.NameNotFoundException;\r
@@ -40,6 +42,7 @@ import android.database.Cursor;
 import android.net.Uri;\r
 import android.os.Bundle;\r
 import android.os.Handler;\r
+import android.os.IBinder;\r
 import android.preference.PreferenceManager;\r
 import android.provider.MediaStore;\r
 import android.support.v4.app.FragmentTransaction;\r
@@ -64,6 +67,7 @@ import com.owncloud.android.datamodel.DataStorageManager;
 import com.owncloud.android.datamodel.FileDataStorageManager;\r
 import com.owncloud.android.datamodel.OCFile;\r
 import com.owncloud.android.files.services.FileDownloader;\r
+import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;\r
 import com.owncloud.android.files.services.FileUploader;\r
 import com.owncloud.android.network.OwnCloudClientUtils;\r
 import com.owncloud.android.syncadapter.FileSyncService;\r
@@ -90,6 +94,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     private SyncBroadcastReceiver mSyncBroadcastReceiver;\r
     private UploadFinishReceiver mUploadFinishReceiver;\r
     private DownloadFinishReceiver mDownloadFinishReceiver;\r
+    private FileDownloaderBinder mDownloaderBinder = null;\r
     \r
     private OCFileListFragment mFileList;\r
     \r
@@ -123,6 +128,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
             \r
         } else {    /// at least an account is available\r
             \r
+            bindService(new Intent(this, FileDownloader.class), mConnection, Context.BIND_AUTO_CREATE);\r
             initDataFromCurrentAccount();\r
             \r
         }\r
@@ -205,7 +211,14 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
             mCurrentDir = mStorageManager.getFileByPath("/");   // this will return NULL if the database has not ever synchronized\r
     }\r
         \r
+    \r
+    @Override\r
+    public void onDestroy() {\r
+        super.onDestroy();\r
+        unbindService(mConnection);\r
+    }\r
 \r
+    \r
     @Override\r
     public boolean onCreateOptionsMenu(Menu menu) {\r
         MenuInflater inflater = getSherlock().getMenuInflater();\r
@@ -421,7 +434,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
             registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);\r
         \r
             // List current directory\r
-            //mFileList.listDirectory(mCurrentDir);\r
+            mFileList.listDirectory(mCurrentDir);   // we should find the way to avoid the need of this\r
             \r
         } else {\r
             \r
@@ -872,6 +885,40 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
             fileListFragment.listDirectory();\r
         }\r
     }\r
+\r
+    \r
+    /**\r
+     * {@inheritDoc}\r
+     */\r
+    @Override\r
+    public FileDownloaderBinder getFileDownloaderBinder() {\r
+        return mDownloaderBinder;\r
+    }\r
+    \r
+    \r
+    /** Defines callbacks for service binding, passed to bindService() */\r
+    private ServiceConnection mConnection = new ServiceConnection() {\r
+\r
+        @Override\r
+        public void onServiceConnected(ComponentName className, IBinder service) {\r
+            mDownloaderBinder = (FileDownloaderBinder) service;\r
+            // a new chance to get the mDownloadBinder through getDownloadBinder() - THIS IS A MESS\r
+            mFileList.listDirectory();\r
+            if (mDualPane) {\r
+                FileDetailFragment fragment = (FileDetailFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);\r
+                if (fragment != null)\r
+                    fragment.updateFileDetails();\r
+            }\r
+            \r
+        }\r
+\r
+        @Override\r
+        public void onServiceDisconnected(ComponentName arg0) {\r
+            mDownloaderBinder = null;\r
+        }\r
+    };    \r
+\r
+    \r
     \r
     /**\r
      * Launch an intent to request the PIN code to the user before letting him use the app\r
@@ -887,5 +934,5 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         }\r
     }\r
 \r
-    \r
+\r
 }\r