793b3d9d358b9b874436ca00bb94ac0970be4075
[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.ActivityNotFoundException;
29 import android.content.Intent;
30 import android.net.Uri;
31 import android.os.AsyncTask;
32 import android.os.Bundle;
33 import android.support.v4.app.Fragment;
34 import android.support.v4.app.FragmentManager;
35 import android.support.v4.app.FragmentTransaction;
36 import android.view.View;
37 import android.view.View.OnClickListener;
38 import android.widget.Button;
39 import android.widget.TextView;
40 import android.widget.Toast;
41
42 import com.actionbarsherlock.app.ActionBar;
43 import com.actionbarsherlock.app.SherlockFragmentActivity;
44 import com.actionbarsherlock.view.MenuItem;
45 import com.owncloud.android.R;
46 import com.owncloud.android.lib.common.utils.Log_OC;
47 import com.owncloud.android.ui.dialog.LoadingDialog;
48 import com.owncloud.android.utils.DisplayUtils;
49 import com.owncloud.android.utils.FileStorageUtils;
50
51
52 public class LogHistoryActivity extends SherlockFragmentActivity {
53
54 private static final String MAIL_ATTACHMENT_TYPE = "text/plain";
55
56 private static final String KEY_LOG_TEXT = "LOG_TEXT";
57
58 private static final String TAG = LogHistoryActivity.class.getSimpleName();
59
60 private static final String DIALOG_WAIT_TAG = "DIALOG_WAIT";
61
62 private String mLogPath = FileStorageUtils.getLogPath();
63 private File logDIR = null;
64 private String mLogText;
65
66
67 @Override
68 protected void onCreate(Bundle savedInstanceState) {
69 super.onCreate(savedInstanceState);
70
71 setContentView(R.layout.log_send_file);
72 setTitle(getText(R.string.actionbar_logger));
73 ActionBar actionBar = getSherlock().getActionBar();
74 actionBar.setIcon(DisplayUtils.getSeasonalIconId());
75 actionBar.setDisplayHomeAsUpEnabled(true);
76 Button deleteHistoryButton = (Button) findViewById(R.id.deleteLogHistoryButton);
77 Button sendHistoryButton = (Button) findViewById(R.id.sendLogHistoryButton);
78 TextView logTV = (TextView) findViewById(R.id.logTV);
79
80 deleteHistoryButton.setOnClickListener(new OnClickListener() {
81
82 @Override
83 public void onClick(View v) {
84
85 Log_OC.deleteHistoryLogging();
86 finish();
87 }
88 });
89
90 sendHistoryButton.setOnClickListener(new OnClickListener() {
91
92 @Override
93 public void onClick(View v) {
94 sendMail();
95 }
96 });
97
98 if (savedInstanceState == null) {
99 if (mLogPath != null) {
100 logDIR = new File(mLogPath);
101 }
102
103 if (logDIR != null && logDIR.isDirectory()) {
104 // Show a dialog while log data is being loaded
105 showLoadingDialog();
106
107 // Start a new thread that will load all the log data
108 LoadingLogTask task = new LoadingLogTask(logTV);
109 task.execute();
110 }
111 } else {
112 mLogText = savedInstanceState.getString(KEY_LOG_TEXT);
113 logTV.setText(mLogText);
114 }
115 }
116
117 @Override
118 public boolean onMenuItemSelected(int featureId, MenuItem item) {
119 super.onMenuItemSelected(featureId, item);
120 switch (item.getItemId()) {
121 case android.R.id.home:
122 finish();
123 break;
124 default:
125 return false;
126 }
127 return true;
128 }
129
130
131 /**
132 * Start activity for sending email with logs attached
133 */
134 private void sendMail() {
135
136 // For the moment we need to consider the possibility that setup.xml
137 // does not include the "mail_logger" entry. This block prevents that
138 // compilation fails in this case.
139 String emailAddress;
140 try {
141 Class<?> stringClass = R.string.class;
142 Field mailLoggerField = stringClass.getField("mail_logger");
143 int emailAddressId = (Integer) mailLoggerField.get(null);
144 emailAddress = getString(emailAddressId);
145 } catch (Exception e) {
146 emailAddress = "";
147 }
148
149 ArrayList<Uri> uris = new ArrayList<Uri>();
150
151 // Convert from paths to Android friendly Parcelable Uri's
152 for (String file : Log_OC.getLogFileNames())
153 {
154 File logFile = new File(mLogPath, file);
155 if (logFile.exists()) {
156 uris.add(Uri.fromFile(logFile));
157 }
158 }
159
160 Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
161
162 intent.putExtra(Intent.EXTRA_EMAIL, emailAddress);
163 String subject = String.format(getString(R.string.log_send_mail_subject), getString(R.string.app_name));
164 intent.putExtra(Intent.EXTRA_SUBJECT, subject);
165 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
166 intent.setType(MAIL_ATTACHMENT_TYPE);
167 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
168 try {
169 startActivity(intent);
170 } catch (ActivityNotFoundException e) {
171 Toast.makeText(this, getString(R.string.log_send_no_mail_app), Toast.LENGTH_LONG).show();
172 Log_OC.i(TAG, "Could not find app for sending log history.");
173 }
174
175 }
176
177 /**
178 *
179 * Class for loading the log data async
180 *
181 */
182 private class LoadingLogTask extends AsyncTask<String, Void, String> {
183 private final WeakReference<TextView> textViewReference;
184
185 public LoadingLogTask(TextView logTV){
186 // Use of a WeakReference to ensure the TextView can be garbage collected
187 textViewReference = new WeakReference<TextView>(logTV);
188 }
189
190 protected String doInBackground(String... args) {
191 return readLogFile();
192 }
193
194 protected void onPostExecute(String result) {
195 if (textViewReference != null && result != null) {
196 final TextView logTV = textViewReference.get();
197 if (logTV != null) {
198 mLogText = result;
199 logTV.setText(mLogText);
200 dismissLoadingDialog();
201 }
202 }
203 }
204
205 /**
206 * Read and show log file info
207 */
208 private String readLogFile() {
209
210 String[] logFileName = Log_OC.getLogFileNames();
211
212 //Read text from files
213 StringBuilder text = new StringBuilder();
214
215 BufferedReader br = null;
216 try {
217 String line;
218
219 for (int i = logFileName.length-1; i >= 0; i--) {
220 File file = new File(mLogPath,logFileName[i]);
221 if (file.exists()) {
222 // Check if FileReader is ready
223 if (new FileReader(file).ready()) {
224 br = new BufferedReader(new FileReader(file));
225 while ((line = br.readLine()) != null) {
226 // Append the log info
227 text.append(line);
228 text.append('\n');
229 }
230 }
231 }
232 }
233 }
234 catch (IOException e) {
235 Log_OC.d(TAG, e.getMessage().toString());
236
237 } finally {
238 if (br != null) {
239 try {
240 br.close();
241 } catch (IOException e) {
242 // ignore
243 }
244 }
245 }
246
247 return text.toString();
248 }
249 }
250
251 /**
252 * Show loading dialog
253 */
254 public void showLoadingDialog() {
255 // Construct dialog
256 LoadingDialog loading = new LoadingDialog(
257 getResources().getString(R.string.log_progress_dialog_text)
258 );
259 FragmentManager fm = getSupportFragmentManager();
260 FragmentTransaction ft = fm.beginTransaction();
261 loading.show(ft, DIALOG_WAIT_TAG);
262 }
263
264 /**
265 * Dismiss loading dialog
266 */
267 public void dismissLoadingDialog(){
268 Fragment frag = getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG);
269 if (frag != null) {
270 LoadingDialog loading = (LoadingDialog) frag;
271 loading.dismiss();
272 }
273 }
274
275 @Override
276 protected void onSaveInstanceState(Bundle outState) {
277 super.onSaveInstanceState(outState);
278
279 /// global state
280 outState.putString(KEY_LOG_TEXT, mLogText);
281 }
282 }