📄 contactlistview.java
字号:
/* * Copyright (C) 2007-2008 Esmertec AG. * Copyright (C) 2007-2008 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.im.app;import com.android.im.IChatSession;import com.android.im.IChatSessionManager;import com.android.im.IContactListListener;import com.android.im.IContactListManager;import com.android.im.IImConnection;import com.android.im.ISubscriptionListener;import com.android.im.R;import com.android.im.app.adapter.ContactListListenerAdapter;import com.android.im.engine.Contact;import com.android.im.engine.ContactListManager;import com.android.im.engine.ImErrorInfo;import com.android.im.service.ImServiceConstants;import android.app.Activity;import android.app.AlertDialog;import android.content.ContentUris;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.content.res.Resources;import android.database.Cursor;import android.net.Uri;import android.os.Parcel;import android.os.Parcelable;import android.os.RemoteException;import android.provider.Im;import android.util.AttributeSet;import android.util.Log;import android.view.View;import android.widget.ExpandableListView;import android.widget.LinearLayout;import android.widget.ExpandableListView.OnChildClickListener;public class ContactListView extends LinearLayout { Activity mScreen; IImConnection mConn; SimpleAlertHandler mHandler; private final IContactListListener mContactListListener; UserPresenceView mPresenceView; ExpandableListView mContactsList; private ContactListTreeAdapter mAdapter; private boolean mHideOfflineContacts; private SavedState mSavedState; public ContactListView(Context screen, AttributeSet attrs) { super(screen, attrs); mScreen = (Activity)screen; mHandler = new SimpleAlertHandler(mScreen); mContactListListener = new MyContactListListener(mHandler); } private class MyContactListListener extends ContactListListenerAdapter { public MyContactListListener(SimpleAlertHandler handler) { super(handler); } @Override public void onAllContactListsLoaded() { if (mAdapter != null) { mAdapter.startAutoRequery(); } } } private final ISubscriptionListener.Stub mSubscriptionListener = new ISubscriptionListener.Stub() { public void onSubScriptionRequest(Contact from) { querySubscription(); } public void onSubscriptionApproved(String contact) { querySubscription(); } public void onSubscriptionDeclined(String contact) { querySubscription(); } private void querySubscription() { if (mAdapter != null) { mAdapter.startQuerySubscriptions(); } } }; @Override protected void onFinishInflate() { super.onFinishInflate(); mPresenceView = (UserPresenceView)findViewById(R.id.userPresence); mContactsList = (ExpandableListView) findViewById(R.id.contactsList); mContactsList.setOnChildClickListener(mOnChildClickListener); } public ExpandableListView getListView() { return mContactsList; } public void setConnection(IImConnection conn) { if (mConn != conn) { if (mConn != null) { unregisterListeners(); } mConn = conn; if (conn != null) { registerListeners(); mPresenceView.setConnection(conn); if (mAdapter == null) { mAdapter = new ContactListTreeAdapter(conn, mScreen); mAdapter.setHideOfflineContacts(mHideOfflineContacts); mContactsList.setAdapter(mAdapter); mContactsList.setOnScrollListener(mAdapter); if (mSavedState != null) { int[] expandedGroups = mSavedState.mExpandedGroups; if(expandedGroups != null) { for (int group : expandedGroups) { mContactsList.expandGroup(group); } } } } else { mAdapter.changeConnection(conn); } try { IContactListManager listMgr = conn.getContactListManager(); if (listMgr.getState() == ContactListManager.LISTS_LOADED) { mAdapter.startAutoRequery(); } } catch (RemoteException e) { Log.e(ImApp.LOG_TAG, "Service died!"); } } } else { mContactsList.invalidateViews(); } } public void setHideOfflineContacts(boolean hide) { if (mAdapter != null) { mAdapter.setHideOfflineContacts(hide); } else { mHideOfflineContacts = hide; } } public void startChat() { startChat(getSelectedContact()); } public void startChatAtPosition(long packedPosition) { startChat(getContactAtPosition(packedPosition)); } void startChat(Cursor c) { if (c != null) { long id = c.getLong(c.getColumnIndexOrThrow(Im.Contacts._ID)); String username = c.getString(c.getColumnIndexOrThrow(Im.Contacts.USERNAME)); try { IChatSessionManager manager = mConn.getChatSessionManager(); IChatSession session = manager.getChatSession(username); if(session == null) { manager.createChatSession(username); } Uri data = ContentUris.withAppendedId(Im.Chats.CONTENT_URI, id); Intent i = new Intent(Intent.ACTION_VIEW, data); mScreen.startActivity(i); } catch (RemoteException e) { mHandler.showServiceErrorAlert(); } clearFocusIfEmpty(c); } } private void clearFocusIfEmpty(Cursor c) { // clear focus if there's only one item so that it would focus on the // "empty" item after the contact removed. if (c.getCount() == 1) { clearFocus(); } } public void endChat() { endChat(getSelectedContact()); } public void endChatAtPosition(long packedPosition) { endChat(getContactAtPosition(packedPosition)); } void endChat(Cursor c) { if(c != null) { String username = c.getString(c.getColumnIndexOrThrow(Im.Contacts.USERNAME)); try { IChatSessionManager manager = mConn.getChatSessionManager(); IChatSession session = manager.getChatSession(username); if(session != null) { session.leave(); } } catch (RemoteException e) { mHandler.showServiceErrorAlert(); } clearFocusIfEmpty(c); } } public void viewContactPresence() { viewContactPresence(getSelectedContact()); } public void viewContactPresenceAtPostion(long packedPosition) { viewContactPresence(getContactAtPosition(packedPosition)); } public void viewContactPresence(Cursor c) { if (c != null) { long id = c.getLong(c.getColumnIndexOrThrow(Im.Contacts._ID)); Uri data = ContentUris.withAppendedId(Im.Contacts.CONTENT_URI, id); Intent i = new Intent(Intent.ACTION_VIEW, data); mScreen.startActivity(i); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -