Breaking out handling of empty message in Contacts
The empty message will now update itself.
More work is needed to turn it into proper MVC.
For now, just breaking it out into a separate view.
Change-Id: I48f319f78eb2d4ed89185b48082bbba96344f6c3
diff --git a/res/layout-finger/contacts_list_empty.xml b/res/layout-finger/contacts_list_empty.xml
index 195da1e..d655899 100644
--- a/res/layout-finger/contacts_list_empty.xml
+++ b/res/layout-finger/contacts_list_empty.xml
@@ -13,8 +13,9 @@
See the License for the specific language governing permissions and
limitations under the License.
-->
-<ScrollView
+<view
xmlns:android="http://schemas.android.com/apk/res/android"
+ class="com.android.contacts.ContactListEmptyView"
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
@@ -35,6 +36,7 @@
android:paddingRight="10dip"
android:paddingTop="10dip"
android:lineSpacingMultiplier="0.92"
+ android:visibility="gone"
/>
<LinearLayout android:id="@+id/import_failure"
@@ -61,4 +63,4 @@
android:text="@string/upgrade_out_of_memory_retry"/>
</LinearLayout>
</LinearLayout>
-</ScrollView>
+</view>
diff --git a/src/com/android/contacts/ContactListEmptyView.java b/src/com/android/contacts/ContactListEmptyView.java
new file mode 100644
index 0000000..58573f1
--- /dev/null
+++ b/src/com/android/contacts/ContactListEmptyView.java
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2010 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;
+
+import android.accounts.Account;
+import android.accounts.AccountManager;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.IContentService;
+import android.content.Intent;
+import android.os.RemoteException;
+import android.provider.ContactsContract;
+import android.telephony.TelephonyManager;
+import android.util.AttributeSet;
+import android.util.Log;
+import android.widget.ScrollView;
+import android.widget.TextView;
+
+/**
+ * Displays a message when there is nothing to display in a contact list.
+ */
+public class ContactListEmptyView extends ScrollView {
+
+ private static final String TAG = "ContactListEmptyView";
+
+ public ContactListEmptyView(Context context, AttributeSet attrs) {
+ super(context, attrs);
+ }
+
+ public void hide() {
+ TextView empty = (TextView) findViewById(R.id.emptyText);
+ empty.setVisibility(GONE);
+ }
+
+ protected void show(boolean searchMode, boolean displayOnlyPhones,
+ boolean isFavoritesMode, boolean isQueryMode, boolean isShortcutAction,
+ boolean isMultipleSelectionEnabled, boolean showSelectedOnly) {
+ if (searchMode) {
+ return;
+ }
+
+ TextView empty = (TextView) findViewById(R.id.emptyText);
+ Context context = getContext();
+ if (displayOnlyPhones) {
+ empty.setText(context.getText(R.string.noContactsWithPhoneNumbers));
+ } else if (isFavoritesMode) {
+ empty.setText(context.getText(R.string.noFavoritesHelpText));
+ } else if (isQueryMode) {
+ empty.setText(context.getText(R.string.noMatchingContacts));
+ } if (isMultipleSelectionEnabled) {
+ if (showSelectedOnly) {
+ empty.setText(context.getText(R.string.no_contacts_selected));
+ } else {
+ empty.setText(context.getText(R.string.noContactsWithPhoneNumbers));
+ }
+ } else {
+ TelephonyManager telephonyManager =
+ (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
+ boolean hasSim = telephonyManager.hasIccCard();
+ if (isSyncActive()) {
+ if (isShortcutAction) {
+ // Help text is the same no matter whether there is SIM or not.
+ empty.setText(
+ context.getText(R.string.noContactsHelpTextWithSyncForCreateShortcut));
+ } else if (hasSim) {
+ empty.setText(context.getText(R.string.noContactsHelpTextWithSync));
+ } else {
+ empty.setText(context.getText(R.string.noContactsNoSimHelpTextWithSync));
+ }
+ } else {
+ if (isShortcutAction) {
+ // Help text is the same no matter whether there is SIM or not.
+ empty.setText(context.getText(R.string.noContactsHelpTextForCreateShortcut));
+ } else if (hasSim) {
+ empty.setText(context.getText(R.string.noContactsHelpText));
+ } else {
+ empty.setText(context.getText(R.string.noContactsNoSimHelpText));
+ }
+ }
+ }
+ empty.setVisibility(VISIBLE);
+ }
+
+ private boolean isSyncActive() {
+ Account[] accounts = AccountManager.get(getContext()).getAccounts();
+ if (accounts != null && accounts.length > 0) {
+ IContentService contentService = ContentResolver.getContentService();
+ for (Account account : accounts) {
+ try {
+ if (contentService.isSyncActive(account, ContactsContract.AUTHORITY)) {
+ return true;
+ }
+ } catch (RemoteException e) {
+ Log.e(TAG, "Could not get the sync status");
+ }
+ }
+ }
+ return false;
+ }
+}
diff --git a/src/com/android/contacts/ContactsListActivity.java b/src/com/android/contacts/ContactsListActivity.java
index 058efb5..e5d3268 100644
--- a/src/com/android/contacts/ContactsListActivity.java
+++ b/src/com/android/contacts/ContactsListActivity.java
@@ -26,7 +26,6 @@
import com.android.contacts.util.Constants;
import android.accounts.Account;
-import android.accounts.AccountManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
@@ -39,7 +38,6 @@
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
-import android.content.IContentService;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.UriMatcher;
@@ -62,7 +60,6 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
-import android.os.RemoteException;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.provider.Settings;
@@ -94,7 +91,6 @@
import android.util.SparseIntArray;
import android.view.ContextMenu;
import android.view.ContextThemeWrapper;
-import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
@@ -108,8 +104,8 @@
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
-import android.view.inputmethod.EditorInfo;
import android.view.animation.AnimationUtils;
+import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.AdapterView;
@@ -129,7 +125,6 @@
import java.lang.ref.WeakReference;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
@@ -394,6 +389,7 @@
private static final String CONTENT_SCHEME = "content";
private ContactItemListAdapter mAdapter;
+ private ContactListEmptyView mEmptyView;
int mMode = MODE_DEFAULT;
@@ -877,6 +873,7 @@
if (mSearchMode) {
setupSearchView();
}
+
if (mMode == MODE_PICK_MULTIPLE_PHONES) {
ViewStub stub = (ViewStub)findViewById(R.id.footer_stub);
if (stub != null) {
@@ -889,6 +886,11 @@
revertButton.setOnClickListener(this);
}
}
+
+ View emptyView = mList.getEmptyView();
+ if (emptyView instanceof ContactListEmptyView) {
+ mEmptyView = (ContactListEmptyView)emptyView;
+ }
}
/**
@@ -981,73 +983,6 @@
}
}
- protected void setEmptyText() {
- if (mSearchMode) {
- return;
- }
-
- TextView empty = (TextView) findViewById(R.id.emptyText);
- int gravity = Gravity.NO_GRAVITY;
-
- if (mDisplayOnlyPhones) {
- empty.setText(getText(R.string.noContactsWithPhoneNumbers));
- } else if (mMode == MODE_STREQUENT || mMode == MODE_STARRED) {
- empty.setText(getText(R.string.noFavoritesHelpText));
- } else if (mMode == MODE_QUERY || mMode == MODE_QUERY_PICK
- || mMode == MODE_QUERY_PICK_PHONE || mMode == MODE_QUERY_PICK_TO_VIEW
- || mMode == MODE_QUERY_PICK_TO_EDIT) {
- empty.setText(getText(R.string.noMatchingContacts));
- } else if (mMode == MODE_PICK_MULTIPLE_PHONES) {
- if (mShowSelectedOnly) {
- empty.setText(getText(R.string.no_contacts_selected));
- } else {
- empty.setText(getText(R.string.noContactsWithPhoneNumbers));
- }
- gravity = Gravity.CENTER;
- } else {
- boolean hasSim = ((TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE))
- .hasIccCard();
- boolean createShortcut = Intent.ACTION_CREATE_SHORTCUT.equals(getIntent().getAction());
- if (isSyncActive()) {
- if (createShortcut) {
- // Help text is the same no matter whether there is SIM or not.
- empty.setText(getText(R.string.noContactsHelpTextWithSyncForCreateShortcut));
- } else if (hasSim) {
- empty.setText(getText(R.string.noContactsHelpTextWithSync));
- } else {
- empty.setText(getText(R.string.noContactsNoSimHelpTextWithSync));
- }
- } else {
- if (createShortcut) {
- // Help text is the same no matter whether there is SIM or not.
- empty.setText(getText(R.string.noContactsHelpTextForCreateShortcut));
- } else if (hasSim) {
- empty.setText(getText(R.string.noContactsHelpText));
- } else {
- empty.setText(getText(R.string.noContactsNoSimHelpText));
- }
- }
- }
- empty.setGravity(gravity);
- }
-
- private boolean isSyncActive() {
- Account[] accounts = AccountManager.get(this).getAccounts();
- if (accounts != null && accounts.length > 0) {
- IContentService contentService = ContentResolver.getContentService();
- for (Account account : accounts) {
- try {
- if (contentService.isSyncActive(account, ContactsContract.AUTHORITY)) {
- return true;
- }
- } catch (RemoteException e) {
- Log.e(TAG, "Could not get the sync status");
- }
- }
- }
- return false;
- }
-
private void buildUserGroupUri(String group) {
mGroupUri = Uri.withAppendedPath(Contacts.CONTENT_GROUP_URI, group);
}
@@ -1357,13 +1292,11 @@
}
case R.id.menu_display_selected: {
mShowSelectedOnly = true;
- setEmptyText();
startQuery();
return true;
}
case R.id.menu_display_all: {
mShowSelectedOnly = false;
- setEmptyText();
startQuery();
return true;
}
@@ -1405,9 +1338,6 @@
* search text edit.
*/
protected void onSearchTextChanged() {
- // Set the proper empty string
- setEmptyText();
-
Filter filter = mAdapter.getFilter();
filter.filter(getTextFilter());
}
@@ -2482,14 +2412,15 @@
}
void startQuery() {
- // Set the proper empty string
- setEmptyText();
-
if (mSearchResultsMode) {
TextView foundContactsText = (TextView)findViewById(R.id.search_results_found);
foundContactsText.setText(R.string.search_results_searching);
}
+ if (mEmptyView != null) {
+ mEmptyView.hide();
+ }
+
mAdapter.setLoading(true);
// Cancel any pending queries
@@ -3512,7 +3443,19 @@
foundContactsText.setText(text);
}
+ if (mEmptyView != null && (cursor == null || cursor.getCount() == 0)) {
+ mEmptyView.show(mSearchMode, mDisplayOnlyPhones,
+ mMode == MODE_STREQUENT || mMode == MODE_STARRED,
+ mMode == MODE_QUERY || mMode == MODE_QUERY_PICK
+ || mMode == MODE_QUERY_PICK_PHONE || mMode == MODE_QUERY_PICK_TO_VIEW
+ || mMode == MODE_QUERY_PICK_TO_EDIT,
+ mShortcutAction != null,
+ mMode == MODE_PICK_MULTIPLE_PHONES,
+ mShowSelectedOnly);
+ }
+
super.changeCursor(cursor);
+
// Update the indexer for the fast scroll widget
updateIndexer(cursor);
@@ -3871,6 +3814,7 @@
return null;
}
+ @Override
public int getItemViewType(int position) {
return position == mStartPos ? IGNORE_ITEM_VIEW_TYPE : super.getItemViewType(position);
}
@@ -3947,7 +3891,7 @@
selectedPhoneIds[index++] = phoneId.longValue();
}
icicle.putLongArray(SELECTED_PHONE_IDS_KEY, selectedPhoneIds);
-
+
}
}
diff --git a/src/com/android/contacts/JoinContactActivity.java b/src/com/android/contacts/JoinContactActivity.java
index 85a050c..47c7547 100644
--- a/src/com/android/contacts/JoinContactActivity.java
+++ b/src/com/android/contacts/JoinContactActivity.java
@@ -200,11 +200,6 @@
super.onQueryComplete(cursor);
}
- @Override
- protected void setEmptyText() {
- return;
- }
-
private class JoinContactListAdapter extends ContactItemListAdapter {
Cursor mSuggestionsCursor;
int mSuggestionsCursorCount;