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