New Layout for FileDetailsFragment (needs to be redone with
[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 private FileDetailFragment mFileDetailsDetailFragment;
56 private boolean mIsLargeDevice = false;
57
58 public FileListFragment() {
59 mDirNames = new Stack<String>();
60 }
61
62 @Override
63 public void onCreate(Bundle savedInstanceState) {
64 super.onCreate(savedInstanceState);
65
66 mAccount = AccountUtils.getCurrentOwnCloudAccount(getActivity());
67 getListView().setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
68 getListView().setDividerHeight(1);
69
70 populateFileList();
71 }
72
73 @Override
74 public void onStart() {
75 // Inflate the original fragment for better performance
76 mFileDetailsDetailFragment = new FileDetailFragment();
77
78 // Create a placeholder upon launch
79 View fragmentContainer = getActivity().findViewById(R.id.file_details_container);
80 if (fragmentContainer != null) {
81 mIsLargeDevice = true;
82 FragmentTransaction transaction = getFragmentManager().beginTransaction();
83 transaction.replace(R.id.file_details_container, new FileDetailFragment(true));
84 transaction.commit();
85 }
86 super.onStart();
87 }
88
89 @Override
90 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
91 if (mFiles.size() <= position) {
92 throw new IndexOutOfBoundsException("Incorrect item selected");
93 }
94 OCFile file = mFiles.get(position);
95
96 // Update ActionBarPath
97 if (file.getMimetype().equals("DIR")) {
98 String dirname = file.getFileName();
99
100 mDirNames.push(dirname);
101 ((FileDisplayActivity) getActivity()).pushPath(dirname);
102
103 populateFileList();
104 resetFileFragment();
105
106 return;
107 }
108
109 Intent showDetailsIntent = new Intent(getActivity(), FileDetailActivity.class);
110 showDetailsIntent.putExtra(FileDetailFragment.FILE, file);
111 showDetailsIntent.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
112
113 // If we are on a large device -> update fragment
114 if (mIsLargeDevice) {
115 FileDetailFragment fileDetails = (FileDetailFragment) getFragmentManager().findFragmentByTag("FileDetails");
116 if (fileDetails == null) {
117 FragmentTransaction transaction = getFragmentManager().beginTransaction();
118 transaction.replace(R.id.file_details_container, mFileDetailsDetailFragment, "FileDetails");
119 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
120 transaction.commit();
121 } else {
122 fileDetails.updateFileDetails(showDetailsIntent);
123 }
124 } else {
125 startActivity(showDetailsIntent);
126 }
127 }
128
129 /**
130 * Resets the FileDetailsFragment on Tablets so that it always displays
131 * "Tab on a file to display it's details"
132 */
133 private void resetFileFragment() {
134 FileDetailFragment fileDetails = (FileDetailFragment) getFragmentManager().findFragmentByTag("FileDetails");
135 if (fileDetails != null) {
136 FragmentTransaction transaction = getFragmentManager().beginTransaction();
137 transaction.remove(fileDetails);
138 transaction.add(R.id.file_details_container, new FileDetailFragment(true));
139 transaction.commit();
140 }
141 }
142
143 @Override
144 public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
145 ClipData.Item item = new ClipData.Item("ASD");
146 ClipDescription cd = new ClipDescription("ASD", new String[] { ClipDescription.MIMETYPE_TEXT_PLAIN });
147 ClipData dragData = new ClipData(cd, item);
148 arg1.startDrag(dragData, new View.DragShadowBuilder(arg0.getChildAt(arg2)), null, 0);
149 return true;
150 }
151
152 /**
153 * Call this, when the user presses the up button
154 */
155 public void onNavigateUp() {
156 mDirNames.pop();
157 populateFileList();
158 resetFileFragment();
159 }
160
161 /**
162 * Lists the directory
163 */
164 public void populateFileList() {
165 String s = "/";
166 for (String a : mDirNames)
167 s += a + "/";
168 Log.e("ASD", s);
169
170 mStorageManager = new FileDataStorageManager(mAccount, getActivity().getContentResolver());
171 OCFile file = mStorageManager.getFileByPath(s);
172 mFiles = mStorageManager.getDirectoryContent(file);
173 if (mFiles == null || mFiles.size() == 0) {
174 Toast.makeText(getActivity(), "There are no files here", Toast.LENGTH_LONG).show();
175 }
176 setListAdapter(new FileListListAdapter(file, mStorageManager, getActivity()));
177 }
178
179 // TODO: Delete this testing stuff.
180 /*
181 * private void addContact(Account account, String name, String username) {
182 * Log.i("ASD", "Adding contact: " + name);
183 * ArrayList<ContentProviderOperation> operationList = new
184 * ArrayList<ContentProviderOperation>();
185 *
186 * //Create our RawContact ContentProviderOperation.Builder builder =
187 * ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);
188 * builder.withValue(RawContacts.ACCOUNT_NAME, account.name);
189 * builder.withValue(RawContacts.ACCOUNT_TYPE, account.type);
190 * builder.withValue(RawContacts.SYNC1, username);
191 * operationList.add(builder.build());
192 *
193 * //Create a Data record of common type 'StructuredName' for our RawContact
194 * builder =
195 * ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
196 * builder
197 * .withValueBackReference(ContactsContract.CommonDataKinds.StructuredName
198 * .RAW_CONTACT_ID, 0); builder.withValue(ContactsContract.Data.MIMETYPE,
199 * ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
200 * builder
201 * .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
202 * name); operationList.add(builder.build());
203 *
204 * //Create a Data record of custom type
205 * "vnd.android.cursor.item/vnd.fm.last.android.profile" to display a link
206 * to the Last.fm profile builder =
207 * ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
208 * builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0);
209 * builder.withValue(ContactsContract.Data.MIMETYPE,
210 * "vnd.android.cursor.item/vnd.owncloud.contact.profile");
211 * builder.withValue(ContactsContract.Data.DATA1, username);
212 * builder.withValue(ContactsContract.Data.DATA2, "Last.fm Profile");
213 * builder.withValue(ContactsContract.Data.DATA3, "View profile");
214 * operationList.add(builder.build());
215 *
216 * try {
217 * getActivity().getContentResolver().applyBatch(ContactsContract.AUTHORITY,
218 * operationList); } catch (Exception e) { Log.e("ASD",
219 * "Something went wrong during creation! " + e); e.printStackTrace(); } }
220 */
221
222 }