Move Logger class to owncloud library and update references. Add async task for loadi...
[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.view.View;
32 import android.view.View.OnClickListener;
33 import android.widget.Button;
34 import android.widget.TextView;
35
36 import com.actionbarsherlock.app.ActionBar;
37 import com.actionbarsherlock.app.SherlockActivity;
38 import com.actionbarsherlock.view.MenuItem;
39 import com.owncloud.android.R;
40 import com.owncloud.android.lib.common.utils.Log_OC;
41 import com.owncloud.android.utils.DisplayUtils;
42 import com.owncloud.android.utils.FileStorageUtils;
43
44
45 public class LogHistoryActivity extends SherlockActivity {
46
47 private static final String MAIL_ATTACHMENT_TYPE = "text/plain";
48
49 private static final String TAG = LogHistoryActivity.class.getSimpleName();
50
51 private String mLogPath = FileStorageUtils.getLogPath();
52 private File logDIR = null;
53
54 private ProgressDialog mPd = null;
55 private TextView mLogTV;
56
57
58 @Override
59 protected void onCreate(Bundle savedInstanceState) {
60 super.onCreate(savedInstanceState);
61
62 setContentView(R.layout.log_send_file);
63 setTitle(getText(R.string.actionbar_logger));
64 ActionBar actionBar = getSherlock().getActionBar();
65 actionBar.setIcon(DisplayUtils.getSeasonalIconId());
66 actionBar.setDisplayHomeAsUpEnabled(true);
67 Button deleteHistoryButton = (Button) findViewById(R.id.deleteLogHistoryButton);
68 Button sendHistoryButton = (Button) findViewById(R.id.sendLogHistoryButton);
69
70 deleteHistoryButton.setOnClickListener(new OnClickListener() {
71
72 @Override
73 public void onClick(View v) {
74
75 Log_OC.deleteHistoryLogging();
76 finish();
77 }
78 });
79
80 sendHistoryButton.setOnClickListener(new OnClickListener() {
81
82 @Override
83 public void onClick(View v) {
84 sendMail();
85 }
86 });
87
88 mLogTV = (TextView) findViewById(R.id.logTV);
89
90 if (mLogPath != null) {
91 logDIR = new File(mLogPath);
92 }
93
94 if (logDIR != null && logDIR.isDirectory()) {
95 // Show the ProgressDialog while log data is being loaded
96 mPd = ProgressDialog.show(this, getText(R.string.actionbar_logger),
97 getText(R.string.log_progress_dialog_text), true, false);
98
99 // Start a new thread that will load all the log data
100 new LoadingLogTask().execute();
101 }
102 }
103
104
105 @Override
106 public boolean onMenuItemSelected(int featureId, MenuItem item) {
107 super.onMenuItemSelected(featureId, item);
108 switch (item.getItemId()) {
109 case android.R.id.home:
110 finish();
111 break;
112 default:
113 return false;
114 }
115 return true;
116 }
117
118
119 /**
120 * Start activity for sending email with logs attached
121 */
122 private void sendMail() {
123
124 String emailAddresses[] = { getText(R.string.mail_logger).toString() };
125
126 ArrayList<Uri> uris = new ArrayList<Uri>();
127
128 // Convert from paths to Android friendly Parcelable Uri's
129 for (String file : Log_OC.getLogFileNames())
130 {
131 if (new File(mLogPath + File.separator, file).exists()) {
132 Uri u = Uri.parse("file://" + mLogPath + File.separator + file);
133 uris.add(u);
134 }
135 }
136
137 Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
138
139 // Explicitly only use Gmail to send
140 intent.setClassName("com.google.android.gm","com.google.android.gm.ComposeActivityGmail");
141 intent.putExtra(Intent.EXTRA_EMAIL, emailAddresses);
142 intent.putExtra(Intent.EXTRA_SUBJECT, getText(R.string.log_mail_subject));
143 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
144 intent.setType(MAIL_ATTACHMENT_TYPE);
145 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
146
147 if (intent.resolveActivity(getPackageManager()) != null) {
148 startActivity(intent);
149 }
150 }
151
152
153 /**
154 * Read and show log file info
155 */
156 private String readLogFile() {
157
158 String[] logFileName = Log_OC.getLogFileNames();
159
160 //Read text from files
161 StringBuilder text = new StringBuilder();
162
163 try {
164
165 String line;
166
167 for (int i = logFileName.length-1; i >= 0; i--) {
168 File file = new File(mLogPath,logFileName[i]);
169 if (file.exists()) {
170 // Check if FileReader is ready
171 if (new FileReader(file).ready()) {
172 BufferedReader br = new BufferedReader(new FileReader(file));
173 while ((line = br.readLine()) != null) {
174 // Append the log info
175 text.append(line);
176 text.append('\n');
177 }
178 }
179 }
180 }
181 }
182 catch (IOException e) {
183 Log_OC.d(TAG, e.getMessage().toString());
184 }
185
186 return text.toString();
187 }
188
189
190 /**
191 *
192 * Class for loading the log data async
193 *
194 */
195 private class LoadingLogTask extends AsyncTask<String, Void, String> {
196 protected String doInBackground(String... args) {
197 return readLogFile();
198 }
199
200 protected void onPostExecute(String result) {
201 mLogTV.setText(result);
202
203 if (mPd != null) {
204 mPd.dismiss();
205 }
206 }
207 }
208 }