FileDisplayActivity.java 72 KB

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