Merge "updater_sample: add metadata mismatch error code"
diff --git a/etc/init.rc b/etc/init.rc
index 0fc6c4c..96c37b1 100644
--- a/etc/init.rc
+++ b/etc/init.rc
@@ -22,6 +22,7 @@
     mkdir /data
     mkdir /cache
     mkdir /sideload
+    mkdir /mnt/system
     mount tmpfs tmpfs /tmp
 
     chown root shell /tmp
@@ -87,7 +88,7 @@
 service recovery /sbin/recovery
     seclabel u:r:recovery:s0
 
-service adbd /sbin/adbd --root_seclabel=u:r:su:s0 --device_banner=recovery
+service adbd /system/bin/adbd --root_seclabel=u:r:su:s0 --device_banner=recovery
     disabled
     socket adbd stream 660 system system
     seclabel u:r:adbd:s0
diff --git a/fsck_unshare_blocks.cpp b/fsck_unshare_blocks.cpp
index a100368..2e6b5b8 100644
--- a/fsck_unshare_blocks.cpp
+++ b/fsck_unshare_blocks.cpp
@@ -115,14 +115,6 @@
   return true;
 }
 
-static const char* get_system_root() {
-  if (android::base::GetBoolProperty("ro.build.system_root_image", false)) {
-    return "/system_root";
-  } else {
-    return "/system";
-  }
-}
-
 bool do_fsck_unshare_blocks() {
   // List of partitions we will try to e2fsck -E unshare_blocks.
   std::vector<std::string> partitions = { "/odm", "/oem", "/product", "/vendor" };
@@ -130,10 +122,10 @@
   // Temporarily mount system so we can copy e2fsck_static.
   bool mounted = false;
   if (android::base::GetBoolProperty("ro.build.system_root_image", false)) {
-    mounted = ensure_path_mounted_at("/", "/system_root") != -1;
+    mounted = ensure_path_mounted_at("/", "/mnt/system") != -1;
     partitions.push_back("/");
   } else {
-    mounted = ensure_path_mounted("/system") != -1;
+    mounted = ensure_path_mounted_at("/system", "/mnt/system") != -1;
     partitions.push_back("/system");
   }
   if (!mounted) {
@@ -144,7 +136,7 @@
     LOG(ERROR) << "Could not copy e2fsck to /tmp.";
     return false;
   }
-  if (umount(get_system_root()) < 0) {
+  if (umount("/mnt/system") < 0) {
     PLOG(ERROR) << "umount failed";
     return false;
   }
diff --git a/recovery.cpp b/recovery.cpp
index 98cbfed..fea65ae 100644
--- a/recovery.cpp
+++ b/recovery.cpp
@@ -820,15 +820,13 @@
         break;
       }
       case Device::MOUNT_SYSTEM:
-        // For a system image built with the root directory (i.e. system_root_image == "true"), we
-        // mount it to /system_root, and symlink /system to /system_root/system to make adb shell
-        // work (the symlink is created through the build system). (Bug: 22855115)
+        // the system partition is mounted at /mnt/system
         if (android::base::GetBoolProperty("ro.build.system_root_image", false)) {
-          if (ensure_path_mounted_at("/", "/system_root") != -1) {
+          if (ensure_path_mounted_at("/", "/mnt/system") != -1) {
             ui->Print("Mounted /system.\n");
           }
         } else {
-          if (ensure_path_mounted("/system") != -1) {
+          if (ensure_path_mounted_at("/system", "/mnt/system") != -1) {
             ui->Print("Mounted /system.\n");
           }
         }
diff --git a/screen_ui.cpp b/screen_ui.cpp
index b4ef054..f9c4a06 100644
--- a/screen_ui.cpp
+++ b/screen_ui.cpp
@@ -176,6 +176,8 @@
   if (progress_thread_.joinable()) {
     progress_thread_.join();
   }
+  // No-op if gr_init() (via Init()) was not called or had failed.
+  gr_exit();
 }
 
 GRSurface* ScreenRecoveryUI::GetCurrentFrame() const {
@@ -700,10 +702,10 @@
 }
 
 bool ScreenRecoveryUI::InitTextParams() {
-  if (gr_init() < 0) {
+  // gr_init() would return successfully on font initialization failure.
+  if (gr_sys_font() == nullptr) {
     return false;
   }
-
   gr_font_size(gr_sys_font(), &char_width_, &char_height_);
   text_rows_ = (ScreenHeight() - kMarginHeight * 2) / char_height_;
   text_cols_ = (ScreenWidth() - kMarginWidth * 2) / char_width_;
@@ -713,6 +715,10 @@
 bool ScreenRecoveryUI::Init(const std::string& locale) {
   RecoveryUI::Init(locale);
 
+  if (gr_init() == -1) {
+    return false;
+  }
+
   if (!InitTextParams()) {
     return false;
   }
diff --git a/tests/unit/screen_ui_test.cpp b/tests/unit/screen_ui_test.cpp
index a3dd2ad..2f4b7b0 100644
--- a/tests/unit/screen_ui_test.cpp
+++ b/tests/unit/screen_ui_test.cpp
@@ -30,6 +30,7 @@
 
 #include "common/test_constants.h"
 #include "device.h"
+#include "minui/minui.h"
 #include "otautil/paths.h"
 #include "private/resources.h"
 #include "screen_ui.h"
@@ -274,18 +275,34 @@
   const std::string kTestRtlLocaleWithSuffix = "ar-EG";
 
   void SetUp() override {
-    ui_ = std::make_unique<TestableScreenRecoveryUI>();
+    has_graphics_ = gr_init() == 0;
+    gr_exit();
+
+    if (has_graphics_) {
+      ui_ = std::make_unique<TestableScreenRecoveryUI>();
+    }
 
     testdata_dir_ = from_testdata_base("");
     Paths::Get().set_resource_dir(testdata_dir_);
     res_set_resource_dir(testdata_dir_);
   }
 
+  bool has_graphics_;
   std::unique_ptr<TestableScreenRecoveryUI> ui_;
   std::string testdata_dir_;
 };
 
+#define RETURN_IF_NO_GRAPHICS                                                 \
+  do {                                                                        \
+    if (!has_graphics_) {                                                     \
+      GTEST_LOG_(INFO) << "Test skipped due to no available graphics device"; \
+      return;                                                                 \
+    }                                                                         \
+  } while (false)
+
 TEST_F(ScreenRecoveryUITest, Init) {
+  RETURN_IF_NO_GRAPHICS;
+
   ASSERT_TRUE(ui_->Init(kTestLocale));
   ASSERT_EQ(kTestLocale, ui_->GetLocale());
   ASSERT_FALSE(ui_->GetRtlLocale());
@@ -299,6 +316,8 @@
 }
 
 TEST_F(ScreenRecoveryUITest, ShowText) {
+  RETURN_IF_NO_GRAPHICS;
+
   ASSERT_TRUE(ui_->Init(kTestLocale));
   ASSERT_FALSE(ui_->IsTextVisible());
   ui_->ShowText(true);
@@ -311,16 +330,22 @@
 }
 
 TEST_F(ScreenRecoveryUITest, RtlLocale) {
+  RETURN_IF_NO_GRAPHICS;
+
   ASSERT_TRUE(ui_->Init(kTestRtlLocale));
   ASSERT_TRUE(ui_->GetRtlLocale());
 }
 
 TEST_F(ScreenRecoveryUITest, RtlLocaleWithSuffix) {
+  RETURN_IF_NO_GRAPHICS;
+
   ASSERT_TRUE(ui_->Init(kTestRtlLocaleWithSuffix));
   ASSERT_TRUE(ui_->GetRtlLocale());
 }
 
 TEST_F(ScreenRecoveryUITest, ShowMenu) {
+  RETURN_IF_NO_GRAPHICS;
+
   ASSERT_TRUE(ui_->Init(kTestLocale));
   ui_->SetKeyBuffer({
       KeyCode::UP,
@@ -347,6 +372,8 @@
 }
 
 TEST_F(ScreenRecoveryUITest, ShowMenu_NotMenuOnly) {
+  RETURN_IF_NO_GRAPHICS;
+
   ASSERT_TRUE(ui_->Init(kTestLocale));
   ui_->SetKeyBuffer({
       KeyCode::MAGIC,
@@ -358,6 +385,8 @@
 }
 
 TEST_F(ScreenRecoveryUITest, ShowMenu_TimedOut) {
+  RETURN_IF_NO_GRAPHICS;
+
   ASSERT_TRUE(ui_->Init(kTestLocale));
   ui_->SetKeyBuffer({
       KeyCode::TIMEOUT,
@@ -366,6 +395,8 @@
 }
 
 TEST_F(ScreenRecoveryUITest, ShowMenu_TimedOut_TextWasEverVisible) {
+  RETURN_IF_NO_GRAPHICS;
+
   ASSERT_TRUE(ui_->Init(kTestLocale));
   ui_->ShowText(true);
   ui_->ShowText(false);
@@ -382,6 +413,8 @@
 }
 
 TEST_F(ScreenRecoveryUITest, LoadAnimation) {
+  RETURN_IF_NO_GRAPHICS;
+
   ASSERT_TRUE(ui_->Init(kTestLocale));
   // Make a few copies of loop00000.png from testdata.
   std::string image_data;
@@ -410,6 +443,8 @@
 }
 
 TEST_F(ScreenRecoveryUITest, LoadAnimation_MissingAnimation) {
+  RETURN_IF_NO_GRAPHICS;
+
   ASSERT_TRUE(ui_->Init(kTestLocale));
   TemporaryDir resource_dir;
   Paths::Get().set_resource_dir(resource_dir.path);
@@ -417,3 +452,5 @@
   ::testing::FLAGS_gtest_death_test_style = "threadsafe";
   ASSERT_EXIT(ui_->RunLoadAnimation(), ::testing::KilledBySignal(SIGABRT), "");
 }
+
+#undef RETURN_IF_NO_GRAPHICS
diff --git a/updater_sample/Android.mk b/updater_sample/Android.mk
index a5deee1..7662111 100644
--- a/updater_sample/Android.mk
+++ b/updater_sample/Android.mk
@@ -20,8 +20,9 @@
 LOCAL_PACKAGE_NAME := SystemUpdaterSample
 LOCAL_MODULE_TAGS := samples
 LOCAL_SDK_VERSION := system_current
-LOCAL_PRIVILEGED_MODULE := true
-LOCAL_PROGUARD_FLAG_FILES := proguard.flags
+
+# TODO: enable proguard and use proguard.flags file
+LOCAL_PROGUARD_ENABLED := disabled
 
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
diff --git a/updater_sample/proguard.flags b/updater_sample/proguard.flags
deleted file mode 100644
index 5883608..0000000
--- a/updater_sample/proguard.flags
+++ /dev/null
@@ -1,23 +0,0 @@
-# Copyright (C) 2018 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.
-
-# Keep, used in tests.
--keep public class com.example.android.systemupdatersample.UpdateManager {
-   public int getUpdaterState();
-}
-
-# Keep, used in tests.
--keep public class com.example.android.systemupdatersample.UpdateConfig {
-   public <init>(java.lang.String, java.lang.String, int);
-}
diff --git a/updater_sample/tests/Android.mk b/updater_sample/tests/Android.mk
index 4157604..9aec372 100644
--- a/updater_sample/tests/Android.mk
+++ b/updater_sample/tests/Android.mk
@@ -22,10 +22,10 @@
 LOCAL_MODULE_TAGS := tests
 LOCAL_JAVA_LIBRARIES := \
     android.test.base.stubs \
-    android.test.runner.stubs
-LOCAL_STATIC_JAVA_LIBRARIES := android-support-test \
-    mockito-target-minus-junit4 \
+    android.test.runner.stubs \
     guava
+LOCAL_STATIC_JAVA_LIBRARIES := android-support-test \
+    mockito-target-minus-junit4
 LOCAL_INSTRUMENTATION_FOR := SystemUpdaterSample
 LOCAL_PROGUARD_ENABLED := disabled