Connected selection of app to send share link with creation of share resource, and...
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / FileActivity.java
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
19 package com.owncloud.android.ui.activity;
20
21 import android.accounts.Account;
22 import android.accounts.AccountManager;
23 import android.accounts.AccountManagerCallback;
24 import android.accounts.AccountManagerFuture;
25 import android.accounts.OperationCanceledException;
26 import android.os.Bundle;
27 import android.os.Handler;
28
29 import com.actionbarsherlock.app.SherlockFragmentActivity;
30 import com.owncloud.android.MainApp;
31 import com.owncloud.android.authentication.AccountUtils;
32 import com.owncloud.android.datamodel.FileDataStorageManager;
33 import com.owncloud.android.datamodel.OCFile;
34 import com.owncloud.android.files.FileOperationsHelper;
35 import com.owncloud.android.lib.operations.common.OnRemoteOperationListener;
36 import com.owncloud.android.lib.operations.common.RemoteOperation;
37 import com.owncloud.android.lib.operations.common.RemoteOperationResult;
38
39 import com.owncloud.android.utils.Log_OC;
40
41
42 /**
43 * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud {@link Account}s .
44 *
45 * @author David A. Velasco
46 */
47 public class FileActivity extends SherlockFragmentActivity implements OnRemoteOperationListener {
48
49 public static final String EXTRA_FILE = "com.owncloud.android.ui.activity.FILE";
50 public static final String EXTRA_ACCOUNT = "com.owncloud.android.ui.activity.ACCOUNT";
51 public static final String EXTRA_WAITING_TO_PREVIEW = "com.owncloud.android.ui.activity.WAITING_TO_PREVIEW";
52 public static final String EXTRA_FROM_NOTIFICATION= "com.owncloud.android.ui.activity.FROM_NOTIFICATION";
53
54 public static final String TAG = FileActivity.class.getSimpleName();
55
56
57 /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located. */
58 private Account mAccount;
59
60 /** Main {@link OCFile} handled by the activity.*/
61 private OCFile mFile;
62
63 /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud {@link Account} */
64 private boolean mRedirectingToSetupAccount = false;
65
66 /** Flag to signal when the value of mAccount was set */
67 private boolean mAccountWasSet;
68
69 /** Flag to signal when the value of mAccount was restored from a saved state */
70 private boolean mAccountWasRestored;
71
72 /** Flag to signal if the activity is launched by a notification */
73 private boolean mFromNotification;
74
75 /** Messages handler associated to the main thread and the life cycle of the activity */
76 private Handler mHandler;
77
78 /** Access point to the cached database for the current ownCloud {@link Account} */
79 private FileDataStorageManager mStorageManager = null;
80
81 private FileOperationsHelper mFileOperationsHelper;
82
83
84 /**
85 * Loads the ownCloud {@link Account} and main {@link OCFile} to be handled by the instance of
86 * the {@link FileActivity}.
87 *
88 * Grants that a valid ownCloud {@link Account} is associated to the instance, or that the user
89 * is requested to create a new one.
90 */
91 @Override
92 protected void onCreate(Bundle savedInstanceState) {
93 super.onCreate(savedInstanceState);
94 mHandler = new Handler();
95 mFileOperationsHelper = new FileOperationsHelper();
96 Account account;
97 if(savedInstanceState != null) {
98 account = savedInstanceState.getParcelable(FileActivity.EXTRA_ACCOUNT);
99 mFile = savedInstanceState.getParcelable(FileActivity.EXTRA_FILE);
100 mFromNotification = savedInstanceState.getBoolean(FileActivity.EXTRA_FROM_NOTIFICATION);
101 } else {
102 account = getIntent().getParcelableExtra(FileActivity.EXTRA_ACCOUNT);
103 mFile = getIntent().getParcelableExtra(FileActivity.EXTRA_FILE);
104 mFromNotification = getIntent().getBooleanExtra(FileActivity.EXTRA_FROM_NOTIFICATION, false);
105 }
106
107 setAccount(account, savedInstanceState != null);
108
109 }
110
111
112 /**
113 * Since ownCloud {@link Account}s can be managed from the system setting menu,
114 * the existence of the {@link Account} associated to the instance must be checked
115 * every time it is restarted.
116 */
117 @Override
118 protected void onRestart() {
119 super.onRestart();
120 boolean validAccount = (mAccount != null && AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), mAccount.name));
121 if (!validAccount) {
122 swapToDefaultAccount();
123 }
124
125 }
126
127
128 @Override
129 protected void onStart() {
130 super.onStart();
131 if (mAccountWasSet) {
132 onAccountSet(mAccountWasRestored);
133 }
134 }
135
136
137 /**
138 * Sets and validates the ownCloud {@link Account} associated to the Activity.
139 *
140 * If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
141 *
142 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
143 *
144 * @param account New {@link Account} to set.
145 * @param savedAccount When 'true', account was retrieved from a saved instance state.
146 */
147 private void setAccount(Account account, boolean savedAccount) {
148 Account oldAccount = mAccount;
149 boolean validAccount = (account != null && AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), account.name));
150 if (validAccount) {
151 mAccount = account;
152 mAccountWasSet = true;
153 mAccountWasRestored = (savedAccount || mAccount.equals(oldAccount));
154
155 } else {
156 swapToDefaultAccount();
157 }
158 }
159
160
161 /**
162 * Tries to swap the current ownCloud {@link Account} for other valid and existing.
163 *
164 * If no valid ownCloud {@link Account} exists, the the user is requested
165 * to create a new ownCloud {@link Account}.
166 *
167 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
168 *
169 * @return 'True' if the checked {@link Account} was valid.
170 */
171 private void swapToDefaultAccount() {
172 // default to the most recently used account
173 Account newAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
174 if (newAccount == null) {
175 /// no account available: force account creation
176 createFirstAccount();
177 mRedirectingToSetupAccount = true;
178 mAccountWasSet = false;
179 mAccountWasRestored = false;
180
181 } else {
182 mAccountWasSet = true;
183 mAccountWasRestored = (newAccount.equals(mAccount));
184 mAccount = newAccount;
185 }
186 }
187
188
189 /**
190 * Launches the account creation activity. To use when no ownCloud account is available
191 */
192 private void createFirstAccount() {
193 AccountManager am = AccountManager.get(getApplicationContext());
194 am.addAccount(MainApp.getAccountType(),
195 null,
196 null,
197 null,
198 this,
199 new AccountCreationCallback(),
200 null);
201 }
202
203
204 /**
205 * {@inheritDoc}
206 */
207 @Override
208 protected void onSaveInstanceState(Bundle outState) {
209 super.onSaveInstanceState(outState);
210 outState.putParcelable(FileActivity.EXTRA_FILE, mFile);
211 outState.putParcelable(FileActivity.EXTRA_ACCOUNT, mAccount);
212 outState.putBoolean(FileActivity.EXTRA_FROM_NOTIFICATION, mFromNotification);
213 }
214
215
216 /**
217 * Getter for the main {@link OCFile} handled by the activity.
218 *
219 * @return Main {@link OCFile} handled by the activity.
220 */
221 public OCFile getFile() {
222 return mFile;
223 }
224
225
226 /**
227 * Setter for the main {@link OCFile} handled by the activity.
228 *
229 * @param file Main {@link OCFile} to be handled by the activity.
230 */
231 public void setFile(OCFile file) {
232 mFile = file;
233 }
234
235
236 /**
237 * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
238 *
239 * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
240 */
241 public Account getAccount() {
242 return mAccount;
243 }
244
245 /**
246 * @return Value of mFromNotification: True if the Activity is launched by a notification
247 */
248 public boolean fromNotification() {
249 return mFromNotification;
250 }
251
252 /**
253 * @return 'True' when the Activity is finishing to enforce the setup of a new account.
254 */
255 protected boolean isRedirectingToSetupAccount() {
256 return mRedirectingToSetupAccount;
257 }
258
259
260 /**
261 * Helper class handling a callback from the {@link AccountManager} after the creation of
262 * a new ownCloud {@link Account} finished, successfully or not.
263 *
264 * At this moment, only called after the creation of the first account.
265 *
266 * @author David A. Velasco
267 */
268 public class AccountCreationCallback implements AccountManagerCallback<Bundle> {
269
270 @Override
271 public void run(AccountManagerFuture<Bundle> future) {
272 FileActivity.this.mRedirectingToSetupAccount = false;
273 boolean accountWasSet = false;
274 if (future != null) {
275 try {
276 Bundle result;
277 result = future.getResult();
278 String name = result.getString(AccountManager.KEY_ACCOUNT_NAME);
279 String type = result.getString(AccountManager.KEY_ACCOUNT_TYPE);
280 if (AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), name)) {
281 setAccount(new Account(name, type), false);
282 accountWasSet = true;
283 }
284 } catch (OperationCanceledException e) {
285 Log_OC.d(TAG, "Account creation canceled");
286
287 } catch (Exception e) {
288 Log_OC.e(TAG, "Account creation finished in exception: ", e);
289 }
290
291 } else {
292 Log_OC.e(TAG, "Account creation callback with null bundle");
293 }
294 if (!accountWasSet) {
295 moveTaskToBack(true);
296 }
297 }
298
299 }
300
301
302 /**
303 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
304 *
305 * Child classes must grant that state depending on the {@link Account} is updated.
306 */
307 protected void onAccountSet(boolean stateWasRecovered) {
308 if (getAccount() != null) {
309 mStorageManager = new FileDataStorageManager(getAccount(), getContentResolver());
310
311 } else {
312 Log_OC.wtf(TAG, "onAccountChanged was called with NULL account associated!");
313 }
314 }
315
316
317 public FileDataStorageManager getStorageManager() {
318 return mStorageManager;
319 }
320
321
322 public OnRemoteOperationListener getRemoteOperationListener() {
323 return this;
324 }
325
326
327 public Handler getHandler() {
328 return mHandler;
329 }
330
331 public FileOperationsHelper getFileOperationsHelper() {
332 return mFileOperationsHelper;
333 }
334
335 /**
336 *
337 * @param operation Removal operation performed.
338 * @param result Result of the removal.
339 */
340 @Override
341 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
342 // does nothing ; to override in child classes
343 Log_OC.d(TAG, "Received result of operation in FileActivity");
344 }
345
346
347 }