Make test apps buildable in Android Studio

- Use AOSP links
- Add git workflow & instructions in readme.md
- Add build.gradle for RotaryPlayground
- Fix build errors in TestMediaApp
- Add git_clone_projects.sh & go_rotary.sh

Bug: 161492279
Test: Build and Run both sucessfully on Android Studio and Emulator
Change-Id: Ia73641f770044518a87f7487f52c734ec46f1a2b
diff --git a/RotaryPlayground/build.gradle b/RotaryPlayground/build.gradle
new file mode 100644
index 0000000..fa8d79c
--- /dev/null
+++ b/RotaryPlayground/build.gradle
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2020 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.
+ */
+
+apply plugin: 'com.android.application'
+
+android {
+    compileSdkVersion 30
+    defaultConfig {
+        applicationId "com.android.car.rotaryplayground"
+        minSdkVersion 30
+        targetSdkVersion 30
+        versionCode 1
+        versionName "1.0"
+    }
+    compileOptions {
+        sourceCompatibility JavaVersion.VERSION_1_8
+        targetCompatibility JavaVersion.VERSION_1_8
+    }
+    lintOptions {
+        abortOnError false
+    }
+    buildTypes {
+        release {
+            minifyEnabled false
+        }
+    }
+
+    sourceSets {
+        main {
+            manifest.srcFile 'AndroidManifest.xml'
+            java.srcDirs = ['src']
+            resources.srcDirs = ['src']
+            aidl.srcDirs = ['src']
+            renderscript.srcDirs = ['src']
+            res.srcDirs = ['res']
+            assets.srcDirs = ['assets']
+        }
+    }
+}
+
+dependencies {
+    implementation "androidx.preference:preference:1.1.1"
+    implementation "androidx.constraintlayout:constraintlayout:2.0.0-beta8"
+    implementation project(":car-ui-lib")
+}
diff --git a/TestMediaApp/build.gradle b/TestMediaApp/build.gradle
index 79fd66d..6711d6d 100644
--- a/TestMediaApp/build.gradle
+++ b/TestMediaApp/build.gradle
@@ -18,11 +18,11 @@
 apply plugin: 'com.android.application'
 
 android {
-    compileSdkVersion 28
+    compileSdkVersion 30
     defaultConfig {
         applicationId "com.android.car.media.testmediaapp"
-        minSdkVersion 21
-        targetSdkVersion 28
+        minSdkVersion 28
+        targetSdkVersion 30
         versionCode 1
         versionName "1.0"
     }
diff --git a/TestMediaApp/src/com/android/car/media/testmediaapp/phone/TmaLauncherActivity.java b/TestMediaApp/src/com/android/car/media/testmediaapp/phone/TmaLauncherActivity.java
index 84f481f..014e81e 100644
--- a/TestMediaApp/src/com/android/car/media/testmediaapp/phone/TmaLauncherActivity.java
+++ b/TestMediaApp/src/com/android/car/media/testmediaapp/phone/TmaLauncherActivity.java
@@ -52,13 +52,21 @@
                     // Get the token for the MediaSession
                     MediaSessionCompat.Token token = mediaBrowser.getSessionToken();
 
-                    // Create a MediaControllerCompat
-                    MediaControllerCompat controller =
-                            new MediaControllerCompat(TmaLauncherActivity.this, token);
-
-                    // Save the controller
-                    MediaControllerCompat.setMediaController(
-                            TmaLauncherActivity.this, controller);
+                    try {
+                        // Create a MediaControllerCompat
+                        MediaControllerCompat controller =
+                                new MediaControllerCompat(TmaLauncherActivity.this, token);
+                        // Save the controller
+                        MediaControllerCompat.setMediaController(
+                                TmaLauncherActivity.this, controller);
+                    } catch (Exception ex) {
+                        // ToDo: b/166328624 Workaround for an Android Studio Build error:
+                        //          unreported exception RemoteException
+                        //       Whereas as an Android Soong Build error:
+                        //          RemoteException is never thrown
+                        Log.e(TAG, "Failed to create MediaControllerCompat", ex);
+                        return;
+                    }
                 }
             };
 
