Skip to content
This repository has been archived by the owner on Apr 28, 2022. It is now read-only.

Commit

Permalink
update OpenIAB library and trivialDrive sample to be compatible with …
Browse files Browse the repository at this point in the history
…new aidls and dynamically bound to OpenStore instances.
  • Loading branch information
Yury Vasileuski committed Jun 18, 2013
1 parent 4c7873e commit add4795
Show file tree
Hide file tree
Showing 23 changed files with 505 additions and 295 deletions.
16 changes: 3 additions & 13 deletions library/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.onepf.life2"
package="org.onepf.oms"
android:versionCode="9"
android:versionName="2.7">

<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17"/>
android:minSdkVersion="8"
android:targetSdkVersion="8"/>

<uses-permission android:name="com.android.vending.BILLING"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
Expand All @@ -42,17 +42,7 @@
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="org.onepf.life2.GameActivity"
android:label="@string/title_activity_game"
android:configChanges="orientation|screenSize|keyboardHidden|locale"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name="com.amazon.inapp.purchasing.ResponseReceiver">
<intent-filter>
<action
Expand Down
25 changes: 17 additions & 8 deletions library/src/org/onepf/oms/Appstore.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,34 @@

package org.onepf.oms;

import android.content.Intent;

/**
* Author: Ruslan Sayfutdinov
* Date: 16.04.13
*/

public interface Appstore {
// IOpenAppstore.idls methods
boolean isAppAvailable(String packageName);

boolean isInstaller();
boolean isInstaller(String packageName);

boolean isServiceSupported(AppstoreService appstoreService);
boolean couldBeInstaller(String packageName);

AppstoreInAppBillingService getInAppBillingService();
Intent getServiceIntent(String packageName, int serviceType);

String getAppstoreName();

AppstoreName getAppstoreName();
Intent getProductPageIntent(String packageName);

//... other methods that return different Appstore specific services
Intent getRateItPageIntent(String packageName);

//a method to open application product page in this appstore
//a method to open the reviews page (aka "rate it") in this appstore
//a method that returns if the appstore allows links to developer website
Intent getSameDeveloperPageIntent(String packageName);

boolean areOutsideLinksAllowed();

// additional methods
AppstoreInAppBillingService getInAppBillingService();
AppstoreType getAppstoreType();
}
92 changes: 56 additions & 36 deletions library/src/org/onepf/oms/AppstoreServiceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public interface OnAppstoreServiceManagerInitFinishedListener {
public void onAppstoreServiceManagerInitFinishedListener();
}


AppstoreServiceManager(Context context, Map<String, String> extra) {
mContext = context;
mExtra = extra;
Expand All @@ -60,93 +59,114 @@ public interface OnAppstoreServiceManagerInitFinishedListener {


void startSetup(final OnAppstoreServiceManagerInitFinishedListener listener) {
final OnAppstoreServiceManagerInitFinishedListener mListener = listener;
final OnAppstoreServiceManagerInitFinishedListener initListener = listener;
PackageManager packageManager = mContext.getPackageManager();
Intent intentAppstoreServices = new Intent(BIND_INTENT);

final Intent intentAppstoreServices = new Intent(BIND_INTENT);
List<ResolveInfo> infoList = packageManager.queryIntentServices(intentAppstoreServices, 0);
if (infoList.size() == 0) {
mListener.onAppstoreServiceManagerInitFinishedListener();
initListener.onAppstoreServiceManagerInitFinishedListener();
}

final CountDownLatch countDownLatch = new CountDownLatch(infoList.size());
for (ResolveInfo info : infoList) {
String packageName = info.serviceInfo.packageName;
String name = info.serviceInfo.name;
Intent intentAppstore = new Intent();
Intent intentAppstore = new Intent(intentAppstoreServices);
intentAppstore.setClassName(packageName, name);
mContext.bindService(intentAppstore, new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "appstoresService connected for component: " + name.flattenToShortString());
IOpenAppstore openAppstoreService = IOpenAppstore.Stub.asInterface(service);

final OpenAppstore openAppstore = new OpenAppstore(openAppstoreService, mContext);

String appstoreName = null;
try {
appstoreName = openAppstoreService.getAppstoreName();
} catch (RemoteException e) {
Log.e(TAG, "RemoteException: " + e.getMessage());
} catch (RemoteException e) {
Log.w(TAG, "RemoteException: " + e.getMessage());
}
String publicKey = mExtra.get(appstoreName);
final OpenAppstore openAppstore = new OpenAppstore(openAppstoreService, mContext, appstoreName);

openAppstore.startSetup(publicKey, new OnAppstoreStartSetupFinishListener() {
@Override
public void onAppstoreStartSetupFinishListener(boolean isOk) {
synchronized (appstores) {
appstores.add(openAppstore);
Log.d(TAG, "onAppstoreStartSetupFinishListener: " + String.valueOf(isOk));
if (isOk == true) {
synchronized (appstores) {
Log.d(TAG, "add new open store by type: " + openAppstore.getAppstoreName());
if (appstores.contains(openAppstore) == false) {
appstores.add(openAppstore);
}
}
}
countDownLatch.countDown();
if (countDownLatch.getCount() == 0) {
listener.onAppstoreServiceManagerInitFinishedListener();
initListener.onAppstoreServiceManagerInitFinishedListener();
}
}
});
}

@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "appstoresService disconnected for component: " + name.flattenToShortString());
//Nothing to do here
}
}, Context.BIND_AUTO_CREATE);
}
}

