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
) {
163 if (inputStream
!= null
) {
166 } catch (IOException e
) {
178 protected void onPostExecute(final StringWriter stringWriter
) {
179 mTextView
.setText(new String(stringWriter
.getBuffer()));
180 mTextView
.setVisibility(View
.VISIBLE
);
181 dismissLoadingDialog();
185 * Show loading dialog
187 public void showLoadingDialog() {
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
);
196 * Dismiss loading dialog
198 public void dismissLoadingDialog() {
199 final Fragment frag
= getActivity().getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG
);
201 LoadingDialog loading
= (LoadingDialog
) frag
;
211 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
212 super.onCreateOptionsMenu(menu
, inflater
);
213 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
220 public void onPrepareOptionsMenu(Menu menu
) {
221 super.onPrepareOptionsMenu(menu
);
223 if (mContainerActivity
.getStorageManager() != null
) {
224 FileMenuFilter mf
= new FileMenuFilter(
226 mContainerActivity
.getStorageManager().getAccount(),
228 getSherlockActivity()
233 // additional restriction for this fragment
234 MenuItem item
= menu
.findItem(R
.id
.action_rename_file
);
236 item
.setVisible(false
);
237 item
.setEnabled(false
);
240 // additional restriction for this fragment
241 item
= menu
.findItem(R
.id
.action_move
);
243 item
.setVisible(false
);
244 item
.setEnabled(false
);
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
);
250 item
.setVisible(false
);
251 item
.setEnabled(false
);
254 item
= menu
.findItem(R
.id
.action_settings
);
256 item
.setVisible(false
);
257 item
.setEnabled(false
);
260 item
= menu
.findItem(R
.id
.action_logger
);
262 item
.setVisible(false
);
263 item
.setEnabled(false
);
266 item
= menu
.findItem(R
.id
.action_sync_file
);
268 item
.setVisible(false
);
269 item
.setEnabled(false
);
272 item
= menu
.findItem(R
.id
.action_sync_account
);
274 item
.setVisible(false
);
275 item
.setEnabled(false
);
283 public boolean onOptionsItemSelected(MenuItem item
) {
284 switch (item
.getItemId()) {
285 case R
.id
.action_share_file
: {
286 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
289 case R
.id
.action_unshare_file
: {
290 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
293 case R
.id
.action_open_file_with
: {
297 case R
.id
.action_remove_file
: {
298 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
299 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
302 case R
.id
.action_see_details
: {
306 case R
.id
.action_send_file
: {
310 case R
.id
.action_sync_file
: {
311 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
321 * Update the file of the fragment with file value
323 * @param file The new file to set
325 public void updateFile(OCFile file
) {
329 private void sendFile() {
330 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
333 private void seeDetails() {
334 mContainerActivity
.showDetails(getFile());
338 public void onPause() {
339 Log_OC
.e(TAG
, "onPause");
344 public void onResume() {
346 Log_OC
.e(TAG
, "onResume");
348 loadAndShowTextPreview();
352 public void onDestroy() {
353 Log_OC
.e(TAG
, "onDestroy");
358 public void onStop() {
360 Log_OC
.e(TAG
, "onStop");
364 * Opens the previewed file with an external application.
366 private void openFile() {
367 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
372 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewTextFragment} to be previewed.
374 * @param file File to test if can be previewed.
375 * @return 'True' if the file can be handled by the fragment.
377 public static boolean canBePreviewed(OCFile file
) {
378 return (file
!= null
&& file
.isDown() && file
.isText());
382 * Finishes the preview
384 private void finish() {
385 getActivity().runOnUiThread(new Runnable() {
388 getSherlockActivity().onBackPressed();