1fb8abccdc1d85eda1d4d5d946add38a7f9a3a9f
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / fragment / FileList.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18 package eu.alefzero.owncloud.ui.fragment;
19
20 import eu.alefzero.owncloud.R;
21 import eu.alefzero.owncloud.R.id;
22 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
23 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
24 import eu.alefzero.owncloud.ui.activity.FileDetailActivity;
25 import eu.alefzero.owncloud.ui.adapter.FileListListAdapter;
26 import eu.alefzero.owncloud.ui.fragment.ActionBar;
27 import android.accounts.Account;
28 import android.accounts.AccountManager;
29 import android.app.Activity;
30 import android.app.Service;
31 import android.content.Intent;
32 import android.database.Cursor;
33 import android.net.Uri;
34 import android.os.Bundle;
35 import android.support.v4.app.FragmentTransaction;
36 import android.support.v4.app.ListFragment;
37 import android.view.LayoutInflater;
38 import android.view.View;
39 import android.view.ViewGroup;
40 import android.widget.ArrayAdapter;
41 import android.widget.ListView;
42 import android.widget.TextView;
43 import android.widget.Toast;
44
45 /**
46 * A Fragment that lists all files and folders in a given path.
47 * @author Bartek Przybylski
48 *
49 */
50 public class FileList extends ListFragment {
51 private Cursor mCursor;
52 private Account mAccount;
53 private AccountManager mAccountManager;
54 private View mheaderView;
55
56
57 @Override
58 public void onCreate(Bundle savedInstanceState) {
59 // TODO Auto-generated method stub
60 super.onCreate(savedInstanceState);
61
62 mAccountManager = (AccountManager)getActivity().getSystemService(Service.ACCOUNT_SERVICE);
63 mAccount = mAccountManager.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[0];
64 populateFileList();
65 }
66
67 @Override
68 public void onActivityCreated(Bundle savedInstanceState) {
69 // TODO Auto-generated method stub
70 super.onActivityCreated(savedInstanceState);
71 }
72
73 @Override
74 public void onListItemClick(ListView l, View v, int position, long id) {
75 FileDetail fd = (FileDetail) getFragmentManager().findFragmentById(R.id.fileDetail);
76 ActionBar ab = (ActionBar) getFragmentManager().findFragmentById(R.id.actionBar);
77
78 if (!mCursor.moveToPosition(position)) {
79 throw new IndexOutOfBoundsException("Incorrect item selected");
80 }
81 if (mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)).equals("DIR")) {
82 String id_ = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta._ID));
83 String dirname = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_NAME));
84 //ab..push(DisplayUtils.HtmlDecode(dirname));
85 //mPath.addLast(DisplayUtils.HtmlDecode(dirname));
86 //mParents.push(id_);
87 mCursor = getActivity().managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, id_),
88 null,
89 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
90 new String[]{mAccount.name}, null);
91 setListAdapter(new FileListListAdapter(mCursor, getActivity()));
92 setListShown(false);
93 setListShown(true);
94 super.onListItemClick(l, v, position, id);
95 return;
96 }
97 Intent i = new Intent(getActivity(), FileDetailActivity.class);
98 i.putExtra("FILE_PATH", ab.getCurrentPath());
99 i.putExtra("FILE_NAME", ((TextView)v.findViewById(R.id.Filename)).getText());
100 if (fd != null) {
101 fd.setStuff(i);
102 //fd.use(((TextView)v.findViewById(R.id.Filename)).getText());
103 } else {
104 i.putExtra("FILE_PATH", ab.getCurrentPath());
105 i.putExtra("FILE_NAME", ((TextView)v.findViewById(R.id.Filename)).getText());
106 startActivity(i);
107 }
108 super.onListItemClick(l, v, position, id);
109
110 }
111
112 @Override
113 public void onDestroyView() {
114 setListAdapter(null);
115 super.onDestroyView();
116 }
117
118 private void populateFileList() {
119 mCursor = getActivity().getContentResolver().query(ProviderTableMeta.CONTENT_URI,
120 null,
121 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
122 new String[]{mAccount.name},
123 null);
124
125 setListAdapter(new FileListListAdapter(mCursor, getActivity()));
126 }
127 }