Fix. Avoid load logs again when changing device orientation
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / LogHistoryActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18 package com.owncloud.android.ui.activity;
19
20 import java.io.BufferedReader;
21 import java.io.File;
22 import java.io.FileReader;
23 import java.io.IOException;
24 import java.lang.ref.WeakReference;
25 import java.lang.reflect.Field;
26 import java.util.ArrayList;
27
28 import android.content.Intent;
29 import android.net.Uri;
30 import android.os.AsyncTask;
31 import android.os.Bundle;
32 import android.support.v4.app.Fragment;
33 import android.support.v4.app.FragmentManager;
34 import android.support.v4.app.FragmentTransaction;
35 import android.view.View;
36 import android.view.View.OnClickListener;
37 import android.widget.Button;
38 import android.widget.TextView;
39
40 import com.actionbarsherlock.app.ActionBar;
41 import com.actionbarsherlock.app.SherlockFragmentActivity;
42 import com.actionbarsherlock.view.MenuItem;
43 import com.owncloud.android.R;
44 import com.owncloud.android.lib.common.utils.Log_OC;
45 import com.owncloud.android.ui.dialog.LoadingDialog;
46 import com.owncloud.android.utils.DisplayUtils;
47 import com.owncloud.android.utils.FileStorageUtils;
48
49
50 public class LogHistoryActivity extends SherlockFragmentActivity {
51
52 private static final String MAIL_ATTACHMENT_TYPE = "text/plain";
53
54 private static final String KEY_LOG_TEXT = "LOG_TEXT";
55
56 private static final String TAG = LogHistoryActivity.class.getSimpleName();
57
58 private static final String DIALOG_WAIT_TAG = "DIALOG_WAIT";
59
60 private String mLogPath = FileStorageUtils.getLogPath();
61 private File logDIR = null;
62 private String mLogText;
63
64
65 @Override
66 protected void onCreate(Bundle savedInstanceState) {
67 super.onCreate(savedInstanceState);
68
69 setContentView(R.layout.log_send_file);
70 setTitle(getText(R.string.actionbar_logger));
71 ActionBar actionBar = getSherlock().getActionBar();
72 actionBar.setIcon(DisplayUtils.getSeasonalIconId());
73 actionBar.setDisplayHomeAsUpEnabled(true);
74 Button deleteHistoryButton = (Button) findViewById(R.id.deleteLogHistoryButton);
75 Button sendHistoryButton = (Button) findViewById(R.id.sendLogHistoryButton);
76 TextView logTV = (TextView) findViewById(R.id.logTV);
77
78 deleteHistoryButton.setOnClickListener(new OnClickListener() {
79
80 @Override
81 public void onClick(View v) {
82
83 Log_OC.deleteHistoryLogging();
84 finish();
85 }
86 });
87
88 sendHistoryButton.setOnClickListener(new OnClickListener() {
89
90 @Override
91 public void onClick(View v) {
92 sendMail();
93 }
94 });
95
96 if (savedInstanceState == null) {
97 if (mLogPath != null) {
98 logDIR = new File(mLogPath);
99 }
100
101 if (logDIR != null && logDIR.isDirectory()) {
102 // Show a dialog while log data is being loaded
103 showLoadingDialog();
104
105 // Start a new thread that will load all the log data
106 LoadingLogTask task = new LoadingLogTask(logTV);
107 task.execute();
108 }
109 } else {
110 mLogText = savedInstanceState.getString(KEY_LOG_TEXT);
111 logTV.setText(mLogText);
112 }
113 }
114
115 @Override
116 public boolean onMenuItemSelected(int featureId, MenuItem item) {
117 super.onMenuItemSelected(featureId, item);
118 switch (item.getItemId()) {
119 case android.R.id.home:
120 finish();
121 break;
122 default:
123 return false;
124 }
125 return true;
126 }
127
128
129 /**
130 * Start activity for sending email with logs attached
131 */
132 private void sendMail() {
133
134 String emailAddress;
135 try {
136 Class<?> stringClass = R.string.class;
137 Field mailLoggerField = stringClass.getField("mail_logger");
138 int emailAddressId = (Integer)mailLoggerField.get(null);
139 emailAddress = getString(emailAddressId);
140
141 } catch (Exception e) {
142 emailAddress = "";
143 }
144
145 ArrayList<Uri> uris = new ArrayList<Uri>();
146
147 // Convert from paths to Android friendly Parcelable Uri's
148 for (String file : Log_OC.getLogFileNames())
149 {
150 if (new File(mLogPath + File.separator, file).exists()) {
151 Uri u = Uri.parse("file://" + mLogPath + File.separator + file);
152 uris.add(u);
153 }
154 }
155
156 Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
157
158 // Explicitly only use Gmail to send
159 intent.setClassName("com.google.android.gm","com.google.android.gm.ComposeActivityGmail");
160 intent.putExtra(Intent.EXTRA_EMAIL, new String[]{ emailAddress });
161 intent.putExtra(Intent.EXTRA_SUBJECT, getText(R.string.log_mail_subject));
162 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
163 intent.setType(MAIL_ATTACHMENT_TYPE);
164 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
165
166 if (intent.resolveActivity(getPackageManager()) != null) {
167 startActivity(intent);
168 }
169 }
170
171 /**
172 *
173 * Class for loading the log data async
174 *
175 */
176 private class LoadingLogTask extends AsyncTask<String, Void, String> {
177 private final WeakReference<TextView> textViewReference;
178
179 public LoadingLogTask(TextView logTV){
180 // Use of a WeakReference to ensure the TextView can be garbage collected
181 textViewReference = new WeakReference<TextView>(logTV);
182 }
183
184 protected String doInBackground(String... args) {
185 return readLogFile();
186 }
187
188 protected void onPostExecute(String result) {
189 if (textViewReference != null && result != null) {
190 final TextView logTV = textViewReference.get();
191 if (logTV != null) {
192 mLogText = result;
193 logTV.setText(mLogText);
194 dismissLoadingDialog();
195 }
196 }
197 }
198
199 /**
200 * Read and show log file info
201 */
202 private String readLogFile() {
203
204 String[] logFileName = Log_OC.getLogFileNames();
205
206 //Read text from files
207 StringBuilder text = new StringBuilder();
208
209 BufferedReader br = null;
210 try {
211 String line;
212
213 for (int i = logFileName.length-1; i >= 0; i--) {
214 File file = new File(mLogPath,logFileName[i]);
215 if (file.exists()) {
216 // Check if FileReader is ready
217 if (new FileReader(file).ready()) {
218 br = new BufferedReader(new FileReader(file));
219 while ((line = br.readLine()) != null) {
220 // Append the log info
221 text.append(line);
222 text.append('\n');
223 }
224 }
225 }
226 }
227 }
228 catch (IOException e) {
229 Log_OC.d(TAG, e.getMessage().toString());
230
231 } finally {
232 if (br != null) {
233 try {
234 br.close();
235 } catch (IOException e) {
236 // ignore
237 }
238 }
239 }
240
241 return text.toString();
242 }
243 }
244
245 /**
246 * Show loading dialog
247 */
248 public void showLoadingDialog() {
249 // Construct dialog
250 LoadingDialog loading = new LoadingDialog(
251 getResources().getString(R.string.log_progress_dialog_text)
252 );
253 FragmentManager fm = getSupportFragmentManager();
254 FragmentTransaction ft = fm.beginTransaction();
255 loading.show(ft, DIALOG_WAIT_TAG);
256 }
257
258 /**
259 * Dismiss loading dialog
260 */
261 public void dismissLoadingDialog(){
262 Fragment frag = getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG);
263 if (frag != null) {
264 LoadingDialog loading = (LoadingDialog) frag;
265 loading.dismiss();
266 }
267 }
268
269 @Override
270 protected void onSaveInstanceState(Bundle outState) {
271 super.onSaveInstanceState(outState);
272
273 /// global state
274 outState.putString(KEY_LOG_TEXT, mLogText);
275 }
276 }