LogHistoryActivity.java 9.1 KB

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