Fixed. App crash when setting a path without slash
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / LogHistoryActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
3 *
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.
7 *
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.
12 *
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/>.
15 *
16 */
17
18 package com.owncloud.android.ui.activity;
19
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
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.view.View;
36 import android.view.View.OnClickListener;
37 import android.widget.Button;
38 import android.widget.TextView;
39
40 import com.actionbarsherlock.app.ActionBar;
41 import com.actionbarsherlock.app.SherlockFragmentActivity;
42 import com.actionbarsherlock.view.MenuItem;
43 import com.owncloud.android.R;
44 import com.owncloud.android.lib.common.utils.Log_OC;
45 import com.owncloud.android.ui.dialog.LoadingDialog;
46 import com.owncloud.android.utils.DisplayUtils;
47 import com.owncloud.android.utils.FileStorageUtils;
48
49
50 public class LogHistoryActivity extends SherlockFragmentActivity {
51
52 private static final String MAIL_ATTACHMENT_TYPE = "text/plain";
53
54 private static final String TAG = LogHistoryActivity.class.getSimpleName();
55
56 private static final String DIALOG_WAIT_TAG = "DIALOG_WAIT";
57
58 private String mLogPath = FileStorageUtils.getLogPath();
59 private File logDIR = null;
60
61
62 @Override
63 protected void onCreate(Bundle savedInstanceState) {
64 super.onCreate(savedInstanceState);
65
66 setContentView(R.layout.log_send_file);
67 setTitle(getText(R.string.actionbar_logger));
68 ActionBar actionBar = getSherlock().getActionBar();
69 actionBar.setIcon(DisplayUtils.getSeasonalIconId());
70 actionBar.setDisplayHomeAsUpEnabled(true);
71 Button deleteHistoryButton = (Button) findViewById(R.id.deleteLogHistoryButton);
72 Button sendHistoryButton = (Button) findViewById(R.id.sendLogHistoryButton);
73
74 deleteHistoryButton.setOnClickListener(new OnClickListener() {
75
76 @Override
77 public void onClick(View v) {
78
79 Log_OC.deleteHistoryLogging();
80 finish();
81 }
82 });
83
84 sendHistoryButton.setOnClickListener(new OnClickListener() {
85
86 @Override
87 public void onClick(View v) {
88 sendMail();
89 }
90 });
91
92 if (mLogPath != null) {
93 logDIR = new File(mLogPath);
94 }
95
96 if (logDIR != null && logDIR.isDirectory()) {
97 // Show a dialog while log data is being loaded
98 showLoadingDialog();
99
100 TextView logTV = (TextView) findViewById(R.id.logTV);
101
102 // Start a new thread that will load all the log data
103 LoadingLogTask task = new LoadingLogTask(logTV);
104 task.execute();
105
106 }
107 }
108
109 @Override
110 public boolean onMenuItemSelected(int featureId, MenuItem item) {
111 super.onMenuItemSelected(featureId, item);
112 switch (item.getItemId()) {
113 case android.R.id.home:
114 finish();
115 break;
116 default:
117 return false;
118 }
119 return true;
120 }
121
122
123 /**
124 * Start activity for sending email with logs attached
125 */
126 private void sendMail() {
127
128 String emailAddress;
129 try {
130 Class<?> stringClass = R.string.class;
131 Field mailLoggerField = stringClass.getField("mail_logger");
132 int emailAddressId = (Integer)mailLoggerField.get(null);
133 emailAddress = getString(emailAddressId);
134
135 } catch (Exception e) {
136 emailAddress = "";
137 }
138
139 ArrayList<Uri> uris = new ArrayList<Uri>();
140
141 // Convert from paths to Android friendly Parcelable Uri's
142 for (String file : Log_OC.getLogFileNames())
143 {
144 if (new File(mLogPath + File.separator, file).exists()) {
145 Uri u = Uri.parse("file://" + mLogPath + File.separator + file);
146 uris.add(u);
147 }
148 }
149
150 Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
151
152 // Explicitly only use Gmail to send
153 intent.setClassName("com.google.android.gm","com.google.android.gm.ComposeActivityGmail");
154 intent.putExtra(Intent.EXTRA_EMAIL, new String[]{ emailAddress });
155 intent.putExtra(Intent.EXTRA_SUBJECT, getText(R.string.log_mail_subject));
156 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
157 intent.setType(MAIL_ATTACHMENT_TYPE);
158 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
159
160 if (intent.resolveActivity(getPackageManager()) != null) {
161 startActivity(intent);
162 }
163 }
164
165 /**
166 *
167 * Class for loading the log data async
168 *
169 */
170 private class LoadingLogTask extends AsyncTask<String, Void, String> {
171 private final WeakReference<TextView> textViewReference;
172
173 public LoadingLogTask(TextView logTV){
174 // Use of a WeakReference to ensure the TextView can be garbage collected
175 textViewReference = new WeakReference<TextView>(logTV);
176 }
177
178 protected String doInBackground(String... args) {
179 return readLogFile();
180 }
181
182 protected void onPostExecute(String result) {
183 if (textViewReference != null && result != null) {
184 final TextView logTV = textViewReference.get();
185 if (logTV != null) {
186 logTV.setText(result);
187 dismissLoadingDialog();
188 }
189 }
190 }
191
192 /**
193 * Read and show log file info
194 */
195 private String readLogFile() {
196
197 String[] logFileName = Log_OC.getLogFileNames();
198
199 //Read text from files
200 StringBuilder text = new StringBuilder();
201
202 BufferedReader br = null;
203 try {
204 String line;
205
206 for (int i = logFileName.length-1; i >= 0; i--) {
207 File file = new File(mLogPath,logFileName[i]);
208 if (file.exists()) {
209 // Check if FileReader is ready
210 if (new FileReader(file).ready()) {
211 br = new BufferedReader(new FileReader(file));
212 while ((line = br.readLine()) != null) {
213 // Append the log info
214 text.append(line);
215 text.append('\n');
216 }
217 }
218 }
219 }
220 }
221 catch (IOException e) {
222 Log_OC.d(TAG, e.getMessage().toString());
223
224 } finally {
225 if (br != null) {
226 try {
227 br.close();
228 } catch (IOException e) {
229 // ignore
230 }
231 }
232 }
233
234 return text.toString();
235 }
236 }
237
238 /**
239 * Show loading dialog
240 */
241 public void showLoadingDialog() {
242 // Construct dialog
243 LoadingDialog loading = new LoadingDialog(
244 getResources().getString(R.string.log_progress_dialog_text)
245 );
246 FragmentManager fm = getSupportFragmentManager();
247 FragmentTransaction ft = fm.beginTransaction();
248 loading.show(ft, DIALOG_WAIT_TAG);
249 }
250
251 /**
252 * Dismiss loading dialog
253 */
254 public void dismissLoadingDialog(){
255 Fragment frag = getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG);
256 if (frag != null) {
257 LoadingDialog loading = (LoadingDialog) frag;
258 loading.dismiss();
259 }
260 }
261 }