Android Open Source - openaccessbutton A P I






From Project

Back to project page openaccessbutton.

License

The source code is released under:

MIT License

If you think the Android project openaccessbutton listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.openaccessbutton.openaccessbutton.api;
//from www.j a v  a  2s. com
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

import com.goebl.david.Webb;
import com.goebl.david.WebbException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.openaccessbutton.openaccessbutton.R;
import org.openaccessbutton.openaccessbutton.map.Item;

/**
 * Created by rickards on 10/14/14.
 */
public class API {
    public final static String API_URL = "https://openaccessbutton.org/api";

    public interface SignupCallback {
        void onComplete(String username, String apikey);
        void onError(String message);
    }

    public interface OAuthSignupCallback {
        void onComplete(String username, String apikey);
    }

    public interface SigninCallback {
        void onComplete(String username, String apikey);
        void onError(String message);
    }

    public interface StoryListCallback {
        void onComplete(Item[] stories);
        void onError(String message);
    }

    public interface Callback {
        void onComplete();
    }

    public static void oauthSignupRequest(OAuthSignupCallback callback) {
        Log.w("oab", "oauth not implemented");
        callback.onComplete("", "");
    }

    public static void signupRequest(final SignupCallback callback, final Context context, final String email, final String profession, final String username, final String password) {
        final ProgressDialog dialog = createProgressDialog(context);
        Runnable r = new Runnable() {
            public void run() {
                try {
                    Webb webb = Webb.create();
                    JSONObject result = webb
                            .post(API_URL + "/register")
                            .param("email", email)
                            .param("profession", profession)
                            .param("username", username)
                            .param("password", password)
                            .asJsonObject()
                            .getBody();

                    if (result == null) {
                        throw new Error("Email address already registered");
                    }

                    String apiKey = result.getString("api_key");
                    String username = result.getString("username");

                    // Store API key so we know we're authenticated (and skip intro pages)
                    // and also for making API requests
                    SharedPreferences prefs = context.getSharedPreferences("org.openaccessbutton.openaccessbutton", Context.MODE_PRIVATE);
                    SharedPreferences.Editor edit = prefs.edit();
                    edit.clear();
                    edit.putString("api_key", apiKey);
                    edit.putString("username", username);
                    edit.apply();

                    closeProgressDialog(dialog, context);
                    callback.onComplete(username, apiKey);
                } catch (JSONException e) {
                    e.printStackTrace();
                    closeProgressDialog(dialog, context);
                    callback.onError(context.getResources().getString(R.string.username_already_taken));
                } catch (Error e) {
                    e.printStackTrace();
                    closeProgressDialog(dialog, context);
                    callback.onError(context.getResources().getString(R.string.username_already_taken));
                }
            }
        };
        (new Thread(r)).start();
    }

    public static void signinRequest(final SigninCallback callback, final Context context, final String username, final String password) {
        final ProgressDialog dialog = createProgressDialog(context);
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    Webb webb = Webb.create();
                    JSONObject result = webb
                            .post(API_URL + "/retrieve")
                            .param("username", username)
                            .param("password", password)
                            .ensureSuccess()
                            .asJsonObject()
                            .getBody();
                    String apiKey = result.getString("api_key");
                    if ((apiKey == null) || (apiKey.equals(""))) {
                        Log.w("result", result.toString());
                        throw new Error("Invalid username or password");
                    }

                    // Store API key so we know we're authenticated (and skip intro pages)
                    // and also for making API requests
                    SharedPreferences prefs = context.getSharedPreferences("org.openaccessbutton.openaccessbutton", Context.MODE_PRIVATE);
                    SharedPreferences.Editor edit = prefs.edit();
                    edit.clear();
                    edit.putString("api_key", apiKey);
                    edit.putString("username", username);
                    edit.apply();

                    closeProgressDialog(dialog, context);
                    callback.onComplete(username, apiKey);
                } catch (JSONException e) {
                    e.printStackTrace();
                    closeProgressDialog(dialog, context);
                    callback.onError(context.getResources().getString(R.string.invalid_username_password));
                } catch (Error e) {
                    e.printStackTrace();
                    closeProgressDialog(dialog, context);
                    callback.onError(context.getResources().getString(R.string.invalid_username_password));
                } catch (WebbException e) {
                    e.printStackTrace();
                    closeProgressDialog(dialog, context);
                    callback.onError(context.getResources().getString(R.string.invalid_username_password));}
                }
        };
        (new Thread(r)).start();

    }

    public static void blockedRequest(final Callback callback, final Context context, final String url, final String location, final String story, final boolean wishlist) {
        SharedPreferences prefs = context.getSharedPreferences("org.openaccessbutton.openaccessbutton", Context.MODE_PRIVATE);
        final String apiKey = prefs.getString("api_key", "");

        final ProgressDialog dialog = createProgressDialog(context);
        Runnable r = new Runnable() {
            @Override
            public void run() {
                Webb webb = Webb.create();
                JSONObject result = webb
                        .post(API_URL + "/blocked")
                        .param("api_key", apiKey) // We need to get this when the user signs up
                        .param("url", url)
                        .param("location", location) // Geocode this either here or in the API
                        .param("story", story)
                        .param("wishlist", wishlist)
                        .param("android", true) // Indicate we're sending a request from a mobile device
                        .ensureSuccess()
                        .asJsonObject()
                        .getBody();

                closeProgressDialog(dialog, context);
                callback.onComplete();
            }
        };
        (new Thread(r)).start();

    }

    public static void storyListRequest(final StoryListCallback callback, final Context context) {
        final ProgressDialog dialog = createProgressDialog(context);
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    // TODO are we getting all of them?
                    String query = "{\"size\":1000,\"query\":{\"match_all\":{}},\"fields\":[\"coords.lat\",\"coords.lng\",\"story\",\"doi\",\"user_profession\",\"url\",\"accessed\",\"user_name\",\"description\"]}";

                    Webb webb = Webb.create();
                    JSONObject results = webb
                        .get("https://openaccessbutton.org/query")
                        .param("source", query)
                        .ensureSuccess()
                        .asJsonObject()
                        .getBody();
                    closeProgressDialog(dialog, context);

                    JSONArray hits = results.getJSONObject("hits").getJSONArray("hits");
                    Item[] items = new Item[hits.length()];
                    for (int i=0; i<hits.length(); i++) {
                        JSONObject fields = hits.getJSONObject(i).getJSONObject("fields");
                        // These are to 1dp however we want to spread them out
                        // TODO Use some super-ultra-fast-but-still-provably-random-library rather than doing this
                        double lat = fields.getDouble("coords.lat") + 0.0005*(((i*i)%200)-100);
                        double lng = fields.getDouble("coords.lng") + 0.0005*(((i*i*i)%200)-100);
                        String story = fields.getString("story");
                        String doi = fields.getString("doi");
                        String userProfession = fields.getString("user_profession");
                        String url = fields.getString("url");
                        String accessed = fields.getString("accessed");
                        String userName = fields.getString("user_name");
                        String description = fields.getString("description");
                        items[i] = new Item(lat, lng, story, doi, userProfession, url, accessed, userName, description);
                    }

                    callback.onComplete(items);
                } catch (JSONException e) {
                    e.printStackTrace();
                    closeProgressDialog(dialog, context);
                    callback.onError(context.getResources().getString(R.string.map_error));
                } catch (Error e) {
                    e.printStackTrace();
                    closeProgressDialog(dialog, context);
                    callback.onError(context.getResources().getString(R.string.map_error));
                } catch (WebbException e) {
                    e.printStackTrace();
                    closeProgressDialog(dialog, context);
                    callback.onError(context.getResources().getString(R.string.map_error));}
            }
        };
        (new Thread(r)).start();
    }

    protected static ProgressDialog createProgressDialog(Context context) {
        ProgressDialog progress = new ProgressDialog(context);
        progress.setTitle("Loading");
        progress.setMessage("Please wait...");
        progress.show();

        return progress;
    }

    // Context must be an Activity
    protected static void closeProgressDialog(final ProgressDialog dialog, Context context) {
        ((Activity) context).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                dialog.dismiss();
            }
        });
    }
}




