Blame view

app/src/main/java/com/gump/passport/demo/MainActivity.java 7.25 KB
c507bd179   赵康   add ignore file
1
2
3
  package com.gump.passport.demo;
  
  import android.content.Intent;
f905cc410   赵康   SDK has update to...
4
  import android.content.SharedPreferences;
c507bd179   赵康   add ignore file
5
  import android.os.Bundle;
f905cc410   赵康   SDK has update to...
6
  import android.preference.PreferenceManager;
c507bd179   赵康   add ignore file
7
8
  import android.support.v7.app.AppCompatActivity;
  import android.util.Log;
f905cc410   赵康   SDK has update to...
9
10
  import android.view.Menu;
  import android.view.MenuItem;
c507bd179   赵康   add ignore file
11
  import android.view.View;
f905cc410   赵康   SDK has update to...
12
  import android.widget.Button;
c507bd179   赵康   add ignore file
13
  import android.widget.TextView;
f905cc410   赵康   SDK has update to...
14
  import android.widget.Toast;
c507bd179   赵康   add ignore file
15
16
17
18
19
20
21
22
  
  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;
f905cc410   赵康   SDK has update to...
23
24
  import com.gump.payment.callback.PurchaseCallback;
  import com.gump.payment.callback.ResultCallback;
c507bd179   赵康   add ignore file
25
26
  
  public class MainActivity extends AppCompatActivity implements StateListener, PurchaseCallback {
f905cc410   赵康   SDK has update to...
27
      private static final String TAG = "MainActivity";
c507bd179   赵康   add ignore file
28
      private Passport passport;
c507bd179   赵康   add ignore file
29
      private TextView tvInfo;
f905cc410   赵康   SDK has update to...
30
31
32
33
      private View mainLayout;
      private Button btnLogin;
  
      private GamePlayer currentPlayer;
c507bd179   赵康   add ignore file
34

f905cc410   赵康   SDK has update to...
35
36
37
      private Button btnIab;
      private Button btnPay;
      private Button btnCheck;
c507bd179   赵康   add ignore file
38
39
40
41
42
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
c507bd179   赵康   add ignore file
43
44
  
          tvInfo = findViewById(R.id.info);
f905cc410   赵康   SDK has update to...
45
46
47
          mainLayout = findViewById(R.id.main_layout);
          btnLogin = findViewById(R.id.login);
          btnLogin.setOnClickListener(new View.OnClickListener() {
c507bd179   赵康   add ignore file
48
49
50
51
52
              @Override
              public void onClick(View v) {
                  passport.signIn(MainActivity.this);
              }
          });
f905cc410   赵康   SDK has update to...
53
54
          btnCheck = mainLayout.findViewById(R.id.check);
          btnCheck.setOnClickListener(new View.OnClickListener() {
c507bd179   赵康   add ignore file
55
56
              @Override
              public void onClick(View v) {
f905cc410   赵康   SDK has update to...
57
58
59
60
61
62
63
64
                  initPay();
              }
          });
          btnIab = mainLayout.findViewById(R.id.iab);
          btnIab.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Bundle payInfo = assemblePayBundle();
c507bd179   赵康   add ignore file
65
66
67
                  Payment.launchIAP(MainActivity.this, payInfo, MainActivity.this);
              }
          });
f905cc410   赵康   SDK has update to...
68
69
          btnPay = mainLayout.findViewById(R.id.pay);
          btnPay.setOnClickListener(new View.OnClickListener() {
c507bd179   赵康   add ignore file
70
71
              @Override
              public void onClick(View v) {
f905cc410   赵康   SDK has update to...
72
                  Bundle payInfo = assemblePayBundle();
c507bd179   赵康   add ignore file
73
74
75
                  Payment.pay(MainActivity.this, payInfo, MainActivity.this);
              }
          });
f905cc410   赵康   SDK has update to...
76

9e8388286   赵康   修改账号绑定,增加账号联动
77
78
79
80
          /**
           * gump账号联动
           */
          mainLayout.findViewById(R.id.link).setOnClickListener(new View.OnClickListener() {
f905cc410   赵康   SDK has update to...
81
82
83
84
85
              @Override
              public void onClick(View v) {
                  passport.link(MainActivity.this);
              }
          });
9e8388286   赵康   修改账号绑定,增加账号联动
86
87
88
          /**
           * 切换PlayGames账号
           */
f905cc410   赵康   SDK has update to...
89
90
91
          mainLayout.findViewById(R.id.switch_acc).setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
9e8388286   赵康   修改账号绑定,增加账号联动
92
                  passport.switchAccount();
f905cc410   赵康   SDK has update to...
93
94
              }
          });
f905cc410   赵康   SDK has update to...
95
96
97
98
99
          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");
9e8388286   赵康   修改账号绑定,增加账号联动
100
101
102
          /**
           * SDK配置
           */
f905cc410   赵康   SDK has update to...
103
104
105
          SDKAgent.init(getApplicationContext(), appId, channel);
          SDKAgent.getSettings().setDebug(mode);
          SDKAgent.getSettings().setPaymentVersion(PaymentVersion.valueOf(payVersion));
9e8388286   赵康   修改账号绑定,增加账号联动
106
107
108
          /**
           * 实例化登录功能
           */
f905cc410   赵康   SDK has update to...
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
          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);
c507bd179   赵康   add ignore file
155
156
157
      }
  
      @Override
f905cc410   赵康   SDK has update to...
158
      public void onResume() {
c507bd179   赵康   add ignore file
159
160
161
162
163
          super.onResume();
          passport.onResume();
      }
  
      @Override
f905cc410   赵康   SDK has update to...
164
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
c507bd179   赵康   add ignore file
165
166
167
168
169
170
          if (!passport.onActivityResult(requestCode, resultCode, data))
              super.onActivityResult(requestCode, resultCode, data);
      }
  
      @Override
      public void onActionSucced(Actions action, GamePlayer player) {
f905cc410   赵康   SDK has update to...
171
172
173
174
175
176
177
          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;
9e8388286   赵康   修改账号绑定,增加账号联动
178
179
180
181
182
              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://账号已切换
f905cc410   赵康   SDK has update to...
183
184
185
186
                  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;
          }
c507bd179   赵康   add ignore file
187
188
189
      }
  
      @Override
f905cc410   赵康   SDK has update to...
190
191
      public void onActionFailured(Throwable e) {
          tvInfo.setText("login has error:" + e.getMessage());
c507bd179   赵康   add ignore file
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
      }
  
      @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");
      }
  }