1fb0c57dda200964050298db6b8d3d0b120a6811
[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.Intent;
25 import android.os.Bundle;
26 import android.util.Log;
27 import android.view.View;
28 import android.widget.AdapterView;
29 import android.widget.Toast;
30 import eu.alefzero.owncloud.R;
31 import eu.alefzero.owncloud.authenticator.AuthUtils;
32 import eu.alefzero.owncloud.datamodel.DataStorageManager;
33 import eu.alefzero.owncloud.datamodel.FileDataStorageManager;
34 import eu.alefzero.owncloud.datamodel.OCFile;
35 import eu.alefzero.owncloud.ui.FragmentListView;
36 import eu.alefzero.owncloud.ui.activity.FileDetailActivity;
37 import eu.alefzero.owncloud.ui.activity.FileDisplayActivity;
38 import eu.alefzero.owncloud.ui.adapter.FileListListAdapter;
39
40 /**
41 * A Fragment that lists all files and folders in a given path.
42 * @author Bartek Przybylski
43 *
44 */
45 public class FileListFragment extends FragmentListView {
46 private Account mAccount;
47 private Stack<String> mDirNames;
48 private Vector<OCFile> mFiles;
49 private DataStorageManager mStorageManager;
50
51 public FileListFragment() {
52 mDirNames = new Stack<String>();
53 }
54
55 @Override
56 public void onCreate(Bundle savedInstanceState) {
57 super.onCreate(savedInstanceState);
58
59 mAccount = AuthUtils.getCurrentOwnCloudAccount(getActivity());
60 populateFileList();
61 }
62
63 @Override
64 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
65 if (mFiles.size() <= position) {
66 throw new IndexOutOfBoundsException("Incorrect item selected");
67 }
68 OCFile file = mFiles.get(position);
69 String id_ = String.valueOf(file.getFileId());
70 if (file.getMimetype().equals("DIR")) {
71 String dirname = file.getFileName();
72
73 mDirNames.push(dirname);
74 ((FileDisplayActivity)getActivity()).pushPath(dirname);
75
76 populateFileList();
77 return;
78 }
79 Intent i = new Intent(getActivity(), FileDetailActivity.class);
80 i.putExtra("FILE_NAME", file.getFileName());
81 i.putExtra("FULL_PATH", file.getPath());
82 i.putExtra("FILE_ID", id_);
83 Log.e("ASD", mAccount+"");
84 i.putExtra("ACCOUNT", mAccount);
85 FileDetailFragment fd = (FileDetailFragment) getFragmentManager().findFragmentById(R.id.fileDetail);
86 if (fd != null) {
87 fd.setStuff(i);
88 } else {
89 startActivity(i);
90 }
91 }
92
93 /**
94 * Call this, when the user presses the up button
95 */
96 public void onNavigateUp() {
97 mDirNames.pop();
98 populateFileList();
99 }
100
101 /**
102 * Lists the directory
103 */
104 private void populateFileList() {
105 String s = "/";
106 for (String a : mDirNames)
107 s+= a + "/";
108 Log.e("ASD", s);
109
110 mStorageManager = new FileDataStorageManager(mAccount, getActivity().getContentResolver());
111 OCFile file = mStorageManager.getFileByPath(s);
112 mFiles = mStorageManager.getDirectoryContent(file);
113 if (mFiles == null || mFiles.size() == 0) {
114 Toast.makeText(getActivity(), "There are no files here", Toast.LENGTH_LONG).show();
115 }
116 setListAdapter(new FileListListAdapter(file, mStorageManager, getActivity()));
117 }
118
119 //TODO: Delete this testing stuff.
120 /*private void addContact(Account account, String name, String username) {
121 Log.i("ASD", "Adding contact: " + name);
122 ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
123
124 //Create our RawContact
125 ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);
126 builder.withValue(RawContacts.ACCOUNT_NAME, account.name);
127 builder.withValue(RawContacts.ACCOUNT_TYPE, account.type);
128 builder.withValue(RawContacts.SYNC1, username);
129 operationList.add(builder.build());
130
131 //Create a Data record of common type 'StructuredName' for our RawContact
132 builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
133 builder.withValueBackReference(ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID, 0);
134 builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
135 builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name);
136 operationList.add(builder.build());
137
138 //Create a Data record of custom type "vnd.android.cursor.item/vnd.fm.last.android.profile" to display a link to the Last.fm profile
139 builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
140 builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0);
141 builder.withValue(ContactsContract.Data.MIMETYPE, "vnd.android.cursor.item/vnd.owncloud.contact.profile");
142 builder.withValue(ContactsContract.Data.DATA1, username);
143 builder.withValue(ContactsContract.Data.DATA2, "Last.fm Profile");
144 builder.withValue(ContactsContract.Data.DATA3, "View profile");
145 operationList.add(builder.build());
146
147 try {
148 getActivity().getContentResolver().applyBatch(ContactsContract.AUTHORITY, operationList);
149 } catch (Exception e) {
150 Log.e("ASD", "Something went wrong during creation! " + e);
151 e.printStackTrace();
152 }
153 }*/
154
155 }