FileActivity.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* ownCloud Android client application
  2. * Copyright (C) 2011 Bartek Przybylski
  3. * Copyright (C) 2012-2013 ownCloud Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2,
  7. * as published by the Free Software Foundation.
  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 android.accounts.Account;
  20. import android.accounts.AccountManager;
  21. import android.accounts.AccountManagerCallback;
  22. import android.accounts.AccountManagerFuture;
  23. import android.accounts.OperationCanceledException;
  24. import android.content.Intent;
  25. import android.net.Uri;
  26. import android.os.Bundle;
  27. import android.webkit.MimeTypeMap;
  28. import com.actionbarsherlock.app.SherlockFragmentActivity;
  29. import com.owncloud.android.Log_OC;
  30. import com.owncloud.android.R;
  31. import com.owncloud.android.authentication.AccountAuthenticator;
  32. import com.owncloud.android.authentication.AccountUtils;
  33. import com.owncloud.android.datamodel.OCFile;
  34. import eu.alefzero.webdav.WebdavUtils;
  35. /**
  36. * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud {@link Account}s .
  37. *
  38. * @author David A. Velasco
  39. */
  40. public abstract class FileActivity extends SherlockFragmentActivity {
  41. public static final String EXTRA_FILE = "com.owncloud.android.ui.activity.FILE";
  42. public static final String EXTRA_ACCOUNT = "com.owncloud.android.ui.activity.ACCOUNT";
  43. public static final String EXTRA_WAITING_TO_PREVIEW = "com.owncloud.android.ui.activity.WAITING_TO_PREVIEW";
  44. public static final String TAG = FileActivity.class.getSimpleName();
  45. /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located. */
  46. private Account mAccount;
  47. /** Main {@link OCFile} handled by the activity.*/
  48. private OCFile mFile;
  49. /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud {@link Account} */
  50. private boolean mRedirectingToSetupAccount = false;
  51. /** Flag to signal when the value of mAccount was set */
  52. private boolean mAccountWasSet;
  53. /** Flag to signal when the value of mAccount was restored from a saved state */
  54. private boolean mAccountWasRestored;
  55. /**
  56. * Loads the ownCloud {@link Account} and main {@link OCFile} to be handled by the instance of
  57. * the {@link FileActivity}.
  58. *
  59. * Grants that a valid ownCloud {@link Account} is associated to the instance, or that the user
  60. * is requested to create a new one.
  61. */
  62. @Override
  63. protected void onCreate(Bundle savedInstanceState) {
  64. super.onCreate(savedInstanceState);
  65. Account account;
  66. if(savedInstanceState != null) {
  67. account = savedInstanceState.getParcelable(FileActivity.EXTRA_ACCOUNT);
  68. mFile = savedInstanceState.getParcelable(FileActivity.EXTRA_FILE);
  69. } else {
  70. account = getIntent().getParcelableExtra(FileActivity.EXTRA_ACCOUNT);
  71. mFile = getIntent().getParcelableExtra(FileActivity.EXTRA_FILE);
  72. }
  73. setAccount(account, savedInstanceState != null);
  74. }
  75. /**
  76. * Since ownCloud {@link Account}s can be managed from the system setting menu,
  77. * the existence of the {@link Account} associated to the instance must be checked
  78. * every time it is restarted.
  79. */
  80. @Override
  81. protected void onRestart() {
  82. super.onRestart();
  83. boolean validAccount = (mAccount != null && AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), mAccount.name));
  84. if (!validAccount) {
  85. swapToDefaultAccount();
  86. }
  87. }
  88. @Override
  89. protected void onStart() {
  90. super.onStart();
  91. if (mAccountWasSet) {
  92. onAccountSet(mAccountWasRestored);
  93. }
  94. }
  95. /**
  96. * Sets and validates the ownCloud {@link Account} associated to the Activity.
  97. *
  98. * If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
  99. *
  100. * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
  101. *
  102. * @param account New {@link Account} to set.
  103. * @param savedAccount When 'true', account was retrieved from a saved instance state.
  104. */
  105. private void setAccount(Account account, boolean savedAccount) {
  106. Account oldAccount = mAccount;
  107. boolean validAccount = (account != null && AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), account.name));
  108. if (validAccount) {
  109. mAccount = account;
  110. mAccountWasSet = true;
  111. mAccountWasRestored = (savedAccount || mAccount.equals(oldAccount));
  112. } else {
  113. swapToDefaultAccount();
  114. }
  115. }
  116. /**
  117. * Tries to swap the current ownCloud {@link Account} for other valid and existing.
  118. *
  119. * If no valid ownCloud {@link Account} exists, the the user is requested
  120. * to create a new ownCloud {@link Account}.
  121. *
  122. * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
  123. *
  124. * @return 'True' if the checked {@link Account} was valid.
  125. */
  126. private void swapToDefaultAccount() {
  127. // default to the most recently used account
  128. Account newAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
  129. if (newAccount == null) {
  130. /// no account available: force account creation
  131. createFirstAccount();
  132. mRedirectingToSetupAccount = true;
  133. mAccountWasSet = false;
  134. mAccountWasRestored = false;
  135. } else {
  136. mAccountWasSet = true;
  137. mAccountWasRestored = (newAccount.equals(mAccount));
  138. mAccount = newAccount;
  139. }
  140. }
  141. /**
  142. * Launches the account creation activity. To use when no ownCloud account is available
  143. */
  144. private void createFirstAccount() {
  145. AccountManager am = AccountManager.get(getApplicationContext());
  146. am.addAccount(AccountAuthenticator.ACCOUNT_TYPE,
  147. null,
  148. null,
  149. null,
  150. this,
  151. new AccountCreationCallback(),
  152. null);
  153. }
  154. /**
  155. * {@inheritDoc}
  156. */
  157. @Override
  158. protected void onSaveInstanceState(Bundle outState) {
  159. super.onSaveInstanceState(outState);
  160. outState.putParcelable(FileActivity.EXTRA_FILE, mFile);
  161. outState.putParcelable(FileActivity.EXTRA_ACCOUNT, mAccount);
  162. }
  163. /**
  164. * Getter for the main {@link OCFile} handled by the activity.
  165. *
  166. * @return Main {@link OCFile} handled by the activity.
  167. */
  168. public OCFile getFile() {
  169. return mFile;
  170. }
  171. /**
  172. * Setter for the main {@link OCFile} handled by the activity.
  173. *
  174. * @param file Main {@link OCFile} to be handled by the activity.
  175. */
  176. public void setFile(OCFile file) {
  177. mFile = file;
  178. }
  179. /**
  180. * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
  181. *
  182. * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
  183. */
  184. public Account getAccount() {
  185. return mAccount;
  186. }
  187. /**
  188. * @return 'True' when the Activity is finishing to enforce the setup of a new account.
  189. */
  190. protected boolean isRedirectingToSetupAccount() {
  191. return mRedirectingToSetupAccount;
  192. }
  193. /**
  194. * Helper class handling a callback from the {@link AccountManager} after the creation of
  195. * a new ownCloud {@link Account} finished, successfully or not.
  196. *
  197. * At this moment, only called after the creation of the first account.
  198. *
  199. * @author David A. Velasco
  200. */
  201. public class AccountCreationCallback implements AccountManagerCallback<Bundle> {
  202. @Override
  203. public void run(AccountManagerFuture<Bundle> future) {
  204. FileActivity.this.mRedirectingToSetupAccount = false;
  205. boolean accountWasSet = false;
  206. if (future != null) {
  207. try {
  208. Bundle result;
  209. result = future.getResult();
  210. String name = result.getString(AccountManager.KEY_ACCOUNT_NAME);
  211. String type = result.getString(AccountManager.KEY_ACCOUNT_TYPE);
  212. if (AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), name)) {
  213. setAccount(new Account(name, type), false);
  214. accountWasSet = true;
  215. }
  216. } catch (OperationCanceledException e) {
  217. Log_OC.d(TAG, "Account creation canceled");
  218. } catch (Exception e) {
  219. Log_OC.e(TAG, "Account creation finished in exception: ", e);
  220. }
  221. } else {
  222. Log_OC.e(TAG, "Account creation callback with null bundle");
  223. }
  224. if (!accountWasSet) {
  225. moveTaskToBack(true);
  226. }
  227. }
  228. }
  229. /**
  230. * Called when the ownCloud {@link Account} associated to the Activity was just updated.
  231. *
  232. * Child classes must grant that state depending on the {@link Account} is updated.
  233. */
  234. protected abstract void onAccountSet(boolean stateWasRecovered);
  235. public void openFile(OCFile file) {
  236. if (file != null) {
  237. String storagePath = file.getStoragePath();
  238. String encodedStoragePath = WebdavUtils.encodePath(storagePath);
  239. Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
  240. intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype());
  241. intentForSavedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  242. Intent intentForGuessedMimeType = null;
  243. if (storagePath.lastIndexOf('.') >= 0) {
  244. String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
  245. if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
  246. intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
  247. intentForGuessedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), guessedMimeType);
  248. intentForGuessedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  249. }
  250. }
  251. Intent chooserIntent = null;
  252. if (intentForGuessedMimeType != null) {
  253. chooserIntent = Intent.createChooser(intentForGuessedMimeType, getString(R.string.actionbar_open_with));
  254. } else {
  255. chooserIntent = Intent.createChooser(intentForSavedMimeType, getString(R.string.actionbar_open_with));
  256. }
  257. startActivity(chooserIntent);
  258. } else {
  259. Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
  260. }
  261. }
  262. }