Integrating With PlayHub

PlayHub supports external gaming apps, and as such you can easily integrate your application using the following steps.

Before you begin

You are welcomed to use existing PlayHub supporting games as examples to help you through this guide. One suggested game which uses most of the features and therefore did most of the steps is Blokish:

Adding PlayHub support for your game

1. Declare the activity which PlayHub will call (The game activity itself, not a menu activity) in your manifest.


    <activity
        ...
        android:exported="true"
        >
        <intent-filter>
            <action android:name="playhub.game" />
        </intent-filter>
        <intent-filter>
            <action android:name="playhub.intent.play" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
         ...
    </activity>
                            

2. If your game has a max number of supported players (i.e. - maximum 2 players for a chess game), declare it using the activity's metadata. If set, it must be at least 2 otherwise it is ignored.


    <activity
        ...
        >
        <meta-data android:name="com.playhub.maxPlayers" android:value="YOUR_MAX" />
        ...
    </activity>
                        

3. If you wish to get ad revenue, supply your AdMob interstitial ad id in the activity's metadata:


    <activity
        ...
        >
        <meta-data android:name="com.playhub.interstitialAdUnitId" android:value="ca-app-pub-9707526852705958/3169613857" />
        ...
    </activity>
                            

4. If you wish to get the player's images from PlayHub, add the permission to read from the external storage (PlayHub passes your application the user's images via the external storage).


    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
                            

5. If you are using progurad, change its settings in order to read the data received from PlayHub. To do so add the following to the proguard settings file:


-keep class * implements java.io.Serializable

-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    private static final java.io.ObjectStreamField[] serialPersistentFields;
    !static !transient <fields>
    !private <fields>
    !private <methods>
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}
    						

6. Add PlayHub support library as an external jar to your project's build path. The library is licensed under GPLv3, and its source can be found here.

7. Adapt your activity to interact with PlayHub. When your activity is called from PlayHub, the received data can be read in the onCreate as follows:


    import com.playhub.GameInfoFromPlayHub;
    import com.playhub.Consts;
    ...
    private GameInfoFromPlayHub gameInfoFromPlayHub;

    protected void onCreate(Bundle savedInstanceState) {
        ...
        Bundle extras = getIntent().getExtras();
        if ((extras != null) && (extras.containsKey(Consts.GAME_INFO_FROM_PLAYHUB_KEY))) {
            gameInfoFromPlayHub = (GameInfoFromPlayHub) extras.getSerializable(Consts.GAME_INFO_FROM_PLAYHUB_KEY);
        }
        ...
    }
                            

When your game activity finishes, it should return the resulted information to PlayHub as follows:


		import com.playhub.GameInfoToReturnToPlayHub;
		import com.playhub.Consts;
    	...
        GameInfoToReturnToPlayHub gameInfoToReturnToPlayHub = new GameInfoToReturnToPlayHub();
        ...
        Intent output = new Intent();
        output.putExtra(Consts.GAME_INFO_TO_RETURN_TO_PLAYHUB_KEY, gameInfoToReturnToPlayHub);
        setResult(RESULT_OK, output);
        finish();
                            

8. Read the documentation of the classes GameInfoFromPlayHub and GameInfoToReturnToPlayHub, and make sure to read and populate them as instructed.

Advanced featues

1. If you wish to be included in the "Find more games" feature of PlayHub, which references the user to all the supporting games in Google Play, simply declare your app in Google Play as PlayHub supported. You do so by adding the phrase PLAYHUBSUPPORTEDAPP anywhere in your application's description in Google Play.

2. To be able to open PlayHub from your application, you may use PlayHub's icon from the icon pack, and the following code:


    String playHubPackageName = "com.playhub";
    if (isAppInstalled(playHubPackageName)) {
        Intent i = getPackageManager().getLaunchIntentForPackage(playHubPackageName);
        startActivity(i);
    } else {
        try {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + playHubPackageName)));
        } catch (android.content.ActivityNotFoundException activityNotFoundException) {
            startActivity(new Intent(Intent.ACTION_VIEW, 
                Uri.parse("http://play.google.com/store/apps/details?id=" + playHubPackageName)));
        }
    }

private boolean isAppInstalled(String uri) {
        PackageManager pm = getPackageManager();
        boolean app_installed;
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            app_installed = true;
        }
        catch (PackageManager.NameNotFoundException e) {
            app_installed = false;
        }
        return app_installed;
    }