FileDetailActivity.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /* ownCloud Android client application
  2. * Copyright (C) 2011 Bartek Przybylski
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. package com.owncloud.android.ui.activity;
  19. import java.lang.ref.WeakReference;
  20. import android.accounts.Account;
  21. import android.app.Dialog;
  22. import android.app.ProgressDialog;
  23. import android.content.ComponentName;
  24. import android.content.Context;
  25. import android.content.Intent;
  26. import android.content.ServiceConnection;
  27. import android.content.res.Configuration;
  28. import android.os.Bundle;
  29. import android.os.IBinder;
  30. import android.support.v4.app.Fragment;
  31. import android.support.v4.app.FragmentTransaction;
  32. import android.util.Log;
  33. import android.widget.ProgressBar;
  34. import com.actionbarsherlock.app.ActionBar;
  35. import com.actionbarsherlock.app.SherlockFragmentActivity;
  36. import com.actionbarsherlock.view.MenuItem;
  37. import com.owncloud.android.datamodel.OCFile;
  38. import com.owncloud.android.files.services.FileDownloader;
  39. import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
  40. import com.owncloud.android.files.services.FileUploader;
  41. import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
  42. import com.owncloud.android.ui.fragment.FileDetailFragment;
  43. import com.owncloud.android.ui.fragment.FilePreviewFragment;
  44. import com.owncloud.android.R;
  45. import eu.alefzero.webdav.OnDatatransferProgressListener;
  46. /**
  47. * This activity displays the details of a file like its name, its size and so
  48. * on.
  49. *
  50. * @author Bartek Przybylski
  51. * @author David A. Velasco
  52. */
  53. public class FileDetailActivity extends SherlockFragmentActivity implements FileDetailFragment.ContainerActivity {
  54. public static final int DIALOG_SHORT_WAIT = 0;
  55. public static final String TAG = FileDetailActivity.class.getSimpleName();
  56. public static final String EXTRA_MODE = "MODE";
  57. public static final int MODE_DETAILS = 0;
  58. public static final int MODE_PREVIEW = 1;
  59. private static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";
  60. private boolean mConfigurationChangedToLandscape = false;
  61. private FileDownloaderBinder mDownloaderBinder = null;
  62. private ServiceConnection mDownloadConnection, mUploadConnection = null;
  63. private FileUploaderBinder mUploaderBinder = null;
  64. private boolean mWaitingToPreview;
  65. public ProgressListener mProgressListener;
  66. @Override
  67. protected void onCreate(Bundle savedInstanceState) {
  68. super.onCreate(savedInstanceState);
  69. // check if configuration changed to large-land ; for a tablet being changed from portrait to landscape when in FileDetailActivity
  70. Configuration conf = getResources().getConfiguration();
  71. mConfigurationChangedToLandscape = (conf.orientation == Configuration.ORIENTATION_LANDSCAPE &&
  72. (conf.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE
  73. );
  74. if (!mConfigurationChangedToLandscape) {
  75. mDownloadConnection = new DetailsServiceConnection();
  76. bindService(new Intent(this, FileDownloader.class), mDownloadConnection, Context.BIND_AUTO_CREATE);
  77. mUploadConnection = new DetailsServiceConnection();
  78. bindService(new Intent(this, FileUploader.class), mUploadConnection, Context.BIND_AUTO_CREATE);
  79. setContentView(R.layout.file_activity_details);
  80. ActionBar actionBar = getSupportActionBar();
  81. actionBar.setDisplayHomeAsUpEnabled(true);
  82. if (savedInstanceState == null) {
  83. mWaitingToPreview = false;
  84. createChildFragment();
  85. } else {
  86. mWaitingToPreview = savedInstanceState.getBoolean(KEY_WAITING_TO_PREVIEW);
  87. }
  88. } else {
  89. backToDisplayActivity(); // the 'back' won't be effective until this.onStart() and this.onResume() are completed;
  90. }
  91. }
  92. /**
  93. * Creates the proper fragment depending upon the state of the handled {@link OCFile} and
  94. * the requested {@link Intent}.
  95. */
  96. private void createChildFragment() {
  97. OCFile file = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);
  98. Account account = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT);
  99. int mode = getIntent().getIntExtra(EXTRA_MODE, MODE_PREVIEW);
  100. Fragment newFragment = null;
  101. if (FilePreviewFragment.canBePreviewed(file) && mode == MODE_PREVIEW) {
  102. if (file.isDown()) {
  103. newFragment = new FilePreviewFragment(file, account);
  104. } else {
  105. newFragment = new FileDetailFragment(file, account);
  106. mWaitingToPreview = true;
  107. }
  108. } else {
  109. newFragment = new FileDetailFragment(file, account);
  110. }
  111. FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
  112. ft.replace(R.id.fragment, newFragment, FileDetailFragment.FTAG);
  113. ft.commit();
  114. }
  115. @Override
  116. protected void onSaveInstanceState(Bundle outState) {
  117. outState.putBoolean(KEY_WAITING_TO_PREVIEW, mWaitingToPreview);
  118. }
  119. /** Defines callbacks for service binding, passed to bindService() */
  120. private class DetailsServiceConnection implements ServiceConnection {
  121. @Override
  122. public void onServiceConnected(ComponentName component, IBinder service) {
  123. Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
  124. FileDetailFragment detailsFragment = (fragment instanceof FileDetailFragment) ? (FileDetailFragment) fragment : null;
  125. if (component.equals(new ComponentName(FileDetailActivity.this, FileDownloader.class))) {
  126. Log.d(TAG, "Download service connected");
  127. mDownloaderBinder = (FileDownloaderBinder) service;
  128. if (detailsFragment != null) {
  129. mProgressListener = new ProgressListener(detailsFragment.getProgressBar());
  130. mDownloaderBinder.addDatatransferProgressListener(
  131. mProgressListener,
  132. (Account) getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT),
  133. (OCFile) getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE)
  134. );
  135. }
  136. } else if (component.equals(new ComponentName(FileDetailActivity.this, FileUploader.class))) {
  137. Log.d(TAG, "Upload service connected");
  138. mUploaderBinder = (FileUploaderBinder) service;
  139. if (detailsFragment != null) {
  140. mProgressListener = new ProgressListener(detailsFragment.getProgressBar());
  141. mUploaderBinder.addDatatransferProgressListener(
  142. mProgressListener,
  143. (Account) getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT),
  144. (OCFile) getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE)
  145. );
  146. }
  147. } else {
  148. return;
  149. }
  150. if (detailsFragment != null) {
  151. detailsFragment.updateFileDetails(false); // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())
  152. }
  153. }
  154. @Override
  155. public void onServiceDisconnected(ComponentName component) {
  156. if (component.equals(new ComponentName(FileDetailActivity.this, FileDownloader.class))) {
  157. Log.d(TAG, "Download service disconnected");
  158. mDownloaderBinder = null;
  159. } else if (component.equals(new ComponentName(FileDetailActivity.this, FileUploader.class))) {
  160. Log.d(TAG, "Upload service disconnected");
  161. mUploaderBinder = null;
  162. }
  163. }
  164. };
  165. /**
  166. * Helper class responsible for updating the progress bar shown for file uploading or downloading
  167. *
  168. * @author David A. Velasco
  169. */
  170. private class ProgressListener implements OnDatatransferProgressListener {
  171. int mLastPercent = 0;
  172. WeakReference<ProgressBar> mProgressBar = null;
  173. ProgressListener(ProgressBar progressBar) {
  174. mProgressBar = new WeakReference<ProgressBar>(progressBar);
  175. }
  176. @Override
  177. public void onTransferProgress(long progressRate) {
  178. // old method, nothing here
  179. };
  180. @Override
  181. public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String filename) {
  182. int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
  183. if (percent != mLastPercent) {
  184. ProgressBar pb = mProgressBar.get();
  185. if (pb != null) {
  186. pb.setProgress(percent);
  187. }
  188. }
  189. mLastPercent = percent;
  190. }
  191. };
  192. @Override
  193. public void onDestroy() {
  194. super.onDestroy();
  195. if (mDownloadConnection != null) {
  196. unbindService(mDownloadConnection);
  197. mDownloadConnection = null;
  198. }
  199. if (mUploadConnection != null) {
  200. unbindService(mUploadConnection);
  201. mUploadConnection = null;
  202. }
  203. }
  204. @Override
  205. public boolean onOptionsItemSelected(MenuItem item) {
  206. boolean returnValue = false;
  207. switch(item.getItemId()){
  208. case android.R.id.home:
  209. backToDisplayActivity();
  210. returnValue = true;
  211. break;
  212. default:
  213. returnValue = super.onOptionsItemSelected(item);
  214. }
  215. return returnValue;
  216. }
  217. @Override
  218. protected void onResume() {
  219. super.onResume();
  220. if (!mConfigurationChangedToLandscape) {
  221. Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
  222. if (fragment != null && fragment instanceof FileDetailFragment) {
  223. ((FileDetailFragment) fragment).updateFileDetails(false);
  224. }
  225. }
  226. }
  227. private void backToDisplayActivity() {
  228. Intent intent = new Intent(this, FileDisplayActivity.class);
  229. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  230. intent.putExtra(FileDetailFragment.EXTRA_FILE, getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE));
  231. intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT));
  232. startActivity(intent);
  233. finish();
  234. }
  235. @Override
  236. protected Dialog onCreateDialog(int id) {
  237. Dialog dialog = null;
  238. switch (id) {
  239. case DIALOG_SHORT_WAIT: {
  240. ProgressDialog working_dialog = new ProgressDialog(this);
  241. working_dialog.setMessage(getResources().getString(
  242. R.string.wait_a_moment));
  243. working_dialog.setIndeterminate(true);
  244. working_dialog.setCancelable(false);
  245. dialog = working_dialog;
  246. break;
  247. }
  248. default:
  249. dialog = null;
  250. }
  251. return dialog;
  252. }
  253. /**
  254. * {@inheritDoc}
  255. */
  256. @Override
  257. public void onFileStateChanged() {
  258. // nothing to do here!
  259. }
  260. /**
  261. * {@inheritDoc}
  262. */
  263. @Override
  264. public FileDownloaderBinder getFileDownloaderBinder() {
  265. return mDownloaderBinder;
  266. }
  267. @Override
  268. public FileUploaderBinder getFileUploaderBinder() {
  269. return mUploaderBinder;
  270. }
  271. @Override
  272. public void showFragmentWithDetails(OCFile file) {
  273. FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  274. transaction.replace(R.id.fragment, new FileDetailFragment(file, (Account) getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT)), FileDetailFragment.FTAG);
  275. transaction.commit();
  276. }
  277. }