Fix extra vox announcement "Alert" in app list

Changes:
1. Use |announcement_view_| in app list to announce all alerts instead
of creating such view in different places.
2. Aggregate all usages of announcing open/close folder in one place.
3. Remove kAlert role from AppsGridView to avoid extra vox announcement
   "Alert".

Bug: 929775
Change-Id: I8ce053d055c6afbf4fa349f9d449a4d069f4854b
Reviewed-on: https://chromium-review.googlesource.com/c/1459796
Commit-Queue: Weidong Guo <weidongg@chromium.org>
Reviewed-by: Alex Newcomer <newcomer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#630107}
diff --git a/ash/app_list/views/app_list_folder_view.cc b/ash/app_list/views/app_list_folder_view.cc
index 3d0260c..f5a77c1 100644
--- a/ash/app_list/views/app_list_folder_view.cc
+++ b/ash/app_list/views/app_list_folder_view.cc
@@ -472,9 +472,6 @@
   view_model_->Add(page_switcher_, kIndexPageSwitcher);
 
   model_->AddObserver(this);
-
-  announcement_view_ = new views::View();
-  AddChildView(announcement_view_);
 }
 
 AppListFolderView::~AppListFolderView() {
@@ -490,7 +487,6 @@
 }
 
 void AppListFolderView::SetAppListFolderItem(AppListFolderItem* folder) {
-  CreateOpenOrCloseFolderAccessibilityEvent(true);
   folder_item_ = folder;
   items_grid_view_->SetItemList(folder_item_->item_list());
   folder_header_view_->SetFolderItem(folder_item_);
@@ -500,6 +496,7 @@
 
 void AppListFolderView::ScheduleShowHideAnimation(bool show,
                                                   bool hide_for_reparent) {
+  CreateOpenOrCloseFolderAccessibilityEvent(show);
   animation_start_frame_number_ =
       GetCompositorActivatedFrameCount(GetCompositor());
 
@@ -805,7 +802,6 @@
 
 void AppListFolderView::CloseFolderPage() {
   GiveBackFocusToSearchBox();
-  CreateOpenOrCloseFolderAccessibilityEvent(false);
   if (items_grid_view()->dragging())
     items_grid_view()->EndDrag(true);
   items_grid_view()->ClearAnySelectedView();
@@ -847,11 +843,13 @@
 }
 
 void AppListFolderView::CreateOpenOrCloseFolderAccessibilityEvent(bool open) {
-  announcement_view_->GetViewAccessibility().OverrideName(
+  auto* announcement_view =
+      contents_view_->app_list_view()->announcement_view();
+  announcement_view->GetViewAccessibility().OverrideName(
       ui::ResourceBundle::GetSharedInstance().GetLocalizedString(
           open ? IDS_APP_LIST_FOLDER_OPEN_FOLDER_ACCESSIBILE_NAME
                : IDS_APP_LIST_FOLDER_CLOSE_FOLDER_ACCESSIBILE_NAME));
-  announcement_view_->NotifyAccessibilityEvent(ax::mojom::Event::kAlert, true);
+  announcement_view->NotifyAccessibilityEvent(ax::mojom::Event::kAlert, true);
 }
 
 }  // namespace app_list
diff --git a/ash/app_list/views/app_list_folder_view.h b/ash/app_list/views/app_list_folder_view.h
index c1c1d89..295fa7c 100644
--- a/ash/app_list/views/app_list_folder_view.h
+++ b/ash/app_list/views/app_list_folder_view.h
@@ -197,9 +197,6 @@
   // The compositor frame number when animation starts.
   int animation_start_frame_number_;
 
-  // View used to announce opening and closing a folder.
-  views::View* announcement_view_ = nullptr;  // Owned by AppListFolderView.
-
   DISALLOW_COPY_AND_ASSIGN(AppListFolderView);
 };
 
diff --git a/ash/app_list/views/app_list_view.h b/ash/app_list/views/app_list_view.h
index cc8e466..c8654749 100644
--- a/ash/app_list/views/app_list_view.h
+++ b/ash/app_list/views/app_list_view.h
@@ -46,7 +46,7 @@
 namespace {
 // The background corner radius in peeking and fullscreen state.
 constexpr int kAppListBackgroundRadius = 28;
-}
+}  // namespace
 
 // AppListView is the top-level view and controller of app list UI. It creates
 // and hosts a AppsGridView and passes AppListModel to it for display.
@@ -229,6 +229,8 @@
 
   AppListMainView* app_list_main_view() const { return app_list_main_view_; }
 
