From a7e84ccfe913e8211fa7c19b380c09139527bb3e Mon Sep 17 00:00:00 2001 From: dosiecki Date: Mon, 2 Jan 2017 18:57:01 -0800 Subject: [PATCH] support for custom server endpoints (#921) --- .../res/layout/fragment_loginregister.xml | 27 ++++- .../android/NewsBlur/res/values/strings.xml | 4 +- .../com/newsblur/activity/AddFacebook.java | 4 +- .../src/com/newsblur/activity/AddTwitter.java | 4 +- .../fragment/LoginRegisterFragment.java | 13 +++ .../com/newsblur/network/APIConstants.java | 107 ++++++++++-------- .../src/com/newsblur/network/APIManager.java | 85 +++++++------- .../src/com/newsblur/util/PrefConstants.java | 1 + .../src/com/newsblur/util/PrefsUtils.java | 28 ++++- 9 files changed, 175 insertions(+), 98 deletions(-) diff --git a/clients/android/NewsBlur/res/layout/fragment_loginregister.xml b/clients/android/NewsBlur/res/layout/fragment_loginregister.xml index edc6769af..36b424259 100644 --- a/clients/android/NewsBlur/res/layout/fragment_loginregister.xml +++ b/clients/android/NewsBlur/res/layout/fragment_loginregister.xml @@ -12,7 +12,7 @@ android:paddingBottom="20dp"> + + + + password email address Log In - I forgot my password + I forgot my password + I have a custom server + https://www.newsblur.com There was problem logging in to NewsBlur. Logging in… Logged in! diff --git a/clients/android/NewsBlur/src/com/newsblur/activity/AddFacebook.java b/clients/android/NewsBlur/src/com/newsblur/activity/AddFacebook.java index 86bda640f..b30ce2d98 100644 --- a/clients/android/NewsBlur/src/com/newsblur/activity/AddFacebook.java +++ b/clients/android/NewsBlur/src/com/newsblur/activity/AddFacebook.java @@ -23,7 +23,7 @@ public class AddFacebook extends NbActivity { webview.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url){ - if (TextUtils.equals(url, APIConstants.NEWSBLUR_URL + "/")) { + if (TextUtils.equals(url, APIConstants.buildUrl("/"))) { AddFacebook.this.setResult(FACEBOOK_AUTHED); AddFacebook.this.finish(); return true; @@ -33,7 +33,7 @@ public class AddFacebook extends NbActivity { } }); - webview.loadUrl(APIConstants.URL_CONNECT_FACEBOOK); + webview.loadUrl(APIConstants.buildUrl(APIConstants.PATH_CONNECT_FACEBOOK)); } } diff --git a/clients/android/NewsBlur/src/com/newsblur/activity/AddTwitter.java b/clients/android/NewsBlur/src/com/newsblur/activity/AddTwitter.java index df9d237c9..e40fbc1dc 100644 --- a/clients/android/NewsBlur/src/com/newsblur/activity/AddTwitter.java +++ b/clients/android/NewsBlur/src/com/newsblur/activity/AddTwitter.java @@ -23,7 +23,7 @@ public class AddTwitter extends NbActivity { webview.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url){ - if (TextUtils.equals(url, APIConstants.NEWSBLUR_URL + "/")) { + if (TextUtils.equals(url, APIConstants.buildUrl("/"))) { AddTwitter.this.setResult(TWITTER_AUTHED); AddTwitter.this.finish(); return true; @@ -33,7 +33,7 @@ public class AddTwitter extends NbActivity { } }); - webview.loadUrl(APIConstants.URL_CONNECT_TWITTER); + webview.loadUrl(APIConstants.buildUrl(APIConstants.PATH_CONNECT_TWITTER)); } } diff --git a/clients/android/NewsBlur/src/com/newsblur/fragment/LoginRegisterFragment.java b/clients/android/NewsBlur/src/com/newsblur/fragment/LoginRegisterFragment.java index fcf2f20ce..d63bdef88 100644 --- a/clients/android/NewsBlur/src/com/newsblur/fragment/LoginRegisterFragment.java +++ b/clients/android/NewsBlur/src/com/newsblur/fragment/LoginRegisterFragment.java @@ -22,7 +22,9 @@ import butterknife.OnClick; import com.newsblur.R; import com.newsblur.activity.LoginProgress; import com.newsblur.activity.RegisterProgress; +import com.newsblur.network.APIConstants; import com.newsblur.util.AppConstants; +import com.newsblur.util.PrefsUtils; public class LoginRegisterFragment extends Fragment { @@ -32,6 +34,8 @@ public class LoginRegisterFragment extends Fragment { @Bind(R.id.registration_password) EditText register_password; @Bind(R.id.registration_email) EditText register_email; @Bind(R.id.login_viewswitcher) ViewSwitcher viewSwitcher; + @Bind(R.id.login_custom_server) View customServer; + @Bind(R.id.login_custom_server_value) EditText customServerValue; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -63,6 +67,10 @@ public class LoginRegisterFragment extends Fragment { @OnClick(R.id.login_button) void logIn() { if (!TextUtils.isEmpty(username.getText().toString())) { + // set the custom server endpoint before any API access, even the cookie fetch. + APIConstants.setCustomServer(customServerValue.getText().toString()); + PrefsUtils.saveCustomServer(getActivity(), customServerValue.getText().toString()); + Intent i = new Intent(getActivity(), LoginProgress.class); i.putExtra("username", username.getText().toString()); i.putExtra("password", password.getText().toString()); @@ -96,4 +104,9 @@ public class LoginRegisterFragment extends Fragment { } } + @OnClick(R.id.login_custom_server) void showCustomServer() { + customServer.setVisibility(View.GONE); + customServerValue.setVisibility(View.VISIBLE); + } + } diff --git a/clients/android/NewsBlur/src/com/newsblur/network/APIConstants.java b/clients/android/NewsBlur/src/com/newsblur/network/APIConstants.java index 945897c42..e8bb6989a 100644 --- a/clients/android/NewsBlur/src/com/newsblur/network/APIConstants.java +++ b/clients/android/NewsBlur/src/com/newsblur/network/APIConstants.java @@ -4,52 +4,72 @@ public class APIConstants { private APIConstants() {} // util class - no instances - public static final String NEWSBLUR_URL = "https://www.newsblur.com"; - public static final String COOKIE_DOMAIN = ".newsblur.com"; - public static final String URL_AUTOFOLLOW_PREF = NEWSBLUR_URL + "/profile/set_preference"; - + private static final String DEFAULT_NEWSBLUR_URL_BASE = "https://www.newsblur.com"; + + private static String CurrentUrlBase = DEFAULT_NEWSBLUR_URL_BASE; + + public static void setCustomServer(String newUrlBase) { + if (newUrlBase == null) return; + if (newUrlBase.length() <= 0) return; + android.util.Log.i(APIConstants.class.getName(), "setting custom server: " + newUrlBase); + CurrentUrlBase = newUrlBase; + } + + public static void unsetCustomServer() { + CurrentUrlBase = DEFAULT_NEWSBLUR_URL_BASE; + } + + public static boolean isCustomServer() { + return DEFAULT_NEWSBLUR_URL_BASE.equals(CurrentUrlBase); + } + // TODO: make use of trailing slashes on URLs consistent or document why // they are not. - public static final String URL_LOGIN = NEWSBLUR_URL + "/api/login"; - public static final String URL_LOGINAS = NEWSBLUR_URL + "/reader/login_as"; - public static final String URL_FEEDS = NEWSBLUR_URL + "/reader/feeds/"; - public static final String URL_USER_PROFILE = NEWSBLUR_URL + "/social/profile"; - public static final String URL_MY_PROFILE = NEWSBLUR_URL + "/social/load_user_profile"; - public static final String URL_FOLLOW = NEWSBLUR_URL + "/social/follow"; - public static final String URL_UNFOLLOW = NEWSBLUR_URL + "/social/unfollow"; + public static final String PATH_LOGIN = "/api/login"; + public static final String PATH_LOGINAS = "/reader/login_as"; + public static final String PATH_FEEDS = "/reader/feeds/"; + public static final String PATH_USER_PROFILE = "/social/profile"; + public static final String PATH_MY_PROFILE = "/social/load_user_profile"; + public static final String PATH_FOLLOW = "/social/follow"; + public static final String PATH_UNFOLLOW = "/social/unfollow"; + public static final String PATH_AUTOFOLLOW_PREF = "/profile/set_preference"; + public static final String PATH_USER_ACTIVITIES = "/social/activities"; + public static final String PATH_USER_INTERACTIONS = "/social/interactions"; + public static final String PATH_RIVER_STORIES = "/reader/river_stories"; + public static final String PATH_SHARED_RIVER_STORIES = "/social/river_stories"; + public static final String PATH_FEED_STORIES = "/reader/feed"; + public static final String PATH_FEED_UNREAD_COUNT = "/reader/feed_unread_count"; + public static final String PATH_SOCIALFEED_STORIES = "/social/stories"; + public static final String PATH_SIGNUP = "/api/signup"; + public static final String PATH_MARK_FEED_AS_READ = "/reader/mark_feed_as_read/"; + public static final String PATH_MARK_ALL_AS_READ = "/reader/mark_all_as_read/"; + public static final String PATH_MARK_STORIES_READ = "/reader/mark_story_hashes_as_read/"; + public static final String PATH_SHARE_STORY = "/social/share_story"; + public static final String PATH_MARK_STORY_AS_STARRED = "/reader/mark_story_hash_as_starred/"; + public static final String PATH_MARK_STORY_AS_UNSTARRED = "/reader/mark_story_hash_as_unstarred/"; + public static final String PATH_MARK_STORY_AS_UNREAD = "/reader/mark_story_as_unread/"; + public static final String PATH_MARK_STORY_HASH_UNREAD = "/reader/mark_story_hash_as_unread/"; + public static final String PATH_STARRED_STORIES = "/reader/starred_stories"; + public static final String PATH_FEED_AUTOCOMPLETE = "/rss_feeds/feed_autocomplete"; + public static final String PATH_LIKE_COMMENT = "/social/like_comment"; + public static final String PATH_UNLIKE_COMMENT = "/social/remove_like_comment"; + public static final String PATH_REPLY_TO = "/social/save_comment_reply"; + public static final String PATH_ADD_FEED = "/reader/add_url"; + public static final String PATH_DELETE_FEED = "/reader/delete_feed"; + public static final String PATH_CLASSIFIER_SAVE = "/classifier/save"; + public static final String PATH_STORY_TEXT = "/rss_feeds/original_text"; + public static final String PATH_UNREAD_HASHES = "/reader/unread_story_hashes"; + public static final String PATH_READ_STORIES = "/reader/read_stories"; + public static final String PATH_MOVE_FEED_TO_FOLDERS = "/reader/move_feed_to_folders"; + public static final String PATH_SAVE_FEED_CHOOSER = "/reader/save_feed_chooser"; + public static final String PATH_CONNECT_FACEBOOK = "/oauth/facebook_connect/"; + public static final String PATH_CONNECT_TWITTER = "/oauth/twitter_connect/"; + + public static String buildUrl(String path) { + return CurrentUrlBase + path; + } - public static final String URL_USER_ACTIVITIES = NEWSBLUR_URL + "/social/activities"; - public static final String URL_USER_INTERACTIONS = NEWSBLUR_URL + "/social/interactions"; - public static final String URL_RIVER_STORIES = NEWSBLUR_URL + "/reader/river_stories"; - public static final String URL_SHARED_RIVER_STORIES = NEWSBLUR_URL + "/social/river_stories"; - - public static final String URL_FEED_STORIES = NEWSBLUR_URL + "/reader/feed"; - public static final String URL_FEED_UNREAD_COUNT = NEWSBLUR_URL + "/reader/feed_unread_count"; - public static final String URL_SOCIALFEED_STORIES = NEWSBLUR_URL + "/social/stories"; - public static final String URL_SIGNUP = NEWSBLUR_URL + "/api/signup"; - public static final String URL_MARK_FEED_AS_READ = NEWSBLUR_URL + "/reader/mark_feed_as_read/"; - public static final String URL_MARK_ALL_AS_READ = NEWSBLUR_URL + "/reader/mark_all_as_read/"; - public static final String URL_MARK_STORIES_READ = NEWSBLUR_URL + "/reader/mark_story_hashes_as_read/"; - public static final String URL_SHARE_STORY = NEWSBLUR_URL + "/social/share_story"; - public static final String URL_MARK_STORY_AS_STARRED = NEWSBLUR_URL + "/reader/mark_story_hash_as_starred/"; - public static final String URL_MARK_STORY_AS_UNSTARRED = NEWSBLUR_URL + "/reader/mark_story_hash_as_unstarred/"; - public static final String URL_MARK_STORY_AS_UNREAD = NEWSBLUR_URL + "/reader/mark_story_as_unread/"; - public static final String URL_MARK_STORY_HASH_UNREAD = NEWSBLUR_URL + "/reader/mark_story_hash_as_unread/"; - public static final String URL_STARRED_STORIES = NEWSBLUR_URL + "/reader/starred_stories"; - public static final String URL_FEED_AUTOCOMPLETE = NEWSBLUR_URL + "/rss_feeds/feed_autocomplete"; - public static final String URL_LIKE_COMMENT = NEWSBLUR_URL + "/social/like_comment"; - public static final String URL_UNLIKE_COMMENT = NEWSBLUR_URL + "/social/remove_like_comment"; - public static final String URL_REPLY_TO = NEWSBLUR_URL + "/social/save_comment_reply"; - public static final String URL_ADD_FEED = NEWSBLUR_URL + "/reader/add_url"; - public static final String URL_DELETE_FEED = NEWSBLUR_URL + "/reader/delete_feed"; - public static final String URL_CLASSIFIER_SAVE = NEWSBLUR_URL + "/classifier/save"; - public static final String URL_STORY_TEXT = NEWSBLUR_URL + "/rss_feeds/original_text"; - public static final String URL_UNREAD_HASHES = NEWSBLUR_URL + "/reader/unread_story_hashes"; - public static final String URL_READ_STORIES = NEWSBLUR_URL + "/reader/read_stories"; - public static final String URL_MOVE_FEED_TO_FOLDERS = NEWSBLUR_URL + "/reader/move_feed_to_folders"; - public static final String URL_SAVE_FEED_CHOOSER = NEWSBLUR_URL + "/reader/save_feed_chooser"; - public static final String PARAMETER_FEEDS = "f"; public static final String PARAMETER_H = "h"; public static final String PARAMETER_PASSWORD = "password"; @@ -96,8 +116,5 @@ public class APIConstants { public static final String VALUE_TRUE = "true"; public static final String VALUE_STARRED = "starred"; - public static final String URL_CONNECT_FACEBOOK = NEWSBLUR_URL + "/oauth/facebook_connect/"; - public static final String URL_CONNECT_TWITTER = NEWSBLUR_URL + "/oauth/twitter_connect/"; - public static final String S3_URL_FEED_ICONS = "https://s3.amazonaws.com/icons.newsblur.com/"; } diff --git a/clients/android/NewsBlur/src/com/newsblur/network/APIManager.java b/clients/android/NewsBlur/src/com/newsblur/network/APIManager.java index b7b36cf4d..1abe5133d 100644 --- a/clients/android/NewsBlur/src/com/newsblur/network/APIManager.java +++ b/clients/android/NewsBlur/src/com/newsblur/network/APIManager.java @@ -26,6 +26,7 @@ import com.newsblur.domain.Feed; import com.newsblur.domain.FeedResult; import com.newsblur.domain.Story; import com.newsblur.domain.ValueMultimap; +import static com.newsblur.network.APIConstants.buildUrl; import com.newsblur.network.domain.ActivitiesResponse; import com.newsblur.network.domain.FeedFolderResponse; import com.newsblur.network.domain.InteractionsResponse; @@ -66,6 +67,8 @@ public class APIManager { public APIManager(final Context context) { this.context = context; + APIConstants.setCustomServer(PrefsUtils.getCustomServer(context)); + this.gson = new GsonBuilder() .registerTypeAdapter(Date.class, new DateStringTypeAdapter()) .registerTypeAdapter(Boolean.class, new BooleanTypeAdapter()) @@ -96,7 +99,7 @@ public class APIManager { final ContentValues values = new ContentValues(); values.put(APIConstants.PARAMETER_USERNAME, username); values.put(APIConstants.PARAMETER_PASSWORD, password); - final APIResponse response = post(APIConstants.URL_LOGIN, values); + final APIResponse response = post(buildUrl(APIConstants.PATH_LOGIN), values); LoginResponse loginResponse = response.getLoginResponse(gson); if (!response.isError()) { PrefsUtils.saveLogin(context, username, response.getCookie()); @@ -107,7 +110,7 @@ public class APIManager { public boolean loginAs(String username) { ContentValues values = new ContentValues(); values.put(APIConstants.PARAMETER_USER, username); - String urlString = APIConstants.URL_LOGINAS + "?" + builderGetParametersString(values); + String urlString = buildUrl(APIConstants.PATH_LOGINAS) + "?" + builderGetParametersString(values); Log.i(this.getClass().getName(), "doing superuser swap: " + urlString); // This API returns a redirect that means the call worked, but we do not want to follow it. To // just get the cookie from the 302 and stop, we directly use a one-off OkHttpClient. @@ -130,7 +133,7 @@ public class APIManager { public boolean setAutoFollow(boolean autofollow) { ContentValues values = new ContentValues(); values.put("autofollow_friends", autofollow ? "true" : "false"); - final APIResponse response = post(APIConstants.URL_AUTOFOLLOW_PREF, values); + final APIResponse response = post(buildUrl(APIConstants.PATH_AUTOFOLLOW_PREF), values); return (!response.isError()); } @@ -175,42 +178,42 @@ public class APIManager { values.put(APIConstants.PARAMETER_DIRECTION, APIConstants.VALUE_NEWER); } - APIResponse response = post(APIConstants.URL_MARK_FEED_AS_READ, values); + APIResponse response = post(buildUrl(APIConstants.PATH_MARK_FEED_AS_READ), values); return response.getResponse(gson, NewsBlurResponse.class); } private NewsBlurResponse markAllAsRead() { ValueMultimap values = new ValueMultimap(); values.put(APIConstants.PARAMETER_DAYS, "0"); - APIResponse response = post(APIConstants.URL_MARK_ALL_AS_READ, values); + APIResponse response = post(buildUrl(APIConstants.PATH_MARK_ALL_AS_READ), values); return response.getResponse(gson, NewsBlurResponse.class); } public NewsBlurResponse markStoryAsRead(String storyHash) { ValueMultimap values = new ValueMultimap(); values.put(APIConstants.PARAMETER_STORY_HASH, storyHash); - APIResponse response = post(APIConstants.URL_MARK_STORIES_READ, values); + APIResponse response = post(buildUrl(APIConstants.PATH_MARK_STORIES_READ), values); return response.getResponse(gson, NewsBlurResponse.class); } public NewsBlurResponse markStoryAsStarred(String storyHash) { ValueMultimap values = new ValueMultimap(); values.put(APIConstants.PARAMETER_STORY_HASH, storyHash); - APIResponse response = post(APIConstants.URL_MARK_STORY_AS_STARRED, values); + APIResponse response = post(buildUrl(APIConstants.PATH_MARK_STORY_AS_STARRED), values); return response.getResponse(gson, NewsBlurResponse.class); } public NewsBlurResponse markStoryAsUnstarred(String storyHash) { ValueMultimap values = new ValueMultimap(); values.put(APIConstants.PARAMETER_STORY_HASH, storyHash); - APIResponse response = post(APIConstants.URL_MARK_STORY_AS_UNSTARRED, values); + APIResponse response = post(buildUrl(APIConstants.PATH_MARK_STORY_AS_UNSTARRED), values); return response.getResponse(gson, NewsBlurResponse.class); } public NewsBlurResponse markStoryHashUnread(String hash) { final ValueMultimap values = new ValueMultimap(); values.put(APIConstants.PARAMETER_STORY_HASH, hash); - APIResponse response = post(APIConstants.URL_MARK_STORY_HASH_UNREAD, values); + APIResponse response = post(buildUrl(APIConstants.PATH_MARK_STORY_HASH_UNREAD), values); return response.getResponse(gson, NewsBlurResponse.class); } @@ -219,7 +222,7 @@ public class APIManager { values.put(APIConstants.PARAMETER_USERNAME, username); values.put(APIConstants.PARAMETER_PASSWORD, password); values.put(APIConstants.PARAMETER_EMAIL, email); - final APIResponse response = post(APIConstants.URL_SIGNUP, values); + final APIResponse response = post(buildUrl(APIConstants.PATH_SIGNUP), values); RegisterResponse registerResponse = response.getRegisterResponse(gson); if (!response.isError()) { PrefsUtils.saveLogin(context, username, response.getCookie()); @@ -228,7 +231,7 @@ public class APIManager { } public ProfileResponse updateUserProfile() { - final APIResponse response = get(APIConstants.URL_MY_PROFILE); + final APIResponse response = get(buildUrl(APIConstants.PATH_MY_PROFILE)); if (!response.isError()) { ProfileResponse profileResponse = (ProfileResponse) response.getResponse(gson, ProfileResponse.class); PrefsUtils.saveUserDetails(context, profileResponse.user); @@ -249,7 +252,7 @@ public class APIManager { values.put(APIConstants.PARAMETER_IN_FOLDERS, folder); } values.put(APIConstants.PARAMETER_FEEDID, feedId); - APIResponse response = post(APIConstants.URL_MOVE_FEED_TO_FOLDERS, values); + APIResponse response = post(buildUrl(APIConstants.PATH_MOVE_FEED_TO_FOLDERS), values); return response.getResponse(gson, NewsBlurResponse.class); } @@ -258,14 +261,14 @@ public class APIManager { for (String id : apiIds) { values.put(APIConstants.PARAMETER_FEEDID, id); } - APIResponse response = get(APIConstants.URL_FEED_UNREAD_COUNT, values); + APIResponse response = get(buildUrl(APIConstants.PATH_FEED_UNREAD_COUNT), values); return (UnreadCountResponse) response.getResponse(gson, UnreadCountResponse.class); } public UnreadStoryHashesResponse getUnreadStoryHashes() { ValueMultimap values = new ValueMultimap(); values.put(APIConstants.PARAMETER_INCLUDE_TIMESTAMPS, "1"); - APIResponse response = get(APIConstants.URL_UNREAD_HASHES, values); + APIResponse response = get(buildUrl(APIConstants.PATH_UNREAD_HASHES), values); return (UnreadStoryHashesResponse) response.getResponse(gson, UnreadStoryHashesResponse.class); } @@ -275,7 +278,7 @@ public class APIManager { values.put(APIConstants.PARAMETER_H, hash); } values.put(APIConstants.PARAMETER_INCLUDE_HIDDEN, APIConstants.VALUE_TRUE); - APIResponse response = get(APIConstants.URL_RIVER_STORIES, values); + APIResponse response = get(buildUrl(APIConstants.PATH_RIVER_STORIES), values); return (StoriesResponse) response.getResponse(gson, StoriesResponse.class); } @@ -289,40 +292,40 @@ public class APIManager { // create the URI and populate request params depending on what kind of stories we want if (fs.getSingleFeed() != null) { - uri = Uri.parse(APIConstants.URL_FEED_STORIES).buildUpon().appendPath(fs.getSingleFeed()).build(); + uri = Uri.parse(buildUrl(APIConstants.PATH_FEED_STORIES)).buildUpon().appendPath(fs.getSingleFeed()).build(); values.put(APIConstants.PARAMETER_FEEDS, fs.getSingleFeed()); values.put(APIConstants.PARAMETER_INCLUDE_HIDDEN, APIConstants.VALUE_TRUE); if (fs.isFilterSaved()) values.put(APIConstants.PARAMETER_READ_FILTER, APIConstants.VALUE_STARRED); } else if (fs.getMultipleFeeds() != null) { - uri = Uri.parse(APIConstants.URL_RIVER_STORIES); + uri = Uri.parse(buildUrl(APIConstants.PATH_RIVER_STORIES)); for (String feedId : fs.getMultipleFeeds()) values.put(APIConstants.PARAMETER_FEEDS, feedId); values.put(APIConstants.PARAMETER_INCLUDE_HIDDEN, APIConstants.VALUE_TRUE); if (fs.isFilterSaved()) values.put(APIConstants.PARAMETER_READ_FILTER, APIConstants.VALUE_STARRED); } else if (fs.getSingleSocialFeed() != null) { String feedId = fs.getSingleSocialFeed().getKey(); String username = fs.getSingleSocialFeed().getValue(); - uri = Uri.parse(APIConstants.URL_SOCIALFEED_STORIES).buildUpon().appendPath(feedId).appendPath(username).build(); + uri = Uri.parse(buildUrl(APIConstants.PATH_SOCIALFEED_STORIES)).buildUpon().appendPath(feedId).appendPath(username).build(); values.put(APIConstants.PARAMETER_USER_ID, feedId); values.put(APIConstants.PARAMETER_USERNAME, username); } else if (fs.getMultipleSocialFeeds() != null) { - uri = Uri.parse(APIConstants.URL_SHARED_RIVER_STORIES); + uri = Uri.parse(buildUrl(APIConstants.PATH_SHARED_RIVER_STORIES)); for (Map.Entry entry : fs.getMultipleSocialFeeds().entrySet()) { values.put(APIConstants.PARAMETER_FEEDS, entry.getKey()); } } else if (fs.isAllNormal()) { - uri = Uri.parse(APIConstants.URL_RIVER_STORIES); + uri = Uri.parse(buildUrl(APIConstants.PATH_RIVER_STORIES)); values.put(APIConstants.PARAMETER_INCLUDE_HIDDEN, APIConstants.VALUE_TRUE); } else if (fs.isAllSocial()) { - uri = Uri.parse(APIConstants.URL_SHARED_RIVER_STORIES); + uri = Uri.parse(buildUrl(APIConstants.PATH_SHARED_RIVER_STORIES)); } else if (fs.isAllRead()) { - uri = Uri.parse(APIConstants.URL_READ_STORIES); + uri = Uri.parse(buildUrl(APIConstants.PATH_READ_STORIES)); } else if (fs.isAllSaved()) { - uri = Uri.parse(APIConstants.URL_STARRED_STORIES); + uri = Uri.parse(buildUrl(APIConstants.PATH_STARRED_STORIES)); } else if (fs.getSingleSavedTag() != null) { - uri = Uri.parse(APIConstants.URL_STARRED_STORIES); + uri = Uri.parse(buildUrl(APIConstants.PATH_STARRED_STORIES)); values.put(APIConstants.PARAMETER_TAG, fs.getSingleSavedTag()); } else if (fs.isGlobalShared()) { - uri = Uri.parse(APIConstants.URL_SHARED_RIVER_STORIES); + uri = Uri.parse(buildUrl(APIConstants.PATH_SHARED_RIVER_STORIES)); values.put(APIConstants.PARAMETER_GLOBAL_FEED, Boolean.TRUE.toString()); } else { throw new IllegalStateException("Asked to get stories for FeedSet of unknown type."); @@ -347,7 +350,7 @@ public class APIManager { public boolean followUser(final String userId) { final ContentValues values = new ContentValues(); values.put(APIConstants.PARAMETER_USERID, userId); - final APIResponse response = post(APIConstants.URL_FOLLOW, values); + final APIResponse response = post(buildUrl(APIConstants.PATH_FOLLOW), values); if (!response.isError()) { return true; } else { @@ -358,7 +361,7 @@ public class APIManager { public boolean unfollowUser(final String userId) { final ContentValues values = new ContentValues(); values.put(APIConstants.PARAMETER_USERID, userId); - final APIResponse response = post(APIConstants.URL_UNFOLLOW, values); + final APIResponse response = post(buildUrl(APIConstants.PATH_UNFOLLOW), values); if (!response.isError()) { return true; } else { @@ -377,7 +380,7 @@ public class APIManager { values.put(APIConstants.PARAMETER_FEEDID, feedId); values.put(APIConstants.PARAMETER_STORYID, storyId); - APIResponse response = post(APIConstants.URL_SHARE_STORY, values); + APIResponse response = post(buildUrl(APIConstants.PATH_SHARE_STORY), values); return response.getResponse(gson, NewsBlurResponse.class); } @@ -392,7 +395,7 @@ public class APIManager { public FeedFolderResponse getFolderFeedMapping(boolean doUpdateCounts) { ContentValues params = new ContentValues(); params.put(APIConstants.PARAMETER_UPDATE_COUNTS, (doUpdateCounts ? "true" : "false")); - APIResponse response = get(APIConstants.URL_FEEDS, params); + APIResponse response = get(buildUrl(APIConstants.PATH_FEEDS), params); if (response.isError()) { // we can't use the magic polymorphism of NewsBlurResponse because this result uses @@ -454,14 +457,14 @@ public class APIManager { } values.put(APIConstants.PARAMETER_FEEDID, feedId); - final APIResponse response = post(APIConstants.URL_CLASSIFIER_SAVE, values); + final APIResponse response = post(buildUrl(APIConstants.PATH_CLASSIFIER_SAVE), values); return response.getResponse(gson, NewsBlurResponse.class); } public ProfileResponse getUser(String userId) { final ContentValues values = new ContentValues(); values.put(APIConstants.PARAMETER_USER_ID, userId); - final APIResponse response = get(APIConstants.URL_USER_PROFILE, values); + final APIResponse response = get(buildUrl(APIConstants.PATH_USER_PROFILE), values); if (!response.isError()) { ProfileResponse profileResponse = (ProfileResponse) response.getResponse(gson, ProfileResponse.class); return profileResponse; @@ -475,7 +478,7 @@ public class APIManager { values.put(APIConstants.PARAMETER_USER_ID, userId); values.put(APIConstants.PARAMETER_LIMIT, "10"); values.put(APIConstants.PARAMETER_PAGE_NUMBER, Integer.toString(pageNumber)); - final APIResponse response = get(APIConstants.URL_USER_ACTIVITIES, values); + final APIResponse response = get(buildUrl(APIConstants.PATH_USER_ACTIVITIES), values); if (!response.isError()) { ActivitiesResponse activitiesResponse = (ActivitiesResponse) response.getResponse(gson, ActivitiesResponse.class); return activitiesResponse; @@ -489,7 +492,7 @@ public class APIManager { values.put(APIConstants.PARAMETER_USER_ID, userId); values.put(APIConstants.PARAMETER_LIMIT, "10"); values.put(APIConstants.PARAMETER_PAGE_NUMBER, Integer.toString(pageNumber)); - final APIResponse response = get(APIConstants.URL_USER_INTERACTIONS, values); + final APIResponse response = get(buildUrl(APIConstants.PATH_USER_INTERACTIONS), values); if (!response.isError()) { InteractionsResponse interactionsResponse = (InteractionsResponse) response.getResponse(gson, InteractionsResponse.class); return interactionsResponse; @@ -502,7 +505,7 @@ public class APIManager { final ContentValues values = new ContentValues(); values.put(APIConstants.PARAMETER_FEEDID, feedId); values.put(APIConstants.PARAMETER_STORYID, storyId); - final APIResponse response = get(APIConstants.URL_STORY_TEXT, values); + final APIResponse response = get(buildUrl(APIConstants.PATH_STORY_TEXT), values); if (!response.isError()) { StoryTextResponse storyTextResponse = (StoryTextResponse) response.getResponse(gson, StoryTextResponse.class); return storyTextResponse; @@ -516,7 +519,7 @@ public class APIManager { values.put(APIConstants.PARAMETER_STORYID, storyId); values.put(APIConstants.PARAMETER_STORY_FEEDID, feedId); values.put(APIConstants.PARAMETER_COMMENT_USERID, commentUserId); - APIResponse response = post(APIConstants.URL_LIKE_COMMENT, values); + APIResponse response = post(buildUrl(APIConstants.PATH_LIKE_COMMENT), values); return response.getResponse(gson, NewsBlurResponse.class); } @@ -525,7 +528,7 @@ public class APIManager { values.put(APIConstants.PARAMETER_STORYID, storyId); values.put(APIConstants.PARAMETER_STORY_FEEDID, feedId); values.put(APIConstants.PARAMETER_COMMENT_USERID, commentUserId); - APIResponse response = post(APIConstants.URL_UNLIKE_COMMENT, values); + APIResponse response = post(buildUrl(APIConstants.PATH_UNLIKE_COMMENT), values); return response.getResponse(gson, NewsBlurResponse.class); } @@ -535,7 +538,7 @@ public class APIManager { values.put(APIConstants.PARAMETER_STORY_FEEDID, storyFeedId); values.put(APIConstants.PARAMETER_COMMENT_USERID, commentUserId); values.put(APIConstants.PARAMETER_REPLY_TEXT, reply); - APIResponse response = post(APIConstants.URL_REPLY_TO, values); + APIResponse response = post(buildUrl(APIConstants.PATH_REPLY_TO), values); return response.getResponse(gson, NewsBlurResponse.class); } @@ -545,14 +548,14 @@ public class APIManager { if (!TextUtils.isEmpty(folderName)) { values.put(APIConstants.PARAMETER_FOLDER, folderName); } - final APIResponse response = post(APIConstants.URL_ADD_FEED, values); + final APIResponse response = post(buildUrl(APIConstants.PATH_ADD_FEED), values); return (!response.isError()); } public FeedResult[] searchForFeed(String searchTerm) { ContentValues values = new ContentValues(); values.put(APIConstants.PARAMETER_FEED_SEARCH_TERM, searchTerm); - final APIResponse response = get(APIConstants.URL_FEED_AUTOCOMPLETE, values); + final APIResponse response = get(buildUrl(APIConstants.PATH_FEED_AUTOCOMPLETE), values); if (!response.isError()) { return gson.fromJson(response.getResponseBody(), FeedResult[].class); @@ -567,7 +570,7 @@ public class APIManager { if ((!TextUtils.isEmpty(folderName)) && (!folderName.equals(AppConstants.ROOT_FOLDER))) { values.put(APIConstants.PARAMETER_IN_FOLDER, folderName); } - APIResponse response = post(APIConstants.URL_DELETE_FEED, values); + APIResponse response = post(buildUrl(APIConstants.PATH_DELETE_FEED), values); return response.getResponse(gson, NewsBlurResponse.class); } @@ -576,7 +579,7 @@ public class APIManager { for (String feed : feeds) { values.put(APIConstants.PARAMETER_APPROVED_FEEDS, feed); } - APIResponse response = post(APIConstants.URL_SAVE_FEED_CHOOSER, values); + APIResponse response = post(buildUrl(APIConstants.PATH_SAVE_FEED_CHOOSER), values); return response.getResponse(gson, NewsBlurResponse.class); } diff --git a/clients/android/NewsBlur/src/com/newsblur/util/PrefConstants.java b/clients/android/NewsBlur/src/com/newsblur/util/PrefConstants.java index 751c61be1..3a2916b14 100644 --- a/clients/android/NewsBlur/src/com/newsblur/util/PrefConstants.java +++ b/clients/android/NewsBlur/src/com/newsblur/util/PrefConstants.java @@ -7,6 +7,7 @@ public class PrefConstants { public static final String PREFERENCES = "preferences"; public static final String PREF_COOKIE = "login_cookie"; public static final String PREF_UNIQUE_LOGIN = "login_unique"; + public static final String PREF_CUSTOM_SERVER = "custom_server"; public final static String USER_USERNAME = "username"; public final static String USER_WEBSITE = "website"; diff --git a/clients/android/NewsBlur/src/com/newsblur/util/PrefsUtils.java b/clients/android/NewsBlur/src/com/newsblur/util/PrefsUtils.java index ea9c28dc5..fd5a129e5 100644 --- a/clients/android/NewsBlur/src/com/newsblur/util/PrefsUtils.java +++ b/clients/android/NewsBlur/src/com/newsblur/util/PrefsUtils.java @@ -26,19 +26,29 @@ import android.util.Log; import com.newsblur.R; import com.newsblur.activity.Login; import com.newsblur.domain.UserDetails; +import com.newsblur.network.APIConstants; import com.newsblur.service.NBSyncService; public class PrefsUtils { private PrefsUtils() {} // util class - no instances - public static void saveLogin(final Context context, final String userName, final String cookie) { - NBSyncService.resumeFromInterrupt(); - final SharedPreferences preferences = context.getSharedPreferences(PrefConstants.PREFERENCES, 0); - final Editor edit = preferences.edit(); + public static void saveCustomServer(Context context, String customServer) { + if (customServer == null) return; + if (customServer.length() <= 0) return; + SharedPreferences preferences = context.getSharedPreferences(PrefConstants.PREFERENCES, 0); + Editor edit = preferences.edit(); + edit.putString(PrefConstants.PREF_CUSTOM_SERVER, customServer); + edit.commit(); + } + + public static void saveLogin(Context context, String userName, String cookie) { + SharedPreferences preferences = context.getSharedPreferences(PrefConstants.PREFERENCES, 0); + Editor edit = preferences.edit(); edit.putString(PrefConstants.PREF_COOKIE, cookie); edit.putString(PrefConstants.PREF_UNIQUE_LOGIN, userName + "_" + System.currentTimeMillis()); edit.commit(); + NBSyncService.resumeFromInterrupt(); } public static boolean checkForUpgrade(Context context) { @@ -84,6 +94,7 @@ public class PrefsUtils { s.append("%0Adevice: ").append(Build.MANUFACTURER + "+" + Build.MODEL + "+(" + Build.BOARD + ")"); s.append("%0Asqlite version: ").append(FeedUtils.dbHelper.getEngineVersion()); s.append("%0Ausername: ").append(getUserDetails(context).username); + s.append("%0Aserver: ").append(APIConstants.isCustomServer() ? "default" : "custom"); s.append("%0Amemory: ").append(NBSyncService.isMemoryLow() ? "low" : "normal"); s.append("%0Aspeed: ").append(NBSyncService.getSpeedInfo()); s.append("%0Apending actions: ").append(NBSyncService.getPendingInfo()); @@ -110,6 +121,9 @@ public class PrefsUtils { // wipe the local DB FeedUtils.dropAndRecreateTables(); + + // reset custom server + APIConstants.unsetCustomServer(); // prompt for a new login Intent i = new Intent(context, Login.class); @@ -127,6 +141,7 @@ public class PrefsUtils { Set keys = new HashSet(prefs.getAll().keySet()); keys.remove(PrefConstants.PREF_COOKIE); keys.remove(PrefConstants.PREF_UNIQUE_LOGIN); + keys.remove(PrefConstants.PREF_CUSTOM_SERVER); SharedPreferences.Editor editor = prefs.edit(); for (String key : keys) { editor.remove(key); @@ -147,6 +162,11 @@ public class PrefsUtils { return preferences.getString(PrefConstants.PREF_UNIQUE_LOGIN, null); } + public static String getCustomServer(Context context) { + SharedPreferences preferences = context.getSharedPreferences(PrefConstants.PREFERENCES, 0); + return preferences.getString(PrefConstants.PREF_CUSTOM_SERVER, null); + } + public static void saveUserDetails(final Context context, final UserDetails profile) { final SharedPreferences preferences = context.getSharedPreferences(PrefConstants.PREFERENCES, 0); final Editor edit = preferences.edit();