Merge "Make Garage Mode more robust in detecting idle-mode jobs" into qt-qpr1-dev
diff --git a/service/src/com/android/car/CarMediaService.java b/service/src/com/android/car/CarMediaService.java
index ee6c643..9f57179 100644
--- a/service/src/com/android/car/CarMediaService.java
+++ b/service/src/com/android/car/CarMediaService.java
@@ -530,8 +530,6 @@
         mActiveUserMediaController = null;
         mPreviousMediaComponent = mPrimaryMediaComponent;
         mPrimaryMediaComponent = componentName;
-        updateActiveMediaController(mMediaSessionManager
-                .getActiveSessionsForUser(null, ActivityManager.getCurrentUser()));
 
         if (mPrimaryMediaComponent != null && !TextUtils.isEmpty(
                 mPrimaryMediaComponent.flattenToString())) {
@@ -546,8 +544,11 @@
         startMediaConnectorService(shouldStartPlayback(mPlayOnMediaSourceChangedConfig),
                 new UserHandle(mCurrentUser));
         // Reset current playback state for the new source, in the case that the app is in an error
-        // state (e.g. not signed in). This state will be updated when the app begins playback.
+        // state (e.g. not signed in). This state will be updated from the app callback registered
+        // below, to make sure mCurrentPlaybackState reflects the current source only.
         mCurrentPlaybackState = PlaybackState.STATE_NONE;
+        updateActiveMediaController(mMediaSessionManager
+                .getActiveSessionsForUser(null, ActivityManager.getCurrentUser()));
     }
 
     private void notifyListeners() {
diff --git a/tests/BugReportApp/README.md b/tests/BugReportApp/README.md
index 2f5b4b9..476efe4 100644
--- a/tests/BugReportApp/README.md
+++ b/tests/BugReportApp/README.md
@@ -66,23 +66,28 @@
 
 ### Manually testing the app using the test script
 
-BugReportApp comes with `utils/bugreport_app_tester.py` script that automates
-many of the BugReportApp testing process. Please follow these instructions
-to test the app:
+Please follow these instructions to test the app:
 
 1. Connect the device to your computer.
 2. Make sure the device has Internet.
-3. Run the script: `$ python bugreport_app_tester.py`
-   * The script works on python 2.7 and above.
-   * If multiple devices connected, see the usage
-     `$ python bugreport_app_tester.py --help`.
-   * Warning: the script will delete all the bug reports on the device.
-4. Script might take up to 10 minutes to finish.
+3. Start BugReport app; here is the list of possible ways to start:
+   * Long press HVAC (A/C) icon
+   * Long press Rear Defrost hardware button (hold up to 6 seconds)
+   * Long press notification icon
+   * Open BugReport app from launcher menu or external apps menu; and click Start Bug Report button.
+   * Using adb, see above instructions under `Starting bugreporting`.
+4. Bug report collection might take up to 7 minutes to finish.
    * It might fail to upload bugreport when time/time-zone is invalid.
    * In rare cases it might not upload the bugreport, depending Android's
      task scheduling rules.
-5. Please manually verify the script's results.
+   * You should see progress bar in notification shade.
+5. Pull collected zip files from the device:
+   * `adb pull /data/user/0/com.google.android.car.bugreport/bug_reports_pending/`
 6. Please manually verify bug report contents.
    * Images - the should contain screenshots of all the physical displays.
    * Audio files - they should contain the audio message you recorded.
    * Dumpstate (bugreport) - it should contain logs and other information.
+7. In any case if bugreport app is not properly functioning, please take adb bugreport and share
+   the zip file with developers: `$ adb bugreport`.
+
+NOTE: `utils/bugreport_app_tester.py` is deprecated.
diff --git a/tests/BugReportApp/src/com/google/android/car/bugreport/BugInfoAdapter.java b/tests/BugReportApp/src/com/google/android/car/bugreport/BugInfoAdapter.java
index f63d937..3d01729 100644
--- a/tests/BugReportApp/src/com/google/android/car/bugreport/BugInfoAdapter.java
+++ b/tests/BugReportApp/src/com/google/android/car/bugreport/BugInfoAdapter.java
@@ -15,6 +15,7 @@
  */
 package com.google.android.car.bugreport;
 
+import android.os.Build;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -129,9 +130,8 @@
             holder.mMoveButton.setEnabled(false);
             holder.mMoveButton.setVisibility(View.GONE);
         }
