📄 chatview.java
字号:
Cursor cursor = getMessageCursor(); if (cursor == null) { startQuery(); } else { requeryCursor(); } updateWarningView(); } registerChatListener(); registerForConnEvents(); } public void onPause(){ Cursor cursor = getMessageCursor(); if (cursor != null) { cursor.deactivate(); } cancelRequery(); if (mViewType == VIEW_TYPE_CHAT) { markAsRead(); } unregisterChatListener(); unregisterForConnEvents(); unregisterChatSessionListener(); } void updateChat() { setViewType(VIEW_TYPE_CHAT); long oldChatId = mChatId; updateContactInfo(); setStatusIcon(); setTitle(); IImConnection conn = mApp.getConnection(mProviderId); if (conn == null) { if (Log.isLoggable(ImApp.LOG_TAG, Log.DEBUG)) log("Connection has been signed out"); mScreen.finish(); return; } BrandingResources brandingRes = mApp.getBrandingResource(mProviderId); mHistory.setBackgroundDrawable( brandingRes.getDrawable(BrandingResourceIDs.DRAWABLE_CHAT_WATERMARK)); if (mMarkup == null) { mMarkup = new Markup(brandingRes); } if (mMessageAdapter == null) { mMessageAdapter = new MessageAdapter(mScreen, null); mHistory.setAdapter(mMessageAdapter); } // only change the message adapter when we switch to another chat if (mChatId != oldChatId) { startQuery(); mEdtInput.setText(""); } updateWarningView(); } private void updateContactInfo() { mChatId = mCursor.getLong(CONTACT_ID_COLUMN); mProviderId = mCursor.getLong(PROVIDER_COLUMN); mAccountId = mCursor.getLong(ACCOUNT_COLUMN); mPresenceStatus = mCursor.getInt(PRESENCE_STATUS_COLUMN); mType = mCursor.getInt(TYPE_COLUMN); mUserName = mCursor.getString(USERNAME_COLUMN); mNickName = mCursor.getString(NICKNAME_COLUMN); } private void setTitle() { if (mType == Im.Contacts.TYPE_GROUP) { final String[] projection = {Im.GroupMembers.NICKNAME}; Uri memberUri = ContentUris.withAppendedId(Im.GroupMembers.CONTENT_URI, mChatId); ContentResolver cr = mScreen.getContentResolver(); Cursor c = cr.query(memberUri, projection, null, null, null); StringBuilder buf = new StringBuilder(); if(c != null) { while(c.moveToNext()) { buf.append(c.getString(0)); if(!c.isLast()) { buf.append(','); } } c.close(); } mTitle.setText(mContext.getString(R.string.chat_with, buf.toString())); } else { mTitle.setText(mContext.getString(R.string.chat_with, mNickName)); } } private void setStatusIcon() { if (mType == Im.Contacts.TYPE_GROUP) { // hide the status icon for group chat. mStatusIcon.setVisibility(GONE); } else { mStatusIcon.setVisibility(VISIBLE); BrandingResources brandingRes = mApp.getBrandingResource(mProviderId); int presenceResId = PresenceUtils.getStatusIconId(mPresenceStatus); mStatusIcon.setImageDrawable(brandingRes.getDrawable(presenceResId)); } } public void bindChat(long chatId) { if (mCursor != null) { mCursor.deactivate(); } Uri contactUri = ContentUris.withAppendedId(Im.Contacts.CONTENT_URI, chatId); mCursor = mScreen.managedQuery(contactUri, CHAT_PROJECTION, null, null); if (mCursor == null || !mCursor.moveToFirst()) { if (Log.isLoggable(ImApp.LOG_TAG, Log.DEBUG)){ log("Failed to query chat: " + chatId); } mScreen.finish(); return; } else { mChatSession = getChatSession(mCursor); updateChat(); registerChatListener(); } } public void bindInvitation(long invitationId) { Uri uri = ContentUris.withAppendedId(Im.Invitation.CONTENT_URI, invitationId); ContentResolver cr = mScreen.getContentResolver(); Cursor cursor = cr.query(uri, INVITATION_PROJECT, null, null, null); if (cursor == null || !cursor.moveToFirst()) { if (Log.isLoggable(ImApp.LOG_TAG, Log.DEBUG)){ log("Failed to query invitation: " + invitationId); } mScreen.finish(); } else { setViewType(VIEW_TYPE_INVITATION); mInvitationId = cursor.getLong(INVITATION_ID_COLUMN); mProviderId = cursor.getLong(INVITATION_PROVIDER_COLUMN); String sender = cursor.getString(INVITATION_SENDER_COLUMN); TextView mInvitationText = (TextView)findViewById(R.id.txtInvitation); mInvitationText.setText(mContext.getString(R.string.invitation_prompt, sender)); mTitle.setText(mContext.getString(R.string.chat_with, sender)); } if (cursor != null) { cursor.close(); } } public void bindSubscription(long providerId, String from) { mProviderId = providerId; mUserName = from; setViewType(VIEW_TYPE_SUBSCRIPTION); TextView text = (TextView)findViewById(R.id.txtSubscription); String displayableAddr = ImpsAddressUtils.getDisplayableAddress(from); text.setText(mContext.getString(R.string.subscription_prompt, displayableAddr)); mTitle.setText(mContext.getString(R.string.chat_with, displayableAddr)); } void acceptInvitation() { try { IImConnection conn = mApp.getConnection(mProviderId); if (conn != null) { // register a chat session listener and wait for a group chat // session to be created after we accept the invitation. registerChatSessionListener(); conn.acceptInvitation(mInvitationId); } } catch (RemoteException e) { mHandler.showServiceErrorAlert(); } } void declineInvitation() { try { IImConnection conn = mApp.getConnection(mProviderId); if (conn != null) { conn.rejectInvitation(mInvitationId); } mScreen.finish(); } catch (RemoteException e) { mHandler.showServiceErrorAlert(); } } void approveSubscription() { IImConnection conn = mApp.getConnection(mProviderId); try { IContactListManager manager = conn.getContactListManager(); manager.approveSubscription(mUserName); } catch (RemoteException ex) { mHandler.showServiceErrorAlert(); } mScreen.finish(); } void declineSubscription() { IImConnection conn = mApp.getConnection(mProviderId); try { IContactListManager manager = conn.getContactListManager(); manager.declineSubscription(mUserName); } catch (RemoteException ex) { mHandler.showServiceErrorAlert(); } mScreen.finish(); } private void setViewType(int type) { mViewType = type; if (type == VIEW_TYPE_CHAT) { findViewById(R.id.invitationPanel).setVisibility(GONE); findViewById(R.id.subscription).setVisibility(GONE); setChatViewEnabled(true); } else if (type == VIEW_TYPE_INVITATION) { setChatViewEnabled(false); findViewById(R.id.invitationPanel).setVisibility(VISIBLE); findViewById(R.id.btnAccept).requestFocus(); } else if (type == VIEW_TYPE_SUBSCRIPTION) { setChatViewEnabled(false); findViewById(R.id.subscription).setVisibility(VISIBLE); findViewById(R.id.btnApproveSubscription).requestFocus(); } } private void setChatViewEnabled(boolean enabled) { mEdtInput.setEnabled(enabled); mSendButton.setEnabled(enabled); if (enabled) { mEdtInput.requestFocus(); } else { mHistory.setAdapter(null); } } private void markAsRead() { ContentValues values = new ContentValues(1); values.put(Im.Chats.LAST_UNREAD_MESSAGE, (String)null); ContentResolver cr = mContext.getContentResolver(); Uri uri = ContentUris.withAppendedId(Im.Chats.CONTENT_URI, mChatId); cr.update(uri, values, null, null); } private void startQuery() { if (mQueryHandler == null) { mQueryHandler = new QueryHandler(mContext); } else { // Cancel any pending queries mQueryHandler.cancelOperation(QUERY_TOKEN); } Uri uri; if (Im.Contacts.TYPE_GROUP == mType) { uri = ContentUris.withAppendedId(Im.GroupMessages.CONTENT_URI_GROUP_MESSAGES_BY, mChatId); } else { uri = Im.Messages.getContentUriByContact(mProviderId, mAccountId, mUserName); } if (Log.isLoggable(ImApp.LOG_TAG, Log.DEBUG)){ log("queryCursor: uri=" + uri); } mQueryHandler.startQuery(QUERY_TOKEN, null, uri, null, null /* selection */, null /* selection args */, null); } void scheduleRequery(long interval) { if (mRequeryCallback == null) { mRequeryCallback = new RequeryCallback(); } else { mHandler.removeCallbacks(mRequeryCallback); } if (Log.isLoggable(ImApp.LOG_TAG, Log.DEBUG)){ log("scheduleRequery"); } mHandler.postDelayed(mRequeryCallback, interval); } void cancelRequery() { if (mRequeryCallback != null) { if (Log.isLoggable(ImApp.LOG_TAG, Log.DEBUG)){ log("cancelRequery"); } mHandler.removeCallbacks(mRequeryCallback); mRequeryCallback = null; } } void requeryCursor() { if (mMessageAdapter.isScrolling()) { mMessageAdapter.setNeedRequeryCursor(true); return; } // TODO: async query? Cursor cursor = getMessageCursor(); if (cursor != null) { cursor.requery(); } } private Cursor getMessageCursor() { return mMessageAdapter == null ? null : mMessageAdapter.getCursor(); } public void insertSmiley(String smiley) { mEdtInput.append(mMarkup.applyEmoticons(smiley)); } public void closeChatSession() { if (mChatSession != null) { try { mChatSession.leave(); } catch (RemoteException e) { mHandler.showServiceErrorAlert(); } } else { // the conversation is already closed, clear data in database ContentResolver cr = mContext.getContentResolver(); cr.delete(ContentUris.withAppendedId(Im.Chats.CONTENT_URI, mChatId), null, null); } mScreen.finish(); } public void viewProfile() { Uri data = ContentUris.withAppendedId(Im.Contacts.CONTENT_URI, mChatId); Intent intent = new Intent(Intent.ACTION_VIEW, data); mScreen.startActivity(intent); } public void blockContact() { // TODO: unify with codes in ContactListView DialogInterface.OnClickListener confirmListener = new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int whichButton) { try { IImConnection conn = mApp.getConnection(mProviderId); IContactListManager manager = conn.getContactListManager(); manager.blockContact(mUserName); mScreen.finish(); } catch (RemoteException e) { mHandler.showServiceErrorAlert(); } } }; Resources r = getResources(); // The positive button is deliberately set as no so that // the no is the default value new AlertDialog.Builder(mContext)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -