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