1 /* 2 * Copyright (C) 2016 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 package com.android.cts.verifier.sensors.sixdof.Utils; 17 18 import com.android.cts.verifier.R; 19 20 import android.content.Context; 21 import android.os.AsyncTask; 22 import android.os.Build; 23 import android.os.Environment; 24 import android.app.AlertDialog; 25 26 import java.io.BufferedOutputStream; 27 import java.io.File; 28 import java.io.FileOutputStream; 29 import java.io.IOException; 30 import java.text.SimpleDateFormat; 31 import java.util.Date; 32 import java.util.Locale; 33 import java.util.logging.Level; 34 import java.util.logging.Logger; 35 import java.util.zip.ZipEntry; 36 import java.util.zip.ZipOutputStream; 37 38 /** 39 * Exports the xml report to a file. 40 */ 41 public class ReportExporter extends AsyncTask<Void, Void, String> { 42 public static final String REPORT_DIRECTORY = "6dofVerifierReports"; 43 protected static final Logger LOG = Logger.getLogger(ReportExporter.class.getName()); 44 45 private final Context mContext; 46 private final String mTestReport; 47 48 public ReportExporter(Context context, String report) { 49 this.mContext = context; 50 this.mTestReport = report; 51 } 52 53 @Override 54 protected String doInBackground(Void... params) { 55 if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { 56 LOG.log(Level.WARNING, "External storage is not writable."); 57 return mContext.getString(R.string.no_storage); 58 } 59 byte[] contents = mTestReport.getBytes(); 60 String storagePath = System.getenv("EXTERNAL_STORAGE"); 61 if (storagePath.length() == 0 || storagePath.equals("")) { 62 storagePath = Environment.getExternalStorageDirectory().getPath(); 63 } 64 File reportPath = new File(storagePath, REPORT_DIRECTORY); 65 reportPath.mkdirs(); 66 67 String baseName = getReportBaseName(); 68 File reportFile = new File(reportPath, baseName + ".zip"); 69 ZipOutputStream out = null; 70 try { 71 out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(reportFile))); 72 ZipEntry entry = new ZipEntry(baseName + ".xml"); 73 out.putNextEntry(entry); 74 out.write(contents); 75 } catch (IOException e) { 76 LOG.log(Level.WARNING, "I/O exception writing report to storage.", e); 77 return mContext.getString(R.string.no_storage); 78 } finally { 79 try { 80 if (out != null) { 81 out.close(); 82 } 83 } catch (IOException e) { 84 LOG.log(Level.WARNING, "I/O exception closing report.", e); 85 } 86 } 87 88 return mContext.getString(R.string.report_saved, reportFile.getPath()); 89 } 90 91 private String getReportBaseName() { 92 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd-HH.mm.ss", Locale.ENGLISH); 93 String date = dateFormat.format(new Date()); 94 return "6dofVerifierReport" 95 + "-" + date 96 + "-" + Build.MANUFACTURER 97 + "-" + Build.PRODUCT 98 + "-" + Build.DEVICE 99 + "-" + Build.ID; 100 } 101 102 @Override 103 protected void onPostExecute(String result) { 104 new AlertDialog.Builder(mContext) 105 .setMessage(result) 106 .setPositiveButton(android.R.string.ok, null) 107 .show(); 108 } 109 } 110