Android Open Source - vk-android-sdk V K Open Auth Activity






From Project

Back to project page vk-android-sdk.

License

The source code is released under:

MIT License

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

//
//  Copyright (c) 2014 VK.com
//// w ww  .  j av a 2 s .c  om
//  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 com.vk.sdk;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import java.util.Locale;

/**
 * Activity for request OAuth authorization in case of missing VK app.
 */
public class VKOpenAuthActivity extends Activity {
    public static final String VK_EXTRA_CLIENT_ID = "client_id";
    public static final String VK_EXTRA_SCOPE = "scope";
    public static final String VK_EXTRA_API_VERSION = "version";
    public static final String VK_EXTRA_REVOKE = "revoke";

    public static final String VK_RESULT_INTENT_NAME = "com.vk.auth-token";
    public static final String VK_EXTRA_TOKEN_DATA = "extra-token-data";
  public static final String VK_EXTRA_VALIDATION_URL = "extra-validation-url";
    public static final String VK_EXTRA_VALIDATION_REQUEST = "extra-validation-reques";

    private static final String REDIRECT_URL = "https://oauth.vk.com/blank.html";
    private static final String ERROR = "error";

    protected WebView mWebView;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new VKOpenAuthView(this));

        hideActionBar();
        findViewById(android.R.id.content).setBackgroundColor(Color.rgb(240, 242, 245));
        loadPage();
    }

    private void loadPage() {
        try {
          String urlToLoad;
          urlToLoad = getIntent().getStringExtra(VK_EXTRA_VALIDATION_URL);
          if (urlToLoad == null)
          {
              int appId = getIntent().getIntExtra(VK_EXTRA_CLIENT_ID, 0);
              String scope = getIntent().getStringExtra(VK_EXTRA_SCOPE),
                      apiV = getIntent().getStringExtra(VK_EXTRA_API_VERSION);
              boolean revoke = getIntent().getBooleanExtra(VK_EXTRA_REVOKE, false);
              urlToLoad = String.format(Locale.US,
                      "https://oauth.vk.com/authorize?client_id=%s" +
                              "&scope=%s" +
                              "&redirect_uri=%s" +
                              "&display=mobile" +
                              "&v=%s" +
                              "&response_type=token&revoke=%d",
                      appId, scope, REDIRECT_URL, apiV, revoke ? 1 : 0);
          }
            mWebView = (WebView) findViewById(android.R.id.copyUrl);
            mWebView.setWebViewClient(new OAuthWebViewClient());
            WebSettings webSettings = mWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            mWebView.loadUrl(urlToLoad);
            mWebView.setVisibility(View.INVISIBLE);

        } catch (Exception e) {
            setResult(RESULT_CANCELED);
            finish();
        }
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void hideActionBar() {
        try {
            if (Build.VERSION.SDK_INT >= 11 && getActionBar() != null) {
                getActionBar().hide();
            }
        } catch (Exception ignored) {
        }
    }

    private class OAuthWebViewClient extends WebViewClient {
        public boolean canShowPage = true;
        private boolean processUrl(String url) {
            if (url.startsWith(REDIRECT_URL)) {
                Intent data = new Intent(VK_RESULT_INTENT_NAME);
                String extraData = url.substring(url.indexOf('#') + 1);
                data.putExtra(VK_EXTRA_TOKEN_DATA, extraData);
                if (getIntent().hasExtra(VK_EXTRA_VALIDATION_URL))
                    data.putExtra(VK_EXTRA_VALIDATION_URL, true);
                if (getIntent().hasExtra(VK_EXTRA_VALIDATION_REQUEST))
                    data.putExtra(VK_EXTRA_VALIDATION_REQUEST, getIntent().getLongExtra(VK_EXTRA_VALIDATION_REQUEST,0));
                if (extraData.contains(ERROR)) {
                    setResult(RESULT_CANCELED, data);
                } else {
                    setResult(RESULT_OK, data);
                }
                finish();
                return true;
            }
            return false;
        }
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (processUrl(url))
                return true;
            canShowPage = true;
            return false;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            processUrl(url);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (canShowPage)
                view.setVisibility(View.VISIBLE);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            canShowPage = false;
            AlertDialog.Builder builder = new AlertDialog.Builder(VKOpenAuthActivity.this)
                    .setMessage(description)
                    .setPositiveButton(R.string.vk_retry, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            loadPage();
                        }
                    })
                    .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            finish();
                        }
                    });
            try {
                builder.show();
            } catch (Exception e) {
                if (VKSdk.DEBUG)
                  e.printStackTrace();
            }
        }
    }

    @Override
    public void onBackPressed() {
        Intent data = new Intent(VK_RESULT_INTENT_NAME);
        setResult(RESULT_CANCELED, data);
        super.onBackPressed();
    }

    private static class VKOpenAuthView extends RelativeLayout {
        public VKOpenAuthView(Context context) {
            super(context);
            ProgressBar progress = new ProgressBar(context);
            LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            lp.addRule(RelativeLayout.CENTER_IN_PARENT, 1);
            progress.setLayoutParams(lp);
            addView(progress);

            WebView webView = new WebView(context);
            lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            webView.setLayoutParams(lp);
            addView(webView);
            webView.setId(android.R.id.copyUrl);
            webView.setVisibility(View.INVISIBLE);
        }
    }
}




