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