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
.ArrayList
;
21 import java
.util
.Stack
;
22 import java
.util
.Vector
;
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
;
44 * A Fragment that lists all files and folders in a given path.
45 * @author Bartek Przybylski
48 public class FileList
extends FragmentListView
{
49 private Account mAccount
;
50 private AccountManager mAccountManager
;
51 private Stack
<String
> mDirNames
;
52 private Vector
<OCFile
> mFiles
;
55 mDirNames
= new Stack
<String
>();
59 public void onCreate(Bundle savedInstanceState
) {
60 super.onCreate(savedInstanceState
);
62 mAccountManager
= (AccountManager
)getActivity().getSystemService(Service
.ACCOUNT_SERVICE
);
63 mAccount
= mAccountManager
.getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
)[0];
65 //addContact(mAccount, "Bartek Przybylski", "czlowiek");
69 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
70 if (mFiles
.size() <= position
) {
71 throw new IndexOutOfBoundsException("Incorrect item selected");
73 OCFile file
= mFiles
.get(position
);
74 String id_
= String
.valueOf(file
.getFileId());
75 if (file
.getMimetype().equals("DIR")) {
76 String dirname
= file
.getFileName();
78 mDirNames
.push(dirname
);
79 ((FileDisplayActivity
)getActivity()).pushPath(dirname
);
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
);
97 public void onBackPressed() {
102 private void populateFileList() {
104 for (String a
: mDirNames
)
107 OCFile file
= new OCFile(getActivity().getContentResolver(), mAccount
, s
);
108 mFiles
= file
.getDirectoryContent();
109 setListAdapter(new FileListListAdapter(file
, getActivity()));
112 private void addContact(Account account
, String name
, String username
) {
113 Log
.i("ASD", "Adding contact: " + name
);
114 ArrayList
<ContentProviderOperation
> operationList
= new ArrayList
<ContentProviderOperation
>();
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());
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());
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());
140 getActivity().getContentResolver().applyBatch(ContactsContract
.AUTHORITY
, operationList
);
141 } catch (Exception e
) {
142 Log
.e("ASD", "Something went wrong during creation! " + e
);