LogHistoryActivity.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * Copyright (C) 2015 ownCloud Inc.
  5. *
  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.
  9. *
  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.
  14. *
  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/>.
  17. *
  18. */
  19. package com.owncloud.android.ui.activity;
  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.lang.reflect.Field;
  26. import java.util.ArrayList;
  27. import android.content.ActivityNotFoundException;
  28. import android.content.Intent;
  29. import android.net.Uri;
  30. import android.os.AsyncTask;
  31. import android.os.Bundle;
  32. import android.support.v4.app.Fragment;
  33. import android.support.v4.app.FragmentManager;
  34. import android.support.v4.app.FragmentTransaction;
  35. import android.support.v7.app.AppCompatActivity;
  36. import android.view.MenuItem;
  37. import android.view.View;
  38. import android.view.View.OnClickListener;
  39. import android.widget.Button;
  40. import android.widget.TextView;
  41. import android.widget.Toast;
  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. public class LogHistoryActivity extends AppCompatActivity {
  48. private static final String MAIL_ATTACHMENT_TYPE = "text/plain";
  49. private static final String KEY_LOG_TEXT = "LOG_TEXT";
  50. private static final String TAG = LogHistoryActivity.class.getSimpleName();
  51. private static final String DIALOG_WAIT_TAG = "DIALOG_WAIT";
  52. private String mLogPath = FileStorageUtils.getLogPath();
  53. private File logDIR = null;
  54. private String mLogText;
  55. @Override
  56. protected void onCreate(Bundle savedInstanceState) {
  57. super.onCreate(savedInstanceState);
  58. setContentView(R.layout.log_send_file);
  59. setTitle(getText(R.string.actionbar_logger));
  60. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  61. Button deleteHistoryButton = (Button) findViewById(R.id.deleteLogHistoryButton);
  62. Button sendHistoryButton = (Button) findViewById(R.id.sendLogHistoryButton);
  63. TextView logTV = (TextView) findViewById(R.id.logTV);
  64. deleteHistoryButton.setOnClickListener(new OnClickListener() {
  65. @Override
  66. public void onClick(View v) {
  67. Log_OC.deleteHistoryLogging();
  68. finish();
  69. }
  70. });
  71. sendHistoryButton.setOnClickListener(new OnClickListener() {
  72. @Override
  73. public void onClick(View v) {
  74. sendMail();
  75. }
  76. });
  77. if (savedInstanceState == null) {
  78. if (mLogPath != null) {
  79. logDIR = new File(mLogPath);
  80. }
  81. if (logDIR != null && logDIR.isDirectory()) {
  82. // Show a dialog while log data is being loaded
  83. showLoadingDialog();
  84. // Start a new thread that will load all the log data
  85. LoadingLogTask task = new LoadingLogTask(logTV);
  86. task.execute();
  87. }
  88. } else {
  89. mLogText = savedInstanceState.getString(KEY_LOG_TEXT);
  90. logTV.setText(mLogText);
  91. }
  92. }
  93. @Override
  94. public boolean onOptionsItemSelected(MenuItem item) {
  95. boolean retval = true;
  96. switch (item.getItemId()) {
  97. case android.R.id.home:
  98. finish();
  99. break;
  100. default:
  101. retval = super.onOptionsItemSelected(item);
  102. }
  103. return retval;
  104. }
  105. /**
  106. * Start activity for sending email with logs attached
  107. */
  108. private void sendMail() {
  109. // For the moment we need to consider the possibility that setup.xml
  110. // does not include the "mail_logger" entry. This block prevents that
  111. // compilation fails in this case.
  112. String emailAddress;
  113. try {
  114. Class<?> stringClass = R.string.class;
  115. Field mailLoggerField = stringClass.getField("mail_logger");
  116. int emailAddressId = (Integer) mailLoggerField.get(null);
  117. emailAddress = getString(emailAddressId);
  118. } catch (Exception e) {
  119. emailAddress = "";
  120. }
  121. ArrayList<Uri> uris = new ArrayList<Uri>();
  122. // Convert from paths to Android friendly Parcelable Uri's
  123. for (String file : Log_OC.getLogFileNames())
  124. {
  125. File logFile = new File(mLogPath, file);
  126. if (logFile.exists()) {
  127. uris.add(Uri.fromFile(logFile));
  128. }
  129. }
  130. Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
  131. intent.putExtra(Intent.EXTRA_EMAIL, emailAddress);
  132. String subject = String.format(getString(R.string.log_send_mail_subject), getString(R.string.app_name));
  133. intent.putExtra(Intent.EXTRA_SUBJECT, subject);
  134. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  135. intent.setType(MAIL_ATTACHMENT_TYPE);
  136. intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
  137. try {
  138. startActivity(intent);
  139. } catch (ActivityNotFoundException e) {
  140. Toast.makeText(this, getString(R.string.log_send_no_mail_app), Toast.LENGTH_LONG).show();
  141. Log_OC.i(TAG, "Could not find app for sending log history.");
  142. }
  143. }
  144. /**
  145. *
  146. * Class for loading the log data async
  147. *
  148. */
  149. private class LoadingLogTask extends AsyncTask<String, Void, String> {
  150. private final WeakReference<TextView> textViewReference;
  151. public LoadingLogTask(TextView logTV){
  152. // Use of a WeakReference to ensure the TextView can be garbage collected
  153. textViewReference = new WeakReference<TextView>(logTV);
  154. }
  155. protected String doInBackground(String... args) {
  156. return readLogFile();
  157. }
  158. protected void onPostExecute(String result) {
  159. if (textViewReference != null && result != null) {
  160. final TextView logTV = textViewReference.get();
  161. if (logTV != null) {
  162. mLogText = result;
  163. logTV.setText(mLogText);
  164. dismissLoadingDialog();
  165. }
  166. }
  167. }
  168. /**
  169. * Read and show log file info
  170. */
  171. private String readLogFile() {
  172. String[] logFileName = Log_OC.getLogFileNames();
  173. //Read text from files
  174. StringBuilder text = new StringBuilder();
  175. BufferedReader br = null;
  176. try {
  177. String line;
  178. for (int i = logFileName.length-1; i >= 0; i--) {
  179. File file = new File(mLogPath,logFileName[i]);
  180. if (file.exists()) {
  181. // Check if FileReader is ready
  182. if (new FileReader(file).ready()) {
  183. br = new BufferedReader(new FileReader(file));
  184. while ((line = br.readLine()) != null) {
  185. // Append the log info
  186. text.append(line);
  187. text.append('\n');
  188. }
  189. }
  190. }
  191. }
  192. }
  193. catch (IOException e) {
  194. Log_OC.d(TAG, e.getMessage().toString());
  195. } finally {
  196. if (br != null) {
  197. try {
  198. br.close();
  199. } catch (IOException e) {
  200. // ignore
  201. }
  202. }
  203. }
  204. return text.toString();
  205. }
  206. }
  207. /**
  208. * Show loading dialog
  209. */
  210. public void showLoadingDialog() {
  211. // Construct dialog
  212. LoadingDialog loading = new LoadingDialog(
  213. getResources().getString(R.string.log_progress_dialog_text)
  214. );
  215. FragmentManager fm = getSupportFragmentManager();
  216. FragmentTransaction ft = fm.beginTransaction();
  217. loading.show(ft, DIALOG_WAIT_TAG);
  218. }
  219. /**
  220. * Dismiss loading dialog
  221. */
  222. public void dismissLoadingDialog(){
  223. Fragment frag = getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG);
  224. if (frag != null) {
  225. LoadingDialog loading = (LoadingDialog) frag;
  226. loading.dismiss();
  227. }
  228. }
  229. @Override
  230. protected void onSaveInstanceState(Bundle outState) {
  231. super.onSaveInstanceState(outState);
  232. /// global state
  233. outState.putString(KEY_LOG_TEXT, mLogText);
  234. }
  235. }