Backport ProviderStatus by using its old definitions

Changes in ContactsUnavailableFragment and ProviderStatusWatcher
are based on pre-M SDKs. Please see ag/699615 for reference.

Bug: 25629359
Change-Id: I813d27f1d445660e10404e66698d5b4ae056e1f9
diff --git a/res/values/strings.xml b/res/values/strings.xml
index a211c20..c963506 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -904,4 +904,7 @@
          For example: Photo from Google abc@gmail.com checked. [CHAR LIMIT=60]-->
     <string name="photo_view_description_checked">Photo from <xliff:g id="account_type">%s </xliff:g><xliff:g id="user_name">%s </xliff:g>checked</string>
 
+    <!-- Text shown in the contacts app while the background process updates contacts after a locale change [CHAR LIMIT=150]-->
+    <string name="locale_change_in_progress">Contact list is being updated to reflect the change of language.\n\nPlease wait...</string>
+
 </resources>
diff --git a/src/com/android/contacts/list/ContactsUnavailableFragment.java b/src/com/android/contacts/list/ContactsUnavailableFragment.java
index 293ed04..2b9e184 100644
--- a/src/com/android/contacts/list/ContactsUnavailableFragment.java
+++ b/src/com/android/contacts/list/ContactsUnavailableFragment.java
@@ -33,6 +33,7 @@
 
 import com.android.contacts.activities.ActionBarAdapter.TabState;
 import com.android.contacts.R;
