1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
18 package com
.owncloud
.android
.ui
.activity
;
20 import java
.io
.BufferedReader
;
22 import java
.io
.FileReader
;
23 import java
.io
.IOException
;
24 import java
.lang
.ref
.WeakReference
;
25 import java
.util
.ArrayList
;
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
;
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
;
49 public class LogHistoryActivity
extends SherlockFragmentActivity
{
51 private static final String MAIL_ATTACHMENT_TYPE
= "text/plain";
53 private static final String TAG
= LogHistoryActivity
.class.getSimpleName();
55 private static final String DIALOG_WAIT_TAG
= "DIALOG_WAIT";
57 private String mLogPath
= FileStorageUtils
.getLogPath();
58 private File logDIR
= null
;
62 protected void onCreate(Bundle savedInstanceState
) {
63 super.onCreate(savedInstanceState
);
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
);
73 deleteHistoryButton
.setOnClickListener(new OnClickListener() {
76 public void onClick(View v
) {
78 Log_OC
.deleteHistoryLogging();
83 sendHistoryButton
.setOnClickListener(new OnClickListener() {
86 public void onClick(View v
) {
91 if (mLogPath
!= null
) {
92 logDIR
= new File(mLogPath
);
95 if (logDIR
!= null
&& logDIR
.isDirectory()) {
96 // Show a dialog while log data is being loaded
99 TextView logTV
= (TextView
) findViewById(R
.id
.logTV
);
101 // Start a new thread that will load all the log data
102 LoadingLogTask task
= new LoadingLogTask(logTV
);
109 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
110 super.onMenuItemSelected(featureId
, item
);
111 switch (item
.getItemId()) {
112 case android
.R
.id
.home
:
123 * Start activity for sending email with logs attached
125 private void sendMail() {
127 String emailAddresses
[] = { getText(R
.string
.mail_logger
).toString() };
129 ArrayList
<Uri
> uris
= new ArrayList
<Uri
>();
131 // Convert from paths to Android friendly Parcelable Uri's
132 for (String file
: Log_OC
.getLogFileNames())
134 if (new File(mLogPath
+ File
.separator
, file
).exists()) {
135 Uri u
= Uri
.parse("file://" + mLogPath
+ File
.separator
+ file
);
140 Intent intent
= new Intent(Intent
.ACTION_SEND_MULTIPLE
);
142 // Explicitly only use Gmail to send
143 intent
.setClassName("com.google.android.gm","com.google.android.gm.ComposeActivityGmail");
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 intent
.putParcelableArrayListExtra(Intent
.EXTRA_STREAM
, uris
);
150 if (intent
.resolveActivity(getPackageManager()) != null
) {
151 startActivity(intent
);
157 * Class for loading the log data async
160 private class LoadingLogTask
extends AsyncTask
<String
, Void
, String
> {
161 private final WeakReference
<TextView
> textViewReference
;
163 public LoadingLogTask(TextView logTV
){
164 // Use of a WeakReference to ensure the TextView can be garbage collected
165 textViewReference
= new WeakReference
<TextView
>(logTV
);
168 protected String
doInBackground(String
... args
) {
169 return readLogFile();
172 protected void onPostExecute(String result
) {
173 if (textViewReference
!= null
&& result
!= null
) {
174 final TextView logTV
= textViewReference
.get();
176 logTV
.setText(result
);
177 dismissLoadingDialog();
183 * Read and show log file info
185 private String
readLogFile() {
187 String
[] logFileName
= Log_OC
.getLogFileNames();
189 //Read text from files
190 StringBuilder text
= new StringBuilder();
196 for (int i
= logFileName
.length
-1; i
>= 0; i
--) {
197 File file
= new File(mLogPath
,logFileName
[i
]);
199 // Check if FileReader is ready
200 if (new FileReader(file
).ready()) {
201 BufferedReader br
= new BufferedReader(new FileReader(file
));
202 while ((line
= br
.readLine()) != null
) {
203 // Append the log info
211 catch (IOException e
) {
212 Log_OC
.d(TAG
, e
.getMessage().toString());
215 return text
.toString();
220 * Show loading dialog
222 public void showLoadingDialog() {
224 LoadingDialog loading
= new LoadingDialog(getResources().getString(R
.string
.log_progress_dialog_text
));
225 FragmentManager fm
= getSupportFragmentManager();
226 FragmentTransaction ft
= fm
.beginTransaction();
227 loading
.show(ft
, DIALOG_WAIT_TAG
);
231 * Dismiss loading dialog
233 public void dismissLoadingDialog(){
234 Fragment frag
= getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG
);
236 LoadingDialog loading
= (LoadingDialog
) frag
;