2 * ownCloud Android client application
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 package com
.owncloud
.android
.ui
.activity
;
24 import java
.util
.ArrayList
;
26 import android
.accounts
.Account
;
27 import android
.content
.Context
;
28 import android
.content
.Intent
;
29 import android
.os
.AsyncTask
;
30 import android
.os
.Bundle
;
31 import android
.os
.Handler
;
32 import android
.support
.v4
.app
.DialogFragment
;
33 import android
.text
.method
.ScrollingMovementMethod
;
34 import android
.view
.LayoutInflater
;
35 import android
.view
.View
;
36 import android
.view
.View
.OnClickListener
;
37 import android
.view
.ViewGroup
;
38 import android
.widget
.ArrayAdapter
;
39 import android
.widget
.Button
;
40 import android
.widget
.ListView
;
41 import android
.widget
.TextView
;
42 import android
.widget
.Toast
;
44 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
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
.lib
.common
.utils
.Log_OC
;
50 import com
.owncloud
.android
.ui
.dialog
.IndeterminateProgressDialog
;
51 import com
.owncloud
.android
.utils
.FileStorageUtils
;
56 * Activity reporting errors occurred when local files uploaded to an ownCloud account with an app in
57 * version under 1.3.16 where being copied to the ownCloud local folder.
59 * Allows the user move the files to the ownCloud local folder. let them unlinked to the remote
62 * Shown when the error notification summarizing the list of errors is clicked by the user.
64 public class ErrorsWhileCopyingHandlerActivity
extends SherlockFragmentActivity
implements OnClickListener
{
66 private static final String TAG
= ErrorsWhileCopyingHandlerActivity
.class.getSimpleName();
68 public static final String EXTRA_ACCOUNT
= ErrorsWhileCopyingHandlerActivity
.class.getCanonicalName() + ".EXTRA_ACCOUNT";
69 public static final String EXTRA_LOCAL_PATHS
= ErrorsWhileCopyingHandlerActivity
.class.getCanonicalName() + ".EXTRA_LOCAL_PATHS";
70 public static final String EXTRA_REMOTE_PATHS
= ErrorsWhileCopyingHandlerActivity
.class.getCanonicalName() + ".EXTRA_REMOTE_PATHS";
72 private static final String WAIT_DIALOG_TAG
= "WAIT_DIALOG";
74 protected Account mAccount
;
75 protected FileDataStorageManager mStorageManager
;
76 protected ArrayList
<String
> mLocalPaths
;
77 protected ArrayList
<String
> mRemotePaths
;
78 protected ArrayAdapter
<String
> mAdapter
;
79 protected Handler mHandler
;
80 private DialogFragment mCurrentDialog
;
86 protected void onCreate(Bundle savedInstanceState
) {
87 super.onCreate(savedInstanceState
);
89 /// read extra parameters in intent
90 Intent intent
= getIntent();
91 mAccount
= intent
.getParcelableExtra(EXTRA_ACCOUNT
);
92 mRemotePaths
= intent
.getStringArrayListExtra(EXTRA_REMOTE_PATHS
);
93 mLocalPaths
= intent
.getStringArrayListExtra(EXTRA_LOCAL_PATHS
);
94 mStorageManager
= new FileDataStorageManager(mAccount
, getContentResolver());
95 mHandler
= new Handler();
96 if (mCurrentDialog
!= null
) {
97 mCurrentDialog
.dismiss();
98 mCurrentDialog
= null
;
101 /// load generic layout
102 setContentView(R
.layout
.generic_explanation
);
104 /// customize text message
105 TextView textView
= (TextView
) findViewById(R
.id
.message
);
106 String appName
= getString(R
.string
.app_name
);
107 String message
= String
.format(getString(R
.string
.sync_foreign_files_forgotten_explanation
), appName
, appName
, appName
, appName
, mAccount
.name
);
108 textView
.setText(message
);
109 textView
.setMovementMethod(new ScrollingMovementMethod());
111 /// load the list of local and remote files that failed
112 ListView listView
= (ListView
) findViewById(R
.id
.list
);
113 if (mLocalPaths
!= null
&& mLocalPaths
.size() > 0) {
114 mAdapter
= new ErrorsWhileCopyingListAdapter();
115 listView
.setAdapter(mAdapter
);
117 listView
.setVisibility(View
.GONE
);
121 /// customize buttons
122 Button cancelBtn
= (Button
) findViewById(R
.id
.cancel
);
123 Button okBtn
= (Button
) findViewById(R
.id
.ok
);
125 okBtn
.setText(R
.string
.foreign_files_move
);
126 cancelBtn
.setOnClickListener(this);
127 okBtn
.setOnClickListener(this);
132 * Customized adapter, showing the local files as main text in two-lines list item and the remote files
133 * as the secondary text.
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 private class MoveFilesTask
extends AsyncTask
<Void
, Void
, Boolean
> {
206 * Updates the UI before trying the movement
209 protected void onPreExecute () {
210 /// progress dialog and disable 'Move' button
211 mCurrentDialog
= IndeterminateProgressDialog
.newInstance(R
.string
.wait_a_moment
, false
);
212 mCurrentDialog
.show(getSupportFragmentManager(), WAIT_DIALOG_TAG
);
213 findViewById(R
.id
.ok
).setEnabled(false
);
218 * Performs the movement
220 * @return 'False' when the movement of any file fails.
223 protected Boolean
doInBackground(Void
... params
) {
224 while (!mLocalPaths
.isEmpty()) {
225 String currentPath
= mLocalPaths
.get(0);
226 File currentFile
= new File(currentPath
);
227 String expectedPath
= FileStorageUtils
.getSavePath(mAccount
.name
) + mRemotePaths
.get(0);
228 File expectedFile
= new File(expectedPath
);
230 if (expectedFile
.equals(currentFile
) || currentFile
.renameTo(expectedFile
)) {
232 OCFile file
= mStorageManager
.getFileByPath(mRemotePaths
.get(0));
233 file
.setStoragePath(expectedPath
);
234 mStorageManager
.saveFile(file
);
235 mRemotePaths
.remove(0);
236 mLocalPaths
.remove(0);
247 * Updates the activity UI after the movement of local files is tried.
249 * If the movement was successful for all the files, finishes the activity immediately.
251 * In other case, the list of remaining files is still available to retry the movement.
253 * @param result 'True' when the movement was successful.
256 protected void onPostExecute(Boolean result
) {
257 mAdapter
.notifyDataSetChanged();
258 mCurrentDialog
.dismiss();
259 mCurrentDialog
= null
;
260 findViewById(R
.id
.ok
).setEnabled(true
);
263 // nothing else to do in this activity
264 Toast t
= Toast
.makeText(ErrorsWhileCopyingHandlerActivity
.this, getString(R
.string
.foreign_files_success
), Toast
.LENGTH_LONG
);
269 Toast t
= Toast
.makeText(ErrorsWhileCopyingHandlerActivity
.this, getString(R
.string
.foreign_files_fail
), Toast
.LENGTH_LONG
);