912312caa87a9f1c0b39244f8c4bbefe1560e52d
[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 android.accounts.Account;
21 import android.accounts.AccountManager;
22 import android.app.FragmentManager;
23 import android.app.Service;
24 import android.content.Intent;
25 import android.database.Cursor;
26 import android.net.Uri;
27 import android.os.Bundle;
28 import android.support.v4.app.FragmentTransaction;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.widget.AdapterView;
33 import android.widget.TextView;
34 import eu.alefzero.owncloud.R;
35 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
36 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
37 import eu.alefzero.owncloud.ui.FragmentListView;
38 import eu.alefzero.owncloud.ui.activity.FileDetailActivity;
39 import eu.alefzero.owncloud.ui.activity.FileDisplayActivity;
40 import eu.alefzero.owncloud.ui.adapter.FileListListAdapter;
41
42 /**
43 * A Fragment that lists all files and folders in a given path.
44 * @author Bartek Przybylski
45 *
46 */
47 public class FileList extends FragmentListView {
48 private Cursor mCursor;
49 private Account mAccount;
50 private AccountManager mAccountManager;
51 private String mDirName;
52 private String mParentId;
53
54 public FileList() {
55 mDirName = null;
56 mParentId = null;
57 }
58
59 public FileList(String dirName, String parentId) {
60 mDirName = dirName;
61 mParentId = parentId;
62 }
63
64 @Override
65 public void onCreate(Bundle savedInstanceState) {
66 super.onCreate(savedInstanceState);
67
68 mAccountManager = (AccountManager)getActivity().getSystemService(Service.ACCOUNT_SERVICE);
69 mAccount = mAccountManager.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[0];
70 populateFileList();
71 }
72
73 @Override
74 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
75 FileDetail fd = (FileDetail) getFragmentManager().findFragmentById(R.id.fileDetail);
76 if (!mCursor.moveToPosition(position)) {
77 throw new IndexOutOfBoundsException("Incorrect item selected");
78 }
79
80 if (mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)).equals("DIR")) {
81 String id_ = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta._ID));
82 String dirname = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_NAME));
83
84 FileList fl = new FileList(dirname, id_);
85 ((FileDisplayActivity)getActivity()).pushPath(dirname);
86
87 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
88 ft.addToBackStack(null);
89 ft.replace(R.id.file_list_container, fl);
90 ft.commit();
91 getSupportFragmentManager().executePendingTransactions();
92 return;
93 }
94 Intent i = new Intent(getActivity(), FileDetailActivity.class);
95 i.putExtra("FILE_NAME", ((TextView)v.findViewById(R.id.Filename)).getText());
96 if (fd != null) {
97 fd.setStuff(i);
98 } else {
99 startActivity(i);
100 }
101 }
102
103 private void populateFileList() {
104 if (mParentId == null || mDirName == null) {
105 mCursor = getActivity().getContentResolver().query(ProviderTableMeta.CONTENT_URI,
106 null,
107 ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?",
108 new String[]{mAccount.name},
109 null);
110 } else {
111 mCursor = getActivity().managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, mParentId),
112 null,
113 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
114 new String[]{mAccount.name}, null);
115 }
116 setListAdapter(new FileListListAdapter(mCursor, getActivity()));
117 }
118 }