diff --git a/build.gradle b/build.gradle
index df49ba3..8ea89fa 100644
--- a/build.gradle
+++ b/build.gradle
@@ -22,7 +22,7 @@
         jcenter()
     }
     dependencies {
-        classpath 'com.android.tools.build:gradle:3.5.0'
+        classpath 'com.android.tools.build:gradle:4.0.0'
 
         // NOTE: Do not place your application dependencies here; they belong
         // in the individual module build.gradle files
diff --git a/gradle.properties b/gradle.properties
new file mode 100644
index 0000000..5bac8ac
--- /dev/null
+++ b/gradle.properties
@@ -0,0 +1 @@
+android.useAndroidX=true
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
index ce751bb..e5a0e03 100644
--- a/gradle/wrapper/gradle-wrapper.properties
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
-#Thu Sep 26 14:52:51 PDT 2019
+#Mon Aug 24 13:36:33 PDT 2020
 distributionBase=GRADLE_USER_HOME
 distributionPath=wrapper/dists
 zipStoreBase=GRADLE_USER_HOME
 zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
diff --git a/readme.md b/readme.md
index 94ad5ac..6871ff1 100644
--- a/readme.md
+++ b/readme.md
@@ -1,16 +1,37 @@
 # Car test apps
 
-This repository is only for car test applications.
+This repository is only for car test applications. They can be unbundled from Android devices. 
+
+## Prerequisites
+
+* You need to build or have a [car-ui-lib](https://cs.android.com/android/platform/superproject/+/android-10.0.0_r30:packages/apps/Car/libs/car-ui-lib/)
+aar ready first if to build test apps on Android Studio.
+* android-10.0.0_r30 is a release tag placeholder in this doc, you should replace the one you need.
 
 ## Building
 
-If you are not contributing to the repo, you can clone the repo via `git clone sso://googleplex-android/platform/packages/apps/Car/tests --branch pi-car-dev --single-branch`. Otherwise, see [workstation setup](#workstation-setup).
+1. There are 3 ways to get the source. Pick one works better for you.
+  * A: Download [tgz](https://android.googlesource.com/platform/packages/apps/Car/tests/+archive/refs/tags/android-10.0.0_r30.tar.gz)
+from the source if no plan to contribue.
+  * B: Repo workflow, see [workstation setup](#workstation-setup).
+  * C: Git workflow, e.g.
+```
+git clone -b $BRANCH https://android.googlesource.com/platform/packages/apps/Car/tests
+cd tests
+f=`git rev-parse --git-dir`/hooks/commit-msg ; mkdir -p $(dirname $f) ; curl -Lo $f https://gerrit-review.googlesource.com/tools/hooks/commit-msg ; chmod +x $f
+```
 
-Install [Android Studio](go/install-android-studio). Then import the `tests` Gradle project into Android Studio.
+* To learn more, checkout [Basic Gerrit Walkthrough](https://gerrit-review.googlesource.com/Documentation/intro-gerrit-walkthrough-github.html).
+* See tools/git_clone_projects.sh as an example to get both Car/libs and tests projects. 
+
+2. Install [Android Studio](https://developer.android.com/studio), open the `tests`
+project by Android Studio and do your magic.
+  * You will need to build car-ui-lib by Android Studio first.
 
 ### TestMediaApp
 
-TestMediaApp should be one of the run configurations. The green Run button should build and install the app on your phone.
+TestMediaApp should be one of the run configurations. The green Run button should build and install
+the app on your phone.
 
 To see TestMediaApp in Android Auto Projected:
 
@@ -24,15 +45,20 @@
 
 ### RotaryPlayground
 
-RotaryPlayground is a test and reference application for the AAOS Rotary framework to use with an external rotary input device.
+RotaryPlayground is a test and reference application for the AAOS Rotary framework to use with an
+external rotary input device.
 
-To buid and install RotaryPlayground into an AAOS device:
+Beside building in Android Studio, you can also build and install RotaryPlayground into an AAOS
+device:
 
 ```
 $ make RotaryPlayground
 $ adb install -r -g out/target/[path]/system/app/RotaryPlayground/RotaryPlayground.apk
 ```
 
+* See tools/go_rotary.sh for an example build, install and run the test app in an Android tree. 
+
+
 ### RotaryIME
 
 RotaryIME is a sample input method for rotary controllers.
@@ -47,18 +73,20 @@
 
 ### Workstation setup
 
-Install [repo](https://source.android.com/setup/build/downloading#installing-repo) command line tool. Then run:
+Install [repo](https://source.android.com/setup/build/downloading#installing-repo) command line
+tool. Then run:
 
 ```
 sudo apt-get install gitk
 sudo apt-get install git-gui
 mkdir WORKING_DIRECTORY_FOR_GIT_REPO
 cd WORKING_DIRECTORY_FOR_GIT_REPO
-repo init -u persistent-https://googleplex-android.git.corp.google.com/platform/manifest -b pi-car-dev -g name:platform/tools/repohooks,name:platform/packages/apps/Car/tests --depth=1
+repo init -u https://android.googlesource.com/platform/manifest -b $BRANCH -g name:platform/tools/repohooks,name:platform/packages/apps/Car/tests --depth=1
 repo sync
 ```
 
 ### Making a change
+#### Repo workflow
 
 ```
 repo start BRANCH_NAME .
@@ -68,3 +96,10 @@
 repo upload .
 ```
 
+#### Git workflow
+```
+# Make some changes
+git add .
+git commit
+git push origin HEAD:refs/for/$BRANCH
+```
diff --git a/settings.gradle b/settings.gradle
index 38f6519..d3837c7 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -14,4 +14,10 @@
  * limitations under the License.
  */
 
+// You need to have car-ui-lib project in place. See tools/git_clone_projects.sh for example.
+include ':car-ui-lib'
+project(':car-ui-lib').projectDir= new File('../libs/car-ui-lib/car-ui-lib')
+
 include ':TestMediaApp'
+include ':RotaryPlayground'
+include ':RotaryIME'
diff --git a/tools/git_clone_projects.sh b/tools/git_clone_projects.sh
new file mode 100755
index 0000000..80fbf5e
--- /dev/null
+++ b/tools/git_clone_projects.sh
@@ -0,0 +1,67 @@
+#!/bin/bash
+
+# Copyright (C) 2020 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.
+
+echo "An example to clone minimal git projects for the tests app development by Android Studio."
+
+if [[ -z $GIT_REPO_URL ]]; then
+    echo 'Error: you need to specify GIT_REPO_URL="target-url"'
+    exit
+fi
+echo "GIT_REPO_URL=$GIT_REPO_URL"
+
+if [[ -z $BRANCH ]]; then
+    echo 'Error: you need to specify BRANCH="target-branch"'
+    exit
+fi
+echo "BRANCH=$BRANCH"
+
+if [[ -z $WORK_DIR ]]; then
+    export WORK_DIR="$PWD/Car"
+fi
+echo "WORK_DIR=$WORK_DIR"
+
+mkdir -p $WORK_DIR
+cd $WORK_DIR
+
+PROJECTS=0
+SECONDS=0
+echo "Cloning Car/libs"
+git clone -b $BRANCH "$GIT_REPO_URL/platform/packages/apps/Car/libs"
+let "PROJECTS++"
+cd "$WORK_DIR/libs"
+f=`git rev-parse --git-dir`/hooks/commit-msg ; mkdir -p $(dirname $f) ; curl -Lo $f https://gerrit-review.googlesource.com/tools/hooks/commit-msg ; chmod +x $f
+cd $WORK_DIR
+echo
+
+echo "Cloning Car/libs"
+git clone -b $BRANCH "$GIT_REPO_URL/platform/packages/apps/Car/tests"
+let "PROJECTS++"
+cd "$WORK_DIR/tests"
+f=`git rev-parse --git-dir`/hooks/commit-msg ; mkdir -p $(dirname $f) ; curl -Lo $f https://gerrit-review.googlesource.com/tools/hooks/commit-msg ; chmod +x $f
+cd $WORK_DIR
+echo
+
+ls -l "$WORK_DIR"
+
+echo "
+
+Cloning $PROJECTS projects takes: $SECONDS sec.
+
+Do your magic and then get the change pushed for review, e.g.:
+git add -u
+git commit
+git push origin HEAD:refs/for/$BRANCH
+"
diff --git a/tools/goRotary.sh b/tools/go_rotary.sh
similarity index 88%
rename from tools/goRotary.sh
rename to tools/go_rotary.sh
index 1df3ac6..b3b28d3 100755
--- a/tools/goRotary.sh
+++ b/tools/go_rotary.sh
@@ -15,13 +15,14 @@
 # limitations under the License.
 
 TMP_OUTDIR="/tmp/rotary"
+ME=`basename "$0"`
 
 function help {
     echo "A simple helper script that runs the Trade Federation unit tests"
-    echo "to print this message: packages/apps/Car/tests/tools/goRotary.sh"
-    echo "to build: packages/apps/Car/tests/tools/goRotary.sh b"
-    echo "to install: packages/apps/Car/tests/tools/goRotary.sh i"
-    echo "to run only: packages/apps/Car/tests/tools/goRotary.sh r"
+    echo "to print this message: packages/apps/Car/tests/tools/$ME"
+    echo "to build: packages/apps/Car/tests/tools/$ME b"
+    echo "to install: packages/apps/Car/tests/tools/$ME i"
+    echo "to run only: packages/apps/Car/tests/tools/$ME r"
     echo "the apks and jar are in $TMP_OUTDIR"
 }
 
@@ -70,4 +71,4 @@
     exit
 fi
 
-help
\ No newline at end of file
+help