ErrorsWhileCopyingHandlerActivity.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012-2013 ownCloud Inc.
  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 version 2,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. package com.owncloud.android.ui.activity;
  18. import java.io.File;
  19. import java.util.ArrayList;
  20. import android.accounts.Account;
  21. import android.content.Context;
  22. import android.content.Intent;
  23. import android.os.AsyncTask;
  24. import android.os.Bundle;
  25. import android.os.Handler;
  26. import android.support.v4.app.DialogFragment;
  27. import android.text.method.ScrollingMovementMethod;
  28. import android.view.LayoutInflater;
  29. import android.view.View;
  30. import android.view.View.OnClickListener;
  31. import android.view.ViewGroup;
  32. import android.widget.ArrayAdapter;
  33. import android.widget.Button;
  34. import android.widget.ListView;
  35. import android.widget.TextView;
  36. import android.widget.Toast;
  37. import com.actionbarsherlock.app.SherlockFragmentActivity;
  38. import com.owncloud.android.R;
  39. import com.owncloud.android.datamodel.FileDataStorageManager;
  40. import com.owncloud.android.datamodel.OCFile;
  41. import com.owncloud.android.ui.dialog.IndeterminateProgressDialog;
  42. import com.owncloud.android.utils.FileStorageUtils;
  43. import com.owncloud.android.utils.Log_OC;
  44. /**
  45. * Activity reporting errors occurred when local files uploaded to an ownCloud account with an app in
  46. * version under 1.3.16 where being copied to the ownCloud local folder.
  47. *
  48. * Allows the user move the files to the ownCloud local folder. let them unlinked to the remote
  49. * files.
  50. *
  51. * Shown when the error notification summarizing the list of errors is clicked by the user.
  52. *
  53. * @author David A. Velasco
  54. */
  55. public class ErrorsWhileCopyingHandlerActivity extends SherlockFragmentActivity implements OnClickListener {
  56. private static final String TAG = ErrorsWhileCopyingHandlerActivity.class.getSimpleName();
  57. public static final String EXTRA_ACCOUNT = ErrorsWhileCopyingHandlerActivity.class.getCanonicalName() + ".EXTRA_ACCOUNT";
  58. public static final String EXTRA_LOCAL_PATHS = ErrorsWhileCopyingHandlerActivity.class.getCanonicalName() + ".EXTRA_LOCAL_PATHS";
  59. public static final String EXTRA_REMOTE_PATHS = ErrorsWhileCopyingHandlerActivity.class.getCanonicalName() + ".EXTRA_REMOTE_PATHS";
  60. private static final String WAIT_DIALOG_TAG = "WAIT_DIALOG";
  61. protected Account mAccount;
  62. protected FileDataStorageManager mStorageManager;
  63. protected ArrayList<String> mLocalPaths;
  64. protected ArrayList<String> mRemotePaths;
  65. protected ArrayAdapter<String> mAdapter;
  66. protected Handler mHandler;
  67. private DialogFragment mCurrentDialog;
  68. /**
  69. * {@link}
  70. */
  71. @Override
  72. protected void onCreate(Bundle savedInstanceState) {
  73. super.onCreate(savedInstanceState);
  74. /// read extra parameters in intent
  75. Intent intent = getIntent();
  76. mAccount = intent.getParcelableExtra(EXTRA_ACCOUNT);
  77. mRemotePaths = intent.getStringArrayListExtra(EXTRA_REMOTE_PATHS);
  78. mLocalPaths = intent.getStringArrayListExtra(EXTRA_LOCAL_PATHS);
  79. mStorageManager = new FileDataStorageManager(mAccount, getContentResolver());
  80. mHandler = new Handler();
  81. if (mCurrentDialog != null) {
  82. mCurrentDialog.dismiss();
  83. mCurrentDialog = null;
  84. }
  85. /// load generic layout
  86. setContentView(R.layout.generic_explanation);
  87. /// customize text message
  88. TextView textView = (TextView) findViewById(R.id.message);
  89. String appName = getString(R.string.app_name);
  90. String message = String.format(getString(R.string.sync_foreign_files_forgotten_explanation), appName, appName, appName, appName, mAccount.name);
  91. textView.setText(message);
  92. textView.setMovementMethod(new ScrollingMovementMethod());
  93. /// load the list of local and remote files that failed
  94. ListView listView = (ListView) findViewById(R.id.list);
  95. if (mLocalPaths != null && mLocalPaths.size() > 0) {
  96. mAdapter = new ErrorsWhileCopyingListAdapter();
  97. listView.setAdapter(mAdapter);
  98. } else {
  99. listView.setVisibility(View.GONE);
  100. mAdapter = null;
  101. }
  102. /// customize buttons
  103. Button cancelBtn = (Button) findViewById(R.id.cancel);
  104. Button okBtn = (Button) findViewById(R.id.ok);
  105. okBtn.setText(R.string.foreign_files_move);
  106. cancelBtn.setOnClickListener(this);
  107. okBtn.setOnClickListener(this);
  108. }
  109. /**
  110. * Customized adapter, showing the local files as main text in two-lines list item and the remote files
  111. * as the secondary text.
  112. *
  113. * @author David A. Velasco
  114. */
  115. public class ErrorsWhileCopyingListAdapter extends ArrayAdapter<String> {
  116. ErrorsWhileCopyingListAdapter() {
  117. super(ErrorsWhileCopyingHandlerActivity.this, android.R.layout.two_line_list_item, android.R.id.text1, mLocalPaths);
  118. }
  119. @Override
  120. public boolean isEnabled(int position) {
  121. return false;
  122. }
  123. /**
  124. * {@inheritDoc}
  125. */
  126. @Override
  127. public View getView (int position, View convertView, ViewGroup parent) {
  128. View view = convertView;
  129. if (view == null) {
  130. LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  131. view = vi.inflate(android.R.layout.two_line_list_item, null);
  132. }
  133. if (view != null) {
  134. String localPath = getItem(position);
  135. if (localPath != null) {
  136. TextView text1 = (TextView) view.findViewById(android.R.id.text1);
  137. if (text1 != null) {
  138. text1.setText(String.format(getString(R.string.foreign_files_local_text), localPath));
  139. }
  140. }
  141. if (mRemotePaths != null && mRemotePaths.size() > 0 && position >= 0 && position < mRemotePaths.size()) {
  142. TextView text2 = (TextView) view.findViewById(android.R.id.text2);
  143. String remotePath = mRemotePaths.get(position);
  144. if (text2 != null && remotePath != null) {
  145. text2.setText(String.format(getString(R.string.foreign_files_remote_text), remotePath));
  146. }
  147. }
  148. }
  149. return view;
  150. }
  151. }
  152. /**
  153. * Listener method to perform the MOVE / CANCEL action available in this activity.
  154. *
  155. * @param v Clicked view (button MOVE or CANCEL)
  156. */
  157. @Override
  158. public void onClick(View v) {
  159. if (v.getId() == R.id.ok) {
  160. /// perform movement operation in background thread
  161. Log_OC.d(TAG, "Clicked MOVE, start movement");
  162. new MoveFilesTask().execute();
  163. } else if (v.getId() == R.id.cancel) {
  164. /// just finish
  165. Log_OC.d(TAG, "Clicked CANCEL, bye");
  166. finish();
  167. } else {
  168. Log_OC.e(TAG, "Clicked phantom button, id: " + v.getId());
  169. }
  170. }
  171. /**
  172. * Asynchronous task performing the move of all the local files to the ownCloud folder.
  173. *
  174. * @author David A. Velasco
  175. */
  176. private class MoveFilesTask extends AsyncTask<Void, Void, Boolean> {
  177. /**
  178. * Updates the UI before trying the movement
  179. */
  180. @Override
  181. protected void onPreExecute () {
  182. /// progress dialog and disable 'Move' button
  183. mCurrentDialog = IndeterminateProgressDialog.newInstance(R.string.wait_a_moment, false);
  184. mCurrentDialog.show(getSupportFragmentManager(), WAIT_DIALOG_TAG);
  185. findViewById(R.id.ok).setEnabled(false);
  186. }
  187. /**
  188. * Performs the movement
  189. *
  190. * @return 'False' when the movement of any file fails.
  191. */
  192. @Override
  193. protected Boolean doInBackground(Void... params) {
  194. while (!mLocalPaths.isEmpty()) {
  195. String currentPath = mLocalPaths.get(0);
  196. File currentFile = new File(currentPath);
  197. String expectedPath = FileStorageUtils.getSavePath(mAccount.name) + mRemotePaths.get(0);
  198. File expectedFile = new File(expectedPath);
  199. if (expectedFile.equals(currentFile) || currentFile.renameTo(expectedFile)) {
  200. // SUCCESS
  201. OCFile file = mStorageManager.getFileByPath(mRemotePaths.get(0));
  202. file.setStoragePath(expectedPath);
  203. mStorageManager.saveFile(file);
  204. mRemotePaths.remove(0);
  205. mLocalPaths.remove(0);
  206. } else {
  207. // FAIL
  208. return false;
  209. }
  210. }
  211. return true;
  212. }
  213. /**
  214. * Updates the activity UI after the movement of local files is tried.
  215. *
  216. * If the movement was successful for all the files, finishes the activity immediately.
  217. *
  218. * In other case, the list of remaining files is still available to retry the movement.
  219. *
  220. * @param result 'True' when the movement was successful.
  221. */
  222. @Override
  223. protected void onPostExecute(Boolean result) {
  224. mAdapter.notifyDataSetChanged();
  225. mCurrentDialog.dismiss();
  226. mCurrentDialog = null;
  227. findViewById(R.id.ok).setEnabled(true);
  228. if (result) {
  229. // nothing else to do in this activity
  230. Toast t = Toast.makeText(ErrorsWhileCopyingHandlerActivity.this, getString(R.string.foreign_files_success), Toast.LENGTH_LONG);
  231. t.show();
  232. finish();
  233. } else {
  234. Toast t = Toast.makeText(ErrorsWhileCopyingHandlerActivity.this, getString(R.string.foreign_files_fail), Toast.LENGTH_LONG);
  235. t.show();
  236. }
  237. }
  238. }
  239. }