blob: f7db56d66bef19649c7c31cd8cb5facdce21a207 [file] [log] [blame]
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.cts.verifier;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Environment;
import android.widget.Toast;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* Background task to generate a report and save it to external storage.
*/
class ReportExporter extends AsyncTask<Void, Void, String> {
protected static final Logger LOG = Logger.getLogger(ReportExporter.class.getName());
private final Context mContext;
private final TestListAdapter mAdapter;
ReportExporter(Context context, TestListAdapter adapter) {
this.mContext = context;
this.mAdapter = adapter;
}
@Override
protected String doInBackground(Void... params) {
if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
LOG.log(Level.WARNING, "External storage is not writable.");
return mContext.getString(R.string.no_storage);
}
byte[] contents;
try {
TestResultsReport report = new TestResultsReport(mContext, mAdapter);
contents = report.getContents().getBytes();
} catch (Exception e) {
LOG.log(Level.WARNING, "Couldn't create test results report", e);
return mContext.getString(R.string.test_results_error);
}
File reportPath = new File(Environment.getExternalStorageDirectory(), "ctsVerifierReports");
reportPath.mkdirs();
File reportFile = new File(reportPath,
"ctsVerifierReport-" + System.currentTimeMillis() + ".zip");
ZipOutputStream out = null;
try {
out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(reportFile)));
ZipEntry entry = new ZipEntry("ctsVerifierReport.xml");
out.putNextEntry(entry);
out.write(contents);
} catch (IOException e) {
LOG.log(Level.WARNING, "I/O exception writing report to storage.", e);
return mContext.getString(R.string.no_storage);
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
LOG.log(Level.WARNING, "I/O exception closing report.", e);
}
}
return mContext.getString(R.string.report_saved, reportFile.getPath());
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(mContext, result, Toast.LENGTH_LONG).show();
}
}