Home | History | Annotate | Download | only in contacts
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.contacts;
     18 
     19 import android.accounts.Account;
     20 import android.accounts.AccountManager;
     21 import android.content.ContentResolver;
     22 import android.content.Context;
     23 import android.content.IContentService;
     24 import android.os.RemoteException;
     25 import android.provider.ContactsContract;
     26 import android.telephony.TelephonyManager;
     27 import android.util.AttributeSet;
     28 import android.util.Log;
     29 import android.widget.ScrollView;
     30 import android.widget.TextView;
     31 
     32 /**
     33  * Displays a message when there is nothing to display in a contact list.
     34  */
     35 public class ContactListEmptyView extends ScrollView {
     36 
     37     private static final String TAG = "ContactListEmptyView";
     38 
     39     public ContactListEmptyView(Context context, AttributeSet attrs) {
     40         super(context, attrs);
     41     }
     42 
     43     public void hide() {
     44         TextView empty = (TextView) findViewById(R.id.emptyText);
     45         empty.setVisibility(GONE);
     46     }
     47 
     48     public void show(boolean searchMode, boolean displayOnlyPhones,
     49             boolean isFavoritesMode, boolean isQueryMode, boolean isShortcutAction,
     50             boolean isMultipleSelectionEnabled, boolean showSelectedOnly) {
     51         if (searchMode) {
     52             return;
     53         }
     54 
     55         TextView empty = (TextView) findViewById(R.id.emptyText);
     56         Context context = getContext();
     57         if (displayOnlyPhones) {
     58             empty.setText(context.getText(R.string.noContactsWithPhoneNumbers));
     59         } else if (isFavoritesMode) {
     60             empty.setText(context.getText(R.string.noFavoritesHelpText));
     61         } else if (isQueryMode) {
     62             empty.setText(context.getText(R.string.noMatchingContacts));
     63         } if (isMultipleSelectionEnabled) {
     64             if (showSelectedOnly) {
     65                 empty.setText(context.getText(R.string.no_contacts_selected));
     66             } else {
     67                 empty.setText(context.getText(R.string.noContactsWithPhoneNumbers));
     68             }
     69         } else {
     70             TelephonyManager telephonyManager =
     71                     (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
     72             boolean hasSim = telephonyManager.hasIccCard();
     73             if (isSyncActive()) {
     74                 if (isShortcutAction) {
     75                     // Help text is the same no matter whether there is SIM or not.
     76                     empty.setText(
     77                             context.getText(R.string.noContactsHelpTextWithSyncForCreateShortcut));
     78                 } else if (hasSim) {
     79                     empty.setText(context.getText(R.string.noContactsHelpTextWithSync));
     80                 } else {
     81                     empty.setText(context.getText(R.string.noContactsNoSimHelpTextWithSync));
     82                 }
     83             } else {
     84                 if (isShortcutAction) {
     85                     // Help text is the same no matter whether there is SIM or not.
     86                     empty.setText(context.getText(R.string.noContactsHelpTextForCreateShortcut));
     87                 } else if (hasSim) {
     88                     empty.setText(context.getText(R.string.noContactsHelpText));
     89                 } else {
     90                     empty.setText(context.getText(R.string.noContactsNoSimHelpText));
     91                 }
     92             }
     93         }
     94         empty.setVisibility(VISIBLE);
     95     }
     96 
     97     private boolean isSyncActive() {
     98         Account[] accounts = AccountManager.get(getContext()).getAccounts();
     99         if (accounts != null && accounts.length > 0) {
    100             IContentService contentService = ContentResolver.getContentService();
    101             for (Account account : accounts) {
    102                 try {
    103                     if (contentService.isSyncActive(account, ContactsContract.AUTHORITY)) {
    104                         return true;
    105                     }
    106                 } catch (RemoteException e) {
    107                     Log.e(TAG, "Could not get the sync status");
    108                 }
    109             }
    110         }
    111         return false;
    112     }
    113 }
    114