fix download file with noascii chars in name
[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.ClipData;
25 import android.content.ClipDescription;
26 import android.content.Intent;
27 import android.os.Bundle;
28 import android.support.v4.app.FragmentTransaction;
29 import android.util.Log;
30 import android.view.View;
31 import android.widget.AdapterView;
32 import android.widget.Toast;
33 import eu.alefzero.owncloud.AccountUtils;
34 import eu.alefzero.owncloud.FileDownloader;
35 import eu.alefzero.owncloud.R;
36 import eu.alefzero.owncloud.datamodel.DataStorageManager;
37 import eu.alefzero.owncloud.datamodel.FileDataStorageManager;
38 import eu.alefzero.owncloud.datamodel.OCFile;
39 import eu.alefzero.owncloud.ui.FragmentListView;
40 import eu.alefzero.owncloud.ui.activity.FileDetailActivity;
41 import eu.alefzero.owncloud.ui.activity.FileDisplayActivity;
42 import eu.alefzero.owncloud.ui.adapter.FileListListAdapter;
43
44 /**
45 * A Fragment that lists all files and folders in a given path.
46 *
47 * @author Bartek Przybylski
48 *
49 */
50 public class FileListFragment extends FragmentListView {
51 private Account mAccount;
52 private Stack<String> mDirNames;
53 private Vector<OCFile> mFiles;
54 private DataStorageManager mStorageManager;
55 private boolean mIsLargeDevice = false;
56
57 public FileListFragment() {
58 mDirNames = new Stack<String>();
59 }
60
61 @Override
62 public void onCreate(Bundle savedInstanceState) {
63 super.onCreate(savedInstanceState);
64
65 mAccount = AccountUtils.getCurrentOwnCloudAccount(getActivity());
66 getListView().setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
67 getListView().setDividerHeight(1);
68
69 populateFileList();
70 }
71
72 @Override
73 public void onStart() {
74
75 // Create a placeholder upon launch
76 View fragmentContainer = getActivity().findViewById(R.id.file_details_container);
77 if (fragmentContainer != null) {
78 mIsLargeDevice = true;
79 FragmentTransaction transaction = getFragmentManager().beginTransaction();
80 transaction.replace(R.id.file_details_container, new FileDetailFragment(true));
81 transaction.commit();
82 }
83 super.onStart();
84 }
85
86 @Override
87 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
88 if (mFiles.size() <= position) {
89 throw new IndexOutOfBoundsException("Incorrect item selected");
90 }
91 OCFile file = mFiles.get(position);
92
93 // Update ActionBarPath
94 if (file.getMimetype().equals("DIR")) {
95 String dirname = file.getFileName();
96
97 mDirNames.push(dirname);
98 ((FileDisplayActivity) getActivity()).pushPath(dirname);
99
100 populateFileList();
101 resetFileFragment();
102
103 return;
104 }
105
106 Intent showDetailsIntent = new Intent(getActivity(), FileDetailActivity.class);
107 showDetailsIntent.putExtra(FileDetailFragment.FILE, file);
108 showDetailsIntent.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
109
110 // If we are on a large device -> update fragment
111 if (mIsLargeDevice) {
112 FileDetailFragment fileDetails = (FileDetailFragment) getFragmentManager().findFragmentByTag("FileDetails");
113 if (fileDetails == null) {
114 FragmentTransaction transaction = getFragmentManager().beginTransaction();
115 transaction.replace(R.id.file_details_container, new FileDetailFragment(showDetailsIntent), "FileDetails");
116 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
117 transaction.commit();
118 } else {
119 fileDetails.updateFileDetails(showDetailsIntent);
120 }
121 } else {
122 startActivity(showDetailsIntent);
123 }
124 }
125
126 /**
127 * Resets the FileDetailsFragment on Tablets so that it always displays
128 * "Tab on a file to display it's details"
129 */
130 private void resetFileFragment() {
131 FileDetailFragment fileDetails = (FileDetailFragment) getFragmentManager().findFragmentByTag("FileDetails");
132 if (fileDetails != null) {
133 FragmentTransaction transaction = getFragmentManager().beginTransaction();
134 transaction.remove(fileDetails);
135 transaction.add(R.id.file_details_container, new FileDetailFragment(true));
136 transaction.commit();
137 }
138 }
139
140 /**
141 * Call this, when the user presses the up button
142 */
143 public void onNavigateUp() {
144 mDirNames.pop();
145 populateFileList();
146 resetFileFragment();
147 }
148
149 /**
150 * Lists the directory
151 */
152 public void populateFileList() {
153 String s = "/";
154 for (String a : mDirNames)
155 s += a + "/";
156 Log.e("ASD", s);
157
158 mStorageManager = new FileDataStorageManager(mAccount, getActivity().getContentResolver());
159 OCFile file = mStorageManager.getFileByPath(s);
160 mFiles = mStorageManager.getDirectoryContent(file);
161 if (mFiles == null || mFiles.size() == 0) {
162 Toast.makeText(getActivity(), "There are no files here", Toast.LENGTH_LONG).show();
163 }
164 setListAdapter(new FileListListAdapter(file, mStorageManager, getActivity()));
165 }
166
167 @Override
168 public void onSaveInstanceState(Bundle outState) {
169 super.onSaveInstanceState(outState);
170 outState.putParcelable("ACCOUNT", mAccount);
171 }
172
173 // TODO: Delete this testing stuff.
174 /*
175 * private void addContact(Account account, String name, String username) {
176 * Log.i("ASD", "Adding contact: " + name);
177 * ArrayList<ContentProviderOperation> operationList = new
178 * ArrayList<ContentProviderOperation>();
179 *
180 * //Create our RawContact ContentProviderOperation.Builder builder =
181 * ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);
182 * builder.withValue(RawContacts.ACCOUNT_NAME, account.name);
183 * builder.withValue(RawContacts.ACCOUNT_TYPE, account.type);
184 * builder.withValue(RawContacts.SYNC1, username);
185 * operationList.add(builder.build());
186 *
187 * //Create a Data record of common type 'StructuredName' for our RawContact
188 * builder =
189 * ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
190 * builder
191 * .withValueBackReference(ContactsContract.CommonDataKinds.StructuredName
192 * .RAW_CONTACT_ID, 0); builder.withValue(ContactsContract.Data.MIMETYPE,
193 * ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
194 * builder
195 * .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
196 * name); operationList.add(builder.build());
197 *
198 * //Create a Data record of custom type
199 * "vnd.android.cursor.item/vnd.fm.last.android.profile" to display a link
200 * to the Last.fm profile builder =
201 * ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
202 * builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0);
203 * builder.withValue(ContactsContract.Data.MIMETYPE,
204 * "vnd.android.cursor.item/vnd.owncloud.contact.profile");
205 * builder.withValue(ContactsContract.Data.DATA1, username);
206 * builder.withValue(ContactsContract.Data.DATA2, "Last.fm Profile");
207 * builder.withValue(ContactsContract.Data.DATA3, "View profile");
208 * operationList.add(builder.build());
209 *
210 * try {
211 * getActivity().getContentResolver().applyBatch(ContactsContract.AUTHORITY,
212 * operationList); } catch (Exception e) { Log.e("ASD",
213 * "Something went wrong during creation! " + e); e.printStackTrace(); } }
214 */
215
216 }