1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
18 package de
.mobilcom
.debitel
.cloud
.android
.ui
.activity
;
21 import java
.util
.ArrayList
;
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
.view
.LayoutInflater
;
32 import android
.view
.View
;
33 import android
.view
.View
.OnClickListener
;
34 import android
.view
.ViewGroup
;
35 import android
.widget
.ArrayAdapter
;
36 import android
.widget
.ListView
;
37 import android
.widget
.TextView
;
38 import android
.widget
.Toast
;
40 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
42 import de
.mobilcom
.debitel
.cloud
.android
.Log_OC
;
43 import de
.mobilcom
.debitel
.cloud
.android
.R
;
44 import de
.mobilcom
.debitel
.cloud
.android
.datamodel
.FileDataStorageManager
;
45 import de
.mobilcom
.debitel
.cloud
.android
.datamodel
.OCFile
;
46 import de
.mobilcom
.debitel
.cloud
.android
.ui
.CustomButton
;
47 import de
.mobilcom
.debitel
.cloud
.android
.ui
.dialog
.IndeterminateProgressDialog
;
48 import de
.mobilcom
.debitel
.cloud
.android
.utils
.FileStorageUtils
;
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.
55 * Allows the user move the files to the ownCloud local folder. let them unlinked to the remote
58 * Shown when the error notification summarizing the list of errors is clicked by the user.
60 * @author David A. Velasco
62 public class ErrorsWhileCopyingHandlerActivity
extends SherlockFragmentActivity
implements OnClickListener
{
64 private static final String TAG
= ErrorsWhileCopyingHandlerActivity
.class.getSimpleName();
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";
70 private static final String WAIT_DIALOG_TAG
= "WAIT_DIALOG";
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
;
84 protected void onCreate(Bundle savedInstanceState
) {
85 super.onCreate(savedInstanceState
);
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
;
99 /// load generic layout
100 setContentView(R
.layout
.generic_explanation
);
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());
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
);
115 listView
.setVisibility(View
.GONE
);
119 /// customize buttons
120 CustomButton cancelBtn
= (CustomButton
) findViewById(R
.id
.cancel
);
121 CustomButton okBtn
= (CustomButton
) findViewById(R
.id
.ok
);
123 okBtn
.setText(R
.string
.foreign_files_move
);
124 cancelBtn
.setOnClickListener(this);
125 okBtn
.setOnClickListener(this);
130 * Customized adapter, showing the local files as main text in two-lines list item and the remote files
131 * as the secondary text.
133 * @author David A. Velasco
135 public class ErrorsWhileCopyingListAdapter
extends ArrayAdapter
<String
> {
137 ErrorsWhileCopyingListAdapter() {
138 super(ErrorsWhileCopyingHandlerActivity
.this, android
.R
.layout
.two_line_list_item
, android
.R
.id
.text1
, mLocalPaths
);
142 public boolean isEnabled(int position
) {
150 public View
getView (int position
, View convertView
, ViewGroup parent
) {
151 View view
= convertView
;
153 LayoutInflater vi
= (LayoutInflater
) getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
154 view
= vi
.inflate(android
.R
.layout
.two_line_list_item
, null
);
157 String localPath
= getItem(position
);
158 if (localPath
!= null
) {
159 TextView text1
= (TextView
) view
.findViewById(android
.R
.id
.text1
);
161 text1
.setText(String
.format(getString(R
.string
.foreign_files_local_text
), localPath
));
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
));
178 * Listener method to perform the MOVE / CANCEL action available in this activity.
180 * @param v Clicked view (button MOVE or CANCEL)
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();
189 } else if (v
.getId() == R
.id
.cancel
) {
191 Log_OC
.d(TAG
, "Clicked CANCEL, bye");
195 Log_OC
.e(TAG
, "Clicked phantom button, id: " + v
.getId());
201 * Asynchronous task performing the move of all the local files to the ownCloud folder.
203 * @author David A. Velasco
205 private class MoveFilesTask
extends AsyncTask
<Void
, Void
, Boolean
> {
208 * Updates the UI before trying the movement
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
);
220 * Performs the movement
222 * @return 'False' when the movement of any file fails.
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
);
232 if (expectedFile
.equals(currentFile
) || currentFile
.renameTo(expectedFile
)) {
234 OCFile file
= mStorageManager
.getFileByPath(mRemotePaths
.get(0));
235 file
.setStoragePath(expectedPath
);
236 mStorageManager
.saveFile(file
);
237 mRemotePaths
.remove(0);
238 mLocalPaths
.remove(0);
249 * Updates the activity UI after the movement of local files is tried.
251 * If the movement was successful for all the files, finishes the activity immediately.
253 * In other case, the list of remaining files is still available to retry the movement.
255 * @param result 'True' when the movement was successful.
258 protected void onPostExecute(Boolean result
) {
259 mAdapter
.notifyDataSetChanged();
260 mCurrentDialog
.dismiss();
261 mCurrentDialog
= null
;
262 findViewById(R
.id
.ok
).setEnabled(true
);
265 // nothing else to do in this activity
266 Toast t
= Toast
.makeText(ErrorsWhileCopyingHandlerActivity
.this, getString(R
.string
.foreign_files_success
), Toast
.LENGTH_LONG
);
271 Toast t
= Toast
.makeText(ErrorsWhileCopyingHandlerActivity
.this, getString(R
.string
.foreign_files_fail
), Toast
.LENGTH_LONG
);