MainActivity.java 7.25 KB
package com.gump.passport.demo;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.gump.PaymentVersion;
import com.gump.SDKAgent;
import com.gump.gpassport.Actions;
import com.gump.gpassport.GamePlayer;
import com.gump.gpassport.Passport;
import com.gump.gpassport.StateListener;
import com.gump.payment.Payment;
import com.gump.payment.callback.PurchaseCallback;
import com.gump.payment.callback.ResultCallback;

public class MainActivity extends AppCompatActivity implements StateListener, PurchaseCallback {

    private static final String TAG = "MainActivity";
    private Passport passport;
    private TextView tvInfo;
    private View mainLayout;
    private Button btnLogin;

    private GamePlayer currentPlayer;

    private Button btnIab;
    private Button btnPay;
    private Button btnCheck;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvInfo = findViewById(R.id.info);
        mainLayout = findViewById(R.id.main_layout);
        btnLogin = findViewById(R.id.login);
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                passport.signIn(MainActivity.this);
            }
        });

        btnCheck = mainLayout.findViewById(R.id.check);
        btnCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initPay();
            }
        });
        btnIab = mainLayout.findViewById(R.id.iab);
        btnIab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle payInfo = assemblePayBundle();
                Payment.launchIAP(MainActivity.this, payInfo, MainActivity.this);
            }
        });

        btnPay = mainLayout.findViewById(R.id.pay);
        btnPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle payInfo = assemblePayBundle();
                Payment.pay(MainActivity.this, payInfo, MainActivity.this);
            }
        });

        /**
         * gump账号联动
         */
        mainLayout.findViewById(R.id.link).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                passport.link(MainActivity.this);
            }
        });

        /**
         * 切换PlayGames账号
         */
        mainLayout.findViewById(R.id.switch_acc).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                passport.switchAccount();
            }
        });

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        boolean mode = sp.getBoolean("key_mode", true);
        String appId = sp.getString("key_appid", "100");
        String channel = sp.getString("key_channel", "1000");
        String payVersion = sp.getString("key_pay_version", "V3");

        /**
         * SDK配置
         */
        SDKAgent.init(getApplicationContext(), appId, channel);
        SDKAgent.getSettings().setDebug(mode);
        SDKAgent.getSettings().setPaymentVersion(PaymentVersion.valueOf(payVersion));

        /**
         * 实例化登录功能
         */
        passport = new Passport.Builder().context(MainActivity.this).setListener(MainActivity.this).build();

    }

    private Bundle assemblePayBundle() {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        Bundle payInfo = new Bundle();
        payInfo.putString("product", sp.getString("key_product", "10080"));
        payInfo.putFloat("amount", sp.getFloat("key_amount", 0.1f));
        payInfo.putString("extraInfo", sp.getString("key_extra", "test"));
        payInfo.putString("serverId", sp.getString("key_server_id", "100"));
        payInfo.putString("roleId", sp.getString("key_role_id", "41080"));
        payInfo.putString("currency", sp.getString("key_currency", "THB"));
        return payInfo;
    }


    private void initPay() {
        btnCheck.setEnabled(false);
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        String serverId = sp.getString("key_server_id", "100");
        String roleId = sp.getString("key_role_id", "41080");
        Payment.shouldUseCoPay(this, serverId, roleId, new ResultCallback() {
            @Override
            public void onResult(boolean isRisk) {
                btnCheck.setEnabled(true);
                btnIab.setEnabled(isRisk);
                btnPay.setEnabled(!isRisk);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.setting) {
            Intent intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onResume() {
        super.onResume();
        passport.onResume();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (!passport.onActivityResult(requestCode, resultCode, data))
            super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onActionSucced(Actions action, GamePlayer player) {
        switch (action) {
            case SIGN:
                currentPlayer = player;
                tvInfo.setText("Login succed! gump id=" + player.getId() + ",playerType=" + player.getPlayerType());
                btnLogin.setVisibility(View.GONE);
                mainLayout.setVisibility(View.VISIBLE);
                break;
            case SWITCH_PENDING://将要切换PlayGame账号
                tvInfo.setText("The player wanna to switch Account!");
                Toast.makeText(this, "The account will be switched!", Toast.LENGTH_LONG).show();
                break;
            case SWITCH://账号已切换
                tvInfo.setText("Switch account succed! Pls reboot the game!");
                Toast.makeText(this, "The account has switched,pls reboot the Game!", Toast.LENGTH_LONG).show();
                break;
        }
    }

    @Override
    public void onActionFailured(Throwable e) {
        tvInfo.setText("login has error:" + e.getMessage());
    }

    @Override
    public void onPurchaseCompleted() {
        Log.i(TAG, "purchase completed");
    }

    @Override
    public void onPurchaseError(int code, String msg) {
        Log.i(TAG, "purchase error");
    }

    @Override
    public void onPurchaseCanceled() {
        Log.i(TAG, "purchase canceled");
    }
}