OperationsService.java 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * @author masensio
  5. * @author David A. Velasco
  6. * @author Andy Scherzinger
  7. * Copyright (C) 2015 ownCloud Inc.
  8. * Copyright (C) 2018 Andy Scherzinger
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. package com.owncloud.android.services;
  23. import android.accounts.Account;
  24. import android.accounts.AccountsException;
  25. import android.app.Service;
  26. import android.content.Intent;
  27. import android.net.Uri;
  28. import android.os.Binder;
  29. import android.os.Handler;
  30. import android.os.HandlerThread;
  31. import android.os.IBinder;
  32. import android.os.Looper;
  33. import android.os.Message;
  34. import android.os.Process;
  35. import android.text.TextUtils;
  36. import android.util.Pair;
  37. import com.owncloud.android.MainApp;
  38. import com.owncloud.android.datamodel.FileDataStorageManager;
  39. import com.owncloud.android.datamodel.OCFile;
  40. import com.owncloud.android.lib.common.OwnCloudAccount;
  41. import com.owncloud.android.lib.common.OwnCloudClient;
  42. import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
  43. import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
  44. import com.owncloud.android.lib.common.operations.RemoteOperation;
  45. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  46. import com.owncloud.android.lib.common.utils.Log_OC;
  47. import com.owncloud.android.lib.resources.files.RestoreFileVersionRemoteOperation;
  48. import com.owncloud.android.lib.resources.files.model.FileVersion;
  49. import com.owncloud.android.lib.resources.shares.ShareType;
  50. import com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation;
  51. import com.owncloud.android.operations.CheckCurrentCredentialsOperation;
  52. import com.owncloud.android.operations.CopyFileOperation;
  53. import com.owncloud.android.operations.CreateFolderOperation;
  54. import com.owncloud.android.operations.CreateShareViaLinkOperation;
  55. import com.owncloud.android.operations.CreateShareWithShareeOperation;
  56. import com.owncloud.android.operations.GetServerInfoOperation;
  57. import com.owncloud.android.operations.MoveFileOperation;
  58. import com.owncloud.android.operations.RemoveFileOperation;
  59. import com.owncloud.android.operations.RenameFileOperation;
  60. import com.owncloud.android.operations.SynchronizeFileOperation;
  61. import com.owncloud.android.operations.SynchronizeFolderOperation;
  62. import com.owncloud.android.operations.UnshareOperation;
  63. import com.owncloud.android.operations.UpdateNoteForShareOperation;
  64. import com.owncloud.android.operations.UpdateSharePermissionsOperation;
  65. import com.owncloud.android.operations.UpdateShareViaLinkOperation;
  66. import com.owncloud.android.operations.common.SyncOperation;
  67. import java.io.IOException;
  68. import java.util.Iterator;
  69. import java.util.concurrent.ConcurrentHashMap;
  70. import java.util.concurrent.ConcurrentLinkedQueue;
  71. import java.util.concurrent.ConcurrentMap;
  72. import dagger.android.AndroidInjection;
  73. public class OperationsService extends Service {
  74. private static final String TAG = OperationsService.class.getSimpleName();
  75. public static final String EXTRA_ACCOUNT = "ACCOUNT";
  76. public static final String EXTRA_SERVER_URL = "SERVER_URL";
  77. public static final String EXTRA_OAUTH2_QUERY_PARAMETERS = "OAUTH2_QUERY_PARAMETERS";
  78. public static final String EXTRA_REMOTE_PATH = "REMOTE_PATH";
  79. public static final String EXTRA_NEWNAME = "NEWNAME";
  80. public static final String EXTRA_REMOVE_ONLY_LOCAL = "REMOVE_LOCAL_COPY";
  81. public static final String EXTRA_CREATE_FULL_PATH = "CREATE_FULL_PATH";
  82. public static final String EXTRA_SYNC_FILE_CONTENTS = "SYNC_FILE_CONTENTS";
  83. public static final String EXTRA_RESULT = "RESULT";
  84. public static final String EXTRA_NEW_PARENT_PATH = "NEW_PARENT_PATH";
  85. public static final String EXTRA_FILE = "FILE";
  86. public static final String EXTRA_FILE_VERSION = "FILE_VERSION";
  87. public static final String EXTRA_SHARE_PASSWORD = "SHARE_PASSWORD";
  88. public static final String EXTRA_SHARE_TYPE = "SHARE_TYPE";
  89. public static final String EXTRA_SHARE_WITH = "SHARE_WITH";
  90. public static final String EXTRA_SHARE_EXPIRATION_DATE_IN_MILLIS = "SHARE_EXPIRATION_YEAR";
  91. public static final String EXTRA_SHARE_PERMISSIONS = "SHARE_PERMISSIONS";
  92. public static final String EXTRA_SHARE_PUBLIC_UPLOAD = "SHARE_PUBLIC_UPLOAD";
  93. public static final String EXTRA_SHARE_HIDE_FILE_DOWNLOAD = "HIDE_FILE_DOWNLOAD";
  94. public static final String EXTRA_SHARE_ID = "SHARE_ID";
  95. public static final String EXTRA_SHARE_NOTE = "SHARE_NOTE";
  96. public static final String EXTRA_IN_BACKGROUND = "IN_BACKGROUND";
  97. public static final String EXTRA_COOKIE = "COOKIE";
  98. public static final String ACTION_CREATE_SHARE_VIA_LINK = "CREATE_SHARE_VIA_LINK";
  99. public static final String ACTION_CREATE_SHARE_WITH_SHAREE = "CREATE_SHARE_WITH_SHAREE";
  100. public static final String ACTION_UNSHARE = "UNSHARE";
  101. public static final String ACTION_UPDATE_SHARE = "UPDATE_SHARE";
  102. public static final String ACTION_UPDATE_SHARE_NOTE = "UPDATE_SHARE_NOTE";
  103. public static final String ACTION_GET_SERVER_INFO = "GET_SERVER_INFO";
  104. public static final String ACTION_GET_USER_NAME = "GET_USER_NAME";
  105. public static final String ACTION_RENAME = "RENAME";
  106. public static final String ACTION_REMOVE = "REMOVE";
  107. public static final String ACTION_CREATE_FOLDER = "CREATE_FOLDER";
  108. public static final String ACTION_SYNC_FILE = "SYNC_FILE";
  109. public static final String ACTION_SYNC_FOLDER = "SYNC_FOLDER";
  110. public static final String ACTION_MOVE_FILE = "MOVE_FILE";
  111. public static final String ACTION_COPY_FILE = "COPY_FILE";
  112. public static final String ACTION_CHECK_CURRENT_CREDENTIALS = "CHECK_CURRENT_CREDENTIALS";
  113. public static final String ACTION_RESTORE_VERSION = "RESTORE_VERSION";
  114. public static final String ACTION_OPERATION_ADDED = OperationsService.class.getName() + ".OPERATION_ADDED";
  115. public static final String ACTION_OPERATION_FINISHED = OperationsService.class.getName() + ".OPERATION_FINISHED";
  116. private ServiceHandler mOperationsHandler;
  117. private OperationsServiceBinder mOperationsBinder;
  118. private SyncFolderHandler mSyncFolderHandler;
  119. private ConcurrentMap<Integer, Pair<RemoteOperation, RemoteOperationResult>>
  120. mUndispatchedFinishedOperations = new ConcurrentHashMap<>();
  121. private static class Target {
  122. public Uri mServerUrl;
  123. public Account mAccount;
  124. public String mCookie;
  125. public Target(Account account, Uri serverUrl, String cookie) {
  126. mAccount = account;
  127. mServerUrl = serverUrl;
  128. mCookie = cookie;
  129. }
  130. }
  131. /**
  132. * Service initialization
  133. */
  134. @Override
  135. public void onCreate() {
  136. super.onCreate();
  137. AndroidInjection.inject(this);
  138. Log_OC.d(TAG, "Creating service");
  139. // First worker thread for most of operations
  140. HandlerThread thread = new HandlerThread("Operations thread",
  141. Process.THREAD_PRIORITY_BACKGROUND);
  142. thread.start();
  143. mOperationsHandler = new ServiceHandler(thread.getLooper(), this);
  144. mOperationsBinder = new OperationsServiceBinder(mOperationsHandler);
  145. // Separated worker thread for download of folders (WIP)
  146. thread = new HandlerThread("Syncfolder thread", Process.THREAD_PRIORITY_BACKGROUND);
  147. thread.start();
  148. mSyncFolderHandler = new SyncFolderHandler(thread.getLooper(), this);
  149. }
  150. /**
  151. * Entry point to add a new operation to the queue of operations.
  152. * <p/>
  153. * New operations are added calling to startService(), resulting in a call to this method.
  154. * This ensures the service will keep on working although the caller activity goes away.
  155. */
  156. @Override
  157. public int onStartCommand(Intent intent, int flags, int startId) {
  158. Log_OC.d(TAG, "Starting command with id " + startId);
  159. // WIP: for the moment, only SYNC_FOLDER is expected here;
  160. // the rest of the operations are requested through the Binder
  161. if (intent != null && ACTION_SYNC_FOLDER.equals(intent.getAction())) {
  162. if (!intent.hasExtra(EXTRA_ACCOUNT) || !intent.hasExtra(EXTRA_REMOTE_PATH)) {
  163. Log_OC.e(TAG, "Not enough information provided in intent");
  164. return START_NOT_STICKY;
  165. }
  166. Account account = intent.getParcelableExtra(EXTRA_ACCOUNT);
  167. String remotePath = intent.getStringExtra(EXTRA_REMOTE_PATH);
  168. Pair<Account, String> itemSyncKey = new Pair<>(account, remotePath);
  169. Pair<Target, RemoteOperation> itemToQueue = newOperation(intent);
  170. if (itemToQueue != null) {
  171. mSyncFolderHandler.add(account, remotePath,
  172. (SynchronizeFolderOperation)itemToQueue.second);
  173. Message msg = mSyncFolderHandler.obtainMessage();
  174. msg.arg1 = startId;
  175. msg.obj = itemSyncKey;
  176. mSyncFolderHandler.sendMessage(msg);
  177. }
  178. } else {
  179. Message msg = mOperationsHandler.obtainMessage();
  180. msg.arg1 = startId;
  181. mOperationsHandler.sendMessage(msg);
  182. }
  183. return START_NOT_STICKY;
  184. }
  185. @Override
  186. public void onDestroy() {
  187. Log_OC.v(TAG, "Destroying service" );
  188. // Saving cookies
  189. OwnCloudClientManagerFactory.getDefaultSingleton()
  190. .saveAllClients(this, MainApp.getAccountType(getApplicationContext()));
  191. mUndispatchedFinishedOperations.clear();
  192. mOperationsBinder = null;
  193. mOperationsHandler.getLooper().quit();
  194. mOperationsHandler = null;
  195. mSyncFolderHandler.getLooper().quit();
  196. mSyncFolderHandler = null;
  197. super.onDestroy();
  198. }
  199. /**
  200. * Provides a binder object that clients can use to perform actions on the queue of operations,
  201. * except the addition of new operations.
  202. */
  203. @Override
  204. public IBinder onBind(Intent intent) {
  205. return mOperationsBinder;
  206. }
  207. /**
  208. * Called when ALL the bound clients were unbound.
  209. */
  210. @Override
  211. public boolean onUnbind(Intent intent) {
  212. mOperationsBinder.clearListeners();
  213. return false; // not accepting rebinding (default behaviour)
  214. }
  215. /**
  216. * Binder to let client components to perform actions on the queue of operations.
  217. * <p/>
  218. * It provides by itself the available operations.
  219. */
  220. public class OperationsServiceBinder extends Binder /* implements OnRemoteOperationListener */ {
  221. /**
  222. * Map of listeners that will be reported about the end of operations from a
  223. * {@link OperationsServiceBinder} instance
  224. */
  225. private final ConcurrentMap<OnRemoteOperationListener, Handler> mBoundListeners = new ConcurrentHashMap<>();
  226. private ServiceHandler mServiceHandler;
  227. public OperationsServiceBinder(ServiceHandler serviceHandler) {
  228. mServiceHandler = serviceHandler;
  229. }
  230. /**
  231. * Cancels a pending or current synchronization.
  232. *
  233. * @param account ownCloud account where the remote folder is stored.
  234. * @param file A folder in the queue of pending synchronizations
  235. */
  236. public void cancel(Account account, OCFile file) {
  237. mSyncFolderHandler.cancel(account, file);
  238. }
  239. public void clearListeners() {
  240. mBoundListeners.clear();
  241. }
  242. /**
  243. * Adds a listener interested in being reported about the end of operations.
  244. *
  245. * @param listener Object to notify about the end of operations.
  246. * @param callbackHandler {@link Handler} to access the listener without
  247. * breaking Android threading protection.
  248. */
  249. public void addOperationListener (OnRemoteOperationListener listener,
  250. Handler callbackHandler) {
  251. synchronized (mBoundListeners) {
  252. mBoundListeners.put(listener, callbackHandler);
  253. }
  254. }
  255. /**
  256. * Removes a listener from the list of objects interested in the being reported about
  257. * the end of operations.
  258. *
  259. * @param listener Object to notify about progress of transfer.
  260. */
  261. public void removeOperationListener(OnRemoteOperationListener listener) {
  262. synchronized (mBoundListeners) {
  263. mBoundListeners.remove(listener);
  264. }
  265. }
  266. /**
  267. * TODO - IMPORTANT: update implementation when more operations are moved into the service
  268. *
  269. * @return 'True' when an operation that enforces the user to wait for completion is
  270. * in process.
  271. */
  272. public boolean isPerformingBlockingOperation() {
  273. return !mServiceHandler.mPendingOperations.isEmpty();
  274. }
  275. /**
  276. * Creates and adds to the queue a new operation, as described by operationIntent.
  277. *
  278. * Calls startService to make the operation is processed by the ServiceHandler.
  279. *
  280. * @param operationIntent Intent describing a new operation to queue and execute.
  281. * @return Identifier of the operation created, or null if failed.
  282. */
  283. public long queueNewOperation(Intent operationIntent) {
  284. Pair<Target, RemoteOperation> itemToQueue = newOperation(operationIntent);
  285. if (itemToQueue != null) {
  286. mServiceHandler.mPendingOperations.add(itemToQueue);
  287. startService(new Intent(OperationsService.this, OperationsService.class));
  288. return itemToQueue.second.hashCode();
  289. } else {
  290. return Long.MAX_VALUE;
  291. }
  292. }
  293. public boolean dispatchResultIfFinished(int operationId,
  294. OnRemoteOperationListener listener) {
  295. Pair<RemoteOperation, RemoteOperationResult> undispatched =
  296. mUndispatchedFinishedOperations.remove(operationId);
  297. if (undispatched != null) {
  298. listener.onRemoteOperationFinish(undispatched.first, undispatched.second);
  299. return true;
  300. } else {
  301. return !mServiceHandler.mPendingOperations.isEmpty();
  302. }
  303. }
  304. /**
  305. * Returns True when the file described by 'file' in the ownCloud account 'account' is
  306. * downloading or waiting to download.
  307. *
  308. * If 'file' is a directory, returns 'true' if some of its descendant files is downloading
  309. * or waiting to download.
  310. *
  311. * @param account ownCloud account where the remote file is stored.
  312. * @param file File to check if something is synchronizing
  313. * / downloading / uploading inside.
  314. */
  315. public boolean isSynchronizing(Account account, OCFile file) {
  316. return mSyncFolderHandler.isSynchronizing(account, file.getRemotePath());
  317. }
  318. }
  319. /**
  320. * Operations worker. Performs the pending operations in the order they were requested.
  321. *
  322. * Created with the Looper of a new thread, started in {@link OperationsService#onCreate()}.
  323. */
  324. private static class ServiceHandler extends Handler {
  325. // don't make it a final class, and don't remove the static ; lint will warn about a p
  326. // ossible memory leak
  327. OperationsService mService;
  328. private ConcurrentLinkedQueue<Pair<Target, RemoteOperation>> mPendingOperations =
  329. new ConcurrentLinkedQueue<>();
  330. private RemoteOperation mCurrentOperation;
  331. private Target mLastTarget;
  332. private OwnCloudClient mOwnCloudClient;
  333. private FileDataStorageManager mStorageManager;
  334. public ServiceHandler(Looper looper, OperationsService service) {
  335. super(looper);
  336. if (service == null) {
  337. throw new IllegalArgumentException("Received invalid NULL in parameter 'service'");
  338. }
  339. mService = service;
  340. }
  341. @Override
  342. public void handleMessage(Message msg) {
  343. nextOperation();
  344. Log_OC.d(TAG, "Stopping after command with id " + msg.arg1);
  345. mService.stopSelf(msg.arg1);
  346. }
  347. /**
  348. * Performs the next operation in the queue
  349. */
  350. private void nextOperation() {
  351. //Log_OC.e(TAG, "nextOperation init" );
  352. Pair<Target, RemoteOperation> next;
  353. synchronized(mPendingOperations) {
  354. next = mPendingOperations.peek();
  355. }
  356. if (next != null) {
  357. mCurrentOperation = next.second;
  358. RemoteOperationResult result = null;
  359. try {
  360. /// prepare client object to send the request to the ownCloud server
  361. if (mLastTarget == null || !mLastTarget.equals(next.first)) {
  362. mLastTarget = next.first;
  363. if (mLastTarget.mAccount != null) {
  364. OwnCloudAccount ocAccount = new OwnCloudAccount(mLastTarget.mAccount, mService);
  365. mOwnCloudClient = OwnCloudClientManagerFactory.getDefaultSingleton().
  366. getClientFor(ocAccount, mService);
  367. mStorageManager = new FileDataStorageManager(
  368. mLastTarget.mAccount,
  369. mService.getContentResolver()
  370. );
  371. } else {
  372. OwnCloudAccount ocAccount = new OwnCloudAccount(mLastTarget.mServerUrl, null);
  373. mOwnCloudClient = OwnCloudClientManagerFactory.getDefaultSingleton().
  374. getClientFor(ocAccount, mService);
  375. mStorageManager = null;
  376. }
  377. }
  378. /// perform the operation
  379. if (mCurrentOperation instanceof SyncOperation) {
  380. result = ((SyncOperation)mCurrentOperation).execute(mOwnCloudClient,
  381. mStorageManager);
  382. } else {
  383. result = mCurrentOperation.execute(mOwnCloudClient);
  384. }
  385. } catch (AccountsException e) {
  386. if (mLastTarget.mAccount == null) {
  387. Log_OC.e(TAG, "Error while trying to get authorization for a NULL account",
  388. e);
  389. } else {
  390. Log_OC.e(TAG, "Error while trying to get authorization for " +
  391. mLastTarget.mAccount.name, e);
  392. }
  393. result = new RemoteOperationResult(e);
  394. } catch (IOException e) {
  395. if (mLastTarget.mAccount == null) {
  396. Log_OC.e(TAG, "Error while trying to get authorization for a NULL account",
  397. e);
  398. } else {
  399. Log_OC.e(TAG, "Error while trying to get authorization for " +
  400. mLastTarget.mAccount.name, e);
  401. }
  402. result = new RemoteOperationResult(e);
  403. } catch (Exception e) {
  404. if (mLastTarget.mAccount == null) {
  405. Log_OC.e(TAG, "Unexpected error for a NULL account", e);
  406. } else {
  407. Log_OC.e(TAG, "Unexpected error for " + mLastTarget.mAccount.name, e);
  408. }
  409. result = new RemoteOperationResult(e);
  410. } finally {
  411. synchronized(mPendingOperations) {
  412. mPendingOperations.poll();
  413. }
  414. }
  415. //sendBroadcastOperationFinished(mLastTarget, mCurrentOperation, result);
  416. mService.dispatchResultToOperationListeners(mCurrentOperation, result);
  417. }
  418. }
  419. }
  420. /**
  421. * Creates a new operation, as described by operationIntent.
  422. *
  423. * TODO - move to ServiceHandler (probably)
  424. *
  425. * @param operationIntent Intent describing a new operation to queue and execute.
  426. * @return Pair with the new operation object and the information about its
  427. * target server.
  428. */
  429. private Pair<Target, RemoteOperation> newOperation(Intent operationIntent) {
  430. RemoteOperation operation = null;
  431. Target target = null;
  432. try {
  433. if (!operationIntent.hasExtra(EXTRA_ACCOUNT) &&
  434. !operationIntent.hasExtra(EXTRA_SERVER_URL)) {
  435. Log_OC.e(TAG, "Not enough information provided in intent");
  436. } else {
  437. Account account = operationIntent.getParcelableExtra(EXTRA_ACCOUNT);
  438. String serverUrl = operationIntent.getStringExtra(EXTRA_SERVER_URL);
  439. String cookie = operationIntent.getStringExtra(EXTRA_COOKIE);
  440. target = new Target(
  441. account,
  442. (serverUrl == null) ? null : Uri.parse(serverUrl),
  443. cookie
  444. );
  445. String action = operationIntent.getAction();
  446. String remotePath;
  447. String password;
  448. ShareType shareType;
  449. String newParentPath;
  450. long shareId;
  451. switch (action) {
  452. case ACTION_CREATE_SHARE_VIA_LINK:
  453. remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
  454. password = operationIntent.getStringExtra(EXTRA_SHARE_PASSWORD);
  455. if (!TextUtils.isEmpty(remotePath)) {
  456. operation = new CreateShareViaLinkOperation(remotePath, password);
  457. }
  458. break;
  459. case ACTION_UPDATE_SHARE:
  460. remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
  461. shareId = operationIntent.getLongExtra(EXTRA_SHARE_ID, -1);
  462. if (!TextUtils.isEmpty(remotePath)) {
  463. UpdateShareViaLinkOperation updateLinkOperation = new UpdateShareViaLinkOperation(remotePath);
  464. password = operationIntent.getStringExtra(EXTRA_SHARE_PASSWORD);
  465. updateLinkOperation.setPassword(password);
  466. long expirationDate = operationIntent.getLongExtra(EXTRA_SHARE_EXPIRATION_DATE_IN_MILLIS, 0);
  467. updateLinkOperation.setExpirationDateInMillis(expirationDate);
  468. boolean hideFileDownload = operationIntent.getBooleanExtra(EXTRA_SHARE_HIDE_FILE_DOWNLOAD,
  469. false);
  470. updateLinkOperation.setHideFileDownload(hideFileDownload);
  471. if (operationIntent.hasExtra(EXTRA_SHARE_PUBLIC_UPLOAD)) {
  472. if (remotePath.endsWith("/")) {
  473. updateLinkOperation.setPublicUploadOnFolder(
  474. operationIntent.getBooleanExtra(EXTRA_SHARE_PUBLIC_UPLOAD, false));
  475. } else {
  476. updateLinkOperation.setPublicUploadOnFile(
  477. operationIntent.getBooleanExtra(EXTRA_SHARE_PUBLIC_UPLOAD, false));
  478. }
  479. }
  480. operation = updateLinkOperation;
  481. } else if (shareId > 0) {
  482. UpdateSharePermissionsOperation updateShare = new UpdateSharePermissionsOperation(shareId);
  483. int permissions = operationIntent.getIntExtra(EXTRA_SHARE_PERMISSIONS, -1);
  484. updateShare.setPermissions(permissions);
  485. long expirationDateInMillis = operationIntent
  486. .getLongExtra(EXTRA_SHARE_EXPIRATION_DATE_IN_MILLIS, 0L);
  487. updateShare.setExpirationDateInMillis(expirationDateInMillis);
  488. password = operationIntent.getStringExtra(EXTRA_SHARE_PASSWORD);
  489. updateShare.setPassword(password);
  490. operation = updateShare;
  491. }
  492. break;
  493. case ACTION_UPDATE_SHARE_NOTE:
  494. shareId = operationIntent.getLongExtra(EXTRA_SHARE_ID, -1);
  495. String note = operationIntent.getStringExtra(EXTRA_SHARE_NOTE);
  496. if (shareId > 0) {
  497. operation = new UpdateNoteForShareOperation(shareId, note);
  498. }
  499. break;
  500. case ACTION_CREATE_SHARE_WITH_SHAREE:
  501. remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
  502. String shareeName = operationIntent.getStringExtra(EXTRA_SHARE_WITH);
  503. shareType = (ShareType) operationIntent.getSerializableExtra(EXTRA_SHARE_TYPE);
  504. int permissions = operationIntent.getIntExtra(EXTRA_SHARE_PERMISSIONS, -1);
  505. if (!TextUtils.isEmpty(remotePath)) {
  506. operation = new CreateShareWithShareeOperation(remotePath, shareeName, shareType,
  507. permissions);
  508. }
  509. break;
  510. case ACTION_UNSHARE:
  511. remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
  512. shareType = (ShareType) operationIntent.getSerializableExtra(EXTRA_SHARE_TYPE);
  513. String shareWith = operationIntent.getStringExtra(EXTRA_SHARE_WITH);
  514. if (!TextUtils.isEmpty(remotePath)) {
  515. operation = new UnshareOperation(remotePath, shareType, shareWith, this);
  516. }
  517. break;
  518. case ACTION_GET_SERVER_INFO:
  519. operation = new GetServerInfoOperation(serverUrl, this);
  520. break;
  521. case ACTION_GET_USER_NAME:
  522. operation = new GetUserInfoRemoteOperation();
  523. break;
  524. case ACTION_RENAME:
  525. remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
  526. String newName = operationIntent.getStringExtra(EXTRA_NEWNAME);
  527. operation = new RenameFileOperation(remotePath, newName);
  528. break;
  529. case ACTION_REMOVE:
  530. // Remove file or folder
  531. remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
  532. boolean onlyLocalCopy = operationIntent.getBooleanExtra(EXTRA_REMOVE_ONLY_LOCAL, false);
  533. boolean inBackground = operationIntent.getBooleanExtra(EXTRA_IN_BACKGROUND, false);
  534. operation = new RemoveFileOperation(remotePath, onlyLocalCopy, account, inBackground,
  535. getApplicationContext());
  536. break;
  537. case ACTION_CREATE_FOLDER:
  538. remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
  539. boolean createFullPath = operationIntent.getBooleanExtra(EXTRA_CREATE_FULL_PATH, true);
  540. operation = new CreateFolderOperation(remotePath, createFullPath);
  541. break;
  542. case ACTION_SYNC_FILE:
  543. remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
  544. boolean syncFileContents = operationIntent.getBooleanExtra(EXTRA_SYNC_FILE_CONTENTS, true);
  545. operation = new SynchronizeFileOperation(remotePath, account, syncFileContents,
  546. getApplicationContext());
  547. break;
  548. case ACTION_SYNC_FOLDER:
  549. remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
  550. operation = new SynchronizeFolderOperation(
  551. this, // TODO remove this dependency from construction time
  552. remotePath,
  553. account,
  554. System.currentTimeMillis() // TODO remove this dependency from construction time
  555. );
  556. break;
  557. case ACTION_MOVE_FILE:
  558. remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
  559. newParentPath = operationIntent.getStringExtra(EXTRA_NEW_PARENT_PATH);
  560. operation = new MoveFileOperation(remotePath, newParentPath);
  561. break;
  562. case ACTION_COPY_FILE:
  563. remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
  564. newParentPath = operationIntent.getStringExtra(EXTRA_NEW_PARENT_PATH);
  565. operation = new CopyFileOperation(remotePath, newParentPath);
  566. break;
  567. case ACTION_CHECK_CURRENT_CREDENTIALS:
  568. operation = new CheckCurrentCredentialsOperation(account);
  569. break;
  570. case ACTION_RESTORE_VERSION:
  571. FileVersion fileVersion = operationIntent.getParcelableExtra(EXTRA_FILE_VERSION);
  572. operation = new RestoreFileVersionRemoteOperation(fileVersion.getRemoteId(),
  573. fileVersion.getFileName());
  574. break;
  575. default:
  576. // do nothing
  577. break;
  578. }
  579. }
  580. } catch (IllegalArgumentException e) {
  581. Log_OC.e(TAG, "Bad information provided in intent: " + e.getMessage());
  582. operation = null;
  583. }
  584. if (operation != null) {
  585. return new Pair<Target, RemoteOperation>(target, operation);
  586. } else {
  587. return null;
  588. }
  589. }
  590. /**
  591. * Notifies the currently subscribed listeners about the end of an operation.
  592. *
  593. * @param operation Finished operation.
  594. * @param result Result of the operation.
  595. */
  596. protected void dispatchResultToOperationListeners(
  597. final RemoteOperation operation, final RemoteOperationResult result
  598. ) {
  599. int count = 0;
  600. Iterator<OnRemoteOperationListener> listeners = mOperationsBinder.mBoundListeners.keySet().iterator();
  601. while (listeners.hasNext()) {
  602. final OnRemoteOperationListener listener = listeners.next();
  603. final Handler handler = mOperationsBinder.mBoundListeners.get(listener);
  604. if (handler != null) {
  605. handler.post(() -> listener.onRemoteOperationFinish(operation, result));
  606. count += 1;
  607. }
  608. }
  609. if (count == 0) {
  610. Pair<RemoteOperation, RemoteOperationResult> undispatched = new Pair<>(operation, result);
  611. mUndispatchedFinishedOperations.put(operation.hashCode(), undispatched);
  612. }
  613. Log_OC.d(TAG, "Called " + count + " listeners");
  614. }
  615. }