1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.example.mytest;

import java.lang.ref.WeakReference;

import android.os.Handler;
import android.os.Message;
import android.util.SparseArray;
import android.widget.Toast;

public class PurchaseSDK{

    private MainActivity context;
    private MainHandler handler;
    private int buyingGoodsId;
    private SparseArray<ItemInfo> payInfoMap = new SparseArray<ItemInfo>();

    public static final int OP_LOGIN     = 0;
    public static final int OP_BUY_GOODS = 1;
    
    public enum AppPURCHASESTATE {
        PS_SUCESS, PS_FAIL, PS_CANCEL
    };
    
    private static class SingletonHolder{
        private static PurchaseSDK instance = new PurchaseSDK();
    }
    
    private PurchaseSDK(){}
    
    public static PurchaseSDK getInstance(){
        return SingletonHolder.instance;
    }
    
    public class ItemInfo {
        public String productName;
        public String price;
        public String payCode;
        public ItemInfo(String _ProductName, String _Amount, String _PayCode) {
            productName = _ProductName;
            price = _Amount;
            payCode = _PayCode;
        }
    }
    
    private void initPayInfoMap() {
        payInfoMap.put(1, new ItemInfo("钻石", "2", "30000911554301"));
    }
    
    private ItemInfo getPayCodeByGoodsId(int goodsId){
        return payInfoMap.get(goodsId);
    }
    
    public int getBuyingGoodsId() {
        return buyingGoodsId;
    }

    public void setBuyingGoodsId(int buyingGoodsId) {
        this.buyingGoodsId = buyingGoodsId;
    }
    
    static class MainHandler extends Handler{
        
        WeakReference<MainActivity> mActivity;
        MainHandler(MainActivity activity){
            mActivity = new WeakReference<MainActivity>(activity);
        }
        
        @Override
        public void handleMessage(Message msg) {
            
            //MainActivity theActivity = mActivity.get(); 
            switch (msg.what) {
            case OP_LOGIN:
                PurchaseSDK.getInstance().doLogin();
                break;
                
            case OP_BUY_GOODS:
                PurchaseSDK.getInstance().buy();
                break;
            }
        }
    }

    public void init(MainActivity _context) {
        initPayInfoMap();
        context = _context;
        handler = new MainHandler(context);
    }
    
    public void sendLoginMessage()
    {
        Message msg = new Message();
        msg.what = OP_LOGIN;
        handler.sendMessage(msg);
    }
    
    public void sendBuyGoodsMessage()
    {
        Message msg = new Message();
        msg.what = OP_BUY_GOODS;
        handler.sendMessage(msg);
    }
    
    private void doLogin() {
        
    }

    private void buy() {
        int goodsId = getBuyingGoodsId();
        ItemInfo item = getPayCodeByGoodsId(goodsId);
        pay(item);
    }
    
    private void pay(ItemInfo item) {
        
    }
    
    public void onBuy(AppPURCHASESTATE result, String orderId) {

    }
    
    public void ToastText(String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
    }
    
}