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
.lang
.reflect
.Field
;
26 import java
.util
.ArrayList
;
28 import android
.content
.ActivityNotFoundException
;
29 import android
.content
.Intent
;
30 import android
.net
.Uri
;
31 import android
.os
.AsyncTask
;
32 import android
.os
.Bundle
;
33 import android
.support
.v4
.app
.Fragment
;
34 import android
.support
.v4
.app
.FragmentManager
;
35 import android
.support
.v4
.app
.FragmentTransaction
;
36 import android
.view
.View
;
37 import android
.view
.View
.OnClickListener
;
38 import android
.widget
.Button
;
39 import android
.widget
.TextView
;
40 import android
.widget
.Toast
;
42 import com
.actionbarsherlock
.app
.ActionBar
;
43 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
44 import com
.actionbarsherlock
.view
.MenuItem
;
45 import com
.owncloud
.android
.R
;
46 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
47 import com
.owncloud
.android
.ui
.dialog
.LoadingDialog
;
48 import com
.owncloud
.android
.utils
.DisplayUtils
;
49 import com
.owncloud
.android
.utils
.FileStorageUtils
;
52 public class LogHistoryActivity
extends SherlockFragmentActivity
{
54 private static final String MAIL_ATTACHMENT_TYPE
= "text/plain";
56 private static final String KEY_LOG_TEXT
= "LOG_TEXT";
58 private static final String TAG
= LogHistoryActivity
.class.getSimpleName();
60 private static final String DIALOG_WAIT_TAG
= "DIALOG_WAIT";
62 private String mLogPath
= FileStorageUtils
.getLogPath();
63 private File logDIR
= null
;
64 private String mLogText
;
68 protected void onCreate(Bundle savedInstanceState
) {
69 super.onCreate(savedInstanceState
);
71 setContentView(R
.layout
.log_send_file
);
72 setTitle(getText(R
.string
.actionbar_logger
));
73 ActionBar actionBar
= getSherlock().getActionBar();
74 actionBar
.setIcon(DisplayUtils
.getSeasonalIconId());
75 actionBar
.setDisplayHomeAsUpEnabled(true
);
76 Button deleteHistoryButton
= (Button
) findViewById(R
.id
.deleteLogHistoryButton
);
77 Button sendHistoryButton
= (Button
) findViewById(R
.id
.sendLogHistoryButton
);
78 TextView logTV
= (TextView
) findViewById(R
.id
.logTV
);
80 deleteHistoryButton
.setOnClickListener(new OnClickListener() {
83 public void onClick(View v
) {
85 Log_OC
.deleteHistoryLogging();
90 sendHistoryButton
.setOnClickListener(new OnClickListener() {
93 public void onClick(View v
) {
98 if (savedInstanceState
== null
) {
99 if (mLogPath
!= null
) {
100 logDIR
= new File(mLogPath
);
103 if (logDIR
!= null
&& logDIR
.isDirectory()) {
104 // Show a dialog while log data is being loaded
107 // Start a new thread that will load all the log data
108 LoadingLogTask task
= new LoadingLogTask(logTV
);
112 mLogText
= savedInstanceState
.getString(KEY_LOG_TEXT
);
113 logTV
.setText(mLogText
);
118 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
119 super.onMenuItemSelected(featureId
, item
);
120 switch (item
.getItemId()) {
121 case android
.R
.id
.home
:
132 * Start activity for sending email with logs attached
134 private void sendMail() {
136 // For the moment we need to consider the possibility that setup.xml
137 // does not include the "mail_logger" entry. This block prevents that
138 // compilation fails in this case.
141 Class
<?
> stringClass
= R
.string
.class;
142 Field mailLoggerField
= stringClass
.getField("mail_logger");
143 int emailAddressId
= (Integer
) mailLoggerField
.get(null
);
144 emailAddress
= getString(emailAddressId
);
145 } catch (Exception e
) {
149 ArrayList
<Uri
> uris
= new ArrayList
<Uri
>();
151 // Convert from paths to Android friendly Parcelable Uri's
152 for (String file
: Log_OC
.getLogFileNames())
154 File logFile
= new File(mLogPath
, file
);
155 if (logFile
.exists()) {
156 uris
.add(Uri
.fromFile(logFile
));
160 Intent intent
= new Intent(Intent
.ACTION_SEND_MULTIPLE
);
162 intent
.putExtra(Intent
.EXTRA_EMAIL
, emailAddress
);
163 String subject
= String
.format(getString(R
.string
.log_send_mail_subject
), getString(R
.string
.app_name
));
164 intent
.putExtra(Intent
.EXTRA_SUBJECT
, subject
);
165 intent
.setFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
166 intent
.setType(MAIL_ATTACHMENT_TYPE
);
167 intent
.putParcelableArrayListExtra(Intent
.EXTRA_STREAM
, uris
);
169 startActivity(intent
);
170 } catch (ActivityNotFoundException e
) {
171 Toast
.makeText(this, getString(R
.string
.log_send_no_mail_app
), Toast
.LENGTH_LONG
).show();
172 Log_OC
.i(TAG
, "Could not find app for sending log history.");
179 * Class for loading the log data async
182 private class LoadingLogTask
extends AsyncTask
<String
, Void
, String
> {
183 private final WeakReference
<TextView
> textViewReference
;
185 public LoadingLogTask(TextView logTV
){
186 // Use of a WeakReference to ensure the TextView can be garbage collected
187 textViewReference
= new WeakReference
<TextView
>(logTV
);
190 protected String
doInBackground(String
... args
) {
191 return readLogFile();
194 protected void onPostExecute(String result
) {
195 if (textViewReference
!= null
&& result
!= null
) {
196 final TextView logTV
= textViewReference
.get();
199 logTV
.setText(mLogText
);
200 dismissLoadingDialog();
206 * Read and show log file info
208 private String
readLogFile() {
210 String
[] logFileName
= Log_OC
.getLogFileNames();
212 //Read text from files
213 StringBuilder text
= new StringBuilder();
215 BufferedReader br
= null
;
219 for (int i
= logFileName
.length
-1; i
>= 0; i
--) {
220 File file
= new File(mLogPath
,logFileName
[i
]);
222 // Check if FileReader is ready
223 if (new FileReader(file
).ready()) {
224 br
= new BufferedReader(new FileReader(file
));
225 while ((line
= br
.readLine()) != null
) {
226 // Append the log info
234 catch (IOException e
) {
235 Log_OC
.d(TAG
, e
.getMessage().toString());
241 } catch (IOException e
) {
247 return text
.toString();
252 * Show loading dialog
254 public void showLoadingDialog() {
256 LoadingDialog loading
= new LoadingDialog(
257 getResources().getString(R
.string
.log_progress_dialog_text
)
259 FragmentManager fm
= getSupportFragmentManager();
260 FragmentTransaction ft
= fm
.beginTransaction();
261 loading
.show(ft
, DIALOG_WAIT_TAG
);
265 * Dismiss loading dialog
267 public void dismissLoadingDialog(){
268 Fragment frag
= getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG
);
270 LoadingDialog loading
= (LoadingDialog
) frag
;
276 protected void onSaveInstanceState(Bundle outState
) {
277 super.onSaveInstanceState(outState
);
280 outState
.putString(KEY_LOG_TEXT
, mLogText
);