Home | History | Annotate | Download | only in verifier
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.cts.verifier;
     18 
     19 import android.app.AlertDialog;
     20 import android.content.Context;
     21 import android.os.AsyncTask;
     22 import android.os.Build;
     23 import android.os.Environment;
     24 
     25 import java.io.BufferedOutputStream;
     26 import java.io.File;
     27 import java.io.FileOutputStream;
     28 import java.io.IOException;
     29 import java.text.SimpleDateFormat;
     30 import java.util.Date;
     31 import java.util.Locale;
     32 import java.util.logging.Level;
     33 import java.util.logging.Logger;
     34 import java.util.zip.ZipEntry;
     35 import java.util.zip.ZipOutputStream;
     36 
     37 /**
     38  * Background task to generate a report and save it to external storage.
     39  */
     40 class ReportExporter extends AsyncTask<Void, Void, String> {
     41     protected static final Logger LOG = Logger.getLogger(ReportExporter.class.getName());
     42 
     43     private final Context mContext;
     44     private final TestListAdapter mAdapter;
     45 
     46     ReportExporter(Context context, TestListAdapter adapter) {
     47         this.mContext = context;
     48         this.mAdapter = adapter;
     49     }
     50 
     51     @Override
     52     protected String doInBackground(Void... params) {
     53         if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
     54             LOG.log(Level.WARNING, "External storage is not writable.");
     55             return mContext.getString(R.string.no_storage);
     56         }
     57         byte[] contents;
     58         try {
     59             TestResultsReport report = new TestResultsReport(mContext, mAdapter);
     60             contents = report.getContents().getBytes();
     61         } catch (Exception e) {
     62             LOG.log(Level.WARNING, "Couldn't create test results report", e);
     63             return mContext.getString(R.string.test_results_error);
     64         }
     65         File reportPath = new File(Environment.getExternalStorageDirectory(), "ctsVerifierReports");
     66         reportPath.mkdirs();
     67 
     68         String baseName = getReportBaseName();
     69         File reportFile = new File(reportPath, baseName + ".zip");
     70         ZipOutputStream out = null;
     71         try {
     72             out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(reportFile)));
     73             ZipEntry entry = new ZipEntry(baseName + ".xml");
     74             out.putNextEntry(entry);
     75             out.write(contents);
     76         } catch (IOException e) {
     77             LOG.log(Level.WARNING, "I/O exception writing report to storage.", e);
     78             return mContext.getString(R.string.no_storage);
     79         } finally {
     80             try {
     81                 if (out != null) {
     82                     out.close();
     83                 }
     84             } catch (IOException e) {
     85                 LOG.log(Level.WARNING, "I/O exception closing report.", e);
     86             }
     87         }
     88 
     89         return mContext.getString(R.string.report_saved, reportFile.getPath());
     90     }
     91 
     92     private String getReportBaseName() {
     93         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd-HH.mm.ss", Locale.ENGLISH);
     94         String date = dateFormat.format(new Date());
     95         return "ctsVerifierReport"
     96                 + "-" + date
     97                 + "-" + Build.MANUFACTURER
     98                 + "-" + Build.PRODUCT
     99                 + "-" + Build.DEVICE
    100                 + "-" + Build.ID;
    101     }
    102 
    103     @Override
    104     protected void onPostExecute(String result) {
    105         new AlertDialog.Builder(mContext)
    106                 .setMessage(result)
    107                 .setPositiveButton(android.R.string.ok, null)
    108                 .show();
    109     }
    110 }
    111