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