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