Java Source Code List

com.viewpagerindicator.CirclePageIndicator.java
com.viewpagerindicator.IconPageIndicator.java
com.viewpagerindicator.IconPagerAdapter.java
com.viewpagerindicator.IcsLinearLayout.java
com.viewpagerindicator.LinePageIndicator.java
com.viewpagerindicator.PageIndicator.java
com.viewpagerindicator.TabPageIndicator.java
com.viewpagerindicator.TitlePageIndicator.java
com.viewpagerindicator.UnderlinePageIndicator.java
org.openaccessbutton.openaccessbutton.ApplicationTest.java
org.openaccessbutton.openaccessbutton.MainActivity.java
org.openaccessbutton.openaccessbutton.NavigationItem.java
org.openaccessbutton.openaccessbutton.NavigationXmlParser.java
org.openaccessbutton.openaccessbutton.OnFragmentNeededListener.java
org.openaccessbutton.openaccessbutton.OnShareIntentInterface.java
org.openaccessbutton.openaccessbutton.about.AboutActivity.java
org.openaccessbutton.openaccessbutton.advocacy.AdvocacyFragment.java
org.openaccessbutton.openaccessbutton.advocacy.FaqView.java
org.openaccessbutton.openaccessbutton.advocacy.Question.java
org.openaccessbutton.openaccessbutton.advocacy.QuestionsActivity.java
org.openaccessbutton.openaccessbutton.advocacy.XmlParser.java
org.openaccessbutton.openaccessbutton.api.API.java
org.openaccessbutton.openaccessbutton.blog.BlogAdapter.java
org.openaccessbutton.openaccessbutton.blog.BlogDetailsFragment.java
org.openaccessbutton.openaccessbutton.blog.BlogFragment.java
org.openaccessbutton.openaccessbutton.blog.DownloadTask.java
org.openaccessbutton.openaccessbutton.blog.Post.java
org.openaccessbutton.openaccessbutton.blog.RssParser.java
org.openaccessbutton.openaccessbutton.browser.BrowserFragment.java
org.openaccessbutton.openaccessbutton.browser.ScrollingWebView.java
org.openaccessbutton.openaccessbutton.button.ButtonSubmitActivity.java
org.openaccessbutton.openaccessbutton.intro.ForgotPasswordActivity.java
org.openaccessbutton.openaccessbutton.intro.IntroActivity.java
org.openaccessbutton.openaccessbutton.intro.LaunchActivity.java
org.openaccessbutton.openaccessbutton.intro.ScreenSlidePageFragment.java
org.openaccessbutton.openaccessbutton.intro.SigninActivity.java
org.openaccessbutton.openaccessbutton.intro.SigninButtonClickListener.java
org.openaccessbutton.openaccessbutton.intro.SignupActivity.java
org.openaccessbutton.openaccessbutton.intro.SignupEmailButtonClickListener.java
org.openaccessbutton.openaccessbutton.map.Item.java
org.openaccessbutton.openaccessbutton.map.MapFragment.java
org.openaccessbutton.openaccessbutton.menu.MenuActivity.java
org.openaccessbutton.openaccessbutton.preferences.AppPreferencesActivity.java
org.openaccessbutton.openaccessbutton.push.PushReceiver.java
org.openaccessbutton.openaccessbutton.push.Push.java