📄 remoteimservice.java
字号:
} } private void autoLogin() { Log.d(TAG, "Scaning accounts and login automatically"); ContentResolver resolver = getContentResolver(); String where = Im.Account.KEEP_SIGNED_IN + "=1 AND " + Im.Account.ACTIVE + "=1"; Cursor cursor = resolver.query(Im.Account.CONTENT_URI, ACCOUNT_PROJECTION, where, null, null); if (cursor == null) { Log.w(TAG, "Can't query account!"); return; } while (cursor.moveToNext()) { long accountId = cursor.getLong(ACCOUNT_ID_COLUMN); long providerId = cursor.getLong(ACCOUNT_PROVIDER_COLUMN); String username = cursor.getString(ACCOUNT_USERNAME_COLUMN); String password = cursor.getString(ACCOUNT_PASSOWRD_COLUMN); IImConnection conn = createConnection(providerId); try { conn.login(accountId, username, password, true); } catch (RemoteException e) { Log.w(TAG, "Logging error while automatically login!"); } } cursor.close(); } private Map<String, String> loadProviderSettings(long providerId) { ContentResolver cr = getContentResolver(); Map<String, String> settings = Im.ProviderSettings.queryProviderSettings(cr, providerId); NetworkInfo networkInfo = mNetworkConnectivityListener.getNetworkInfo(); // Insert a fake msisdn on emulator. We don't need this on device // because the mobile network will take care of it. if ("1".equals(SystemProperties.get("ro.kernel.qemu"))) { settings.put(ImpsConfigNames.MSISDN, "1231231234"); } else if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { // Wi-Fi network won't insert a MSISDN, we should get from the SIM // card. Assume we can always get the correct MSISDN from SIM, otherwise, // the sign in would fail and an error message should be shown to warn // the user to contact their operator. String msisdn = TelephonyManager.getDefault().getLine1Number(); if (!TextUtils.isEmpty(msisdn)) { settings.put(ImpsConfigNames.MSISDN, msisdn); } else { // TODO: This should be removed. We can't fetch phone number from // the test T-Mobile SIMs. Use a fake phone number so that we can // work with our test SIMs right now. This can't happen with T-Mobile // production SIMs Log.w(TAG, "Can not get phone number from SIM, use a fake one"); settings.put(ImpsConfigNames.MSISDN, "1231231234"); } } return settings; } @Override public void onDestroy() { Log.w(TAG, "ImService stopped."); for (ImConnectionAdapter conn : mConnections) { conn.logout(); } mNetworkConnectivityListener.unregisterHandler(mServiceHandler); mNetworkConnectivityListener.stopListening(); mNetworkConnectivityListener = null; stopTcpCirAlarm(); } @Override public IBinder onBind(Intent intent) { return mBinder; } public void showToast(CharSequence text, int duration) { Message msg = Message.obtain(mServiceHandler, EVENT_SHOW_TOAST, duration, 0, text); msg.sendToTarget(); } public StatusBarNotifier getStatusBarNotifier() { return mStatusBarNotifier; } public void scheduleReconnect(long delay) { if (!isNetworkAvailable()) { // Don't schedule reconnect if no network available. We will try to // reconnect when network state become CONNECTED. return; } mServiceHandler.postDelayed(new Runnable() { public void run() { reestablishConnections(); } }, delay); } IImConnection createConnection(long providerId) { Map<String, String> settings = loadProviderSettings(providerId); String protocol = settings.get(ImConfigNames.PROTOCOL_NAME); if(!"IMPS".equals(protocol)) { Log.e(TAG, "Unsupported protocol: " + protocol); return null; } ImpsConnectionConfig config = new ImpsConnectionConfig(settings); ConnectionFactory factory = ConnectionFactory.getInstance(); try { ImConnection conn = factory.createConnection(config); ImConnectionAdapter result = new ImConnectionAdapter(providerId, conn, this); mConnections.add(result); if (config.getCirChannelBinding() == CirMethod.STCP) { startTcpCirAlarm(); } mListenerMgr.notifyConnectionCreated(result); return result; } catch (ImException e) { Log.e(TAG, "Error creating connection", e); return null; } } void removeConnection(IImConnection connection) { mConnections.remove(connection); } private boolean isNetworkAvailable() { return mNetworkConnectivityListener.getState() == State.CONNECTED; } void networkStateChanged() { if (mNetworkConnectivityListener == null) { return; } NetworkInfo networkInfo = mNetworkConnectivityListener.getNetworkInfo(); NetworkInfo.State state = networkInfo.getState(); Log.d(TAG, "networkStateChanged:" + state); int oldType = mNetworkType; mNetworkType = networkInfo.getType(); // Notify the connection that network type has changed. Note that this // only work for connected connections, we need to reestablish if it's // suspended. if (mNetworkType != oldType && isNetworkAvailable()) { for (ImConnectionAdapter conn : mConnections) { conn.networkTypeChanged(); } } switch (state) { case CONNECTED: if (mNeedCheckAutoLogin) { mNeedCheckAutoLogin = false; autoLogin(); break; } reestablishConnections(); break; case DISCONNECTED: if (!isNetworkAvailable()) { suspendConnections(); } break; } } // package private for inner class access void reestablishConnections() { if (!isNetworkAvailable()) { return; } for (ImConnectionAdapter conn : mConnections) { int connState = conn.getState(); if (connState == ImConnection.SUSPENDED) { conn.reestablishSession(); } } } private void suspendConnections() { for (ImConnectionAdapter conn : mConnections) { if (conn.getState() != ImConnection.LOGGED_IN) { continue; } conn.suspend(); } } private final IRemoteImService.Stub mBinder = new IRemoteImService.Stub() { public List getAllPlugins() { return new ArrayList(mPlugins.values()); } public void addConnectionCreatedListener(IConnectionCreationListener listener) { mListenerMgr.addRemoteListener(listener); } public void removeConnectionCreatedListener(IConnectionCreationListener listener) { mListenerMgr.removeRemoteListener(listener); } public IImConnection createConnection(long providerId) { return RemoteImService.this.createConnection(providerId); } public List getActiveConnections() { ArrayList<IBinder> result = new ArrayList<IBinder>(mConnections.size()); for(IImConnection conn : mConnections) { result.add(conn.asBinder()); } return result; } public void dismissNotifications(long providerId) { mStatusBarNotifier.dismissNotifications(providerId); } }; final ConnectionListenerManager mListenerMgr = new ConnectionListenerManager(); private final static class ConnectionListenerManager extends RemoteListenerManager<IConnectionCreationListener> { public ConnectionListenerManager(){ } public void notifyConnectionCreated(final ImConnectionAdapter conn) { notifyRemoteListeners(new ListenerInvocation<IConnectionCreationListener>() { public void invoke(IConnectionCreationListener remoteListener) throws RemoteException { remoteListener.onConnectionCreated(conn); } }); } } private final class ServiceHandler extends Handler { public ServiceHandler() { } @Override public void handleMessage(Message msg) { switch (msg.what) { case EVENT_SHOW_TOAST: Toast.makeText(RemoteImService.this, (CharSequence) msg.obj, msg.arg1).show(); break; case EVENT_NETWORK_STATE_CHANGED: networkStateChanged(); break; default: } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -