Android Open Source - Java-Yandex.Money-API-SDK Auth Activity






From Project

Back to project page Java-Yandex.Money-API-SDK.

License

The source code is released under:

MIT License

If you think the Android project Java-Yandex.Money-API-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

package ru.yandex.money.droid;
/*from   ww  w. j  a v  a 2 s  .  co  m*/
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import ru.yandex.money.api.YandexMoney;
import ru.yandex.money.api.response.ReceiveOAuthTokenResponse;

import java.io.IOException;

/**
 * @author dvmelnikov
 */

public class AuthActivity extends Activity {

    private ProgressDialog pd;

    private String clientId;
    private String redirectUri;
    private String authUri;
    private String clientSecret;

    private WebView authView;

    public static final String AUTH_IN_SHOW_RES_DLG = "show_result_dialog";
    public static final String AUTH_IN_REDIRECT_URI = "redirect_uri";
    public static final String AUTH_IN_CLIENT_ID = "client_id";
    public static final String AUTH_IN_AUTH_URI = "authorize_uri";
    public static final String AUTH_IN_SECRET = "client_secret";

    public static String AUTH_CODE = "code";
    public static final String AUTH = "?????????????";    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        pd = Utils.makeProgressDialog(this, AUTH, Consts.WAIT);
        pd.setOnCancelListener(new DialogInterface.OnCancelListener() {
            public void onCancel(DialogInterface dialog) {
                dialog.dismiss();
                Intent result = new Intent();
                result.putExtra(ActivityParams.AUTH_OUT_IS_SUCCESS, false);
                setResult(Activity.RESULT_CANCELED, result);
                finish();
            }
        });
        if (!isFinishing())
            pd.show();

        setContentView(R.layout.ymd_auth);
        setupIntentParams();
        setupAuthView();

        authView.loadUrl(authUri);
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra(ActivityParams.PAYMENT_OUT_IS_SUCCESS,
                false);        
        this.setResult(Activity.RESULT_CANCELED, intent);
        finish();
    }

    private void setupAuthView() {
        authView = (WebView) findViewById(R.id.wv_auth);
        authView.setWebViewClient(new AuthWebViewClient());
        authView.setWebChromeClient(new AuthWebChromeClient());
        authView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        authView.getSettings()
                .setRenderPriority(WebSettings.RenderPriority.HIGH);
        authView.getSettings().setJavaScriptEnabled(false);
        authView.getSettings().setBuiltInZoomControls(true);
    }

    private void setupIntentParams() {
        clientId = getIntent().getStringExtra(AuthActivity.AUTH_IN_CLIENT_ID);
        redirectUri = getIntent().getStringExtra(AuthActivity.AUTH_IN_REDIRECT_URI);
        clientSecret = getIntent().getStringExtra(AuthActivity.AUTH_IN_SECRET);        
        authUri = getIntent().getStringExtra(AuthActivity.AUTH_IN_AUTH_URI);
    }

    private String extractCode(String urlWithCode) {
        Uri uri = Uri.parse(urlWithCode);
        return uri.getQueryParameter(AUTH_CODE);
    }

    private ReceiveTokenResp receiveToken(String code) {
        AndroidHttpClient client = Utils.httpClient();
        YandexMoney ym = Utils.getYandexMoney(clientId, client);
        try {
            ReceiveOAuthTokenResponse resp;
            if (clientSecret == null)
                resp = ym.receiveOAuthToken(code, redirectUri);
            else
                resp = ym.receiveOAuthToken(code, redirectUri, clientSecret);
            return new ReceiveTokenResp(resp, null);            
        } catch (IOException e) {
            return new ReceiveTokenResp(null, e);
        } finally {
            client.close();
        }
    }

    private AlertDialog makeAlertDialog(final ReceiveTokenResp resp) {
        AlertDialog.Builder builder;
        builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.ic_wallet);
        builder.setTitle("?????????????");
        if (resp.getResponse().isSuccess())
            builder.setMessage("???????? ?????????");
        else
            builder.setMessage("??????: " + resp.getResponse().getError());

        builder.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Intent authResult = new Intent();
                if (resp.getResponse().getAccessToken() != null)
                    authResult.putExtra(ActivityParams.AUTH_OUT_ACCESS_TOKEN, resp.getResponse().getAccessToken());
                if (resp.getResponse().getError() != null)
                    authResult.putExtra(ActivityParams.AUTH_OUT_ERROR, resp.getResponse().getError());                
                authResult.putExtra(ActivityParams.AUTH_OUT_IS_SUCCESS, resp.getResponse().isSuccess());
                if (resp.getException() != null)
                    authResult.putExtra(ActivityParams.AUTH_OUT_EXCEPTION, resp.getException());
                
                if (resp.getResponse().isSuccess())
                    AuthActivity.this.setResult(Activity.RESULT_OK, authResult);
                else
                    AuthActivity.this.setResult(Activity.RESULT_CANCELED, authResult);
                finish();
            }
        });
        
        AlertDialog dialog = builder.create();
        dialog.setCanceledOnTouchOutside(false);
        return dialog;
    }

    private class AuthWebViewClient extends WebViewClient {

        private AuthWebViewClient() {
            super();
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains(redirectUri)) {
                view.goBack();
                new StartAuthResultActivity().execute(extractCode(url));
                return false;
            }

            authView.loadUrl(url);
            return true;
        }
    }

    private class AuthWebChromeClient extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100)
                pd.dismiss();
        }
    }

    private class StartAuthResultActivity extends
            AsyncTask<String, Void, ReceiveTokenResp> {

        @Override
        protected void onPreExecute() {
            if (!isFinishing())
                pd.show();
        }

        @Override
        protected void onPostExecute(ReceiveTokenResp resp) {
            pd.dismiss();
            if (resp.getException() == null) {
                if (resp.getResponse().isSuccess()) {
                    boolean showResultDialog = getIntent().getBooleanExtra(AuthActivity.AUTH_IN_SHOW_RES_DLG, false);
                    if (showResultDialog) {
                        if (!isFinishing())
                            makeAlertDialog(resp).show();    
                    } else {
                        Intent result = new Intent();
                        result.putExtra(ActivityParams.AUTH_OUT_IS_SUCCESS, resp.getResponse().isSuccess());
                        result.putExtra(ActivityParams.AUTH_OUT_ACCESS_TOKEN, resp.getResponse().getAccessToken());
                        setResult(Activity.RESULT_OK, result);
                        finish();
                    }                    
                } else {
                    Intent result = new Intent();
                    result.putExtra(ActivityParams.AUTH_OUT_IS_SUCCESS, false);
                    result.putExtra(ActivityParams.AUTH_OUT_ERROR, resp.getResponse().getError());
                    setResult(Activity.RESULT_CANCELED, result);
                    finish();
                }
            } else {
                Intent result = new Intent();
                result.putExtra(ActivityParams.AUTH_OUT_IS_SUCCESS, false);                
                result.putExtra(ActivityParams.AUTH_OUT_EXCEPTION, resp.getException());
                setResult(Activity.RESULT_CANCELED, result);
                finish();
            }                                    
        }

        @Override
        protected ReceiveTokenResp doInBackground(String... params) {
            return receiveToken(params[0]);
        }
    }

    private class ReceiveTokenResp {
        ReceiveOAuthTokenResponse response;
        private final Exception exception;

        public ReceiveTokenResp(ReceiveOAuthTokenResponse response, Exception exception) {
            this.response = response;
            this.exception = exception; 
        }

        public ReceiveOAuthTokenResponse getResponse() {
            return response;
        }

        public Exception getException() {
            return exception;
        }
    }
}




