FileActivity.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. /**
  52. * Loads the cownCloud {@link Account} and main {@link OCFile} to be handled by the instance of
  53. * the {@link FileActivity}.
  54. *
  55. * Grants that a valid ownCloud {@link Account} is associated to the instance, or that the user
  56. * is requested to create a new one.
  57. */
  58. @Override
  59. protected void onCreate(Bundle savedInstanceState) {
  60. super.onCreate(savedInstanceState);
  61. if(savedInstanceState != null) {
  62. mFile = savedInstanceState.getParcelable(FileActivity.EXTRA_FILE);
  63. mAccount = savedInstanceState.getParcelable(FileActivity.EXTRA_ACCOUNT);
  64. } else {
  65. mAccount = getIntent().getParcelableExtra(FileActivity.EXTRA_ACCOUNT);
  66. mFile = getIntent().getParcelableExtra(FileActivity.EXTRA_FILE);
  67. }
  68. grantValidAccount();
  69. if (mAccount != null) {
  70. onAccountSet(savedInstanceState != null);
  71. }
  72. }
  73. /**
  74. * Since ownCloud {@link Account}s can be managed from the system setting menu,
  75. * the existence of the {@link Account} associated to the instance must be checked
  76. * every time it is restarted.
  77. */
  78. @Override
  79. protected void onRestart() {
  80. super.onRestart();
  81. Account oldAccount = mAccount;
  82. grantValidAccount();
  83. if (mAccount != null && !mAccount.equals(oldAccount)) {
  84. onAccountSet(false);
  85. }
  86. }
  87. /**
  88. * Validates the ownCloud {@link Account} associated to the Activity any time it is restarted.
  89. *
  90. * If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
  91. *
  92. * If no valid ownCloud {@link Account} exists, mAccount is set to NULL and the user is requested
  93. * to create a new ownCloud {@link Account}.
  94. */
  95. private void grantValidAccount() {
  96. boolean validAccount = (mAccount != null && AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), mAccount.name));
  97. if (!validAccount) {
  98. // get most recently used account as default account
  99. mAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
  100. if (mAccount == null) {
  101. /// no account available: force account creation
  102. createFirstAccount();
  103. mRedirectingToSetupAccount = true;
  104. }
  105. }
  106. }
  107. /**
  108. * Launches the account creation activity. To use when no ownCloud account is available
  109. */
  110. private void createFirstAccount() {
  111. AccountManager am = AccountManager.get(getApplicationContext());
  112. am.addAccount(AccountAuthenticator.ACCOUNT_TYPE,
  113. AccountAuthenticator.AUTH_TOKEN_TYPE_PASSWORD,
  114. null,
  115. null,
  116. this,
  117. new AccountCreationCallback(),
  118. null);
  119. }
  120. /**
  121. * {@inheritDoc}
  122. */
  123. @Override
  124. protected void onSaveInstanceState(Bundle outState) {
  125. super.onSaveInstanceState(outState);
  126. outState.putParcelable(FileActivity.EXTRA_FILE, mFile);
  127. outState.putParcelable(FileActivity.EXTRA_ACCOUNT, mAccount);
  128. }
  129. /**
  130. * Getter for the main {@link OCFile} handled by the activity.
  131. *
  132. * @return Main {@link OCFile} handled by the activity.
  133. */
  134. public OCFile getFile() {
  135. return mFile;
  136. }
  137. /**
  138. * Setter for the main {@link OCFile} handled by the activity.
  139. *
  140. * @param file Main {@link OCFile} to be handled by the activity.
  141. */
  142. public void setFile(OCFile file) {
  143. mFile = file;
  144. }
  145. /**
  146. * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
  147. *
  148. * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
  149. */
  150. public Account getAccount() {
  151. return mAccount;
  152. }
  153. /**
  154. * @return 'True' when the Activity is finishing to enforce the setup of a new account.
  155. */
  156. protected boolean isRedirectingToSetupAccount() {
  157. return mRedirectingToSetupAccount;
  158. }
  159. /**
  160. * Helper class handling a callback from the {@link AccountManager} after the creation of
  161. * a new ownCloud {@link Account} finished, successfully or not.
  162. *
  163. * At this moment, only called after the creation of the first account.
  164. *
  165. * @author David A. Velasco
  166. */
  167. public class AccountCreationCallback implements AccountManagerCallback<Bundle> {
  168. @Override
  169. public void run(AccountManagerFuture<Bundle> future) {
  170. FileActivity.this.mRedirectingToSetupAccount = false;
  171. if (future != null) {
  172. try {
  173. Bundle result;
  174. result = future.getResult();
  175. String name = result.getString(AccountManager.KEY_ACCOUNT_NAME);
  176. String type = result.getString(AccountManager.KEY_ACCOUNT_TYPE);
  177. if (AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), name)) {
  178. FileActivity.this.mAccount = new Account(name, type);
  179. FileActivity.this.onAccountSet(false);
  180. }
  181. } catch (OperationCanceledException e) {
  182. Log_OC.e(TAG, "Account creation canceled");
  183. } catch (Exception e) {
  184. Log_OC.e(TAG, "Account creation finished in exception: ", e);
  185. }
  186. } else {
  187. Log_OC.e(TAG, "Account creation callback with null bundle");
  188. }
  189. if (mAccount == null) {
  190. finish();
  191. }
  192. }
  193. }
  194. /**
  195. * Called when the ownCloud {@link Account} associated to the Activity was just updated.
  196. *
  197. * Child classes must grant that state depending on the {@link Account} is updated.
  198. */
  199. protected abstract void onAccountSet(boolean stateWasRecovered);
  200. public void openFile(OCFile file) {
  201. if (file != null) {
  202. String storagePath = file.getStoragePath();
  203. String encodedStoragePath = WebdavUtils.encodePath(storagePath);
  204. Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
  205. intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype());
  206. intentForSavedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  207. Intent intentForGuessedMimeType = null;
  208. if (storagePath.lastIndexOf('.') >= 0) {
  209. String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
  210. if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
  211. intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
  212. intentForGuessedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), guessedMimeType);
  213. intentForGuessedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  214. }
  215. }
  216. Intent chooserIntent = null;
  217. if (intentForGuessedMimeType != null) {
  218. chooserIntent = Intent.createChooser(intentForGuessedMimeType, getString(R.string.actionbar_open_with));
  219. } else {
  220. chooserIntent = Intent.createChooser(intentForSavedMimeType, getString(R.string.actionbar_open_with));
  221. }
  222. startActivity(chooserIntent);
  223. } else {
  224. Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
  225. }
  226. }
  227. }