Download service refactoring: multiple downloads and cancellation support
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / FileDetailActivity.java
index 3c80a48..539cf26 100644 (file)
@@ -20,9 +20,13 @@ package com.owncloud.android.ui.activity;
 import android.accounts.Account;\r
 import android.app.Dialog;\r
 import android.app.ProgressDialog;\r
+import android.content.ComponentName;\r
+import android.content.Context;\r
 import android.content.Intent;\r
+import android.content.ServiceConnection;\r
 import android.content.res.Configuration;\r
 import android.os.Bundle;\r
+import android.os.IBinder;\r
 import android.support.v4.app.FragmentTransaction;\r
 \r
 import com.actionbarsherlock.app.ActionBar;\r
@@ -30,6 +34,7 @@ import com.actionbarsherlock.app.SherlockFragmentActivity;
 import com.actionbarsherlock.view.MenuItem;\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.ui.fragment.FileDetailFragment;\r
 \r
 import com.owncloud.android.R;\r
@@ -46,6 +51,7 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
     public static final int DIALOG_SHORT_WAIT = 0;\r
     \r
     private boolean mConfigurationChangedToLandscape = false;\r
+    private FileDownloaderBinder mDownloaderBinder = null;\r
 \r
     @Override\r
     protected void onCreate(Bundle savedInstanceState) {\r
@@ -58,11 +64,13 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
                                            );\r
 \r
         if (!mConfigurationChangedToLandscape) {\r
+            bindService(new Intent(this, FileDownloader.class), mConnection, Context.BIND_AUTO_CREATE);\r
+            \r
             setContentView(R.layout.file_activity_details);\r
         \r
             ActionBar actionBar = getSupportActionBar();\r
             actionBar.setDisplayHomeAsUpEnabled(true);\r
-        \r
+\r
             OCFile file = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);\r
             Account account = getIntent().getParcelableExtra(FileDownloader.EXTRA_ACCOUNT);\r
             FileDetailFragment mFileDetail = new FileDetailFragment(file, account);\r
@@ -77,8 +85,34 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
         \r
         \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
+            FileDetailFragment fragment = (FileDetailFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);\r
+            if (fragment != null)\r
+                fragment.updateFileDetails();   // a new chance to get the mDownloadBinder through getDownloadBinder()\r
+        }\r
+\r
+        @Override\r
+        public void onServiceDisconnected(ComponentName arg0) {\r
+            mDownloaderBinder = null;\r
+        }\r
+    };    \r
+    \r
 \r
     @Override\r
+    public void onDestroy() {\r
+        super.onDestroy();\r
+        unbindService(mConnection);\r
+    }\r
+    \r
+    \r
+    @Override\r
     public boolean onOptionsItemSelected(MenuItem item) {\r
         boolean returnValue = false;\r
         \r
@@ -141,4 +175,13 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
         // nothing to do here!\r
     }\r
 \r
+    \r
+    /**\r
+     * {@inheritDoc}\r
+     */\r
+    @Override\r
+    public FileDownloaderBinder getFileDownloaderBinder() {\r
+        return mDownloaderBinder;\r
+    }\r
+    \r
 }\r