PreviewImageActivity.java 17 KB

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