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