Android Open Source - UPPlatform_Android_SDK Oauth Web View Activity






From Project

Back to project page UPPlatform_Android_SDK.

License

The source code is released under:

Apache License

If you think the Android project UPPlatform_Android_SDK 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

/**
 * @author Omer Muhammed//from  www .j  a  v a  2s  . co m
 * Copyright 2014 (c) Jawbone. All rights reserved.
 *
 */
package com.jawbone.upplatformsdk.oauth;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.jawbone.upplatformsdk.R;
import com.jawbone.upplatformsdk.utils.UpPlatformSdkConstants;

/**
 * Simple Web View for Oauth authorization, we display the web page so that
 * user can agree to, or cancel the permissions requested
 */
public class OauthWebViewActivity extends Activity {

    private static final String TAG = OauthWebViewActivity.class.getSimpleName();

    // AccessCode returned from server.
    private String accessCode;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.oauth_webview);

        Intent intent = this.getIntent();
        Uri uri = intent.getParcelableExtra(UpPlatformSdkConstants.AUTH_URI);

        WebView webview = (WebView) findViewById(R.id.webview);
        webview.getSettings().setJavaScriptEnabled(true);

        webview.setWebViewClient(new WebViewClient() {
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                String accessCodeFragment = "&code=";
                Log.e(TAG, "oauth response from server: " + url);

                int start = url.indexOf(accessCodeFragment);

                // We hijack the GET request to extract the OAuth parameters
                if(start > -1) {
                    // the GET request contains an authorization code
                    Log.d(TAG, "user accepted, url is :" + url);
                    accessCode = url.substring(start + accessCodeFragment.length(), url.length());
                    Log.d(TAG, "user accepted, code is :" + accessCode);

                    view.clearCache(true);
                    Intent i = getIntent();
                    i.putExtra(UpPlatformSdkConstants.ACCESS_CODE, accessCode);
                    setResult(RESULT_OK, i);
                    finish();
                }
            }
        });
        webview.loadUrl(uri.toString());
    }
}




Java Source Code List

com.jawbone.helloup.HelloUpActivity.java
com.jawbone.helloup.UpApiListActivity.java
com.jawbone.upplatformsdk.api.ApiHeaders.java
com.jawbone.upplatformsdk.api.ApiManager.java
com.jawbone.upplatformsdk.api.RestApiInterface.java
com.jawbone.upplatformsdk.api.response.OauthAccessTokenResponse.java
com.jawbone.upplatformsdk.datamodel.DataCollection.java
com.jawbone.upplatformsdk.datamodel.Data.java
com.jawbone.upplatformsdk.datamodel.Links.java
com.jawbone.upplatformsdk.datamodel.Meta.java
com.jawbone.upplatformsdk.oauth.OauthUtils.java
com.jawbone.upplatformsdk.oauth.OauthWebViewActivity.java
com.jawbone.upplatformsdk.utils.UpPlatformSdkConstants.java