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
.TextView
;
14 import com
.actionbarsherlock
.view
.Menu
;
15 import com
.actionbarsherlock
.view
.MenuInflater
;
16 import com
.actionbarsherlock
.view
.MenuItem
;
17 import com
.owncloud
.android
.R
;
18 import com
.owncloud
.android
.datamodel
.OCFile
;
19 import com
.owncloud
.android
.files
.FileMenuFilter
;
20 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
21 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
22 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
23 import com
.owncloud
.android
.ui
.dialog
.LoadingDialog
;
24 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
25 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
27 import java
.io
.BufferedWriter
;
28 import java
.io
.FileInputStream
;
29 import java
.io
.IOException
;
30 import java
.io
.StringWriter
;
31 import java
.lang
.ref
.WeakReference
;
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
;
41 private TextLoadAsyncTask mTextLoadTask
;
44 * Creates an empty fragment for previews.
46 * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically
47 * (for instance, when the device is turned a aside).
49 * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful
52 public PreviewTextFragment() {
61 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
62 Bundle savedInstanceState
) {
63 super.onCreateView(inflater
, container
, savedInstanceState
);
64 Log_OC
.e(TAG
, "onCreateView");
67 View ret
= inflater
.inflate(R
.layout
.text_file_preview
, container
, false
);
69 mTextPreview
= (TextView
) ret
.findViewById(R
.id
.text_preview
);
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 if (mAccount
== null
) {
90 mAccount
= args
.getParcelable(FileDisplayActivity
.EXTRA_ACCOUNT
);
93 if (savedInstanceState
== null
) {
95 throw new IllegalStateException("Instanced with a NULL OCFile");
97 if (mAccount
== null
) {
98 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
101 file
= savedInstanceState
.getParcelable(EXTRA_FILE
);
102 mAccount
= savedInstanceState
.getParcelable(EXTRA_ACCOUNT
);
105 setHasOptionsMenu(true
);
112 public void onSaveInstanceState(Bundle outState
) {
113 super.onSaveInstanceState(outState
);
114 outState
.putParcelable(PreviewImageFragment
.EXTRA_FILE
, getFile());
115 outState
.putParcelable(PreviewImageFragment
.EXTRA_ACCOUNT
, mAccount
);
119 public void onStart() {
121 Log_OC
.e(TAG
, "onStart");
124 private void loadAndShowTextPreview() {
125 mTextLoadTask
= new TextLoadAsyncTask(new WeakReference
<TextView
>(mTextPreview
));
126 mTextLoadTask
.execute(getFile().getStoragePath());
131 * Reads the file to preview and shows its contents. Too critical to be anonymous.
133 private class TextLoadAsyncTask
extends AsyncTask
<Object
, Void
, StringWriter
> {
134 private final String DIALOG_WAIT_TAG
= "DIALOG_WAIT";
135 private final WeakReference
<TextView
> mTextViewReference
;
137 private TextLoadAsyncTask(WeakReference
<TextView
> textView
) {
138 mTextViewReference
= textView
;
143 protected void onPreExecute() {
148 protected StringWriter
doInBackground(java
.lang
.Object
... params
) {
149 if (params
.length
!= 1) {
150 throw new IllegalArgumentException("The parameter to " + TextLoadAsyncTask
.class.getName() + " must be (1) the file location");
152 final String location
= (String
) params
[0];
154 FileInputStream inputStream
= null
;
156 StringWriter source
= new StringWriter();
157 BufferedWriter bufferedWriter
= new BufferedWriter(source
);
159 inputStream
= new FileInputStream(location
);
160 sc
= new Scanner(inputStream
);
161 while (sc
.hasNextLine()) {
162 bufferedWriter
.append(sc
.nextLine());
163 if (sc
.hasNextLine()) bufferedWriter
.append("\n");
165 bufferedWriter
.close();
166 IOException exc
= sc
.ioException();
167 if (exc
!= null
) throw exc
;
168 } catch (IOException e
) {
169 Log_OC
.e(TAG
, e
.getMessage(), e
);
172 if (inputStream
!= null
) {
175 } catch (IOException e
) {
176 Log_OC
.e(TAG
, e
.getMessage(), e
);
188 protected void onPostExecute(final StringWriter stringWriter
) {
189 final TextView textView
= mTextViewReference
.get();
191 if (textView
!= null
) {
192 textView
.setText(new String(stringWriter
.getBuffer()));
193 textView
.setVisibility(View
.VISIBLE
);
196 dismissLoadingDialog();
200 * Show loading dialog
202 public void showLoadingDialog() {
204 LoadingDialog loading
= new LoadingDialog(getResources().getString(R
.string
.wait_a_moment
));
205 FragmentManager fm
= getActivity().getSupportFragmentManager();
206 FragmentTransaction ft
= fm
.beginTransaction();
207 loading
.show(ft
, DIALOG_WAIT_TAG
);
211 * Dismiss loading dialog
213 public void dismissLoadingDialog() {
214 final Fragment frag
= getActivity().getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG
);
216 LoadingDialog loading
= (LoadingDialog
) frag
;
226 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
227 super.onCreateOptionsMenu(menu
, inflater
);
228 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
235 public void onPrepareOptionsMenu(Menu menu
) {
236 super.onPrepareOptionsMenu(menu
);
238 if (mContainerActivity
.getStorageManager() != null
) {
239 FileMenuFilter mf
= new FileMenuFilter(
241 mContainerActivity
.getStorageManager().getAccount(),
243 getSherlockActivity()
248 // additional restriction for this fragment
249 MenuItem item
= menu
.findItem(R
.id
.action_rename_file
);
251 item
.setVisible(false
);
252 item
.setEnabled(false
);
255 // additional restriction for this fragment
256 item
= menu
.findItem(R
.id
.action_move
);
258 item
.setVisible(false
);
259 item
.setEnabled(false
);
262 // this one doesn't make sense since the file has to be down in order to be previewed
263 item
= menu
.findItem(R
.id
.action_download_file
);
265 item
.setVisible(false
);
266 item
.setEnabled(false
);
269 item
= menu
.findItem(R
.id
.action_settings
);
271 item
.setVisible(false
);
272 item
.setEnabled(false
);
275 item
= menu
.findItem(R
.id
.action_logger
);
277 item
.setVisible(false
);
278 item
.setEnabled(false
);
281 item
= menu
.findItem(R
.id
.action_sync_file
);
283 item
.setVisible(false
);
284 item
.setEnabled(false
);
287 item
= menu
.findItem(R
.id
.action_sync_account
);
289 item
.setVisible(false
);
290 item
.setEnabled(false
);
298 public boolean onOptionsItemSelected(MenuItem item
) {
299 switch (item
.getItemId()) {
300 case R
.id
.action_share_file
: {
301 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
304 case R
.id
.action_unshare_file
: {
305 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
308 case R
.id
.action_open_file_with
: {
312 case R
.id
.action_remove_file
: {
313 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
314 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
317 case R
.id
.action_see_details
: {
321 case R
.id
.action_send_file
: {
325 case R
.id
.action_sync_file
: {
326 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
336 * Update the file of the fragment with file value
338 * @param file The new file to set
340 public void updateFile(OCFile file
) {
344 private void sendFile() {
345 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
348 private void seeDetails() {
349 mContainerActivity
.showDetails(getFile());
353 public void onPause() {
354 Log_OC
.e(TAG
, "onPause");
359 public void onResume() {
361 Log_OC
.e(TAG
, "onResume");
363 loadAndShowTextPreview();
367 public void onDestroy() {
368 Log_OC
.e(TAG
, "onDestroy");
373 public void onStop() {
375 Log_OC
.e(TAG
, "onStop");
376 if (mTextLoadTask
!= null
)
377 mTextLoadTask
.cancel(Boolean
.TRUE
);
381 * Opens the previewed file with an external application.
383 private void openFile() {
384 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
389 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewTextFragment} to be previewed.
391 * @param file File to test if can be previewed.
392 * @return 'True' if the file can be handled by the fragment.
394 public static boolean canBePreviewed(OCFile file
) {
395 return (file
!= null
&& file
.isDown() && file
.isText());
399 * Finishes the preview
401 private void finish() {
402 getActivity().runOnUiThread(new Runnable() {
405 getSherlockActivity().onBackPressed();