+  views::View* announcement_view() const { return announcement_view_; }
+
   bool is_fullscreen() const {
     return app_list_state_ == AppListViewState::FULLSCREEN_ALL_APPS ||
            app_list_state_ == AppListViewState::FULLSCREEN_SEARCH;
@@ -419,7 +421,10 @@
   // Whether the on-screen keyboard is shown.
   bool onscreen_keyboard_shown_ = false;
 
-  // View used to announce the state transition for peeking and fullscreen.
+  // View used to announce:
+  // 1. state transition for peeking and fullscreen
+  // 2. folder opening and closing.
+  // 3. app dragging in AppsGridView.
   views::View* announcement_view_ = nullptr;  // Owned by AppListView.
 
   base::WeakPtrFactory<AppListView> weak_ptr_factory_;
diff --git a/ash/app_list/views/apps_grid_view.cc b/ash/app_list/views/apps_grid_view.cc
index 8c3ed2d7..02560ac 100644
--- a/ash/app_list/views/apps_grid_view.cc
+++ b/ash/app_list/views/apps_grid_view.cc
@@ -869,11 +869,6 @@
   return "AppsGridView";
 }
 
-void AppsGridView::GetAccessibleNodeData(ui::AXNodeData* node_data) {
-  node_data->role = ax::mojom::Role::kAlert;
-  node_data->SetName(accessible_name_);
-}
-
 void AppsGridView::Layout() {
   if (bounds_animator_.IsAnimating())
     bounds_animator_.Cancel();
@@ -1484,11 +1479,6 @@
     // Do not observe any data change since it is going to be hidden.
     item_list_->RemoveObserver(this);
     item_list_ = nullptr;
-
-    // Announce the folder close for dragging to the outside of the folder.
-    accessible_name_ = l10n_util::GetStringUTF16(
-        IDS_APP_LIST_FOLDER_CLOSE_FOLDER_ACCESSIBILE_NAME);
-    NotifyAccessibilityEvent(ax::mojom::Event::kAlert, true);
   }
 }
 
@@ -2759,12 +2749,15 @@
   DCHECK(drop_view);
 
   // Set a11y name to announce possible move to folder or creation of folder.
-  accessible_name_ = l10n_util::GetStringFUTF16(
-      IsFolderItem(drop_view->item())
-          ? IDS_APP_LIST_APP_DRAG_MOVE_TO_FOLDER_ACCESSIBILE_NAME
-          : IDS_APP_LIST_APP_DRAG_CREATE_FOLDER_ACCESSIBILE_NAME,
-      drag_view_->title()->text(), drop_view->title()->text());
-  NotifyAccessibilityEvent(ax::mojom::Event::kAlert, true);
+  auto* announcement_view =
+      contents_view_->app_list_view()->announcement_view();
+  announcement_view->GetViewAccessibility().OverrideName(
+      l10n_util::GetStringFUTF16(
+          IsFolderItem(drop_view->item())
+              ? IDS_APP_LIST_APP_DRAG_MOVE_TO_FOLDER_ACCESSIBILE_NAME
+              : IDS_APP_LIST_APP_DRAG_CREATE_FOLDER_ACCESSIBILE_NAME,
+          drag_view_->title()->text(), drop_view->title()->text()));
+  announcement_view->NotifyAccessibilityEvent(ax::mojom::Event::kAlert, true);
 }
 
 void AppsGridView::MaybeCreateReorderAccessibilityEvent() {
@@ -2792,12 +2785,15 @@
   const int col_number = (drop_target_.slot % cols_) + 1;
 
   // Set accessible name to announce drop target location by row and column.
-  accessible_name_ = l10n_util::GetStringFUTF16(
-      IDS_APP_LIST_APP_DRAG_LOCATION_ACCESSIBILE_NAME,
-      base::NumberToString16(drop_target_.page + 1),
-      base::NumberToString16(row_number), base::NumberToString16(col_number));
-
-  NotifyAccessibilityEvent(ax::mojom::Event::kAlert, true);
+  auto* announcement_view =
+      contents_view_->app_list_view()->announcement_view();
+  announcement_view->GetViewAccessibility().OverrideName(
+      l10n_util::GetStringFUTF16(
+          IDS_APP_LIST_APP_DRAG_LOCATION_ACCESSIBILE_NAME,
+          base::NumberToString16(drop_target_.page + 1),
+          base::NumberToString16(row_number),
+          base::NumberToString16(col_number)));
+  announcement_view->NotifyAccessibilityEvent(ax::mojom::Event::kAlert, true);
 }
 
 }  // namespace app_list
diff --git a/ash/app_list/views/apps_grid_view.h b/ash/app_list/views/apps_grid_view.h
index cb385d68..b378895 100644
--- a/ash/app_list/views/apps_grid_view.h
+++ b/ash/app_list/views/apps_grid_view.h
@@ -183,7 +183,6 @@
   bool CanDrop(const OSExchangeData& data) override;
   int OnDragUpdated(const ui::DropTargetEvent& event) override;
   const char* GetClassName() const override;
-  void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
 
   // Updates the visibility of app list items according to |app_list_state| and
   // |is_in_drag|.
@@ -717,9 +716,6 @@
   int horizontal_tile_padding_ = 0;
   int vertical_tile_padding_ = 0;
 
-  // Name used for app dragging accessibility events.
-  base::string16 accessible_name_;
-
   // The drop location of the most recent reorder related accessibility event.
   GridIndex last_reorder_a11y_event_location_;