-        // Always enable upload to GCS button, because the app is enabled only for userdebug,
-        // and sometimes Config might not be properly set.
-        if (enableUserActionButtons) {
+        // Enable the upload button only for userdebug/eng builds.
+        if (enableUserActionButtons && Build.IS_DEBUGGABLE) {
             holder.mUploadButton.setText(R.string.bugreport_upload_gcs_button_text);
             holder.mUploadButton.setEnabled(true);
             holder.mUploadButton.setVisibility(View.VISIBLE);
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 a66ce0f..a5aee0f 100644
--- a/tests/BugReportApp/src/com/google/android/car/bugreport/BugReportActivity.java
+++ b/tests/BugReportApp/src/com/google/android/car/bugreport/BugReportActivity.java
@@ -103,7 +103,10 @@
     private Button mSubmitButton;
 
     private boolean mBound;
+    /** Audio message recording process started (including waiting for permission). */
     private boolean mAudioRecordingStarted;
+    /** Audio recording using MIC is running (permission given). */
+    private boolean mAudioRecordingIsRunning;
     private boolean mIsNewBugReport;
     private boolean mIsOnActivityStartedWithBugReportServiceBoundCalled;
     private boolean mIsSubmitButtonClicked;
@@ -199,6 +202,7 @@
         }
         // Reset variables for the next onStart().
         mAudioRecordingStarted = false;
+        mAudioRecordingIsRunning = false;
         mIsSubmitButtonClicked = false;
         mIsOnActivityStartedWithBugReportServiceBoundCalled = false;
         mMetaBugReport = null;
@@ -411,7 +415,10 @@
      * Cancels bugreporting by stopping audio recording and deleting temp files.
      */
     private void cancelAudioMessageRecording() {
-        if (!mAudioRecordingStarted) {
+        // If audio recording is not running, most likely there were permission issues,
+        // so leave the bugreport as is without cancelling it.
+        if (!mAudioRecordingIsRunning) {
+            Log.w(TAG, "Cannot cancel, audio recording is not running.");
             return;
         }
         stopAudioRecording();
@@ -427,6 +434,7 @@
                 this, mMetaBugReport, Status.STATUS_USER_CANCELLED, "");
         Log.i(TAG, "Bug report " + mMetaBugReport.getTimestamp() + " is cancelled");
         mAudioRecordingStarted = false;
+        mAudioRecordingIsRunning = false;
     }
 
     private void buttonCancelClick(View view) {
@@ -507,8 +515,13 @@
                 + Arrays.toString(permissions);
         Log.w(TAG, text);
         Toast.makeText(this, text, Toast.LENGTH_LONG).show();
-        BugStorageUtils.setBugReportStatus(this, mMetaBugReport,
-                Status.STATUS_USER_CANCELLED, text);
+        if (mIsNewBugReport) {
+            BugStorageUtils.setBugReportStatus(this, mMetaBugReport,
+                    Status.STATUS_USER_CANCELLED, text);
+        } else {
+            BugStorageUtils.setBugReportStatus(this, mMetaBugReport,
+                    Status.STATUS_AUDIO_PENDING, text);
+        }
         finish();
     }
 
@@ -550,6 +563,7 @@
 
         mRecorder.start();
         mVoiceRecordingView.setRecorder(mRecorder);
+        mAudioRecordingIsRunning = true;
 
         // Messages with token mRecorder are cleared when the activity finishes or recording stops.
         mHandler.postDelayed(() -> {
diff --git a/tests/BugReportApp/utils/bugreport_app_tester.py b/tests/BugReportApp/utils/bugreport_app_tester.py
index baf8ada..e86f827 100755
--- a/tests/BugReportApp/utils/bugreport_app_tester.py
+++ b/tests/BugReportApp/utils/bugreport_app_tester.py
@@ -15,6 +15,9 @@
 # limitations under the License.
 """Semi-automatic AAE BugReport App test utility.
 
+WARNING: the script is deprecated, because BugReportApp contains complicated logic of statuses,
+         and the script requires many changes to test them.
+
 It automates most of mundane steps when testing AAE BugReport app, but still
 requires manual input from a tester.
 
@@ -349,7 +352,7 @@
             _bugreport_status_to_str(meta_bugreport.status))
 
   def _wait_for_bugreport_to_complete(self, bugreport_id):
-    """Waits until status changes to WRITE_PENDING.
+    """Waits until status changes to UPLOAD_PENDING.
 
     It means dumpstate (bugreport) is completed (or failed).
 
@@ -370,8 +373,7 @@
   def _wait_for_bugreport_to_upload(self, bugreport_id):
     """Waits bugreport to be uploaded and returns None if succeeds.
 
-    NOTE: If "android.car.bugreport.disableautoupload" system property is set,
-    the App will not upload.
+    NOTE: Depending on configuration BugReportApp will not upload bugreports by default.
     """
     print('\nWaiting for the bug report to be uploaded.')
     err_msg = self._wait_for_bugreport_status_to_change_to(