⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imapp.java

📁 Android平台上即时通讯聊天工具源代码。 支持手机聊天。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        resMapping.put(BrandingResourceIDs.STRING_ONGOING_CONVERSATION,                R.string.ongoing_conversation);        resMapping.put(BrandingResourceIDs.STRING_ADD_CONTACT_TITLE, R.string.add_contact_title);        resMapping.put(BrandingResourceIDs.STRING_LABEL_INPUT_CONTACT,                R.string.input_contact_label);        resMapping.put(BrandingResourceIDs.STRING_BUTTON_ADD_CONTACT, R.string.invite_label);        resMapping.put(BrandingResourceIDs.STRING_CONTACT_INFO_TITLE,                R.string.contact_profile_title);        resMapping.put(BrandingResourceIDs.STRING_MENU_ADD_CONTACT, R.string.menu_add_contact);        resMapping.put(BrandingResourceIDs.STRING_MENU_BLOCK_CONTACT, R.string.menu_block_contact);        resMapping.put(BrandingResourceIDs.STRING_MENU_CONTACT_LIST,                R.string.menu_view_contact_list);        resMapping.put(BrandingResourceIDs.STRING_MENU_DELETE_CONTACT,                R.string.menu_remove_contact);        resMapping.put(BrandingResourceIDs.STRING_MENU_END_CHAT, R.string.menu_end_conversation);        resMapping.put(BrandingResourceIDs.STRING_MENU_INSERT_SMILEY, R.string.menu_insert_smiley);        resMapping.put(BrandingResourceIDs.STRING_MENU_START_CHAT, R.string.menu_start_chat);        resMapping.put(BrandingResourceIDs.STRING_MENU_VIEW_PROFILE, R.string.menu_view_profile);        resMapping.put(BrandingResourceIDs.STRING_MENU_SWITCH_CHATS, R.string.menu_switch_chats);        resMapping.put(BrandingResourceIDs.STRING_TOAST_CHECK_AUTO_SIGN_IN,                R.string.check_auto_sign_in);        resMapping.put(BrandingResourceIDs.STRING_LABEL_SIGN_UP, R.string.sign_up);        mDefaultBrandingResources = new BrandingResources(this, resMapping, null /* default res */);    }    void loadBrandingResources() {        try {            List<ImPluginInfo> plugins = mImService.getAllPlugins();            for (ImPluginInfo plugin : plugins) {                long providerId = getProviderId(plugin.mProviderName);                if (!mBrandingResources.containsKey(providerId)) {                    BrandingResources res = new BrandingResources(this,                            plugin, mDefaultBrandingResources);                    mBrandingResources.put(providerId, res);                }            }        } catch (RemoteException e) {            Log.e(LOG_TAG, "Service died while loading branding resource");        }    }    public long getProviderId(String name) {        loadImProviderSettings();        for (ProviderDef provider: mProviders.values()) {            if(provider.mName.equals(name)) {                return provider.mId;            }        }        return -1;    }    public ProviderDef getProvider(long id) {        loadImProviderSettings();        return mProviders.get(id);    }    public List<ProviderDef> getProviders() {        loadImProviderSettings();        ArrayList<ProviderDef> result = new ArrayList<ProviderDef>();        result.addAll(mProviders.values());        return result;    }    public BrandingResources getBrandingResource(long providerId) {        BrandingResources res = mBrandingResources.get(providerId);        return res == null ? mDefaultBrandingResources : res;    }    public IImConnection createConnection(long providerId) throws RemoteException {        IImConnection conn = getConnection(providerId);        if (conn == null) {            conn = mImService.createConnection(providerId);        }        return conn;    }    IImConnection getConnection(long providerId) {        synchronized (mConnections) {            return mConnections.get(providerId);        }    }    public IImConnection getConnectionByAccount(long accountId) {        synchronized (mConnections) {            for (IImConnection conn : mConnections.values()) {                try {                    if (conn.getAccountId() == accountId) {                        return conn;                    }                } catch (RemoteException e) {                    // No server!                }            }            return null;        }    }    public List<IImConnection> getActiveConnections() {        synchronized (mConnections) {            ArrayList<IImConnection> result = new ArrayList<IImConnection>();            result.addAll(mConnections.values());            return result;        }    }    public void callWhenServiceConnected(Handler target, Runnable callback) {        Message msg = Message.obtain(target, callback);        if (serviceConnected()) {            msg.sendToTarget();        } else {            startImServiceIfNeed();            synchronized (mQueue) {                mQueue.add(msg);            }        }    }    public void removePendingCall(Handler target) {        synchronized (mQueue) {           Iterator<Message> iter = mQueue.iterator();           while (iter.hasNext()) {               Message msg = iter.next();               if (msg.getTarget() == target) {                   iter.remove();               }           }       }   }    public void registerForBroadcastEvent(int what, Handler target) {        mBroadcaster.request(what, target, what);    }    public void unregisterForBroadcastEvent(int what, Handler target) {        mBroadcaster.cancelRequest(what, target, what);    }    public void registerForConnEvents(Handler handler) {        mBroadcaster.request(EVENT_CONNECTION_CREATED, handler,                EVENT_CONNECTION_CREATED);        mBroadcaster.request(EVENT_CONNECTION_LOGGING_IN, handler,                EVENT_CONNECTION_LOGGING_IN);        mBroadcaster.request(EVENT_CONNECTION_LOGGED_IN, handler,                EVENT_CONNECTION_LOGGED_IN);        mBroadcaster.request(EVENT_CONNECTION_LOGGING_OUT, handler,                EVENT_CONNECTION_LOGGING_OUT);        mBroadcaster.request(EVENT_CONNECTION_SUSPENDED, handler,                EVENT_CONNECTION_SUSPENDED);        mBroadcaster.request(EVENT_CONNECTION_DISCONNECTED, handler,                EVENT_CONNECTION_DISCONNECTED);        mBroadcaster.request(EVENT_USER_PRESENCE_UPDATED, handler,                EVENT_USER_PRESENCE_UPDATED);        mBroadcaster.request(EVENT_UPDATE_USER_PRESENCE_ERROR, handler,                EVENT_UPDATE_USER_PRESENCE_ERROR);    }    public void unregisterForConnEvents(Handler handler) {        mBroadcaster.cancelRequest(EVENT_CONNECTION_CREATED, handler,                EVENT_CONNECTION_CREATED);        mBroadcaster.cancelRequest(EVENT_CONNECTION_LOGGING_IN, handler,                EVENT_CONNECTION_LOGGING_IN);        mBroadcaster.cancelRequest(EVENT_CONNECTION_LOGGED_IN, handler,                EVENT_CONNECTION_LOGGED_IN);        mBroadcaster.cancelRequest(EVENT_CONNECTION_LOGGING_OUT, handler,                EVENT_CONNECTION_LOGGING_OUT);        mBroadcaster.cancelRequest(EVENT_CONNECTION_SUSPENDED, handler,                EVENT_CONNECTION_SUSPENDED);        mBroadcaster.cancelRequest(EVENT_CONNECTION_DISCONNECTED, handler,                EVENT_CONNECTION_DISCONNECTED);        mBroadcaster.cancelRequest(EVENT_USER_PRESENCE_UPDATED, handler,                EVENT_USER_PRESENCE_UPDATED);        mBroadcaster.cancelRequest(EVENT_UPDATE_USER_PRESENCE_ERROR, handler,                EVENT_UPDATE_USER_PRESENCE_ERROR);    }    void broadcastConnEvent(int what, long providerId, ImErrorInfo error) {        if(Log.isLoggable(LOG_TAG, Log.DEBUG)){            log("broadcasting connection event " + what + ", provider id " + providerId);        }        android.os.Message msg = android.os.Message.obtain(                null,                what,                (int)(providerId >> 32), (int)providerId,                error);        mBroadcaster.broadcast(msg);    }    public void dismissNotifications(long providerId) {        if (mImService != null) {            try {                mImService.dismissNotifications(providerId);            } catch (RemoteException e) {            }        }    }    private void fetchActiveConnections() {        try {            // register the listener before fetch so that we won't miss any connection.            mImService.addConnectionCreatedListener(mConnCreationListener);            synchronized (mConnections) {                for(IBinder binder: (List<IBinder>) mImService.getActiveConnections()) {                    IImConnection conn = IImConnection.Stub.asInterface(binder);                    long providerId = conn.getProviderId();                    if (!mConnections.containsKey(providerId)) {                        mConnections.put(providerId, conn);                        conn.registerConnectionListener(mConnectionListener);                    }                }            }        } catch (RemoteException e) {            Log.e(LOG_TAG, "fetching active connections", e);        }    }    private final IConnectionCreationListener mConnCreationListener            = new IConnectionCreationListener.Stub() {        public void onConnectionCreated(IImConnection conn)                throws RemoteException {            long providerId = conn.getProviderId();            synchronized (mConnections) {                if (!mConnections.containsKey(providerId)) {                    mConnections.put(providerId, conn);                    conn.registerConnectionListener(mConnectionListener);                }            }            broadcastConnEvent(EVENT_CONNECTION_CREATED, providerId, null);        }      };    private final class MyConnListener extends ConnectionListenerAdapter {        public MyConnListener(Handler handler) {            super(handler);        }        @Override        public void onConnectionStateChange(IImConnection conn, int state,                ImErrorInfo error) {            if(Log.isLoggable(LOG_TAG, Log.DEBUG)){                log("onConnectionStateChange(" + state + ", " + error + ")");            }            try {                int what = -1;                long providerId = conn.getProviderId();                switch (state) {                case ImConnection.LOGGED_IN:                    what = EVENT_CONNECTION_LOGGED_IN;                    // Update the active value. We restrict to only one active                    // account per provider right now, so update all accounts of                    // this provider to inactive first and then update this                    // account to active.                    ContentValues values = new ContentValues(1);                    values.put(Im.Account.ACTIVE, 0);                    ContentResolver cr = getContentResolver();                    cr.update(Im.Account.CONTENT_URI, values,                            Im.Account.PROVIDER + "=" + providerId, null);                    values.put(Im.Account.ACTIVE, 1);                    cr.update(ContentUris.withAppendedId(Im.Account.CONTENT_URI, conn.getAccountId()),                            values, null, null);                    break;                case ImConnection.LOGGING_IN:                    what = EVENT_CONNECTION_LOGGING_IN;                    break;                case ImConnection.LOGGING_OUT:                    what = EVENT_CONNECTION_LOGGING_OUT;                    break;                case ImConnection.DISCONNECTED:                    what = EVENT_CONNECTION_DISCONNECTED;                    synchronized (mConnections) {                        mConnections.remove(providerId);                    }                    // stop the service if there isn't an active connection anymore.                    stopImServiceIfInactive();                    break;                case ImConnection.SUSPENDED:                    what = EVENT_CONNECTION_SUSPENDED;                    break;                }                if (what != -1) {                    broadcastConnEvent(what, providerId, error);                }            } catch (RemoteException e) {                Log.e(LOG_TAG, "onConnectionStateChange", e);            }        }        @Override        public void onUpdateSelfPresenceError(IImConnection connection,                ImErrorInfo error) {            if(Log.isLoggable(LOG_TAG, Log.DEBUG)){                log("onUpdateUserPresenceError(" + error + ")");            }            try {                long providerId = connection.getProviderId();                broadcastConnEvent(EVENT_UPDATE_USER_PRESENCE_ERROR, providerId,                        error);            } catch (RemoteException e) {                Log.e(LOG_TAG, "onUpdateUserPresenceError", e);            }        }        @Override        public void onSelfPresenceUpdated(IImConnection connection) {            if(Log.isLoggable(LOG_TAG, Log.DEBUG)) log("onUserPresenceUpdated");            try {                long providerId = connection.getProviderId();                broadcastConnEvent(EVENT_USER_PRESENCE_UPDATED, providerId,                        null);            } catch (RemoteException e) {                Log.e(LOG_TAG, "onUserPresenceUpdated", e);            }        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -