1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
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 3 of the License, or
7 * (at your option) any later version.
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.
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/>.
19 package eu
.alefzero
.owncloud
.ui
.activity
;
22 import java
.net
.URLDecoder
;
23 import java
.net
.URLEncoder
;
25 import android
.accounts
.Account
;
26 import android
.accounts
.AccountManager
;
27 import android
.app
.AlertDialog
;
28 import android
.app
.AlertDialog
.Builder
;
29 import android
.app
.Dialog
;
30 import android
.content
.BroadcastReceiver
;
31 import android
.content
.ContentResolver
;
32 import android
.content
.Context
;
33 import android
.content
.DialogInterface
;
34 import android
.content
.DialogInterface
.OnClickListener
;
35 import android
.content
.Intent
;
36 import android
.content
.IntentFilter
;
37 import android
.database
.Cursor
;
38 import android
.net
.Uri
;
39 import android
.os
.Bundle
;
40 import android
.provider
.MediaStore
;
41 import android
.util
.Log
;
42 import android
.view
.View
;
43 import android
.view
.ViewGroup
;
44 import android
.widget
.ArrayAdapter
;
45 import android
.widget
.EditText
;
46 import android
.widget
.TextView
;
48 import com
.actionbarsherlock
.app
.ActionBar
;
49 import com
.actionbarsherlock
.app
.ActionBar
.OnNavigationListener
;
50 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
51 import com
.actionbarsherlock
.view
.Menu
;
52 import com
.actionbarsherlock
.view
.MenuInflater
;
53 import com
.actionbarsherlock
.view
.MenuItem
;
54 import com
.actionbarsherlock
.view
.Window
;
56 import eu
.alefzero
.owncloud
.AccountUtils
;
57 import eu
.alefzero
.owncloud
.R
;
58 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
59 import eu
.alefzero
.owncloud
.datamodel
.DataStorageManager
;
60 import eu
.alefzero
.owncloud
.datamodel
.FileDataStorageManager
;
61 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
62 import eu
.alefzero
.owncloud
.files
.services
.FileUploader
;
63 import eu
.alefzero
.owncloud
.syncadapter
.FileSyncService
;
64 import eu
.alefzero
.owncloud
.ui
.fragment
.FileListFragment
;
65 import eu
.alefzero
.webdav
.WebdavClient
;
68 * Displays, what files the user has available in his ownCloud.
70 * @author Bartek Przybylski
74 public class FileDisplayActivity
extends SherlockFragmentActivity
implements
75 OnNavigationListener
, OnClickListener
{
76 private ArrayAdapter
<String
> mDirectories
;
77 private DataStorageManager mStorageManager
;
78 private String
[] mDirs
= null
;
80 private SyncBroadcastReceiver syncBroadcastRevceiver
;
82 private static final String KEY_DIR
= "DIR";
84 private static final int DIALOG_SETUP_ACCOUNT
= 0;
85 private static final int DIALOG_CREATE_DIR
= 1;
86 private static final int ACTION_SELECT_FILE
= 1;
88 public void pushPath(String path
) {
89 mDirectories
.insert(path
, 0);
92 public boolean popPath() {
93 mDirectories
.remove(mDirectories
.getItem(0));
94 return !mDirectories
.isEmpty();
98 protected Dialog
onCreateDialog(int id
) {
100 AlertDialog
.Builder builder
;
102 case DIALOG_SETUP_ACCOUNT
:
103 builder
= new AlertDialog
.Builder(this);
104 builder
.setTitle(R
.string
.main_tit_accsetup
);
105 builder
.setMessage(R
.string
.main_wrn_accsetup
);
106 builder
.setCancelable(false
);
107 builder
.setPositiveButton(android
.R
.string
.ok
, this);
108 builder
.setNegativeButton(android
.R
.string
.cancel
, this);
109 dialog
= builder
.create();
111 case DIALOG_CREATE_DIR
: {
112 builder
= new Builder(this);
113 final EditText dirName
= new EditText(getBaseContext());
114 final Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
115 builder
.setView(dirName
);
116 builder
.setTitle(R
.string
.uploader_info_dirname
);
117 int typed_color
= getResources().getColor(R
.color
.setup_text_typed
);
118 dirName
.setTextColor(typed_color
);
120 builder
.setPositiveButton(android
.R
.string
.ok
,
121 new OnClickListener() {
122 public void onClick(DialogInterface dialog
, int which
) {
123 String s
= dirName
.getText().toString();
124 if (s
.trim().length() == 0) {
130 for (int i
= mDirectories
.getCount() - 2; i
>= 0; --i
) {
131 path
+= "/" + mDirectories
.getItem(i
);
133 OCFile parent
= mStorageManager
.getFileByPath(path
136 Thread thread
= new Thread(new DirectoryCreator(
140 OCFile new_file
= new OCFile(path
);
141 new_file
.setMimetype("DIR");
142 new_file
.setParentId(parent
.getParentId());
143 mStorageManager
.saveFile(new_file
);
148 builder
.setNegativeButton(R
.string
.common_cancel
,
149 new OnClickListener() {
150 public void onClick(DialogInterface dialog
, int which
) {
154 dialog
= builder
.create();
165 public void onCreate(Bundle savedInstanceState
) {
166 super.onCreate(savedInstanceState
);
168 if (!accountsAreSetup()) {
169 showDialog(DIALOG_SETUP_ACCOUNT
);
173 requestWindowFeature(Window
.FEATURE_INDETERMINATE_PROGRESS
);
174 setProgressBarIndeterminateVisibility(false
);
176 setContentView(R
.layout
.files
);
181 public boolean onOptionsItemSelected(MenuItem item
) {
182 boolean retval
= true
;
183 switch (item
.getItemId()) {
184 case R
.id
.createDirectoryItem
: {
185 showDialog(DIALOG_CREATE_DIR
);
188 case R
.id
.startSync
: {
189 Bundle bundle
= new Bundle();
190 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
191 ContentResolver
.requestSync(
192 AccountUtils
.getCurrentOwnCloudAccount(this),
193 "org.owncloud", bundle
);
196 case R
.id
.action_upload
: {
197 Intent action
= new Intent(Intent
.ACTION_GET_CONTENT
);
198 action
= action
.setType("*/*")
199 .addCategory(Intent
.CATEGORY_OPENABLE
);
200 startActivityForResult(
201 Intent
.createChooser(action
, "Upload file from..."),
206 case android
.R
.id
.home
: {
207 Intent i
= new Intent(this, AccountSelectActivity
.class);
219 public void onBackPressed() {
220 if (mDirectories
.getCount() == 1) {
225 ((FileListFragment
) getSupportFragmentManager().findFragmentById(
226 R
.id
.fileList
)).onNavigateUp();
230 public boolean onCreateOptionsMenu(Menu menu
) {
231 MenuInflater inflater
= getSherlock().getMenuInflater();
232 inflater
.inflate(R
.menu
.menu
, menu
);
237 protected void onRestoreInstanceState(Bundle savedInstanceState
) {
238 super.onRestoreInstanceState(savedInstanceState
);
239 // Check, if there are ownCloud accounts
240 if (!accountsAreSetup()) {
241 showDialog(DIALOG_SETUP_ACCOUNT
);
243 mDirs
= savedInstanceState
.getStringArray(KEY_DIR
);
244 mDirectories
= new CustomArrayAdapter
<String
>(this, R
.layout
.sherlock_spinner_dropdown_item
);
245 mDirectories
.add("/");
247 for (String s
: mDirs
)
248 mDirectories
.insert(s
, 0);
252 protected void onSaveInstanceState(Bundle outState
) {
253 super.onSaveInstanceState(outState
);
254 if(mDirectories
!= null
){
255 mDirs
= new String
[mDirectories
.getCount()-1];
256 for (int j
= mDirectories
.getCount() - 2, i
= 0; j
>= 0; --j
, ++i
) {
257 mDirs
[i
] = mDirectories
.getItem(j
);
263 protected void onResume() {
265 if (!accountsAreSetup()) {
266 showDialog(DIALOG_SETUP_ACCOUNT
);
270 IntentFilter f
= new IntentFilter(FileSyncService
.SYNC_MESSAGE
);
271 syncBroadcastRevceiver
= new SyncBroadcastReceiver();
272 registerReceiver(syncBroadcastRevceiver
, f
);
274 mDirectories
= new CustomArrayAdapter
<String
>(this, R
.layout
.sherlock_spinner_dropdown_item
);
275 mDirectories
.add("/");
277 for (String s
: mDirs
)
278 mDirectories
.insert(s
, 0);
279 FileListFragment fileListFramgent
= (FileListFragment
) getSupportFragmentManager()
280 .findFragmentById(R
.id
.fileList
);
281 if (fileListFramgent
!= null
) fileListFramgent
.listDirectory();
284 mStorageManager
= new FileDataStorageManager(
285 AccountUtils
.getCurrentOwnCloudAccount(this),
286 getContentResolver());
287 ActionBar action_bar
= getSupportActionBar();
288 action_bar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_LIST
);
289 action_bar
.setDisplayShowTitleEnabled(false
);
290 action_bar
.setListNavigationCallbacks(mDirectories
, this);
291 action_bar
.setDisplayHomeAsUpEnabled(true
);
294 public void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
295 if (resultCode
== RESULT_OK
) {
296 if (requestCode
== ACTION_SELECT_FILE
) {
297 Uri selectedImageUri
= data
.getData();
299 String filemanagerstring
= selectedImageUri
.getPath();
300 String selectedImagePath
= getPath(selectedImageUri
);
303 if (selectedImagePath
!= null
)
304 filepath
= selectedImagePath
;
306 filepath
= filemanagerstring
;
308 if (filepath
== null
) {
309 Log
.e("FileDisplay", "Couldnt resolve path to file");
313 Intent i
= new Intent(this, FileUploader
.class);
314 i
.putExtra(FileUploader
.KEY_ACCOUNT
,
315 AccountUtils
.getCurrentOwnCloudAccount(this));
316 String remotepath
= new String();
317 for (int j
= mDirectories
.getCount() - 2; j
>= 0; --j
) {
318 remotepath
+= "/" + URLEncoder
.encode(mDirectories
.getItem(j
));
320 if (!remotepath
.endsWith("/"))
322 remotepath
+= URLEncoder
.encode(new File(filepath
).getName());
323 Log
.e("ASD", remotepath
+ "");
325 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filepath
);
326 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotepath
);
327 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
333 public String
getPath(Uri uri
) {
334 String
[] projection
= { MediaStore
.Images
.Media
.DATA
};
335 Cursor cursor
= managedQuery(uri
, projection
, null
, null
, null
);
336 if (cursor
!= null
) {
337 int column_index
= cursor
338 .getColumnIndexOrThrow(MediaStore
.Images
.Media
.DATA
);
339 cursor
.moveToFirst();
340 return cursor
.getString(column_index
);
346 protected void onPause() {
348 if (syncBroadcastRevceiver
!= null
) {
349 unregisterReceiver(syncBroadcastRevceiver
);
350 syncBroadcastRevceiver
= null
;
356 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
357 int i
= itemPosition
;
364 private class DirectoryCreator
implements Runnable
{
365 private String mTargetPath
;
366 private Account mAccount
;
367 private AccountManager mAm
;
369 public DirectoryCreator(String targetPath
, Account account
) {
370 mTargetPath
= targetPath
;
372 mAm
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
377 WebdavClient wdc
= new WebdavClient(Uri
.parse(mAm
.getUserData(
378 mAccount
, AccountAuthenticator
.KEY_OC_URL
)));
380 String username
= mAccount
.name
.substring(0,
381 mAccount
.name
.lastIndexOf('@'));
382 String password
= mAm
.getPassword(mAccount
);
384 wdc
.setCredentials(username
, password
);
385 wdc
.allowUnsignedCertificates();
386 wdc
.createDirectory(mTargetPath
);
391 // Custom array adapter to override text colors
392 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
394 public CustomArrayAdapter(FileDisplayActivity ctx
, int view
) {
398 public View
getView(int position
, View convertView
, ViewGroup parent
) {
399 View v
= super.getView(position
, convertView
, parent
);
401 ((TextView
) v
).setTextColor(getResources().getColorStateList(
402 android
.R
.color
.white
));
406 public View
getDropDownView(int position
, View convertView
,
408 View v
= super.getDropDownView(position
, convertView
, parent
);
410 ((TextView
) v
).setTextColor(getResources().getColorStateList(
411 android
.R
.color
.white
));
418 public void onClick(DialogInterface dialog
, int which
) {
419 // In any case - we won't need it anymore
422 case DialogInterface
.BUTTON_POSITIVE
:
423 Intent intent
= new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
424 intent
.putExtra("authorities",
425 new String
[] { AccountAuthenticator
.AUTH_TOKEN_TYPE
});
426 startActivity(intent
);
428 case DialogInterface
.BUTTON_NEGATIVE
:
435 * Checks, whether or not there are any ownCloud accounts setup.
437 * @return true, if there is at least one account.
439 private boolean accountsAreSetup() {
440 AccountManager accMan
= AccountManager
.get(this);
441 Account
[] accounts
= accMan
442 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
443 return accounts
.length
> 0;
446 private class SyncBroadcastReceiver
extends BroadcastReceiver
{
448 * {@link BroadcastReceiver} to enable syncing feedback in UI
451 public void onReceive(Context context
, Intent intent
) {
452 boolean inProgress
= intent
.getBooleanExtra(
453 FileSyncService
.IN_PROGRESS
, false
);
454 String account_name
= intent
455 .getStringExtra(FileSyncService
.ACCOUNT_NAME
);
456 Log
.d("FileDisplay", "sync of account " + account_name
457 + " is in_progress: " + inProgress
);
458 setProgressBarIndeterminateVisibility(inProgress
);
460 FileListFragment fileListFramgent
= (FileListFragment
) getSupportFragmentManager()
461 .findFragmentById(R
.id
.fileList
);
462 if (fileListFramgent
!= null
)
463 fileListFramgent
.listDirectory();