some file list handling, action bar added
[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 java.util.ListIterator;
21 import java.util.Stack;
22
23 import eu.alefzero.owncloud.DisplayUtils;
24 import eu.alefzero.owncloud.R;
25 import eu.alefzero.owncloud.R.id;
26 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
27 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
28 import eu.alefzero.owncloud.ui.FragmentListView;
29 import eu.alefzero.owncloud.ui.activity.FileDetailActivity;
30 import eu.alefzero.owncloud.ui.activity.FileDisplayActivity;
31 import eu.alefzero.owncloud.ui.adapter.FileListListAdapter;
32 import eu.alefzero.owncloud.ui.fragment.ActionBar;
33 import android.accounts.Account;
34 import android.accounts.AccountManager;
35 import android.app.Activity;
36 import android.app.Service;
37 import android.content.Intent;
38 import android.database.Cursor;
39 import android.net.Uri;
40 import android.os.Bundle;
41 import android.support.v4.app.FragmentTransaction;
42 import android.support.v4.app.Fragment;
43 import android.support.v4.app.ListFragment;
44 import android.util.Log;
45 import android.view.LayoutInflater;
46 import android.view.View;
47 import android.view.ViewGroup;
48 import android.widget.AdapterView;
49 import android.widget.ArrayAdapter;
50 import android.widget.ListView;
51 import android.widget.TextView;
52 import android.widget.Toast;
53 import android.widget.AdapterView.OnItemClickListener;
54
55 /**
56 * A Fragment that lists all files and folders in a given path.
57 * @author Bartek Przybylski
58 *
59 */
60 public class FileList extends FragmentListView {
61 private Cursor mCursor;
62 private Account mAccount;
63 private AccountManager mAccountManager;
64 private View mheaderView;
65 private Stack<String> mParentsIds;
66 private Stack<String> mDirNames;
67
68 @Override
69 public void onCreate(Bundle savedInstanceState) {
70 // TODO Auto-generated method stub
71 super.onCreate(savedInstanceState);
72
73 mParentsIds = new Stack<String>();
74 mDirNames = new Stack<String>();
75 mAccountManager = (AccountManager)getActivity().getSystemService(Service.ACCOUNT_SERVICE);
76 mAccount = mAccountManager.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[0];
77 populateFileList();
78 }
79
80 @Override
81 public void onActivityCreated(Bundle savedInstanceState) {
82 // TODO Auto-generated method stub
83 super.onActivityCreated(savedInstanceState);
84 }
85
86 @Override
87 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
88 FileDetail fd = (FileDetail) getFragmentManager().findFragmentById(R.id.fileDetail);
89 if (!mCursor.moveToPosition(position)) {
90 throw new IndexOutOfBoundsException("Incorrect item selected");
91 }
92
93 if (mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)).equals("DIR")) {
94 String id_ = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta._ID));
95 mParentsIds.push(id_);
96 String dirname = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_NAME));
97 mDirNames.push(dirname);
98 ((FileDisplayActivity)getActivity()).pushPath(DisplayUtils.HtmlDecode(dirname));
99 mCursor = getActivity().managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, id_),
100 null,
101 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
102 new String[]{mAccount.name}, null);
103 setListAdapter(new FileListListAdapter(mCursor, getActivity()));
104 //super.onListItemClick(l, v, position, id);
105 return;
106 }
107 Intent i = new Intent(getActivity(), FileDetailActivity.class);
108 i.putExtra("FILE_NAME", ((TextView)v.findViewById(R.id.Filename)).getText());
109 if (fd != null) {
110 fd.setStuff(i);
111 //fd.use(((TextView)v.findViewById(R.id.Filename)).getText());
112 } else {
113 i.putExtra("FILE_NAME", ((TextView)v.findViewById(R.id.Filename)).getText());
114 startActivity(i);
115 }
116 FragmentTransaction ft = getFragmentManager().beginTransaction();
117 ft.replace(R.id.fileList, this);
118 ft.commitAllowingStateLoss();
119 //super.onListItemClick(l, v, position, id);
120
121 }
122
123 @Override
124 public void onDestroyView() {
125 setListAdapter(null);
126 super.onDestroyView();
127 }
128
129 private void populateFileList() {
130 mCursor = getActivity().getContentResolver().query(ProviderTableMeta.CONTENT_URI,
131 null,
132 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
133 new String[]{mAccount.name},
134 null);
135
136 setListAdapter(new FileListListAdapter(mCursor, getActivity()));
137 }
138
139 public void onBackPressed() {
140 if (!mParentsIds.empty()) {
141 mParentsIds.pop();
142 mDirNames.pop();
143 }
144 if (!mParentsIds.empty()) {
145
146 String id_ = mParentsIds.peek();
147 String dirname = mDirNames.peek();
148 mCursor = getActivity().managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, id_),
149 null,
150 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
151 new String[]{mAccount.name}, null);
152 setListAdapter(new FileListListAdapter(mCursor, getActivity()));
153 } else {
154 populateFileList();
155 }
156
157 }
158 }