integrating @davivels remarks
[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 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
63
64 @Override
65 protected void onCreate(Bundle savedInstanceState) {
66 super.onCreate(savedInstanceState);
67
68 setContentView(R.layout.log_send_file);
69 setTitle(getText(R.string.actionbar_logger));
70 ActionBar actionBar = getSherlock().getActionBar();
71 actionBar.setIcon(DisplayUtils.getSeasonalIconId());
72 actionBar.setDisplayHomeAsUpEnabled(true);
73 Button deleteHistoryButton = (Button) findViewById(R.id.deleteLogHistoryButton);
74 Button sendHistoryButton = (Button) findViewById(R.id.sendLogHistoryButton);
75
76 deleteHistoryButton.setOnClickListener(new OnClickListener() {
77
78 @Override
79 public void onClick(View v) {
80
81 Log_OC.deleteHistoryLogging();
82 finish();
83 }
84 });
85
86 sendHistoryButton.setOnClickListener(new OnClickListener() {
87
88 @Override
89 public void onClick(View v) {
90 sendMail();
91 }
92 });
93
94 if (mLogPath != null) {
95 logDIR = new File(mLogPath);
96 }
97
98 if (logDIR != null && logDIR.isDirectory()) {
99 // Show a dialog while log data is being loaded
100 showLoadingDialog();
101
102 TextView logTV = (TextView) findViewById(R.id.logTV);
103
104 // Start a new thread that will load all the log data
105 LoadingLogTask task = new LoadingLogTask(logTV);
106 task.execute();
107
108 }
109 }
110
111 @Override
112 public boolean onMenuItemSelected(int featureId, MenuItem item) {
113 super.onMenuItemSelected(featureId, item);
114 switch (item.getItemId()) {
115 case android.R.id.home:
116 finish();
117 break;
118 default:
119 return false;
120 }
121 return true;
122 }
123
124
125 /**
126 * Start activity for sending email with logs attached
127 */
128 private void sendMail() {
129
130 // For the moment we need to consider the possibility that setup.xml
131 // does not include the "mail_logger" entry. This block prevents that
132 // compilation fails in this case.
133 String emailAddress;
134 try {
135 Class<?> stringClass = R.string.class;
136 Field mailLoggerField = stringClass.getField("mail_logger");
137 int emailAddressId = (Integer) mailLoggerField.get(null);
138 emailAddress = getString(emailAddressId);
139 } catch (Exception e) {
140 emailAddress = "";
141 }
142
143 ArrayList<Uri> uris = new ArrayList<Uri>();
144
145 // Convert from paths to Android friendly Parcelable Uri's
146 for (String file : Log_OC.getLogFileNames())
147 {
148 File logFile = new File(mLogPath, file);
149 if (logFile.exists()) {
150 uris.add(Uri.fromFile(logFile));
151 }
152 }
153
154 Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
155
156 intent.putExtra(Intent.EXTRA_EMAIL, emailAddress);
157 String subject = String.format(getString(R.string.log_send_mail_subject), getString(R.string.app_name));
158 intent.putExtra(Intent.EXTRA_SUBJECT, subject);
159 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
160 intent.setType(MAIL_ATTACHMENT_TYPE);
161 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
162 try {
163 startActivity(intent);
164 } catch (ActivityNotFoundException e) {
165 Toast.makeText(this, getString(R.string.log_send_no_mail_app), Toast.LENGTH_LONG).show();
166 Log_OC.i(TAG, "Could not find app for sending log history.");
167 }
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 logTV.setText(result);
193 dismissLoadingDialog();
194 }
195 }
196 }
197
198 /**
199 * Read and show log file info
200 */
201 private String readLogFile() {
202
203 String[] logFileName = Log_OC.getLogFileNames();
204
205 //Read text from files
206 StringBuilder text = new StringBuilder();
207
208 BufferedReader br = null;
209 try {
210 String line;
211
212 for (int i = logFileName.length-1; i >= 0; i--) {
213 File file = new File(mLogPath,logFileName[i]);
214 if (file.exists()) {
215 // Check if FileReader is ready
216 if (new FileReader(file).ready()) {
217 br = new BufferedReader(new FileReader(file));
218 while ((line = br.readLine()) != null) {
219 // Append the log info
220 text.append(line);
221 text.append('\n');
222 }
223 }
224 }
225 }
226 }
227 catch (IOException e) {
228 Log_OC.d(TAG, e.getMessage().toString());
229
230 } finally {
231 if (br != null) {
232 try {
233 br.close();
234 } catch (IOException e) {
235 // ignore
236 }
237 }
238 }
239
240 return text.toString();
241 }
242 }
243
244 /**
245 * Show loading dialog
246 */
247 public void showLoadingDialog() {
248 // Construct dialog
249 LoadingDialog loading = new LoadingDialog(
250 getResources().getString(R.string.log_progress_dialog_text)
251 );
252 FragmentManager fm = getSupportFragmentManager();
253 FragmentTransaction ft = fm.beginTransaction();
254 loading.show(ft, DIALOG_WAIT_TAG);
255 }
256
257 /**
258 * Dismiss loading dialog
259 */
260 public void dismissLoadingDialog(){
261 Fragment frag = getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG);
262 if (frag != null) {
263 LoadingDialog loading = (LoadingDialog) frag;
264 loading.dismiss();
265 }
266 }
267 }