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
.Menu
;
11 import android
.view
.MenuInflater
;
12 import android
.view
.MenuItem
;
13 import android
.view
.View
;
14 import android
.view
.ViewGroup
;
15 import android
.widget
.TextView
;
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(PreviewTextFragment
.EXTRA_FILE
, getFile());
115 outState
.putParcelable(PreviewTextFragment
.EXTRA_ACCOUNT
, mAccount
);
119 public void onStart() {
121 Log_OC
.e(TAG
, "onStart");
123 loadAndShowTextPreview();
126 private void loadAndShowTextPreview() {
127 mTextLoadTask
= new TextLoadAsyncTask(new WeakReference
<TextView
>(mTextPreview
));
128 mTextLoadTask
.execute(getFile().getStoragePath());
133 * Reads the file to preview and shows its contents. Too critical to be anonymous.
135 private class TextLoadAsyncTask
extends AsyncTask
<Object
, Void
, StringWriter
> {
136 private final String DIALOG_WAIT_TAG
= "DIALOG_WAIT";
137 private final WeakReference
<TextView
> mTextViewReference
;
139 private TextLoadAsyncTask(WeakReference
<TextView
> textView
) {
140 mTextViewReference
= textView
;
145 protected void onPreExecute() {
150 protected StringWriter
doInBackground(java
.lang
.Object
... params
) {
151 if (params
.length
!= 1) {
152 throw new IllegalArgumentException("The parameter to " + TextLoadAsyncTask
.class.getName() + " must be (1) the file location");
154 final String location
= (String
) params
[0];
156 FileInputStream inputStream
= null
;
158 StringWriter source
= new StringWriter();
159 BufferedWriter bufferedWriter
= new BufferedWriter(source
);
161 inputStream
= new FileInputStream(location
);
162 sc
= new Scanner(inputStream
);
163 while (sc
.hasNextLine()) {
164 bufferedWriter
.append(sc
.nextLine());
165 if (sc
.hasNextLine()) bufferedWriter
.append("\n");
167 bufferedWriter
.close();
168 IOException exc
= sc
.ioException();
169 if (exc
!= null
) throw exc
;
170 } catch (IOException e
) {
171 Log_OC
.e(TAG
, e
.getMessage(), e
);
174 if (inputStream
!= null
) {
177 } catch (IOException e
) {
178 Log_OC
.e(TAG
, e
.getMessage(), e
);
190 protected void onPostExecute(final StringWriter stringWriter
) {
191 final TextView textView
= mTextViewReference
.get();
193 if (textView
!= null
) {
194 textView
.setText(new String(stringWriter
.getBuffer()));
195 textView
.setVisibility(View
.VISIBLE
);
198 dismissLoadingDialog();
202 * Show loading dialog
204 public void showLoadingDialog() {
206 LoadingDialog loading
= new LoadingDialog(getResources().getString(R
.string
.wait_a_moment
));
207 FragmentManager fm
= getActivity().getSupportFragmentManager();
208 FragmentTransaction ft
= fm
.beginTransaction();
209 loading
.show(ft
, DIALOG_WAIT_TAG
);
213 * Dismiss loading dialog
215 public void dismissLoadingDialog() {
216 final Fragment frag
= getActivity().getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG
);
218 LoadingDialog loading
= (LoadingDialog
) frag
;
228 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
229 super.onCreateOptionsMenu(menu
, inflater
);
230 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
237 public void onPrepareOptionsMenu(Menu menu
) {
238 super.onPrepareOptionsMenu(menu
);
240 if (mContainerActivity
.getStorageManager() != null
) {
241 FileMenuFilter mf
= new FileMenuFilter(
243 mContainerActivity
.getStorageManager().getAccount(),
250 // additional restriction for this fragment
251 MenuItem item
= menu
.findItem(R
.id
.action_rename_file
);
253 item
.setVisible(false
);
254 item
.setEnabled(false
);
257 // additional restriction for this fragment
258 item
= menu
.findItem(R
.id
.action_move
);
260 item
.setVisible(false
);
261 item
.setEnabled(false
);
264 // this one doesn't make sense since the file has to be down in order to be previewed
265 item
= menu
.findItem(R
.id
.action_download_file
);
267 item
.setVisible(false
);
268 item
.setEnabled(false
);
271 item
= menu
.findItem(R
.id
.action_sync_file
);
273 item
.setVisible(false
);
274 item
.setEnabled(false
);
277 item
= menu
.findItem(R
.id
.action_sync_account
);
279 item
.setVisible(false
);
280 item
.setEnabled(false
);
288 public boolean onOptionsItemSelected(MenuItem item
) {
289 switch (item
.getItemId()) {
290 case R
.id
.action_share_file
: {
291 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
294 case R
.id
.action_unshare_file
: {
295 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
298 case R
.id
.action_open_file_with
: {
302 case R
.id
.action_remove_file
: {
303 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
304 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
307 case R
.id
.action_see_details
: {
311 case R
.id
.action_send_file
: {
315 case R
.id
.action_sync_file
: {
316 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
326 * Update the file of the fragment with file value
328 * @param file The new file to set
330 public void updateFile(OCFile file
) {
334 private void sendFile() {
335 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
338 private void seeDetails() {
339 mContainerActivity
.showDetails(getFile());
343 public void onPause() {
344 Log_OC
.e(TAG
, "onPause");
349 public void onResume() {
351 Log_OC
.e(TAG
, "onResume");
355 public void onDestroy() {
356 Log_OC
.e(TAG
, "onDestroy");
361 public void onStop() {
363 Log_OC
.e(TAG
, "onStop");
364 if (mTextLoadTask
!= null
)
365 mTextLoadTask
.cancel(Boolean
.TRUE
);
369 * Opens the previewed file with an external application.
371 private void openFile() {
372 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
377 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewTextFragment} to be previewed.
379 * @param file File to test if can be previewed.
380 * @return 'True' if the file can be handled by the fragment.
382 public static boolean canBePreviewed(OCFile file
) {
383 return (file
!= null
&& file
.isDown() && file
.isText());
387 * Finishes the preview
389 private void finish() {
390 getActivity().runOnUiThread(new Runnable() {
393 getActivity().onBackPressed();