am eae29cdf: am 36b9437a: am fbcdba23: Merge "Export CTS Verifier reports to the SD card." into gingerbread

* commit 'eae29cdfa98ab9119b79ead021699dbaab69d80f':
  Export CTS Verifier reports to the SD card.
diff --git a/apps/CtsVerifier/res/menu/test_list_menu.xml b/apps/CtsVerifier/res/menu/test_list_menu.xml
index 67c626c..495e36f 100644
--- a/apps/CtsVerifier/res/menu/test_list_menu.xml
+++ b/apps/CtsVerifier/res/menu/test_list_menu.xml
@@ -6,7 +6,7 @@
     <item android:id="@+id/copy"
           android:icon="@android:drawable/ic_menu_upload"
           android:title="@string/copy" />
-    <item android:id="@+id/share"
+    <item android:id="@+id/export"
           android:icon="@android:drawable/ic_menu_share"
-          android:title="@string/share" />
+          android:title="@string/export" />
 </menu>
\ No newline at end of file
diff --git a/apps/CtsVerifier/res/values/strings.xml b/apps/CtsVerifier/res/values/strings.xml
index 7291d7c..5242a55 100644
--- a/apps/CtsVerifier/res/values/strings.xml
+++ b/apps/CtsVerifier/res/values/strings.xml
@@ -40,9 +40,10 @@
     <string name="test_results_cleared">Test results cleared.</string>
     <string name="copy">Copy</string>
     <string name="test_results_copied">Test results copied to clipboard.</string>
-    <string name="share">Share</string>
-    <string name="share_test_results">Share Test Results</string>
     <string name="test_results_error">Couldn\'t create test results report.</string>
+    <string name="export">Export</string>
+    <string name="no_storage">Cannot save report to external storage, see log for details.</string>
+    <string name="report_saved">Report saved to: %s</string>
 
     <!-- Strings for Device Administration tests -->
     <string name="da_policy_serialization_test">Policy Serialization Test</string>
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/ReportExporter.java b/apps/CtsVerifier/src/com/android/cts/verifier/ReportExporter.java
new file mode 100644
index 0000000..f7db56d
--- /dev/null
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/ReportExporter.java
@@ -0,0 +1,91 @@
+/*
+ * 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();
+    }
+}
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/TestListActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/TestListActivity.java
index fe41583..bc7a2b0 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/TestListActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/TestListActivity.java
@@ -100,8 +100,8 @@
                 handleCopyItemSelected();
                 return true;
 
-            case R.id.share:
-                handleShareItemSelected();
+            case R.id.export:
+                handleExportItemSelected();
                 return true;
 
             default:
@@ -119,25 +119,15 @@
             TestResultsReport report = new TestResultsReport(this, mAdapter);
             ClipboardManager clipboardManager = (ClipboardManager)
                     getSystemService(CLIPBOARD_SERVICE);
-            clipboardManager.setText(report.getBody());
+            clipboardManager.setText(report.getContents());
             Toast.makeText(this, R.string.test_results_copied, Toast.LENGTH_SHORT).show();
         } catch (IOException e) {
             Toast.makeText(this, R.string.test_results_error, Toast.LENGTH_SHORT).show();
-            Log.e(TAG, "Coudn't copy test results report", e);
+            Log.e(TAG, "Couldn't copy test results report", e);
         }
     }
 
-    private void handleShareItemSelected() {
-        try {
-            Intent target = new Intent(Intent.ACTION_SEND);
-            TestResultsReport report = new TestResultsReport(this, mAdapter);
-            target.setType(report.getType());
-            target.putExtra(Intent.EXTRA_SUBJECT, report.getSubject());
-            target.putExtra(Intent.EXTRA_TEXT, report.getBody());
-            startActivity(Intent.createChooser(target, getString(R.string.share_test_results)));
-        } catch (IOException e) {
-            Toast.makeText(this, R.string.test_results_error, Toast.LENGTH_SHORT).show();
-            Log.e(TAG, "Coudn't share test results report", e);
-        }
+    private void handleExportItemSelected() {
+        new ReportExporter(this, mAdapter).execute();
     }
 }
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/TestResultsReport.java b/apps/CtsVerifier/src/com/android/cts/verifier/TestResultsReport.java
index c7af68a..37d4819 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/TestResultsReport.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/TestResultsReport.java
@@ -77,17 +77,7 @@
         this.mAdapter = adapter;
     }
 
-    String getType() {
-        return "application/xml";
-    }
-
-    String getSubject() {
-        return mContext.getString(R.string.subject_header,
-                Version.getVersionName(mContext),
-                Build.FINGERPRINT);
-    }
-
-    String getBody() throws IllegalArgumentException, IllegalStateException, IOException {
+    String getContents() throws IllegalArgumentException, IllegalStateException, IOException {
         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 
         XmlSerializer xml = Xml.newSerializer();
@@ -106,7 +96,16 @@
 
         xml.startTag(null, DEVICE_INFO_TAG);
         xml.startTag(null, BUILD_INFO_TAG);
+        xml.attribute(null, "board", Build.BOARD);
+        xml.attribute(null, "brand", Build.BRAND);
+        xml.attribute(null, "device", Build.DEVICE);
+        xml.attribute(null, "display", Build.DISPLAY);
         xml.attribute(null, "fingerprint", Build.FINGERPRINT);
+        xml.attribute(null, "id", Build.ID);
+        xml.attribute(null, "model", Build.MODEL);
+        xml.attribute(null, "product", Build.PRODUCT);
+        xml.attribute(null, "release", Build.VERSION.RELEASE);
+        xml.attribute(null, "sdk", Build.VERSION.SDK);
         xml.endTag(null, BUILD_INFO_TAG);
         xml.endTag(null, DEVICE_INFO_TAG);