Added permission revoked screen for new search fragment

screenshot: http://screen/WpQJZ0Xy1gi
Bug: 37209462
Test: NewSearchFragmentTest
PiperOrigin-RevId: 164407405
Change-Id: I3c66dc289524573e687266217b57b19a8ded8c9c
diff --git a/java/com/android/dialer/searchfragment/list/NewSearchFragment.java b/java/com/android/dialer/searchfragment/list/NewSearchFragment.java
index 566ba77..dff6c72 100644
--- a/java/com/android/dialer/searchfragment/list/NewSearchFragment.java
+++ b/java/com/android/dialer/searchfragment/list/NewSearchFragment.java
@@ -19,9 +19,13 @@
 import android.app.Fragment;
 import android.app.LoaderManager.LoaderCallbacks;
 import android.content.Loader;
+import android.content.pm.PackageManager;
 import android.database.Cursor;
 import android.os.Bundle;
+import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
+import android.support.annotation.VisibleForTesting;
+import android.support.v13.app.FragmentCompat;
 import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
 import android.view.LayoutInflater;
@@ -30,21 +34,30 @@
 import android.view.animation.Interpolator;
 import com.android.contacts.common.extensions.PhoneDirectoryExtenderAccessor;
 import com.android.dialer.animation.AnimUtils;
+import com.android.dialer.common.LogUtil;
 import com.android.dialer.common.concurrent.ThreadUtil;
 import com.android.dialer.searchfragment.cp2.SearchContactsCursorLoader;
 import com.android.dialer.searchfragment.nearbyplaces.NearbyPlacesCursorLoader;
+import com.android.dialer.util.PermissionsUtil;
 import com.android.dialer.util.ViewUtil;
+import com.android.dialer.widget.EmptyContentView;
+import com.android.dialer.widget.EmptyContentView.OnEmptyViewActionButtonClickedListener;
+import java.util.Arrays;
 
 /** Fragment used for searching contacts. */
-public final class NewSearchFragment extends Fragment implements LoaderCallbacks<Cursor> {
+public final class NewSearchFragment extends Fragment
+    implements LoaderCallbacks<Cursor>, OnEmptyViewActionButtonClickedListener {
 
   // Since some of our queries can generate network requests, we should delay them until the user
   // stops typing to prevent generating too much network traffic.
   private static final int NETWORK_SEARCH_DELAY_MILLIS = 300;
 
+  @VisibleForTesting public static final int READ_CONTACTS_PERMISSION_REQUEST_CODE = 1;
+
   private static final int CONTACTS_LOADER_ID = 0;
   private static final int NEARBY_PLACES_ID = 1;
 
+  private EmptyContentView emptyContentView;
   private RecyclerView recyclerView;
   private SearchAdapter adapter;
   private String query;
@@ -58,15 +71,22 @@
   @Override
   public View onCreateView(
       LayoutInflater inflater, @Nullable ViewGroup parent, @Nullable Bundle bundle) {
-    getLoaderManager().initLoader(0, null, this);
     View view = inflater.inflate(R.layout.fragment_search, parent, false);
     adapter = new SearchAdapter(getContext());
+    emptyContentView = view.findViewById(R.id.empty_view);
     recyclerView = view.findViewById(R.id.recycler_view);
     recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
     recyclerView.setAdapter(adapter);
 
-    getLoaderManager().initLoader(CONTACTS_LOADER_ID, null, this);
-    loadNearbyPlacesCursor();
+    if (!PermissionsUtil.hasContactsReadPermissions(getContext())) {
+      emptyContentView.setDescription(R.string.new_permission_no_search);
+      emptyContentView.setActionLabel(R.string.permission_single_turn_on);
+      emptyContentView.setActionClickedListener(this);
+      emptyContentView.setImage(R.drawable.empty_contacts);
+      emptyContentView.setVisibility(View.VISIBLE);
+    } else {
+      initLoaders();
+    }
 
     if (updatePositionRunnable != null) {
       ViewUtil.doOnPreDraw(view, false, updatePositionRunnable);
@@ -74,6 +94,11 @@
     return view;
   }
 
+  private void initLoaders() {
+    getLoaderManager().initLoader(CONTACTS_LOADER_ID, null, this);
+    loadNearbyPlacesCursor();
+  }
+
   @Override
   public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
     // TODO(calderwoodra) add enterprise loader
@@ -144,4 +169,30 @@
     ThreadUtil.getUiThreadHandler()
         .postDelayed(loadNearbyPlacesRunnable, NETWORK_SEARCH_DELAY_MILLIS);
   }
+
+  @Override
+  public void onRequestPermissionsResult(
+      int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
+    if (requestCode == READ_CONTACTS_PERMISSION_REQUEST_CODE) {
+      if (grantResults.length >= 1 && PackageManager.PERMISSION_GRANTED == grantResults[0]) {
+        // Force a refresh of the data since we were missing the permission before this.
+        emptyContentView.setVisibility(View.GONE);
+        initLoaders();
+      }
+    }
+  }
+
+  @Override
+  public void onEmptyViewActionButtonClicked() {
+    String[] deniedPermissions =
+        PermissionsUtil.getPermissionsCurrentlyDenied(
+            getContext(), PermissionsUtil.allContactsGroupPermissionsUsedInDialer);
+    if (deniedPermissions.length > 0) {
+      LogUtil.i(
+          "NewSearchFragment.onEmptyViewActionButtonClicked",
+          "Requesting permissions: " + Arrays.toString(deniedPermissions));
+      FragmentCompat.requestPermissions(
+          this, deniedPermissions, READ_CONTACTS_PERMISSION_REQUEST_CODE);
+    }
+  }
 }
diff --git a/java/com/android/dialer/searchfragment/list/res/layout/fragment_search.xml b/java/com/android/dialer/searchfragment/list/res/layout/fragment_search.xml
index 06f2348..06c2660 100644
--- a/java/com/android/dialer/searchfragment/list/res/layout/fragment_search.xml
+++ b/java/com/android/dialer/searchfragment/list/res/layout/fragment_search.xml
@@ -14,8 +14,19 @@
   ~ See the License for the specific language governing permissions and
   ~ limitations under the License
   -->
-<android.support.v7.widget.RecyclerView
+<FrameLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
-    android:id="@+id/recycler_view"
-    android:layout_width="match_parent"
-    android:layout_height="match_parent"/>
+   android:layout_height="match_parent"
+    android:layout_width="match_parent">
+
+  <android.support.v7.widget.RecyclerView
+      android:id="@+id/recycler_view"
+      android:layout_width="match_parent"
+      android:layout_height="match_parent"/>
+
+  <com.android.dialer.widget.EmptyContentView
+      android:id="@+id/empty_view"
+      android:layout_width="match_parent"
+      android:layout_height="match_parent"
+      android:visibility="gone"/>
+</FrameLayout>
diff --git a/java/com/android/dialer/searchfragment/list/res/values/strings.xml b/java/com/android/dialer/searchfragment/list/res/values/strings.xml
new file mode 100644
index 0000000..0d25b8c
--- /dev/null
+++ b/java/com/android/dialer/searchfragment/list/res/values/strings.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2012 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
+  -->
+<resources>
+  <!-- Shown as a prompt to turn on contacts permissions to allow contact search [CHAR LIMIT=NONE]. See 2424710404207193826 for current translation. -->
+  <string name="new_permission_no_search">To search your contacts, turn on the Contacts permissions.</string>
+</resources>