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