1e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project/*
2e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * Copyright (C) 2008 Esmertec AG.
3e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
4e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project *
5e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * use this file except in compliance with the License. You may obtain a copy of
7e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * the License at
8e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project *
9e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0
10e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project *
11e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
12e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * License for the specific language governing permissions and limitations under
15e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project * the License.
16e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project */
17e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectpackage com.android.im.app;
18e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
19e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport static com.android.im.service.ImServiceConstants.ACTION_MANAGE_SUBSCRIPTION;
20e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport static com.android.im.service.ImServiceConstants.EXTRA_INTENT_ACCOUNT_ID;
21e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport static com.android.im.service.ImServiceConstants.EXTRA_INTENT_FROM_ADDRESS;
22e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport static com.android.im.service.ImServiceConstants.EXTRA_INTENT_PROVIDER_ID;
23e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport static com.android.im.service.ImServiceConstants.EXTRA_INTENT_SHOW_MULTIPLE;
24e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
25e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport com.android.im.IChatSession;
26e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport com.android.im.R;
27e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport com.android.im.app.adapter.ChatListenerAdapter;
28e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport com.android.im.plugin.BrandingResourceIDs;
29c84f5b8faa3fac30fabe6b21ff30b7c98a36832eScott Suimport com.android.im.provider.Imps;
30e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
31e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport android.app.Activity;
32e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport android.app.AlertDialog;
33e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport android.content.ContentResolver;
34e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport android.content.ContentUris;
35e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport android.content.DialogInterface;
36e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport android.content.Intent;
37e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport android.database.Cursor;
38e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport android.graphics.drawable.Drawable;
39e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport android.net.Uri;
40e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport android.os.Bundle;
41e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport android.os.Handler;
42e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport android.os.RemoteException;
43e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport android.view.KeyEvent;
44d9ff158f5ed2c07967c2e14e6fa60ac7e5b20c4dThe Android Open Source Projectimport android.view.LayoutInflater;
45e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport android.view.Menu;
46e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport android.view.MenuInflater;
47e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport android.view.MenuItem;
48e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport android.view.View;
49d9ff158f5ed2c07967c2e14e6fa60ac7e5b20c4dThe Android Open Source Projectimport android.view.ViewGroup;
50e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport android.view.Window;
51e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport android.widget.ImageView;
52e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport android.widget.SimpleAdapter;
53e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport android.widget.Toast;
54e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
55e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport java.util.ArrayList;
56e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport java.util.HashMap;
57e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport java.util.List;
58e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectimport java.util.Map;
59e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
60e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Projectpublic class NewChatActivity extends Activity {
61e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
62e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    private static final int REQUEST_PICK_CONTACTS = RESULT_FIRST_USER + 1;
63e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
64e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    ImApp mApp;
65e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
66e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    ChatView mChatView;
67e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    SimpleAlertHandler mHandler;
68e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
69e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    private AlertDialog mSmileyDialog;
70d9ff158f5ed2c07967c2e14e6fa60ac7e5b20c4dThe Android Open Source Project    private ChatSwitcher mChatSwitcher;
71d9ff158f5ed2c07967c2e14e6fa60ac7e5b20c4dThe Android Open Source Project
72d9ff158f5ed2c07967c2e14e6fa60ac7e5b20c4dThe Android Open Source Project    private LayoutInflater mInflater;
73e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
74e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    @Override
75e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    protected void onCreate(Bundle icicle) {
76e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        super.onCreate(icicle);
77e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
78e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        requestWindowFeature(Window.FEATURE_NO_TITLE);
79e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
80e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        setContentView(R.layout.chat_view);
81e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
82e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        mChatView = (ChatView) findViewById(R.id.chatView);
83e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        mHandler = mChatView.mHandler;
84d9ff158f5ed2c07967c2e14e6fa60ac7e5b20c4dThe Android Open Source Project        mInflater = LayoutInflater.from(this);
85d9ff158f5ed2c07967c2e14e6fa60ac7e5b20c4dThe Android Open Source Project
86d9ff158f5ed2c07967c2e14e6fa60ac7e5b20c4dThe Android Open Source Project        mApp = ImApp.getApplication(this);
87d9ff158f5ed2c07967c2e14e6fa60ac7e5b20c4dThe Android Open Source Project        mChatSwitcher = new ChatSwitcher(this, mHandler, mApp, mInflater, null);
88e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
89e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        final Handler handler = new Handler();
90e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        mApp.callWhenServiceConnected(handler, new Runnable() {
91e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            public void run() {
92e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                resolveIntent(getIntent());
93e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            }
94e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        });
95e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
96e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
97e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    @Override
98e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    protected void onResume() {
99e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        super.onResume();
100e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        mChatView.onResume();
101e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
102e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
103e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    @Override
104e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    protected void onPause() {
105e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        mChatView.onPause();
106e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        super.onPause();
107e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
108e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
109e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    @Override
110e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    protected void onNewIntent(Intent intent) {
111e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        resolveIntent(intent);
112e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
113e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
114e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    void resolveIntent(Intent intent) {
115e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        if (requireOpenDashboardOnStart(intent)) {
116e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            long providerId = intent.getLongExtra(EXTRA_INTENT_PROVIDER_ID, -1L);
117e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            final long accountId = intent.getLongExtra(EXTRA_INTENT_ACCOUNT_ID, -1L);
118e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            if (providerId == -1L || accountId == -1L) {
119e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                finish();
120e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            } else {
121d9ff158f5ed2c07967c2e14e6fa60ac7e5b20c4dThe Android Open Source Project                mChatSwitcher.open();
122e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            }
123e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            return;
124e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        }
125e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
126e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        if (ACTION_MANAGE_SUBSCRIPTION.equals(intent.getAction())) {
127e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            long providerId = intent.getLongExtra(EXTRA_INTENT_PROVIDER_ID, -1);
128e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            String from = intent.getStringExtra(EXTRA_INTENT_FROM_ADDRESS);
129e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            if ((providerId == -1) || (from == null)) {
130e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                finish();
131e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            } else {
132e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                mChatView.bindSubscription(providerId, from);
133e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            }
134e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        } else {
135e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            Uri data = intent.getData();
136e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            String type = getContentResolver().getType(data);
137c84f5b8faa3fac30fabe6b21ff30b7c98a36832eScott Su            if (Imps.Chats.CONTENT_ITEM_TYPE.equals(type)) {
138e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                mChatView.bindChat(ContentUris.parseId(data));
139c84f5b8faa3fac30fabe6b21ff30b7c98a36832eScott Su            } else if (Imps.Invitation.CONTENT_ITEM_TYPE.equals(type)) {
140e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                mChatView.bindInvitation(ContentUris.parseId(data));
141e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            }
142e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        }
143e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
144e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
145e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    @Override
146e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    public boolean onCreateOptionsMenu(Menu menu) {
147e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        MenuInflater inflater = getMenuInflater();
148e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        inflater.inflate(R.menu.chat_screen_menu, menu);
149e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
150e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        long providerId = mChatView.getProviderId();
151e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        BrandingResources brandingRes = mApp.getBrandingResource(providerId);
152e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        menu.findItem(R.id.menu_view_friend_list).setTitle(
153e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                brandingRes.getString(BrandingResourceIDs.STRING_MENU_CONTACT_LIST));
154e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        menu.findItem(R.id.menu_switch_chats).setTitle(
155e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                brandingRes.getString(BrandingResourceIDs.STRING_MENU_SWITCH_CHATS));
156e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        menu.findItem(R.id.menu_insert_smiley).setTitle(
157e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                brandingRes.getString(BrandingResourceIDs.STRING_MENU_INSERT_SMILEY));
158e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        menu.findItem(R.id.menu_end_conversation).setTitle(
159e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                brandingRes.getString(BrandingResourceIDs.STRING_MENU_END_CHAT));
160e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        menu.findItem(R.id.menu_view_profile).setTitle(
161e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                brandingRes.getString(BrandingResourceIDs.STRING_MENU_VIEW_PROFILE));
162e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        menu.findItem(R.id.menu_block_contact).setTitle(
163e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                brandingRes.getString(BrandingResourceIDs.STRING_MENU_BLOCK_CONTACT));
164e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        return true;
165e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
166e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
167e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    @Override
168e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    public boolean onPrepareOptionsMenu(Menu menu) {
169e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        super.onPrepareOptionsMenu(menu);
170e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
171e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        //XXX hide the invite menu, group chat is not supported by the server.
172e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        menu.findItem(R.id.menu_invite_contact).setVisible(false);
173e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
174e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        //XXX HACK: Yahoo! doesn't allow to block a friend. We can only block a temporary contact.
175e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        ProviderDef provider = mApp.getProvider(mChatView.getProviderId());
176c84f5b8faa3fac30fabe6b21ff30b7c98a36832eScott Su        if ((provider != null) && Imps.ProviderNames.YAHOO.equals(provider.mName)) {
177c84f5b8faa3fac30fabe6b21ff30b7c98a36832eScott Su            if (Imps.Contacts.TYPE_TEMPORARY != mChatView.mType) {
178e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                menu.findItem(R.id.menu_block_contact).setVisible(false);
179e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            }
180e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        }
181e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        return true;
182e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
183e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
184e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    @Override
185e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    public boolean onOptionsItemSelected(MenuItem item) {
186e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        switch (item.getItemId()) {
187e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            case R.id.menu_view_friend_list:
188e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                finish();
189e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                showRosterScreen();
190e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                return true;
191e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
192e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            case R.id.menu_insert_smiley:
193e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                showSmileyDialog();
194e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                return true;
195e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
196e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            case R.id.menu_end_conversation:
197e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                mChatView.closeChatSession();
198e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                return true;
199e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
200e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            case R.id.menu_switch_chats:
201d9ff158f5ed2c07967c2e14e6fa60ac7e5b20c4dThe Android Open Source Project                if (mChatSwitcher.isOpen()) {
202d9ff158f5ed2c07967c2e14e6fa60ac7e5b20c4dThe Android Open Source Project                    mChatSwitcher.close();
203d9ff158f5ed2c07967c2e14e6fa60ac7e5b20c4dThe Android Open Source Project                } else {
204d9ff158f5ed2c07967c2e14e6fa60ac7e5b20c4dThe Android Open Source Project                    mChatSwitcher.open();
205d9ff158f5ed2c07967c2e14e6fa60ac7e5b20c4dThe Android Open Source Project                }
206d9ff158f5ed2c07967c2e14e6fa60ac7e5b20c4dThe Android Open Source Project
207e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                return true;
208e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
209e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            case R.id.menu_invite_contact:
210e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                startContactPicker();
211e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                return true;
212e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
213e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            case R.id.menu_view_profile:
214e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                mChatView.viewProfile();
215e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                return true;
216e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
217e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            case R.id.menu_block_contact:
218e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                mChatView.blockContact();
219e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                return true;
220e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
221e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            case R.id.menu_prev_chat:
222e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                switchChat(-1);
223e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                return true;
224e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
225e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            case R.id.menu_next_chat:
226e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                switchChat(1);
227e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                return true;
228e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
229e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            case R.id.menu_quick_switch_0:
230e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            case R.id.menu_quick_switch_1:
231e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            case R.id.menu_quick_switch_2:
232e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            case R.id.menu_quick_switch_3:
233e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            case R.id.menu_quick_switch_4:
234e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            case R.id.menu_quick_switch_5:
235e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            case R.id.menu_quick_switch_6:
236e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            case R.id.menu_quick_switch_7:
237e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            case R.id.menu_quick_switch_8:
238e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            case R.id.menu_quick_switch_9:
239d9ff158f5ed2c07967c2e14e6fa60ac7e5b20c4dThe Android Open Source Project                mChatSwitcher.handleShortcut(item.getAlphabeticShortcut());
240e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                return true;
241e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        }
242e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
243e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        return super.onOptionsItemSelected(item);
244e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
245e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
246e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    @Override
247e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    public boolean dispatchKeyEvent(KeyEvent event) {
248e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
249e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                && event.getAction() == KeyEvent.ACTION_DOWN) {
250e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            mChatView.closeChatSessionIfInactive();
251e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        }
252e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        return super.dispatchKeyEvent(event);
253e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
254e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
255e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    /**
256e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     * Check whether we are asked to open Dashboard on startup.
257e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project     */
258e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    private boolean requireOpenDashboardOnStart(Intent intent) {
259e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        return intent.getBooleanExtra(EXTRA_INTENT_SHOW_MULTIPLE, false);
260e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
261e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
262e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    private void showRosterScreen() {
263e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        Intent intent = new Intent(Intent.ACTION_VIEW);
264e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        intent.setClass(this, ContactListActivity.class);
265e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        intent.putExtra(EXTRA_INTENT_ACCOUNT_ID, mChatView.getAccountId());
266e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        startActivity(intent);
267e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
268e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
269e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    private void showSmileyDialog() {
270e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        if (mSmileyDialog == null) {
271e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            long providerId = mChatView.getProviderId();
272e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
273e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            final BrandingResources brandingRes = mApp.getBrandingResource(providerId);
274e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            int[] icons = brandingRes.getSmileyIcons();
275e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            String[] names = brandingRes.getStringArray(
276e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    BrandingResourceIDs.STRING_ARRAY_SMILEY_NAMES);
277e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            final String[] texts = brandingRes.getStringArray(
278e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    BrandingResourceIDs.STRING_ARRAY_SMILEY_TEXTS);
279e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
280e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            final int N = names.length;
281e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
282e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            List<Map<String, ?>> entries = new ArrayList<Map<String, ?>>();
283e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            for (int i = 0; i < N; i++) {
284e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                // We might have different ASCII for the same icon, skip it if
285e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                // the icon is already added.
286e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                boolean added = false;
287e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                for (int j = 0; j < i; j++) {
288e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    if (icons[i] == icons[j]) {
289e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                        added = true;
290e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                        break;
291e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    }
292e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                }
293e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                if (!added) {
294e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    HashMap<String, Object> entry = new HashMap<String, Object>();
295e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
296e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    entry. put("icon", icons[i]);
297e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    entry. put("name", names[i]);
298e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    entry.put("text", texts[i]);
299e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
300e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    entries.add(entry);
301e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                }
302e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            }
303e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
304e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            final SimpleAdapter a = new SimpleAdapter(
305e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    this,
306e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    entries,
307e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    R.layout.smiley_menu_item,
308e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    new String[] {"icon", "name", "text"},
309e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    new int[] {R.id.smiley_icon, R.id.smiley_name, R.id.smiley_text});
310e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() {
311e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                public boolean setViewValue(View view, Object data, String textRepresentation) {
312e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    if (view instanceof ImageView) {
313e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                        Drawable img = brandingRes.getSmileyIcon((Integer)data);
314e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                        ((ImageView)view).setImageDrawable(img);
315e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                        return true;
316e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    }
317e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    return false;
318e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                }
319e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            };
320e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            a.setViewBinder(viewBinder);
321e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
322e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            AlertDialog.Builder b = new AlertDialog.Builder(this);
323e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
324e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            b.setTitle(brandingRes.getString(
325e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    BrandingResourceIDs.STRING_MENU_INSERT_SMILEY));
326e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
327e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            b.setCancelable(true);
328e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            b.setAdapter(a, new DialogInterface.OnClickListener() {
329e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                public final void onClick(DialogInterface dialog, int which) {
330e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    HashMap<String, Object> item = (HashMap<String, Object>) a.getItem(which);
331e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    mChatView.insertSmiley((String)item.get("text"));
332e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                }
333e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            });
334e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
335e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            mSmileyDialog = b.create();
336e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        }
337e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
338e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        mSmileyDialog.show();
339e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
340e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
341e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    private void switchChat(int delta) {
342d9ff158f5ed2c07967c2e14e6fa60ac7e5b20c4dThe Android Open Source Project        long providerId = mChatView.getProviderId();
343d9ff158f5ed2c07967c2e14e6fa60ac7e5b20c4dThe Android Open Source Project        long accountId = mChatView.getAccountId();
344d9ff158f5ed2c07967c2e14e6fa60ac7e5b20c4dThe Android Open Source Project        String contact = mChatView.getUserName();
345e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
346d9ff158f5ed2c07967c2e14e6fa60ac7e5b20c4dThe Android Open Source Project        mChatSwitcher.rotateChat(delta, contact, accountId, providerId);
347e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
348e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
349e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    private void startContactPicker() {
350c84f5b8faa3fac30fabe6b21ff30b7c98a36832eScott Su        Uri.Builder builder = Imps.Contacts.CONTENT_URI_ONLINE_CONTACTS_BY.buildUpon();
351e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        ContentUris.appendId(builder, mChatView.getProviderId());
352e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        ContentUris.appendId(builder, mChatView.getAccountId());
353e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        Uri data = builder.build();
354e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
355e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        try {
356e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            Intent i = new Intent(Intent.ACTION_PICK, data);
357e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            i.putExtra(ContactsPickerActivity.EXTRA_EXCLUDED_CONTACTS,
358e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    mChatView.getCurrentChatSession().getPariticipants());
359e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            startActivityForResult(i, REQUEST_PICK_CONTACTS);
360e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        } catch (RemoteException e) {
361e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            mHandler.showServiceErrorAlert();
362e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        }
363e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
364e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
365e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    @Override
366e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    protected void onActivityResult(int requestCode, int resultCode,
367e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            Intent data) {
368e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        if (resultCode == RESULT_OK) {
369e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            if (requestCode == REQUEST_PICK_CONTACTS) {
370e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                String username = data.getStringExtra(
371e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                        ContactsPickerActivity.EXTRA_RESULT_USERNAME);
372e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                try {
373e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    IChatSession chatSession = mChatView.getCurrentChatSession();
374e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    if (chatSession.isGroupChatSession()) {
375e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                        chatSession.inviteContact(username);
376e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                        showInvitationHasSent(username);
377e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    } else {
378e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                        chatSession.convertToGroupChat();
379e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                        new ContactInvitor(chatSession, username).start();
380e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    }
381e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                } catch (RemoteException e) {
382e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    mHandler.showServiceErrorAlert();
383e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                }
384e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            }
385e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        }
386e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
387e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
388e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    void showInvitationHasSent(String contact) {
389e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        Toast.makeText(NewChatActivity.this,
390e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                getString(R.string.invitation_sent_prompt, contact),
391e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                Toast.LENGTH_SHORT).show();
392e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
393e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
394e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    private class ContactInvitor extends ChatListenerAdapter {
395e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        private final IChatSession mChatSession;
396e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        String mContact;
397e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
398e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        public ContactInvitor(IChatSession session, String data) {
399e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            mChatSession = session;
400e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            mContact = data;
401e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        }
402e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
403e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        @Override
404e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        public void onConvertedToGroupChat(IChatSession ses) {
405e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            try {
406e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                final long chatId = mChatSession.getId();
407e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                mChatSession.inviteContact(mContact);
408e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                mHandler.post(new Runnable(){
409e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    public void run() {
410e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                        mChatView.bindChat(chatId);
411e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                        showInvitationHasSent(mContact);
412e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                    }
413e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                });
414e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                mChatSession.unregisterChatListener(this);
415e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            } catch (RemoteException e) {
416e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project                mHandler.showServiceErrorAlert();
417e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            }
418e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        }
419e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project
420e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        public void start() throws RemoteException {
421e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project            mChatSession.registerChatListener(this);
422e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project        }
423e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project    }
424e3ea5527f63cfebfc1ae7fea0f3bd70200bf8f42The Android Open Source Project}
425