FileDisplayActivity.java 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  1. /* ownCloud Android client application
  2. * Copyright (C) 2011 Bartek Przybylski
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. package com.owncloud.android.ui.activity;
  19. import java.io.File;
  20. import android.accounts.Account;
  21. import android.app.Activity;
  22. import android.app.AlertDialog;
  23. import android.app.ProgressDialog;
  24. import android.app.AlertDialog.Builder;
  25. import android.app.Dialog;
  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.DialogInterface.OnClickListener;
  32. import android.content.Intent;
  33. import android.content.IntentFilter;
  34. import android.content.ServiceConnection;
  35. import android.content.SharedPreferences;
  36. import android.content.SharedPreferences.Editor;
  37. import android.content.pm.PackageInfo;
  38. import android.content.pm.PackageManager.NameNotFoundException;
  39. import android.content.res.Resources.NotFoundException;
  40. import android.database.Cursor;
  41. import android.graphics.Bitmap;
  42. import android.graphics.drawable.BitmapDrawable;
  43. import android.net.Uri;
  44. import android.os.Bundle;
  45. import android.os.Handler;
  46. import android.os.IBinder;
  47. import android.preference.PreferenceManager;
  48. import android.provider.MediaStore;
  49. import android.support.v4.app.Fragment;
  50. import android.support.v4.app.FragmentTransaction;
  51. import android.util.Log;
  52. import android.view.View;
  53. import android.view.ViewGroup;
  54. import android.widget.ArrayAdapter;
  55. import android.widget.EditText;
  56. import android.widget.TextView;
  57. import android.widget.Toast;
  58. import com.actionbarsherlock.app.ActionBar;
  59. import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
  60. import com.actionbarsherlock.app.SherlockFragmentActivity;
  61. import com.actionbarsherlock.view.Menu;
  62. import com.actionbarsherlock.view.MenuInflater;
  63. import com.actionbarsherlock.view.MenuItem;
  64. import com.actionbarsherlock.view.Window;
  65. import com.owncloud.android.AccountUtils;
  66. import com.owncloud.android.authenticator.AccountAuthenticator;
  67. import com.owncloud.android.datamodel.DataStorageManager;
  68. import com.owncloud.android.datamodel.FileDataStorageManager;
  69. import com.owncloud.android.datamodel.OCFile;
  70. import com.owncloud.android.files.services.FileDownloader;
  71. import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
  72. import com.owncloud.android.files.services.FileObserverService;
  73. import com.owncloud.android.files.services.FileUploader;
  74. import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
  75. import com.owncloud.android.network.OwnCloudClientUtils;
  76. import com.owncloud.android.operations.OnRemoteOperationListener;
  77. import com.owncloud.android.operations.RemoteOperation;
  78. import com.owncloud.android.operations.RemoteOperationResult;
  79. import com.owncloud.android.operations.RemoveFileOperation;
  80. import com.owncloud.android.operations.RenameFileOperation;
  81. import com.owncloud.android.operations.SynchronizeFileOperation;
  82. import com.owncloud.android.operations.RemoteOperationResult.ResultCode;
  83. import com.owncloud.android.syncadapter.FileSyncService;
  84. import com.owncloud.android.ui.dialog.ChangelogDialog;
  85. import com.owncloud.android.ui.dialog.SslValidatorDialog;
  86. import com.owncloud.android.ui.dialog.SslValidatorDialog.OnSslValidatorListener;
  87. import com.owncloud.android.ui.fragment.FileDetailFragment;
  88. import com.owncloud.android.ui.fragment.FileFragment;
  89. import com.owncloud.android.ui.fragment.FilePreviewFragment;
  90. import com.owncloud.android.ui.fragment.OCFileListFragment;
  91. import com.owncloud.android.R;
  92. import eu.alefzero.webdav.WebdavClient;
  93. /**
  94. * Displays, what files the user has available in his ownCloud.
  95. *
  96. * @author Bartek Przybylski
  97. *
  98. */
  99. public class FileDisplayActivity extends SherlockFragmentActivity implements
  100. OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNavigationListener, OnSslValidatorListener, OnRemoteOperationListener {
  101. private ArrayAdapter<String> mDirectories;
  102. private OCFile mCurrentDir = null;
  103. private OCFile mCurrentFile = null;
  104. private DataStorageManager mStorageManager;
  105. private SyncBroadcastReceiver mSyncBroadcastReceiver;
  106. private UploadFinishReceiver mUploadFinishReceiver;
  107. private DownloadFinishReceiver mDownloadFinishReceiver;
  108. private FileDownloaderBinder mDownloaderBinder = null;
  109. private FileUploaderBinder mUploaderBinder = null;
  110. private ServiceConnection mDownloadConnection = null, mUploadConnection = null;
  111. private RemoteOperationResult mLastSslUntrustedServerResult = null;
  112. private OCFileListFragment mFileList;
  113. private boolean mDualPane;
  114. private static final int DIALOG_SETUP_ACCOUNT = 0;
  115. private static final int DIALOG_CREATE_DIR = 1;
  116. private static final int DIALOG_ABOUT_APP = 2;
  117. public static final int DIALOG_SHORT_WAIT = 3;
  118. private static final int DIALOG_CHOOSE_UPLOAD_SOURCE = 4;
  119. private static final int DIALOG_SSL_VALIDATOR = 5;
  120. private static final int DIALOG_CERT_NOT_SAVED = 6;
  121. private static final String DIALOG_CHANGELOG_TAG = "DIALOG_CHANGELOG";
  122. private static final int ACTION_SELECT_CONTENT_FROM_APPS = 1;
  123. private static final int ACTION_SELECT_MULTIPLE_FILES = 2;
  124. private static final String TAG = "FileDisplayActivity";
  125. private static int[] mMenuIdentifiersToPatch = {R.id.action_about_app};
  126. @Override
  127. public void onCreate(Bundle savedInstanceState) {
  128. Log.d(getClass().toString(), "onCreate() start");
  129. super.onCreate(savedInstanceState);
  130. /// Load of parameters from received intent
  131. mCurrentDir = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE); // no check necessary, mCurrenDir == null if the parameter is not in the intent
  132. Account account = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT);
  133. if (account != null)
  134. AccountUtils.setCurrentOwnCloudAccount(this, account.name);
  135. /// Load of saved instance state: keep this always before initDataFromCurrentAccount()
  136. if(savedInstanceState != null) {
  137. // TODO - test if savedInstanceState should take precedence over file in the intent ALWAYS (now), NEVER, or SOME TIMES
  138. mCurrentDir = savedInstanceState.getParcelable(FileDetailFragment.EXTRA_FILE);
  139. }
  140. if (!AccountUtils.accountsAreSetup(this)) {
  141. /// no account available: FORCE ACCOUNT CREATION
  142. mStorageManager = null;
  143. createFirstAccount();
  144. } else { /// at least an account is available
  145. initDataFromCurrentAccount(); // it checks mCurrentDir and mCurrentFile with the current account
  146. }
  147. mUploadConnection = new ListServiceConnection();
  148. mDownloadConnection = new ListServiceConnection();
  149. bindService(new Intent(this, FileUploader.class), mUploadConnection, Context.BIND_AUTO_CREATE);
  150. bindService(new Intent(this, FileDownloader.class), mDownloadConnection, Context.BIND_AUTO_CREATE);
  151. // PIN CODE request ; best location is to decide, let's try this first
  152. if (getIntent().getAction() != null && getIntent().getAction().equals(Intent.ACTION_MAIN) && savedInstanceState == null) {
  153. requestPinCode();
  154. }
  155. // file observer
  156. Intent observer_intent = new Intent(this, FileObserverService.class);
  157. observer_intent.putExtra(FileObserverService.KEY_FILE_CMD, FileObserverService.CMD_INIT_OBSERVED_LIST);
  158. startService(observer_intent);
  159. /// USER INTERFACE
  160. requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
  161. // Drop-down navigation
  162. mDirectories = new CustomArrayAdapter<String>(this, R.layout.sherlock_spinner_dropdown_item);
  163. OCFile currFile = mCurrentDir;
  164. while(currFile != null && currFile.getFileName() != OCFile.PATH_SEPARATOR) {
  165. mDirectories.add(currFile.getFileName());
  166. currFile = mStorageManager.getFileById(currFile.getParentId());
  167. }
  168. mDirectories.add(OCFile.PATH_SEPARATOR);
  169. // Inflate and set the layout view
  170. setContentView(R.layout.files);
  171. mFileList = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);
  172. mDualPane = (findViewById(R.id.file_details_container) != null);
  173. if (mDualPane) {
  174. initFileDetailsInDualPane();
  175. }
  176. // Action bar setup
  177. ActionBar actionBar = getSupportActionBar();
  178. actionBar.setHomeButtonEnabled(true); // mandatory since Android ICS, according to the official documentation
  179. actionBar.setDisplayHomeAsUpEnabled(mCurrentDir != null && mCurrentDir.getParentId() != 0);
  180. actionBar.setDisplayShowTitleEnabled(false);
  181. actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
  182. actionBar.setListNavigationCallbacks(mDirectories, this);
  183. setSupportProgressBarIndeterminateVisibility(false); // always AFTER setContentView(...) ; to workaround bug in its implementation
  184. // show changelog, if needed
  185. showChangeLog();
  186. Log.d(getClass().toString(), "onCreate() end");
  187. }
  188. /**
  189. * Shows a dialog with the change log of the current version after each app update
  190. *
  191. * TODO make it permanent; by now, only to advice the workaround app for 4.1.x
  192. */
  193. private void showChangeLog() {
  194. if (android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.JELLY_BEAN) {
  195. final String KEY_VERSION = "version";
  196. SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  197. int currentVersionNumber = 0;
  198. int savedVersionNumber = sharedPref.getInt(KEY_VERSION, 0);
  199. try {
  200. PackageInfo pi = getPackageManager().getPackageInfo(getPackageName(), 0);
  201. currentVersionNumber = pi.versionCode;
  202. } catch (Exception e) {}
  203. if (currentVersionNumber > savedVersionNumber) {
  204. ChangelogDialog.newInstance(true).show(getSupportFragmentManager(), DIALOG_CHANGELOG_TAG);
  205. Editor editor = sharedPref.edit();
  206. editor.putInt(KEY_VERSION, currentVersionNumber);
  207. editor.commit();
  208. }
  209. }
  210. }
  211. /**
  212. * Launches the account creation activity. To use when no ownCloud account is available
  213. */
  214. private void createFirstAccount() {
  215. Intent intent = new Intent(android.provider.Settings.ACTION_ADD_ACCOUNT);
  216. intent.putExtra(android.provider.Settings.EXTRA_AUTHORITIES, new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE });
  217. startActivity(intent); // the new activity won't be created until this.onStart() and this.onResume() are finished;
  218. }
  219. /**
  220. * Load of state dependent of the existence of an ownCloud account
  221. */
  222. private void initDataFromCurrentAccount() {
  223. /// Storage manager initialization - access to local database
  224. mStorageManager = new FileDataStorageManager(
  225. AccountUtils.getCurrentOwnCloudAccount(this),
  226. getContentResolver());
  227. /// Check if mCurrentDir is a directory
  228. if(mCurrentDir != null && !mCurrentDir.isDirectory()) {
  229. mCurrentFile = mCurrentDir;
  230. mCurrentDir = mStorageManager.getFileById(mCurrentDir.getParentId());
  231. }
  232. /// Check if mCurrentDir and mCurrentFile are in the current account, and update them
  233. if (mCurrentDir != null) {
  234. mCurrentDir = mStorageManager.getFileByPath(mCurrentDir.getRemotePath()); // mCurrentDir == null if it is not in the current account
  235. }
  236. if (mCurrentFile != null) {
  237. if (mCurrentFile.fileExists()) {
  238. mCurrentFile = mStorageManager.getFileByPath(mCurrentFile.getRemotePath()); // mCurrentFile == null if it is not in the current account
  239. } // else : keep mCurrentFile with the received value; this is currently the case of an upload in progress, when the user presses the status notification in a landscape tablet
  240. }
  241. /// Default to root if mCurrentDir was not found
  242. if (mCurrentDir == null) {
  243. mCurrentDir = mStorageManager.getFileByPath("/"); // will be NULL if the database was never synchronized
  244. }
  245. }
  246. private void initFileDetailsInDualPane() {
  247. if (mDualPane && getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG) == null) {
  248. FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  249. if (mCurrentFile != null) {
  250. if (mCurrentFile.isDown() && FilePreviewFragment.canBePreviewed(mCurrentFile)) {
  251. transaction.replace(R.id.file_details_container, new FilePreviewFragment(mCurrentFile, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);
  252. } else {
  253. transaction.replace(R.id.file_details_container, new FileDetailFragment(mCurrentFile, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);
  254. }
  255. mCurrentFile = null;
  256. } else {
  257. transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null), FileDetailFragment.FTAG); // empty FileDetailFragment
  258. }
  259. transaction.commit();
  260. }
  261. }
  262. @Override
  263. public void onDestroy() {
  264. super.onDestroy();
  265. if (mDownloadConnection != null)
  266. unbindService(mDownloadConnection);
  267. if (mUploadConnection != null)
  268. unbindService(mUploadConnection);
  269. }
  270. @Override
  271. public boolean onCreateOptionsMenu(Menu menu) {
  272. MenuInflater inflater = getSherlock().getMenuInflater();
  273. inflater.inflate(R.menu.main_menu, menu);
  274. patchHiddenAccents(menu);
  275. return true;
  276. }
  277. /**
  278. * Workaround for this: <a href="http://code.google.com/p/android/issues/detail?id=3974">http://code.google.com/p/android/issues/detail?id=3974</a>
  279. *
  280. * @param menu Menu to patch
  281. */
  282. private void patchHiddenAccents(Menu menu) {
  283. for (int i = 0; i < mMenuIdentifiersToPatch.length ; i++) {
  284. MenuItem aboutItem = menu.findItem(mMenuIdentifiersToPatch[i]);
  285. if (aboutItem != null && aboutItem.getIcon() instanceof BitmapDrawable) {
  286. // Clip off the bottom three (density independent) pixels of transparent padding
  287. Bitmap original = ((BitmapDrawable) aboutItem.getIcon()).getBitmap();
  288. float scale = getResources().getDisplayMetrics().density;
  289. int clippedHeight = (int) (original.getHeight() - (3 * scale));
  290. Bitmap scaled = Bitmap.createBitmap(original, 0, 0, original.getWidth(), clippedHeight);
  291. aboutItem.setIcon(new BitmapDrawable(getResources(), scaled));
  292. }
  293. }
  294. }
  295. @Override
  296. public boolean onOptionsItemSelected(MenuItem item) {
  297. boolean retval = true;
  298. switch (item.getItemId()) {
  299. case R.id.action_create_dir: {
  300. showDialog(DIALOG_CREATE_DIR);
  301. break;
  302. }
  303. case R.id.action_sync_account: {
  304. startSynchronization();
  305. break;
  306. }
  307. case R.id.action_upload: {
  308. showDialog(DIALOG_CHOOSE_UPLOAD_SOURCE);
  309. break;
  310. }
  311. case R.id.action_settings: {
  312. Intent settingsIntent = new Intent(this, Preferences.class);
  313. startActivity(settingsIntent);
  314. break;
  315. }
  316. case R.id.action_about_app: {
  317. showDialog(DIALOG_ABOUT_APP);
  318. break;
  319. }
  320. case android.R.id.home: {
  321. if(mCurrentDir != null && mCurrentDir.getParentId() != 0){
  322. onBackPressed();
  323. }
  324. break;
  325. }
  326. default:
  327. retval = super.onOptionsItemSelected(item);
  328. }
  329. return retval;
  330. }
  331. private void startSynchronization() {
  332. ContentResolver.cancelSync(null, AccountAuthenticator.AUTH_TOKEN_TYPE); // cancel the current synchronizations of any ownCloud account
  333. Bundle bundle = new Bundle();
  334. bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
  335. ContentResolver.requestSync(
  336. AccountUtils.getCurrentOwnCloudAccount(this),
  337. AccountAuthenticator.AUTH_TOKEN_TYPE, bundle);
  338. }
  339. @Override
  340. public boolean onNavigationItemSelected(int itemPosition, long itemId) {
  341. int i = itemPosition;
  342. while (i-- != 0) {
  343. onBackPressed();
  344. }
  345. // the next operation triggers a new call to this method, but it's necessary to
  346. // ensure that the name exposed in the action bar is the current directory when the
  347. // user selected it in the navigation list
  348. if (itemPosition != 0)
  349. getSupportActionBar().setSelectedNavigationItem(0);
  350. return true;
  351. }
  352. /**
  353. * Called, when the user selected something for uploading
  354. */
  355. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  356. if (requestCode == ACTION_SELECT_CONTENT_FROM_APPS && (resultCode == RESULT_OK || resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)) {
  357. requestSimpleUpload(data, resultCode);
  358. } else if (requestCode == ACTION_SELECT_MULTIPLE_FILES && (resultCode == RESULT_OK || resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)) {
  359. requestMultipleUpload(data, resultCode);
  360. }
  361. }
  362. private void requestMultipleUpload(Intent data, int resultCode) {
  363. String[] filePaths = data.getStringArrayExtra(UploadFilesActivity.EXTRA_CHOSEN_FILES);
  364. if (filePaths != null) {
  365. String[] remotePaths = new String[filePaths.length];
  366. String remotePathBase = "";
  367. for (int j = mDirectories.getCount() - 2; j >= 0; --j) {
  368. remotePathBase += OCFile.PATH_SEPARATOR + mDirectories.getItem(j);
  369. }
  370. if (!remotePathBase.endsWith(OCFile.PATH_SEPARATOR))
  371. remotePathBase += OCFile.PATH_SEPARATOR;
  372. for (int j = 0; j< remotePaths.length; j++) {
  373. remotePaths[j] = remotePathBase + (new File(filePaths[j])).getName();
  374. }
  375. Intent i = new Intent(this, FileUploader.class);
  376. i.putExtra(FileUploader.KEY_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(this));
  377. i.putExtra(FileUploader.KEY_LOCAL_FILE, filePaths);
  378. i.putExtra(FileUploader.KEY_REMOTE_FILE, remotePaths);
  379. i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_MULTIPLE_FILES);
  380. if (resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)
  381. i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_MOVE);
  382. startService(i);
  383. } else {
  384. Log.d("FileDisplay", "User clicked on 'Update' with no selection");
  385. Toast t = Toast.makeText(this, getString(R.string.filedisplay_no_file_selected), Toast.LENGTH_LONG);
  386. t.show();
  387. return;
  388. }
  389. }
  390. private void requestSimpleUpload(Intent data, int resultCode) {
  391. String filepath = null;
  392. try {
  393. Uri selectedImageUri = data.getData();
  394. String filemanagerstring = selectedImageUri.getPath();
  395. String selectedImagePath = getPath(selectedImageUri);
  396. if (selectedImagePath != null)
  397. filepath = selectedImagePath;
  398. else
  399. filepath = filemanagerstring;
  400. } catch (Exception e) {
  401. Log.e("FileDisplay", "Unexpected exception when trying to read the result of Intent.ACTION_GET_CONTENT", e);
  402. e.printStackTrace();
  403. } finally {
  404. if (filepath == null) {
  405. Log.e("FileDisplay", "Couldnt resolve path to file");
  406. Toast t = Toast.makeText(this, getString(R.string.filedisplay_unexpected_bad_get_content), Toast.LENGTH_LONG);
  407. t.show();
  408. return;
  409. }
  410. }
  411. Intent i = new Intent(this, FileUploader.class);
  412. i.putExtra(FileUploader.KEY_ACCOUNT,
  413. AccountUtils.getCurrentOwnCloudAccount(this));
  414. String remotepath = new String();
  415. for (int j = mDirectories.getCount() - 2; j >= 0; --j) {
  416. remotepath += OCFile.PATH_SEPARATOR + mDirectories.getItem(j);
  417. }
  418. if (!remotepath.endsWith(OCFile.PATH_SEPARATOR))
  419. remotepath += OCFile.PATH_SEPARATOR;
  420. remotepath += new File(filepath).getName();
  421. i.putExtra(FileUploader.KEY_LOCAL_FILE, filepath);
  422. i.putExtra(FileUploader.KEY_REMOTE_FILE, remotepath);
  423. i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
  424. if (resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)
  425. i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_MOVE);
  426. startService(i);
  427. }
  428. @Override
  429. public void onBackPressed() {
  430. if (mDirectories.getCount() <= 1) {
  431. finish();
  432. return;
  433. }
  434. popDirname();
  435. mFileList.onNavigateUp();
  436. mCurrentDir = mFileList.getCurrentFile();
  437. if (mDualPane) {
  438. // Resets the FileDetailsFragment on Tablets so that it always displays
  439. Fragment fileFragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
  440. if (fileFragment != null && (fileFragment instanceof FilePreviewFragment || !((FileDetailFragment) fileFragment).isEmpty())) {
  441. FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  442. transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null), FileDetailFragment.FTAG); // empty FileDetailFragment
  443. transaction.commit();
  444. }
  445. }
  446. if(mCurrentDir.getParentId() == 0){
  447. ActionBar actionBar = getSupportActionBar();
  448. actionBar.setDisplayHomeAsUpEnabled(false);
  449. }
  450. }
  451. @Override
  452. protected void onSaveInstanceState(Bundle outState) {
  453. // responsibility of restore is preferred in onCreate() before than in onRestoreInstanceState when there are Fragments involved
  454. Log.d(getClass().toString(), "onSaveInstanceState() start");
  455. super.onSaveInstanceState(outState);
  456. outState.putParcelable(FileDetailFragment.EXTRA_FILE, mCurrentDir);
  457. if (mDualPane) {
  458. FileFragment fragment = (FileFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
  459. if (fragment != null) {
  460. OCFile file = fragment.getFile();
  461. if (file != null) {
  462. outState.putParcelable(FileDetailFragment.EXTRA_FILE, file);
  463. }
  464. }
  465. }
  466. Log.d(getClass().toString(), "onSaveInstanceState() end");
  467. }
  468. @Override
  469. protected void onResume() {
  470. Log.d(getClass().toString(), "onResume() start");
  471. super.onResume();
  472. if (AccountUtils.accountsAreSetup(this)) {
  473. if (mStorageManager == null) {
  474. // this is necessary for handling the come back to FileDisplayActivity when the first ownCloud account is created
  475. initDataFromCurrentAccount();
  476. if (mDualPane) {
  477. initFileDetailsInDualPane();
  478. }
  479. }
  480. // Listen for sync messages
  481. IntentFilter syncIntentFilter = new IntentFilter(FileSyncService.SYNC_MESSAGE);
  482. mSyncBroadcastReceiver = new SyncBroadcastReceiver();
  483. registerReceiver(mSyncBroadcastReceiver, syncIntentFilter);
  484. // Listen for upload messages
  485. IntentFilter uploadIntentFilter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);
  486. mUploadFinishReceiver = new UploadFinishReceiver();
  487. registerReceiver(mUploadFinishReceiver, uploadIntentFilter);
  488. // Listen for download messages
  489. IntentFilter downloadIntentFilter = new IntentFilter(FileDownloader.DOWNLOAD_FINISH_MESSAGE);
  490. mDownloadFinishReceiver = new DownloadFinishReceiver();
  491. registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);
  492. // List current directory
  493. mFileList.listDirectory(mCurrentDir); // TODO we should find the way to avoid the need of this (maybe it's not necessary yet; to check)
  494. } else {
  495. mStorageManager = null; // an invalid object will be there if all the ownCloud accounts are removed
  496. showDialog(DIALOG_SETUP_ACCOUNT);
  497. }
  498. Log.d(getClass().toString(), "onResume() end");
  499. }
  500. @Override
  501. protected void onPause() {
  502. Log.d(getClass().toString(), "onPause() start");
  503. super.onPause();
  504. if (mSyncBroadcastReceiver != null) {
  505. unregisterReceiver(mSyncBroadcastReceiver);
  506. mSyncBroadcastReceiver = null;
  507. }
  508. if (mUploadFinishReceiver != null) {
  509. unregisterReceiver(mUploadFinishReceiver);
  510. mUploadFinishReceiver = null;
  511. }
  512. if (mDownloadFinishReceiver != null) {
  513. unregisterReceiver(mDownloadFinishReceiver);
  514. mDownloadFinishReceiver = null;
  515. }
  516. if (!AccountUtils.accountsAreSetup(this)) {
  517. dismissDialog(DIALOG_SETUP_ACCOUNT);
  518. }
  519. Log.d(getClass().toString(), "onPause() end");
  520. }
  521. @Override
  522. protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
  523. if (id == DIALOG_SSL_VALIDATOR && mLastSslUntrustedServerResult != null) {
  524. ((SslValidatorDialog)dialog).updateResult(mLastSslUntrustedServerResult);
  525. }
  526. }
  527. @Override
  528. protected Dialog onCreateDialog(int id) {
  529. Dialog dialog = null;
  530. AlertDialog.Builder builder;
  531. switch (id) {
  532. case DIALOG_SETUP_ACCOUNT: {
  533. builder = new AlertDialog.Builder(this);
  534. builder.setTitle(R.string.main_tit_accsetup);
  535. builder.setMessage(R.string.main_wrn_accsetup);
  536. builder.setCancelable(false);
  537. builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
  538. public void onClick(DialogInterface dialog, int which) {
  539. createFirstAccount();
  540. dialog.dismiss();
  541. }
  542. });
  543. String message = String.format(getString(R.string.common_exit), getString(R.string.app_name));
  544. builder.setNegativeButton(message, new OnClickListener() {
  545. public void onClick(DialogInterface dialog, int which) {
  546. dialog.dismiss();
  547. finish();
  548. }
  549. });
  550. //builder.setNegativeButton(android.R.string.cancel, this);
  551. dialog = builder.create();
  552. break;
  553. }
  554. case DIALOG_ABOUT_APP: {
  555. builder = new AlertDialog.Builder(this);
  556. builder.setTitle(getString(R.string.about_title));
  557. PackageInfo pkg;
  558. try {
  559. pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
  560. builder.setMessage(String.format(getString(R.string.about_message), getString(R.string.app_name), pkg.versionName));
  561. builder.setIcon(android.R.drawable.ic_menu_info_details);
  562. dialog = builder.create();
  563. } catch (NameNotFoundException e) {
  564. builder = null;
  565. dialog = null;
  566. Log.e(TAG, "Error while showing about dialog", e);
  567. }
  568. break;
  569. }
  570. case DIALOG_CREATE_DIR: {
  571. builder = new Builder(this);
  572. final EditText dirNameInput = new EditText(getBaseContext());
  573. builder.setView(dirNameInput);
  574. builder.setTitle(R.string.uploader_info_dirname);
  575. int typed_color = getResources().getColor(R.color.setup_text_typed);
  576. dirNameInput.setTextColor(typed_color);
  577. builder.setPositiveButton(android.R.string.ok,
  578. new OnClickListener() {
  579. public void onClick(DialogInterface dialog, int which) {
  580. String directoryName = dirNameInput.getText().toString();
  581. if (directoryName.trim().length() == 0) {
  582. dialog.cancel();
  583. return;
  584. }
  585. // Figure out the path where the dir needs to be created
  586. String path;
  587. if (mCurrentDir == null) {
  588. // this is just a patch; we should ensure that mCurrentDir never is null
  589. if (!mStorageManager.fileExists(OCFile.PATH_SEPARATOR)) {
  590. OCFile file = new OCFile(OCFile.PATH_SEPARATOR);
  591. mStorageManager.saveFile(file);
  592. }
  593. mCurrentDir = mStorageManager.getFileByPath(OCFile.PATH_SEPARATOR);
  594. }
  595. path = FileDisplayActivity.this.mCurrentDir.getRemotePath();
  596. // Create directory
  597. path += directoryName + OCFile.PATH_SEPARATOR;
  598. Thread thread = new Thread(new DirectoryCreator(path, AccountUtils.getCurrentOwnCloudAccount(FileDisplayActivity.this), new Handler()));
  599. thread.start();
  600. dialog.dismiss();
  601. showDialog(DIALOG_SHORT_WAIT);
  602. }
  603. });
  604. builder.setNegativeButton(R.string.common_cancel,
  605. new OnClickListener() {
  606. public void onClick(DialogInterface dialog, int which) {
  607. dialog.cancel();
  608. }
  609. });
  610. dialog = builder.create();
  611. break;
  612. }
  613. case DIALOG_SHORT_WAIT: {
  614. ProgressDialog working_dialog = new ProgressDialog(this);
  615. working_dialog.setMessage(getResources().getString(
  616. R.string.wait_a_moment));
  617. working_dialog.setIndeterminate(true);
  618. working_dialog.setCancelable(false);
  619. dialog = working_dialog;
  620. break;
  621. }
  622. case DIALOG_CHOOSE_UPLOAD_SOURCE: {
  623. final String [] items = { getString(R.string.actionbar_upload_files),
  624. getString(R.string.actionbar_upload_from_apps) };
  625. builder = new AlertDialog.Builder(this);
  626. builder.setTitle(R.string.actionbar_upload);
  627. builder.setItems(items, new DialogInterface.OnClickListener() {
  628. public void onClick(DialogInterface dialog, int item) {
  629. if (item == 0) {
  630. //if (!mDualPane) {
  631. Intent action = new Intent(FileDisplayActivity.this, UploadFilesActivity.class);
  632. action.putExtra(UploadFilesActivity.EXTRA_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(FileDisplayActivity.this));
  633. startActivityForResult(action, ACTION_SELECT_MULTIPLE_FILES);
  634. //} else {
  635. // TODO create and handle new fragment LocalFileListFragment
  636. //}
  637. } else if (item == 1) {
  638. Intent action = new Intent(Intent.ACTION_GET_CONTENT);
  639. action = action.setType("*/*")
  640. .addCategory(Intent.CATEGORY_OPENABLE);
  641. startActivityForResult(
  642. Intent.createChooser(action, getString(R.string.upload_chooser_title)),
  643. ACTION_SELECT_CONTENT_FROM_APPS);
  644. }
  645. }
  646. });
  647. dialog = builder.create();
  648. break;
  649. }
  650. case DIALOG_SSL_VALIDATOR: {
  651. dialog = SslValidatorDialog.newInstance(this, mLastSslUntrustedServerResult, this);
  652. break;
  653. }
  654. case DIALOG_CERT_NOT_SAVED: {
  655. builder = new AlertDialog.Builder(this);
  656. builder.setMessage(getResources().getString(R.string.ssl_validator_not_saved));
  657. builder.setCancelable(false);
  658. builder.setPositiveButton(R.string.common_ok, new DialogInterface.OnClickListener() {
  659. @Override
  660. public void onClick(DialogInterface dialog, int which) {
  661. dialog.dismiss();
  662. };
  663. });
  664. dialog = builder.create();
  665. break;
  666. }
  667. default:
  668. dialog = null;
  669. }
  670. return dialog;
  671. }
  672. /**
  673. * Translates a content URI of an image to a physical path
  674. * on the disk
  675. * @param uri The URI to resolve
  676. * @return The path to the image or null if it could not be found
  677. */
  678. public String getPath(Uri uri) {
  679. String[] projection = { MediaStore.Images.Media.DATA };
  680. Cursor cursor = managedQuery(uri, projection, null, null, null);
  681. if (cursor != null) {
  682. int column_index = cursor
  683. .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  684. cursor.moveToFirst();
  685. return cursor.getString(column_index);
  686. }
  687. return null;
  688. }
  689. /**
  690. * Pushes a directory to the drop down list
  691. * @param directory to push
  692. * @throws IllegalArgumentException If the {@link OCFile#isDirectory()} returns false.
  693. */
  694. public void pushDirname(OCFile directory) {
  695. if(!directory.isDirectory()){
  696. throw new IllegalArgumentException("Only directories may be pushed!");
  697. }
  698. mDirectories.insert(directory.getFileName(), 0);
  699. mCurrentDir = directory;
  700. }
  701. /**
  702. * Pops a directory name from the drop down list
  703. * @return True, unless the stack is empty
  704. */
  705. public boolean popDirname() {
  706. mDirectories.remove(mDirectories.getItem(0));
  707. return !mDirectories.isEmpty();
  708. }
  709. private class DirectoryCreator implements Runnable {
  710. private String mTargetPath;
  711. private Account mAccount;
  712. private Handler mHandler;
  713. public DirectoryCreator(String targetPath, Account account, Handler handler) {
  714. mTargetPath = targetPath;
  715. mAccount = account;
  716. mHandler = handler;
  717. }
  718. @Override
  719. public void run() {
  720. WebdavClient wdc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getApplicationContext());
  721. boolean created = wdc.createDirectory(mTargetPath);
  722. if (created) {
  723. mHandler.post(new Runnable() {
  724. @Override
  725. public void run() {
  726. dismissDialog(DIALOG_SHORT_WAIT);
  727. // Save new directory in local database
  728. OCFile newDir = new OCFile(mTargetPath);
  729. newDir.setMimetype("DIR");
  730. newDir.setParentId(mCurrentDir.getFileId());
  731. mStorageManager.saveFile(newDir);
  732. // Display the new folder right away
  733. mFileList.listDirectory();
  734. }
  735. });
  736. } else {
  737. mHandler.post(new Runnable() {
  738. @Override
  739. public void run() {
  740. dismissDialog(DIALOG_SHORT_WAIT);
  741. try {
  742. Toast msg = Toast.makeText(FileDisplayActivity.this, R.string.create_dir_fail_msg, Toast.LENGTH_LONG);
  743. msg.show();
  744. } catch (NotFoundException e) {
  745. Log.e(TAG, "Error while trying to show fail message " , e);
  746. }
  747. }
  748. });
  749. }
  750. }
  751. }
  752. // Custom array adapter to override text colors
  753. private class CustomArrayAdapter<T> extends ArrayAdapter<T> {
  754. public CustomArrayAdapter(FileDisplayActivity ctx, int view) {
  755. super(ctx, view);
  756. }
  757. public View getView(int position, View convertView, ViewGroup parent) {
  758. View v = super.getView(position, convertView, parent);
  759. ((TextView) v).setTextColor(getResources().getColorStateList(
  760. android.R.color.white));
  761. return v;
  762. }
  763. public View getDropDownView(int position, View convertView,
  764. ViewGroup parent) {
  765. View v = super.getDropDownView(position, convertView, parent);
  766. ((TextView) v).setTextColor(getResources().getColorStateList(
  767. android.R.color.white));
  768. return v;
  769. }
  770. }
  771. private class SyncBroadcastReceiver extends BroadcastReceiver {
  772. /**
  773. * {@link BroadcastReceiver} to enable syncing feedback in UI
  774. */
  775. @Override
  776. public void onReceive(Context context, Intent intent) {
  777. boolean inProgress = intent.getBooleanExtra(
  778. FileSyncService.IN_PROGRESS, false);
  779. String accountName = intent
  780. .getStringExtra(FileSyncService.ACCOUNT_NAME);
  781. Log.d("FileDisplay", "sync of account " + accountName
  782. + " is in_progress: " + inProgress);
  783. if (accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name)) {
  784. String synchFolderRemotePath = intent.getStringExtra(FileSyncService.SYNC_FOLDER_REMOTE_PATH);
  785. boolean fillBlankRoot = false;
  786. if (mCurrentDir == null) {
  787. mCurrentDir = mStorageManager.getFileByPath("/");
  788. fillBlankRoot = (mCurrentDir != null);
  789. }
  790. if ((synchFolderRemotePath != null && mCurrentDir != null && (mCurrentDir.getRemotePath().equals(synchFolderRemotePath)))
  791. || fillBlankRoot ) {
  792. if (!fillBlankRoot)
  793. mCurrentDir = getStorageManager().getFileByPath(synchFolderRemotePath);
  794. OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager()
  795. .findFragmentById(R.id.fileList);
  796. if (fileListFragment != null) {
  797. fileListFragment.listDirectory(mCurrentDir);
  798. }
  799. }
  800. setSupportProgressBarIndeterminateVisibility(inProgress);
  801. removeStickyBroadcast(intent);
  802. }
  803. RemoteOperationResult synchResult = (RemoteOperationResult)intent.getSerializableExtra(FileSyncService.SYNC_RESULT);
  804. if (synchResult != null) {
  805. if (synchResult.getCode().equals(RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED)) {
  806. mLastSslUntrustedServerResult = synchResult;
  807. showDialog(DIALOG_SSL_VALIDATOR);
  808. }
  809. }
  810. }
  811. }
  812. private class UploadFinishReceiver extends BroadcastReceiver {
  813. /**
  814. * Once the file upload has finished -> update view
  815. * @author David A. Velasco
  816. * {@link BroadcastReceiver} to enable upload feedback in UI
  817. */
  818. @Override
  819. public void onReceive(Context context, Intent intent) {
  820. String uploadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
  821. String accountName = intent.getStringExtra(FileUploader.ACCOUNT_NAME);
  822. boolean sameAccount = accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name);
  823. boolean isDescendant = (mCurrentDir != null) && (uploadedRemotePath != null) && (uploadedRemotePath.startsWith(mCurrentDir.getRemotePath()));
  824. if (sameAccount && isDescendant) {
  825. OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);
  826. if (fileListFragment != null) {
  827. fileListFragment.listDirectory();
  828. }
  829. }
  830. }
  831. }
  832. /**
  833. * Once the file download has finished -> update view
  834. */
  835. private class DownloadFinishReceiver extends BroadcastReceiver {
  836. @Override
  837. public void onReceive(Context context, Intent intent) {
  838. String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
  839. String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);
  840. boolean sameAccount = accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name);
  841. boolean isDescendant = (mCurrentDir != null) && (downloadedRemotePath != null) && (downloadedRemotePath.startsWith(mCurrentDir.getRemotePath()));
  842. if (sameAccount && isDescendant) {
  843. OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);
  844. if (fileListFragment != null) {
  845. fileListFragment.listDirectory();
  846. }
  847. }
  848. }
  849. }
  850. /**
  851. * {@inheritDoc}
  852. */
  853. @Override
  854. public DataStorageManager getStorageManager() {
  855. return mStorageManager;
  856. }
  857. /**
  858. * {@inheritDoc}
  859. */
  860. @Override
  861. public void onDirectoryClick(OCFile directory) {
  862. pushDirname(directory);
  863. ActionBar actionBar = getSupportActionBar();
  864. actionBar.setDisplayHomeAsUpEnabled(true);
  865. if (mDualPane) {
  866. // Resets the FileDetailsFragment on Tablets so that it always displays
  867. Fragment fileFragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
  868. if (fileFragment != null && (fileFragment instanceof FilePreviewFragment || !((FileDetailFragment) fileFragment).isEmpty())) {
  869. FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  870. transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null), FileDetailFragment.FTAG); // empty FileDetailFragment
  871. transaction.commit();
  872. }
  873. }
  874. }
  875. /**
  876. * {@inheritDoc}
  877. */
  878. @Override
  879. public void onFileClick(OCFile file) {
  880. // If we are on a large device -> update fragment
  881. if (mDualPane) {
  882. // buttons in the details view are problematic when trying to reuse an existing fragment; create always a new one solves some of them, BUT no all; downloads are 'dangerous'
  883. FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  884. if (file != null && file.isDown() && FilePreviewFragment.canBePreviewed(file)) {
  885. transaction.replace(R.id.file_details_container, new FilePreviewFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);
  886. } else {
  887. transaction.replace(R.id.file_details_container, new FileDetailFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);
  888. }
  889. //transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
  890. transaction.commit();
  891. } else { // small or medium screen device -> new Activity
  892. Intent showDetailsIntent = new Intent(this, FileDetailActivity.class);
  893. showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, file);
  894. showDetailsIntent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(this));
  895. startActivity(showDetailsIntent);
  896. }
  897. }
  898. /**
  899. * {@inheritDoc}
  900. */
  901. @Override
  902. public OCFile getInitialDirectory() {
  903. return mCurrentDir;
  904. }
  905. /**
  906. * {@inheritDoc}
  907. */
  908. @Override
  909. public void onFileStateChanged() {
  910. OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);
  911. if (fileListFragment != null) {
  912. fileListFragment.listDirectory();
  913. }
  914. }
  915. /**
  916. * {@inheritDoc}
  917. */
  918. @Override
  919. public FileDownloaderBinder getFileDownloaderBinder() {
  920. return mDownloaderBinder;
  921. }
  922. /**
  923. * {@inheritDoc}
  924. */
  925. @Override
  926. public FileUploaderBinder getFileUploaderBinder() {
  927. return mUploaderBinder;
  928. }
  929. /** Defines callbacks for service binding, passed to bindService() */
  930. private class ListServiceConnection implements ServiceConnection {
  931. @Override
  932. public void onServiceConnected(ComponentName component, IBinder service) {
  933. if (component.equals(new ComponentName(FileDisplayActivity.this, FileDownloader.class))) {
  934. Log.d(TAG, "Download service connected");
  935. mDownloaderBinder = (FileDownloaderBinder) service;
  936. } else if (component.equals(new ComponentName(FileDisplayActivity.this, FileUploader.class))) {
  937. Log.d(TAG, "Upload service connected");
  938. mUploaderBinder = (FileUploaderBinder) service;
  939. } else {
  940. return;
  941. }
  942. // a new chance to get the mDownloadBinder through getFileDownloadBinder() - THIS IS A MESS
  943. if (mFileList != null)
  944. mFileList.listDirectory();
  945. if (mDualPane) {
  946. Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
  947. if (fragment != null && fragment instanceof FileDetailFragment) {
  948. ((FileDetailFragment)fragment).updateFileDetails(false);
  949. }
  950. }
  951. }
  952. @Override
  953. public void onServiceDisconnected(ComponentName component) {
  954. if (component.equals(new ComponentName(FileDisplayActivity.this, FileDownloader.class))) {
  955. Log.d(TAG, "Download service disconnected");
  956. mDownloaderBinder = null;
  957. } else if (component.equals(new ComponentName(FileDisplayActivity.this, FileUploader.class))) {
  958. Log.d(TAG, "Upload service disconnected");
  959. mUploaderBinder = null;
  960. }
  961. }
  962. };
  963. /**
  964. * Launch an intent to request the PIN code to the user before letting him use the app
  965. */
  966. private void requestPinCode() {
  967. boolean pinStart = false;
  968. SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  969. pinStart = appPrefs.getBoolean("set_pincode", false);
  970. if (pinStart) {
  971. Intent i = new Intent(getApplicationContext(), PinCodeActivity.class);
  972. i.putExtra(PinCodeActivity.EXTRA_ACTIVITY, "FileDisplayActivity");
  973. startActivity(i);
  974. }
  975. }
  976. @Override
  977. public void onSavedCertificate() {
  978. startSynchronization();
  979. }
  980. @Override
  981. public void onFailedSavingCertificate() {
  982. showDialog(DIALOG_CERT_NOT_SAVED);
  983. }
  984. /**
  985. * Updates the view associated to the activity after the finish of some operation over files
  986. * in the current account.
  987. *
  988. * @param operation Removal operation performed.
  989. * @param result Result of the removal.
  990. */
  991. @Override
  992. public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
  993. if (operation instanceof RemoveFileOperation) {
  994. onRemoveFileOperationFinish((RemoveFileOperation)operation, result);
  995. } else if (operation instanceof RenameFileOperation) {
  996. onRenameFileOperationFinish((RenameFileOperation)operation, result);
  997. } else if (operation instanceof SynchronizeFileOperation) {
  998. onSynchronizeFileOperationFinish((SynchronizeFileOperation)operation, result);
  999. }
  1000. }
  1001. /**
  1002. * Updates the view associated to the activity after the finish of an operation trying to remove a
  1003. * file.
  1004. *
  1005. * @param operation Removal operation performed.
  1006. * @param result Result of the removal.
  1007. */
  1008. private void onRemoveFileOperationFinish(RemoveFileOperation operation, RemoteOperationResult result) {
  1009. dismissDialog(DIALOG_SHORT_WAIT);
  1010. if (result.isSuccess()) {
  1011. Toast msg = Toast.makeText(this, R.string.remove_success_msg, Toast.LENGTH_LONG);
  1012. msg.show();
  1013. OCFile removedFile = operation.getFile();
  1014. if (mDualPane) {
  1015. FileFragment details = (FileFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
  1016. if (details != null && removedFile.equals(details.getFile())) {
  1017. FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  1018. transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null)); // empty FileDetailFragment
  1019. transaction.commit();
  1020. }
  1021. }
  1022. if (mStorageManager.getFileById(removedFile.getParentId()).equals(mCurrentDir)) {
  1023. mFileList.listDirectory();
  1024. }
  1025. } else {
  1026. Toast msg = Toast.makeText(this, R.string.remove_fail_msg, Toast.LENGTH_LONG);
  1027. msg.show();
  1028. if (result.isSslRecoverableException()) {
  1029. mLastSslUntrustedServerResult = result;
  1030. showDialog(DIALOG_SSL_VALIDATOR);
  1031. }
  1032. }
  1033. }
  1034. /**
  1035. * Updates the view associated to the activity after the finish of an operation trying to rename a
  1036. * file.
  1037. *
  1038. * @param operation Renaming operation performed.
  1039. * @param result Result of the renaming.
  1040. */
  1041. private void onRenameFileOperationFinish(RenameFileOperation operation, RemoteOperationResult result) {
  1042. dismissDialog(DIALOG_SHORT_WAIT);
  1043. OCFile renamedFile = operation.getFile();
  1044. if (result.isSuccess()) {
  1045. if (mDualPane) {
  1046. FileFragment details = (FileFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
  1047. if (details != null && details instanceof FileDetailFragment && renamedFile.equals(details.getFile()) ) {
  1048. ((FileDetailFragment) details).updateFileDetails(renamedFile, AccountUtils.getCurrentOwnCloudAccount(this));
  1049. }
  1050. }
  1051. if (mStorageManager.getFileById(renamedFile.getParentId()).equals(mCurrentDir)) {
  1052. mFileList.listDirectory();
  1053. }
  1054. } else {
  1055. if (result.getCode().equals(ResultCode.INVALID_LOCAL_FILE_NAME)) {
  1056. Toast msg = Toast.makeText(this, R.string.rename_local_fail_msg, Toast.LENGTH_LONG);
  1057. msg.show();
  1058. // TODO throw again the new rename dialog
  1059. } else {
  1060. Toast msg = Toast.makeText(this, R.string.rename_server_fail_msg, Toast.LENGTH_LONG);
  1061. msg.show();
  1062. if (result.isSslRecoverableException()) {
  1063. mLastSslUntrustedServerResult = result;
  1064. showDialog(DIALOG_SSL_VALIDATOR);
  1065. }
  1066. }
  1067. }
  1068. }
  1069. private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation, RemoteOperationResult result) {
  1070. dismissDialog(DIALOG_SHORT_WAIT);
  1071. OCFile syncedFile = operation.getLocalFile();
  1072. if (!result.isSuccess()) {
  1073. if (result.getCode() == ResultCode.SYNC_CONFLICT) {
  1074. Intent i = new Intent(this, ConflictsResolveActivity.class);
  1075. i.putExtra(ConflictsResolveActivity.EXTRA_FILE, syncedFile);
  1076. i.putExtra(ConflictsResolveActivity.EXTRA_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(this));
  1077. startActivity(i);
  1078. } else {
  1079. Toast msg = Toast.makeText(this, R.string.sync_file_fail_msg, Toast.LENGTH_LONG);
  1080. msg.show();
  1081. }
  1082. } else {
  1083. if (operation.transferWasRequested()) {
  1084. mFileList.listDirectory();
  1085. onTransferStateChanged(syncedFile, true, true);
  1086. } else {
  1087. Toast msg = Toast.makeText(this, R.string.sync_file_nothing_to_do_msg, Toast.LENGTH_LONG);
  1088. msg.show();
  1089. }
  1090. }
  1091. }
  1092. /**
  1093. * {@inheritDoc}
  1094. */
  1095. @Override
  1096. public void onTransferStateChanged(OCFile file, boolean downloading, boolean uploading) {
  1097. /*OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);
  1098. if (fileListFragment != null) {
  1099. fileListFragment.listDirectory();
  1100. }*/
  1101. if (mDualPane) {
  1102. FileFragment details = (FileFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
  1103. if (details != null && details instanceof FileDetailFragment && file.equals(details.getFile()) ) {
  1104. if (downloading || uploading) {
  1105. ((FileDetailFragment)details).updateFileDetails(file, AccountUtils.getCurrentOwnCloudAccount(this));
  1106. } else {
  1107. ((FileDetailFragment)details).updateFileDetails(downloading || uploading);
  1108. }
  1109. }
  1110. }
  1111. }
  1112. @Override
  1113. public void showFragmentWithDetails(OCFile file) {
  1114. if (mDualPane) {
  1115. FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  1116. transaction.replace(R.id.file_details_container, new FileDetailFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);
  1117. transaction.commit();
  1118. } else {
  1119. Intent showDetailsIntent = new Intent(this, FileDetailActivity.class);
  1120. showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, file);
  1121. showDetailsIntent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(this));
  1122. showDetailsIntent.putExtra(FileDetailActivity.EXTRA_MODE, FileDetailActivity.MODE_DETAILS);
  1123. startActivity(showDetailsIntent);
  1124. }
  1125. }
  1126. }