diff options
Diffstat (limited to 'app/src/main/java/net/lacolaco/smileessence/twitter/task')
29 files changed, 2240 insertions, 0 deletions
diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/AccessTokenTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/AccessTokenTask.java new file mode 100644 index 00000000..ea549fc4 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/AccessTokenTask.java @@ -0,0 +1,56 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import twitter4j.Twitter; +import twitter4j.TwitterException; +import twitter4j.auth.AccessToken; +import twitter4j.auth.RequestToken; + +public class AccessTokenTask extends TwitterTask<AccessToken> { + + // ------------------------------ FIELDS ------------------------------ + + private final RequestToken requestToken; + private final String pinCode; + + // --------------------------- CONSTRUCTORS --------------------------- + + public AccessTokenTask(Twitter twitter, RequestToken requestToken, String pinCode) { + super(twitter); + this.requestToken = requestToken; + this.pinCode = pinCode; + } + + @Override + protected AccessToken doInBackground(Void... params) { + try { + return twitter.getOAuthAccessToken(requestToken, pinCode); + } catch (TwitterException e) { + e.printStackTrace(); + return null; + } + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/BlockIDsTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/BlockIDsTask.java new file mode 100644 index 00000000..cf57fb80 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/BlockIDsTask.java @@ -0,0 +1,74 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import net.lacolaco.smileessence.data.UserCache; +import net.lacolaco.smileessence.logging.Logger; + +import twitter4j.IDs; +import twitter4j.Twitter; +import twitter4j.TwitterException; + +import java.util.ArrayList; +import java.util.List; + +public class BlockIDsTask extends TwitterTask<Long[]> { + + // --------------------------- CONSTRUCTORS --------------------------- + + public BlockIDsTask(Twitter twitter) { + super(twitter); + } + + // ------------------------ OVERRIDE METHODS ------------------------ + + @Override + protected void onPostExecute(Long[] blockIDs) { + for (Long blockID : blockIDs) { + UserCache.getInstance().putInvisibleUser(blockID); + } + } + + @Override + protected Long[] doInBackground(Void... params) { + try { + List<Long> idList = new ArrayList<>(); + long cursor = -1; + do { + IDs blocksIDs = twitter.getBlocksIDs(cursor); + cursor = blocksIDs.getNextCursor(); + for (long id : blocksIDs.getIDs()) { + idList.add(id); + } + } + while (cursor != 0); + + return idList.toArray(new Long[idList.size()]); + } catch (TwitterException e) { + Logger.error(e); + return new Long[0]; + } + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/BlockTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/BlockTask.java new file mode 100644 index 00000000..70f59a21 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/BlockTask.java @@ -0,0 +1,76 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import android.app.Activity; + +import net.lacolaco.smileessence.R; +import net.lacolaco.smileessence.data.UserCache; +import net.lacolaco.smileessence.logging.Logger; +import net.lacolaco.smileessence.notification.NotificationType; +import net.lacolaco.smileessence.notification.Notificator; + +import twitter4j.Twitter; +import twitter4j.TwitterException; +import twitter4j.User; + +public class BlockTask extends TwitterTask<User> { + + // ------------------------------ FIELDS ------------------------------ + + private final long userID; + private final Activity activity; + + // --------------------------- CONSTRUCTORS --------------------------- + + public BlockTask(Twitter twitter, long userID, Activity activity) { + super(twitter); + this.userID = userID; + this.activity = activity; + } + + // ------------------------ OVERRIDE METHODS ------------------------ + + @Override + protected void onPostExecute(User user) { + if (user != null) { + UserCache.getInstance().put(user); + new Notificator(activity, R.string.notice_block_succeeded).publish(); + } else { + new Notificator(activity, R.string.notice_block_failed, NotificationType.ALERT).publish(); + } + } + + @Override + protected User doInBackground(Void... params) { + try { + return twitter.users().createBlock(userID); + } catch (TwitterException e) { + e.printStackTrace(); + Logger.error(e.toString()); + return null; + } + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/DeleteMessageTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/DeleteMessageTask.java new file mode 100644 index 00000000..c218cfe5 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/DeleteMessageTask.java @@ -0,0 +1,77 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import android.app.Activity; + +import net.lacolaco.smileessence.R; +import net.lacolaco.smileessence.data.DirectMessageCache; +import net.lacolaco.smileessence.logging.Logger; +import net.lacolaco.smileessence.notification.NotificationType; +import net.lacolaco.smileessence.notification.Notificator; + +import twitter4j.DirectMessage; +import twitter4j.Twitter; +import twitter4j.TwitterException; + +public class DeleteMessageTask extends TwitterTask<DirectMessage> { + + // ------------------------------ FIELDS ------------------------------ + + private final long messageID; + private final Activity activity; + + // --------------------------- CONSTRUCTORS --------------------------- + + public DeleteMessageTask(Twitter twitter, long messageID, Activity activity) { + super(twitter); + this.messageID = messageID; + this.activity = activity; + } + + // ------------------------ OVERRIDE METHODS ------------------------ + + @Override + protected void onPostExecute(DirectMessage message) { + if (message != null) { + DirectMessageCache.getInstance().put(message); + new Notificator(activity, R.string.notice_message_delete_succeeded).publish(); + } else { + new Notificator(activity, R.string.notice_message_delete_failed, NotificationType.ALERT).publish(); + } + } + + @Override + protected DirectMessage doInBackground(Void... params) { + try { + return twitter.directMessages().destroyDirectMessage(messageID); + } catch (TwitterException e) { + e.printStackTrace(); + Logger.error(e.toString()); + return null; + } + } + +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/DeleteStatusTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/DeleteStatusTask.java new file mode 100644 index 00000000..7dcc7b07 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/DeleteStatusTask.java @@ -0,0 +1,76 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import android.app.Activity; + +import net.lacolaco.smileessence.R; +import net.lacolaco.smileessence.data.StatusCache; +import net.lacolaco.smileessence.logging.Logger; +import net.lacolaco.smileessence.notification.NotificationType; +import net.lacolaco.smileessence.notification.Notificator; + +import twitter4j.Status; +import twitter4j.Twitter; +import twitter4j.TwitterException; + +public class DeleteStatusTask extends TwitterTask<Status> { + + // ------------------------------ FIELDS ------------------------------ + + private final long statusID; + private final Activity activity; + + // --------------------------- CONSTRUCTORS --------------------------- + + public DeleteStatusTask(Twitter twitter, long statusID, Activity activity) { + super(twitter); + this.statusID = statusID; + this.activity = activity; + } + + // ------------------------ OVERRIDE METHODS ------------------------ + + @Override + protected void onPostExecute(twitter4j.Status status) { + if (status != null) { + StatusCache.getInstance().remove(status.getId()); + new Notificator(activity, R.string.notice_status_delete_succeeded).publish(); + } else { + new Notificator(activity, R.string.notice_status_delete_failed, NotificationType.ALERT).publish(); + } + } + + @Override + protected twitter4j.Status doInBackground(Void... params) { + try { + return twitter.tweets().destroyStatus(statusID); + } catch (TwitterException e) { + e.printStackTrace(); + Logger.error(e.toString()); + return null; + } + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/DirectMessagesTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/DirectMessagesTask.java new file mode 100644 index 00000000..13b69255 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/DirectMessagesTask.java @@ -0,0 +1,88 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import android.app.Activity; + +import net.lacolaco.smileessence.R; +import net.lacolaco.smileessence.data.DirectMessageCache; +import net.lacolaco.smileessence.logging.Logger; +import net.lacolaco.smileessence.notification.NotificationType; +import net.lacolaco.smileessence.notification.Notificator; + +import twitter4j.*; + +public class DirectMessagesTask extends TwitterTask<DirectMessage[]> { + + // ------------------------------ FIELDS ------------------------------ + + private final Activity activity; + private final Paging paging; + + // --------------------------- CONSTRUCTORS --------------------------- + + protected DirectMessagesTask(Twitter twitter, Activity activity) { + this(twitter, activity, null); + } + + public DirectMessagesTask(Twitter twitter, Activity activity, Paging paging) { + super(twitter); + this.activity = activity; + this.paging = paging; + } + + // ------------------------ OVERRIDE METHODS ------------------------ + + @Override + protected void onPostExecute(DirectMessage[] directMessages) { + if (directMessages.length != 0) { + for (DirectMessage message : directMessages) { + DirectMessageCache.getInstance().put(message); + } + } + } + + @Override + protected DirectMessage[] doInBackground(Void... params) { + ResponseList<DirectMessage> responseList; + try { + if (paging == null) { + responseList = twitter.directMessages().getDirectMessages(); + } else { + responseList = twitter.directMessages().getDirectMessages(paging); + } + } catch (TwitterException e) { + e.printStackTrace(); + Logger.error(e.toString()); + if (e.exceededRateLimitation()) { + Notificator.publish(activity, R.string.notice_error_rate_limit, NotificationType.ALERT); + } else { + Notificator.publish(activity, R.string.notice_error_get_messages, NotificationType.ALERT); + } + return new DirectMessage[0]; + } + return responseList.toArray(new DirectMessage[responseList.size()]); + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/FavoriteTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/FavoriteTask.java new file mode 100644 index 00000000..d0ee13c5 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/FavoriteTask.java @@ -0,0 +1,76 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import android.app.Activity; + +import net.lacolaco.smileessence.R; +import net.lacolaco.smileessence.data.StatusCache; +import net.lacolaco.smileessence.logging.Logger; +import net.lacolaco.smileessence.notification.NotificationType; +import net.lacolaco.smileessence.notification.Notificator; + +import twitter4j.Status; +import twitter4j.Twitter; +import twitter4j.TwitterException; + +public class FavoriteTask extends TwitterTask<Status> { + + // ------------------------------ FIELDS ------------------------------ + + private final long statusID; + private final Activity activity; + + // --------------------------- CONSTRUCTORS --------------------------- + + public FavoriteTask(Twitter twitter, long statusID, Activity activity) { + super(twitter); + this.statusID = statusID; + this.activity = activity; + } + + // ------------------------ OVERRIDE METHODS ------------------------ + + @Override + protected void onPostExecute(twitter4j.Status status) { + if (status != null) { + StatusCache.getInstance().put(status); + new Notificator(activity, R.string.notice_favorite_succeeded).publish(); + } else { + new Notificator(activity, R.string.notice_favorite_failed, NotificationType.ALERT).publish(); + } + } + + @Override + protected twitter4j.Status doInBackground(Void... params) { + try { + return twitter.favorites().createFavorite(statusID); + } catch (TwitterException e) { + e.printStackTrace(); + Logger.error(e.toString()); + return null; + } + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/FollowTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/FollowTask.java new file mode 100644 index 00000000..ecd6fe17 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/FollowTask.java @@ -0,0 +1,76 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import android.app.Activity; + +import net.lacolaco.smileessence.R; +import net.lacolaco.smileessence.data.UserCache; +import net.lacolaco.smileessence.logging.Logger; +import net.lacolaco.smileessence.notification.NotificationType; +import net.lacolaco.smileessence.notification.Notificator; + +import twitter4j.Twitter; +import twitter4j.TwitterException; +import twitter4j.User; + +public class FollowTask extends TwitterTask<User> { + + // ------------------------------ FIELDS ------------------------------ + + private final long userID; + private final Activity activity; + + // --------------------------- CONSTRUCTORS --------------------------- + + public FollowTask(Twitter twitter, long userID, Activity activity) { + super(twitter); + this.userID = userID; + this.activity = activity; + } + + // ------------------------ OVERRIDE METHODS ------------------------ + + @Override + protected void onPostExecute(User user) { + if (user != null) { + UserCache.getInstance().put(user); + new Notificator(activity, R.string.notice_follow_succeeded).publish(); + } else { + new Notificator(activity, R.string.notice_follow_failed, NotificationType.ALERT).publish(); + } + } + + @Override + protected User doInBackground(Void... params) { + try { + return twitter.friendsFollowers().createFriendship(userID); + } catch (TwitterException e) { + e.printStackTrace(); + Logger.error(e.toString()); + return null; + } + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/GetUserListsTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/GetUserListsTask.java new file mode 100644 index 00000000..dafbc0d0 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/GetUserListsTask.java @@ -0,0 +1,66 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import net.lacolaco.smileessence.data.UserListCache; +import net.lacolaco.smileessence.logging.Logger; + +import twitter4j.Twitter; +import twitter4j.TwitterException; +import twitter4j.UserList; + +import java.util.ArrayList; +import java.util.List; + +public class GetUserListsTask extends TwitterTask<UserList[]> { + + // --------------------------- CONSTRUCTORS --------------------------- + + public GetUserListsTask(Twitter twitter) { + super(twitter); + } + + // ------------------------ OVERRIDE METHODS ------------------------ + + @Override + protected void onPostExecute(UserList[] lists) { + for (UserList list : lists) { + UserListCache.getInstance().put(list.getFullName()); + } + } + + @Override + protected UserList[] doInBackground(Void... params) { + try { + List<UserList> userLists = new ArrayList<>(); + + userLists.addAll(twitter.list().getUserLists(twitter.getId())); + return userLists.toArray(new UserList[userLists.size()]); + } catch (TwitterException e) { + Logger.error(e); + return new UserList[0]; + } + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/HomeTimelineTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/HomeTimelineTask.java new file mode 100644 index 00000000..9b435f74 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/HomeTimelineTask.java @@ -0,0 +1,90 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import android.app.Activity; + +import net.lacolaco.smileessence.R; +import net.lacolaco.smileessence.data.FavoriteCache; +import net.lacolaco.smileessence.data.StatusCache; +import net.lacolaco.smileessence.logging.Logger; +import net.lacolaco.smileessence.notification.NotificationType; +import net.lacolaco.smileessence.notification.Notificator; + +import twitter4j.*; + +public class HomeTimelineTask extends TwitterTask<Status[]> { + + // ------------------------------ FIELDS ------------------------------ + + private final Paging paging; + private final Activity activity; + + // --------------------------- CONSTRUCTORS --------------------------- + + public HomeTimelineTask(Twitter twitter, Activity activity) { + this(twitter, activity, null); + } + + public HomeTimelineTask(Twitter twitter, Activity activity, Paging paging) { + super(twitter); + this.activity = activity; + this.paging = paging; + } + + // ------------------------ OVERRIDE METHODS ------------------------ + + @Override + protected void onPostExecute(twitter4j.Status[] statuses) { + if (statuses.length != 0) { + for (twitter4j.Status status : statuses) { + StatusCache.getInstance().put(status); + FavoriteCache.getInstance().put(status); + } + } + } + + @Override + protected twitter4j.Status[] doInBackground(Void... params) { + ResponseList<twitter4j.Status> responseList; + try { + if (paging == null) { + responseList = twitter.timelines().getHomeTimeline(); + } else { + responseList = twitter.timelines().getHomeTimeline(paging); + } + } catch (TwitterException e) { + e.printStackTrace(); + Logger.error(e.toString()); + if (e.exceededRateLimitation()) { + Notificator.publish(activity, R.string.notice_error_rate_limit, NotificationType.ALERT); + } else { + Notificator.publish(activity, R.string.notice_error_get_home, NotificationType.ALERT); + } + return new twitter4j.Status[0]; + } + return responseList.toArray(new twitter4j.Status[responseList.size()]); + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/MentionsTimelineTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/MentionsTimelineTask.java new file mode 100644 index 00000000..87d9aacf --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/MentionsTimelineTask.java @@ -0,0 +1,90 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import android.app.Activity; + +import net.lacolaco.smileessence.R; +import net.lacolaco.smileessence.data.FavoriteCache; +import net.lacolaco.smileessence.data.StatusCache; +import net.lacolaco.smileessence.logging.Logger; +import net.lacolaco.smileessence.notification.NotificationType; +import net.lacolaco.smileessence.notification.Notificator; + +import twitter4j.*; + +public class MentionsTimelineTask extends TwitterTask<Status[]> { + + // ------------------------------ FIELDS ------------------------------ + + private final Activity activity; + private final Paging paging; + + // --------------------------- CONSTRUCTORS --------------------------- + + protected MentionsTimelineTask(Twitter twitter, Activity activity) { + this(twitter, activity, null); + } + + public MentionsTimelineTask(Twitter twitter, Activity activity, Paging paging) { + super(twitter); + this.activity = activity; + this.paging = paging; + } + + // ------------------------ OVERRIDE METHODS ------------------------ + + @Override + protected void onPostExecute(twitter4j.Status[] statuses) { + if (statuses.length != 0) { + for (twitter4j.Status status : statuses) { + StatusCache.getInstance().put(status); + FavoriteCache.getInstance().put(status); + } + } + } + + @Override + protected twitter4j.Status[] doInBackground(Void... params) { + ResponseList<twitter4j.Status> responseList; + try { + if (paging == null) { + responseList = twitter.timelines().getMentionsTimeline(); + } else { + responseList = twitter.timelines().getMentionsTimeline(paging); + } + } catch (TwitterException e) { + e.printStackTrace(); + Logger.error(e.toString()); + if (e.exceededRateLimitation()) { + Notificator.publish(activity, R.string.notice_error_rate_limit, NotificationType.ALERT); + } else { + Notificator.publish(activity, R.string.notice_error_get_mentions, NotificationType.ALERT); + } + return new twitter4j.Status[0]; + } + return responseList.toArray(new twitter4j.Status[responseList.size()]); + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/MutesIDsTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/MutesIDsTask.java new file mode 100644 index 00000000..d65b4bce --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/MutesIDsTask.java @@ -0,0 +1,74 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import net.lacolaco.smileessence.data.UserCache; +import net.lacolaco.smileessence.logging.Logger; + +import twitter4j.IDs; +import twitter4j.Twitter; +import twitter4j.TwitterException; + +import java.util.ArrayList; +import java.util.List; + +public class MutesIDsTask extends TwitterTask<Long[]> { + + // --------------------------- CONSTRUCTORS --------------------------- + + public MutesIDsTask(Twitter twitter) { + super(twitter); + } + + // ------------------------ OVERRIDE METHODS ------------------------ + + @Override + protected void onPostExecute(Long[] mutesIDs) { + for (Long mutesID : mutesIDs) { + UserCache.getInstance().putInvisibleUser(mutesID); + } + } + + @Override + protected Long[] doInBackground(Void... params) { + try { + List<Long> idList = new ArrayList<>(); + long cursor = -1; + do { + IDs mutesIDs = twitter.getMutesIDs(cursor); + cursor = mutesIDs.getNextCursor(); + for (long id : mutesIDs.getIDs()) { + idList.add(id); + } + } + while (cursor != 0); + + return idList.toArray(new Long[idList.size()]); + } catch (TwitterException e) { + Logger.error(e); + return new Long[0]; + } + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/ReportForSpamTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/ReportForSpamTask.java new file mode 100644 index 00000000..35e71fbc --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/ReportForSpamTask.java @@ -0,0 +1,76 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import android.app.Activity; + +import net.lacolaco.smileessence.R; +import net.lacolaco.smileessence.data.UserCache; +import net.lacolaco.smileessence.logging.Logger; +import net.lacolaco.smileessence.notification.NotificationType; +import net.lacolaco.smileessence.notification.Notificator; + +import twitter4j.Twitter; +import twitter4j.TwitterException; +import twitter4j.User; + +public class ReportForSpamTask extends TwitterTask<User> { + + // ------------------------------ FIELDS ------------------------------ + + private final long userID; + private final Activity activity; + + // --------------------------- CONSTRUCTORS --------------------------- + + public ReportForSpamTask(Twitter twitter, long userID, Activity activity) { + super(twitter); + this.userID = userID; + this.activity = activity; + } + + // ------------------------ OVERRIDE METHODS ------------------------ + + @Override + protected void onPostExecute(User user) { + if (user != null) { + UserCache.getInstance().put(user); + new Notificator(activity, R.string.notice_r4s_succeeded).publish(); + } else { + new Notificator(activity, R.string.notice_r4s_failed, NotificationType.ALERT).publish(); + } + } + + @Override + protected User doInBackground(Void... params) { + try { + return twitter.spamReporting().reportSpam(userID); + } catch (TwitterException e) { + e.printStackTrace(); + Logger.error(e.toString()); + return null; + } + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/RequestTokenTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/RequestTokenTask.java new file mode 100644 index 00000000..bee6484b --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/RequestTokenTask.java @@ -0,0 +1,54 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import net.lacolaco.smileessence.logging.Logger; + +import twitter4j.Twitter; +import twitter4j.TwitterException; +import twitter4j.auth.RequestToken; + +public class RequestTokenTask extends TwitterTask<RequestToken> { + + // ------------------------------ FIELDS ------------------------------ + + + // --------------------------- CONSTRUCTORS --------------------------- + + public RequestTokenTask(Twitter twitter) { + super(twitter); + } + + @Override + protected RequestToken doInBackground(Void... params) { + try { + return twitter.getOAuthRequestToken(); + } catch (TwitterException e) { + e.printStackTrace(); + Logger.error(e.toString()); + return null; + } + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/RetweetTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/RetweetTask.java new file mode 100644 index 00000000..ffd780a6 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/RetweetTask.java @@ -0,0 +1,76 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import android.app.Activity; + +import net.lacolaco.smileessence.R; +import net.lacolaco.smileessence.data.StatusCache; +import net.lacolaco.smileessence.logging.Logger; +import net.lacolaco.smileessence.notification.NotificationType; +import net.lacolaco.smileessence.notification.Notificator; + +import twitter4j.Status; +import twitter4j.Twitter; +import twitter4j.TwitterException; + +public class RetweetTask extends TwitterTask<Status> { + + // ------------------------------ FIELDS ------------------------------ + + private final long statusID; + private final Activity activity; + + // --------------------------- CONSTRUCTORS --------------------------- + + public RetweetTask(Twitter twitter, long statusID, Activity activity) { + super(twitter); + this.statusID = statusID; + this.activity = activity; + } + + // ------------------------ OVERRIDE METHODS ------------------------ + + @Override + protected void onPostExecute(twitter4j.Status status) { + if (status != null) { + StatusCache.getInstance().put(status); + new Notificator(activity, R.string.notice_retweet_succeeded).publish(); + } else { + new Notificator(activity, R.string.notice_retweet_failed, NotificationType.ALERT).publish(); + } + } + + @Override + protected twitter4j.Status doInBackground(Void... params) { + try { + return twitter.tweets().retweetStatus(statusID); + } catch (TwitterException e) { + e.printStackTrace(); + Logger.error(e.toString()); + return null; + } + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/SearchTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/SearchTask.java new file mode 100644 index 00000000..121b8ac1 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/SearchTask.java @@ -0,0 +1,101 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import android.content.res.Configuration; + +import net.lacolaco.smileessence.R; +import net.lacolaco.smileessence.activity.MainActivity; +import net.lacolaco.smileessence.data.FavoriteCache; +import net.lacolaco.smileessence.data.StatusCache; +import net.lacolaco.smileessence.logging.Logger; +import net.lacolaco.smileessence.notification.NotificationType; +import net.lacolaco.smileessence.notification.Notificator; +import net.lacolaco.smileessence.twitter.util.TwitterUtils; + +import twitter4j.Query; +import twitter4j.QueryResult; +import twitter4j.Twitter; +import twitter4j.TwitterException; + +public class SearchTask extends TwitterTask<QueryResult> { + + // ------------------------------ FIELDS ------------------------------ + + private final MainActivity activity; + private final Query query; + + // -------------------------- STATIC METHODS -------------------------- + + public SearchTask(Twitter twitter, String queryString, MainActivity activity) { + this(twitter, getBaseQuery(activity, queryString), activity); + } + + // --------------------------- CONSTRUCTORS --------------------------- + + public SearchTask(Twitter twitter, Query query, MainActivity activity) { + super(twitter); + this.activity = activity; + this.query = query; + } + + public static Query getBaseQuery(MainActivity activity, String queryString) { + Configuration config = activity.getResources().getConfiguration(); + Query query = new Query(); + query.setQuery(queryString); + query.setLang(config.locale.getLanguage()); + query.setCount(TwitterUtils.getPagingCount(activity)); + query.setResultType(Query.RECENT); + return query; + } + + // ------------------------ OVERRIDE METHODS ------------------------ + + @Override + protected void onPostExecute(QueryResult queryResult) { + if (queryResult != null) { + for (twitter4j.Status status : queryResult.getTweets()) { + StatusCache.getInstance().put(status); + FavoriteCache.getInstance().put(status); + } + } + } + + @Override + protected QueryResult doInBackground(Void... params) { + try { + return twitter.search(query); + } catch (TwitterException e) { + e.printStackTrace(); + Logger.debug(e); + if (e.exceededRateLimitation()) { + Notificator.publish(activity, R.string.notice_error_rate_limit, NotificationType.ALERT); + } else { + Notificator.publish(activity, R.string.notice_error_search, NotificationType.ALERT); + } + return null; + } + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/SendMessageTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/SendMessageTask.java new file mode 100644 index 00000000..c3338e63 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/SendMessageTask.java @@ -0,0 +1,79 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import android.app.Activity; + +import net.lacolaco.smileessence.R; +import net.lacolaco.smileessence.data.DirectMessageCache; +import net.lacolaco.smileessence.logging.Logger; +import net.lacolaco.smileessence.notification.NotificationType; +import net.lacolaco.smileessence.notification.Notificator; + +import twitter4j.DirectMessage; +import twitter4j.Twitter; +import twitter4j.TwitterException; + +public class SendMessageTask extends TwitterTask<DirectMessage> { + + // ------------------------------ FIELDS ------------------------------ + + private final String userID; + private final String text; + private final Activity activity; + + // --------------------------- CONSTRUCTORS --------------------------- + + public SendMessageTask(Twitter twitter, String screenName, String text, Activity activity) { + super(twitter); + this.userID = screenName; + this.text = text; + this.activity = activity; + } + + // ------------------------ OVERRIDE METHODS ------------------------ + + @Override + protected void onPostExecute(DirectMessage message) { + if (message != null) { + DirectMessageCache.getInstance().put(message); + new Notificator(activity, R.string.notice_message_send_succeeded).publish(); + } else { + new Notificator(activity, R.string.notice_message_send_failed, NotificationType.ALERT).publish(); + } + } + + @Override + protected DirectMessage doInBackground(Void... params) { + try { + return twitter.directMessages().sendDirectMessage(userID, text); + } catch (TwitterException e) { + e.printStackTrace(); + Logger.error(e.toString()); + return null; + } + } + +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/SentDirectMessagesTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/SentDirectMessagesTask.java new file mode 100644 index 00000000..5cbb6700 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/SentDirectMessagesTask.java @@ -0,0 +1,88 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import android.app.Activity; + +import net.lacolaco.smileessence.R; +import net.lacolaco.smileessence.data.DirectMessageCache; +import net.lacolaco.smileessence.logging.Logger; +import net.lacolaco.smileessence.notification.NotificationType; +import net.lacolaco.smileessence.notification.Notificator; + +import twitter4j.*; + +public class SentDirectMessagesTask extends TwitterTask<DirectMessage[]> { + + // ------------------------------ FIELDS ------------------------------ + + private final Activity activity; + private final Paging paging; + + // --------------------------- CONSTRUCTORS --------------------------- + + protected SentDirectMessagesTask(Twitter twitter, Activity activity) { + this(twitter, activity, null); + } + + public SentDirectMessagesTask(Twitter twitter, Activity activity, Paging paging) { + super(twitter); + this.activity = activity; + this.paging = paging; + } + + // ------------------------ OVERRIDE METHODS ------------------------ + + @Override + protected void onPostExecute(DirectMessage[] directMessages) { + if (directMessages.length != 0) { + for (DirectMessage message : directMessages) { + DirectMessageCache.getInstance().put(message); + } + } + } + + @Override + protected DirectMessage[] doInBackground(Void... params) { + ResponseList<DirectMessage> responseList; + try { + if (paging == null) { + responseList = twitter.directMessages().getSentDirectMessages(); + } else { + responseList = twitter.directMessages().getSentDirectMessages(paging); + } + } catch (TwitterException e) { + e.printStackTrace(); + Logger.error(e.toString()); + if (e.exceededRateLimitation()) { + Notificator.publish(activity, R.string.notice_error_rate_limit, NotificationType.ALERT); + } else { + Notificator.publish(activity, R.string.notice_error_get_messages, NotificationType.ALERT); + } + return new DirectMessage[0]; + } + return responseList.toArray(new DirectMessage[responseList.size()]); + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/ShowDirectMessageTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/ShowDirectMessageTask.java new file mode 100644 index 00000000..f48f1510 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/ShowDirectMessageTask.java @@ -0,0 +1,66 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import net.lacolaco.smileessence.data.DirectMessageCache; +import net.lacolaco.smileessence.logging.Logger; + +import twitter4j.DirectMessage; +import twitter4j.Twitter; +import twitter4j.TwitterException; + +public class ShowDirectMessageTask extends TwitterTask<DirectMessage> { + + // ------------------------------ FIELDS ------------------------------ + + private final long messageID; + + // --------------------------- CONSTRUCTORS --------------------------- + + public ShowDirectMessageTask(Twitter twitter, long messageID) { + super(twitter); + this.messageID = messageID; + } + + // ------------------------ OVERRIDE METHODS ------------------------ + + @Override + protected void onPostExecute(DirectMessage directMessage) { + if (directMessage != null) { + DirectMessageCache.getInstance().put(directMessage); + } + } + + @Override + protected DirectMessage doInBackground(Void... params) { + try { + return twitter.directMessages().showDirectMessage(messageID); + } catch (TwitterException e) { + e.printStackTrace(); + Logger.error(e.toString()); + return null; + } + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/ShowFriendshipTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/ShowFriendshipTask.java new file mode 100644 index 00000000..48547d68 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/ShowFriendshipTask.java @@ -0,0 +1,68 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import net.lacolaco.smileessence.logging.Logger; + +import twitter4j.Relationship; +import twitter4j.Twitter; +import twitter4j.TwitterException; + +public class ShowFriendshipTask extends TwitterTask<Relationship> { + + // ------------------------------ FIELDS ------------------------------ + + private final long userID; + private final String screenName; + + // --------------------------- CONSTRUCTORS --------------------------- + + public ShowFriendshipTask(Twitter twitter, long userID) { + super(twitter); + this.userID = userID; + this.screenName = null; + } + + public ShowFriendshipTask(Twitter twitter, String screenName) { + super(twitter); + this.screenName = screenName; + this.userID = -1; + } + + @Override + protected Relationship doInBackground(Void... params) { + try { + if (screenName != null) { + return twitter.friendsFollowers().showFriendship(twitter.getScreenName(), screenName); + } else { + return twitter.friendsFollowers().showFriendship(twitter.getId(), userID); + } + } catch (TwitterException e) { + e.printStackTrace(); + Logger.error(e.toString()); + return null; + } + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/ShowStatusTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/ShowStatusTask.java new file mode 100644 index 00000000..89066553 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/ShowStatusTask.java @@ -0,0 +1,66 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import net.lacolaco.smileessence.data.StatusCache; +import net.lacolaco.smileessence.logging.Logger; + +import twitter4j.Status; +import twitter4j.Twitter; +import twitter4j.TwitterException; + +public class ShowStatusTask extends TwitterTask<Status> { + + // ------------------------------ FIELDS ------------------------------ + + private final long id; + + // --------------------------- CONSTRUCTORS --------------------------- + + public ShowStatusTask(Twitter twitter, long id) { + super(twitter); + this.id = id; + } + + // ------------------------ OVERRIDE METHODS ------------------------ + + @Override + protected void onPostExecute(twitter4j.Status status) { + if (status != null) { + StatusCache.getInstance().put(status); + } + } + + @Override + protected twitter4j.Status doInBackground(Void... params) { + try { + return twitter.tweets().showStatus(id); + } catch (TwitterException e) { + e.printStackTrace(); + Logger.error(e.toString()); + return null; + } + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/ShowUserTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/ShowUserTask.java new file mode 100644 index 00000000..71706edf --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/ShowUserTask.java @@ -0,0 +1,78 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import net.lacolaco.smileessence.data.UserCache; +import net.lacolaco.smileessence.logging.Logger; + +import twitter4j.Twitter; +import twitter4j.TwitterException; +import twitter4j.User; + +public class ShowUserTask extends TwitterTask<User> { + + // ------------------------------ FIELDS ------------------------------ + + private final long userID; + private final String screenName; + + // --------------------------- CONSTRUCTORS --------------------------- + + public ShowUserTask(Twitter twitter, long userID) { + super(twitter); + this.userID = userID; + this.screenName = null; + } + + public ShowUserTask(Twitter twitter, String screenName) { + super(twitter); + this.screenName = screenName; + this.userID = -1; + } + + // ------------------------ OVERRIDE METHODS ------------------------ + + @Override + protected void onPostExecute(User user) { + if (user != null) { + UserCache.getInstance().put(user); + } + } + + @Override + protected User doInBackground(Void... params) { + try { + if (screenName != null) { + return twitter.users().showUser(screenName); + } else { + return twitter.users().showUser(userID); + } + } catch (TwitterException e) { + e.printStackTrace(); + Logger.error(e.toString()); + return null; + } + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/TweetTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/TweetTask.java new file mode 100644 index 00000000..3cec661b --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/TweetTask.java @@ -0,0 +1,143 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import android.app.Activity; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.os.Environment; +import android.text.TextUtils; + +import net.lacolaco.smileessence.R; +import net.lacolaco.smileessence.data.StatusCache; +import net.lacolaco.smileessence.logging.Logger; +import net.lacolaco.smileessence.notification.NotificationType; +import net.lacolaco.smileessence.notification.Notificator; +import net.lacolaco.smileessence.preference.UserPreferenceHelper; +import net.lacolaco.smileessence.twitter.TwitterApi; + +import twitter4j.Status; +import twitter4j.StatusUpdate; +import twitter4j.Twitter; +import twitter4j.TwitterException; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + +public class TweetTask extends TwitterTask<Status> { + + // ------------------------------ FIELDS ------------------------------ + + private final StatusUpdate update; + private final String mediaPath; + private final Activity activity; + private String tempFilePath; + + // --------------------------- CONSTRUCTORS --------------------------- + + public TweetTask(Twitter twitter, StatusUpdate update, Activity activity) { + this(twitter, update, null, activity); + } + + public TweetTask(Twitter twitter, StatusUpdate update, String mediaPath, Activity activity) { + super(twitter); + this.update = update; + this.mediaPath = mediaPath; + this.activity = activity; + } + + // --------------------- GETTER / SETTER METHODS --------------------- + + public File getMediaFile() { + File file = new File(mediaPath); + boolean resizeFlag = new UserPreferenceHelper(activity).getValue(R.string.key_setting_resize_post_image, false); + if (file.length() >= TwitterApi.MEDIA_SIZE_LIMIT && resizeFlag) { + BitmapFactory.Options opt = new BitmapFactory.Options(); + opt.inJustDecodeBounds = true; //decoder is not return bitmap but set option + BitmapFactory.decodeFile(mediaPath, opt); + tempFilePath = Environment.getExternalStorageDirectory() + "/temp.jpg"; + File compressedFile = new File(tempFilePath); + FileOutputStream fos = null; + try { + fos = new FileOutputStream(compressedFile); + float ratio = (float) file.length() / (float) TwitterApi.MEDIA_SIZE_LIMIT; + BitmapFactory.Options resizeOpt = new BitmapFactory.Options(); + resizeOpt.inPurgeable = true; + resizeOpt.inSampleSize = (int) Math.ceil(ratio); + Bitmap bitmap = BitmapFactory.decodeFile(mediaPath, resizeOpt); + bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); + bitmap.recycle(); + return compressedFile; + } catch (Exception e) { + e.printStackTrace(); + Logger.error(e); + } finally { + try { + fos.close(); + } catch (IOException e) { + e.printStackTrace(); + Logger.error(e); + } + } + } + return file; + } + + // ------------------------ OVERRIDE METHODS ------------------------ + + @Override + protected void onPostExecute(twitter4j.Status status) { + if (status != null) { + StatusCache.getInstance().put(status); + new Notificator(activity, R.string.notice_tweet_succeeded).publish(); + } else { + new Notificator(activity, R.string.notice_tweet_failed, NotificationType.ALERT).publish(); + } + } + + @Override + protected twitter4j.Status doInBackground(Void... params) { + try { + if (TextUtils.isEmpty(mediaPath)) { + return twitter.tweets().updateStatus(update); + } else { + File mediaFile = getMediaFile(); + if (mediaFile.exists()) { + update.setMedia(mediaFile); + } + twitter4j.Status status = twitter.tweets().updateStatus(update); + if (tempFilePath != null) { + new File(tempFilePath).delete(); + } + return status; + } + } catch (TwitterException e) { + e.printStackTrace(); + Logger.error(e.toString()); + return null; + } + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/TwitterTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/TwitterTask.java new file mode 100644 index 00000000..b62e6770 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/TwitterTask.java @@ -0,0 +1,42 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import android.os.AsyncTask; + +import twitter4j.Twitter; + +public abstract class TwitterTask<T> extends AsyncTask<Void, Void, T> { + + // ------------------------------ FIELDS ------------------------------ + + protected Twitter twitter; + + // --------------------------- CONSTRUCTORS --------------------------- + + protected TwitterTask(Twitter twitter) { + this.twitter = twitter; + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/UnblockTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/UnblockTask.java new file mode 100644 index 00000000..9199a136 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/UnblockTask.java @@ -0,0 +1,76 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import android.app.Activity; + +import net.lacolaco.smileessence.R; +import net.lacolaco.smileessence.data.UserCache; +import net.lacolaco.smileessence.logging.Logger; +import net.lacolaco.smileessence.notification.NotificationType; +import net.lacolaco.smileessence.notification.Notificator; + +import twitter4j.Twitter; +import twitter4j.TwitterException; +import twitter4j.User; + +public class UnblockTask extends TwitterTask<User> { + + // ------------------------------ FIELDS ------------------------------ + + private final long userID; + private final Activity activity; + + // --------------------------- CONSTRUCTORS --------------------------- + + public UnblockTask(Twitter twitter, long userID, Activity activity) { + super(twitter); + this.userID = userID; + this.activity = activity; + } + + // ------------------------ OVERRIDE METHODS ------------------------ + + @Override + protected void onPostExecute(User user) { + if (user != null) { + UserCache.getInstance().put(user); + new Notificator(activity, R.string.notice_unblock_succeeded).publish(); + } else { + new Notificator(activity, R.string.notice_unblock_failed, NotificationType.ALERT).publish(); + } + } + + @Override + protected User doInBackground(Void... params) { + try { + return twitter.users().destroyBlock(userID); + } catch (TwitterException e) { + e.printStackTrace(); + Logger.error(e.toString()); + return null; + } + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/UnfavoriteTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/UnfavoriteTask.java new file mode 100644 index 00000000..93eb8623 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/UnfavoriteTask.java @@ -0,0 +1,76 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import android.app.Activity; + +import net.lacolaco.smileessence.R; +import net.lacolaco.smileessence.data.StatusCache; +import net.lacolaco.smileessence.logging.Logger; +import net.lacolaco.smileessence.notification.NotificationType; +import net.lacolaco.smileessence.notification.Notificator; + +import twitter4j.Status; +import twitter4j.Twitter; +import twitter4j.TwitterException; + +public class UnfavoriteTask extends TwitterTask<Status> { + + // ------------------------------ FIELDS ------------------------------ + + private final long statusID; + private final Activity activity; + + // --------------------------- CONSTRUCTORS --------------------------- + + public UnfavoriteTask(Twitter twitter, long statusID, Activity activity) { + super(twitter); + this.statusID = statusID; + this.activity = activity; + } + + // ------------------------ OVERRIDE METHODS ------------------------ + + @Override + protected void onPostExecute(twitter4j.Status status) { + if (status != null) { + StatusCache.getInstance().put(status); + new Notificator(activity, R.string.notice_unfavorite_succeeded).publish(); + } else { + new Notificator(activity, R.string.notice_unfavorite_failed, NotificationType.ALERT).publish(); + } + } + + @Override + protected twitter4j.Status doInBackground(Void... params) { + try { + return twitter.favorites().destroyFavorite(statusID); + } catch (TwitterException e) { + e.printStackTrace(); + Logger.error(e.toString()); + return null; + } + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/UnfollowTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/UnfollowTask.java new file mode 100644 index 00000000..8c5f7dc5 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/UnfollowTask.java @@ -0,0 +1,76 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import android.app.Activity; + +import net.lacolaco.smileessence.R; +import net.lacolaco.smileessence.data.UserCache; +import net.lacolaco.smileessence.logging.Logger; +import net.lacolaco.smileessence.notification.NotificationType; +import net.lacolaco.smileessence.notification.Notificator; + +import twitter4j.Twitter; +import twitter4j.TwitterException; +import twitter4j.User; + +public class UnfollowTask extends TwitterTask<User> { + + // ------------------------------ FIELDS ------------------------------ + + private final long userID; + private final Activity activity; + + // --------------------------- CONSTRUCTORS --------------------------- + + public UnfollowTask(Twitter twitter, long userID, Activity activity) { + super(twitter); + this.userID = userID; + this.activity = activity; + } + + // ------------------------ OVERRIDE METHODS ------------------------ + + @Override + protected void onPostExecute(User user) { + if (user != null) { + UserCache.getInstance().put(user); + new Notificator(activity, R.string.notice_unfollow_succeeded).publish(); + } else { + new Notificator(activity, R.string.notice_unfollow_failed, NotificationType.ALERT).publish(); + } + } + + @Override + protected User doInBackground(Void... params) { + try { + return twitter.friendsFollowers().destroyFriendship(userID); + } catch (TwitterException e) { + e.printStackTrace(); + Logger.error(e.toString()); + return null; + } + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/UserListStatusesTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/UserListStatusesTask.java new file mode 100644 index 00000000..8d56463e --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/UserListStatusesTask.java @@ -0,0 +1,79 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import net.lacolaco.smileessence.activity.MainActivity; +import net.lacolaco.smileessence.data.FavoriteCache; +import net.lacolaco.smileessence.data.StatusCache; +import net.lacolaco.smileessence.logging.Logger; +import net.lacolaco.smileessence.twitter.util.TwitterUtils; + +import twitter4j.*; + +public class UserListStatusesTask extends TwitterTask<Status[]> { + + // ------------------------------ FIELDS ------------------------------ + + private final String listFullName; + private final MainActivity activity; + private final Paging paging; + + // --------------------------- CONSTRUCTORS --------------------------- + + public UserListStatusesTask(Twitter twitter, String listFullName, MainActivity activity) { + this(twitter, listFullName, activity, TwitterUtils.getPaging(TwitterUtils.getPagingCount(activity))); + } + + public UserListStatusesTask(Twitter twitter, String listFullName, MainActivity activity, Paging paging) { + super(twitter); + this.listFullName = listFullName; + this.activity = activity; + this.paging = paging; + } + + // ------------------------ OVERRIDE METHODS ------------------------ + + @Override + protected void onPostExecute(twitter4j.Status[] statuses) { + for (twitter4j.Status status : statuses) { + StatusCache.getInstance().put(status); + FavoriteCache.getInstance().put(status); + } + } + + @Override + protected twitter4j.Status[] doInBackground(Void... params) { + ResponseList<twitter4j.Status> responseList; + try { + String[] strings = listFullName.split("/"); + responseList = twitter.list().getUserListStatuses(strings[0], strings[1], paging); + } catch (TwitterException e) { + e.printStackTrace(); + Logger.error(e.toString()); + return new twitter4j.Status[0]; + } + return responseList.toArray(new twitter4j.Status[responseList.size()]); + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/UserTimelineTask.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/UserTimelineTask.java new file mode 100644 index 00000000..6950c563 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/UserTimelineTask.java @@ -0,0 +1,77 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.twitter.task; + +import net.lacolaco.smileessence.data.StatusCache; +import net.lacolaco.smileessence.logging.Logger; + +import twitter4j.*; + +public class UserTimelineTask extends TwitterTask<Status[]> { + + // ------------------------------ FIELDS ------------------------------ + + private final long userID; + private final Paging paging; + + // --------------------------- CONSTRUCTORS --------------------------- + + public UserTimelineTask(Twitter twitter, long userID) { + this(twitter, userID, null); + } + + public UserTimelineTask(Twitter twitter, long userID, Paging paging) { + super(twitter); + this.userID = userID; + this.paging = paging; + } + + // ------------------------ OVERRIDE METHODS ------------------------ + + @Override + protected void onPostExecute(twitter4j.Status[] statuses) { + for (twitter4j.Status status : statuses) { + StatusCache.getInstance().put(status); + } + + } + + @Override + protected twitter4j.Status[] doInBackground(Void... params) { + ResponseList<twitter4j.Status> responseList; + try { + if (paging == null) { + responseList = twitter.timelines().getUserTimeline(userID); + } else { + responseList = twitter.timelines().getUserTimeline(userID, paging); + } + } catch (TwitterException e) { + e.printStackTrace(); + Logger.error(e.toString()); + return new twitter4j.Status[0]; + } + return responseList.toArray(new twitter4j.Status[responseList.size()]); + } +} |