Add show-bugreports button when car is parked. am: 741d06f633
am: 88810a9c95

Change-Id: Ibf3801512f9299ec5f720f35550272c2fc4a28e0
diff --git a/tests/BugReportApp/AndroidManifest.xml b/tests/BugReportApp/AndroidManifest.xml
index 3356151..ddb312e 100644
--- a/tests/BugReportApp/AndroidManifest.xml
+++ b/tests/BugReportApp/AndroidManifest.xml
@@ -19,6 +19,7 @@
           android:versionCode="7"
           android:versionName="1.5.1">
 
+    <uses-permission android:name="android.car.permission.CAR_DRIVING_STATE"/>
     <uses-permission android:name="android.permission.INTERNET"/>
     <uses-permission android:name="android.permission.READ_LOGS"/>
     <uses-permission android:name="android.permission.READ_FRAME_BUFFER"/>
diff --git a/tests/BugReportApp/res/layout/bug_report_activity.xml b/tests/BugReportApp/res/layout/bug_report_activity.xml
index f7e5888..d5dce22 100644
--- a/tests/BugReportApp/res/layout/bug_report_activity.xml
+++ b/tests/BugReportApp/res/layout/bug_report_activity.xml
@@ -65,6 +65,15 @@
             android:layout_marginTop="@dimen/bug_report_button_margin_top"
             android:padding="@dimen/bug_report_secondary_button_padding"
             android:text="@string/bugreport_dialog_cancel"/>
+        <Button
+            android:id="@+id/button_show_bugreports"
+            style="@style/standard_button"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:layout_marginTop="@dimen/bug_report_button_margin_top"
+            android:padding="@dimen/bug_report_small_button_padding"
+            android:visibility="gone"
+            android:text="@string/bugreport_dialog_show_bugreports"/>
     </LinearLayout>
 
     <LinearLayout
diff --git a/tests/BugReportApp/res/values/dimens.xml b/tests/BugReportApp/res/values/dimens.xml
index e6c1162..ebddcfe 100644
--- a/tests/BugReportApp/res/values/dimens.xml
+++ b/tests/BugReportApp/res/values/dimens.xml
@@ -25,8 +25,9 @@
 
     <!-- Buttons dimensions -->
     <dimen name="bug_report_button_margin_top">14dp</dimen>
-    <dimen name="bug_report_primary_button_padding">58dp</dimen>
-    <dimen name="bug_report_secondary_button_padding">32dp</dimen>
+    <dimen name="bug_report_primary_button_padding">52dp</dimen>
+    <dimen name="bug_report_secondary_button_padding">30dp</dimen>
+    <dimen name="bug_report_small_button_padding">16dp</dimen>
 
     <!-- ProgressBar dimensions -->
     <dimen name="bug_report_progress_bar_margin_top">32dp</dimen>
diff --git a/tests/BugReportApp/res/values/strings.xml b/tests/BugReportApp/res/values/strings.xml
index 4fe2500..491bbc2 100644
--- a/tests/BugReportApp/res/values/strings.xml
+++ b/tests/BugReportApp/res/values/strings.xml
@@ -22,6 +22,7 @@
 
     <string name="bugreport_dialog_submit" translatable="false">Submit</string>
     <string name="bugreport_dialog_cancel" translatable="false">Cancel</string>
+    <string name="bugreport_dialog_show_bugreports" translatable="false">Show Bug Reports</string>
     <string name="bugreport_dialog_close" translatable="false">Close</string>
     <string name="bugreport_dialog_title" translatable="false">Speak &amp; Describe The Issue</string>
     <string name="bugreport_dialog_recording_finished" translatable="false">Recording finished</string>
diff --git a/tests/BugReportApp/src/com/google/android/car/bugreport/BugReportActivity.java b/tests/BugReportApp/src/com/google/android/car/bugreport/BugReportActivity.java
index c0b9da3..4ac699b 100644
--- a/tests/BugReportApp/src/com/google/android/car/bugreport/BugReportActivity.java
+++ b/tests/BugReportApp/src/com/google/android/car/bugreport/BugReportActivity.java
@@ -20,6 +20,10 @@
 
 import android.Manifest;
 import android.app.Activity;
+import android.car.Car;
+import android.car.CarNotConnectedException;
+import android.car.drivingstate.CarDrivingStateEvent;
+import android.car.drivingstate.CarDrivingStateManager;
 import android.content.ComponentName;
 import android.content.Intent;
 import android.content.ServiceConnection;
@@ -73,6 +77,7 @@
     private View mVoiceRecordingFinishedView;
     private View mSubmitBugReportLayout;
     private View mInProgressLayout;
+    private View mShowBugReportsButton;
 
     private boolean mBound;
     private boolean mAudioRecordingStarted;
