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