Search code examples
androidin-app-billing

In-App Billing, disable and enable editText based on premium purchase


I still cannot wrap my head around this in-app billing. The idea is that before premium upgrade, I cannot type anything to the EditText. There is no error (the app works perfectly), but I must have made a logical mistake somewhere because after the premium purchase, variable mPremium should be equal to 1 (if no premium, mPremium==0). Any help will be appreciated.

public class MainActivity extends Activity {

private static final String TAG = "com.dt.tryout4";
EditText txtIncome;
KeyListener mKeyListener;
IabHelper mHelper;
int mPremium;
static final String ITEM_SKU = "android.test.purchased";
Button upgrade;
TextView txtTest;
SharedPreferences sp;

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

    sp = getPreferences(MODE_PRIVATE);
    mPremium = sp.getInt("premium", 0);
    txtTest = (TextView) findViewById(R.id.textTest);
    txtTest.setText(Integer.toString(mPremium));

    txtIncome = (EditText) findViewById(R.id.textIncome);
    mKeyListener = txtIncome.getKeyListener();
    if (mPremium==0)
    {
        txtIncome.setKeyListener(null);
    }
    else if(mPremium==1)
    {
        txtIncome.setKeyListener(mKeyListener);
    }


    upgrade = (Button) findViewById(R.id.btnUpgrade);

    String base64EncodedPublicKey =
            "<your license key here>";

    mHelper = new IabHelper(this, base64EncodedPublicKey);

    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
                                   public void onIabSetupFinished(IabResult result)
                                   {
                                       if (!result.isSuccess()) {
                                           Log.d(TAG, "In-app Billing setup failed: " +
                                                   result);
                                       } else {
                                           Log.d(TAG, "In-app Billing is set up OK");
                                       }
                                   }
                               });
}

public void premiumUpgrade (View view)
{
    try {
        mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001, mPurchaseFinishedListener, "mypurchasetoken");
    } catch (IabHelper.IabAsyncInProgressException e) {
        e.printStackTrace();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
                                Intent data)
{
    if (!mHelper.handleActivityResult(requestCode,
            resultCode, data)) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
        = new IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(IabResult result,
                                      Purchase purchase)
    {
        if (result.isFailure()) {
            return;
        }
        else if (purchase.getSku().equals(ITEM_SKU)) {
            upgrade.setEnabled(false);
            mPremium=1;
            saveData();
        }

    }
};

public void saveData()
{
    SharedPreferences.Editor spe = getPreferences(MODE_PRIVATE).edit();
    spe.putInt("premium", mPremium);
    spe.apply();
}


public static boolean verifyPurchase(String base64PublicKey,
                                     String signedData, String signature) {
    if (TextUtils.isEmpty(signedData) ||
            TextUtils.isEmpty(base64PublicKey) ||
            TextUtils.isEmpty(signature)) {
        Log.e(TAG, "Purchase verification failed: missing data.");
        if (BuildConfig.DEBUG) {
            return true;
        }
        return false;
    }

    PublicKey key = Security.generatePublicKey(base64PublicKey);
    return Security.verify(key, signedData, signature);
}

Solution

  • Here's my updated answer. Apparently, you have to delay the Runnable r so that your phone/ emulator can handle the purchase appropriately. So you should add this code if your purchase is successful:

    Handler handler = new Handler();
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            txtIncome.setEnabled(true);
                            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                            SharedPreferences.Editor editor = preferences.edit();
                            editor.putBoolean("isPurchase",true);
                            editor.commit();
                        }
                    }, 4000);
    

    Then, you can add this within onCreate:

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        boolean isPurchase = preferences.getBoolean("isPurchase", false);
        if(isPurchase==true)
        {
            txtIncome.setEnabled(true);
        }
        else
        {
            txtIncome.setEnabled(false);
        }