+import com.android.contacts.util.ProviderStatusCompat;
 
 /**
  * Fragment shown when contacts are unavailable. It contains provider status
@@ -94,27 +95,41 @@
             // The view hasn't been inflated yet.
             return;
         }
-        switch (providerStatus) {
-            case ProviderStatus.STATUS_EMPTY:
-                setTabInfo(mNoContactsMsgResId, mLastTab);
-                if (mLastTab == TabState.ALL) {
-                    mAddAccountButton.setVisibility(View.VISIBLE);
-                    mImportContactsButton.setVisibility(View.VISIBLE);
-                }
-                mProgress.setVisibility(View.GONE);
-                break;
-
-            case ProviderStatus.STATUS_BUSY:
-                mMessageView.setText(R.string.upgrade_in_progress);
-                mMessageView.setGravity(Gravity.CENTER_HORIZONTAL);
-                mMessageView.setVisibility(View.VISIBLE);
-                mAddAccountButton.setVisibility(View.GONE);
-                mImportContactsButton.setVisibility(View.GONE);
-                mProgress.setVisibility(View.VISIBLE);
-                break;
+        if (providerStatus == ProviderStatusCompat.STATUS_EMPTY) {
+            updateViewsForEmptyStatus();
+        } else if (providerStatus == ProviderStatusCompat.STATUS_BUSY) {
+            updateViewsForBusyStatus(R.string.upgrade_in_progress);
+        } else if (providerStatus == ProviderStatusCompat.STATUS_CHANGING_LOCALE){
+            updateViewsForBusyStatus(R.string.locale_change_in_progress);
         }
     }
 
+    /**
+     * Update views in the fragment when provider status is empty.
+     */
+    private void updateViewsForEmptyStatus() {
+        setTabInfo(mNoContactsMsgResId, mLastTab);
+        if (mLastTab == TabState.ALL) {
+            mAddAccountButton.setVisibility(View.VISIBLE);
+            mImportContactsButton.setVisibility(View.VISIBLE);
+        }
+        mProgress.setVisibility(View.GONE);
+    }
+
+    /**
+     * Update views in the fragment when provider status is busy.
+     *
+     * @param resId resource ID of the string to show in mMessageView.
+     */
+    private void updateViewsForBusyStatus(int resId) {
+        mMessageView.setText(resId);
+        mMessageView.setGravity(Gravity.CENTER_HORIZONTAL);
+        mMessageView.setVisibility(View.VISIBLE);
+        mAddAccountButton.setVisibility(View.GONE);
+        mImportContactsButton.setVisibility(View.GONE);
+        mProgress.setVisibility(View.VISIBLE);
+    }
+
     @Override
     public void onClick(View v) {
         if (mListener == null) {
@@ -138,7 +153,7 @@
         mNoContactsMsgResId = resId;
         mLastTab = callerTab;
         if ((mMessageView != null) && (mProviderStatus != null) &&
-                (mProviderStatus.equals(ProviderStatus.STATUS_EMPTY))) {
+                mProviderStatus.equals(ProviderStatusCompat.STATUS_EMPTY)) {
             if (resId != -1) {
                 mMessageView.setText(mNoContactsMsgResId);
                 mMessageView.setGravity(Gravity.CENTER_HORIZONTAL);
diff --git a/src/com/android/contacts/list/ProviderStatusWatcher.java b/src/com/android/contacts/list/ProviderStatusWatcher.java
index f42bc3e..50ed653 100644
--- a/src/com/android/contacts/list/ProviderStatusWatcher.java
+++ b/src/com/android/contacts/list/ProviderStatusWatcher.java
@@ -25,6 +25,8 @@
 import android.provider.ContactsContract.ProviderStatus;
 import android.util.Log;
 
+import com.android.contacts.util.ProviderStatusCompat;
+
 import com.google.common.collect.Lists;
 
 import java.util.ArrayList;
@@ -167,16 +169,16 @@
      * provider?) this may still cause ANRs.
      *
      * In order to avoid that, if we can't load the status within {@link #LOAD_WAIT_TIMEOUT_MS},
-     * we'll give up and just returns {@link ProviderStatus#STATUS_BUSY} in order to unblock
+     * we'll give up and just returns {@link ProviderStatusCompat#STATUS_BUSY} in order to unblock
      * the UI thread.  The actual result will be delivered later via {@link ProviderStatusListener}.
-     * (If {@link ProviderStatus#STATUS_BUSY} is returned, the app (should) shows an according
+     * (If {@link ProviderStatusCompat#STATUS_BUSY} is returned, the app (should) shows an according
      * message, like "contacts are being updated".)
      */
     public int getProviderStatus() {
         waitForLoaded();
 
         if (mProviderStatus == null) {
-            return ProviderStatus.STATUS_BUSY;
+            return ProviderStatusCompat.STATUS_BUSY;
         }
 
         return mProviderStatus;
diff --git a/src/com/android/contacts/util/ProviderStatusCompat.java b/src/com/android/contacts/util/ProviderStatusCompat.java
new file mode 100644
index 0000000..72476f9
--- /dev/null
+++ b/src/com/android/contacts/util/ProviderStatusCompat.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+package com.android.contacts.util;
+
+import android.os.Build;
+import android.provider.ContactsContract.ProviderStatus;
+import com.android.contacts.common.compat.SdkVersionOverride;
+
+/**
+ * This class contains constants from the pre-M version of ContactsContract.ProviderStatus class
+ * and also the mappings between pre-M constants and M constants for compatibility purpose,
+ * because ProviderStatus class constant names and values changed and the class became visible in
+ * API level 23.
+ */
+public class ProviderStatusCompat {
+    /**
+     * Not instantiable.
+     */
+    private ProviderStatusCompat() {
+    }
+
+    public static final boolean USE_CURRENT_VERSION =
+            SdkVersionOverride.getSdkVersion(Build.VERSION_CODES.LOLLIPOP) >= Build.VERSION_CODES.M;
+
+    public static final int STATUS_EMPTY = USE_CURRENT_VERSION ?
+            ProviderStatus.STATUS_EMPTY : ProviderStatusCompat.STATUS_NO_ACCOUNTS_NO_CONTACTS;
+
+    public static final int STATUS_BUSY = USE_CURRENT_VERSION ?
+            ProviderStatus.STATUS_BUSY : ProviderStatusCompat.STATUS_UPGRADING;
+
+    /**
+     * Default status of the provider, using the actual constant to guard against errors
+     */
+    public static final int STATUS_NORMAL = ProviderStatus.STATUS_NORMAL;
+
+    /**
+     * The following three constants are from pre-M.
+     *
+     * The status used when the provider is in the process of upgrading.  Contacts
+     * are temporarily unaccessible.
+     */
+    private static final int STATUS_UPGRADING = 1;
+
+    /**
+     * The status used during a locale change.
+     */
+    public static final int STATUS_CHANGING_LOCALE = 3;
+
+    /**
+     * The status that indicates that there are no accounts and no contacts
+     * on the device.
+     */
+    private static final int STATUS_NO_ACCOUNTS_NO_CONTACTS = 4;
+}