Enable log into text file automatically when BuildConfig.DEBUG is true. Open log...
[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
25 import android.content.Intent;
26 import android.net.Uri;
27 import android.os.Bundle;
28
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
41
42 public class LogHistoryActivity extends SherlockActivity {
43 String mLogPath = FileStorageUtils.getLogPath();
44
45 private static final String MAIL_ATTACHMENT_TYPE = "plain/text";
46 private static final String LOGGER_FILE_NAME = "log.txt";
47
48 File logDIR = null;
49
50
51 @Override
52 protected void onCreate(Bundle savedInstanceState) {
53 super.onCreate(savedInstanceState);
54
55 setContentView(R.layout.log_send_file);
56 setTitle(getText(R.string.actionbar_logger));
57 ActionBar actionBar = getSherlock().getActionBar();
58 actionBar.setIcon(DisplayUtils.getSeasonalIconId());
59 actionBar.setDisplayHomeAsUpEnabled(true);
60 Button deleteHistoryButton = (Button) findViewById(R.id.deleteLogHistoryButton);
61 Button sendHistoryButton = (Button) findViewById(R.id.sendLogHistoryButton);
62
63 deleteHistoryButton.setOnClickListener(new OnClickListener() {
64
65 @Override
66 public void onClick(View v) {
67 File dir = new File(mLogPath);
68 if (dir != null) {
69 File[] files = dir.listFiles();
70 if(files!=null) {
71 for(File f: files) {
72 f.delete();
73 }
74 }
75 dir.delete();
76 }
77 finish();
78 }
79
80 });
81
82
83 sendHistoryButton.setOnClickListener(new OnClickListener() {
84
85 @Override
86 public void onClick(View v) {
87 sendMail();
88 }
89
90 });
91
92
93 if(mLogPath != null){
94 logDIR = new File(mLogPath);
95 }
96
97 if(logDIR != null && logDIR.isDirectory()) {
98 // File[] files = logDIR.listFiles();
99 //
100 // if (files != null && files.length != 0) {
101 // ArrayList<String> logfiles_name = new ArrayList<String>();
102 // for (File file : files) {
103 // logfiles_name.add(file.getName());
104 // }
105 // String[] logFiles2Array = logfiles_name.toArray(new String[logfiles_name.size()]);
106 // LogListAdapter listadapter = new LogListAdapter(this,logFiles2Array);
107 // listView.setAdapter(listadapter);
108 // }
109
110 readLogFile();
111
112 }
113 }
114
115
116 @Override
117 public boolean onMenuItemSelected(int featureId, MenuItem item) {
118 super.onMenuItemSelected(featureId, item);
119
120 switch (item.getItemId()) {
121 case android.R.id.home:
122 finish();
123 break;
124 default:
125 return false;
126 }
127 return true;
128 }
129
130
131 /**
132 * Start activity for sending email with logs attached
133 */
134 private void sendMail() {
135
136 String emailAddresses[] = { getText(R.string.mail_logger).toString() };
137
138 Uri uri = Uri.parse("file://" + mLogPath + File.separator + LOGGER_FILE_NAME);
139 Intent intent = new Intent(Intent.ACTION_SEND);
140
141 // Explicitly only use Gmail to send
142 intent.setClassName("com.google.android.gm","com.google.android.gm.ComposeActivityGmail");
143
144 intent.putExtra(Intent.EXTRA_EMAIL, emailAddresses);
145 intent.putExtra(Intent.EXTRA_SUBJECT, getText(R.string.log_mail_subject));
146 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
147 intent.setType(MAIL_ATTACHMENT_TYPE);
148
149 intent.putExtra(Intent.EXTRA_STREAM, uri);
150 if (intent.resolveActivity(getPackageManager()) != null) {
151 startActivity(intent);
152 }
153 }
154
155
156 /**
157 * Read and show log file info
158 */
159 private void readLogFile() {
160
161 //Get the text file
162 File file = new File(mLogPath,LOGGER_FILE_NAME);
163
164 //Read text from file
165 StringBuilder text = new StringBuilder();
166
167 try {
168 BufferedReader br = new BufferedReader(new FileReader(file));
169 String line;
170
171 while ((line = br.readLine()) != null) {
172 text.append(line);
173 text.append('\n');
174 }
175 }
176 catch (IOException e) {
177
178 }
179
180
181 TextView logTV = (TextView) findViewById(R.id.logTV);
182 logTV.setText(text);
183 }
184 }