diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2015-09-21 22:27:14 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2015-09-21 22:27:14 +0900 |
commit | 8cbe0f59cb84389b77fe16c1b8a99ff8a8656983 (patch) | |
tree | 2621cb978a691a3ab0d1a1be7796aa4eecc5fa16 /app | |
parent | 0578d0b26d5fa0f68d9fc3fc1da890e4b940de7e (diff) | |
download | SmileEssence-8cbe0f59cb84389b77fe16c1b8a99ff8a8656983.tar.gz |
UserStreamListener は Activity の参照を保持してはいけない
Diffstat (limited to 'app')
4 files changed, 36 insertions, 43 deletions
diff --git a/app/src/main/java/net/lacolaco/smileessence/activity/MainActivity.java b/app/src/main/java/net/lacolaco/smileessence/activity/MainActivity.java index ae1dd95a..0e410005 100644 --- a/app/src/main/java/net/lacolaco/smileessence/activity/MainActivity.java +++ b/app/src/main/java/net/lacolaco/smileessence/activity/MainActivity.java @@ -78,6 +78,7 @@ public class MainActivity extends Activity { private TwitterStream stream; private boolean streaming = false; private Uri cameraTempFilePath; + private UserStreamListener userStreamListener; // --------------------- GETTER / SETTER METHODS --------------------- public int getRequestCountPerPage() { @@ -138,16 +139,12 @@ public class MainActivity extends Activity { } /** - * Returns which twitter stream is running + * Returns whether twitter stream is running * * @return */ public boolean isStreaming() { - return streaming; - } - - public void setStreaming(boolean streaming) { - this.streaming = streaming; + return userStreamListener != null && userStreamListener.isConnected(); } public void setSelectedPageIndex(int position) { @@ -345,9 +342,9 @@ public class MainActivity extends Activity { stream.shutdown(); } stream = currentAccount.getTwitterStream(); - UserStreamListener listener = new UserStreamListener(this); - stream.addListener(listener); - stream.addConnectionLifeCycleListener(listener); + userStreamListener = new UserStreamListener(currentAccount); + stream.addListener(userStreamListener); + stream.addConnectionLifeCycleListener(userStreamListener); stream.user(); return true; } diff --git a/app/src/main/java/net/lacolaco/smileessence/entity/Account.java b/app/src/main/java/net/lacolaco/smileessence/entity/Account.java index 9e59d06a..4b4d804d 100644 --- a/app/src/main/java/net/lacolaco/smileessence/entity/Account.java +++ b/app/src/main/java/net/lacolaco/smileessence/entity/Account.java @@ -42,8 +42,7 @@ import twitter4j.auth.AccessToken; @Table(name = "Accounts") public class Account extends Model { - - // ------------------------------ FIELDS ------------------------------ + private User user; @Column(name = "Token", notNull = true) public String accessToken; @@ -51,19 +50,9 @@ public class Account extends Model { public String accessSecret; @Column(name = "UserID", notNull = true) public long userID; - @Column(name = "ScreenName", notNull = true) + @Deprecated @Column(name = "ScreenName", notNull = true) public String screenName; - private User user; - - // -------------------------- STATIC METHODS -------------------------- - - public Account() { - super(); - } - - // --------------------------- CONSTRUCTORS --------------------------- - public Account(String token, String tokenSecret, long userID, String screenName) { super(); this.accessToken = token; @@ -72,10 +61,15 @@ public class Account extends Model { this.screenName = screenName; } + @Deprecated public static void deleteAll() { new Delete().from(Account.class).execute(); } + public long getUserId() { + return userID; + } + public Twitter getTwitter() { Twitter twitter = new TwitterFactory().getInstance(); twitter.setOAuthAccessToken(new AccessToken(accessToken, accessSecret)); diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/UserStreamListener.java b/app/src/main/java/net/lacolaco/smileessence/twitter/UserStreamListener.java index 73e1f437..4bd60b10 100644 --- a/app/src/main/java/net/lacolaco/smileessence/twitter/UserStreamListener.java +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/UserStreamListener.java @@ -28,6 +28,7 @@ import net.lacolaco.smileessence.R; import net.lacolaco.smileessence.activity.MainActivity; import net.lacolaco.smileessence.data.FavoriteCache; import net.lacolaco.smileessence.data.UserListCache; +import net.lacolaco.smileessence.entity.Account; import net.lacolaco.smileessence.entity.DirectMessage; import net.lacolaco.smileessence.entity.Tweet; import net.lacolaco.smileessence.entity.User; @@ -46,18 +47,19 @@ public class UserStreamListener implements twitter4j.UserStreamListener, Connect // ------------------------------ FIELDS ------------------------------ - private final MainActivity activity; + private final Account account; + private boolean connected = false; // --------------------------- CONSTRUCTORS --------------------------- - public UserStreamListener(MainActivity activity) { - this.activity = activity; + public UserStreamListener(Account account) { + this.account = account; } // --------------------- GETTER / SETTER METHODS --------------------- - private long getMyID() { - return activity.getCurrentAccount().userID; + public boolean isConnected() { + return connected; } // ------------------------ INTERFACE METHODS ------------------------ @@ -67,13 +69,13 @@ public class UserStreamListener implements twitter4j.UserStreamListener, Connect @Override public void onConnect() { - activity.setStreaming(true); + connected = true; Notificator.getInstance().publish(R.string.notice_stream_connect); } @Override public void onDisconnect() { - activity.setStreaming(false); + connected = false; Notificator.getInstance().publish(R.string.notice_stream_disconnect); } @@ -87,17 +89,16 @@ public class UserStreamListener implements twitter4j.UserStreamListener, Connect public void onStatus(Status status) { Tweet tweet = Tweet.fromTwitter(status); StatusViewModel vm = new StatusViewModel(tweet); - if (tweet.isRetweet()) { - //if (viewModel.isRetweetOfMe()) { - // addToHistory(new EventViewModel(EnumEvent.RETWEETED, status.getUser(), status)); - //} - }// else if (viewModel.isMention()) { - // addToMentions(viewModel); - // EventViewModel mentioned = new EventViewModel(EnumEvent.MENTIONED, status.getUser(), status); - // Notificator.getInstance().publish(mentioned.getFormattedString(activity)); - //} StatusFilter.getInstance().filter(vm); FavoriteCache.getInstance().put(status); + if (tweet.isRetweet()) { + if (tweet.getUser().getId() == account.getUserId()) { + addToHistory(new EventViewModel(EnumEvent.RETWEETED, tweet.getUser(), tweet)); + } + } else if (account.getCachedUser() != null && tweet.getMentions().contains(account.getCachedUser().getScreenName())) { + EventViewModel mentioned = new EventViewModel(EnumEvent.MENTIONED, tweet.getUser(), tweet); + Notificator.getInstance().publish(mentioned.getFormattedString()); + } } @Override @@ -257,10 +258,10 @@ public class UserStreamListener implements twitter4j.UserStreamListener, Connect private void addToHistory(EventViewModel mentioned) { StatusFilter.getInstance().filter(mentioned); - Notificator.getInstance().publish(mentioned.getFormattedString(activity)); + Notificator.getInstance().publish(mentioned.getFormattedString()); } private boolean isMe(User user) { - return user.getId() == getMyID(); + return user.getId() == account.getUserId(); } } diff --git a/app/src/main/java/net/lacolaco/smileessence/viewmodel/EventViewModel.java b/app/src/main/java/net/lacolaco/smileessence/viewmodel/EventViewModel.java index dadb462b..c11ff890 100644 --- a/app/src/main/java/net/lacolaco/smileessence/viewmodel/EventViewModel.java +++ b/app/src/main/java/net/lacolaco/smileessence/viewmodel/EventViewModel.java @@ -31,6 +31,7 @@ import android.view.View; import android.widget.ImageView; import android.widget.TextView; import com.android.volley.toolbox.NetworkImageView; +import net.lacolaco.smileessence.Application; import net.lacolaco.smileessence.R; import net.lacolaco.smileessence.activity.MainActivity; import net.lacolaco.smileessence.data.ImageCache; @@ -143,7 +144,7 @@ public class EventViewModel implements IViewModel { header.setTextSize(textSize); int colorHeader = Themes.getStyledColor(activity, theme, R.attr.color_status_text_mine, 0); header.setTextColor(colorHeader); - header.setText(getFormattedString(activity)); + header.setText(getFormattedString()); TextView content = (TextView) convertedView.findViewById(R.id.textview_status_text); content.setTextSize(textSize); int colorNormal = Themes.getStyledColor(activity, theme, R.attr.color_status_text_normal, 0); @@ -168,7 +169,7 @@ public class EventViewModel implements IViewModel { // -------------------------- OTHER METHODS -------------------------- - public String getFormattedString(Context context) { - return context.getString(event.getTextFormatResourceID(), sourceScreenName); + public String getFormattedString() { + return Application.getContext().getString(event.getTextFormatResourceID(), sourceScreenName); } } |