private static AppstoreServiceManager instance;

/*
public static AppstoreServiceManager getInstance(Context context, String publicKey, String samsungGroupId) {
if (instance == null) {
instance = new AppstoreServiceManager(context, publicKey, samsungGroupId);
}
return instance;
}
*/

public Appstore getAppstoreForService(AppstoreService appstoreService) {
//TODO: implement logic to choose app store.
String packageName = mContext.getPackageName();

if (appstoreService == AppstoreService.IN_APP_BILLING) {
Appstore installer = getInstallerAppstore();
if (installer != null) {
Log.d(TAG, "Installer appstore: " + installer.getAppstoreName().name());
if (installer.isServiceSupported(appstoreService)) {
Log.d(TAG, "Installer appstore: " + installer.getAppstoreName());
Intent inappIntent = installer.getServiceIntent(packageName, 0);
if (inappIntent != null) {
return installer;
}
}
for (Appstore appstore : appstores) {
if (appstore.isServiceSupported(appstoreService)) {
return appstore;

synchronized (appstores) {
for (Appstore appstore : appstores) {
Intent inappIntent = installer.getServiceIntent(packageName, 0);
if (inappIntent != null) {
return appstore;
}
}
}
}
return null;
}

public Appstore getInstallerAppstore() {
for (Appstore appstore : appstores) {
if (appstore.isInstaller()) {
return appstore;
//TODO: implement logic to choose app store.
String packageName = mContext.getPackageName();
Appstore returnAppstore = null;
synchronized (appstores) {
for (Appstore appstore : appstores) {
if (appstore.isInstaller(packageName)) {
return appstore;
}
// return last appstore if no one is selected (debug mode)
returnAppstore = appstore;
}
for (Appstore appstore : appstores) {
if (appstore.couldBeInstaller(packageName)) {
return appstore;
}
// return last appstore if no one is selected (debug mode)
returnAppstore = appstore;
}
}
return null;
}

public List<Appstore> getAppstoresSupportingAPI(String packageName, AppstoreService appstoreService) {
return null;
if (returnAppstore != null) {
Log.w(TAG, "getInstallerAppstore returns random appstore");
}
return returnAppstore;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* Date: 16.04.13
* Time: 17:41
*/
public enum AppstoreName {
public enum AppstoreType {
GOOGLE,
AMAZON,
SAMSUNG,
Expand Down
60 changes: 60 additions & 0 deletions library/src/org/onepf/oms/DefaultAppstore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.onepf.oms;

import android.content.Intent;

public class DefaultAppstore implements Appstore {
@Override
public boolean isAppAvailable(String packageName) {
return false;
}

@Override
public boolean isInstaller(String packageName) {
return false;
}

@Override
public boolean couldBeInstaller(String packageName) {
return false;
}

@Override
public Intent getServiceIntent(String packageName, int serviceType) {
return null;
}

@Override
public String getAppstoreName() {
return null;
}

@Override
public Intent getProductPageIntent(String packageName) {
return null;
}

@Override
public Intent getRateItPageIntent(String packageName) {
return null;
}

@Override
public Intent getSameDeveloperPageIntent(String packageName) {
return null;
}

@Override
public boolean areOutsideLinksAllowed() {
return false;
}

@Override
public AppstoreInAppBillingService getInAppBillingService() {
return null;
}

@Override
public AppstoreType getAppstoreType() {
return AppstoreType.OPENSTORE;
}
}
5 changes: 2 additions & 3 deletions library/src/org/onepf/oms/IOpenAppstore.aidl
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ interface IOpenAppstore {

boolean isInstaller(String packageName);

boolean isServiceSupported(String packageName, int serviceType);
boolean couldBeInstaller(String packageName);

// 0 - billing service
Intent getServiceIntent(String packageName, int serviceType);

String getAppstoreName();
Expand All @@ -43,4 +42,4 @@ interface IOpenAppstore {
Intent getSameDeveloperPageIntent(String packageName);

boolean areOutsideLinksAllowed();
}
}
17 changes: 10 additions & 7 deletions library/src/org/onepf/oms/OpenIabHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,13 @@ public void startSetup(final IabHelper.OnIabSetupFinishedListener listener, fina
public void onAppstoreServiceManagerInitFinishedListener() {
mAppstore = mServiceManager.getAppstoreForService(AppstoreService.IN_APP_BILLING);
if (mAppstore == null) {
logWarn("FAIL startSetup: Appstore is null");
return;
}
logDebug("OpenIabHelper use appstore: " + mAppstore.getAppstoreName());

mAppstoreBillingService = mAppstore.getInAppBillingService();
Log.d(TAG, "OpenIabHelper use appstore: " + mAppstore.getAppstoreName().name());

mSetupDone = true;
// TODO: this is always false!
if (!mSetupDone) {
Expand Down Expand Up @@ -134,10 +137,10 @@ public void launchSubscriptionPurchaseFlow(Activity act, OpenSku sku, int reques
public void launchPurchaseFlow(Activity act, OpenSku sku, String itemType, int requestCode,
IabHelper.OnIabPurchaseFinishedListener listener, String extraData) {
checkSetupDone("launchPurchaseFlow");
String skuCurrentStore = sku.getSku(mAppstore.getAppstoreName());
String skuCurrentStore = sku.getSku(mAppstore.getAppstoreType());
if (skuCurrentStore == null) {
// TODO: throw an exception
Log.e(TAG, "No SKU for current appstore");
logError("No SKU for current appstore");
return;
}
mAppstoreBillingService.launchPurchaseFlow(act, skuCurrentStore, itemType, requestCode, listener, extraData);
Expand All @@ -160,10 +163,10 @@ public Inventory queryInventory(boolean querySkuDetails, List<OpenSku> moreItemS
moreItemSkusCurrentStore = null;
} else {
for (OpenSku sku : moreItemSkus) {
String skuCurrentStore = sku.getSku(mAppstore.getAppstoreName());
String skuCurrentStore = sku.getSku(mAppstore.getAppstoreType());
if (skuCurrentStore == null) {
// TODO: throw an exception
Log.e(TAG, "No matching SKU found for current appstore");
logError("No matching SKU found for current appstore");
return null;
}
moreItemSkusCurrentStore.add(skuCurrentStore);
Expand All @@ -174,10 +177,10 @@ public Inventory queryInventory(boolean querySkuDetails, List<OpenSku> moreItemS
moreSubsSkusCurrentStore = null;
} else {
for (OpenSku sku : moreSubsSkus) {
String skuCurrentStore = sku.getSku(mAppstore.getAppstoreName());
String skuCurrentStore = sku.getSku(mAppstore.getAppstoreType());
if (skuCurrentStore == null) {
// TODO: throw an exception
Log.e(TAG, "No matching SKU found for current appstore");
logError("No matching SKU found for current appstore");
return null;
}
moreSubsSkusCurrentStore.add(skuCurrentStore);
Expand Down
Loading

0 comments on commit add4795

Please sign in to comment.