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 if (mAccount
== null
) {
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");
123 private void loadAndShowTextPreview() {
124 new TextLoadAsyncTask().execute(getFile().getStoragePath(), mTextPreview
);
128 * Reads the file to preview and shows its contents. Too critical to be anonymous.
130 private class TextLoadAsyncTask
extends AsyncTask
<Object
, Void
, StringWriter
> {
131 private final String DIALOG_WAIT_TAG
= "DIALOG_WAIT";
132 private TextView mTextView
;
135 protected void onPreExecute() {
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];
146 FileInputStream inputStream
= null
;
148 StringWriter source
= new StringWriter();
149 BufferedWriter bufferedWriter
= new BufferedWriter(source
);
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");
157 bufferedWriter
.close();
158 IOException exc
= sc
.ioException();
159 if (exc
!= null
) throw exc
;
160 } catch (IOException e
) {
161 Log_OC
.e(TAG
, e
.getMessage(), e
);
164 if (inputStream
!= null
) {
167 } catch (IOException e
) {
168 Log_OC
.e(TAG
, e
.getMessage(), e
);
180 protected void onPostExecute(final StringWriter stringWriter
) {
181 mTextView
.setText(new String(stringWriter
.getBuffer()));
182 mTextView
.setVisibility(View
.VISIBLE
);
183 dismissLoadingDialog();
187 * Show loading dialog
189 public void showLoadingDialog() {
191 LoadingDialog loading
= new LoadingDialog(getResources().getString(R
.string
.wait_a_moment
));
192 FragmentManager fm
= getActivity().getSupportFragmentManager();
193 FragmentTransaction ft
= fm
.beginTransaction();
194 loading
.show(ft
, DIALOG_WAIT_TAG
);
198 * Dismiss loading dialog
200 public void dismissLoadingDialog() {
201 final Fragment frag
= getActivity().getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG
);
203 LoadingDialog loading
= (LoadingDialog
) frag
;
213 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
214 super.onCreateOptionsMenu(menu
, inflater
);
215 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
222 public void onPrepareOptionsMenu(Menu menu
) {
223 super.onPrepareOptionsMenu(menu
);
225 if (mContainerActivity
.getStorageManager() != null
) {
226 FileMenuFilter mf
= new FileMenuFilter(
228 mContainerActivity
.getStorageManager().getAccount(),
230 getSherlockActivity()
235 // additional restriction for this fragment
236 MenuItem item
= menu
.findItem(R
.id
.action_rename_file
);
238 item
.setVisible(false
);
239 item
.setEnabled(false
);
242 // additional restriction for this fragment
243 item
= menu
.findItem(R
.id
.action_move
);
245 item
.setVisible(false
);
246 item
.setEnabled(false
);
249 // this one doesn't make sense since the file has to be down in order to be previewed
250 item
= menu
.findItem(R
.id
.action_download_file
);
252 item
.setVisible(false
);
253 item
.setEnabled(false
);
256 item
= menu
.findItem(R
.id
.action_settings
);
258 item
.setVisible(false
);
259 item
.setEnabled(false
);
262 item
= menu
.findItem(R
.id
.action_logger
);
264 item
.setVisible(false
);
265 item
.setEnabled(false
);
268 item
= menu
.findItem(R
.id
.action_sync_file
);
270 item
.setVisible(false
);
271 item
.setEnabled(false
);
274 item
= menu
.findItem(R
.id
.action_sync_account
);
276 item
.setVisible(false
);
277 item
.setEnabled(false
);
285 public boolean onOptionsItemSelected(MenuItem item
) {
286 switch (item
.getItemId()) {
287 case R
.id
.action_share_file
: {
288 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
291 case R
.id
.action_unshare_file
: {
292 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
295 case R
.id
.action_open_file_with
: {
299 case R
.id
.action_remove_file
: {
300 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
301 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
304 case R
.id
.action_see_details
: {
308 case R
.id
.action_send_file
: {
312 case R
.id
.action_sync_file
: {
313 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
323 * Update the file of the fragment with file value
325 * @param file The new file to set
327 public void updateFile(OCFile file
) {
331 private void sendFile() {
332 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
335 private void seeDetails() {
336 mContainerActivity
.showDetails(getFile());
340 public void onPause() {
341 Log_OC
.e(TAG
, "onPause");
346 public void onResume() {
348 Log_OC
.e(TAG
, "onResume");
350 loadAndShowTextPreview();
354 public void onDestroy() {
355 Log_OC
.e(TAG
, "onDestroy");
360 public void onStop() {
362 Log_OC
.e(TAG
, "onStop");
366 * Opens the previewed file with an external application.
368 private void openFile() {
369 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
374 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewTextFragment} to be previewed.
376 * @param file File to test if can be previewed.
377 * @return 'True' if the file can be handled by the fragment.
379 public static boolean canBePreviewed(OCFile file
) {
380 return (file
!= null
&& file
.isDown() && file
.isText());
384 * Finishes the preview
386 private void finish() {
387 getActivity().runOnUiThread(new Runnable() {
390 getSherlockActivity().onBackPressed();