@@ -80,6 +85,8 @@
     private BugReportService mService;
     private MediaRecorder mRecorder;
     private MetaBugReport mMetaBugReport;
+    private Car mCar;
+    private CarDrivingStateManager mDrivingStateManager;
 
     /** Defines callbacks for service binding, passed to bindService() */
     private ServiceConnection mConnection = new ServiceConnection() {
@@ -98,6 +105,24 @@
         }
     };
 
+    private final ServiceConnection mServiceConnection = new ServiceConnection() {
+        @Override
+        public void onServiceConnected(ComponentName name, IBinder service) {
+            try {
+                mDrivingStateManager = (CarDrivingStateManager) mCar.getCarManager(
+                        Car.CAR_DRIVING_STATE_SERVICE);
+                mDrivingStateManager.registerListener(
+                        BugReportActivity.this::onCarDrivingStateChanged);
+            } catch (CarNotConnectedException e) {
+                Log.w(TAG, "Failed to get CarDrivingStateManager.", e);
+            }
+        }
+
+        @Override
+        public void onServiceDisconnected(ComponentName name) {
+        }
+    };
+
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -112,11 +137,16 @@
         mVoiceRecordingFinishedView = findViewById(R.id.voice_recording_finished_text_view);
         mSubmitBugReportLayout = findViewById(R.id.submit_bug_report_layout);
         mInProgressLayout = findViewById(R.id.in_progress_layout);
+        mShowBugReportsButton = findViewById(R.id.button_show_bugreports);
 
+        mShowBugReportsButton.setOnClickListener(this::buttonShowBugReportsClick);
         findViewById(R.id.button_submit).setOnClickListener(this::buttonSubmitClick);
         findViewById(R.id.button_cancel).setOnClickListener(this::buttonCancelClick);
         findViewById(R.id.button_close).setOnClickListener(this::buttonCancelClick);
 
+        mCar = Car.createCar(this, mServiceConnection);
+        mCar.connect();
+
         // Bind to BugReportService.
         Intent intent = new Intent(this, BugReportService.class);
         bindService(intent, mConnection, BIND_AUTO_CREATE);
@@ -152,6 +182,18 @@
             unbindService(mConnection);
             mBound = false;
         }
+        if (mCar != null && mCar.isConnected()) {
+            mCar.disconnect();
+            mCar = null;
+        }
+    }
+
+    private void onCarDrivingStateChanged(CarDrivingStateEvent event) {
+        if (event.eventValue == CarDrivingStateEvent.DRIVING_STATE_PARKED) {
+            mShowBugReportsButton.setVisibility(View.VISIBLE);
+        } else {
+            mShowBugReportsButton.setVisibility(View.GONE);
+        }
     }
 
     private void onProgressChanged(float progress) {
@@ -180,6 +222,16 @@
             mVoiceRecordingFinishedView.setVisibility(View.VISIBLE);
             mVoiceRecordingView.setVisibility(View.GONE);
         }
+        // NOTE: mShowBugReportsButton visibility is also handled in #onCarDrivingStateChanged().
+        mShowBugReportsButton.setVisibility(View.GONE);
+        if (mDrivingStateManager != null) {
+            try {
+                // Call onCarDrivingStateChanged(), because it's not called when Car is connected.
+                onCarDrivingStateChanged(mDrivingStateManager.getCurrentCarDrivingState());
+            } catch (CarNotConnectedException e) {
+                Log.e(TAG, "Failed to get current driving state.", e);
+            }
+        }
     }
 
     /**
@@ -239,6 +291,17 @@
         finish();
     }
 
+    /**
+     * Starts {@link BugReportInfoActivity} and finishes current activity, so it won't be running
+     * in the background and closing {@link BugReportInfoActivity} will not open it again.
+     */
+    private void buttonShowBugReportsClick(View view) {
+        Intent intent = new Intent(this, BugReportInfoActivity.class);
+        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+        startActivity(intent);
+        finish();
+    }
+
     private void startBugReportingInService() {
         stopAudioRecording();
         Bundle bundle = new Bundle();
@@ -288,8 +351,7 @@
     private void startRecordingWithPermission() {
         File recordingFile = FileUtils.getFileWithSuffix(this, mMetaBugReport.getTimestamp(),
                 "-message.3gp");
-        Log.d(TAG, "start voice recording: " + recordingFile + ". activityObjectId"
-                + System.identityHashCode(this));
+        Log.i(TAG, "Started voice recording, and saving audio to " + recordingFile);
 
         mRecorder = new MediaRecorder();
         mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
@@ -367,7 +429,7 @@
         @Override
         protected Void doInBackground(File... files) {
             for (File file : files) {
-                Log.d(TAG, "Deleting " + file.getAbsolutePath());
+                Log.i(TAG, "Deleting " + file.getAbsolutePath());
                 FileUtils.deleteDirectory(file);
             }
             return null;