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 android
.accounts
.Account
;
22 import android
.accounts
.AccountManager
;
23 import android
.app
.AlertDialog
;
24 import android
.app
.AlertDialog
.Builder
;
25 import android
.app
.Dialog
;
26 import android
.content
.Context
;
27 import android
.content
.DialogInterface
;
28 import android
.content
.DialogInterface
.OnCancelListener
;
29 import android
.content
.DialogInterface
.OnClickListener
;
30 import android
.content
.BroadcastReceiver
;
31 import android
.content
.Intent
;
32 import android
.content
.IntentFilter
;
33 import android
.net
.Uri
;
34 import android
.os
.Bundle
;
35 import android
.util
.Log
;
36 import android
.view
.View
;
37 import android
.view
.ViewGroup
;
38 import android
.widget
.ArrayAdapter
;
39 import android
.widget
.EditText
;
40 import android
.widget
.TextView
;
42 import com
.actionbarsherlock
.app
.ActionBar
;
43 import com
.actionbarsherlock
.app
.ActionBar
.OnNavigationListener
;
44 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
45 import com
.actionbarsherlock
.view
.Menu
;
46 import com
.actionbarsherlock
.view
.MenuInflater
;
47 import com
.actionbarsherlock
.view
.MenuItem
;
48 import com
.actionbarsherlock
.view
.Window
;
50 import eu
.alefzero
.owncloud
.AccountUtils
;
51 import eu
.alefzero
.owncloud
.R
;
52 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
53 import eu
.alefzero
.owncloud
.datamodel
.DataStorageManager
;
54 import eu
.alefzero
.owncloud
.datamodel
.FileDataStorageManager
;
55 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
56 import eu
.alefzero
.owncloud
.syncadapter
.FileSyncAdapter
;
57 import eu
.alefzero
.owncloud
.ui
.fragment
.FileListFragment
;
58 import eu
.alefzero
.webdav
.WebdavClient
;
61 * Displays, what files the user has available in his ownCloud.
63 * @author Bartek Przybylski
67 public class FileDisplayActivity
extends SherlockFragmentActivity
implements
68 OnNavigationListener
, OnClickListener
{
69 private ArrayAdapter
<String
> mDirectories
;
70 private DataStorageManager mStorageManager
;
74 private static final int DIALOG_SETUP_ACCOUNT
= 0;
75 private static final int DIALOG_CREATE_DIR
= 1;
77 public void pushPath(String path
) {
78 mDirectories
.insert(path
, 0);
81 public boolean popPath() {
82 mDirectories
.remove(mDirectories
.getItem(0));
83 return !mDirectories
.isEmpty();
87 protected Dialog
onCreateDialog(int id
) {
89 AlertDialog
.Builder builder
;
91 case DIALOG_SETUP_ACCOUNT
:
92 builder
= new AlertDialog
.Builder(this);
93 builder
.setTitle(R
.string
.main_tit_accsetup
);
94 builder
.setMessage(R
.string
.main_wrn_accsetup
);
95 builder
.setCancelable(false
);
96 builder
.setPositiveButton(R
.string
.common_ok
, this);
97 builder
.setNegativeButton(R
.string
.common_cancel
, this);
98 dialog
= builder
.create();
100 case DIALOG_CREATE_DIR
:
102 builder
= new Builder(this);
103 final EditText dirName
= new EditText(getBaseContext());
104 final Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
105 builder
.setView(dirName
);
106 builder
.setTitle(R
.string
.uploader_info_dirname
);
107 dirName
.setTextColor(R
.color
.setup_text_typed
);
109 builder
.setPositiveButton(R
.string
.common_ok
, new OnClickListener() {
110 public void onClick(DialogInterface dialog
, int which
) {
111 String s
= dirName
.getText().toString();
112 if (s
.trim().isEmpty()) {
118 for (int i
= mDirectories
.getCount() - 2; i
>= 0; --i
) {
119 path
+= "/" + mDirectories
.getItem(i
);
121 OCFile parent
= mStorageManager
.getFileByPath(path
+ "/");
123 Thread thread
= new Thread(new DirectoryCreator(path
, a
));
126 OCFile new_file
= new OCFile(path
);
127 new_file
.setMimetype("DIR");
128 new_file
.setParentId(parent
.getParentId());
129 mStorageManager
.saveFile(new_file
);
134 builder
.setNegativeButton(R
.string
.common_cancel
,
135 new OnClickListener() {
136 public void onClick(DialogInterface dialog
, int which
) {
149 public void onCreate(Bundle savedInstanceState
) {
150 super.onCreate(savedInstanceState
);
151 if(!accountsAreSetup()){
152 showDialog(DIALOG_SETUP_ACCOUNT
);
156 requestWindowFeature(Window
.FEATURE_INDETERMINATE_PROGRESS
);
158 mDirectories
= new CustomArrayAdapter
<String
>(this,
159 R
.layout
.sherlock_spinner_dropdown_item
);
160 mDirectories
.add("/");
161 setContentView(R
.layout
.files
);
162 mStorageManager
= new FileDataStorageManager(AccountUtils
.getCurrentOwnCloudAccount(this), getContentResolver());
163 ActionBar action_bar
= getSupportActionBar();
164 action_bar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_LIST
);
165 action_bar
.setDisplayShowTitleEnabled(false
);
166 action_bar
.setListNavigationCallbacks(mDirectories
, this);
167 action_bar
.setDisplayHomeAsUpEnabled(true
);
171 public boolean onOptionsItemSelected(MenuItem item
) {
172 switch (item
.getItemId()) {
173 case R
.id
.settingsItem
: {
174 Intent i
= new Intent(this, Preferences
.class);
178 case R
.id
.createDirectoryItem
: {
179 showDialog(DIALOG_CREATE_DIR
);
182 case android
.R
.id
.home
: {
192 public void onBackPressed(){
193 if(mDirectories
.getCount() == 1) {
198 ((FileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
))
203 public boolean onCreateOptionsMenu(Menu menu
) {
204 MenuInflater inflater
= getSherlock().getMenuInflater();
205 inflater
.inflate(R
.menu
.menu
, menu
);
210 protected void onRestoreInstanceState(Bundle savedInstanceState
) {
211 super.onRestoreInstanceState(savedInstanceState
);
212 // Check, if there are ownCloud accounts
213 if(!accountsAreSetup()){
214 showDialog(DIALOG_SETUP_ACCOUNT
);
219 protected void onStart() {
221 // Check, if there are ownCloud accounts
222 if(!accountsAreSetup()){
223 showDialog(DIALOG_SETUP_ACCOUNT
);
228 protected void onResume() {
230 if(!accountsAreSetup()){
231 showDialog(DIALOG_SETUP_ACCOUNT
);
234 IntentFilter f
= new IntentFilter(FileSyncAdapter
.SYNC_MESSAGE
);
236 registerReceiver(b
, f
);
237 setProgressBarIndeterminateVisibility(false
);
241 protected void onPause() {
243 unregisterReceiver(b
);
247 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
248 int i
= itemPosition
;
255 private class DirectoryCreator
implements Runnable
{
256 private String mTargetPath
;
257 private Account mAccount
;
258 private AccountManager mAm
;
260 public DirectoryCreator(String targetPath
, Account account
) {
261 mTargetPath
= targetPath
;
263 mAm
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
268 WebdavClient wdc
= new WebdavClient(Uri
.parse(mAm
.getUserData(
269 mAccount
, AccountAuthenticator
.KEY_OC_URL
)));
271 String username
= mAccount
.name
.substring(0,
272 mAccount
.name
.lastIndexOf('@'));
273 String password
= mAm
.getPassword(mAccount
);
275 wdc
.setCredentials(username
, password
);
276 wdc
.allowUnsignedCertificates();
277 wdc
.createDirectory(mTargetPath
);
282 // Custom array adapter to override text colors
283 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
285 public CustomArrayAdapter(FileDisplayActivity ctx
,
290 public View
getView(int position
, View convertView
,
292 View v
= super.getView(position
, convertView
, parent
);
294 ((TextView
) v
).setTextColor(
296 .getColorStateList(android
.R
.color
.white
));
300 public View
getDropDownView(int position
, View convertView
,
302 View v
= super.getDropDownView(position
, convertView
,
305 ((TextView
) v
).setTextColor(getResources().getColorStateList(
306 android
.R
.color
.white
));
315 public void onClick(DialogInterface dialog
, int which
) {
316 // In any case - we won't need it anymore
319 case DialogInterface
.BUTTON_POSITIVE
:
320 Intent intent
= new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
321 intent
.putExtra("authorities",
322 new String
[] { AccountAuthenticator
.AUTH_TOKEN_TYPE
});
323 startActivity(intent
);
325 case DialogInterface
.BUTTON_NEGATIVE
:
332 * Checks, whether or not there are any ownCloud accounts
335 * @return true, if there is at least one account.
337 private boolean accountsAreSetup() {
338 AccountManager accMan
= AccountManager
.get(this);
339 Account
[] accounts
= accMan
340 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
341 return accounts
.length
> 0;
344 private class BR
extends BroadcastReceiver
{
346 public void onReceive(Context context
, Intent intent
) {
347 boolean in_progress
= intent
.getBooleanExtra(FileSyncAdapter
.IN_PROGRESS
, false
);
348 String account_name
= intent
.getStringExtra(FileSyncAdapter
.ACCOUNT_NAME
);
349 Log
.d("FileDisplay", "sync of account " + account_name
+ " is in_progress: " + in_progress
);
350 setProgressBarIndeterminateVisibility(in_progress
);
352 FileListFragment f
= (FileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
);
354 f
.populateFileList();