@Override protected ResponseList<DirectMessage> doInBackground(Void... params) { try { Twitter twitter = TwitterManager.getTwitter(); // 受信したDM Paging directMessagesPaging = new Paging(); if (mDirectMessagesMaxId > 0 && !mReloading) { directMessagesPaging.setMaxId(mDirectMessagesMaxId - 1); directMessagesPaging.setCount(BasicSettings.getPageCount() / 2); } else { directMessagesPaging.setCount(10); } ResponseList<DirectMessage> directMessages = twitter.getDirectMessages(directMessagesPaging); for (DirectMessage directMessage : directMessages) { if (mDirectMessagesMaxId <= 0L || mDirectMessagesMaxId > directMessage.getId()) { mDirectMessagesMaxId = directMessage.getId(); } } // 送信したDM Paging sentDirectMessagesPaging = new Paging(); if (mSentDirectMessagesMaxId > 0 && !mReloading) { sentDirectMessagesPaging.setMaxId(mSentDirectMessagesMaxId - 1); sentDirectMessagesPaging.setCount(BasicSettings.getPageCount() / 2); } else { sentDirectMessagesPaging.setCount(10); } ResponseList<DirectMessage> sentDirectMessages = twitter.getSentDirectMessages(sentDirectMessagesPaging); for (DirectMessage directMessage : sentDirectMessages) { if (mSentDirectMessagesMaxId <= 0L || mSentDirectMessagesMaxId > directMessage.getId()) { mSentDirectMessagesMaxId = directMessage.getId(); } } directMessages.addAll(sentDirectMessages); // 日付でソート Collections.sort( directMessages, new Comparator<DirectMessage>() { @Override public int compare(DirectMessage arg0, DirectMessage arg1) { return arg1.getCreatedAt().compareTo(arg0.getCreatedAt()); } }); return directMessages; } catch (OutOfMemoryError e) { return null; } catch (Exception e) { e.printStackTrace(); return null; } }
public static void switchAccessToken(final AccessToken accessToken) { AccessTokenManager.setAccessToken(accessToken); if (BasicSettings.getStreamingMode()) { MessageUtil.showToast(R.string.toast_destroy_streaming); sUserStreamAdapter.stop(); new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... params) { sTwitterStream.cleanUp(); sTwitterStream.shutdown(); return null; } @Override protected void onPostExecute(Void status) { sTwitterStream.setOAuthAccessToken(accessToken); new Handler() .postDelayed( new Runnable() { @Override public void run() { MessageUtil.showToast(R.string.toast_create_streaming); sUserStreamAdapter.start(); sTwitterStream.user(); } }, 5000); } }.execute(); } EventBus.getDefault().post(new AccountChangeEvent()); }
public static void startStreaming() { if (sTwitterStream != null) { if (!sTwitterStreamConnected) { sUserStreamAdapter.start(); sTwitterStream.setOAuthAccessToken(AccessTokenManager.getAccessToken()); sTwitterStream.user(); } return; } sTwitterStream = getTwitterStream(); sUserStreamAdapter = new MyUserStreamAdapter(); sTwitterStream.addListener(sUserStreamAdapter); sTwitterStream.addConnectionLifeCycleListener(new MyConnectionLifeCycleListener()); sTwitterStream.user(); BasicSettings.resetNotification(); }
public static void stopStreaming() { if (sTwitterStream == null) { return; } BasicSettings.setStreamingMode(false); sUserStreamAdapter.stop(); new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... params) { sTwitterStream.cleanUp(); sTwitterStream.shutdown(); return null; } @Override protected void onPostExecute(Void status) {} }.execute(); }