1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
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.
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.
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/>.
18 package eu
.alefzero
.owncloud
.ui
.fragment
;
20 import java
.util
.Stack
;
21 import java
.util
.Vector
;
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
;
45 * A Fragment that lists all files and folders in a given path.
47 * @author Bartek Przybylski
50 public class FileListFragment
extends FragmentListView
{
51 private Account mAccount
;
52 private Stack
<String
> mDirNames
;
53 private Vector
<OCFile
> mFiles
;
54 private DataStorageManager mStorageManager
;
56 public FileListFragment() {
57 mDirNames
= new Stack
<String
>();
61 public void onCreate(Bundle savedInstanceState
) {
62 super.onCreate(savedInstanceState
);
64 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
65 getListView().setDivider(
66 getResources().getDrawable(R
.drawable
.uploader_list_separator
));
67 getListView().setDividerHeight(1);
73 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
74 if (mFiles
.size() <= position
) {
75 throw new IndexOutOfBoundsException("Incorrect item selected");
77 OCFile file
= mFiles
.get(position
);
79 // Update ActionBarPath
80 if (file
.getMimetype().equals("DIR")) {
81 String dirname
= file
.getFileName();
83 mDirNames
.push(dirname
);
84 ((FileDisplayActivity
) getActivity()).pushPath(dirname
);
92 Intent showDetailsIntent
= new Intent(getActivity(),
93 FileDetailActivity
.class);
94 showDetailsIntent
.putExtra(FileDetailFragment
.FILE
, file
);
95 showDetailsIntent
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
97 // Try to find by tag first
98 FileDetailFragment fd
= (FileDetailFragment
) getFragmentManager()
99 .findFragmentByTag("FileDetails");
101 // Could be the first time the user has touched a file. find by id
103 fd
= (FileDetailFragment
) getFragmentManager().findFragmentById(
107 // Tablets will have this fragment, phones not. Could still be null
110 if (fd
.isEmptyLayout()) {
111 // True, if this is the first time a user taps on a file
112 FragmentTransaction transaction
= getFragmentManager()
114 fd
= new FileDetailFragment(showDetailsIntent
);
115 transaction
.replace(R
.id
.fileDetail
, fd
, "FileDetails");
116 transaction
.setTransition(FragmentTransaction
.TRANSIT_FRAGMENT_FADE
);
117 transaction
.commit();
119 fd
.updateFileDetails(showDetailsIntent
);
123 startActivity(showDetailsIntent
);
128 * Resets the FileDetailsFragment on Tablets
129 * so that it always displays "Tab on a file to display it's details"
131 private void resetFileFragment() {
132 FileDetailFragment fileDetails
= (FileDetailFragment
) getFragmentManager().findFragmentByTag("FileDetails");
133 if(fileDetails
!= null
){
134 FragmentTransaction transaction
= getFragmentManager().beginTransaction();
135 transaction
.remove(fileDetails
);
136 transaction
.add(R
.id
.file_details_container
, new FileDetailFragment());
137 transaction
.commit();
142 public boolean onItemLongClick(AdapterView
<?
> arg0
, View arg1
, int arg2
,
144 ClipData
.Item item
= new ClipData
.Item("ASD");
145 ClipDescription cd
= new ClipDescription("ASD",
146 new String
[] { ClipDescription
.MIMETYPE_TEXT_PLAIN
});
147 ClipData dragData
= new ClipData(cd
, item
);
148 arg1
.startDrag(dragData
,
149 new View
.DragShadowBuilder(arg0
.getChildAt(arg2
)), null
, 0);
154 * Call this, when the user presses the up button
156 public void onNavigateUp() {
163 * Lists the directory
165 public void populateFileList() {
167 for (String a
: mDirNames
)
171 mStorageManager
= new FileDataStorageManager(mAccount
, getActivity()
172 .getContentResolver());
173 OCFile file
= mStorageManager
.getFileByPath(s
);
174 mFiles
= mStorageManager
.getDirectoryContent(file
);
175 if (mFiles
== null
|| mFiles
.size() == 0) {
176 Toast
.makeText(getActivity(), "There are no files here",
177 Toast
.LENGTH_LONG
).show();
179 setListAdapter(new FileListListAdapter(file
, mStorageManager
,
183 // TODO: Delete this testing stuff.
185 * private void addContact(Account account, String name, String username) {
186 * Log.i("ASD", "Adding contact: " + name);
187 * ArrayList<ContentProviderOperation> operationList = new
188 * ArrayList<ContentProviderOperation>();
190 * //Create our RawContact ContentProviderOperation.Builder builder =
191 * ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);
192 * builder.withValue(RawContacts.ACCOUNT_NAME, account.name);
193 * builder.withValue(RawContacts.ACCOUNT_TYPE, account.type);
194 * builder.withValue(RawContacts.SYNC1, username);
195 * operationList.add(builder.build());
197 * //Create a Data record of common type 'StructuredName' for our RawContact
199 * ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
201 * .withValueBackReference(ContactsContract.CommonDataKinds.StructuredName
202 * .RAW_CONTACT_ID, 0); builder.withValue(ContactsContract.Data.MIMETYPE,
203 * ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
205 * .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
206 * name); operationList.add(builder.build());
208 * //Create a Data record of custom type
209 * "vnd.android.cursor.item/vnd.fm.last.android.profile" to display a link
210 * to the Last.fm profile builder =
211 * ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
212 * builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0);
213 * builder.withValue(ContactsContract.Data.MIMETYPE,
214 * "vnd.android.cursor.item/vnd.owncloud.contact.profile");
215 * builder.withValue(ContactsContract.Data.DATA1, username);
216 * builder.withValue(ContactsContract.Data.DATA2, "Last.fm Profile");
217 * builder.withValue(ContactsContract.Data.DATA3, "View profile");
218 * operationList.add(builder.build());
221 * getActivity().getContentResolver().applyBatch(ContactsContract.AUTHORITY,
222 * operationList); } catch (Exception e) { Log.e("ASD",
223 * "Something went wrong during creation! " + e); e.printStackTrace(); } }