PreviewImageActivity.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012-2013 ownCloud Inc.
  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 version 2,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. package com.owncloud.android.ui.preview;
  18. import android.content.BroadcastReceiver;
  19. import android.content.ComponentName;
  20. import android.content.Context;
  21. import android.content.Intent;
  22. import android.content.IntentFilter;
  23. import android.content.ServiceConnection;
  24. import android.os.Bundle;
  25. import android.os.IBinder;
  26. import android.support.v4.app.Fragment;
  27. import android.support.v4.app.FragmentManager;
  28. import android.support.v4.app.FragmentTransaction;
  29. import android.support.v4.view.ViewPager;
  30. import android.view.MotionEvent;
  31. import android.view.View;
  32. import android.view.View.OnTouchListener;
  33. import com.actionbarsherlock.app.ActionBar;
  34. import com.actionbarsherlock.view.MenuItem;
  35. import com.actionbarsherlock.view.Window;
  36. import com.owncloud.android.R;
  37. import com.owncloud.android.authentication.AccountUtils;
  38. import com.owncloud.android.datamodel.FileDataStorageManager;
  39. import com.owncloud.android.datamodel.OCFile;
  40. import com.owncloud.android.files.services.FileDownloader;
  41. import com.owncloud.android.files.services.FileUploader;
  42. import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
  43. import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
  44. import com.owncloud.android.ui.activity.FileActivity;
  45. import com.owncloud.android.ui.activity.FileDisplayActivity;
  46. import com.owncloud.android.ui.dialog.LoadingDialog;
  47. import com.owncloud.android.ui.fragment.FileFragment;
  48. import com.owncloud.android.utils.DisplayUtils;
  49. import com.owncloud.android.utils.Log_OC;
  50. /**
  51. * Holds a swiping galley where image files contained in an ownCloud directory are shown
  52. *
  53. * @author David A. Velasco
  54. */
  55. public class PreviewImageActivity extends FileActivity implements FileFragment.ContainerActivity, ViewPager.OnPageChangeListener, OnTouchListener {
  56. public static final int DIALOG_SHORT_WAIT = 0;
  57. public static final String TAG = PreviewImageActivity.class.getSimpleName();
  58. public static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";
  59. private static final String KEY_WAITING_FOR_BINDER = "WAITING_FOR_BINDER";
  60. private static final String DIALOG_WAIT_TAG = "DIALOG_WAIT";
  61. private FileDataStorageManager mStorageManager;
  62. private ViewPager mViewPager;
  63. private PreviewImagePagerAdapter mPreviewImagePagerAdapter;
  64. private FileDownloaderBinder mDownloaderBinder = null;
  65. private ServiceConnection mDownloadConnection, mUploadConnection = null;
  66. private FileUploaderBinder mUploaderBinder = null;
  67. private boolean mRequestWaitingForBinder;
  68. private DownloadFinishReceiver mDownloadFinishReceiver;
  69. private boolean mFullScreen;
  70. @Override
  71. protected void onCreate(Bundle savedInstanceState) {
  72. super.onCreate(savedInstanceState);
  73. requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
  74. setContentView(R.layout.preview_image_activity);
  75. ActionBar actionBar = getSupportActionBar();
  76. actionBar.setIcon(DisplayUtils.getSeasonalIconId());
  77. actionBar.setDisplayHomeAsUpEnabled(true);
  78. actionBar.hide();
  79. mFullScreen = true;
  80. if (savedInstanceState != null) {
  81. mRequestWaitingForBinder = savedInstanceState.getBoolean(KEY_WAITING_FOR_BINDER);
  82. } else {
  83. mRequestWaitingForBinder = false;
  84. }
  85. }
  86. private void initViewPager() {
  87. // get parent from path
  88. String parentPath = getFile().getRemotePath().substring(0, getFile().getRemotePath().lastIndexOf(getFile().getFileName()));
  89. OCFile parentFolder = mStorageManager.getFileByPath(parentPath);
  90. //OCFile parentFolder = mStorageManager.getFileById(getFile().getParentId());
  91. if (parentFolder == null) {
  92. // should not be necessary
  93. parentFolder = mStorageManager.getFileByPath(OCFile.ROOT_PATH);
  94. }
  95. mPreviewImagePagerAdapter = new PreviewImagePagerAdapter(getSupportFragmentManager(), parentFolder, getAccount(), mStorageManager);
  96. mViewPager = (ViewPager) findViewById(R.id.fragmentPager);
  97. int position = mPreviewImagePagerAdapter.getFilePosition(getFile());
  98. position = (position >= 0) ? position : 0;
  99. mViewPager.setAdapter(mPreviewImagePagerAdapter);
  100. mViewPager.setOnPageChangeListener(this);
  101. mViewPager.setCurrentItem(position);
  102. if (position == 0 && !getFile().isDown()) {
  103. // this is necessary because mViewPager.setCurrentItem(0) just after setting the adapter does not result in a call to #onPageSelected(0)
  104. mRequestWaitingForBinder = true;
  105. }
  106. }
  107. @Override
  108. public void onStart() {
  109. super.onStart();
  110. mDownloadConnection = new PreviewImageServiceConnection();
  111. bindService(new Intent(this, FileDownloader.class), mDownloadConnection, Context.BIND_AUTO_CREATE);
  112. mUploadConnection = new PreviewImageServiceConnection();
  113. bindService(new Intent(this, FileUploader.class), mUploadConnection, Context.BIND_AUTO_CREATE);
  114. }
  115. @Override
  116. protected void onSaveInstanceState(Bundle outState) {
  117. super.onSaveInstanceState(outState);
  118. outState.putBoolean(KEY_WAITING_FOR_BINDER, mRequestWaitingForBinder);
  119. }
  120. /** Defines callbacks for service binding, passed to bindService() */
  121. private class PreviewImageServiceConnection implements ServiceConnection {
  122. @Override
  123. public void onServiceConnected(ComponentName component, IBinder service) {
  124. if (component.equals(new ComponentName(PreviewImageActivity.this, FileDownloader.class))) {
  125. mDownloaderBinder = (FileDownloaderBinder) service;
  126. if (mRequestWaitingForBinder) {
  127. mRequestWaitingForBinder = false;
  128. Log_OC.d(TAG, "Simulating reselection of current page after connection of download binder");
  129. onPageSelected(mViewPager.getCurrentItem());
  130. }
  131. } else if (component.equals(new ComponentName(PreviewImageActivity.this, FileUploader.class))) {
  132. Log_OC.d(TAG, "Upload service connected");
  133. mUploaderBinder = (FileUploaderBinder) service;
  134. } else {
  135. return;
  136. }
  137. }
  138. @Override
  139. public void onServiceDisconnected(ComponentName component) {
  140. if (component.equals(new ComponentName(PreviewImageActivity.this, FileDownloader.class))) {
  141. Log_OC.d(TAG, "Download service suddenly disconnected");
  142. mDownloaderBinder = null;
  143. } else if (component.equals(new ComponentName(PreviewImageActivity.this, FileUploader.class))) {
  144. Log_OC.d(TAG, "Upload service suddenly disconnected");
  145. mUploaderBinder = null;
  146. }
  147. }
  148. };
  149. @Override
  150. public void onStop() {
  151. super.onStop();
  152. if (mDownloadConnection != null) {
  153. unbindService(mDownloadConnection);
  154. mDownloadConnection = null;
  155. }
  156. if (mUploadConnection != null) {
  157. unbindService(mUploadConnection);
  158. mUploadConnection = null;
  159. }
  160. }
  161. @Override
  162. public void onDestroy() {
  163. super.onDestroy();
  164. }
  165. @Override
  166. public boolean onOptionsItemSelected(MenuItem item) {
  167. boolean returnValue = false;
  168. switch(item.getItemId()){
  169. case android.R.id.home:
  170. backToDisplayActivity();
  171. returnValue = true;
  172. break;
  173. default:
  174. returnValue = super.onOptionsItemSelected(item);
  175. }
  176. return returnValue;
  177. }
  178. @Override
  179. protected void onResume() {
  180. super.onResume();
  181. //Log.e(TAG, "ACTIVITY, ONRESUME");
  182. mDownloadFinishReceiver = new DownloadFinishReceiver();
  183. IntentFilter filter = new IntentFilter(FileDownloader.getDownloadFinishMessage());
  184. filter.addAction(FileDownloader.getDownloadAddedMessage());
  185. registerReceiver(mDownloadFinishReceiver, filter);
  186. }
  187. @Override
  188. protected void onPostResume() {
  189. //Log.e(TAG, "ACTIVITY, ONPOSTRESUME");
  190. super.onPostResume();
  191. }
  192. @Override
  193. public void onPause() {
  194. super.onPause();
  195. unregisterReceiver(mDownloadFinishReceiver);
  196. mDownloadFinishReceiver = null;
  197. }
  198. private void backToDisplayActivity() {
  199. finish();
  200. }
  201. /**
  202. * Show loading dialog
  203. */
  204. public void showLoadingDialog() {
  205. // Construct dialog
  206. LoadingDialog loading = new LoadingDialog(getResources().getString(R.string.wait_a_moment));
  207. FragmentManager fm = getSupportFragmentManager();
  208. FragmentTransaction ft = fm.beginTransaction();
  209. loading.show(ft, DIALOG_WAIT_TAG);
  210. }
  211. /**
  212. * Dismiss loading dialog
  213. */
  214. public void dismissLoadingDialog(){
  215. Fragment frag = getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG);
  216. if (frag != null) {
  217. LoadingDialog loading = (LoadingDialog) frag;
  218. loading.dismiss();
  219. }
  220. }
  221. /**
  222. * {@inheritDoc}
  223. */
  224. @Override
  225. public void onFileStateChanged() {
  226. // nothing to do here!
  227. }
  228. /**
  229. * {@inheritDoc}
  230. */
  231. @Override
  232. public FileDownloaderBinder getFileDownloaderBinder() {
  233. return mDownloaderBinder;
  234. }
  235. @Override
  236. public FileUploaderBinder getFileUploaderBinder() {
  237. return mUploaderBinder;
  238. }
  239. @Override
  240. public void showDetails(OCFile file) {
  241. Intent showDetailsIntent = new Intent(this, FileDisplayActivity.class);
  242. showDetailsIntent.setAction(FileDisplayActivity.ACTION_DETAILS);
  243. showDetailsIntent.putExtra(FileActivity.EXTRA_FILE, file);
  244. showDetailsIntent.putExtra(FileActivity.EXTRA_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(this));
  245. startActivity(showDetailsIntent);
  246. int pos = mPreviewImagePagerAdapter.getFilePosition(file);
  247. file = mPreviewImagePagerAdapter.getFileAt(pos);
  248. }
  249. private void requestForDownload(OCFile file) {
  250. if (mDownloaderBinder == null) {
  251. Log_OC.d(TAG, "requestForDownload called without binder to download service");
  252. } else if (!mDownloaderBinder.isDownloading(getAccount(), file)) {
  253. Intent i = new Intent(this, FileDownloader.class);
  254. i.putExtra(FileDownloader.EXTRA_ACCOUNT, getAccount());
  255. i.putExtra(FileDownloader.EXTRA_FILE, file);
  256. startService(i);
  257. }
  258. }
  259. /**
  260. * This method will be invoked when a new page becomes selected. Animation is not necessarily complete.
  261. *
  262. * @param Position Position index of the new selected page
  263. */
  264. @Override
  265. public void onPageSelected(int position) {
  266. if (mDownloaderBinder == null) {
  267. mRequestWaitingForBinder = true;
  268. } else {
  269. OCFile currentFile = mPreviewImagePagerAdapter.getFileAt(position);
  270. getSupportActionBar().setTitle(currentFile.getFileName());
  271. if (!currentFile.isDown()) {
  272. if (!mPreviewImagePagerAdapter.pendingErrorAt(position)) {
  273. requestForDownload(currentFile);
  274. }
  275. }
  276. }
  277. }
  278. /**
  279. * Called when the scroll state changes. Useful for discovering when the user begins dragging,
  280. * when the pager is automatically settling to the current page. when it is fully stopped/idle.
  281. *
  282. * @param State The new scroll state (SCROLL_STATE_IDLE, _DRAGGING, _SETTLING
  283. */
  284. @Override
  285. public void onPageScrollStateChanged(int state) {
  286. }
  287. /**
  288. * This method will be invoked when the current page is scrolled, either as part of a programmatically
  289. * initiated smooth scroll or a user initiated touch scroll.
  290. *
  291. * @param position Position index of the first page currently being displayed.
  292. * Page position+1 will be visible if positionOffset is nonzero.
  293. *
  294. * @param positionOffset Value from [0, 1) indicating the offset from the page at position.
  295. * @param positionOffsetPixels Value in pixels indicating the offset from position.
  296. */
  297. @Override
  298. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  299. }
  300. /**
  301. * Class waiting for broadcast events from the {@link FielDownloader} service.
  302. *
  303. * Updates the UI when a download is started or finished, provided that it is relevant for the
  304. * folder displayed in the gallery.
  305. */
  306. private class DownloadFinishReceiver extends BroadcastReceiver {
  307. @Override
  308. public void onReceive(Context context, Intent intent) {
  309. String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);
  310. String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
  311. if (getAccount().name.equals(accountName) &&
  312. downloadedRemotePath != null) {
  313. OCFile file = mStorageManager.getFileByPath(downloadedRemotePath);
  314. int position = mPreviewImagePagerAdapter.getFilePosition(file);
  315. boolean downloadWasFine = intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false);
  316. //boolean isOffscreen = Math.abs((mViewPager.getCurrentItem() - position)) <= mViewPager.getOffscreenPageLimit();
  317. if (position >= 0 && intent.getAction().equals(FileDownloader.getDownloadFinishMessage())) {
  318. if (downloadWasFine) {
  319. mPreviewImagePagerAdapter.updateFile(position, file);
  320. } else {
  321. mPreviewImagePagerAdapter.updateWithDownloadError(position);
  322. }
  323. mPreviewImagePagerAdapter.notifyDataSetChanged(); // will trigger the creation of new fragments
  324. } else {
  325. Log_OC.d(TAG, "Download finished, but the fragment is offscreen");
  326. }
  327. }
  328. removeStickyBroadcast(intent);
  329. }
  330. }
  331. @Override
  332. public boolean onTouch(View v, MotionEvent event) {
  333. if (event.getAction() == MotionEvent.ACTION_UP) {
  334. toggleFullScreen();
  335. }
  336. return true;
  337. }
  338. private void toggleFullScreen() {
  339. ActionBar actionBar = getSupportActionBar();
  340. if (mFullScreen) {
  341. actionBar.show();
  342. } else {
  343. actionBar.hide();
  344. }
  345. mFullScreen = !mFullScreen;
  346. }
  347. @Override
  348. protected void onAccountSet(boolean stateWasRecovered) {
  349. if (getAccount() != null) {
  350. OCFile file = getFile();
  351. /// Validate handled file (first image to preview)
  352. if (file == null) {
  353. throw new IllegalStateException("Instanced with a NULL OCFile");
  354. }
  355. if (!file.isImage()) {
  356. throw new IllegalArgumentException("Non-image file passed as argument");
  357. }
  358. mStorageManager = new FileDataStorageManager(getAccount(), getContentResolver());
  359. // Update file according to DB file, if it is possible
  360. if (file.getFileId() > FileDataStorageManager.ROOT_PARENT_ID)
  361. file = mStorageManager.getFileById(file.getFileId());
  362. if (file != null) {
  363. /// Refresh the activity according to the Account and OCFile set
  364. setFile(file); // reset after getting it fresh from mStorageManager
  365. getSupportActionBar().setTitle(getFile().getFileName());
  366. //if (!stateWasRecovered) {
  367. initViewPager();
  368. //}
  369. } else {
  370. // handled file not in the current Account
  371. finish();
  372. }
  373. } else {
  374. Log_OC.wtf(TAG, "onAccountChanged was called with NULL account associated!");
  375. }
  376. }
  377. }