2 * ownCloud Android client application
4 * Copyright (C) 2015 ownCloud Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 package com
.owncloud
.android
.ui
.activity
;
22 import java
.io
.BufferedReader
;
24 import java
.io
.FileReader
;
25 import java
.io
.IOException
;
26 import java
.lang
.ref
.WeakReference
;
27 import java
.lang
.reflect
.Field
;
28 import java
.util
.ArrayList
;
30 import android
.content
.ActivityNotFoundException
;
31 import android
.content
.Intent
;
32 import android
.net
.Uri
;
33 import android
.os
.AsyncTask
;
34 import android
.os
.Bundle
;
35 import android
.support
.v4
.app
.Fragment
;
36 import android
.support
.v4
.app
.FragmentManager
;
37 import android
.support
.v4
.app
.FragmentTransaction
;
38 import android
.support
.v7
.app
.AppCompatActivity
;
39 import android
.view
.MenuItem
;
40 import android
.view
.View
;
41 import android
.view
.View
.OnClickListener
;
42 import android
.widget
.Button
;
43 import android
.widget
.TextView
;
44 import android
.widget
.Toast
;
46 import com
.owncloud
.android
.R
;
47 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
48 import com
.owncloud
.android
.ui
.dialog
.LoadingDialog
;
49 import com
.owncloud
.android
.utils
.DisplayUtils
;
50 import com
.owncloud
.android
.utils
.FileStorageUtils
;
53 public class LogHistoryActivity
extends AppCompatActivity
{
55 private static final String MAIL_ATTACHMENT_TYPE
= "text/plain";
57 private static final String KEY_LOG_TEXT
= "LOG_TEXT";
59 private static final String TAG
= LogHistoryActivity
.class.getSimpleName();
61 private static final String DIALOG_WAIT_TAG
= "DIALOG_WAIT";
63 private String mLogPath
= FileStorageUtils
.getLogPath();
64 private File logDIR
= null
;
65 private String mLogText
;
69 protected void onCreate(Bundle savedInstanceState
) {
70 super.onCreate(savedInstanceState
);
72 setContentView(R
.layout
.log_send_file
);
73 setTitle(getText(R
.string
.actionbar_logger
));
74 getSupportActionBar().setDisplayHomeAsUpEnabled(true
);
75 Button deleteHistoryButton
= (Button
) findViewById(R
.id
.deleteLogHistoryButton
);
76 Button sendHistoryButton
= (Button
) findViewById(R
.id
.sendLogHistoryButton
);
77 TextView logTV
= (TextView
) findViewById(R
.id
.logTV
);
79 deleteHistoryButton
.setOnClickListener(new OnClickListener() {
82 public void onClick(View v
) {
84 Log_OC
.deleteHistoryLogging();
89 sendHistoryButton
.setOnClickListener(new OnClickListener() {
92 public void onClick(View v
) {
97 if (savedInstanceState
== null
) {
98 if (mLogPath
!= null
) {
99 logDIR
= new File(mLogPath
);
102 if (logDIR
!= null
&& logDIR
.isDirectory()) {
103 // Show a dialog while log data is being loaded
106 // Start a new thread that will load all the log data
107 LoadingLogTask task
= new LoadingLogTask(logTV
);
111 mLogText
= savedInstanceState
.getString(KEY_LOG_TEXT
);
112 logTV
.setText(mLogText
);
117 public boolean onOptionsItemSelected(MenuItem item
) {
118 super.onOptionsItemSelected(item
);
119 switch (item
.getItemId()) {
120 case android
.R
.id
.home
:
131 * Start activity for sending email with logs attached
133 private void sendMail() {
135 // For the moment we need to consider the possibility that setup.xml
136 // does not include the "mail_logger" entry. This block prevents that
137 // compilation fails in this case.
140 Class
<?
> stringClass
= R
.string
.class;
141 Field mailLoggerField
= stringClass
.getField("mail_logger");
142 int emailAddressId
= (Integer
) mailLoggerField
.get(null
);
143 emailAddress
= getString(emailAddressId
);
144 } catch (Exception e
) {
148 ArrayList
<Uri
> uris
= new ArrayList
<Uri
>();
150 // Convert from paths to Android friendly Parcelable Uri's
151 for (String file
: Log_OC
.getLogFileNames())
153 File logFile
= new File(mLogPath
, file
);
154 if (logFile
.exists()) {
155 uris
.add(Uri
.fromFile(logFile
));
159 Intent intent
= new Intent(Intent
.ACTION_SEND_MULTIPLE
);
161 intent
.putExtra(Intent
.EXTRA_EMAIL
, emailAddress
);
162 String subject
= String
.format(getString(R
.string
.log_send_mail_subject
), getString(R
.string
.app_name
));
163 intent
.putExtra(Intent
.EXTRA_SUBJECT
, subject
);
164 intent
.setFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
165 intent
.setType(MAIL_ATTACHMENT_TYPE
);
166 intent
.putParcelableArrayListExtra(Intent
.EXTRA_STREAM
, uris
);
168 startActivity(intent
);
169 } catch (ActivityNotFoundException e
) {
170 Toast
.makeText(this, getString(R
.string
.log_send_no_mail_app
), Toast
.LENGTH_LONG
).show();
171 Log_OC
.i(TAG
, "Could not find app for sending log history.");
178 * Class for loading the log data async
181 private class LoadingLogTask
extends AsyncTask
<String
, Void
, String
> {
182 private final WeakReference
<TextView
> textViewReference
;
184 public LoadingLogTask(TextView logTV
){
185 // Use of a WeakReference to ensure the TextView can be garbage collected
186 textViewReference
= new WeakReference
<TextView
>(logTV
);
189 protected String
doInBackground(String
... args
) {
190 return readLogFile();
193 protected void onPostExecute(String result
) {
194 if (textViewReference
!= null
&& result
!= null
) {
195 final TextView logTV
= textViewReference
.get();
198 logTV
.setText(mLogText
);
199 dismissLoadingDialog();
205 * Read and show log file info
207 private String
readLogFile() {
209 String
[] logFileName
= Log_OC
.getLogFileNames();
211 //Read text from files
212 StringBuilder text
= new StringBuilder();
214 BufferedReader br
= null
;
218 for (int i
= logFileName
.length
-1; i
>= 0; i
--) {
219 File file
= new File(mLogPath
,logFileName
[i
]);
221 // Check if FileReader is ready
222 if (new FileReader(file
).ready()) {
223 br
= new BufferedReader(new FileReader(file
));
224 while ((line
= br
.readLine()) != null
) {
225 // Append the log info
233 catch (IOException e
) {
234 Log_OC
.d(TAG
, e
.getMessage().toString());
240 } catch (IOException e
) {
246 return text
.toString();
251 * Show loading dialog
253 public void showLoadingDialog() {
255 LoadingDialog loading
= new LoadingDialog(
256 getResources().getString(R
.string
.log_progress_dialog_text
)
258 FragmentManager fm
= getSupportFragmentManager();
259 FragmentTransaction ft
= fm
.beginTransaction();
260 loading
.show(ft
, DIALOG_WAIT_TAG
);
264 * Dismiss loading dialog
266 public void dismissLoadingDialog(){
267 Fragment frag
= getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG
);
269 LoadingDialog loading
= (LoadingDialog
) frag
;
275 protected void onSaveInstanceState(Bundle outState
) {
276 super.onSaveInstanceState(outState
);
279 outState
.putString(KEY_LOG_TEXT
, mLogText
);