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