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
.DialogInterface
;
27 import android
.content
.DialogInterface
.OnCancelListener
;
28 import android
.content
.DialogInterface
.OnClickListener
;
29 import android
.content
.Intent
;
30 import android
.net
.Uri
;
31 import android
.os
.Bundle
;
32 import android
.view
.View
;
33 import android
.view
.ViewGroup
;
34 import android
.widget
.ArrayAdapter
;
35 import android
.widget
.EditText
;
36 import android
.widget
.TextView
;
38 import com
.actionbarsherlock
.app
.ActionBar
;
39 import com
.actionbarsherlock
.app
.ActionBar
.OnNavigationListener
;
40 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
41 import com
.actionbarsherlock
.view
.Menu
;
42 import com
.actionbarsherlock
.view
.MenuInflater
;
43 import com
.actionbarsherlock
.view
.MenuItem
;
45 import eu
.alefzero
.owncloud
.R
;
46 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
47 import eu
.alefzero
.owncloud
.authenticator
.AuthUtils
;
48 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
49 import eu
.alefzero
.owncloud
.ui
.fragment
.FileListFragment
;
50 import eu
.alefzero
.webdav
.WebdavClient
;
53 * Displays, what files the user has available in his ownCloud.
55 * @author Bartek Przybylski
59 public class FileDisplayActivity
extends SherlockFragmentActivity
implements
60 OnNavigationListener
{
61 private ArrayAdapter
<String
> mDirectories
;
63 private static final int DIALOG_CHOOSE_ACCOUNT
= 0;
65 public void pushPath(String path
) {
66 mDirectories
.insert(path
, 0);
69 public boolean popPath() {
70 mDirectories
.remove(mDirectories
.getItem(0));
71 return !mDirectories
.isEmpty();
75 protected Dialog
onCreateDialog(int id
, Bundle args
) {
76 final AlertDialog
.Builder builder
= new Builder(this);
77 final EditText dirName
= new EditText(getBaseContext());
78 final Account a
= AuthUtils
.getCurrentOwnCloudAccount(this);
79 builder
.setView(dirName
);
80 builder
.setTitle(R
.string
.uploader_info_dirname
);
81 dirName
.setTextColor(R
.color
.setup_text_typed
);
83 builder
.setPositiveButton(R
.string
.common_ok
, new OnClickListener() {
84 public void onClick(DialogInterface dialog
, int which
) {
85 String s
= dirName
.getText().toString();
86 if (s
.trim().isEmpty()) {
92 for (int i
= mDirectories
.getCount() - 2; i
>= 0; --i
) {
93 path
+= "/" + mDirectories
.getItem(i
);
95 OCFile parent
= new OCFile(getContentResolver(), a
, path
+ "/");
96 path
+= "/" + s
+ "/";
97 Thread thread
= new Thread(new DirectoryCreator(path
, a
));
99 OCFile
.createNewFile(getContentResolver(), a
, path
, 0, 0, 0,
100 "DIR", parent
.getFileId()).save();
105 builder
.setNegativeButton(R
.string
.common_cancel
,
106 new OnClickListener() {
107 public void onClick(DialogInterface dialog
, int which
) {
111 return builder
.create();
115 public void onCreate(Bundle savedInstanceState
) {
116 super.onCreate(savedInstanceState
);
117 mDirectories
= new CustomArrayAdapter
<String
>(this,
118 R
.layout
.sherlock_spinner_dropdown_item
);
119 mDirectories
.add("/");
120 setContentView(R
.layout
.files
);
121 ActionBar action_bar
= getSupportActionBar();
122 action_bar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_LIST
);
123 action_bar
.setDisplayShowTitleEnabled(false
);
124 action_bar
.setListNavigationCallbacks(mDirectories
, this);
125 action_bar
.setDisplayHomeAsUpEnabled(true
);
129 public boolean onOptionsItemSelected(MenuItem item
) {
130 switch (item
.getItemId()) {
131 case R
.id
.settingsItem
: {
132 Intent i
= new Intent(this, Preferences
.class);
136 case R
.id
.createDirectoryItem
: {
140 case android
.R
.id
.home
: {
150 public void onBackPressed(){
152 if(mDirectories
.getCount() == 0) {
153 Intent intent
= new Intent(this, LandingActivity
.class);
154 startActivity(intent
);
157 ((FileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
))
162 protected Dialog
onCreateDialog(int id
) {
164 case DIALOG_CHOOSE_ACCOUNT
:
165 return createChooseAccountDialog();
167 throw new IllegalArgumentException("Unknown dialog id: " + id
);
172 public boolean onCreateOptionsMenu(Menu menu
) {
173 MenuInflater inflater
= getSherlock().getMenuInflater();
174 inflater
.inflate(R
.menu
.menu
, menu
);
178 private Dialog
createChooseAccountDialog() {
179 final AccountManager accMan
= AccountManager
.get(this);
180 CharSequence
[] items
= new CharSequence
[accMan
181 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
).length
];
183 for (Account a
: accMan
184 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)) {
188 AlertDialog
.Builder builder
= new AlertDialog
.Builder(this);
189 builder
.setTitle(R
.string
.common_choose_account
);
190 builder
.setCancelable(true
);
191 builder
.setItems(items
, new DialogInterface
.OnClickListener() {
192 public void onClick(DialogInterface dialog
, int item
) {
194 // accMan.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[item];
198 builder
.setOnCancelListener(new OnCancelListener() {
199 public void onCancel(DialogInterface dialog
) {
200 FileDisplayActivity
.this.finish();
203 AlertDialog alert
= builder
.create();
208 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
209 int i
= itemPosition
;
216 private class DirectoryCreator
implements Runnable
{
217 private String mTargetPath
;
218 private Account mAccount
;
219 private AccountManager mAm
;
221 public DirectoryCreator(String targetPath
, Account account
) {
222 mTargetPath
= targetPath
;
224 mAm
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
229 WebdavClient wdc
= new WebdavClient(Uri
.parse(mAm
.getUserData(
230 mAccount
, AccountAuthenticator
.KEY_OC_URL
)));
232 String username
= mAccount
.name
.substring(0,
233 mAccount
.name
.lastIndexOf('@'));
234 String password
= mAm
.getPassword(mAccount
);
236 wdc
.setCredentials(username
, password
);
237 wdc
.allowUnsignedCertificates();
238 wdc
.createDirectory(mTargetPath
);
243 // Custom array adapter to override text colors
244 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
246 public CustomArrayAdapter(FileDisplayActivity ctx
,
251 public View
getView(int position
, View convertView
,
253 View v
= super.getView(position
, convertView
, parent
);
255 ((TextView
) v
).setTextColor(
257 .getColorStateList(android
.R
.color
.white
));
261 public View
getDropDownView(int position
, View convertView
,
263 View v
= super.getDropDownView(position
, convertView
,
266 ((TextView
) v
).setTextColor(getResources().getColorStateList(
267 android
.R
.color
.white
));