Android Open Source - GradeCalculator-Android Google Play Billing Service






From Project

Back to project page GradeCalculator-Android.

License

The source code is released under:

Copyright (C) 2012 Jimmy Theis Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in ...

If you think the Android project GradeCalculator-Android 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

/*
 * Copyright (C) 2012 Jimmy Theis. Licensed under the MIT License:
 * //from   ww w  .  j a  v a  2s  .c o m
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.jetheis.android.grades.billing.googleplay;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.android.vending.billing.IMarketBillingService;
import com.jetheis.android.grades.Constants;
import com.jetheis.android.grades.billing.Security;

public class GooglePlayBillingService extends Service implements ServiceConnection {

    private IMarketBillingService mService;
    private final IBinder mBinder = new GooglePlayBillingBinder();
    private boolean mWillConnect;
    private OnGooglePlayBillingSupportResultListener mOnSupportListener;

    public class GooglePlayBillingBinder extends Binder {
        GooglePlayBillingService getService() {
            return GooglePlayBillingService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        try {
            mWillConnect = bindService(new Intent(
                    GooglePlayBillingConstants.GOOGLE_PLAY_BIND_INTENT), this, BIND_AUTO_CREATE);
            if (!mWillConnect) {
                Log.e(Constants.TAG, "Could not bind to the Google Play service");
            }
        } catch (SecurityException e) {
            Log.e(Constants.TAG, "Security exception: " + e);
        }
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        Log.i(Constants.TAG, "Google Play service connected");
        mService = IMarketBillingService.Stub.asInterface(service);

        if (mOnSupportListener != null) {
            Log.v(Constants.TAG, "Notifying listener of Google Play connection");
            mOnSupportListener.onGooglePlayBillingSupportResultFound(isBillingSupported());
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        mService = null;
    }

    protected Bundle makeGooglePlayRequestBundle(String method) {
        Bundle request = new Bundle();
        request.putString(GooglePlayBillingConstants.GOOGLE_PLAY_BUNDLE_KEY_BILLING_REQUEST, method);
        request.putInt(GooglePlayBillingConstants.GOOGLE_PLAY_BUNDLE_KEY_API_VERSION,
                GooglePlayBillingConstants.GOOGLE_PLAY_API_VERSION);
        request.putString(GooglePlayBillingConstants.GOOGLE_PLAY_BUNDLE_KEY_PACKAGE_NAME,
                getPackageName());

        return request;
    }

    public Bundle makeGooglePlayRequest(String method) throws RemoteException {
        if (mService == null) {
            Log.e(Constants.TAG, "Google Play service not connected");
            return null;
        }

        return sendBillingRequest(makeGooglePlayRequestBundle(method));
    }

    private Bundle sendBillingRequest(Bundle request) throws RemoteException {

        Bundle result = mService.sendBillingRequest(request);

        String method = request
                .getString(GooglePlayBillingConstants.GOOGLE_PLAY_BUNDLE_KEY_BILLING_REQUEST);
        int responseCode = result
                .getInt(GooglePlayBillingConstants.GOOGLE_PLAY_BUNDLE_KEY_RESPONSE_CODE);

        if (responseCode == GooglePlayBillingConstants.GooglePlayResponseCode.RESULT_OK.ordinal()) {
            Log.v(Constants.TAG,
                    "Sent Google Play "
                            + method
                            + " request ("
                            + result.getLong(GooglePlayBillingConstants.GOOGLE_PLAY_BUNDLE_KEY_REQUEST_ID)
                            + "): "
                            + GooglePlayBillingConstants.GooglePlayResponseCode
                                    .fromInt(responseCode));
        } else {
            Log.e(Constants.TAG,
                    "Sent Google Play "
                            + method
                            + " request ("
                            + result.getLong(GooglePlayBillingConstants.GOOGLE_PLAY_BUNDLE_KEY_REQUEST_ID)
                            + "): "
                            + GooglePlayBillingConstants.GooglePlayResponseCode
                                    .fromInt(responseCode));
        }

        return result;
    }

    public void checkIsBillingSupported(OnGooglePlayBillingSupportResultListener onSupportListener) {
        if (mService == null && mWillConnect) {
            Log.v(Constants.TAG, "Google Play service not ready. Registering listener");
            mOnSupportListener = onSupportListener;
        } else if (mService == null) {
            Log.v(Constants.TAG, "No Google Play connection found. Notifying of no support");
            onSupportListener.onGooglePlayBillingSupportResultFound(false);
        } else {
            Log.v(Constants.TAG, "Google Play already connected. Checking support");
            onSupportListener.onGooglePlayBillingSupportResultFound(isBillingSupported());
        }
    }

    private boolean isBillingSupported() {
        try {
            Bundle response = makeGooglePlayRequest(GooglePlayBillingConstants.GOOGLE_PLAY_REQUEST_METHOD_CHECK_BILLING_SUPPORTED);
            return response.getInt(GooglePlayBillingConstants.GOOGLE_PLAY_BUNDLE_KEY_RESPONSE_CODE) == GooglePlayBillingConstants.GooglePlayResponseCode.RESULT_OK
                    .ordinal();
        } catch (RemoteException e) {
            Log.e(Constants.TAG, "RemoteException: " + e.getLocalizedMessage());
            return false;
        }
    }

    public Bundle makeGooglePlayPurchaseRequest(String productId) throws RemoteException {
        Bundle request = makeGooglePlayRequestBundle(GooglePlayBillingConstants.GOOGLE_PLAY_REQUEST_METHOD_REQUEST_PURCHASE);
        request.putString(GooglePlayBillingConstants.GOOGLE_PLAY_BUNDLE_KEY_ITEM_ID, productId);
        return sendBillingRequest(request);
    }

    public Bundle makeGooglePlayPurchaseInformationRequest(String[] notifyIds)
            throws RemoteException {
        Bundle request = makeGooglePlayRequestBundle(GooglePlayBillingConstants.GOOGLE_PLAY_REQUEST_METHOD_GET_PURCHASE_INFORMATION);
        request.putStringArray(GooglePlayBillingConstants.GOOGLE_PLAY_BUNDLE_KEY_NOTIFY_IDS,
                notifyIds);
        request.putLong(GooglePlayBillingConstants.GOOGLE_PLAY_BUNDLE_KEY_NONCE,
                Security.generateNonce());

        return sendBillingRequest(request);
    }

    public Bundle makeGooglePlayRestoreTransactionsRequest() throws RemoteException {
        Bundle request = makeGooglePlayRequestBundle(GooglePlayBillingConstants.GOOGLE_PLAY_REQUEST_METHOD_RESTORE_TRANSACTIONS);
        request.putLong(GooglePlayBillingConstants.GOOGLE_PLAY_BUNDLE_KEY_NONCE,
                Security.generateNonce());

        return sendBillingRequest(request);
    }

    public Bundle makeGooglePlayConfirmNotificationsRequest(String[] notificationIds)
            throws RemoteException {
        Bundle request = makeGooglePlayRequestBundle(GooglePlayBillingConstants.GOOGLE_PLAY_REQUEST_METHOD_CONFIRM_NOTIFICATIONS);
        request.putStringArray(GooglePlayBillingConstants.GOOGLE_PLAY_BUNDLE_KEY_NOTIFY_IDS,
                notificationIds);

        return sendBillingRequest(request);
    }

    public interface OnGooglePlayBillingSupportResultListener {
        public void onGooglePlayBillingSupportResultFound(boolean billingSupported);
    }

}




Java Source Code List

com.jetheis.android.grades.Constants.java
com.jetheis.android.grades.activity.AboutActivity.java
com.jetheis.android.grades.activity.CourseActivity.java
com.jetheis.android.grades.activity.CourseListActivity.java
com.jetheis.android.grades.billing.BillingWrapper.java
com.jetheis.android.grades.billing.FreeBillingWrapper.java
com.jetheis.android.grades.billing.Security.java
com.jetheis.android.grades.billing.googleplay.GooglePlayBillingConstants.java
com.jetheis.android.grades.billing.googleplay.GooglePlayBillingReceiver.java
com.jetheis.android.grades.billing.googleplay.GooglePlayBillingService.java
com.jetheis.android.grades.billing.googleplay.GooglePlayBillingWrapper.java
com.jetheis.android.grades.fragment.AboutFragment.java
com.jetheis.android.grades.fragment.AddCourseDialogFragment.java
com.jetheis.android.grades.fragment.AddGradeComponentDialogFragment.java
com.jetheis.android.grades.fragment.BuyFullVersionFragment.java
com.jetheis.android.grades.fragment.CourseListFragment.java
com.jetheis.android.grades.fragment.CourseOverviewFragment.java
com.jetheis.android.grades.fragment.EditCourseDialogFragment.java
com.jetheis.android.grades.fragment.EditGradeComponentDialogFragment.java
com.jetheis.android.grades.fragment.GradeComponentListFragment.java
com.jetheis.android.grades.listadapter.CourseArrayAdapter.java
com.jetheis.android.grades.model.Course.java
com.jetheis.android.grades.model.GradeComponent.java
com.jetheis.android.grades.model.PercentageGradeComponent.java
com.jetheis.android.grades.model.PointTotalGradeComponent.java
com.jetheis.android.grades.storage.CourseStorageAdapter.java
com.jetheis.android.grades.storage.DatabaseHelper.java
com.jetheis.android.grades.storage.GradeComponentStorageAdapter.java
com.jetheis.android.grades.storage.Storable.java
com.jetheis.android.grades.storage.StorageAdapter.java
com.jetheis.android.grades.storage.StorageIterator.java