1 package com
.owncloud
.android
.ui
.preview
;
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
;
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
;
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
;
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();
39 private Account mAccount
;
40 private TextView mTextPreview
;
43 * Creates an empty fragment for previews.
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).
48 * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful
51 public PreviewTextFragment() {
60 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
61 Bundle savedInstanceState
) {
62 super.onCreateView(inflater
, container
, savedInstanceState
);
63 Log_OC
.e(TAG
, "onCreateView");
66 View ret
= inflater
.inflate(R
.layout
.text_file_preview
, container
, false
);
68 mTextPreview
= (TextView
) ret
.findViewById(R
.id
.text_preview
);
77 public void onCreate(Bundle savedInstanceState
) {
78 super.onCreate(savedInstanceState
);
80 OCFile file
= getFile();
82 Bundle args
= getArguments();
85 file
= args
.getParcelable(FileDisplayActivity
.EXTRA_FILE
);
88 mAccount
= args
.getParcelable(FileDisplayActivity
.EXTRA_ACCOUNT
);
91 if (savedInstanceState
== null
) {
93 throw new IllegalStateException("Instanced with a NULL OCFile");
95 if (mAccount
== null
) {
96 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
99 file
= savedInstanceState
.getParcelable(EXTRA_FILE
);
100 mAccount
= savedInstanceState
.getParcelable(EXTRA_ACCOUNT
);
103 setHasOptionsMenu(true
);
110 public void onSaveInstanceState(Bundle outState
) {
111 super.onSaveInstanceState(outState
);
112 outState
.putParcelable(PreviewImageFragment
.EXTRA_FILE
, getFile());
113 outState
.putParcelable(PreviewImageFragment
.EXTRA_ACCOUNT
, mAccount
);
117 public void onStart() {
119 Log_OC
.e(TAG
, "onStart");
122 private void loadAndShowTextPreview() {
123 new TextLoadAsyncTask().execute(getFile().getStoragePath(), mTextPreview
);
127 * Reads the file to preview and shows its contents. Too critical to be anonymous.
129 private class TextLoadAsyncTask
extends AsyncTask
<Object
, Void
, StringWriter
> {
130 private final String DIALOG_WAIT_TAG
= "DIALOG_WAIT";
131 private TextView mTextView
;
134 protected void onPreExecute() {
139 protected StringWriter
doInBackground(java
.lang
.Object
... params
) {
140 if (params
.length
!= 2)
141 throw new IllegalArgumentException("The parameters to " + TextLoadAsyncTask
.class.getName() + " must be (1) the file location and (2) the text view to update");
142 final String location
= (String
) params
[0];
143 mTextView
= (TextView
) params
[1];
145 FileInputStream inputStream
= null
;
147 StringWriter source
= new StringWriter();
148 BufferedWriter bufferedWriter
= new BufferedWriter(source
);
150 inputStream
= new FileInputStream(location
);
151 sc
= new Scanner(inputStream
);
152 while (sc
.hasNextLine()) {
153 bufferedWriter
.append(sc
.nextLine());
154 if (sc
.hasNextLine()) bufferedWriter
.append("\n");
156 bufferedWriter
.close();
157 IOException exc
= sc
.ioException();
158 if (exc
!= null
) throw exc
;
159 } catch (IOException e
) {
162 if (inputStream
!= null
) {
165 } catch (IOException e
) {
177 protected void onPostExecute(final StringWriter stringWriter
) {
178 mTextView
.setText(new String(stringWriter
.getBuffer()));
179 mTextView
.setVisibility(View
.VISIBLE
);
180 dismissLoadingDialog();
184 * Show loading dialog
186 public void showLoadingDialog() {
188 LoadingDialog loading
= new LoadingDialog(getResources().getString(R
.string
.wait_a_moment
));
189 FragmentManager fm
= getActivity().getSupportFragmentManager();
190 FragmentTransaction ft
= fm
.beginTransaction();
191 loading
.show(ft
, DIALOG_WAIT_TAG
);
195 * Dismiss loading dialog
197 public void dismissLoadingDialog() {
198 Fragment frag
= getActivity().getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG
);
200 LoadingDialog loading
= (LoadingDialog
) frag
;
210 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
211 super.onCreateOptionsMenu(menu
, inflater
);
212 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
219 public void onPrepareOptionsMenu(Menu menu
) {
220 super.onPrepareOptionsMenu(menu
);
222 if (mContainerActivity
.getStorageManager() != null
) {
223 FileMenuFilter mf
= new FileMenuFilter(
225 mContainerActivity
.getStorageManager().getAccount(),
227 getSherlockActivity()
232 // additional restriction for this fragment
233 MenuItem item
= menu
.findItem(R
.id
.action_rename_file
);
235 item
.setVisible(false
);
236 item
.setEnabled(false
);
239 // additional restriction for this fragment
240 item
= menu
.findItem(R
.id
.action_move
);
242 item
.setVisible(false
);
243 item
.setEnabled(false
);
246 // this one doesn't make sense since the file has to be down in order to be previewed
247 item
= menu
.findItem(R
.id
.action_download_file
);
249 item
.setVisible(false
);
250 item
.setEnabled(false
);
253 item
= menu
.findItem(R
.id
.action_settings
);
255 item
.setVisible(false
);
256 item
.setEnabled(false
);
259 item
= menu
.findItem(R
.id
.action_logger
);
261 item
.setVisible(false
);
262 item
.setEnabled(false
);
265 item
= menu
.findItem(R
.id
.action_sync_file
);
267 item
.setVisible(false
);
268 item
.setEnabled(false
);
271 item
= menu
.findItem(R
.id
.action_sync_account
);
273 item
.setVisible(false
);
274 item
.setEnabled(false
);
282 public boolean onOptionsItemSelected(MenuItem item
) {
283 switch (item
.getItemId()) {
284 case R
.id
.action_share_file
: {
285 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
288 case R
.id
.action_unshare_file
: {
289 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
292 case R
.id
.action_open_file_with
: {
296 case R
.id
.action_remove_file
: {
297 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
298 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
301 case R
.id
.action_see_details
: {
305 case R
.id
.action_send_file
: {
309 case R
.id
.action_sync_file
: {
310 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
320 * Update the file of the fragment with file value
322 * @param file The new file to set
324 public void updateFile(OCFile file
) {
328 private void sendFile() {
329 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
332 private void seeDetails() {
333 mContainerActivity
.showDetails(getFile());
337 public void onPause() {
338 Log_OC
.e(TAG
, "onPause");
343 public void onResume() {
345 Log_OC
.e(TAG
, "onResume");
347 loadAndShowTextPreview();
351 public void onDestroy() {
352 Log_OC
.e(TAG
, "onDestroy");
357 public void onStop() {
359 Log_OC
.e(TAG
, "onStop");
363 * Opens the previewed file with an external application.
365 private void openFile() {
366 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
371 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewTextFragment} to be previewed.
373 * @param file File to test if can be previewed.
374 * @return 'True' if the file can be handled by the fragment.
376 public static boolean canBePreviewed(OCFile file
) {
377 return (file
!= null
&& file
.isDown() && file
.isText());
381 * Finishes the preview
383 private void finish() {
384 getActivity().runOnUiThread(new Runnable() {
387 getSherlockActivity().onBackPressed();