FileDisplayActivity.java 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author Bartek Przybylski
  5. * @author David A. Velasco
  6. * Copyright (C) 2011 Bartek Przybylski
  7. * Copyright (C) 2016 ownCloud Inc.
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.ui.activity;
  22. import android.Manifest;
  23. import android.accounts.Account;
  24. import android.accounts.AuthenticatorException;
  25. import android.annotation.TargetApi;
  26. import android.content.BroadcastReceiver;
  27. import android.content.ComponentName;
  28. import android.content.ContentResolver;
  29. import android.content.Context;
  30. import android.content.DialogInterface;
  31. import android.content.Intent;
  32. import android.content.IntentFilter;
  33. import android.content.ServiceConnection;
  34. import android.content.SyncRequest;
  35. import android.content.pm.PackageManager;
  36. import android.content.res.Resources.NotFoundException;
  37. import android.os.Build;
  38. import android.os.Bundle;
  39. import android.os.IBinder;
  40. import android.os.Parcelable;
  41. import android.support.design.widget.Snackbar;
  42. import android.support.v4.app.Fragment;
  43. import android.support.v4.app.FragmentManager;
  44. import android.support.v4.app.FragmentTransaction;
  45. import android.support.v4.content.ContextCompat;
  46. import android.support.v7.app.AlertDialog;
  47. import android.view.Menu;
  48. import android.view.MenuInflater;
  49. import android.view.MenuItem;
  50. import android.view.View;
  51. import android.widget.Toast;
  52. import com.owncloud.android.MainApp;
  53. import com.owncloud.android.R;
  54. import com.owncloud.android.datamodel.FileDataStorageManager;
  55. import com.owncloud.android.datamodel.OCFile;
  56. import com.owncloud.android.files.services.FileDownloader;
  57. import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
  58. import com.owncloud.android.files.services.FileUploader;
  59. import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
  60. import com.owncloud.android.lib.common.operations.RemoteOperation;
  61. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  62. import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
  63. import com.owncloud.android.lib.common.utils.Log_OC;
  64. import com.owncloud.android.operations.CopyFileOperation;
  65. import com.owncloud.android.operations.CreateFolderOperation;
  66. import com.owncloud.android.operations.MoveFileOperation;
  67. import com.owncloud.android.operations.RefreshFolderOperation;
  68. import com.owncloud.android.operations.RemoveFileOperation;
  69. import com.owncloud.android.operations.RenameFileOperation;
  70. import com.owncloud.android.operations.SynchronizeFileOperation;
  71. import com.owncloud.android.operations.UploadFileOperation;
  72. import com.owncloud.android.services.observer.FileObserverService;
  73. import com.owncloud.android.syncadapter.FileSyncAdapter;
  74. import com.owncloud.android.ui.fragment.FileDetailFragment;
  75. import com.owncloud.android.ui.fragment.FileFragment;
  76. import com.owncloud.android.ui.fragment.OCFileListFragment;
  77. import com.owncloud.android.ui.fragment.TaskRetainerFragment;
  78. import com.owncloud.android.ui.helpers.UriUploader;
  79. import com.owncloud.android.ui.preview.PreviewImageActivity;
  80. import com.owncloud.android.ui.preview.PreviewImageFragment;
  81. import com.owncloud.android.ui.preview.PreviewMediaFragment;
  82. import com.owncloud.android.ui.preview.PreviewTextFragment;
  83. import com.owncloud.android.ui.preview.PreviewVideoActivity;
  84. import com.owncloud.android.utils.DisplayUtils;
  85. import com.owncloud.android.utils.ErrorMessageAdapter;
  86. import com.owncloud.android.utils.PermissionUtil;
  87. import java.io.File;
  88. import java.util.ArrayList;
  89. import java.util.Collection;
  90. import static com.owncloud.android.db.PreferenceManager.getSortOrder;
  91. /**
  92. * Displays, what files the user has available in his ownCloud. This is the main view.
  93. */
  94. public class FileDisplayActivity extends HookActivity
  95. implements FileFragment.ContainerActivity,
  96. OnEnforceableRefreshListener {
  97. private SyncBroadcastReceiver mSyncBroadcastReceiver;
  98. private UploadFinishReceiver mUploadFinishReceiver;
  99. private DownloadFinishReceiver mDownloadFinishReceiver;
  100. private RemoteOperationResult mLastSslUntrustedServerResult = null;
  101. private boolean mDualPane;
  102. private View mLeftFragmentContainer;
  103. private View mRightFragmentContainer;
  104. private static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";
  105. private static final String KEY_SYNC_IN_PROGRESS = "SYNC_IN_PROGRESS";
  106. private static final String KEY_WAITING_TO_SEND = "WAITING_TO_SEND";
  107. public static final String ACTION_DETAILS = "com.owncloud.android.ui.activity.action.DETAILS";
  108. public static final int REQUEST_CODE__SELECT_CONTENT_FROM_APPS = REQUEST_CODE__LAST_SHARED + 1;
  109. public static final int REQUEST_CODE__SELECT_FILES_FROM_FILE_SYSTEM = REQUEST_CODE__LAST_SHARED + 2;
  110. public static final int REQUEST_CODE__MOVE_FILES = REQUEST_CODE__LAST_SHARED + 3;
  111. public static final int REQUEST_CODE__COPY_FILES = REQUEST_CODE__LAST_SHARED + 4;
  112. private static final String TAG = FileDisplayActivity.class.getSimpleName();
  113. private static final String TAG_LIST_OF_FILES = "LIST_OF_FILES";
  114. private static final String TAG_SECOND_FRAGMENT = "SECOND_FRAGMENT";
  115. private OCFile mWaitingToPreview;
  116. private boolean mSyncInProgress = false;
  117. private OCFile mWaitingToSend;
  118. @Override
  119. protected void onCreate(Bundle savedInstanceState) {
  120. Log_OC.v(TAG, "onCreate() start");
  121. super.onCreate(savedInstanceState); // this calls onAccountChanged() when ownCloud Account
  122. // is valid
  123. /// grant that FileObserverService is watching favorite files
  124. if (savedInstanceState == null) {
  125. Intent initObserversIntent = FileObserverService.makeInitIntent(this);
  126. startService(initObserversIntent);
  127. }
  128. /// Load of saved instance state
  129. if (savedInstanceState != null) {
  130. mWaitingToPreview = (OCFile) savedInstanceState.getParcelable(
  131. FileDisplayActivity.KEY_WAITING_TO_PREVIEW);
  132. mSyncInProgress = savedInstanceState.getBoolean(KEY_SYNC_IN_PROGRESS);
  133. mWaitingToSend = (OCFile) savedInstanceState.getParcelable(
  134. FileDisplayActivity.KEY_WAITING_TO_SEND);
  135. } else {
  136. mWaitingToPreview = null;
  137. mSyncInProgress = false;
  138. mWaitingToSend = null;
  139. }
  140. /// USER INTERFACE
  141. // Inflate and set the layout view
  142. setContentView(R.layout.files);
  143. // setup toolbar
  144. setupToolbar();
  145. // setup drawer
  146. setupDrawer(R.id.nav_all_files);
  147. mDualPane = getResources().getBoolean(R.bool.large_land_layout);
  148. mLeftFragmentContainer = findViewById(R.id.left_fragment_container);
  149. mRightFragmentContainer = findViewById(R.id.right_fragment_container);
  150. // Action bar setup
  151. getSupportActionBar().setHomeButtonEnabled(true);
  152. // Init Fragment without UI to retain AsyncTask across configuration changes
  153. FragmentManager fm = getSupportFragmentManager();
  154. TaskRetainerFragment taskRetainerFragment =
  155. (TaskRetainerFragment) fm.findFragmentByTag(TaskRetainerFragment.FTAG_TASK_RETAINER_FRAGMENT);
  156. if (taskRetainerFragment == null) {
  157. taskRetainerFragment = new TaskRetainerFragment();
  158. fm.beginTransaction()
  159. .add(taskRetainerFragment, TaskRetainerFragment.FTAG_TASK_RETAINER_FRAGMENT).commit();
  160. } // else, Fragment already created and retained across configuration change
  161. Log_OC.v(TAG, "onCreate() end");
  162. }
  163. @Override
  164. protected void onPostCreate(Bundle savedInstanceState) {
  165. super.onPostCreate(savedInstanceState);
  166. if (PermissionUtil.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
  167. // Check if we should show an explanation
  168. if (PermissionUtil.shouldShowRequestPermissionRationale(this,
  169. Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
  170. // Show explanation to the user and then request permission
  171. Snackbar snackbar = Snackbar.make(findViewById(R.id.ListLayout), R.string.permission_storage_access,
  172. Snackbar.LENGTH_INDEFINITE)
  173. .setAction(R.string.common_ok, new View.OnClickListener() {
  174. @Override
  175. public void onClick(View v) {
  176. PermissionUtil.requestWriteExternalStoreagePermission(FileDisplayActivity.this);
  177. }
  178. });
  179. DisplayUtils.colorSnackbar(this, snackbar);
  180. snackbar.show();
  181. } else {
  182. // No explanation needed, request the permission.
  183. PermissionUtil.requestWriteExternalStoreagePermission(this);
  184. }
  185. }
  186. if (savedInstanceState == null) {
  187. createMinFragments();
  188. }
  189. refreshList(true);
  190. setIndeterminate(mSyncInProgress);
  191. // always AFTER setContentView(...) in onCreate(); to work around bug in its implementation
  192. setBackgroundText();
  193. }
  194. @Override
  195. public void onRequestPermissionsResult(int requestCode,
  196. String permissions[], int[] grantResults) {
  197. switch (requestCode) {
  198. case PermissionUtil.PERMISSIONS_WRITE_EXTERNAL_STORAGE: {
  199. // If request is cancelled, result arrays are empty.
  200. if (grantResults.length > 0
  201. && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  202. // permission was granted
  203. startSynchronization();
  204. // toggle on is save since this is the only scenario this code gets accessed
  205. } else {
  206. // permission denied --> do nothing
  207. }
  208. return;
  209. }
  210. }
  211. }
  212. @Override
  213. protected void onStart() {
  214. Log_OC.v(TAG, "onStart() start");
  215. super.onStart();
  216. Log_OC.v(TAG, "onStart() end");
  217. }
  218. @Override
  219. protected void onStop() {
  220. Log_OC.v(TAG, "onStop() start");
  221. super.onStop();
  222. Log_OC.v(TAG, "onStop() end");
  223. }
  224. @Override
  225. protected void onDestroy() {
  226. Log_OC.v(TAG, "onDestroy() start");
  227. super.onDestroy();
  228. Log_OC.v(TAG, "onDestroy() end");
  229. }
  230. /**
  231. * Called when the ownCloud {@link Account} associated to the Activity was just updated.
  232. */
  233. @Override
  234. protected void onAccountSet(boolean stateWasRecovered) {
  235. super.onAccountSet(stateWasRecovered);
  236. if (getAccount() != null) {
  237. /// Check whether the 'main' OCFile handled by the Activity is contained in the
  238. // current Account
  239. OCFile file = getFile();
  240. // get parent from path
  241. String parentPath = "";
  242. if (file != null) {
  243. if (file.isDown() && file.getLastSyncDateForProperties() == 0) {
  244. // upload in progress - right now, files are not inserted in the local
  245. // cache until the upload is successful get parent from path
  246. parentPath = file.getRemotePath().substring(0,
  247. file.getRemotePath().lastIndexOf(file.getFileName()));
  248. if (getStorageManager().getFileByPath(parentPath) == null)
  249. file = null; // not able to know the directory where the file is uploading
  250. } else {
  251. file = getStorageManager().getFileByPath(file.getRemotePath());
  252. // currentDir = null if not in the current Account
  253. }
  254. }
  255. if (file == null) {
  256. // fall back to root folder
  257. file = getStorageManager().getFileByPath(OCFile.ROOT_PATH); // never returns null
  258. }
  259. setFile(file);
  260. if (mAccountWasSet) {
  261. setAccountInDrawer(getAccount());
  262. }
  263. if (!stateWasRecovered) {
  264. Log_OC.d(TAG, "Initializing Fragments in onAccountChanged..");
  265. initFragmentsWithFile();
  266. if (file.isFolder()) {
  267. startSyncFolderOperation(file, false);
  268. }
  269. } else {
  270. updateFragmentsVisibility(!file.isFolder());
  271. updateActionBarTitleAndHomeButton(file.isFolder() ? null : file);
  272. }
  273. }
  274. }
  275. private void createMinFragments() {
  276. OCFileListFragment listOfFiles = new OCFileListFragment();
  277. Bundle args = new Bundle();
  278. args.putBoolean(OCFileListFragment.ARG_ALLOW_CONTEXTUAL_ACTIONS, true);
  279. listOfFiles.setArguments(args);
  280. FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  281. transaction.add(R.id.left_fragment_container, listOfFiles, TAG_LIST_OF_FILES);
  282. transaction.commit();
  283. }
  284. private void initFragmentsWithFile() {
  285. if (getAccount() != null && getFile() != null) {
  286. /// First fragment
  287. OCFileListFragment listOfFiles = getListOfFilesFragment();
  288. if (listOfFiles != null) {
  289. listOfFiles.listDirectory(getCurrentDir(), MainApp.getOnlyOnDevice());
  290. } else {
  291. Log_OC.e(TAG, "Still have a chance to lose the initializacion of list fragment >(");
  292. }
  293. /// Second fragment
  294. OCFile file = getFile();
  295. Fragment secondFragment = chooseInitialSecondFragment(file);
  296. if (secondFragment != null) {
  297. setSecondFragment(secondFragment);
  298. updateFragmentsVisibility(true);
  299. updateActionBarTitleAndHomeButton(file);
  300. } else {
  301. cleanSecondFragment();
  302. if (file.isDown() && PreviewTextFragment.canBePreviewed(file))
  303. startTextPreview(file);
  304. }
  305. } else {
  306. Log_OC.wtf(TAG, "initFragments() called with invalid NULLs!");
  307. if (getAccount() == null) {
  308. Log_OC.wtf(TAG, "\t account is NULL");
  309. }
  310. if (getFile() == null) {
  311. Log_OC.wtf(TAG, "\t file is NULL");
  312. }
  313. }
  314. }
  315. private Fragment chooseInitialSecondFragment(OCFile file) {
  316. Fragment secondFragment = null;
  317. if (file != null && !file.isFolder()) {
  318. if (file.isDown() && PreviewMediaFragment.canBePreviewed(file)
  319. && file.getLastSyncDateForProperties() > 0 // temporal fix
  320. ) {
  321. int startPlaybackPosition =
  322. getIntent().getIntExtra(PreviewVideoActivity.EXTRA_START_POSITION, 0);
  323. boolean autoplay =
  324. getIntent().getBooleanExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, true);
  325. secondFragment = new PreviewMediaFragment(file, getAccount(),
  326. startPlaybackPosition, autoplay);
  327. } else if (file.isDown() && PreviewTextFragment.canBePreviewed(file)) {
  328. secondFragment = null;
  329. } else {
  330. secondFragment = FileDetailFragment.newInstance(file, getAccount());
  331. }
  332. }
  333. return secondFragment;
  334. }
  335. /**
  336. * Replaces the second fragment managed by the activity with the received as
  337. * a parameter.
  338. * <p/>
  339. * Assumes never will be more than two fragments managed at the same time.
  340. *
  341. * @param fragment New second Fragment to set.
  342. */
  343. private void setSecondFragment(Fragment fragment) {
  344. FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  345. transaction.replace(R.id.right_fragment_container, fragment, TAG_SECOND_FRAGMENT);
  346. transaction.commit();
  347. }
  348. private void updateFragmentsVisibility(boolean existsSecondFragment) {
  349. if (mDualPane) {
  350. if (mLeftFragmentContainer.getVisibility() != View.VISIBLE) {
  351. mLeftFragmentContainer.setVisibility(View.VISIBLE);
  352. }
  353. if (mRightFragmentContainer.getVisibility() != View.VISIBLE) {
  354. mRightFragmentContainer.setVisibility(View.VISIBLE);
  355. }
  356. } else if (existsSecondFragment) {
  357. if (mLeftFragmentContainer.getVisibility() != View.GONE) {
  358. mLeftFragmentContainer.setVisibility(View.GONE);
  359. }
  360. if (mRightFragmentContainer.getVisibility() != View.VISIBLE) {
  361. mRightFragmentContainer.setVisibility(View.VISIBLE);
  362. }
  363. } else {
  364. if (mLeftFragmentContainer.getVisibility() != View.VISIBLE) {
  365. mLeftFragmentContainer.setVisibility(View.VISIBLE);
  366. }
  367. if (mRightFragmentContainer.getVisibility() != View.GONE) {
  368. mRightFragmentContainer.setVisibility(View.GONE);
  369. }
  370. }
  371. }
  372. private OCFileListFragment getListOfFilesFragment() {
  373. Fragment listOfFiles = getSupportFragmentManager().findFragmentByTag(
  374. FileDisplayActivity.TAG_LIST_OF_FILES);
  375. if (listOfFiles != null) {
  376. return (OCFileListFragment) listOfFiles;
  377. }
  378. Log_OC.wtf(TAG, "Access to unexisting list of files fragment!!");
  379. return null;
  380. }
  381. public FileFragment getSecondFragment() {
  382. Fragment second = getSupportFragmentManager().findFragmentByTag(
  383. FileDisplayActivity.TAG_SECOND_FRAGMENT);
  384. if (second != null) {
  385. return (FileFragment) second;
  386. }
  387. return null;
  388. }
  389. protected void cleanSecondFragment() {
  390. Fragment second = getSecondFragment();
  391. if (second != null) {
  392. FragmentTransaction tr = getSupportFragmentManager().beginTransaction();
  393. tr.remove(second);
  394. tr.commit();
  395. }
  396. updateFragmentsVisibility(false);
  397. updateActionBarTitleAndHomeButton(null);
  398. }
  399. protected void refreshListOfFilesFragment() {
  400. OCFileListFragment fileListFragment = getListOfFilesFragment();
  401. if (fileListFragment != null) {
  402. fileListFragment.listDirectory(MainApp.getOnlyOnDevice());
  403. }
  404. }
  405. protected void refreshSecondFragment(String downloadEvent, String downloadedRemotePath,
  406. boolean success) {
  407. FileFragment secondFragment = getSecondFragment();
  408. boolean waitedPreview = (mWaitingToPreview != null &&
  409. mWaitingToPreview.getRemotePath().equals(downloadedRemotePath));
  410. if (secondFragment != null && secondFragment instanceof FileDetailFragment) {
  411. FileDetailFragment detailsFragment = (FileDetailFragment) secondFragment;
  412. OCFile fileInFragment = detailsFragment.getFile();
  413. if (fileInFragment != null &&
  414. !downloadedRemotePath.equals(fileInFragment.getRemotePath())) {
  415. // the user browsed to other file ; forget the automatic preview
  416. mWaitingToPreview = null;
  417. } else if (downloadEvent.equals(FileDownloader.getDownloadAddedMessage())) {
  418. // grant that the right panel updates the progress bar
  419. detailsFragment.listenForTransferProgress();
  420. detailsFragment.updateFileDetails(true, false);
  421. } else if (downloadEvent.equals(FileDownloader.getDownloadFinishMessage())) {
  422. // update the right panel
  423. boolean detailsFragmentChanged = false;
  424. if (waitedPreview) {
  425. if (success) {
  426. mWaitingToPreview = getStorageManager().getFileById(
  427. mWaitingToPreview.getFileId()); // update the file from database,
  428. // for the local storage path
  429. if (PreviewMediaFragment.canBePreviewed(mWaitingToPreview)) {
  430. startMediaPreview(mWaitingToPreview, 0, true);
  431. detailsFragmentChanged = true;
  432. } else if (PreviewTextFragment.canBePreviewed(mWaitingToPreview)) {
  433. startTextPreview(mWaitingToPreview);
  434. detailsFragmentChanged = true;
  435. } else {
  436. getFileOperationsHelper().openFile(mWaitingToPreview);
  437. }
  438. }
  439. mWaitingToPreview = null;
  440. }
  441. if (!detailsFragmentChanged) {
  442. detailsFragment.updateFileDetails(false, (success));
  443. }
  444. }
  445. }
  446. }
  447. @Override
  448. public boolean onPrepareOptionsMenu(Menu menu) {
  449. boolean drawerOpen = isDrawerOpen();
  450. menu.findItem(R.id.action_sort).setVisible(!drawerOpen);
  451. menu.findItem(R.id.action_sync_account).setVisible(!drawerOpen);
  452. menu.findItem(R.id.action_switch_view).setVisible(!drawerOpen);
  453. return super.onPrepareOptionsMenu(menu);
  454. }
  455. @Override
  456. public boolean onCreateOptionsMenu(Menu menu) {
  457. MenuInflater inflater = getMenuInflater();
  458. inflater.inflate(R.menu.main_menu, menu);
  459. menu.findItem(R.id.action_create_dir).setVisible(false);
  460. return true;
  461. }
  462. @Override
  463. public boolean onOptionsItemSelected(MenuItem item) {
  464. boolean retval = true;
  465. switch (item.getItemId()) {
  466. case R.id.action_sync_account: {
  467. startSynchronization();
  468. break;
  469. }
  470. case android.R.id.home: {
  471. FileFragment second = getSecondFragment();
  472. OCFile currentDir = getCurrentDir();
  473. if (isDrawerOpen()) {
  474. closeDrawer();
  475. } else if ((currentDir != null && currentDir.getParentId() != 0) ||
  476. (second != null && second.getFile() != null)) {
  477. onBackPressed();
  478. } else {
  479. openDrawer();
  480. }
  481. break;
  482. }
  483. case R.id.action_sort: {
  484. Integer sortOrder = getSortOrder(this);
  485. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  486. builder.setTitle(R.string.actionbar_sort_title)
  487. .setSingleChoiceItems(R.array.actionbar_sortby, sortOrder,
  488. new DialogInterface.OnClickListener() {
  489. public void onClick(DialogInterface dialog, int which) {
  490. switch (which) {
  491. case 0:
  492. sortByName(true);
  493. break;
  494. case 1:
  495. sortByDate(false);
  496. break;
  497. case 2:
  498. sortBySize(false);
  499. }
  500. dialog.dismiss();
  501. }
  502. });
  503. builder.create().show();
  504. break;
  505. }
  506. case R.id.action_switch_view: {
  507. if (isGridView()) {
  508. item.setTitle(getString(R.string.action_switch_grid_view));
  509. item.setIcon(ContextCompat.getDrawable(getApplicationContext(),
  510. R.drawable.ic_view_module));
  511. getListOfFilesFragment().setListAsPreferred();
  512. } else {
  513. item.setTitle(getApplicationContext().getString(R.string.action_switch_list_view));
  514. item.setIcon(ContextCompat.getDrawable(getApplicationContext(),
  515. R.drawable.ic_view_list));
  516. getListOfFilesFragment().setGridAsPreferred();
  517. }
  518. return true;
  519. }
  520. default:
  521. retval = super.onOptionsItemSelected(item);
  522. }
  523. return retval;
  524. }
  525. private void startSynchronization() {
  526. Log_OC.d(TAG, "Got to start sync");
  527. if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
  528. Log_OC.d(TAG, "Canceling all syncs for " + MainApp.getAuthority());
  529. ContentResolver.cancelSync(null, MainApp.getAuthority());
  530. // cancel the current synchronizations of any ownCloud account
  531. Bundle bundle = new Bundle();
  532. bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
  533. bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
  534. Log_OC.d(TAG, "Requesting sync for " + getAccount().name + " at " +
  535. MainApp.getAuthority());
  536. ContentResolver.requestSync(
  537. getAccount(),
  538. MainApp.getAuthority(), bundle);
  539. } else {
  540. Log_OC.d(TAG, "Requesting sync for " + getAccount().name + " at " +
  541. MainApp.getAuthority() + " with new API");
  542. SyncRequest.Builder builder = new SyncRequest.Builder();
  543. builder.setSyncAdapter(getAccount(), MainApp.getAuthority());
  544. builder.setExpedited(true);
  545. builder.setManual(true);
  546. builder.syncOnce();
  547. // Fix bug in Android Lollipop when you click on refresh the whole account
  548. Bundle extras = new Bundle();
  549. builder.setExtras(extras);
  550. SyncRequest request = builder.build();
  551. ContentResolver.requestSync(request);
  552. }
  553. }
  554. /**
  555. * Called, when the user selected something for uploading
  556. */
  557. @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
  558. @Override
  559. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  560. if (requestCode == REQUEST_CODE__SELECT_CONTENT_FROM_APPS &&
  561. (resultCode == RESULT_OK || resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)) {
  562. requestUploadOfContentFromApps(data, resultCode);
  563. } else if (requestCode == REQUEST_CODE__SELECT_FILES_FROM_FILE_SYSTEM &&
  564. (resultCode == RESULT_OK || resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)) {
  565. requestUploadOfFilesFromFileSystem(data, resultCode);
  566. } else if (requestCode == REQUEST_CODE__MOVE_FILES && resultCode == RESULT_OK) {
  567. final Intent fData = data;
  568. getHandler().postDelayed(
  569. new Runnable() {
  570. @Override
  571. public void run() {
  572. requestMoveOperation(fData);
  573. }
  574. },
  575. DELAY_TO_REQUEST_OPERATIONS_LATER
  576. );
  577. } else if (requestCode == REQUEST_CODE__COPY_FILES && resultCode == RESULT_OK) {
  578. final Intent fData = data;
  579. final int fResultCode = resultCode;
  580. getHandler().postDelayed(
  581. new Runnable() {
  582. @Override
  583. public void run() {
  584. requestCopyOperation(fData);
  585. }
  586. },
  587. DELAY_TO_REQUEST_OPERATIONS_LATER
  588. );
  589. } else {
  590. super.onActivityResult(requestCode, resultCode, data);
  591. }
  592. }
  593. private void requestUploadOfFilesFromFileSystem(Intent data, int resultCode) {
  594. String[] filePaths = data.getStringArrayExtra(UploadFilesActivity.EXTRA_CHOSEN_FILES);
  595. if (filePaths != null) {
  596. String[] remotePaths = new String[filePaths.length];
  597. String remotePathBase = getCurrentDir().getRemotePath();
  598. for (int j = 0; j < remotePaths.length; j++) {
  599. remotePaths[j] = remotePathBase + (new File(filePaths[j])).getName();
  600. }
  601. int behaviour = (resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE) ? FileUploader
  602. .LOCAL_BEHAVIOUR_MOVE : FileUploader.LOCAL_BEHAVIOUR_COPY;
  603. FileUploader.UploadRequester requester = new FileUploader.UploadRequester();
  604. requester.uploadNewFile(
  605. this,
  606. getAccount(),
  607. filePaths,
  608. remotePaths,
  609. null, // MIME type will be detected from file name
  610. behaviour,
  611. false, // do not create parent folder if not existent
  612. UploadFileOperation.CREATED_BY_USER
  613. );
  614. } else {
  615. Log_OC.d(TAG, "User clicked on 'Update' with no selection");
  616. Toast t = Toast.makeText(this, getString(R.string.filedisplay_no_file_selected),
  617. Toast.LENGTH_LONG);
  618. t.show();
  619. return;
  620. }
  621. }
  622. private void requestUploadOfContentFromApps(Intent contentIntent, int resultCode) {
  623. ArrayList<Parcelable> streamsToUpload = new ArrayList<>();
  624. //getClipData is only supported on api level 16+, Jelly Bean
  625. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN &&
  626. contentIntent.getClipData() != null &&
  627. contentIntent.getClipData().getItemCount() > 0) {
  628. for (int i = 0; i < contentIntent.getClipData().getItemCount(); i++) {
  629. streamsToUpload.add(contentIntent.getClipData().getItemAt(i).getUri());
  630. }
  631. } else {
  632. streamsToUpload.add(contentIntent.getData());
  633. }
  634. int behaviour = (resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE) ? FileUploader.LOCAL_BEHAVIOUR_MOVE :
  635. FileUploader.LOCAL_BEHAVIOUR_COPY;
  636. OCFile currentDir = getCurrentDir();
  637. String remotePath = (currentDir != null) ? currentDir.getRemotePath() : OCFile.ROOT_PATH;
  638. UriUploader uploader = new UriUploader(
  639. this,
  640. streamsToUpload,
  641. remotePath,
  642. getAccount(),
  643. behaviour,
  644. false, // Not show waiting dialog while file is being copied from private storage
  645. null // Not needed copy temp task listener
  646. );
  647. uploader.uploadUris();
  648. }
  649. /**
  650. * Request the operation for moving the file/folder from one path to another
  651. *
  652. * @param data Intent received
  653. */
  654. private void requestMoveOperation(Intent data) {
  655. OCFile folderToMoveAt = data.getParcelableExtra(FolderPickerActivity.EXTRA_FOLDER);
  656. ArrayList<OCFile> files = data.getParcelableArrayListExtra(FolderPickerActivity.EXTRA_FILES);
  657. getFileOperationsHelper().moveFiles(files, folderToMoveAt);
  658. }
  659. /**
  660. * Request the operation for copying the file/folder from one path to another
  661. *
  662. * @param data Intent received
  663. */
  664. private void requestCopyOperation(Intent data) {
  665. OCFile folderToMoveAt = data.getParcelableExtra(FolderPickerActivity.EXTRA_FOLDER);
  666. ArrayList<OCFile> files = data.getParcelableArrayListExtra(FolderPickerActivity.EXTRA_FILES);
  667. getFileOperationsHelper().copyFiles(files, folderToMoveAt);
  668. }
  669. @Override
  670. public void onBackPressed() {
  671. boolean isFabOpen = isFabOpen();
  672. boolean isDrawerOpen = isDrawerOpen();
  673. /*
  674. * BackPressed priority/hierarchy:
  675. * 1. close drawer if opened
  676. * 2. close FAB if open (only if drawer isn't open)
  677. * 3. navigate up (only if drawer and FAB aren't open)
  678. */
  679. if(isDrawerOpen && isFabOpen) {
  680. // close drawer first
  681. super.onBackPressed();
  682. } else if(isDrawerOpen && !isFabOpen) {
  683. // close drawer
  684. super.onBackPressed();
  685. } else if (!isDrawerOpen && isFabOpen) {
  686. // close fab
  687. getListOfFilesFragment().getFabMain().collapse();
  688. } else {
  689. // all closed
  690. OCFileListFragment listOfFiles = getListOfFilesFragment();
  691. if (mDualPane || getSecondFragment() == null) {
  692. OCFile currentDir = getCurrentDir();
  693. if (currentDir == null || currentDir.getParentId() == FileDataStorageManager.ROOT_PARENT_ID) {
  694. finish();
  695. return;
  696. }
  697. if (listOfFiles != null) { // should never be null, indeed
  698. listOfFiles.onBrowseUp();
  699. }
  700. }
  701. if (listOfFiles != null) { // should never be null, indeed
  702. setFile(listOfFiles.getCurrentFile());
  703. }
  704. cleanSecondFragment();
  705. }
  706. }
  707. @Override
  708. protected void onSaveInstanceState(Bundle outState) {
  709. // responsibility of restore is preferred in onCreate() before than in
  710. // onRestoreInstanceState when there are Fragments involved
  711. Log_OC.v(TAG, "onSaveInstanceState() start");
  712. super.onSaveInstanceState(outState);
  713. outState.putParcelable(FileDisplayActivity.KEY_WAITING_TO_PREVIEW, mWaitingToPreview);
  714. outState.putBoolean(FileDisplayActivity.KEY_SYNC_IN_PROGRESS, mSyncInProgress);
  715. //outState.putBoolean(FileDisplayActivity.KEY_REFRESH_SHARES_IN_PROGRESS,
  716. // mRefreshSharesInProgress);
  717. outState.putParcelable(FileDisplayActivity.KEY_WAITING_TO_SEND, mWaitingToSend);
  718. Log_OC.v(TAG, "onSaveInstanceState() end");
  719. }
  720. @Override
  721. protected void onResume() {
  722. Log_OC.v(TAG, "onResume() start");
  723. super.onResume();
  724. // refresh list of files
  725. refreshListOfFilesFragment();
  726. // Listen for sync messages
  727. IntentFilter syncIntentFilter = new IntentFilter(FileSyncAdapter.EVENT_FULL_SYNC_START);
  728. syncIntentFilter.addAction(FileSyncAdapter.EVENT_FULL_SYNC_END);
  729. syncIntentFilter.addAction(FileSyncAdapter.EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED);
  730. syncIntentFilter.addAction(RefreshFolderOperation.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED);
  731. syncIntentFilter.addAction(RefreshFolderOperation.EVENT_SINGLE_FOLDER_SHARES_SYNCED);
  732. mSyncBroadcastReceiver = new SyncBroadcastReceiver();
  733. registerReceiver(mSyncBroadcastReceiver, syncIntentFilter);
  734. //LocalBroadcastManager.getInstance(this).registerReceiver(mSyncBroadcastReceiver,
  735. // syncIntentFilter);
  736. // Listen for upload messages
  737. IntentFilter uploadIntentFilter = new IntentFilter(FileUploader.getUploadFinishMessage());
  738. mUploadFinishReceiver = new UploadFinishReceiver();
  739. registerReceiver(mUploadFinishReceiver, uploadIntentFilter);
  740. // Listen for download messages
  741. IntentFilter downloadIntentFilter = new IntentFilter(
  742. FileDownloader.getDownloadAddedMessage());
  743. downloadIntentFilter.addAction(FileDownloader.getDownloadFinishMessage());
  744. mDownloadFinishReceiver = new DownloadFinishReceiver();
  745. registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);
  746. Log_OC.v(TAG, "onResume() end");
  747. }
  748. @Override
  749. protected void onPause() {
  750. Log_OC.v(TAG, "onPause() start");
  751. if (mSyncBroadcastReceiver != null) {
  752. unregisterReceiver(mSyncBroadcastReceiver);
  753. //LocalBroadcastManager.getInstance(this).unregisterReceiver(mSyncBroadcastReceiver);
  754. mSyncBroadcastReceiver = null;
  755. }
  756. if (mUploadFinishReceiver != null) {
  757. unregisterReceiver(mUploadFinishReceiver);
  758. mUploadFinishReceiver = null;
  759. }
  760. if (mDownloadFinishReceiver != null) {
  761. unregisterReceiver(mDownloadFinishReceiver);
  762. mDownloadFinishReceiver = null;
  763. }
  764. super.onPause();
  765. Log_OC.v(TAG, "onPause() end");
  766. }
  767. public boolean isFabOpen() {
  768. if(getListOfFilesFragment() != null
  769. && getListOfFilesFragment().getFabMain() != null
  770. && getListOfFilesFragment().getFabMain().isExpanded()) {
  771. return true;
  772. } else {
  773. return false;
  774. }
  775. }
  776. private class SyncBroadcastReceiver extends BroadcastReceiver {
  777. /**
  778. * {@link BroadcastReceiver} to enable syncing feedback in UI
  779. */
  780. @Override
  781. public void onReceive(Context context, Intent intent) {
  782. try {
  783. String event = intent.getAction();
  784. Log_OC.d(TAG, "Received broadcast " + event);
  785. String accountName = intent.getStringExtra(FileSyncAdapter.EXTRA_ACCOUNT_NAME);
  786. String synchFolderRemotePath =
  787. intent.getStringExtra(FileSyncAdapter.EXTRA_FOLDER_PATH);
  788. RemoteOperationResult synchResult =
  789. (RemoteOperationResult) intent.getSerializableExtra(
  790. FileSyncAdapter.EXTRA_RESULT);
  791. boolean sameAccount = (getAccount() != null &&
  792. accountName.equals(getAccount().name) && getStorageManager() != null);
  793. if (sameAccount) {
  794. if (FileSyncAdapter.EVENT_FULL_SYNC_START.equals(event)) {
  795. mSyncInProgress = true;
  796. } else {
  797. OCFile currentFile = (getFile() == null) ? null :
  798. getStorageManager().getFileByPath(getFile().getRemotePath());
  799. OCFile currentDir = (getCurrentDir() == null) ? null :
  800. getStorageManager().getFileByPath(getCurrentDir().getRemotePath());
  801. if (currentDir == null) {
  802. // current folder was removed from the server
  803. Toast.makeText(FileDisplayActivity.this,
  804. String.format(
  805. getString(R.string.
  806. sync_current_folder_was_removed),
  807. synchFolderRemotePath),
  808. Toast.LENGTH_LONG)
  809. .show();
  810. browseToRoot();
  811. } else {
  812. if (currentFile == null && !getFile().isFolder()) {
  813. // currently selected file was removed in the server, and now we
  814. // know it
  815. cleanSecondFragment();
  816. currentFile = currentDir;
  817. }
  818. if (synchFolderRemotePath != null &&
  819. currentDir.getRemotePath().equals(synchFolderRemotePath)) {
  820. OCFileListFragment fileListFragment = getListOfFilesFragment();
  821. if (fileListFragment != null) {
  822. fileListFragment.listDirectory(currentDir,
  823. MainApp.getOnlyOnDevice());
  824. }
  825. }
  826. setFile(currentFile);
  827. }
  828. mSyncInProgress = (!FileSyncAdapter.EVENT_FULL_SYNC_END.equals(event) &&
  829. !RefreshFolderOperation.EVENT_SINGLE_FOLDER_SHARES_SYNCED
  830. .equals(event));
  831. if (RefreshFolderOperation.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED.
  832. equals(event)) {
  833. if (synchResult != null && !synchResult.isSuccess()) {
  834. /// TODO refactor and make common
  835. if (checkForRemoteOperationError(synchResult)) {
  836. requestCredentialsUpdate(context);
  837. } else if (RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED.equals(
  838. synchResult.getCode())) {
  839. showUntrustedCertDialog(synchResult);
  840. }
  841. }
  842. }
  843. removeStickyBroadcast(intent);
  844. Log_OC.d(TAG, "Setting progress visibility to " + mSyncInProgress);
  845. setIndeterminate(mSyncInProgress);
  846. setBackgroundText();
  847. }
  848. }
  849. if (synchResult != null) {
  850. if (synchResult.getCode().equals(
  851. RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED)) {
  852. mLastSslUntrustedServerResult = synchResult;
  853. }
  854. }
  855. } catch (RuntimeException e) {
  856. // avoid app crashes after changing the serial id of RemoteOperationResult
  857. // in owncloud library with broadcast notifications pending to process
  858. removeStickyBroadcast(intent);
  859. }
  860. }
  861. }
  862. private boolean checkForRemoteOperationError(RemoteOperationResult syncResult) {
  863. return ResultCode.UNAUTHORIZED.equals(syncResult.getCode()) ||
  864. (syncResult.isException() && syncResult.getException()
  865. instanceof AuthenticatorException);
  866. }
  867. /**
  868. * Show a text message on screen view for notifying user if content is
  869. * loading or folder is empty
  870. */
  871. private void setBackgroundText() {
  872. OCFileListFragment ocFileListFragment = getListOfFilesFragment();
  873. if (ocFileListFragment != null) {
  874. int message = R.string.file_list_loading;
  875. if (!mSyncInProgress) {
  876. // In case file list is empty
  877. message = R.string.file_list_empty;
  878. }
  879. ocFileListFragment.setMessageForEmptyList(getString(message));
  880. } else {
  881. Log_OC.e(TAG, "OCFileListFragment is null");
  882. }
  883. }
  884. /**
  885. * Once the file upload has finished -> update view
  886. */
  887. private class UploadFinishReceiver extends BroadcastReceiver {
  888. /**
  889. * Once the file upload has finished -> update view
  890. *
  891. * @author David A. Velasco
  892. * {@link BroadcastReceiver} to enable upload feedback in UI
  893. */
  894. @Override
  895. public void onReceive(Context context, Intent intent) {
  896. try {
  897. String uploadedRemotePath = intent.getStringExtra(FileUploader.EXTRA_REMOTE_PATH);
  898. String accountName = intent.getStringExtra(FileUploader.ACCOUNT_NAME);
  899. boolean sameAccount = getAccount() != null && accountName.equals(getAccount().name);
  900. OCFile currentDir = getCurrentDir();
  901. boolean isDescendant = (currentDir != null) && (uploadedRemotePath != null) &&
  902. (uploadedRemotePath.startsWith(currentDir.getRemotePath()));
  903. if (sameAccount && isDescendant) {
  904. String linkedToRemotePath =
  905. intent.getStringExtra(FileUploader.EXTRA_LINKED_TO_PATH);
  906. if (linkedToRemotePath == null || isAscendant(linkedToRemotePath)) {
  907. refreshListOfFilesFragment();
  908. }
  909. }
  910. boolean uploadWasFine = intent.getBooleanExtra(
  911. FileUploader.EXTRA_UPLOAD_RESULT,
  912. false);
  913. boolean renamedInUpload = getFile().getRemotePath().
  914. equals(intent.getStringExtra(FileUploader.EXTRA_OLD_REMOTE_PATH));
  915. boolean sameFile = getFile().getRemotePath().equals(uploadedRemotePath) ||
  916. renamedInUpload;
  917. FileFragment details = getSecondFragment();
  918. boolean detailFragmentIsShown = (details != null &&
  919. details instanceof FileDetailFragment);
  920. if (sameAccount && sameFile && detailFragmentIsShown) {
  921. if (uploadWasFine) {
  922. setFile(getStorageManager().getFileByPath(uploadedRemotePath));
  923. } else {
  924. //TODO remove upload progress bar after upload failed.
  925. }
  926. if (renamedInUpload) {
  927. String newName = (new File(uploadedRemotePath)).getName();
  928. Toast msg = Toast.makeText(
  929. context,
  930. String.format(
  931. getString(R.string.filedetails_renamed_in_upload_msg),
  932. newName),
  933. Toast.LENGTH_LONG);
  934. msg.show();
  935. }
  936. if (uploadWasFine || getFile().fileExists()) {
  937. ((FileDetailFragment) details).updateFileDetails(false, true);
  938. } else {
  939. cleanSecondFragment();
  940. }
  941. // Force the preview if the file is an image or text file
  942. if (uploadWasFine) {
  943. OCFile ocFile = getFile();
  944. if (PreviewImageFragment.canBePreviewed(ocFile))
  945. startImagePreview(getFile());
  946. else if (PreviewTextFragment.canBePreviewed(ocFile))
  947. startTextPreview(ocFile);
  948. // TODO what about other kind of previews?
  949. }
  950. }
  951. setIndeterminate(false);
  952. } finally {
  953. if (intent != null) {
  954. removeStickyBroadcast(intent);
  955. }
  956. }
  957. }
  958. // TODO refactor this receiver, and maybe DownloadFinishReceiver; this method is duplicated :S
  959. private boolean isAscendant(String linkedToRemotePath) {
  960. OCFile currentDir = getCurrentDir();
  961. return (
  962. currentDir != null &&
  963. currentDir.getRemotePath().startsWith(linkedToRemotePath)
  964. );
  965. }
  966. }
  967. /**
  968. * Class waiting for broadcast events from the {@link FileDownloader} service.
  969. * <p/>
  970. * Updates the UI when a download is started or finished, provided that it is relevant for the
  971. * current folder.
  972. */
  973. private class DownloadFinishReceiver extends BroadcastReceiver {
  974. @Override
  975. public void onReceive(Context context, Intent intent) {
  976. try {
  977. boolean sameAccount = isSameAccount(intent);
  978. String downloadedRemotePath =
  979. intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
  980. boolean isDescendant = isDescendant(downloadedRemotePath);
  981. if (sameAccount && isDescendant) {
  982. String linkedToRemotePath =
  983. intent.getStringExtra(FileDownloader.EXTRA_LINKED_TO_PATH);
  984. if (linkedToRemotePath == null || isAscendant(linkedToRemotePath)) {
  985. refreshListOfFilesFragment();
  986. }
  987. refreshSecondFragment(
  988. intent.getAction(),
  989. downloadedRemotePath,
  990. intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false)
  991. );
  992. }
  993. if (mWaitingToSend != null) {
  994. mWaitingToSend =
  995. getStorageManager().getFileByPath(mWaitingToSend.getRemotePath());
  996. if (mWaitingToSend.isDown()) {
  997. sendDownloadedFile();
  998. }
  999. }
  1000. } finally {
  1001. if (intent != null) {
  1002. removeStickyBroadcast(intent);
  1003. }
  1004. }
  1005. }
  1006. private boolean isDescendant(String downloadedRemotePath) {
  1007. OCFile currentDir = getCurrentDir();
  1008. return (
  1009. currentDir != null &&
  1010. downloadedRemotePath != null &&
  1011. downloadedRemotePath.startsWith(currentDir.getRemotePath())
  1012. );
  1013. }
  1014. private boolean isAscendant(String linkedToRemotePath) {
  1015. OCFile currentDir = getCurrentDir();
  1016. return (
  1017. currentDir != null &&
  1018. currentDir.getRemotePath().startsWith(linkedToRemotePath)
  1019. );
  1020. }
  1021. private boolean isSameAccount(Intent intent) {
  1022. String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);
  1023. return (accountName != null && getAccount() != null &&
  1024. accountName.equals(getAccount().name));
  1025. }
  1026. }
  1027. public void browseToRoot() {
  1028. OCFileListFragment listOfFiles = getListOfFilesFragment();
  1029. if (listOfFiles != null) { // should never be null, indeed
  1030. OCFile root = getStorageManager().getFileByPath(OCFile.ROOT_PATH);
  1031. listOfFiles.listDirectory(root, MainApp.getOnlyOnDevice());
  1032. setFile(listOfFiles.getCurrentFile());
  1033. startSyncFolderOperation(root, false);
  1034. }
  1035. cleanSecondFragment();
  1036. }
  1037. /**
  1038. * {@inheritDoc}
  1039. * Updates action bar and second fragment, if in dual pane mode.
  1040. */
  1041. @Override
  1042. public void onBrowsedDownTo(OCFile directory) {
  1043. setFile(directory);
  1044. cleanSecondFragment();
  1045. // Sync Folder
  1046. startSyncFolderOperation(directory, false);
  1047. }
  1048. /**
  1049. * Shows the information of the {@link OCFile} received as a
  1050. * parameter in the second fragment.
  1051. *
  1052. * @param file {@link OCFile} whose details will be shown
  1053. */
  1054. @Override
  1055. public void showDetails(OCFile file) {
  1056. Fragment detailFragment = FileDetailFragment.newInstance(file, getAccount());
  1057. setSecondFragment(detailFragment);
  1058. updateFragmentsVisibility(true);
  1059. updateActionBarTitleAndHomeButton(file);
  1060. setFile(file);
  1061. }
  1062. @Override
  1063. protected void updateActionBarTitleAndHomeButton(OCFile chosenFile) {
  1064. if (chosenFile == null) {
  1065. chosenFile = getFile(); // if no file is passed, current file decides
  1066. }
  1067. if (mDualPane) {
  1068. // in dual pane mode, keep the focus of title an action bar in the current folder
  1069. super.updateActionBarTitleAndHomeButton(getCurrentDir());
  1070. } else {
  1071. super.updateActionBarTitleAndHomeButton(chosenFile);
  1072. }
  1073. }
  1074. @Override
  1075. protected ServiceConnection newTransferenceServiceConnection() {
  1076. return new ListServiceConnection();
  1077. }
  1078. /**
  1079. * Defines callbacks for service binding, passed to bindService()
  1080. */
  1081. private class ListServiceConnection implements ServiceConnection {
  1082. @Override
  1083. public void onServiceConnected(ComponentName component, IBinder service) {
  1084. if (component.equals(new ComponentName(
  1085. FileDisplayActivity.this, FileDownloader.class))) {
  1086. Log_OC.d(TAG, "Download service connected");
  1087. mDownloaderBinder = (FileDownloaderBinder) service;
  1088. if (mWaitingToPreview != null)
  1089. if (getStorageManager() != null) {
  1090. // update the file
  1091. mWaitingToPreview =
  1092. getStorageManager().getFileById(mWaitingToPreview.getFileId());
  1093. if (!mWaitingToPreview.isDown()) {
  1094. requestForDownload();
  1095. }
  1096. }
  1097. } else if (component.equals(new ComponentName(FileDisplayActivity.this,
  1098. FileUploader.class))) {
  1099. Log_OC.d(TAG, "Upload service connected");
  1100. mUploaderBinder = (FileUploaderBinder) service;
  1101. } else {
  1102. return;
  1103. }
  1104. // a new chance to get the mDownloadBinder through
  1105. // getFileDownloadBinder() - THIS IS A MESS
  1106. OCFileListFragment listOfFiles = getListOfFilesFragment();
  1107. if (listOfFiles != null) {
  1108. listOfFiles.listDirectory(MainApp.getOnlyOnDevice());
  1109. }
  1110. FileFragment secondFragment = getSecondFragment();
  1111. if (secondFragment != null && secondFragment instanceof FileDetailFragment) {
  1112. FileDetailFragment detailFragment = (FileDetailFragment) secondFragment;
  1113. detailFragment.listenForTransferProgress();
  1114. detailFragment.updateFileDetails(false, false);
  1115. }
  1116. }
  1117. @Override
  1118. public void onServiceDisconnected(ComponentName component) {
  1119. if (component.equals(new ComponentName(FileDisplayActivity.this,
  1120. FileDownloader.class))) {
  1121. Log_OC.d(TAG, "Download service disconnected");
  1122. mDownloaderBinder = null;
  1123. } else if (component.equals(new ComponentName(FileDisplayActivity.this,
  1124. FileUploader.class))) {
  1125. Log_OC.d(TAG, "Upload service disconnected");
  1126. mUploaderBinder = null;
  1127. }
  1128. }
  1129. }
  1130. /**
  1131. * Updates the view associated to the activity after the finish of some operation over files
  1132. * in the current account.
  1133. *
  1134. * @param operation Removal operation performed.
  1135. * @param result Result of the removal.
  1136. */
  1137. @Override
  1138. public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
  1139. super.onRemoteOperationFinish(operation, result);
  1140. if (operation instanceof RemoveFileOperation) {
  1141. onRemoveFileOperationFinish((RemoveFileOperation) operation, result);
  1142. } else if (operation instanceof RenameFileOperation) {
  1143. onRenameFileOperationFinish((RenameFileOperation) operation, result);
  1144. } else if (operation instanceof SynchronizeFileOperation) {
  1145. onSynchronizeFileOperationFinish((SynchronizeFileOperation) operation, result);
  1146. } else if (operation instanceof CreateFolderOperation) {
  1147. onCreateFolderOperationFinish((CreateFolderOperation) operation, result);
  1148. } else if (operation instanceof MoveFileOperation) {
  1149. onMoveFileOperationFinish((MoveFileOperation) operation, result);
  1150. } else if (operation instanceof CopyFileOperation) {
  1151. onCopyFileOperationFinish((CopyFileOperation) operation, result);
  1152. }
  1153. }
  1154. private void refreshShowDetails() {
  1155. FileFragment details = getSecondFragment();
  1156. if (details != null) {
  1157. OCFile file = details.getFile();
  1158. if (file != null) {
  1159. file = getStorageManager().getFileByPath(file.getRemotePath());
  1160. if (details instanceof PreviewMediaFragment) {
  1161. // Refresh OCFile of the fragment
  1162. ((PreviewMediaFragment) details).updateFile(file);
  1163. } else if (details instanceof PreviewTextFragment) {
  1164. // Refresh OCFile of the fragment
  1165. ((PreviewTextFragment) details).updateFile(file);
  1166. } else {
  1167. showDetails(file);
  1168. }
  1169. }
  1170. invalidateOptionsMenu();
  1171. }
  1172. }
  1173. /**
  1174. * Updates the view associated to the activity after the finish of an operation trying to
  1175. * remove a file.
  1176. *
  1177. * @param operation Removal operation performed.
  1178. * @param result Result of the removal.
  1179. */
  1180. private void onRemoveFileOperationFinish(RemoveFileOperation operation,
  1181. RemoteOperationResult result) {
  1182. Toast msg = Toast.makeText(this,
  1183. ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
  1184. Toast.LENGTH_LONG);
  1185. msg.show();
  1186. if (result.isSuccess()) {
  1187. OCFile removedFile = operation.getFile();
  1188. FileFragment second = getSecondFragment();
  1189. if (second != null && removedFile.equals(second.getFile())) {
  1190. if (second instanceof PreviewMediaFragment) {
  1191. ((PreviewMediaFragment) second).stopPreview(true);
  1192. }
  1193. setFile(getStorageManager().getFileById(removedFile.getParentId()));
  1194. cleanSecondFragment();
  1195. }
  1196. if (getStorageManager().getFileById(removedFile.getParentId()).equals(getCurrentDir())) {
  1197. refreshListOfFilesFragment();
  1198. }
  1199. invalidateOptionsMenu();
  1200. } else {
  1201. if (result.isSslRecoverableException()) {
  1202. mLastSslUntrustedServerResult = result;
  1203. showUntrustedCertDialog(mLastSslUntrustedServerResult);
  1204. }
  1205. }
  1206. }
  1207. /**
  1208. * Updates the view associated to the activity after the finish of an operation trying to move a
  1209. * file.
  1210. *
  1211. * @param operation Move operation performed.
  1212. * @param result Result of the move operation.
  1213. */
  1214. private void onMoveFileOperationFinish(MoveFileOperation operation,
  1215. RemoteOperationResult result) {
  1216. if (result.isSuccess()) {
  1217. refreshListOfFilesFragment();
  1218. } else {
  1219. try {
  1220. Toast msg = Toast.makeText(FileDisplayActivity.this,
  1221. ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
  1222. Toast.LENGTH_LONG);
  1223. msg.show();
  1224. } catch (NotFoundException e) {
  1225. Log_OC.e(TAG, "Error while trying to show fail message ", e);
  1226. }
  1227. }
  1228. }
  1229. /**
  1230. * Updates the view associated to the activity after the finish of an operation trying to copy a
  1231. * file.
  1232. *
  1233. * @param operation Copy operation performed.
  1234. * @param result Result of the copy operation.
  1235. */
  1236. private void onCopyFileOperationFinish(CopyFileOperation operation, RemoteOperationResult result) {
  1237. if (result.isSuccess()) {
  1238. refreshListOfFilesFragment();
  1239. } else {
  1240. try {
  1241. Toast msg = Toast.makeText(FileDisplayActivity.this,
  1242. ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
  1243. Toast.LENGTH_LONG);
  1244. msg.show();
  1245. } catch (NotFoundException e) {
  1246. Log_OC.e(TAG, "Error while trying to show fail message ", e);
  1247. }
  1248. }
  1249. }
  1250. /**
  1251. * Updates the view associated to the activity after the finish of an operation trying to rename
  1252. * a file.
  1253. *
  1254. * @param operation Renaming operation performed.
  1255. * @param result Result of the renaming.
  1256. */
  1257. private void onRenameFileOperationFinish(RenameFileOperation operation,
  1258. RemoteOperationResult result) {
  1259. OCFile renamedFile = operation.getFile();
  1260. if (result.isSuccess()) {
  1261. FileFragment details = getSecondFragment();
  1262. if (details != null) {
  1263. if (details instanceof FileDetailFragment &&
  1264. renamedFile.equals(details.getFile())) {
  1265. ((FileDetailFragment) details).updateFileDetails(renamedFile, getAccount());
  1266. showDetails(renamedFile);
  1267. } else if (details instanceof PreviewMediaFragment &&
  1268. renamedFile.equals(details.getFile())) {
  1269. ((PreviewMediaFragment) details).updateFile(renamedFile);
  1270. if (PreviewMediaFragment.canBePreviewed(renamedFile)) {
  1271. int position = ((PreviewMediaFragment) details).getPosition();
  1272. startMediaPreview(renamedFile, position, true);
  1273. } else {
  1274. getFileOperationsHelper().openFile(renamedFile);
  1275. }
  1276. } else if (details instanceof PreviewTextFragment &&
  1277. renamedFile.equals(details.getFile())) {
  1278. ((PreviewTextFragment) details).updateFile(renamedFile);
  1279. if (PreviewTextFragment.canBePreviewed(renamedFile)) {
  1280. startTextPreview(renamedFile);
  1281. } else {
  1282. getFileOperationsHelper().openFile(renamedFile);
  1283. }
  1284. }
  1285. }
  1286. if (getStorageManager().getFileById(renamedFile.getParentId()).equals(getCurrentDir())) {
  1287. refreshListOfFilesFragment();
  1288. }
  1289. } else {
  1290. Toast msg = Toast.makeText(this,
  1291. ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
  1292. Toast.LENGTH_LONG);
  1293. msg.show();
  1294. if (result.isSslRecoverableException()) {
  1295. mLastSslUntrustedServerResult = result;
  1296. showUntrustedCertDialog(mLastSslUntrustedServerResult);
  1297. }
  1298. }
  1299. }
  1300. private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation,
  1301. RemoteOperationResult result) {
  1302. if (result.isSuccess()) {
  1303. if (operation.transferWasRequested()) {
  1304. OCFile syncedFile = operation.getLocalFile();
  1305. onTransferStateChanged(syncedFile, true, true);
  1306. invalidateOptionsMenu();
  1307. refreshShowDetails();
  1308. }
  1309. }
  1310. }
  1311. /**
  1312. * Updates the view associated to the activity after the finish of an operation trying create a
  1313. * new folder
  1314. *
  1315. * @param operation Creation operation performed.
  1316. * @param result Result of the creation.
  1317. */
  1318. private void onCreateFolderOperationFinish(CreateFolderOperation operation,
  1319. RemoteOperationResult result) {
  1320. if (result.isSuccess()) {
  1321. refreshListOfFilesFragment();
  1322. } else {
  1323. try {
  1324. Toast msg = Toast.makeText(FileDisplayActivity.this,
  1325. ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
  1326. Toast.LENGTH_LONG);
  1327. msg.show();
  1328. } catch (NotFoundException e) {
  1329. Log_OC.e(TAG, "Error while trying to show fail message ", e);
  1330. }
  1331. }
  1332. }
  1333. /**
  1334. * {@inheritDoc}
  1335. */
  1336. @Override
  1337. public void onTransferStateChanged(OCFile file, boolean downloading, boolean uploading) {
  1338. refreshListOfFilesFragment();
  1339. FileFragment details = getSecondFragment();
  1340. if (details != null && details instanceof FileDetailFragment &&
  1341. file.equals(details.getFile())) {
  1342. if (downloading || uploading) {
  1343. ((FileDetailFragment) details).updateFileDetails(file, getAccount());
  1344. } else {
  1345. if (!file.fileExists()) {
  1346. cleanSecondFragment();
  1347. } else {
  1348. ((FileDetailFragment) details).updateFileDetails(false, true);
  1349. }
  1350. }
  1351. }
  1352. }
  1353. private void requestForDownload() {
  1354. Account account = getAccount();
  1355. //if (!mWaitingToPreview.isDownloading()) {
  1356. if (!mDownloaderBinder.isDownloading(account, mWaitingToPreview)) {
  1357. Intent i = new Intent(this, FileDownloader.class);
  1358. i.putExtra(FileDownloader.EXTRA_ACCOUNT, account);
  1359. i.putExtra(FileDownloader.EXTRA_FILE, mWaitingToPreview);
  1360. startService(i);
  1361. }
  1362. }
  1363. @Override
  1364. public void onSavedCertificate() {
  1365. startSyncFolderOperation(getCurrentDir(), false);
  1366. }
  1367. /**
  1368. * Starts an operation to refresh the requested folder.
  1369. * <p/>
  1370. * The operation is run in a new background thread created on the fly.
  1371. * <p/>
  1372. * The refresh updates is a "light sync": properties of regular files in folder are updated (including
  1373. * associated shares), but not their contents. Only the contents of files marked to be kept-in-sync are
  1374. * synchronized too.
  1375. *
  1376. * @param folder Folder to refresh.
  1377. * @param ignoreETag If 'true', the data from the server will be fetched and sync'ed even if the eTag
  1378. * didn't change.
  1379. */
  1380. public void startSyncFolderOperation(final OCFile folder, final boolean ignoreETag) {
  1381. // the execution is slightly delayed to allow the activity get the window focus if it's being started
  1382. // or if the method is called from a dialog that is being dismissed
  1383. getHandler().postDelayed(
  1384. new Runnable() {
  1385. @Override
  1386. public void run() {
  1387. if (hasWindowFocus()) {
  1388. long currentSyncTime = System.currentTimeMillis();
  1389. mSyncInProgress = true;
  1390. // perform folder synchronization
  1391. RemoteOperation synchFolderOp = new RefreshFolderOperation(folder,
  1392. currentSyncTime,
  1393. false,
  1394. getFileOperationsHelper().isSharedSupported(),
  1395. ignoreETag,
  1396. getStorageManager(),
  1397. getAccount(),
  1398. getApplicationContext()
  1399. );
  1400. synchFolderOp.execute(
  1401. getAccount(),
  1402. MainApp.getAppContext(),
  1403. FileDisplayActivity.this,
  1404. null,
  1405. null
  1406. );
  1407. setIndeterminate(true);
  1408. setBackgroundText();
  1409. } // else: NOTHING ; lets' not refresh when the user rotates the device but there is
  1410. // another window floating over
  1411. }
  1412. },
  1413. DELAY_TO_REQUEST_OPERATIONS_LATER
  1414. );
  1415. }
  1416. private void requestForDownload(OCFile file) {
  1417. Account account = getAccount();
  1418. if (!mDownloaderBinder.isDownloading(account, mWaitingToPreview)) {
  1419. Intent i = new Intent(this, FileDownloader.class);
  1420. i.putExtra(FileDownloader.EXTRA_ACCOUNT, account);
  1421. i.putExtra(FileDownloader.EXTRA_FILE, file);
  1422. startService(i);
  1423. }
  1424. }
  1425. private void sendDownloadedFile() {
  1426. getFileOperationsHelper().sendDownloadedFile(mWaitingToSend);
  1427. mWaitingToSend = null;
  1428. }
  1429. /**
  1430. * Requests the download of the received {@link OCFile} , updates the UI
  1431. * to monitor the download progress and prepares the activity to send the file
  1432. * when the download finishes.
  1433. *
  1434. * @param file {@link OCFile} to download and preview.
  1435. */
  1436. public void startDownloadForSending(OCFile file) {
  1437. mWaitingToSend = file;
  1438. requestForDownload(mWaitingToSend);
  1439. boolean hasSecondFragment = (getSecondFragment() != null);
  1440. updateFragmentsVisibility(hasSecondFragment);
  1441. }
  1442. /**
  1443. * Opens the image gallery showing the image {@link OCFile} received as parameter.
  1444. *
  1445. * @param file Image {@link OCFile} to show.
  1446. */
  1447. public void startImagePreview(OCFile file) {
  1448. Intent showDetailsIntent = new Intent(this, PreviewImageActivity.class);
  1449. showDetailsIntent.putExtra(EXTRA_FILE, file);
  1450. showDetailsIntent.putExtra(EXTRA_ACCOUNT, getAccount());
  1451. startActivity(showDetailsIntent);
  1452. }
  1453. /**
  1454. * Stars the preview of an already down media {@link OCFile}.
  1455. *
  1456. * @param file Media {@link OCFile} to preview.
  1457. * @param startPlaybackPosition Media position where the playback will be started,
  1458. * in milliseconds.
  1459. * @param autoplay When 'true', the playback will start without user
  1460. * interactions.
  1461. */
  1462. public void startMediaPreview(OCFile file, int startPlaybackPosition, boolean autoplay) {
  1463. Fragment mediaFragment = new PreviewMediaFragment(file, getAccount(), startPlaybackPosition,
  1464. autoplay);
  1465. setSecondFragment(mediaFragment);
  1466. updateFragmentsVisibility(true);
  1467. updateActionBarTitleAndHomeButton(file);
  1468. setFile(file);
  1469. }
  1470. /**
  1471. * Stars the preview of a text file {@link OCFile}.
  1472. *
  1473. * @param file Text {@link OCFile} to preview.
  1474. */
  1475. public void startTextPreview(OCFile file) {
  1476. Bundle args = new Bundle();
  1477. args.putParcelable(EXTRA_FILE, file);
  1478. args.putParcelable(EXTRA_ACCOUNT, getAccount());
  1479. Fragment textPreviewFragment = Fragment.instantiate(getApplicationContext(),
  1480. PreviewTextFragment.class.getName(), args);
  1481. setSecondFragment(textPreviewFragment);
  1482. updateFragmentsVisibility(true);
  1483. updateActionBarTitleAndHomeButton(file);
  1484. setFile(file);
  1485. }
  1486. /**
  1487. * Requests the download of the received {@link OCFile} , updates the UI
  1488. * to monitor the download progress and prepares the activity to preview
  1489. * or open the file when the download finishes.
  1490. *
  1491. * @param file {@link OCFile} to download and preview.
  1492. */
  1493. public void startDownloadForPreview(OCFile file) {
  1494. Fragment detailFragment = FileDetailFragment.newInstance(file, getAccount());
  1495. setSecondFragment(detailFragment);
  1496. mWaitingToPreview = file;
  1497. requestForDownload();
  1498. updateFragmentsVisibility(true);
  1499. updateActionBarTitleAndHomeButton(file);
  1500. setFile(file);
  1501. }
  1502. /**
  1503. * Request stopping the upload/download operation in progress over the given {@link OCFile} file.
  1504. *
  1505. * @param file {@link OCFile} file which operation are wanted to be cancel
  1506. */
  1507. public void cancelTransference(OCFile file) {
  1508. getFileOperationsHelper().cancelTransference(file);
  1509. if (mWaitingToPreview != null &&
  1510. mWaitingToPreview.getRemotePath().equals(file.getRemotePath())) {
  1511. mWaitingToPreview = null;
  1512. }
  1513. if (mWaitingToSend != null &&
  1514. mWaitingToSend.getRemotePath().equals(file.getRemotePath())) {
  1515. mWaitingToSend = null;
  1516. }
  1517. onTransferStateChanged(file, false, false);
  1518. }
  1519. /**
  1520. * Request stopping all upload/download operations in progress over the given {@link OCFile} files.
  1521. *
  1522. * @param files collection of {@link OCFile} files which operations are wanted to be cancel
  1523. */
  1524. public void cancelTransference(Collection<OCFile> files) {
  1525. for(OCFile file: files) {
  1526. cancelTransference(file);
  1527. }
  1528. }
  1529. @Override
  1530. public void onRefresh(boolean ignoreETag) {
  1531. refreshList(ignoreETag);
  1532. }
  1533. @Override
  1534. public void onRefresh() {
  1535. refreshList(true);
  1536. }
  1537. private void refreshList(boolean ignoreETag) {
  1538. OCFileListFragment listOfFiles = getListOfFilesFragment();
  1539. if (listOfFiles != null) {
  1540. OCFile folder = listOfFiles.getCurrentFile();
  1541. if (folder != null) {
  1542. /*mFile = mContainerActivity.getStorageManager().getFileById(mFile.getFileId());
  1543. listDirectory(mFile);*/
  1544. startSyncFolderOperation(folder, ignoreETag);
  1545. }
  1546. }
  1547. }
  1548. private void sortByDate(boolean ascending) {
  1549. getListOfFilesFragment().sortByDate(ascending);
  1550. }
  1551. private void sortBySize(boolean ascending) {
  1552. getListOfFilesFragment().sortBySize(ascending);
  1553. }
  1554. private void sortByName(boolean ascending) {
  1555. getListOfFilesFragment().sortByName(ascending);
  1556. }
  1557. private boolean isGridView() {
  1558. return getListOfFilesFragment().isGridEnabled();
  1559. }
  1560. public void allFilesOption() {
  1561. browseToRoot();
  1562. }
  1563. public void refreshDirectory() {
  1564. getListOfFilesFragment().refreshDirectory();
  1565. }
  1566. }