Java Source Code List

com.samples.client.Settings.java
com.samples.server.SampleIncomingTransferListener.java
com.samples.server.ServletListener.java
com.samples.yamodroid.Consts.java
com.samples.yamodroid.GreatAppActivity.java
ru.yandex.money.api.ApiCommandsFacadeImpl.java
ru.yandex.money.api.ApiCommandsFacade.java
ru.yandex.money.api.CommandUrlHolder.java
ru.yandex.money.api.InsufficientScopeException.java
ru.yandex.money.api.InternalServerErrorException.java
ru.yandex.money.api.InvalidTokenException.java
ru.yandex.money.api.ProtocolRequestException.java
ru.yandex.money.api.TestUrlHolder.java
ru.yandex.money.api.TokenRequesterImpl.java
ru.yandex.money.api.TokenRequester.java
ru.yandex.money.api.YamoneyAccount.java
ru.yandex.money.api.YamoneyApiClient.java
ru.yandex.money.api.YandexMoneyImpl.java
ru.yandex.money.api.YandexMoney.java
ru.yandex.money.api.enums.Destination.java
ru.yandex.money.api.enums.MoneyDirection.java
ru.yandex.money.api.enums.MoneySource.java
ru.yandex.money.api.enums.OperationHistoryType.java
ru.yandex.money.api.enums.Status.java
ru.yandex.money.api.notifications.IncomingTransferListener.java
ru.yandex.money.api.notifications.IncomingTransfer.java
ru.yandex.money.api.notifications.NotificationUtils.java
ru.yandex.money.api.notifications.NotificationsServlet.java
ru.yandex.money.api.response.AccountInfoResponse.java
ru.yandex.money.api.response.FundraisingStatsResponse.java
ru.yandex.money.api.response.OperationDetailResponse.java
ru.yandex.money.api.response.OperationHistoryResponse.java
ru.yandex.money.api.response.ProcessPaymentResponse.java
ru.yandex.money.api.response.ReceiveOAuthTokenResponse.java
ru.yandex.money.api.response.RequestPaymentResponse.java
ru.yandex.money.api.response.util.OperationHistoryError.java
ru.yandex.money.api.response.util.Operation.java
ru.yandex.money.api.response.util.PaymentErrorCode.java
ru.yandex.money.api.response.util.ProcessPaymentError.java
ru.yandex.money.api.response.util.RequestPaymentError.java
ru.yandex.money.api.response.util.money.PaymentMethods.java
ru.yandex.money.api.rights.AbstractLimitedPermission.java
ru.yandex.money.api.rights.AbstractPermission.java
ru.yandex.money.api.rights.AccountInfo.java
ru.yandex.money.api.rights.IdentifierType.java
ru.yandex.money.api.rights.MoneySource.java
ru.yandex.money.api.rights.OperationDetails.java
ru.yandex.money.api.rights.OperationHistory.java
ru.yandex.money.api.rights.PaymentP2P.java
ru.yandex.money.api.rights.PaymentShop.java
ru.yandex.money.api.rights.Payment.java
ru.yandex.money.api.rights.Permission.java
ru.yandex.money.droid.ActivityParams.java
ru.yandex.money.droid.AuthActivity.java
ru.yandex.money.droid.Consts.java
ru.yandex.money.droid.DetailHistoryActivity.java
ru.yandex.money.droid.HistoryActivity.java
ru.yandex.money.droid.HistoryAdapter.java
ru.yandex.money.droid.IntentCreator.java
ru.yandex.money.droid.LoadHistoryTask.java
ru.yandex.money.droid.PaymentActivity.java
ru.yandex.money.droid.PaymentConfirmActivity.java
ru.yandex.money.droid.PaymentShopParcelable.java
ru.yandex.money.droid.ProcessPaymentTask.java
ru.yandex.money.droid.Utils.java
ru.yandex.money.droid.YandexMoneyDroid.java