5647bf56d3d98558d0ecfc242776dec7a273b7e1
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / fragment / FileListFragment.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.Stack;
21 import java.util.Vector;
22
23 import android.accounts.Account;
24 import android.content.ClipData;
25 import android.content.ClipDescription;
26 import android.content.Intent;
27 import android.os.Bundle;
28 import android.support.v4.app.FragmentTransaction;
29 import android.util.Log;
30 import android.view.View;
31 import android.widget.AdapterView;
32 import android.widget.Toast;
33 import eu.alefzero.owncloud.AccountUtils;
34 import eu.alefzero.owncloud.FileDownloader;
35 import eu.alefzero.owncloud.R;
36 import eu.alefzero.owncloud.datamodel.DataStorageManager;
37 import eu.alefzero.owncloud.datamodel.FileDataStorageManager;
38 import eu.alefzero.owncloud.datamodel.OCFile;
39 import eu.alefzero.owncloud.ui.FragmentListView;
40 import eu.alefzero.owncloud.ui.activity.FileDetailActivity;
41 import eu.alefzero.owncloud.ui.activity.FileDisplayActivity;
42 import eu.alefzero.owncloud.ui.adapter.FileListListAdapter;
43
44 /**
45 * A Fragment that lists all files and folders in a given path.
46 *
47 * @author Bartek Przybylski
48 *
49 */
50 public class FileListFragment extends FragmentListView {
51 private Account mAccount;
52 private Stack<String> mDirNames;
53 private Vector<OCFile> mFiles;
54 private DataStorageManager mStorageManager;
55
56 public FileListFragment() {
57 mDirNames = new Stack<String>();
58 }
59
60 @Override
61 public void onCreate(Bundle savedInstanceState) {
62 super.onCreate(savedInstanceState);
63
64 mAccount = AccountUtils.getCurrentOwnCloudAccount(getActivity());
65 getListView().setDivider(
66 getResources().getDrawable(R.drawable.uploader_list_separator));
67 getListView().setDividerHeight(1);
68
69 populateFileList();
70 }
71
72 @Override
73 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
74 if (mFiles.size() <= position) {
75 throw new IndexOutOfBoundsException("Incorrect item selected");
76 }
77 OCFile file = mFiles.get(position);
78
79 // Update ActionBarPath
80 if (file.getMimetype().equals("DIR")) {
81 String dirname = file.getFileName();
82
83 mDirNames.push(dirname);
84 ((FileDisplayActivity) getActivity()).pushPath(dirname);
85
86 populateFileList();
87 return;
88 }
89
90 Intent showDetailsIntent = new Intent(getActivity(),
91 FileDetailActivity.class);
92 showDetailsIntent.putExtra(FileDetailFragment.FILE, file);
93 showDetailsIntent.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
94
95 // Try to find by tag first
96 FileDetailFragment fd = (FileDetailFragment) getFragmentManager()
97 .findFragmentByTag("FileDetails");
98
99 // Could be the first time the user has touched a file. find by id
100 if (fd == null) {
101 fd = (FileDetailFragment) getFragmentManager().findFragmentById(
102 R.id.fileDetail);
103 }
104
105 // Tablets will have this fragment, phones not. Could still be null
106 if (fd != null) {
107
108 if (fd.isEmptyLayout()) {
109 // True, if this is the first time a user taps on a file
110 fd = new FileDetailFragment(showDetailsIntent);
111 FragmentTransaction transaction = getFragmentManager()
112 .beginTransaction();
113 transaction.replace(R.id.file_details_container, fd,
114 "FileDetails");
115 transaction.commit();
116 } else {
117 fd.updateFileDetails(showDetailsIntent);
118 }
119
120 } else {
121 startActivity(showDetailsIntent);
122 }
123 }
124
125 @Override
126 public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
127 long arg3) {
128 ClipData.Item item = new ClipData.Item("ASD");
129 ClipDescription cd = new ClipDescription("ASD",
130 new String[] { ClipDescription.MIMETYPE_TEXT_PLAIN });
131 ClipData dragData = new ClipData(cd, item);
132 arg1.startDrag(dragData,
133 new View.DragShadowBuilder(arg0.getChildAt(arg2)), null, 0);
134 return true;
135 }
136
137 /**
138 * Call this, when the user presses the up button
139 */
140 public void onNavigateUp() {
141 mDirNames.pop();
142 populateFileList();
143 }
144
145 /**
146 * Lists the directory
147 */
148 public void populateFileList() {
149 String s = "/";
150 for (String a : mDirNames)
151 s += a + "/";
152 Log.e("ASD", s);
153
154 mStorageManager = new FileDataStorageManager(mAccount, getActivity()
155 .getContentResolver());
156 OCFile file = mStorageManager.getFileByPath(s);
157 mFiles = mStorageManager.getDirectoryContent(file);
158 if (mFiles == null || mFiles.size() == 0) {
159 Toast.makeText(getActivity(), "There are no files here",
160 Toast.LENGTH_LONG).show();
161 }
162 setListAdapter(new FileListListAdapter(file, mStorageManager,
163 getActivity()));
164 }
165
166 // TODO: Delete this testing stuff.
167 /*
168 * private void addContact(Account account, String name, String username) {
169 * Log.i("ASD", "Adding contact: " + name);
170 * ArrayList<ContentProviderOperation> operationList = new
171 * ArrayList<ContentProviderOperation>();
172 *
173 * //Create our RawContact ContentProviderOperation.Builder builder =
174 * ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);
175 * builder.withValue(RawContacts.ACCOUNT_NAME, account.name);
176 * builder.withValue(RawContacts.ACCOUNT_TYPE, account.type);
177 * builder.withValue(RawContacts.SYNC1, username);
178 * operationList.add(builder.build());
179 *
180 * //Create a Data record of common type 'StructuredName' for our RawContact
181 * builder =
182 * ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
183 * builder
184 * .withValueBackReference(ContactsContract.CommonDataKinds.StructuredName
185 * .RAW_CONTACT_ID, 0); builder.withValue(ContactsContract.Data.MIMETYPE,
186 * ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
187 * builder
188 * .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
189 * name); operationList.add(builder.build());
190 *
191 * //Create a Data record of custom type
192 * "vnd.android.cursor.item/vnd.fm.last.android.profile" to display a link
193 * to the Last.fm profile builder =
194 * ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
195 * builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0);
196 * builder.withValue(ContactsContract.Data.MIMETYPE,
197 * "vnd.android.cursor.item/vnd.owncloud.contact.profile");
198 * builder.withValue(ContactsContract.Data.DATA1, username);
199 * builder.withValue(ContactsContract.Data.DATA2, "Last.fm Profile");
200 * builder.withValue(ContactsContract.Data.DATA3, "View profile");
201 * operationList.add(builder.build());
202 *
203 * try {
204 * getActivity().getContentResolver().applyBatch(ContactsContract.AUTHORITY,
205 * operationList); } catch (Exception e) { Log.e("ASD",
206 * "Something went wrong during creation! " + e); e.printStackTrace(); } }
207 */
208
209 }