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
;
21 import java
.io
.BufferedReader
;
23 import java
.io
.InputStreamReader
;
24 import java
.lang
.Thread
.UncaughtExceptionHandler
;
25 import java
.net
.URLEncoder
;
26 import java
.util
.ArrayList
;
28 import android
.accounts
.Account
;
29 import android
.accounts
.AccountManager
;
30 import android
.app
.AlertDialog
;
31 import android
.app
.AlertDialog
.Builder
;
32 import android
.app
.Dialog
;
33 import android
.content
.BroadcastReceiver
;
34 import android
.content
.ContentResolver
;
35 import android
.content
.Context
;
36 import android
.content
.DialogInterface
;
37 import android
.content
.DialogInterface
.OnClickListener
;
38 import android
.content
.Intent
;
39 import android
.content
.IntentFilter
;
40 import android
.database
.Cursor
;
41 import android
.net
.Uri
;
42 import android
.os
.Bundle
;
43 import android
.provider
.MediaStore
;
44 import android
.telephony
.TelephonyManager
;
45 import android
.util
.Log
;
46 import android
.view
.View
;
47 import android
.view
.ViewGroup
;
48 import android
.widget
.ArrayAdapter
;
49 import android
.widget
.CheckedTextView
;
50 import android
.widget
.EditText
;
51 import android
.widget
.TextView
;
53 import com
.actionbarsherlock
.app
.ActionBar
;
54 import com
.actionbarsherlock
.app
.ActionBar
.OnNavigationListener
;
55 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
56 import com
.actionbarsherlock
.view
.Menu
;
57 import com
.actionbarsherlock
.view
.MenuInflater
;
58 import com
.actionbarsherlock
.view
.MenuItem
;
59 import com
.actionbarsherlock
.view
.Window
;
61 import eu
.alefzero
.owncloud
.AccountUtils
;
62 import eu
.alefzero
.owncloud
.CrashHandler
;
63 import eu
.alefzero
.owncloud
.R
;
64 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
65 import eu
.alefzero
.owncloud
.datamodel
.DataStorageManager
;
66 import eu
.alefzero
.owncloud
.datamodel
.FileDataStorageManager
;
67 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
68 import eu
.alefzero
.owncloud
.files
.services
.FileUploader
;
69 import eu
.alefzero
.owncloud
.syncadapter
.FileSyncService
;
70 import eu
.alefzero
.owncloud
.ui
.fragment
.FileDetailFragment
;
71 import eu
.alefzero
.owncloud
.ui
.fragment
.FileListFragment
;
72 import eu
.alefzero
.webdav
.WebdavClient
;
75 * Displays, what files the user has available in his ownCloud.
77 * @author Bartek Przybylski
81 public class FileDisplayActivity
extends SherlockFragmentActivity
implements
82 OnNavigationListener
, OnClickListener
, android
.view
.View
.OnClickListener
{
83 private ArrayAdapter
<String
> mDirectories
;
84 private DataStorageManager mStorageManager
;
85 private FileListFragment mFileList
;
86 private OCFile mCurrentDir
;
87 private String
[] mDirs
= null
;
89 private SyncBroadcastReceiver syncBroadcastRevceiver
;
91 private View mLayoutView
= null
;
93 private static final String KEY_DIR_ARRAY
= "DIR_ARRAY";
94 private static final String KEY_CURRENT_DIR
= "DIR";
96 private static final int DIALOG_SETUP_ACCOUNT
= 0;
97 private static final int DIALOG_CREATE_DIR
= 1;
98 private static final int ACTION_SELECT_FILE
= 1;
101 public void onCreate(Bundle savedInstanceState
) {
102 super.onCreate(savedInstanceState
);
104 requestWindowFeature(Window
.FEATURE_INDETERMINATE_PROGRESS
);
105 setProgressBarIndeterminateVisibility(false
);
107 Thread
.setDefaultUncaughtExceptionHandler(new CrashHandler(getApplicationContext()));
109 if(savedInstanceState
!= null
){
110 mCurrentDir
= (OCFile
) savedInstanceState
.getParcelable(KEY_CURRENT_DIR
);
113 if (findViewById(R
.id
.file_list_view
) == null
)
114 mLayoutView
= getLayoutInflater().inflate(R
.layout
.files
, null
); // always inflate this at onCreate() ; just once!
116 //TODO: Dialog useless -> get rid of this
117 if (!accountsAreSetup()) {
118 setContentView(R
.layout
.no_account_available
);
119 setProgressBarIndeterminateVisibility(false
);
120 getSupportActionBar().setNavigationMode(ActionBar
.DISPLAY_SHOW_TITLE
);
121 findViewById(R
.id
.setup_account
).setOnClickListener(this);
123 } else if (findViewById(R
.id
.file_list_view
) == null
) {
124 setContentView(mLayoutView
);
130 public boolean onCreateOptionsMenu(Menu menu
) {
131 MenuInflater inflater
= getSherlock().getMenuInflater();
132 inflater
.inflate(R
.menu
.menu
, menu
);
137 public boolean onOptionsItemSelected(MenuItem item
) {
138 boolean retval
= true
;
139 switch (item
.getItemId()) {
140 case R
.id
.createDirectoryItem
: {
141 showDialog(DIALOG_CREATE_DIR
);
144 case R
.id
.startSync
: {
145 Bundle bundle
= new Bundle();
146 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
147 ContentResolver
.requestSync(
148 AccountUtils
.getCurrentOwnCloudAccount(this),
149 "org.owncloud", bundle
);
152 case R
.id
.action_upload
: {
153 Intent action
= new Intent(Intent
.ACTION_GET_CONTENT
);
154 action
= action
.setType("*/*")
155 .addCategory(Intent
.CATEGORY_OPENABLE
);
156 startActivityForResult(
157 Intent
.createChooser(action
, "Upload file from..."),
161 case R
.id
.action_settings
: {
162 Intent settingsIntent
= new Intent(this, Preferences
.class);
163 startActivity(settingsIntent
);
165 case android
.R
.id
.home
: {
166 if(mCurrentDir
!= null
&& mCurrentDir
.getParentId() != 0){
178 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
179 int i
= itemPosition
;
187 * Called, when the user selected something for uploading
189 public void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
190 if (resultCode
== RESULT_OK
) {
191 if (requestCode
== ACTION_SELECT_FILE
) {
192 Uri selectedImageUri
= data
.getData();
194 String filemanagerstring
= selectedImageUri
.getPath();
195 String selectedImagePath
= getPath(selectedImageUri
);
198 if (selectedImagePath
!= null
)
199 filepath
= selectedImagePath
;
201 filepath
= filemanagerstring
;
203 if (filepath
== null
) {
204 Log
.e("FileDisplay", "Couldnt resolve path to file");
208 Intent i
= new Intent(this, FileUploader
.class);
209 i
.putExtra(FileUploader
.KEY_ACCOUNT
,
210 AccountUtils
.getCurrentOwnCloudAccount(this));
211 String remotepath
= new String();
212 for (int j
= mDirectories
.getCount() - 2; j
>= 0; --j
) {
213 remotepath
+= "/" + URLEncoder
.encode(mDirectories
.getItem(j
));
215 if (!remotepath
.endsWith("/"))
217 remotepath
+= URLEncoder
.encode(new File(filepath
).getName());
219 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filepath
);
220 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotepath
);
221 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
228 public void onBackPressed() {
229 if (mDirectories
== null
|| mDirectories
.getCount() <= 1) {
234 mFileList
.onNavigateUp();
235 mCurrentDir
= mFileList
.getCurrentFile();
237 if(mCurrentDir
.getParentId() == 0){
238 ActionBar actionBar
= getSupportActionBar();
239 actionBar
.setDisplayHomeAsUpEnabled(false
);
244 protected void onRestoreInstanceState(Bundle savedInstanceState
) {
245 super.onRestoreInstanceState(savedInstanceState
);
246 mDirs
= savedInstanceState
.getStringArray(KEY_DIR_ARRAY
);
247 mDirectories
= new CustomArrayAdapter
<String
>(this, R
.layout
.sherlock_spinner_dropdown_item
);
248 mDirectories
.add("/");
250 for (String s
: mDirs
)
251 mDirectories
.insert(s
, 0);
252 mCurrentDir
= savedInstanceState
.getParcelable(KEY_CURRENT_DIR
);
256 protected void onSaveInstanceState(Bundle outState
) {
257 super.onSaveInstanceState(outState
);
258 if(mDirectories
!= null
&& mDirectories
.getCount() != 0){
259 mDirs
= new String
[mDirectories
.getCount()-1];
260 for (int j
= mDirectories
.getCount() - 2, i
= 0; j
>= 0; --j
, ++i
) {
261 mDirs
[i
] = mDirectories
.getItem(j
);
264 outState
.putStringArray(KEY_DIR_ARRAY
, mDirs
);
265 outState
.putParcelable(KEY_CURRENT_DIR
, mCurrentDir
);
269 protected void onResume() {
272 if (accountsAreSetup()) {
274 setContentView(mLayoutView
); // this should solve the crash by repeated inflating in big screens (DROIDCLOUD-27)
276 // Listen for sync messages
277 IntentFilter syncIntentFilter
= new IntentFilter(FileSyncService
.SYNC_MESSAGE
);
278 syncBroadcastRevceiver
= new SyncBroadcastReceiver();
279 registerReceiver(syncBroadcastRevceiver
, syncIntentFilter
);
281 // Storage manager initialization
282 mStorageManager
= new FileDataStorageManager(
283 AccountUtils
.getCurrentOwnCloudAccount(this),
284 getContentResolver());
287 mFileList
= (FileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
);
289 // Figure out what directory to list.
290 // Priority: Intent (here), savedInstanceState (onCreate), root dir (dir is null)
291 if(getIntent().hasExtra(FileDetailFragment
.EXTRA_FILE
)){
292 mCurrentDir
= (OCFile
) getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_FILE
);
293 if(!mCurrentDir
.isDirectory()){
294 mCurrentDir
= mStorageManager
.getFileById(mCurrentDir
.getParentId());
297 // Clear intent extra, so rotating the screen will not return us to this directory
298 getIntent().removeExtra(FileDetailFragment
.EXTRA_FILE
);
300 mCurrentDir
= mStorageManager
.getFileByPath("/");
303 // Drop-Down navigation and file list restore
304 mDirectories
= new CustomArrayAdapter
<String
>(this, R
.layout
.sherlock_spinner_dropdown_item
);
307 // Given the case we have a file to display:
308 if(mCurrentDir
!= null
){
309 ArrayList
<OCFile
> files
= new ArrayList
<OCFile
>();
310 OCFile currFile
= mCurrentDir
;
311 while(currFile
!= null
){
313 currFile
= mStorageManager
.getFileById(currFile
.getParentId());
317 mDirs
= new String
[files
.size()];
318 for(int i
= files
.size() - 1; i
>= 0; i
--){
319 mDirs
[i
] = files
.get(i
).getFileName();
324 for (String s
: mDirs
)
327 mDirectories
.add("/");
331 ActionBar action_bar
= getSupportActionBar();
332 action_bar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_LIST
);
333 action_bar
.setDisplayShowTitleEnabled(false
);
334 action_bar
.setListNavigationCallbacks(mDirectories
, this);
335 if(mCurrentDir
!= null
&& mCurrentDir
.getParentId() != 0){
336 action_bar
.setDisplayHomeAsUpEnabled(true
);
338 action_bar
.setDisplayHomeAsUpEnabled(false
);
342 mFileList
.listDirectory(mCurrentDir
);
347 protected void onPause() {
349 if (syncBroadcastRevceiver
!= null
) {
350 unregisterReceiver(syncBroadcastRevceiver
);
351 syncBroadcastRevceiver
= null
;
356 protected Dialog
onCreateDialog(int id
) {
358 AlertDialog
.Builder builder
;
360 case DIALOG_SETUP_ACCOUNT
:
361 builder
= new AlertDialog
.Builder(this);
362 builder
.setTitle(R
.string
.main_tit_accsetup
);
363 builder
.setMessage(R
.string
.main_wrn_accsetup
);
364 builder
.setCancelable(false
);
365 builder
.setPositiveButton(android
.R
.string
.ok
, this);
366 builder
.setNegativeButton(android
.R
.string
.cancel
, this);
367 dialog
= builder
.create();
369 case DIALOG_CREATE_DIR
: {
370 builder
= new Builder(this);
371 final EditText dirNameInput
= new EditText(getBaseContext());
372 final Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
373 builder
.setView(dirNameInput
);
374 builder
.setTitle(R
.string
.uploader_info_dirname
);
375 int typed_color
= getResources().getColor(R
.color
.setup_text_typed
);
376 dirNameInput
.setTextColor(typed_color
);
377 builder
.setPositiveButton(android
.R
.string
.ok
,
378 new OnClickListener() {
379 public void onClick(DialogInterface dialog
, int which
) {
380 String directoryName
= dirNameInput
.getText().toString();
381 if (directoryName
.trim().length() == 0) {
386 // Figure out the path where the dir needs to be created
387 String path
= FileDisplayActivity
.this.mCurrentDir
.getRemotePath();
390 path
+= directoryName
+ "/";
391 Thread thread
= new Thread(new DirectoryCreator(path
, a
));
394 // Save new directory in local database
395 OCFile newDir
= new OCFile(path
);
396 newDir
.setMimetype("DIR");
397 newDir
.setParentId(mCurrentDir
.getFileId());
398 mStorageManager
.saveFile(newDir
);
400 // Display the new folder right away
402 mFileList
.listDirectory(mCurrentDir
);
405 builder
.setNegativeButton(R
.string
.common_cancel
,
406 new OnClickListener() {
407 public void onClick(DialogInterface dialog
, int which
) {
411 dialog
= builder
.create();
423 * Responds to the "There are no ownCloud Accounts setup" dialog
424 * TODO: Dialog is 100% useless -> Remove
427 public void onClick(DialogInterface dialog
, int which
) {
428 // In any case - we won't need it anymore
431 case DialogInterface
.BUTTON_POSITIVE
:
432 Intent intent
= new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
433 intent
.putExtra("authorities",
434 new String
[] { AccountAuthenticator
.AUTH_TOKEN_TYPE
});
435 startActivity(intent
);
437 case DialogInterface
.BUTTON_NEGATIVE
:
444 * Translates a content URI of an image to a physical path
446 * @param uri The URI to resolve
447 * @return The path to the image or null if it could not be found
449 public String
getPath(Uri uri
) {
450 String
[] projection
= { MediaStore
.Images
.Media
.DATA
};
451 Cursor cursor
= managedQuery(uri
, projection
, null
, null
, null
);
452 if (cursor
!= null
) {
453 int column_index
= cursor
454 .getColumnIndexOrThrow(MediaStore
.Images
.Media
.DATA
);
455 cursor
.moveToFirst();
456 return cursor
.getString(column_index
);
462 * Pushes a directory to the drop down list
463 * @param directory to push
464 * @throws IllegalArgumentException If the {@link OCFile#isDirectory()} returns false.
466 public void pushDirname(OCFile directory
) {
467 if(!directory
.isDirectory()){
468 throw new IllegalArgumentException("Only directories may be pushed!");
470 mDirectories
.insert(directory
.getFileName(), 0);
471 mCurrentDir
= directory
;
475 * Pops a directory name from the drop down list
476 * @return True, unless the stack is empty
478 public boolean popDirname() {
479 mDirectories
.remove(mDirectories
.getItem(0));
480 return !mDirectories
.isEmpty();
484 * Checks, whether or not there are any ownCloud accounts setup.
486 * @return true, if there is at least one account.
488 private boolean accountsAreSetup() {
489 AccountManager accMan
= AccountManager
.get(this);
490 Account
[] accounts
= accMan
491 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
492 return accounts
.length
> 0;
495 private class DirectoryCreator
implements Runnable
{
496 private String mTargetPath
;
497 private Account mAccount
;
498 private AccountManager mAm
;
500 public DirectoryCreator(String targetPath
, Account account
) {
501 mTargetPath
= targetPath
;
503 mAm
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
508 WebdavClient wdc
= new WebdavClient(Uri
.parse(mAm
.getUserData(
509 mAccount
, AccountAuthenticator
.KEY_OC_URL
)));
511 String username
= mAccount
.name
.substring(0,
512 mAccount
.name
.lastIndexOf('@'));
513 String password
= mAm
.getPassword(mAccount
);
515 wdc
.setCredentials(username
, password
);
516 wdc
.allowSelfsignedCertificates();
517 wdc
.createDirectory(mTargetPath
);
522 // Custom array adapter to override text colors
523 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
525 public CustomArrayAdapter(FileDisplayActivity ctx
, int view
) {
529 public View
getView(int position
, View convertView
, ViewGroup parent
) {
530 View v
= super.getView(position
, convertView
, parent
);
532 ((TextView
) v
).setTextColor(getResources().getColorStateList(
533 android
.R
.color
.white
));
537 public View
getDropDownView(int position
, View convertView
,
539 View v
= super.getDropDownView(position
, convertView
, parent
);
541 ((TextView
) v
).setTextColor(getResources().getColorStateList(
542 android
.R
.color
.white
));
549 private class SyncBroadcastReceiver
extends BroadcastReceiver
{
551 * {@link BroadcastReceiver} to enable syncing feedback in UI
554 public void onReceive(Context context
, Intent intent
) {
555 boolean inProgress
= intent
.getBooleanExtra(
556 FileSyncService
.IN_PROGRESS
, false
);
557 String account_name
= intent
558 .getStringExtra(FileSyncService
.ACCOUNT_NAME
);
559 Log
.d("FileDisplay", "sync of account " + account_name
560 + " is in_progress: " + inProgress
);
561 setProgressBarIndeterminateVisibility(inProgress
);
563 FileListFragment fileListFramgent
= (FileListFragment
) getSupportFragmentManager()
564 .findFragmentById(R
.id
.fileList
);
565 if (fileListFramgent
!= null
)
566 fileListFramgent
.listDirectory();
573 public void onClick(View v
) {
574 if (v
.getId() == R
.id
.setup_account
) {
575 Intent intent
= new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
576 intent
.putExtra("authorities", new String
[] { AccountAuthenticator
.AUTH_TOKEN_TYPE
});
577 startActivity(intent
);
581 public DataStorageManager
getStorageManager() {
582 return mStorageManager
;