Fixed bug related with app crashes when opening the logs and rotate the device
[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.util.ArrayList;
25
26 import android.app.ProgressDialog;
27 import android.content.Intent;
28 import android.net.Uri;
29 import android.os.AsyncTask;
30 import android.os.Bundle;
31 import android.support.v4.app.Fragment;
32 import android.support.v4.app.FragmentManager;
33 import android.support.v4.app.FragmentTransaction;
34 import android.view.View;
35 import android.view.View.OnClickListener;
36 import android.widget.Button;
37 import android.widget.TextView;
38
39 import com.actionbarsherlock.app.ActionBar;
40 import com.actionbarsherlock.app.SherlockFragmentActivity;
41 import com.actionbarsherlock.view.MenuItem;
42 import com.owncloud.android.R;
43 import com.owncloud.android.lib.common.utils.Log_OC;
44 import com.owncloud.android.ui.dialog.LoadingDialog;
45 import com.owncloud.android.utils.DisplayUtils;
46 import com.owncloud.android.utils.FileStorageUtils;
47
48
49 public class LogHistoryActivity extends SherlockFragmentActivity {
50
51 private static final String MAIL_ATTACHMENT_TYPE = "text/plain";
52
53 private static final String TAG = LogHistoryActivity.class.getSimpleName();
54
55 private static final String DIALOG_WAIT_TAG = "DIALOG_WAIT";
56
57 private static final String KEY_LOG_TEXT = "LOG_TEXT";
58
59 private String mLogPath = FileStorageUtils.getLogPath();
60 private File logDIR = null;
61
62 private TextView mLogTV;
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
77 deleteHistoryButton.setOnClickListener(new OnClickListener() {
78
79 @Override
80 public void onClick(View v) {
81
82 Log_OC.deleteHistoryLogging();
83 finish();
84 }
85 });
86
87 sendHistoryButton.setOnClickListener(new OnClickListener() {
88
89 @Override
90 public void onClick(View v) {
91 sendMail();
92 }
93 });
94
95 mLogTV = (TextView) findViewById(R.id.logTV);
96
97 /// Load of saved instance state
98 if(savedInstanceState != null) {
99 String logTextSaved = savedInstanceState.getString(LogHistoryActivity.KEY_LOG_TEXT);
100 mLogTV.setText(logTextSaved);
101 }
102
103 if (mLogPath != null) {
104 logDIR = new File(mLogPath);
105 }
106
107 if (logDIR != null && logDIR.isDirectory()) {
108 // Show a dialog while log data is being loaded
109 showLoadingDialog();
110
111 // Start a new thread that will load all the log data
112 new LoadingLogTask().execute();
113 }
114 }
115
116
117 @Override
118 protected void onPause() {
119 // TODO Auto-generated method stub
120 super.onPause();
121
122 dismissLoadingDialog();
123 }
124
125
126 @Override
127 public boolean onMenuItemSelected(int featureId, MenuItem item) {
128 super.onMenuItemSelected(featureId, item);
129 switch (item.getItemId()) {
130 case android.R.id.home:
131 finish();
132 break;
133 default:
134 return false;
135 }
136 return true;
137 }
138
139
140 /**
141 * Start activity for sending email with logs attached
142 */
143 private void sendMail() {
144
145 String emailAddresses[] = { getText(R.string.mail_logger).toString() };
146
147 ArrayList<Uri> uris = new ArrayList<Uri>();
148
149 // Convert from paths to Android friendly Parcelable Uri's
150 for (String file : Log_OC.getLogFileNames())
151 {
152 if (new File(mLogPath + File.separator, file).exists()) {
153 Uri u = Uri.parse("file://" + mLogPath + File.separator + file);
154 uris.add(u);
155 }
156 }
157
158 Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
159
160 // Explicitly only use Gmail to send
161 intent.setClassName("com.google.android.gm","com.google.android.gm.ComposeActivityGmail");
162 intent.putExtra(Intent.EXTRA_EMAIL, emailAddresses);
163 intent.putExtra(Intent.EXTRA_SUBJECT, getText(R.string.log_mail_subject));
164 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
165 intent.setType(MAIL_ATTACHMENT_TYPE);
166 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
167
168 if (intent.resolveActivity(getPackageManager()) != null) {
169 startActivity(intent);
170 }
171 }
172
173
174 /**
175 * Read and show log file info
176 */
177 private String readLogFile() {
178
179 String[] logFileName = Log_OC.getLogFileNames();
180
181 //Read text from files
182 StringBuilder text = new StringBuilder();
183
184 try {
185
186 String line;
187
188 for (int i = logFileName.length-1; i >= 0; i--) {
189 File file = new File(mLogPath,logFileName[i]);
190 if (file.exists()) {
191 // Check if FileReader is ready
192 if (new FileReader(file).ready()) {
193 BufferedReader br = new BufferedReader(new FileReader(file));
194 while ((line = br.readLine()) != null) {
195 // Append the log info
196 text.append(line);
197 text.append('\n');
198 }
199 }
200 }
201 }
202 }
203 catch (IOException e) {
204 Log_OC.d(TAG, e.getMessage().toString());
205 }
206
207 return text.toString();
208 }
209
210
211 /**
212 *
213 * Class for loading the log data async
214 *
215 */
216 private class LoadingLogTask extends AsyncTask<String, Void, String> {
217 protected String doInBackground(String... args) {
218 return readLogFile();
219 }
220
221 protected void onPostExecute(String result) {
222 mLogTV.setText(result);
223
224 dismissLoadingDialog();
225 }
226 }
227
228 @Override
229 protected void onSaveInstanceState(Bundle outState) {
230 Log_OC.e(TAG, "onSaveInstanceState() start");
231 super.onSaveInstanceState(outState);
232 outState.putString(LogHistoryActivity.KEY_LOG_TEXT, mLogTV.getText().toString());
233 Log_OC.d(TAG, "onSaveInstanceState() end");
234 }
235
236 /**
237 * Show loading dialog
238 */
239 public void showLoadingDialog() {
240 // Construct dialog
241 LoadingDialog loading = new LoadingDialog(getResources().getString(R.string.log_progress_dialog_text));
242 FragmentManager fm = getSupportFragmentManager();
243 FragmentTransaction ft = fm.beginTransaction();
244 loading.show(ft, DIALOG_WAIT_TAG);
245 }
246
247 /**
248 * Dismiss loading dialog
249 */
250 public void dismissLoadingDialog(){
251 Fragment frag = getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG);
252 if (frag != null) {
253 LoadingDialog loading = (LoadingDialog) frag;
254 loading.dismiss();
255 }
256 }
257 }