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
.view
.LayoutInflater
;
7 import android
.view
.View
;
8 import android
.view
.ViewGroup
;
9 import android
.widget
.ProgressBar
;
10 import android
.widget
.ScrollView
;
11 import android
.widget
.TextView
;
13 import com
.actionbarsherlock
.view
.Menu
;
14 import com
.actionbarsherlock
.view
.MenuInflater
;
15 import com
.actionbarsherlock
.view
.MenuItem
;
16 import com
.owncloud
.android
.R
;
17 import com
.owncloud
.android
.datamodel
.OCFile
;
18 import com
.owncloud
.android
.files
.FileMenuFilter
;
19 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
20 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
21 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
22 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
23 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
25 import java
.io
.BufferedWriter
;
26 import java
.io
.FileInputStream
;
27 import java
.io
.IOException
;
28 import java
.io
.StringWriter
;
29 import java
.util
.Scanner
;
31 public class PreviewTextFragment
extends FileFragment
{
32 private static final String EXTRA_FILE
= "FILE";
33 private static final String EXTRA_ACCOUNT
= "ACCOUNT";
34 private static final String TAG
= PreviewTextFragment
.class.getSimpleName();
36 private Account mAccount
;
37 private TextView mTextPreview
;
38 private ProgressBar mProgressBar
;
39 private ScrollView mScrollView
;
42 * Creates an empty fragment for previews.
44 * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically
45 * (for instance, when the device is turned a aside).
47 * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful
50 public PreviewTextFragment() {
59 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
60 Bundle savedInstanceState
) {
61 super.onCreateView(inflater
, container
, savedInstanceState
);
62 Log_OC
.e(TAG
, "onCreateView");
65 View ret
= inflater
.inflate(R
.layout
.text_file_preview
, container
, false
);
67 mScrollView
= (ScrollView
) ret
.findViewById(R
.id
.text_scrollview
);
68 mTextPreview
= (TextView
) ret
.findViewById(R
.id
.text_preview
);
69 mProgressBar
= (ProgressBar
) ret
.findViewById(R
.id
.progress_bar
);
78 public void onCreate(Bundle savedInstanceState
) {
79 super.onCreate(savedInstanceState
);
81 OCFile file
= getFile();
83 Bundle args
= getArguments();
86 file
= args
.getParcelable(FileDisplayActivity
.EXTRA_FILE
);
89 mAccount
= args
.getParcelable(FileDisplayActivity
.EXTRA_ACCOUNT
);
92 if (savedInstanceState
== null
) {
94 throw new IllegalStateException("Instanced with a NULL OCFile");
96 if (mAccount
== null
) {
97 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
100 file
= savedInstanceState
.getParcelable(EXTRA_FILE
);
101 mAccount
= savedInstanceState
.getParcelable(EXTRA_ACCOUNT
);
104 setHasOptionsMenu(true
);
111 public void onSaveInstanceState(Bundle outState
) {
112 super.onSaveInstanceState(outState
);
113 outState
.putParcelable(PreviewImageFragment
.EXTRA_FILE
, getFile());
114 outState
.putParcelable(PreviewImageFragment
.EXTRA_ACCOUNT
, mAccount
);
118 public void onStart() {
120 Log_OC
.e(TAG
, "onStart");
122 loadAndShowTextPreview(getFile().getStoragePath(), mTextPreview
);
125 private void loadAndShowTextPreview(String location
, TextView textView
) {
126 new TextLoadAsyncTask().execute(location
, textView
);
130 * Reads the file to preview and shows its contents. Too critical to be anonymous.
132 private class TextLoadAsyncTask
extends AsyncTask
<Object
, Void
, StringWriter
> {
136 protected void onPreExecute() {
137 mProgressBar
.setVisibility(View
.VISIBLE
);
141 protected StringWriter
doInBackground(java
.lang
.Object
... params
) {
142 if (params
.length
!= 2)
143 throw new IllegalArgumentException("The parameters to " + TextLoadAsyncTask
.class.getName() + " must be (1) the file location and (2) the text view to update");
144 final String location
= (String
) params
[0];
145 mTextView
= (TextView
) params
[1];
147 FileInputStream inputStream
= null
;
149 StringWriter source
= new StringWriter();
150 BufferedWriter bufferedWriter
= new BufferedWriter(source
);
152 inputStream
= new FileInputStream(location
);
153 sc
= new Scanner(inputStream
);
154 while (sc
.hasNextLine()) {
155 bufferedWriter
.append(sc
.nextLine());
156 if (sc
.hasNextLine()) bufferedWriter
.append("\n");
158 bufferedWriter
.close();
159 IOException exc
= sc
.ioException();
160 if (exc
!= null
) throw exc
;
161 } catch (IOException e
) {
164 if (inputStream
!= null
) {
167 } catch (IOException e
) {
179 protected void onPostExecute(final StringWriter stringWriter
) {
180 super.onPostExecute(stringWriter
);
181 mProgressBar
.setVisibility(View
.GONE
);
182 mScrollView
.setVisibility(View
.VISIBLE
);
183 mTextView
.setText(new String(stringWriter
.getBuffer()));
191 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
192 super.onCreateOptionsMenu(menu
, inflater
);
193 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
200 public void onPrepareOptionsMenu(Menu menu
) {
201 super.onPrepareOptionsMenu(menu
);
203 if (mContainerActivity
.getStorageManager() != null
) {
204 FileMenuFilter mf
= new FileMenuFilter(
206 mContainerActivity
.getStorageManager().getAccount(),
208 getSherlockActivity()
213 // additional restriction for this fragment
214 MenuItem item
= menu
.findItem(R
.id
.action_rename_file
);
216 item
.setVisible(false
);
217 item
.setEnabled(false
);
220 // additional restriction for this fragment
221 item
= menu
.findItem(R
.id
.action_move
);
223 item
.setVisible(false
);
224 item
.setEnabled(false
);
227 // this one doesn't make sense since the file has to be down in order to be previewed
228 item
= menu
.findItem(R
.id
.action_download_file
);
230 item
.setVisible(false
);
231 item
.setEnabled(false
);
239 public boolean onOptionsItemSelected(MenuItem item
) {
240 switch (item
.getItemId()) {
241 case R
.id
.action_share_file
: {
242 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
245 case R
.id
.action_unshare_file
: {
246 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
249 case R
.id
.action_open_file_with
: {
253 case R
.id
.action_remove_file
: {
254 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
255 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
258 case R
.id
.action_see_details
: {
262 case R
.id
.action_send_file
: {
266 case R
.id
.action_sync_file
: {
267 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
277 * Update the file of the fragment with file value
281 public void updateFile(OCFile file
) {
285 private void sendFile() {
286 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
289 private void seeDetails() {
290 mContainerActivity
.showDetails(getFile());
294 public void onPause() {
295 Log_OC
.e(TAG
, "onPause");
300 public void onResume() {
302 Log_OC
.e(TAG
, "onResume");
306 public void onDestroy() {
307 Log_OC
.e(TAG
, "onDestroy");
312 public void onStop() {
314 Log_OC
.e(TAG
, "onStop");
318 * Opens the previewed file with an external application.
320 private void openFile() {
321 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
326 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewTextFragment} to be previewed.
328 * @param file File to test if can be previewed.
329 * @return 'True' if the file can be handled by the fragment.
331 public static boolean canBePreviewed(OCFile file
) {
332 return (file
!= null
&& file
.isDown() && file
.isText());
336 * Finishes the preview
338 private void finish() {
339 getSherlockActivity().onBackPressed();