Uploader.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012 Bartek Przybylski
  3. * Copyright (C) 2012-2013 ownCloud Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2,
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. package com.owncloud.android.ui.activity;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.LinkedList;
  24. import java.util.List;
  25. import java.util.Stack;
  26. import java.util.Vector;
  27. import com.owncloud.android.MainApp;
  28. import com.owncloud.android.R;
  29. import com.owncloud.android.authentication.AccountAuthenticator;
  30. import com.owncloud.android.authentication.AuthenticatorActivity;
  31. import com.owncloud.android.datamodel.FileDataStorageManager;
  32. import com.owncloud.android.datamodel.OCFile;
  33. import com.owncloud.android.files.services.FileUploader;
  34. import com.owncloud.android.lib.common.OwnCloudAccount;
  35. import com.owncloud.android.lib.common.OwnCloudClient;
  36. import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
  37. import com.owncloud.android.lib.common.OwnCloudCredentials;
  38. import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
  39. import com.owncloud.android.lib.common.operations.RemoteOperation;
  40. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  41. import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
  42. import com.owncloud.android.lib.common.utils.Log_OC;
  43. import com.owncloud.android.operations.SynchronizeFolderOperation;
  44. import android.accounts.Account;
  45. import android.accounts.AccountManager;
  46. import android.accounts.AuthenticatorException;
  47. import android.accounts.OperationCanceledException;
  48. import android.app.AlertDialog;
  49. import android.app.AlertDialog.Builder;
  50. import android.app.Dialog;
  51. import android.app.ProgressDialog;
  52. import android.content.BroadcastReceiver;
  53. import android.content.Context;
  54. import android.content.DialogInterface;
  55. import android.content.IntentFilter;
  56. import android.content.DialogInterface.OnCancelListener;
  57. import android.content.DialogInterface.OnClickListener;
  58. import android.content.Intent;
  59. import android.content.SharedPreferences;
  60. import android.database.Cursor;
  61. import android.net.Uri;
  62. import android.os.Bundle;
  63. import android.os.Parcelable;
  64. import android.preference.PreferenceManager;
  65. import android.provider.MediaStore.Audio;
  66. import android.provider.MediaStore.Images;
  67. import android.provider.MediaStore.Video;
  68. import android.view.View;
  69. import android.widget.AdapterView;
  70. import android.widget.AdapterView.OnItemClickListener;
  71. import android.widget.Button;
  72. import android.widget.EditText;
  73. import android.widget.SimpleAdapter;
  74. import android.widget.Toast;
  75. import com.actionbarsherlock.app.ActionBar;
  76. import com.actionbarsherlock.app.SherlockListActivity;
  77. import com.actionbarsherlock.view.MenuItem;
  78. import com.owncloud.android.syncadapter.FileSyncAdapter;
  79. import com.owncloud.android.ui.fragment.OCFileListFragment;
  80. import com.owncloud.android.utils.DisplayUtils;
  81. /**
  82. * This can be used to upload things to an ownCloud instance.
  83. *
  84. * @author Bartek Przybylski
  85. *
  86. */
  87. public class Uploader extends SherlockListActivity implements OnItemClickListener, android.view.View.OnClickListener {
  88. private static final String TAG = "ownCloudUploader";
  89. private Account mAccount;
  90. private AccountManager mAccountManager;
  91. private Stack<String> mParents;
  92. private ArrayList<Parcelable> mStreamsToUpload;
  93. private boolean mCreateDir;
  94. private String mUploadPath;
  95. private FileDataStorageManager mStorageManager;
  96. private OCFile mFile;
  97. private SyncBroadcastReceiver mSyncBroadcastReceiver;
  98. private boolean mSyncInProgress = false;
  99. private final static int DIALOG_NO_ACCOUNT = 0;
  100. private final static int DIALOG_WAITING = 1;
  101. private final static int DIALOG_NO_STREAM = 2;
  102. private final static int DIALOG_MULTIPLE_ACCOUNT = 3;
  103. private final static int REQUEST_CODE_SETUP_ACCOUNT = 0;
  104. @Override
  105. protected void onCreate(Bundle savedInstanceState) {
  106. super.onCreate(savedInstanceState);
  107. mParents = new Stack<String>();
  108. ActionBar actionBar = getSupportActionBar();
  109. actionBar.setIcon(DisplayUtils.getSeasonalIconId());
  110. if (prepareStreamsToUpload()) {
  111. mAccountManager = (AccountManager) getSystemService(Context.ACCOUNT_SERVICE);
  112. Account[] accounts = mAccountManager.getAccountsByType(MainApp.getAccountType());
  113. if (accounts.length == 0) {
  114. Log_OC.i(TAG, "No ownCloud account is available");
  115. showDialog(DIALOG_NO_ACCOUNT);
  116. } else if (accounts.length > 1) {
  117. Log_OC.i(TAG, "More then one ownCloud is available");
  118. showDialog(DIALOG_MULTIPLE_ACCOUNT);
  119. } else {
  120. mAccount = accounts[0];
  121. mStorageManager = new FileDataStorageManager(mAccount, getContentResolver());
  122. initTargetFolder();
  123. populateDirectoryList();
  124. }
  125. } else {
  126. showDialog(DIALOG_NO_STREAM);
  127. }
  128. // Listen for sync messages
  129. IntentFilter syncIntentFilter = new IntentFilter(SynchronizeFolderOperation.
  130. EVENT_SINGLE_FOLDER_CONTENTS_SYNCED);
  131. syncIntentFilter.addAction(SynchronizeFolderOperation.EVENT_SINGLE_FOLDER_SHARES_SYNCED);
  132. mSyncBroadcastReceiver = new SyncBroadcastReceiver();
  133. registerReceiver(mSyncBroadcastReceiver, syncIntentFilter);
  134. }
  135. @Override
  136. protected Dialog onCreateDialog(final int id) {
  137. final AlertDialog.Builder builder = new Builder(this);
  138. switch (id) {
  139. case DIALOG_WAITING:
  140. ProgressDialog pDialog = new ProgressDialog(this);
  141. pDialog.setIndeterminate(false);
  142. pDialog.setCancelable(false);
  143. pDialog.setMessage(getResources().getString(R.string.uploader_info_uploading));
  144. return pDialog;
  145. case DIALOG_NO_ACCOUNT:
  146. builder.setIcon(android.R.drawable.ic_dialog_alert);
  147. builder.setTitle(R.string.uploader_wrn_no_account_title);
  148. builder.setMessage(String.format(getString(R.string.uploader_wrn_no_account_text),
  149. getString(R.string.app_name)));
  150. builder.setCancelable(false);
  151. builder.setPositiveButton(R.string.uploader_wrn_no_account_setup_btn_text, new OnClickListener() {
  152. @Override
  153. public void onClick(DialogInterface dialog, int which) {
  154. if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.ECLAIR_MR1) {
  155. // using string value since in API7 this
  156. // constant is not defined
  157. // in API7 < this constant is defined in
  158. // Settings.ADD_ACCOUNT_SETTINGS
  159. // and Settings.EXTRA_AUTHORITIES
  160. Intent intent = new Intent(android.provider.Settings.ACTION_ADD_ACCOUNT);
  161. intent.putExtra("authorities", new String[] { MainApp.getAuthTokenType() });
  162. startActivityForResult(intent, REQUEST_CODE_SETUP_ACCOUNT);
  163. } else {
  164. // since in API7 there is no direct call for
  165. // account setup, so we need to
  166. // show our own AccountSetupAcricity, get
  167. // desired results and setup
  168. // everything for ourself
  169. Intent intent = new Intent(getBaseContext(), AccountAuthenticator.class);
  170. startActivityForResult(intent, REQUEST_CODE_SETUP_ACCOUNT);
  171. }
  172. }
  173. });
  174. builder.setNegativeButton(R.string.uploader_wrn_no_account_quit_btn_text, new OnClickListener() {
  175. @Override
  176. public void onClick(DialogInterface dialog, int which) {
  177. finish();
  178. }
  179. });
  180. return builder.create();
  181. case DIALOG_MULTIPLE_ACCOUNT:
  182. CharSequence ac[] = new CharSequence[mAccountManager.getAccountsByType(MainApp.getAccountType()).length];
  183. for (int i = 0; i < ac.length; ++i) {
  184. ac[i] = DisplayUtils.convertIdn(mAccountManager.getAccountsByType(MainApp.getAccountType())[i].name,
  185. false);
  186. }
  187. builder.setTitle(R.string.common_choose_account);
  188. builder.setItems(ac, new OnClickListener() {
  189. @Override
  190. public void onClick(DialogInterface dialog, int which) {
  191. mAccount = mAccountManager.getAccountsByType(MainApp.getAccountType())[which];
  192. mStorageManager = new FileDataStorageManager(mAccount, getContentResolver());
  193. initTargetFolder();
  194. populateDirectoryList();
  195. }
  196. });
  197. builder.setCancelable(true);
  198. builder.setOnCancelListener(new OnCancelListener() {
  199. @Override
  200. public void onCancel(DialogInterface dialog) {
  201. dialog.cancel();
  202. finish();
  203. }
  204. });
  205. return builder.create();
  206. case DIALOG_NO_STREAM:
  207. builder.setIcon(android.R.drawable.ic_dialog_alert);
  208. builder.setTitle(R.string.uploader_wrn_no_content_title);
  209. builder.setMessage(R.string.uploader_wrn_no_content_text);
  210. builder.setCancelable(false);
  211. builder.setNegativeButton(R.string.common_cancel, new OnClickListener() {
  212. @Override
  213. public void onClick(DialogInterface dialog, int which) {
  214. finish();
  215. }
  216. });
  217. return builder.create();
  218. default:
  219. throw new IllegalArgumentException("Unknown dialog id: " + id);
  220. }
  221. }
  222. class a implements OnClickListener {
  223. String mPath;
  224. EditText mDirname;
  225. public a(String path, EditText dirname) {
  226. mPath = path;
  227. mDirname = dirname;
  228. }
  229. @Override
  230. public void onClick(DialogInterface dialog, int which) {
  231. Uploader.this.mUploadPath = mPath + mDirname.getText().toString();
  232. Uploader.this.mCreateDir = true;
  233. uploadFiles();
  234. }
  235. }
  236. @Override
  237. public void onBackPressed() {
  238. if (mParents.size() <= 1) {
  239. unregisterReceiver(mSyncBroadcastReceiver);
  240. super.onBackPressed();
  241. return;
  242. } else {
  243. mParents.pop();
  244. String full_path = generatePath(mParents);
  245. startSyncFolderOperation(mStorageManager.getFileByPath(full_path));
  246. populateDirectoryList();
  247. }
  248. }
  249. @Override
  250. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  251. // click on folder in the list
  252. Log_OC.d(TAG, "on item click");
  253. Vector<OCFile> tmpfiles = mStorageManager.getFolderContent(mFile);
  254. if (tmpfiles.size() <= 0) return;
  255. // filter on dirtype
  256. Vector<OCFile> files = new Vector<OCFile>();
  257. for (OCFile f : tmpfiles)
  258. files.add(f);
  259. if (files.size() < position) {
  260. throw new IndexOutOfBoundsException("Incorrect item selected");
  261. }
  262. if (files.get(position).isFolder()){
  263. OCFile folderToEnter = files.get(position);
  264. startSyncFolderOperation(folderToEnter);
  265. mParents.push(folderToEnter.getFileName());
  266. populateDirectoryList();
  267. }
  268. }
  269. @Override
  270. public void onClick(View v) {
  271. // click on button
  272. switch (v.getId()) {
  273. case R.id.uploader_choose_folder:
  274. mUploadPath = ""; // first element in mParents is root dir, represented by "";
  275. // init mUploadPath with "/" results in a "//" prefix
  276. for (String p : mParents)
  277. mUploadPath += p + OCFile.PATH_SEPARATOR;
  278. Log_OC.d(TAG, "Uploading file to dir " + mUploadPath);
  279. uploadFiles();
  280. break;
  281. default:
  282. throw new IllegalArgumentException("Wrong element clicked");
  283. }
  284. }
  285. @Override
  286. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  287. super.onActivityResult(requestCode, resultCode, data);
  288. Log_OC.i(TAG, "result received. req: " + requestCode + " res: " + resultCode);
  289. if (requestCode == REQUEST_CODE_SETUP_ACCOUNT) {
  290. dismissDialog(DIALOG_NO_ACCOUNT);
  291. if (resultCode == RESULT_CANCELED) {
  292. finish();
  293. }
  294. Account[] accounts = mAccountManager.getAccountsByType(MainApp.getAuthTokenType());
  295. if (accounts.length == 0) {
  296. showDialog(DIALOG_NO_ACCOUNT);
  297. } else {
  298. // there is no need for checking for is there more then one
  299. // account at this point
  300. // since account setup can set only one account at time
  301. mAccount = accounts[0];
  302. populateDirectoryList();
  303. }
  304. }
  305. }
  306. private void populateDirectoryList() {
  307. setContentView(R.layout.uploader_layout);
  308. String current_dir = mParents.peek();
  309. if(current_dir.equals("")){
  310. getSupportActionBar().setTitle(getString(R.string.default_display_name_for_root_folder));
  311. }
  312. else{
  313. getSupportActionBar().setTitle(current_dir);
  314. }
  315. boolean notRoot = (mParents.size() > 1);
  316. ActionBar actionBar = getSupportActionBar();
  317. actionBar.setDisplayHomeAsUpEnabled(notRoot);
  318. actionBar.setHomeButtonEnabled(notRoot);
  319. String full_path = generatePath(mParents);
  320. Log_OC.d(TAG, "Populating view with content of : " + full_path);
  321. mFile = mStorageManager.getFileByPath(full_path);
  322. Log_OC.d(TAG, "directory exists: " + (mFile != null));
  323. if (mFile != null) {
  324. Vector<OCFile> files = mStorageManager.getFolderContent(mFile);
  325. List<HashMap<String, OCFile>> data = new LinkedList<HashMap<String,OCFile>>();
  326. for (OCFile f : files) {
  327. HashMap<String, OCFile> h = new HashMap<String, OCFile>();
  328. h.put("dirname", f);
  329. data.add(h);
  330. }
  331. ImageSimpleAdapter sa = new ImageSimpleAdapter(this,
  332. data,
  333. R.layout.uploader_list_item_layout,
  334. new String[] {},
  335. new int[] {}, mStorageManager, mAccount);
  336. setListAdapter(sa);
  337. Button btn = (Button) findViewById(R.id.uploader_choose_folder);
  338. btn.setOnClickListener(this);
  339. getListView().setOnItemClickListener(this);
  340. }
  341. }
  342. public void startSyncFolderOperation(OCFile folder) {
  343. long currentSyncTime = System.currentTimeMillis();
  344. mSyncInProgress = true;
  345. // perform folder synchronization
  346. RemoteOperation synchFolderOp = new SynchronizeFolderOperation( folder,
  347. currentSyncTime,
  348. false,
  349. false,
  350. false,
  351. mStorageManager,
  352. mAccount,
  353. getApplicationContext()
  354. );
  355. synchFolderOp.execute(mAccount, this, null, null);
  356. setSupportProgressBarIndeterminateVisibility(true);
  357. }
  358. private String generatePath(Stack<String> dirs) {
  359. String full_path = "";
  360. for (String a : dirs)
  361. full_path += a + "/";
  362. return full_path;
  363. }
  364. private boolean prepareStreamsToUpload() {
  365. if (getIntent().getAction().equals(Intent.ACTION_SEND)) {
  366. mStreamsToUpload = new ArrayList<Parcelable>();
  367. mStreamsToUpload.add(getIntent().getParcelableExtra(Intent.EXTRA_STREAM));
  368. } else if (getIntent().getAction().equals(Intent.ACTION_SEND_MULTIPLE)) {
  369. mStreamsToUpload = getIntent().getParcelableArrayListExtra(Intent.EXTRA_STREAM);
  370. }
  371. return (mStreamsToUpload != null && mStreamsToUpload.get(0) != null);
  372. }
  373. public void uploadFiles() {
  374. try {
  375. ArrayList<String> local = new ArrayList<String>();
  376. ArrayList<String> remote = new ArrayList<String>();
  377. // this checks the mimeType
  378. for (Parcelable mStream : mStreamsToUpload) {
  379. Uri uri = (Uri) mStream;
  380. if (uri !=null) {
  381. if (uri.getScheme().equals("content")) {
  382. String mimeType = getContentResolver().getType(uri);
  383. if (mimeType.contains("image")) {
  384. String[] CONTENT_PROJECTION = { Images.Media.DATA, Images.Media.DISPLAY_NAME,
  385. Images.Media.MIME_TYPE, Images.Media.SIZE};
  386. Cursor c = getContentResolver().query(uri, CONTENT_PROJECTION, null, null, null);
  387. c.moveToFirst();
  388. int index = c.getColumnIndex(Images.Media.DATA);
  389. String data = c.getString(index);
  390. local.add(data);
  391. remote.add(mUploadPath + c.getString(c.getColumnIndex(Images.Media.DISPLAY_NAME)));
  392. }
  393. else if (mimeType.contains("video")) {
  394. String[] CONTENT_PROJECTION = { Video.Media.DATA, Video.Media.DISPLAY_NAME,
  395. Video.Media.MIME_TYPE, Video.Media.SIZE,
  396. Video.Media.DATE_MODIFIED };
  397. Cursor c = getContentResolver().query(uri, CONTENT_PROJECTION, null, null, null);
  398. c.moveToFirst();
  399. int index = c.getColumnIndex(Video.Media.DATA);
  400. String data = c.getString(index);
  401. local.add(data);
  402. remote.add(mUploadPath + c.getString(c.getColumnIndex(Video.Media.DISPLAY_NAME)));
  403. }
  404. else if (mimeType.contains("audio")) {
  405. String[] CONTENT_PROJECTION = { Audio.Media.DATA, Audio.Media.DISPLAY_NAME,
  406. Audio.Media.MIME_TYPE, Audio.Media.SIZE };
  407. Cursor c = getContentResolver().query(uri, CONTENT_PROJECTION, null, null, null);
  408. c.moveToFirst();
  409. int index = c.getColumnIndex(Audio.Media.DATA);
  410. String data = c.getString(index);
  411. local.add(data);
  412. remote.add(mUploadPath + c.getString(c.getColumnIndex(Audio.Media.DISPLAY_NAME)));
  413. }
  414. else {
  415. String filePath = Uri.decode(uri.toString()).replace(uri.getScheme() + "://", "");
  416. // cut everything whats before mnt. It occured to me that sometimes apps send
  417. // their name into the URI
  418. if (filePath.contains("mnt")) {
  419. String splitedFilePath[] = filePath.split("/mnt");
  420. filePath = splitedFilePath[1];
  421. }
  422. final File file = new File(filePath);
  423. local.add(file.getAbsolutePath());
  424. remote.add(mUploadPath + file.getName());
  425. }
  426. } else if (uri.getScheme().equals("file")) {
  427. String filePath = Uri.decode(uri.toString()).replace(uri.getScheme() + "://", "");
  428. if (filePath.contains("mnt")) {
  429. String splitedFilePath[] = filePath.split("/mnt");
  430. filePath = splitedFilePath[1];
  431. }
  432. final File file = new File(filePath);
  433. local.add(file.getAbsolutePath());
  434. remote.add(mUploadPath + file.getName());
  435. }
  436. else {
  437. throw new SecurityException();
  438. }
  439. }
  440. else {
  441. throw new SecurityException();
  442. }
  443. Intent intent = new Intent(getApplicationContext(), FileUploader.class);
  444. intent.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_MULTIPLE_FILES);
  445. intent.putExtra(FileUploader.KEY_LOCAL_FILE, local.toArray(new String[local.size()]));
  446. intent.putExtra(FileUploader.KEY_REMOTE_FILE, remote.toArray(new String[remote.size()]));
  447. intent.putExtra(FileUploader.KEY_ACCOUNT, mAccount);
  448. startService(intent);
  449. //Save the path to shared preferences
  450. SharedPreferences.Editor appPrefs = PreferenceManager
  451. .getDefaultSharedPreferences(getApplicationContext()).edit();
  452. appPrefs.putString("last_upload_path", mUploadPath);
  453. appPrefs.apply();
  454. finish();
  455. }
  456. } catch (SecurityException e) {
  457. String message = String.format(getString(R.string.uploader_error_forbidden_content),
  458. getString(R.string.app_name));
  459. Toast.makeText(this, message, Toast.LENGTH_LONG).show();
  460. }
  461. }
  462. /**
  463. * Loads the target folder initialize shown to the user.
  464. *
  465. * The target account has to be chosen before this method is called.
  466. */
  467. private void initTargetFolder() {
  468. if (mStorageManager == null) {
  469. throw new IllegalStateException("Do not call this method before initializing mStorageManager");
  470. }
  471. SharedPreferences appPreferences = PreferenceManager
  472. .getDefaultSharedPreferences(getApplicationContext());
  473. String last_path = appPreferences.getString("last_upload_path", "");
  474. // "/" equals root-directory
  475. if(last_path.equals("/")) {
  476. mParents.add("");
  477. }
  478. else{
  479. String[] dir_names = last_path.split("/");
  480. for (String dir : dir_names)
  481. mParents.add(dir);
  482. }
  483. //Make sure that path still exists, if it doesn't pop the stack and try the previous path
  484. while(!mStorageManager.fileExists(generatePath(mParents)) && mParents.size() > 1){
  485. mParents.pop();
  486. }
  487. }
  488. @Override
  489. public boolean onOptionsItemSelected(MenuItem item) {
  490. boolean retval = true;
  491. switch (item.getItemId()) {
  492. case android.R.id.home: {
  493. if((mParents.size() > 1)) {
  494. onBackPressed();
  495. }
  496. break;
  497. }
  498. default:
  499. retval = super.onOptionsItemSelected(item);
  500. }
  501. return retval;
  502. }
  503. private OCFile getCurrentFolder(){
  504. OCFile file = mFile;
  505. if (file != null) {
  506. if (file.isFolder()) {
  507. return file;
  508. } else if (mStorageManager != null) {
  509. String parentPath = file.getRemotePath().substring(0,
  510. file.getRemotePath().lastIndexOf(file.getFileName()));
  511. return mStorageManager.getFileByPath(parentPath);
  512. }
  513. }
  514. return null;
  515. }
  516. private void browseToRoot() {
  517. OCFile root = mStorageManager.getFileByPath(OCFile.ROOT_PATH);
  518. mFile = root;
  519. startSyncFolderOperation(root);
  520. }
  521. protected void requestCredentialsUpdate() {
  522. Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
  523. updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, mAccount);
  524. updateAccountCredentials.putExtra(
  525. AuthenticatorActivity.EXTRA_ACTION,
  526. AuthenticatorActivity.ACTION_UPDATE_EXPIRED_TOKEN);
  527. updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
  528. startActivity(updateAccountCredentials);
  529. }
  530. private class SyncBroadcastReceiver extends BroadcastReceiver {
  531. /**
  532. * {@link BroadcastReceiver} to enable syncing feedback in UI
  533. */
  534. @Override
  535. public void onReceive(Context context, Intent intent) {
  536. try {
  537. String event = intent.getAction();
  538. Log_OC.d(TAG, "Received broadcast " + event);
  539. String accountName = intent.getStringExtra(FileSyncAdapter.EXTRA_ACCOUNT_NAME);
  540. String synchFolderRemotePath = intent.getStringExtra(FileSyncAdapter.EXTRA_FOLDER_PATH);
  541. RemoteOperationResult synchResult = (RemoteOperationResult)intent.getSerializableExtra(
  542. FileSyncAdapter.EXTRA_RESULT);
  543. boolean sameAccount = (mAccount != null && accountName.equals(mAccount.name)
  544. && mStorageManager != null);
  545. if (sameAccount) {
  546. if (FileSyncAdapter.EVENT_FULL_SYNC_START.equals(event)) {
  547. mSyncInProgress = true;
  548. } else {
  549. OCFile currentFile = (mFile == null) ? null :
  550. mStorageManager.getFileByPath(mFile.getRemotePath());
  551. OCFile currentDir = (getCurrentFolder() == null) ? null :
  552. mStorageManager.getFileByPath(getCurrentFolder().getRemotePath());
  553. if (currentDir == null) {
  554. // current folder was removed from the server
  555. Toast.makeText( context,
  556. String.format(
  557. getString(R.string.sync_current_folder_was_removed),
  558. getCurrentFolder().getFileName()),
  559. Toast.LENGTH_LONG)
  560. .show();
  561. browseToRoot();
  562. } else {
  563. if (currentFile == null && !mFile.isFolder()) {
  564. // currently selected file was removed in the server, and now we know it
  565. currentFile = currentDir;
  566. }
  567. if (synchFolderRemotePath != null &&
  568. currentDir.getRemotePath().equals(synchFolderRemotePath)) {
  569. populateDirectoryList();
  570. }
  571. mFile = currentFile;
  572. }
  573. mSyncInProgress = (!FileSyncAdapter.EVENT_FULL_SYNC_END.equals(event) &&
  574. !SynchronizeFolderOperation.EVENT_SINGLE_FOLDER_SHARES_SYNCED.equals(event));
  575. if (SynchronizeFolderOperation.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED.
  576. equals(event) &&
  577. /// TODO refactor and make common
  578. synchResult != null && !synchResult.isSuccess() &&
  579. (synchResult.getCode() == ResultCode.UNAUTHORIZED ||
  580. synchResult.isIdPRedirection() ||
  581. (synchResult.isException() && synchResult.getException()
  582. instanceof AuthenticatorException))) {
  583. OwnCloudClient client = null;
  584. try {
  585. OwnCloudAccount ocAccount =
  586. new OwnCloudAccount(mAccount, context);
  587. client = (OwnCloudClientManagerFactory.getDefaultSingleton().
  588. removeClientFor(ocAccount));
  589. // TODO get rid of these exceptions
  590. } catch (AccountNotFoundException e) {
  591. e.printStackTrace();
  592. } catch (AuthenticatorException e) {
  593. e.printStackTrace();
  594. } catch (OperationCanceledException e) {
  595. e.printStackTrace();
  596. } catch (IOException e) {
  597. e.printStackTrace();
  598. }
  599. if (client != null) {
  600. OwnCloudCredentials cred = client.getCredentials();
  601. if (cred != null) {
  602. AccountManager am = AccountManager.get(context);
  603. if (cred.authTokenExpires()) {
  604. am.invalidateAuthToken(
  605. mAccount.type,
  606. cred.getAuthToken()
  607. );
  608. } else {
  609. am.clearPassword(mAccount);
  610. }
  611. }
  612. }
  613. requestCredentialsUpdate();
  614. }
  615. }
  616. removeStickyBroadcast(intent);
  617. Log_OC.d(TAG, "Setting progress visibility to " + mSyncInProgress);
  618. setSupportProgressBarIndeterminateVisibility(mSyncInProgress /*|| mRefreshSharesInProgress*/);
  619. }
  620. } catch (RuntimeException e) {
  621. // avoid app crashes after changing the serial id of RemoteOperationResult
  622. // in owncloud library with broadcast notifications pending to process
  623. removeStickyBroadcast(intent);
  624. }
  625. }
  626. }
  627. }