Android Open Source - Java-Yandex.Money-API-SDK Great App 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 com.samples.yamodroid;
/*w ww . ja  v a 2  s.  c om*/
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import ru.yandex.money.api.ApiCommandsFacade;
import ru.yandex.money.api.InsufficientScopeException;
import ru.yandex.money.api.InvalidTokenException;
import ru.yandex.money.api.response.AccountInfoResponse;
import ru.yandex.money.droid.ActivityParams;
import ru.yandex.money.droid.Utils;
import ru.yandex.money.droid.YandexMoneyDroid;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;

public class GreatAppActivity extends Activity {

    private final int CODE_AUTH = 1710;
    private final int CODE_PAYMENT_P2P = 1720;
    private final int CODE_PAYMENT_SHOP = 1730;
    private static final int CODE_HISTORY = 1740;

    private static final String PREFERENCES = "yandex_money_preferences";
    private final String PREF_ACCESS_TOKEN = "access_token";

    private TextView tvAuthStatus;
    private TextView tvAccount;
    private TextView tvBalance;
    private TextView tvCurrency;
    private Button btnAuth;
    private LinearLayout llFuncs;

    private YandexMoneyDroid ymd = new YandexMoneyDroid(Consts.CLIENT_ID);
    private MyDialogListener dialogListener = new MyDialogListener();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        llFuncs = (LinearLayout) findViewById(R.id.ll_funcs);

        btnAuth = (Button) findViewById(R.id.btnAuth);
        btnAuth.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {                
                if (!isAuthorized(false)) {
                    ymd.authorize(GreatAppActivity.this, CODE_AUTH, Consts.REDIRECT_URI, Consts.getPermissions(), true,
                            dialogListener);
                } else {
                    storeToken("");
                }
                refresh();
            }
        });                

        Button btnHistory = (Button) findViewById(R.id.btnHistory);
        btnHistory.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (!isAuthorized(true))
                    return;
                ymd.showHistory(GreatAppActivity.this, CODE_HISTORY, restoreToken(), dialogListener);
            }
        });

        Button btnPaymentP2P = (Button) findViewById(R.id.btnP2p);
        btnPaymentP2P.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                if (!isAuthorized(true))
                    return;

                ymd.showPaymentP2P(GreatAppActivity.this, CODE_PAYMENT_P2P, restoreToken(),
                        "410011161616877", BigDecimal.valueOf(0.02), "comment for p2p",
                        "message for p2p", true, dialogListener);
            }
        });

        Button btnPaymentShop = (Button) findViewById(R.id.btnShop);
        btnPaymentShop.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                if (!isAuthorized(true))
                    return;

                HashMap<String, String> params = new HashMap<String, String>();
                params.put("PROPERTY1", "921");
                params.put("PROPERTY2", "3020052");
                params.put("sum", "1.00");

                ymd.showPaymentShop(GreatAppActivity.this, CODE_PAYMENT_SHOP, restoreToken(), BigDecimal.valueOf(1.00),
                        "337", params, true, dialogListener);
            }
        });

        tvAccount = (TextView) findViewById(R.id.tvAccount);
        tvBalance = (TextView) findViewById(R.id.tvBalance);
        tvCurrency = (TextView) findViewById(R.id.tvCurrency);

        tvAuthStatus = (TextView) findViewById(R.id.tvAuthStatus);
        renewAuthCaptions();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,
            Intent data) {
        ymd.callbackOnResult(requestCode, resultCode, data);
    }

    @Override
    protected void onResume() {
        super.onResume();
        refresh();
    }

    private void refresh() {
        visibleFunctions();
        renewAuthCaptions();
        renewAccountInfo();
    }

    private void visibleFunctions() {
        if (isAuthorized(false))
            llFuncs.setVisibility(View.VISIBLE);
        else
            llFuncs.setVisibility(View.GONE);
    }

    private void renewAccountInfo() {
        new LoadAccountInfoTask().execute();
    }

    private void renewAuthCaptions() {
        if (isAuthorized(false)) {
            tvAuthStatus.setText("???????: ???????????");
            btnAuth.setText("?????");
        } else {
            tvAuthStatus.setText("???????: ?? ???????????");
            btnAuth.setText("?????????????????");
        }
    }   

    private void storeToken(String token) {
        SharedPreferences sp = getSharedPreferences(PREFERENCES, MODE_PRIVATE);
        sp.edit().putString(PREF_ACCESS_TOKEN, token).commit();
    }

    private String restoreToken() {
        SharedPreferences sp = getSharedPreferences(PREFERENCES, MODE_PRIVATE);
        return sp.getString(PREF_ACCESS_TOKEN, "");
    }

    private boolean isAuthorized(boolean showMessage) {
        if (restoreToken().equals("")) {
            if (showMessage)
                Toast.makeText(GreatAppActivity.this, "??? ???????????!",
                        Toast.LENGTH_LONG).show();
            return false;
        } else
            return true;
    }

    class LoadAccountInfoTask extends AsyncTask<Void, Void, AccountInfoResponse> {

        @Override
        protected void onPostExecute(AccountInfoResponse air) {
            if (air != null) {
                tvAccount.setText(air.getAccount());
                tvBalance.setText(air.getBalance().toString());
                if (air.getCurrency().equals("643"))
                    tvCurrency.setText("?????");
            } else {
                tvAccount.setText("");
                tvBalance.setText("");
                tvCurrency.setText("");
            }
        }

        @Override
        protected AccountInfoResponse doInBackground(Void... params) {
            AndroidHttpClient client = Utils.httpClient();
            try {
                if (isAuthorized(false)) {
                    ApiCommandsFacade ym = Utils.getYandexMoney(Consts.CLIENT_ID, client);
                    return ym.accountInfo(restoreToken());
                }
            } catch (InsufficientScopeException e) {
                // do new authorization with sufficient permissions scope
            } catch (InvalidTokenException e) {
                // do auth again
            } catch (IOException e) {
                // there is no internet ;-(
            } finally {
                client.close();
            }
            return null;
        }
    }

    private class MyDialogListener implements YandexMoneyDroid.DialogListener {
        public void onSuccess(Bundle values) {
            String token = values.getString(ActivityParams.AUTH_OUT_ACCESS_TOKEN);
            if (token != null) {
                storeToken(token);
                Toast.makeText(GreatAppActivity.this, "Success. Access token = " + token, Toast.LENGTH_LONG).show();
            }

            String paymentId = values.getString(ActivityParams.PAYMENT_OUT_OPERATION_ID);
            if (paymentId != null) {
                Toast.makeText(GreatAppActivity.this, "payment successfully finished. Payment id: " + paymentId, Toast.LENGTH_LONG).show();
            }
        }

        public void onFail(String cause) {
            Toast.makeText(GreatAppActivity.this, "Fail: " + cause, Toast.LENGTH_LONG).show();
        }

        public void onException(Exception exception) {
            Toast.makeText(GreatAppActivity.this, "Exception: " + exception.getClass().getName(), Toast.LENGTH_LONG)
                    .show();            
        }

        public void onCancel() {
            Toast.makeText(GreatAppActivity.this, "Cancelled", Toast.LENGTH_LONG).show();
        }
    }

}




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