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