Java Source Code List

com.vk.sdk.VKAccessToken.java
com.vk.sdk.VKObject.java
com.vk.sdk.VKOpenAuthActivity.java
com.vk.sdk.VKScope.java
com.vk.sdk.VKSdkListener.java
com.vk.sdk.VKSdkVersion.java
com.vk.sdk.VKSdk.java
com.vk.sdk.VKUIHelper.java
com.vk.sdk.api.VKApiConst.java
com.vk.sdk.api.VKApi.java
com.vk.sdk.api.VKBatchRequest.java
com.vk.sdk.api.VKDefaultParser.java
com.vk.sdk.api.VKError.java
com.vk.sdk.api.VKParameters.java
com.vk.sdk.api.VKParser.java
com.vk.sdk.api.VKRequest.java
com.vk.sdk.api.VKResponse.java
com.vk.sdk.api.httpClient.VKAbstractOperation.java
com.vk.sdk.api.httpClient.VKHttpClient.java
com.vk.sdk.api.httpClient.VKHttpOperation.java
com.vk.sdk.api.httpClient.VKImageOperation.java
com.vk.sdk.api.httpClient.VKJsonOperation.java
com.vk.sdk.api.httpClient.VKModelOperation.java
com.vk.sdk.api.httpClient.VKMultipartEntity.java
com.vk.sdk.api.methods.VKApiBase.java
com.vk.sdk.api.methods.VKApiCaptcha.java
com.vk.sdk.api.methods.VKApiFriends.java
com.vk.sdk.api.methods.VKApiGroups.java
com.vk.sdk.api.methods.VKApiPhotos.java
com.vk.sdk.api.methods.VKApiUsers.java
com.vk.sdk.api.methods.VKApiWall.java
com.vk.sdk.api.model.Identifiable.java
com.vk.sdk.api.model.ParseUtils.java
com.vk.sdk.api.model.VKApiApplicationContent.java
com.vk.sdk.api.model.VKApiAudio.java
com.vk.sdk.api.model.VKApiChat.java
com.vk.sdk.api.model.VKApiCity.java
com.vk.sdk.api.model.VKApiComment.java
com.vk.sdk.api.model.VKApiCommunityArray.java
com.vk.sdk.api.model.VKApiCommunityFull.java
com.vk.sdk.api.model.VKApiCommunity.java
com.vk.sdk.api.model.VKApiCountry.java
com.vk.sdk.api.model.VKApiDocument.java
com.vk.sdk.api.model.VKApiLink.java
com.vk.sdk.api.model.VKApiMessage.java
com.vk.sdk.api.model.VKApiModel.java
com.vk.sdk.api.model.VKApiNote.java
com.vk.sdk.api.model.VKApiOwner.java
com.vk.sdk.api.model.VKApiPhotoAlbum.java
com.vk.sdk.api.model.VKApiPhotoSize.java
com.vk.sdk.api.model.VKApiPhoto.java
com.vk.sdk.api.model.VKApiPlace.java
com.vk.sdk.api.model.VKApiPoll.java
com.vk.sdk.api.model.VKApiPost.java
com.vk.sdk.api.model.VKApiPostedPhoto.java
com.vk.sdk.api.model.VKApiSchool.java
com.vk.sdk.api.model.VKApiUniversity.java
com.vk.sdk.api.model.VKApiUserFull.java
com.vk.sdk.api.model.VKApiUser.java
com.vk.sdk.api.model.VKApiVideo.java
com.vk.sdk.api.model.VKApiWikiPage.java
com.vk.sdk.api.model.VKAttachments.java
com.vk.sdk.api.model.VKCommentArray.java
com.vk.sdk.api.model.VKList.java
com.vk.sdk.api.model.VKPhotoArray.java
com.vk.sdk.api.model.VKPhotoSizes.java
com.vk.sdk.api.model.VKPostArray.java
com.vk.sdk.api.model.VKPrivacy.java
com.vk.sdk.api.model.VKScopes.java
com.vk.sdk.api.model.VKUsersArray.java
com.vk.sdk.api.model.VKWallPostResult.java
com.vk.sdk.api.model.package-info.java
com.vk.sdk.api.photo.VKImageParameters.java
com.vk.sdk.api.photo.VKUploadAlbumPhotoRequest.java
com.vk.sdk.api.photo.VKUploadImage.java
com.vk.sdk.api.photo.VKUploadPhotoBase.java
com.vk.sdk.api.photo.VKUploadWallPhotoRequest.java
com.vk.sdk.dialogs.VKCaptchaDialog.java
com.vk.sdk.dialogs.VKShareDialog.java
com.vk.sdk.util.VKJsonHelper.java
com.vk.sdk.util.VKStringJoiner.java
com.vk.sdk.util.VKUtil.java
com.vk.vktestapp.ApiCallActivity.java
com.vk.vktestapp.LoginActivity.java
com.vk.vktestapp.TestActivity.java