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
.ContentResolver
;
32 import android
.content
.Intent
;
33 import android
.content
.IntentFilter
;
34 import android
.net
.Uri
;
35 import android
.os
.Bundle
;
36 import android
.util
.Log
;
37 import android
.view
.View
;
38 import android
.view
.ViewGroup
;
39 import android
.widget
.ArrayAdapter
;
40 import android
.widget
.EditText
;
41 import android
.widget
.TextView
;
43 import com
.actionbarsherlock
.app
.ActionBar
;
44 import com
.actionbarsherlock
.app
.ActionBar
.OnNavigationListener
;
45 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
46 import com
.actionbarsherlock
.view
.Menu
;
47 import com
.actionbarsherlock
.view
.MenuInflater
;
48 import com
.actionbarsherlock
.view
.MenuItem
;
49 import com
.actionbarsherlock
.view
.Window
;
51 import eu
.alefzero
.owncloud
.AccountUtils
;
52 import eu
.alefzero
.owncloud
.R
;
53 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
54 import eu
.alefzero
.owncloud
.datamodel
.DataStorageManager
;
55 import eu
.alefzero
.owncloud
.datamodel
.FileDataStorageManager
;
56 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
57 import eu
.alefzero
.owncloud
.db
.ProviderMeta
.ProviderTableMeta
;
58 import eu
.alefzero
.owncloud
.syncadapter
.FileSyncAdapter
;
59 import eu
.alefzero
.owncloud
.syncadapter
.FileSyncService
;
60 import eu
.alefzero
.owncloud
.ui
.fragment
.FileListFragment
;
61 import eu
.alefzero
.webdav
.WebdavClient
;
64 * Displays, what files the user has available in his ownCloud.
66 * @author Bartek Przybylski
70 public class FileDisplayActivity
extends SherlockFragmentActivity
implements
71 OnNavigationListener
, OnClickListener
{
72 private ArrayAdapter
<String
> mDirectories
;
73 private DataStorageManager mStorageManager
;
77 private static final int DIALOG_SETUP_ACCOUNT
= 0;
78 private static final int DIALOG_CREATE_DIR
= 1;
80 public void pushPath(String path
) {
81 mDirectories
.insert(path
, 0);
84 public boolean popPath() {
85 mDirectories
.remove(mDirectories
.getItem(0));
86 return !mDirectories
.isEmpty();
90 protected Dialog
onCreateDialog(int id
) {
92 AlertDialog
.Builder builder
;
94 case DIALOG_SETUP_ACCOUNT
:
95 builder
= new AlertDialog
.Builder(this);
96 builder
.setTitle(R
.string
.main_tit_accsetup
);
97 builder
.setMessage(R
.string
.main_wrn_accsetup
);
98 builder
.setCancelable(false
);
99 builder
.setPositiveButton(R
.string
.common_ok
, this);
100 builder
.setNegativeButton(R
.string
.common_cancel
, this);
101 dialog
= builder
.create();
103 case DIALOG_CREATE_DIR
:
105 builder
= new Builder(this);
106 final EditText dirName
= new EditText(getBaseContext());
107 final Account a
= AccountUtils
.getCurrentOwnCloudAccount(this);
108 builder
.setView(dirName
);
109 builder
.setTitle(R
.string
.uploader_info_dirname
);
110 dirName
.setTextColor(R
.color
.setup_text_typed
);
112 builder
.setPositiveButton(R
.string
.common_ok
, new OnClickListener() {
113 public void onClick(DialogInterface dialog
, int which
) {
114 String s
= dirName
.getText().toString();
115 if (s
.trim().isEmpty()) {
121 for (int i
= mDirectories
.getCount() - 2; i
>= 0; --i
) {
122 path
+= "/" + mDirectories
.getItem(i
);
124 OCFile parent
= mStorageManager
.getFileByPath(path
+ "/");
126 Thread thread
= new Thread(new DirectoryCreator(path
, a
));
129 OCFile new_file
= new OCFile(path
);
130 new_file
.setMimetype("DIR");
131 new_file
.setParentId(parent
.getParentId());
132 mStorageManager
.saveFile(new_file
);
137 builder
.setNegativeButton(R
.string
.common_cancel
,
138 new OnClickListener() {
139 public void onClick(DialogInterface dialog
, int which
) {
152 public void onCreate(Bundle savedInstanceState
) {
153 super.onCreate(savedInstanceState
);
154 if(!accountsAreSetup()){
155 showDialog(DIALOG_SETUP_ACCOUNT
);
159 requestWindowFeature(Window
.FEATURE_INDETERMINATE_PROGRESS
);
161 mDirectories
= new CustomArrayAdapter
<String
>(this,
162 R
.layout
.sherlock_spinner_dropdown_item
);
163 mDirectories
.add("/");
164 setContentView(R
.layout
.files
);
165 mStorageManager
= new FileDataStorageManager(AccountUtils
.getCurrentOwnCloudAccount(this), getContentResolver());
166 ActionBar action_bar
= getSupportActionBar();
167 action_bar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_LIST
);
168 action_bar
.setDisplayShowTitleEnabled(false
);
169 action_bar
.setListNavigationCallbacks(mDirectories
, this);
170 action_bar
.setDisplayHomeAsUpEnabled(true
);
174 public boolean onOptionsItemSelected(MenuItem item
) {
175 switch (item
.getItemId()) {
176 case R
.id
.settingsItem
: {
177 Intent i
= new Intent(this, Preferences
.class);
181 case R
.id
.createDirectoryItem
: {
182 showDialog(DIALOG_CREATE_DIR
);
185 case R
.id
.startSync
: {
186 Bundle bundle
= new Bundle();
187 bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
);
188 ContentResolver
.requestSync(AccountUtils
.getCurrentOwnCloudAccount(this),
193 case android
.R
.id
.home
: {
203 public void onBackPressed(){
204 if(mDirectories
.getCount() == 1) {
209 ((FileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
))
214 public boolean onCreateOptionsMenu(Menu menu
) {
215 MenuInflater inflater
= getSherlock().getMenuInflater();
216 inflater
.inflate(R
.menu
.menu
, menu
);
221 protected void onRestoreInstanceState(Bundle savedInstanceState
) {
222 super.onRestoreInstanceState(savedInstanceState
);
223 // Check, if there are ownCloud accounts
224 if(!accountsAreSetup()){
225 showDialog(DIALOG_SETUP_ACCOUNT
);
230 protected void onStart() {
232 // Check, if there are ownCloud accounts
233 if(!accountsAreSetup()){
234 showDialog(DIALOG_SETUP_ACCOUNT
);
239 protected void onResume() {
241 if(!accountsAreSetup()){
242 showDialog(DIALOG_SETUP_ACCOUNT
);
245 IntentFilter f
= new IntentFilter(FileSyncService
.SYNC_MESSAGE
);
247 registerReceiver(b
, f
);
248 setProgressBarIndeterminateVisibility(false
);
252 protected void onPause() {
254 unregisterReceiver(b
);
258 public boolean onNavigationItemSelected(int itemPosition
, long itemId
) {
259 int i
= itemPosition
;
266 private class DirectoryCreator
implements Runnable
{
267 private String mTargetPath
;
268 private Account mAccount
;
269 private AccountManager mAm
;
271 public DirectoryCreator(String targetPath
, Account account
) {
272 mTargetPath
= targetPath
;
274 mAm
= (AccountManager
) getSystemService(ACCOUNT_SERVICE
);
279 WebdavClient wdc
= new WebdavClient(Uri
.parse(mAm
.getUserData(
280 mAccount
, AccountAuthenticator
.KEY_OC_URL
)));
282 String username
= mAccount
.name
.substring(0,
283 mAccount
.name
.lastIndexOf('@'));
284 String password
= mAm
.getPassword(mAccount
);
286 wdc
.setCredentials(username
, password
);
287 wdc
.allowUnsignedCertificates();
288 wdc
.createDirectory(mTargetPath
);
293 // Custom array adapter to override text colors
294 private class CustomArrayAdapter
<T
> extends ArrayAdapter
<T
> {
296 public CustomArrayAdapter(FileDisplayActivity ctx
,
301 public View
getView(int position
, View convertView
,
303 View v
= super.getView(position
, convertView
, parent
);
305 ((TextView
) v
).setTextColor(
307 .getColorStateList(android
.R
.color
.white
));
311 public View
getDropDownView(int position
, View convertView
,
313 View v
= super.getDropDownView(position
, convertView
,
316 ((TextView
) v
).setTextColor(getResources().getColorStateList(
317 android
.R
.color
.white
));
326 public void onClick(DialogInterface dialog
, int which
) {
327 // In any case - we won't need it anymore
330 case DialogInterface
.BUTTON_POSITIVE
:
331 Intent intent
= new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
332 intent
.putExtra("authorities",
333 new String
[] { AccountAuthenticator
.AUTH_TOKEN_TYPE
});
334 startActivity(intent
);
336 case DialogInterface
.BUTTON_NEGATIVE
:
343 * Checks, whether or not there are any ownCloud accounts
346 * @return true, if there is at least one account.
348 private boolean accountsAreSetup() {
349 AccountManager accMan
= AccountManager
.get(this);
350 Account
[] accounts
= accMan
351 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
);
352 return accounts
.length
> 0;
355 private class BR
extends BroadcastReceiver
{
357 public void onReceive(Context context
, Intent intent
) {
358 boolean in_progress
= intent
.getBooleanExtra(FileSyncService
.IN_PROGRESS
, false
);
359 String account_name
= intent
.getStringExtra(FileSyncService
.ACCOUNT_NAME
);
360 Log
.d("FileDisplay", "sync of account " + account_name
+ " is in_progress: " + in_progress
);
361 setProgressBarIndeterminateVisibility(in_progress
);
363 FileListFragment f
= (FileListFragment
) getSupportFragmentManager().findFragmentById(R
.id
.fileList
);
365 f
.populateFileList();