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
.owncloud
.utils
.OwnCloudVersion
;
66 import eu
.alefzero
.webdav
.WebdavClient
;
69 * Displays, what files the user has available in his ownCloud.
71 * @author Bartek Przybylski
75 public class FileDisplayActivity
extends SherlockFragmentActivity
implements
76 OnNavigationListener
, OnClickListener
{
77 private ArrayAdapter
<String
> mDirectories
;
78 private DataStorageManager mStorageManager
;
79 private String
[] mDirs
= null
;
81 private SyncBroadcastReceiver syncBroadcastRevceiver
;
83 private static final String KEY_DIR
= "DIR";
85 private static final int DIALOG_SETUP_ACCOUNT
= 0;
86 private static final int DIALOG_CREATE_DIR
= 1;
88 private static final int REQUEST_ACCOUNT_SETUP
= 0;
89 private static final int ACTION_SELECT_FILE
= 1;
91 public void pushPath(String path
) {
92 mDirectories
.insert(path
, 0);
95 public boolean popPath() {
96 mDirectories
.remove(mDirectories
.getItem(0));
97 return !mDirectories
.isEmpty();
101 protected Dialog
onCreateDialog(int id
) {
103 AlertDialog
.Builder builder
;
105 case DIALOG_SETUP_ACCOUNT
:
106 builder
= new AlertDialog
.Builder(this);
107 builder
.setTitle(R
.string
.main_tit_accsetup
);
108 builder
.setMessage(R
.string
.main_wrn_accsetup
);
109 builder
.setCancelable(false
);
110 builder
.setPositiveButton(android
.R
.string
.ok
, this);
111 builder
.setNegativeButton(android
.R
.string
.cancel
, this);
112 dialog
= builder
.create();
114 case DIALOG_CREATE_DIR
: {
115 builder
= new Builder(this);
116 final EditText dirName
= new EditText(getBaseContext());
117 final Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
118 builder
.setView(dirName
);
119 builder
.setTitle(R
.string
.uploader_info_dirname
);
120 int typed_color
= getResources().getColor(R
.color
.setup_text_typed
);
121 dirName
.setTextColor(typed_color
);
123 builder
.setPositiveButton(android
.R
.string
.ok
,
124 new OnClickListener() {
125 public void onClick(DialogInterface dialog
, int which
) {
126 String s
= dirName
.getText().toString();
127 if (s
.trim().length() == 0) {
133 for (int i
= mDirectories
.getCount() - 2; i
>= 0; --i
) {
134 path
+= "/" + mDirectories
.getItem(i
);
136 OCFile parent
= mStorageManager
.getFileByPath(path
139 Thread thread
= new Thread(new DirectoryCreator(
143 OCFile new_file
= new OCFile(path
);
144 new_file
.setMimetype("DIR");
145 new_file
.setParentId(parent
.getParentId());
146 mStorageManager
.saveFile(new_file
);
151 builder
.setNegativeButton(R
.string
.common_cancel
,
152 new OnClickListener() {
153 public void onClick(DialogInterface dialog
, int which
) {
157 dialog
= builder
.create();
168 public void onCreate(Bundle savedInstanceState
) {
169 super.onCreate(savedInstanceState
);
171 if (!accountsAreSetup()) {
172 showDialog(DIALOG_SETUP_ACCOUNT
);
176 requestWindowFeature(Window
.FEATURE_INDETERMINATE_PROGRESS
);
177 setProgressBarIndeterminateVisibility(false
);
178 // if (getSupportFragmentManager().findFragmentById(R.id.fileList) ==
180 setContentView(R
.layout
.files
);
185 public boolean onOptionsItemSelected(MenuItem item
) {
186 boolean retval
= true
;
187 switch (item
.getItemId()) {
188 case R
.id
.settingsItem
: {
189 Intent i
= new Intent(this, Preferences
.class);
193 case R
.id
.createDirectoryItem
: {
194 showDialog(DIALOG_CREATE_DIR
);
197 case R
.id
.startSync
: {
198 Bundle bundle
= new Bundle();
199 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
200 ContentResolver
.requestSync(
201 AccountUtils
.getCurrentOwnCloudAccount(this),
202 "org.owncloud", bundle
);
205 case R
.id
.action_upload
: {
206 Intent action
= new Intent(Intent
.ACTION_GET_CONTENT
);
207 action
= action
.setType("*/*")
208 .addCategory(Intent
.CATEGORY_OPENABLE
);
209 startActivityForResult(
210 Intent
.createChooser(action
, "Upload file from..."),
215 case android
.R
.id
.home
: {
216 Intent i
= new Intent(this, AccountSelectActivity
.class);
228 public void onBackPressed() {
229 if (mDirectories
.getCount() == 1) {
234 ((FileListFragment
) getSupportFragmentManager().findFragmentById(
235 R
.id
.fileList
)).onNavigateUp();
239 public boolean onCreateOptionsMenu(Menu menu
) {
240 MenuInflater inflater
= getSherlock().getMenuInflater();
241 inflater
.inflate(R
.menu
.menu
, menu
);
246 protected void onRestoreInstanceState(Bundle savedInstanceState
) {
247 super.onRestoreInstanceState(savedInstanceState
);
248 // Check, if there are ownCloud accounts
249 if (!accountsAreSetup()) {
250 showDialog(DIALOG_SETUP_ACCOUNT
);
252 mDirs
= savedInstanceState
.getStringArray(KEY_DIR
);
253 mDirectories
= new CustomArrayAdapter
<String
>(this, R
.layout
.sherlock_spinner_dropdown_item
);
254 mDirectories
.add("/");
256 for (String s
: mDirs
)
257 mDirectories
.insert(s
, 0);
261 protected void onSaveInstanceState(Bundle outState
) {
262 super.onSaveInstanceState(outState
);
263 mDirs
= new String
[mDirectories
.getCount()-1];
264 for (int j
= mDirectories
.getCount() - 2, i
= 0; j
>= 0; --j
, ++i
) {
265 mDirs
[i
] = mDirectories
.getItem(j
);
270 protected void onResume() {
272 if (!accountsAreSetup()) {
273 showDialog(DIALOG_SETUP_ACCOUNT
);
277 IntentFilter f
= new IntentFilter(FileSyncService
.SYNC_MESSAGE
);
278 syncBroadcastRevceiver
= new SyncBroadcastReceiver();
279 registerReceiver(syncBroadcastRevceiver
, f
);
281 mDirectories
= new CustomArrayAdapter
<String
>(this, R
.layout
.sherlock_spinner_dropdown_item
);
282 mDirectories
.add("/");
284 for (String s
: mDirs
)
285 mDirectories
.insert(s
, 0);
286 FileListFragment fileListFramgent
= (FileListFragment
) getSupportFragmentManager()
287 .findFragmentById(R
.id
.fileList
);
288 if (fileListFramgent
!= null
) fileListFramgent
.populateFileList();
291 mStorageManager
= new FileDataStorageManager(
292 AccountUtils
.getCurrentOwnCloudAccount(this),
293 getContentResolver());
294 ActionBar action_bar
= getSupportActionBar();
295 action_bar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_LIST
);
296 action_bar
.setDisplayShowTitleEnabled(false
);
297 action_bar
.setListNavigationCallbacks(mDirectories
, this);
298 action_bar
.setDisplayHomeAsUpEnabled(true
);
301 public void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
302 Log
.e("ASD", requestCode
+ " " + resultCode
);
303 if (resultCode
== RESULT_OK
) {
304 if (requestCode
== ACTION_SELECT_FILE
) {
305 Uri selectedImageUri
= data
.getData();
307 String filemanagerstring
= selectedImageUri
.getPath();
308 String selectedImagePath
= getPath(selectedImageUri
);
311 if (selectedImagePath
!= null
)
312 filepath
= selectedImagePath
;
314 filepath
= filemanagerstring
;
316 if (filepath
== null
) {
317 Log
.e("FileDisplay", "Couldnt resolve path to file");
321 Intent i
= new Intent(this, FileUploader
.class);
322 i
.putExtra(FileUploader
.KEY_ACCOUNT
,
323 AccountUtils
.getCurrentOwnCloudAccount(this));
324 String remotepath
= new String();
325 for (int j
= mDirectories
.getCount() - 2; j
>= 0; --j
) {
326 remotepath
+= "/" + URLDecoder
.decode(mDirectories
.getItem(j
));
328 if (!remotepath
.endsWith("/"))
330 remotepath
+= new File(filepath
).getName();
331 Log
.e("ASD", remotepath
+ "");
333 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, filepath
);
334 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, remotepath
);
335 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
,
336 FileUploader
.UPLOAD_SINGLE_FILE
);
342 public String
getPath(Uri uri
) {
343 String
[] projection
= { MediaStore
.Images
.Media
.DATA
};
344 Cursor cursor
= managedQuery(uri
, projection
, null
, null
, null
);
345 if (cursor
!= null
) {
346 int column_index
= cursor
347 .getColumnIndexOrThrow(MediaStore
.Images
.Media
.DATA
);
348 cursor
.moveToFirst();
349 return cursor
.getString(column_index
);
355 protected void onPause() {
357 if (syncBroadcastRevceiver
!= null
) {
358 unregisterReceiver(syncBroadcastRevceiver
);
359 syncBroadcastRevceiver
= null
;
365 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
366 int i
= itemPosition
;
373 private class DirectoryCreator
implements Runnable
{
374 private String mTargetPath
;
375 private Account mAccount
;
376 private AccountManager mAm
;
378 public DirectoryCreator(String targetPath
, Account account
) {
379 mTargetPath
= targetPath
;
381 mAm
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
386 WebdavClient wdc
= new WebdavClient(Uri
.parse(mAm
.getUserData(
387 mAccount
, AccountAuthenticator
.KEY_OC_URL
)));
389 String username
= mAccount
.name
.substring(0,
390 mAccount
.name
.lastIndexOf('@'));
391 String password
= mAm
.getPassword(mAccount
);
393 wdc
.setCredentials(username
, password
);
394 wdc
.allowUnsignedCertificates();
395 wdc
.createDirectory(mTargetPath
);
400 // Custom array adapter to override text colors
401 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
403 public CustomArrayAdapter(FileDisplayActivity ctx
, int view
) {
407 public View
getView(int position
, View convertView
, ViewGroup parent
) {
408 View v
= super.getView(position
, convertView
, parent
);
410 ((TextView
) v
).setTextColor(getResources().getColorStateList(
411 android
.R
.color
.white
));
415 public View
getDropDownView(int position
, View convertView
,
417 View v
= super.getDropDownView(position
, convertView
, parent
);
419 ((TextView
) v
).setTextColor(getResources().getColorStateList(
420 android
.R
.color
.white
));
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 * Checks, whether or not there are any ownCloud accounts setup.
446 * @return true, if there is at least one account.
448 private boolean accountsAreSetup() {
449 AccountManager accMan
= AccountManager
.get(this);
450 Account
[] accounts
= accMan
451 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
452 return accounts
.length
> 0;
455 private class SyncBroadcastReceiver
extends BroadcastReceiver
{
457 * {@link BroadcastReceiver} to enable syncing feedback in UI
460 public void onReceive(Context context
, Intent intent
) {
461 boolean inProgress
= intent
.getBooleanExtra(
462 FileSyncService
.IN_PROGRESS
, false
);
463 String account_name
= intent
464 .getStringExtra(FileSyncService
.ACCOUNT_NAME
);
465 Log
.d("FileDisplay", "sync of account " + account_name
466 + " is in_progress: " + inProgress
);
467 setProgressBarIndeterminateVisibility(inProgress
);
469 FileListFragment fileListFramgent
= (FileListFragment
) getSupportFragmentManager()
470 .findFragmentById(R
.id
.fileList
);
471 if (fileListFramgent
!= null
)
472 fileListFramgent
.populateFileList();