d0a678c8afc16d81a712954da1b309d860faf13e
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / preview / PreviewTextFragment.java
1 package com.owncloud.android.ui.preview;
2
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;
12
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;
24
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;
30
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();
35
36 private Account mAccount;
37 private TextView mTextPreview;
38 private ProgressBar mProgressBar;
39 private ScrollView mScrollView;
40
41 /**
42 * Creates an empty fragment for previews.
43 * <p/>
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).
46 * <p/>
47 * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful
48 * construction
49 */
50 public PreviewTextFragment() {
51 super();
52 mAccount = null;
53 }
54
55 /**
56 * {@inheritDoc}
57 */
58 @Override
59 public View onCreateView(LayoutInflater inflater, ViewGroup container,
60 Bundle savedInstanceState) {
61 super.onCreateView(inflater, container, savedInstanceState);
62 Log_OC.e(TAG, "onCreateView");
63
64
65 View ret = inflater.inflate(R.layout.text_file_preview, container, false);
66
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);
70
71 return ret;
72 }
73
74 /**
75 * {@inheritDoc}
76 */
77 @Override
78 public void onCreate(Bundle savedInstanceState) {
79 super.onCreate(savedInstanceState);
80
81 OCFile file = getFile();
82
83 Bundle args = getArguments();
84
85 if (file == null)
86 file = args.getParcelable(FileDisplayActivity.EXTRA_FILE);
87
88 if (mAccount == null)
89 mAccount = args.getParcelable(FileDisplayActivity.EXTRA_ACCOUNT);
90
91
92 if (savedInstanceState == null) {
93 if (file == null) {
94 throw new IllegalStateException("Instanced with a NULL OCFile");
95 }
96 if (mAccount == null) {
97 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
98 }
99 } else {
100 file = savedInstanceState.getParcelable(EXTRA_FILE);
101 mAccount = savedInstanceState.getParcelable(EXTRA_ACCOUNT);
102 }
103 setFile(file);
104 setHasOptionsMenu(true);
105 }
106
107 /**
108 * {@inheritDoc}
109 */
110 @Override
111 public void onSaveInstanceState(Bundle outState) {
112 super.onSaveInstanceState(outState);
113 outState.putParcelable(PreviewImageFragment.EXTRA_FILE, getFile());
114 outState.putParcelable(PreviewImageFragment.EXTRA_ACCOUNT, mAccount);
115 }
116
117 @Override
118 public void onStart() {
119 super.onStart();
120 Log_OC.e(TAG, "onStart");
121
122 loadAndShowTextPreview(getFile().getStoragePath(), mTextPreview);
123 }
124
125 private void loadAndShowTextPreview(String location, TextView textView) {
126 new TextLoadAsyncTask().execute(location, textView);
127 }
128
129 /**
130 * Reads the file to preview and shows its contents. Too critical to be anonymous.
131 */
132 private class TextLoadAsyncTask extends AsyncTask<Object, Void, StringWriter> {
133 TextView mTextView;
134
135 @Override
136 protected void onPreExecute() {
137 mProgressBar.setVisibility(View.VISIBLE);
138 }
139
140 @Override
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];
146
147 FileInputStream inputStream = null;
148 Scanner sc = null;
149 StringWriter source = new StringWriter();
150 BufferedWriter bufferedWriter = new BufferedWriter(source);
151 try {
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");
157 }
158 bufferedWriter.close();
159 IOException exc = sc.ioException();
160 if (exc != null) throw exc;
161 } catch (IOException e) {
162 finish();
163 } finally {
164 if (inputStream != null) {
165 try {
166 inputStream.close();
167 } catch (IOException e) {
168 finish();
169 }
170 }
171 if (sc != null) {
172 sc.close();
173 }
174 }
175 return source;
176 }
177
178 @Override
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()));
184 }
185 }
186
187 /**
188 * {@inheritDoc}
189 */
190 @Override
191 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
192 super.onCreateOptionsMenu(menu, inflater);
193 inflater.inflate(R.menu.file_actions_menu, menu);
194 }
195
196 /**
197 * {@inheritDoc}
198 */
199 @Override
200 public void onPrepareOptionsMenu(Menu menu) {
201 super.onPrepareOptionsMenu(menu);
202
203 if (mContainerActivity.getStorageManager() != null) {
204 FileMenuFilter mf = new FileMenuFilter(
205 getFile(),
206 mContainerActivity.getStorageManager().getAccount(),
207 mContainerActivity,
208 getSherlockActivity()
209 );
210 mf.filter(menu);
211 }
212
213 // additional restriction for this fragment
214 MenuItem item = menu.findItem(R.id.action_rename_file);
215 if (item != null) {
216 item.setVisible(false);
217 item.setEnabled(false);
218 }
219
220 // additional restriction for this fragment
221 item = menu.findItem(R.id.action_move);
222 if (item != null) {
223 item.setVisible(false);
224 item.setEnabled(false);
225 }
226
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);
229 if (item != null) {
230 item.setVisible(false);
231 item.setEnabled(false);
232 }
233 }
234
235 /**
236 * {@inheritDoc}
237 */
238 @Override
239 public boolean onOptionsItemSelected(MenuItem item) {
240 switch (item.getItemId()) {
241 case R.id.action_share_file: {
242 mContainerActivity.getFileOperationsHelper().shareFileWithLink(getFile());
243 return true;
244 }
245 case R.id.action_unshare_file: {
246 mContainerActivity.getFileOperationsHelper().unshareFileWithLink(getFile());
247 return true;
248 }
249 case R.id.action_open_file_with: {
250 openFile();
251 return true;
252 }
253 case R.id.action_remove_file: {
254 RemoveFileDialogFragment dialog = RemoveFileDialogFragment.newInstance(getFile());
255 dialog.show(getFragmentManager(), ConfirmationDialogFragment.FTAG_CONFIRMATION);
256 return true;
257 }
258 case R.id.action_see_details: {
259 seeDetails();
260 return true;
261 }
262 case R.id.action_send_file: {
263 sendFile();
264 return true;
265 }
266 case R.id.action_sync_file: {
267 mContainerActivity.getFileOperationsHelper().syncFile(getFile());
268 return true;
269 }
270
271 default:
272 return false;
273 }
274 }
275
276 /**
277 * Update the file of the fragment with file value
278 *
279 * @param file
280 */
281 public void updateFile(OCFile file) {
282 setFile(file);
283 }
284
285 private void sendFile() {
286 mContainerActivity.getFileOperationsHelper().sendDownloadedFile(getFile());
287 }
288
289 private void seeDetails() {
290 mContainerActivity.showDetails(getFile());
291 }
292
293 @Override
294 public void onPause() {
295 Log_OC.e(TAG, "onPause");
296 super.onPause();
297 }
298
299 @Override
300 public void onResume() {
301 super.onResume();
302 Log_OC.e(TAG, "onResume");
303 }
304
305 @Override
306 public void onDestroy() {
307 Log_OC.e(TAG, "onDestroy");
308 super.onDestroy();
309 }
310
311 @Override
312 public void onStop() {
313 super.onStop();
314 Log_OC.e(TAG, "onStop");
315 }
316
317 /**
318 * Opens the previewed file with an external application.
319 */
320 private void openFile() {
321 mContainerActivity.getFileOperationsHelper().openFile(getFile());
322 finish();
323 }
324
325 /**
326 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewTextFragment} to be previewed.
327 *
328 * @param file File to test if can be previewed.
329 * @return 'True' if the file can be handled by the fragment.
330 */
331 public static boolean canBePreviewed(OCFile file) {
332 return (file != null && file.isDown() && file.isText());
333 }
334
335 /**
336 * Finishes the preview
337 */
338 private void finish() {
339 getSherlockActivity().onBackPressed();
340 }
341 }