/* Copyright © 2013-2014, Silent Circle, LLC. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Any redistribution, use, or modification is done solely for personal benefit and not for any commercial purpose or for monetary gain * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name Silent Circle nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SILENT CIRCLE, LLC BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* * Copyright (C) 2011 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.silentcircle.contacts.group; import android.annotation.TargetApi; import android.content.ContentUris; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.os.Build; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.silentcircle.contacts.GroupListLoader; import com.silentcircle.contacts.R; import com.silentcircle.silentcontacts.ScContactsContract.Groups; /** * Adapter to populate the list of groups. */ public class GroupBrowseListAdapter extends BaseAdapter { private final Context mContext; private final LayoutInflater mLayoutInflater; private Cursor mCursor; private boolean mSelectionVisible; private Uri mSelectedGroupUri; public GroupBrowseListAdapter(Context context) { mContext = context; mLayoutInflater = LayoutInflater.from(context); } public void setCursor(Cursor cursor) { mCursor = cursor; // If there's no selected group already and the cursor is valid, then by default, select the // first group if (mSelectedGroupUri == null && cursor != null && cursor.getCount() > 0) { GroupListItem firstItem = getItem(0); long groupId = (firstItem == null) ? null : firstItem.getGroupId(); mSelectedGroupUri = getGroupUriFromId(groupId); } notifyDataSetChanged(); } public int getSelectedGroupPosition() { if (mSelectedGroupUri == null || mCursor == null || mCursor.getCount() == 0) { return -1; } int index = 0; mCursor.moveToPosition(-1); while (mCursor.moveToNext()) { long groupId = mCursor.getLong(GroupListLoader.GROUP_ID); Uri uri = getGroupUriFromId(groupId); if (mSelectedGroupUri.equals(uri)) { return index; } index++; } return -1; } public void setSelectionVisible(boolean flag) { mSelectionVisible = flag; } public void setSelectedGroup(Uri groupUri) { mSelectedGroupUri = groupUri; } private boolean isSelectedGroup(Uri groupUri) { return mSelectedGroupUri != null && mSelectedGroupUri.equals(groupUri); } public Uri getSelectedGroup() { return mSelectedGroupUri; } @Override public int getCount() { return mCursor == null ? 0 : mCursor.getCount(); } @Override public long getItemId(int position) { return position; } @Override public GroupListItem getItem(int position) { if (mCursor == null || mCursor.isClosed() || !mCursor.moveToPosition(position)) { return null; } long groupId = mCursor.getLong(GroupListLoader.GROUP_ID); String title = mCursor.getString(GroupListLoader.TITLE); int memberCount = mCursor.getInt(GroupListLoader.MEMBER_COUNT); return new GroupListItem(null, null, null, groupId, title, true, memberCount); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override public View getView(int position, View convertView, ViewGroup parent) { GroupListItem entry = getItem(position); View result; GroupListItemViewCache viewCache; if (convertView != null) { result = convertView; viewCache = (GroupListItemViewCache) result.getTag(); } else { result = mLayoutInflater.inflate(R.layout.group_browse_list_item, parent, false); viewCache = new GroupListItemViewCache(result); result.setTag(viewCache); } // Add a header if this is the first group in an account and hide the divider if (entry.isFirstGroupInAccount()) { viewCache.divider.setVisibility(View.GONE); } else { viewCache.divider.setVisibility(View.VISIBLE); } // Bind the group data Uri groupUri = getGroupUriFromId(entry.getGroupId()); String memberCountString = mContext.getResources().getQuantityString( R.plurals.group_list_num_contacts_in_group, entry.getMemberCount(), entry.getMemberCount()); viewCache.setUri(groupUri); viewCache.groupTitle.setText(entry.getTitle()); viewCache.groupMemberCount.setText(memberCountString); if (mSelectionVisible) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) result.setActivated(isSelectedGroup(groupUri)); } return result; } private static Uri getGroupUriFromId(long groupId) { return ContentUris.withAppendedId(Groups.CONTENT_URI, groupId); } /** * Cache of the children views of a contact detail entry represented by a * {@link GroupListItem} */ public static class GroupListItemViewCache { public final TextView groupTitle; public final TextView groupMemberCount; public final View divider; private Uri mUri; public GroupListItemViewCache(View view) { groupTitle = (TextView) view.findViewById(R.id.label); groupMemberCount = (TextView) view.findViewById(R.id.count); divider = view.findViewById(R.id.divider); } public void setUri(Uri uri) { mUri = uri; } public Uri getUri() { return mUri; } } }