107b2a2f5647ea4ef5d76cbe4e41a668bc28ced8
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / preview / PreviewTextFragment.java
1 package com.owncloud.android.ui.preview;
2
3 import android.accounts.Account;
4 import android.os.AsyncTask;
5 import android.os.Bundle;
6 import android.support.v4.app.Fragment;
7 import android.support.v4.app.FragmentManager;
8 import android.support.v4.app.FragmentTransaction;
9 import android.view.LayoutInflater;
10 import android.view.View;
11 import android.view.ViewGroup;
12 import android.widget.ScrollView;
13 import android.widget.TextView;
14
15 import com.actionbarsherlock.view.Menu;
16 import com.actionbarsherlock.view.MenuInflater;
17 import com.actionbarsherlock.view.MenuItem;
18 import com.owncloud.android.R;
19 import com.owncloud.android.datamodel.OCFile;
20 import com.owncloud.android.files.FileMenuFilter;
21 import com.owncloud.android.lib.common.utils.Log_OC;
22 import com.owncloud.android.ui.activity.FileDisplayActivity;
23 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
24 import com.owncloud.android.ui.dialog.LoadingDialog;
25 import com.owncloud.android.ui.dialog.RemoveFileDialogFragment;
26 import com.owncloud.android.ui.fragment.FileFragment;
27
28 import java.io.BufferedWriter;
29 import java.io.FileInputStream;
30 import java.io.IOException;
31 import java.io.StringWriter;
32 import java.util.Scanner;
33
34 public class PreviewTextFragment extends FileFragment {
35 private static final String EXTRA_FILE = "FILE";
36 private static final String EXTRA_ACCOUNT = "ACCOUNT";
37 private static final String TAG = PreviewTextFragment.class.getSimpleName();
38
39 private Account mAccount;
40 private TextView mTextPreview;
41
42 /**
43 * Creates an empty fragment for previews.
44 * <p/>
45 * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically
46 * (for instance, when the device is turned a aside).
47 * <p/>
48 * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful
49 * construction
50 */
51 public PreviewTextFragment() {
52 super();
53 mAccount = null;
54 }
55
56 /**
57 * {@inheritDoc}
58 */
59 @Override
60 public View onCreateView(LayoutInflater inflater, ViewGroup container,
61 Bundle savedInstanceState) {
62 super.onCreateView(inflater, container, savedInstanceState);
63 Log_OC.e(TAG, "onCreateView");
64
65
66 View ret = inflater.inflate(R.layout.text_file_preview, container, false);
67
68 mTextPreview = (TextView) ret.findViewById(R.id.text_preview);
69
70 return ret;
71 }
72
73 /**
74 * {@inheritDoc}
75 */
76 @Override
77 public void onCreate(Bundle savedInstanceState) {
78 super.onCreate(savedInstanceState);
79
80 OCFile file = getFile();
81
82 Bundle args = getArguments();
83
84 if (file == null){
85 file = args.getParcelable(FileDisplayActivity.EXTRA_FILE);
86 }
87
88 if (mAccount == null) {
89 mAccount = args.getParcelable(FileDisplayActivity.EXTRA_ACCOUNT);
90 }
91
92 if (savedInstanceState == null) {
93 if (file == null) {
94 throw new IllegalStateException("Instanced with a NULL OCFile");
95 }
96 if (mAccount == null) {
97 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
98 }
99 } else {
100 file = savedInstanceState.getParcelable(EXTRA_FILE);
101 mAccount = savedInstanceState.getParcelable(EXTRA_ACCOUNT);
102 }
103 setFile(file);
104 setHasOptionsMenu(true);
105 }
106
107 /**
108 * {@inheritDoc}
109 */
110 @Override
111 public void onSaveInstanceState(Bundle outState) {
112 super.onSaveInstanceState(outState);
113 outState.putParcelable(PreviewImageFragment.EXTRA_FILE, getFile());
114 outState.putParcelable(PreviewImageFragment.EXTRA_ACCOUNT, mAccount);
115 }
116
117 @Override
118 public void onStart() {
119 super.onStart();
120 Log_OC.e(TAG, "onStart");
121 }
122
123 private void loadAndShowTextPreview() {
124 new TextLoadAsyncTask().execute(getFile().getStoragePath(), mTextPreview);
125 }
126
127 /**
128 * Reads the file to preview and shows its contents. Too critical to be anonymous.
129 */
130 private class TextLoadAsyncTask extends AsyncTask<Object, Void, StringWriter> {
131 private final String DIALOG_WAIT_TAG = "DIALOG_WAIT";
132 private TextView mTextView;
133
134 @Override
135 protected void onPreExecute() {
136 showLoadingDialog();
137 }
138
139 @Override
140 protected StringWriter doInBackground(java.lang.Object... params) {
141 if (params.length != 2) {
142 throw new IllegalArgumentException("The parameters to " + TextLoadAsyncTask.class.getName() + " must be (1) the file location and (2) the text view to update");}
143 final String location = (String) params[0];
144 mTextView = (TextView) params[1];
145
146 FileInputStream inputStream = null;
147 Scanner sc = null;
148 StringWriter source = new StringWriter();
149 BufferedWriter bufferedWriter = new BufferedWriter(source);
150 try {
151 inputStream = new FileInputStream(location);
152 sc = new Scanner(inputStream);
153 while (sc.hasNextLine()) {
154 bufferedWriter.append(sc.nextLine());
155 if (sc.hasNextLine()) bufferedWriter.append("\n");
156 }
157 bufferedWriter.close();
158 IOException exc = sc.ioException();
159 if (exc != null) throw exc;
160 } catch (IOException e) {
161 finish();
162 } finally {
163 if (inputStream != null) {
164 try {
165 inputStream.close();
166 } catch (IOException e) {
167 finish();
168 }
169 }
170 if (sc != null) {
171 sc.close();
172 }
173 }
174 return source;
175 }
176
177 @Override
178 protected void onPostExecute(final StringWriter stringWriter) {
179 mTextView.setText(new String(stringWriter.getBuffer()));
180 mTextView.setVisibility(View.VISIBLE);
181 dismissLoadingDialog();
182 }
183
184 /**
185 * Show loading dialog
186 */
187 public void showLoadingDialog() {
188 // Construct dialog
189 LoadingDialog loading = new LoadingDialog(getResources().getString(R.string.wait_a_moment));
190 FragmentManager fm = getActivity().getSupportFragmentManager();
191 FragmentTransaction ft = fm.beginTransaction();
192 loading.show(ft, DIALOG_WAIT_TAG);
193 }
194
195 /**
196 * Dismiss loading dialog
197 */
198 public void dismissLoadingDialog() {
199 final Fragment frag = getActivity().getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG);
200 if (frag != null) {
201 LoadingDialog loading = (LoadingDialog) frag;
202 loading.dismiss();
203 }
204 }
205 }
206
207 /**
208 * {@inheritDoc}
209 */
210 @Override
211 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
212 super.onCreateOptionsMenu(menu, inflater);
213 inflater.inflate(R.menu.file_actions_menu, menu);
214 }
215
216 /**
217 * {@inheritDoc}
218 */
219 @Override
220 public void onPrepareOptionsMenu(Menu menu) {
221 super.onPrepareOptionsMenu(menu);
222
223 if (mContainerActivity.getStorageManager() != null) {
224 FileMenuFilter mf = new FileMenuFilter(
225 getFile(),
226 mContainerActivity.getStorageManager().getAccount(),
227 mContainerActivity,
228 getSherlockActivity()
229 );
230 mf.filter(menu);
231 }
232
233 // additional restriction for this fragment
234 MenuItem item = menu.findItem(R.id.action_rename_file);
235 if (item != null) {
236 item.setVisible(false);
237 item.setEnabled(false);
238 }
239
240 // additional restriction for this fragment
241 item = menu.findItem(R.id.action_move);
242 if (item != null) {
243 item.setVisible(false);
244 item.setEnabled(false);
245 }
246
247 // this one doesn't make sense since the file has to be down in order to be previewed
248 item = menu.findItem(R.id.action_download_file);
249 if (item != null) {
250 item.setVisible(false);
251 item.setEnabled(false);
252 }
253
254 item = menu.findItem(R.id.action_settings);
255 if (item != null) {
256 item.setVisible(false);
257 item.setEnabled(false);
258 }
259
260 item = menu.findItem(R.id.action_logger);
261 if (item != null) {
262 item.setVisible(false);
263 item.setEnabled(false);
264 }
265
266 item = menu.findItem(R.id.action_sync_file);
267 if (item != null) {
268 item.setVisible(false);
269 item.setEnabled(false);
270 }
271
272 item = menu.findItem(R.id.action_sync_account);
273 if (item != null) {
274 item.setVisible(false);
275 item.setEnabled(false);
276 }
277 }
278
279 /**
280 * {@inheritDoc}
281 */
282 @Override
283 public boolean onOptionsItemSelected(MenuItem item) {
284 switch (item.getItemId()) {
285 case R.id.action_share_file: {
286 mContainerActivity.getFileOperationsHelper().shareFileWithLink(getFile());
287 return true;
288 }
289 case R.id.action_unshare_file: {
290 mContainerActivity.getFileOperationsHelper().unshareFileWithLink(getFile());
291 return true;
292 }
293 case R.id.action_open_file_with: {
294 openFile();
295 return true;
296 }
297 case R.id.action_remove_file: {
298 RemoveFileDialogFragment dialog = RemoveFileDialogFragment.newInstance(getFile());
299 dialog.show(getFragmentManager(), ConfirmationDialogFragment.FTAG_CONFIRMATION);
300 return true;
301 }
302 case R.id.action_see_details: {
303 seeDetails();
304 return true;
305 }
306 case R.id.action_send_file: {
307 sendFile();
308 return true;
309 }
310 case R.id.action_sync_file: {
311 mContainerActivity.getFileOperationsHelper().syncFile(getFile());
312 return true;
313 }
314
315 default:
316 return false;
317 }
318 }
319
320 /**
321 * Update the file of the fragment with file value
322 *
323 * @param file The new file to set
324 */
325 public void updateFile(OCFile file) {
326 setFile(file);
327 }
328
329 private void sendFile() {
330 mContainerActivity.getFileOperationsHelper().sendDownloadedFile(getFile());
331 }
332
333 private void seeDetails() {
334 mContainerActivity.showDetails(getFile());
335 }
336
337 @Override
338 public void onPause() {
339 Log_OC.e(TAG, "onPause");
340 super.onPause();
341 }
342
343 @Override
344 public void onResume() {
345 super.onResume();
346 Log_OC.e(TAG, "onResume");
347
348 loadAndShowTextPreview();
349 }
350
351 @Override
352 public void onDestroy() {
353 Log_OC.e(TAG, "onDestroy");
354 super.onDestroy();
355 }
356
357 @Override
358 public void onStop() {
359 super.onStop();
360 Log_OC.e(TAG, "onStop");
361 }
362
363 /**
364 * Opens the previewed file with an external application.
365 */
366 private void openFile() {
367 mContainerActivity.getFileOperationsHelper().openFile(getFile());
368 finish();
369 }
370
371 /**
372 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewTextFragment} to be previewed.
373 *
374 * @param file File to test if can be previewed.
375 * @return 'True' if the file can be handled by the fragment.
376 */
377 public static boolean canBePreviewed(OCFile file) {
378 return (file != null && file.isDown() && file.isText());
379 }
380
381 /**
382 * Finishes the preview
383 */
384 private void finish() {
385 getActivity().runOnUiThread(new Runnable() {
386 @Override
387 public void run() {
388 getSherlockActivity().onBackPressed();
389 }
390 });
391 }
392 }