B4A Library Google Plus SDK Wrapper + Native Google +1Button

Introduction

This is my second contribution to the discovery of the decade (#b4a-rocks) and the fun continues!

Used SocialApi classes:
  • GooglePlusProvider: A non activity object that must be declared in any Sub Process_Globals.
  • GooglePlusOneButton: A CustomView (implements DesignerCustomView) that can be used design-time.
  • SocialApiActivity: An activity object that can be only declared in a Sub Globals and is used to bind a GooglePlusProvider object to an activity through the GooglePlusProvider.SetActivity method.
Getting started
  1. Download the latest socialapi package, extract it anywhere on your hard disk and note the folder name.
  2. Copy the wrapper files (jar + xml) in the socialapi folder into your B4A Libraries folder.
  3. Each sample in the socialapi\googleplus\samples folder has AdditionalRes and AdditionalJar directives that have to be changed. Change the C:\b4a-dev folder to where you have extracted the socialapi archive.

The samples folder contains:
  • Sample1: Quick start sample that can be used as a bare-bone template for your new apps
Step-by-step new app tutorial (based on Sample1)

1. Add following code to your manifest:
B4X:
AddApplicationText(
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version"/>)


2. Add following directives to your project's main file:
B4X:
#AdditionalRes: <your-installation-folder>\socialapi\googleplus\sdk\res, com.google.android.gms
#AdditionalJar: <your-installation-folder>\socialapi\googleplus\sdk\gplussdk.jar


3. Select the SocialApi library from the list of the available libraries in the IDE. Declare a GooglePlusProvider and a SocialApiActivity object in Sub Process_Globals and Sub Globals respectively
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Dim GooglePlus As GooglePlusProvider
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim ThisActivity As SocialApiActivity
End Sub


4. Drop a CustomView in the designer and set the CustomType property to GooglePlusOneButton. Then, Generate a Click handler.

upload_2014-7-2_19-50-26.png
upload_2014-7-2_19-50-35.png

B4X:
Sub BtnPlusOne_Click
    Log("+1 Clicked!")
End Sub


5. Call the GooglePlusProvider.SetActivity method in your Sub Activity_Resume in order to bind the GooglePlusProvider instance to the current activity passing the name of the event that will be raised every time the GooglePlus session changes. Also, call the BtnPlusOne.SetUrl to set the button's destination url.
B4X:
Sub Activity_Resume
    BtnPlusOne.SetUrl("http://www.b4x.com")
    GooglePlus.SetActivity(ThisActivity.Initialize("googleplus"))
End Sub


6. Create your event handlers
B4X:
Sub GooglePlus_Event (Provider as SocialApiProvider)
    BtnConnect.Enabled = Not(Provider.Connected)
    BtnDisconnect.Enabled = Provider.Connected
End Sub

Sub GooglePlust_Connected (Provider as SocialApiProvider)
    Msgbox(Provider.Me, "!")
End Sub

Sub GooglePlus_Disconnected (Provider as SocialApiProvider)
    Msgbox("Bye bye!", "JustDisconnected!")
End Sub

Sub GooglePlust_Failed (Provider as SocialApiProvider)
    If Msgbox2("Failed to actualize your details."&CRLF&CRLF&"Retry?", "Error", "Yes", "No", "", Null) = DialogResponse.POSITIVE Then
        Provider.Retry
    End If
End Sub


7. Create the login/logout handlers. The login and logout actions are performed asynchronously
B4X:
Sub BtnConnect_Click
    GooglePlus.Login
End Sub

Sub BtnDisconnect_Click
    GooglePlus.Logout
End Sub


8. Do something and evaluate the result
B4X:
Sub BtnGetPeopleVisible_Click
    GooglePlus.PleaseWaitText = "Getting people information"
    Dim Result As GooglePlusResult = GooglePlus.GetPeopleVisible
    Msgbox(Result, "Done")
End Sub

Sub BtnGetPeople_Click
    GooglePlus.PleaseWaitText = "Getting Erel's and Periklis' details"
    Dim Result As GooglePlusResult = GooglePlus.GetPeople(Array As String ("102781867331980802136", "+PeriklisKoutsogiannis"))
    Msgbox(Result, "Done")
End Sub

Done! Now we have to setup our app in our Google+ developer dashboard.

Setup our app in the Google+ developer dashboard (courtesy of androidhive.info)

1. Get your SHA1 fingerprint from B4A IDE

sha-jpg.26015


2. Open Google APIs console and on the left, under APIs & auth section, click on APIs and on the right enable Google+ API service.

google-console-api-google-plus.png


3. Now again on the left, click on Credentials and on the right, click on CREATE NEW CLIENT ID button. It will open a popup to configure a new client id.

google-console-creating-new-client-id.png


4. In the popup, select Installed application as Application type. Under Installed application type section select Android and give your project package name. This package name should be equal to your android project. For the sake of the example the package name is info.androidhive.gpluslogin. Enter your SHA1 fingerprint (from step 1) in Signing certificate fingerprint (SHA1) field and enable Deep Linking and click on Create Client ID button.

google-api-console-creating-new-client-id.png


5. Now you should see a new client created for your android application.

google-api-client-id.png


Done! You are ready to go! :D


Useful links

Please test and post any feedback, questions, comments you may have!
That's all for now folks!~


Version history until SocialApi 2.3

2.3
  • Restructured again into a single package (socialapi) after finding out how to properly use the #AdditionalRes directive to avoid conflict with other b4a libraries
2.2
  • Restructured due to a conflict with AdMod library and GooglePlusProvider has its own package
2.0
  • Now part of the SocialApi unified wrapper library
  • Major code refactoring
  • Renamed: GooglePlus -> GooglePlusProvider
  • Renamed: WrapperActivity -> SocialApiActivity
  • Renamed: WrapperActivity.Initialize -> SocialApiActivity.GetWithEvent
  • Added: Google +1 button custom view
1.2
  • Change: Complete code refactoring. Now using our SocialApiWrapper library that provides common functionality to our SDK wrappers
1.1
  • Added: GooglePlus.GetPeopleVisible
1.0
  • Initial version
 

Attachments

  • sample1-2.jpg
    sample1-2.jpg
    26.3 KB · Views: 604
  • sha.jpg
    sha.jpg
    91.5 KB · Views: 702
  • sample1-1.png
    sample1-1.png
    64.9 KB · Views: 400
  • sample1-3.png
    sample1-3.png
    82.3 KB · Views: 289
Last edited:

Periklis Koutsogiannis

Active Member
Licensed User
Longtime User
It seems that an existing and outdated google play services library is already included in your project.

You have to update to the latest version in the android sdk manager and remove the #AdditionalJar that includes my gplussdk.jar since you already include it through an other dependency. Keep the res folder and recompile.
 

Periklis Koutsogiannis

Active Member
Licensed User
Longtime User

Douglas Farias

Expert
Licensed User
Longtime User
strange my google play services is updated

here is my code

B4X:
#Region  Project Attributes
    #ApplicationLabel: Click Fight
    #VersionCode: 1
    #VersionName: 1.0
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
   
    'ANALYTICS
    #AdditionalRes: C:\Program Files (x86)\Android\android-sdk\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms
   
    'FACEBOOK
    #AdditionalRes: ..\AppConfig
    #AdditionalRes: C:\b4a-dev\fbsdk\res
    #AdditionalJar: C:\b4a-dev\fbsdk\bolts.jar
    #AdditionalJar: C:\b4a-dev\fbsdk\android-support-v4.jar
    #AdditionalJar: C:\b4a-dev\fbsdk\facebooksdk.jar
   
    'GOOGLE
    #AdditionalRes: C:\b4a-dev\gplussdk\res
    #AdditionalJar: C:\b4a-dev\gplussdk\gplussdk.jar

   
   
#End Region


i dont have adtional libraries on path
 

Periklis Koutsogiannis

Active Member
Licensed User
Longtime User
It appears that it is not.

This line:

B4X:
'ANALYTICS
#AdditionalRes: C:\Program Files (x86)\Android\android-sdk\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms


Overrides the version declared in gplussdk res folder ...
B4X:
'GOOGLE
#AdditionalRes: C:\b4a-dev\gplussdk\res
... which is the newest version number.

Remove the directive under the 'ANALYTICS line and recompile.

You google play services is outdated. If you are already using it you have to follow the steps in my previous post.
 

Douglas Farias

Expert
Licensed User
Longtime User
B4X:
#Region  Project Attributes

    #ApplicationLabel: Click Fight
    #VersionCode: 1
    #VersionName: 1.0
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
  
  
    'FACEBOOK
    #AdditionalRes: ..\AppConfig
    #AdditionalRes: C:\b4a-dev\fbsdk\res
    #AdditionalJar: C:\b4a-dev\fbsdk\bolts.jar
    #AdditionalJar: C:\b4a-dev\fbsdk\android-support-v4.jar
    #AdditionalJar: C:\b4a-dev\fbsdk\facebooksdk.jar
  
    'GOOGLE
    #AdditionalRes: C:\b4a-dev\gplussdk\res


  
  
#End Region

#Region  Activity Attributes
    #FullScreen: True
    #IncludeTitle: False
#End Region

Sub Process_Globals
Dim FB As Facebook
Dim GP As GooglePlus
End Sub

Sub Globals
    Private logoclickfight As ImageView
    Private loginfacebook As ImageView
    Private labelfacebook As Label
    Private logingoogle As ImageView
    Private logovallow As ImageView
    Private labelgoogle As Label
    Private splash As SplashFade
    Private CurrentActivity As WrapperActivity
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("login")
    splash.Initialize(Activity, Me, "splash.png", Gravity.FILL, 3000, 100 , "Left" , False)
End Sub

Sub Activity_Resume
    FB.SetActivity(CurrentActivity.Initialize("facebook_status_changed"))
    GP.SetActivity(CurrentActivity.Initialize("googleplus_statuschanged_event"))
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



Sub labelfacebook_Click
    FB.Login(Array As String ("email"))
End Sub

Sub labelgoogle_Click
    GP.Login
End Sub




Sub facebook_status_changed
  
    If FB.JustConnected Then
    Msgbox(FB.Me, "JustConnected!")
    End If

    If FB.JustDisconnected Then
        Msgbox("Bye bye!", "JustDisconnected!")
    End If
  
End Sub


Sub googleplus_statuschanged_event
    Log("googleplus_statuschanged_event")

    If GP.JustConnected Then
        Msgbox(GP.Me, "JustConnected!")
    End If
  
    If GP.JustDisconnected Then
        Msgbox("Bye bye!", "JustDisconnected!")
    End If
  
End Sub



(Intent) Intent { act=android.intent.action.MAIN flg=0x20000000 cmp=click.fight.vallow/.main }
no extras
java.lang.ClassNotFoundException: com.datasteam$b4a$googleplus$GooglePlus
at anywheresoftware.b4a.shell.Shell.getCorrectClassName(Shell.java:544)
at anywheresoftware.b4a.shell.Shell.createObject(Shell.java:516)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:320)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at click.fight.vallow.main.afterFirstLayout(main.java:91)
at click.fight.vallow.main.access$100(main.java:16)
at click.fight.vallow.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
(Intent) Intent { act=android.intent.action.MAIN flg=0x20000000 cmp=click.fight.vallow/.main }
no extras
java.lang.ClassNotFoundException: com.datasteam$b4a$googleplus$GooglePlus
at anywheresoftware.b4a.shell.Shell.getCorrectClassName(Shell.java:544)
at anywheresoftware.b4a.shell.Shell.createObject(Shell.java:516)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:320)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at click.fight.vallow.main.afterFirstLayout(main.java:91)
at click.fight.vallow.main.access$100(main.java:16)
at click.fight.vallow.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
(Intent) Intent { act=android.intent.action.MAIN flg=0x20000000 cmp=click.fight.vallow/.main }
no extras
java.lang.ClassNotFoundException: com.datasteam$b4a$googleplus$GooglePlus
at anywheresoftware.b4a.shell.Shell.getCorrectClassName(Shell.java:544)
at anywheresoftware.b4a.shell.Shell.createObject(Shell.java:516)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:320)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at click.fight.vallow.main.afterFirstLayout(main.java:91)
at click.fight.vallow.main.access$100(main.java:16)
at click.fight.vallow.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **



aditional libs on path = C:\lib
C:\lib contains your google play services . jar


my manifest
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="19"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.

AddApplicationText(
    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/app_id"/>
    <activity
        android:name="com.facebook.LoginActivity"
        android:theme="@android:style/Theme.Translucent.NoTitleBar"
        android:label="@string/app_name"/>)

AddApplicationText(
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />)


error on this line
GP.SetActivity(CurrentActivity.Initialize("googleplus_statuschanged_event"))
 

Periklis Koutsogiannis

Active Member
Licensed User
Longtime User
B4X:
java.lang.ClassNotFoundException: com.datasteam$b4a$googleplus$GooglePlus

Have you checked the GooglePlus lib in the IDE?
 

Periklis Koutsogiannis

Active Member
Licensed User
Longtime User
Try with that:

B4X:
    #Region Project Attributes

    #ApplicationLabel: Click Fight
    #VersionCode: 1
    #VersionName: 1.0
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
 
    'FACEBOOK 
    #AdditionalRes: ..\AppConfig
    #AdditionalRes: C:\b4a-dev\fbsdk\res
    #AdditionalJar: C:\b4a-dev\fbsdk\bolts.jar
    #AdditionalJar: C:\b4a-dev\fbsdk\android-support-v4.jar
    #AdditionalJar: C:\b4a-dev\fbsdk\facebooksdk.jar

    'GOOGLE 
    #AdditionalRes: C:\b4a-dev\gplussdk\res
    #AdditionalJar: C:\b4a-dev\gplussdk\gplussdk.jar
#End Region

AND DELETE the google_play_services.jar from the lib folder
 

Douglas Farias

Expert
Licensed User
Longtime User
Have you checked the GooglePlus lib in the IDE?
yes
i have added the
#AdditionalJar: C:\b4a-dev\gplussdk\gplussdk.jar
again and now dont have error when open the app

but when i click at login button show me
toastmessageshow internal error *-*
 

Douglas Farias

Expert
Licensed User
Longtime User
#Region Project Attributes

#ApplicationLabel: Click Fight
#VersionCode: 1
#VersionName: 1.0
#SupportedOrientations: portrait
#CanInstallToExternalStorage: False
'FACEBOOK #AdditionalRes: ..\AppConfig
#AdditionalRes: C:\b4a-dev\fbsdk\res
#AdditionalJar: C:\b4a-dev\fbsdk\bolts.jar
#AdditionalJar: C:\b4a-dev\fbsdk\android-support-v4.jar
#AdditionalJar: C:\b4a-dev\fbsdk\facebooksdk.jar
'GOOGLE #AdditionalRes: C:\b4a-dev\gplussdk\res
#AdditionalJar: C:\b4a-dev\gplussdk\gplussdk.jar#End Region
AND DELETE the google_play_services.jar from the lib folder

this works but when i press to login
toastmessageshow internal error *-*
 

Periklis Koutsogiannis

Active Member
Licensed User
Longtime User
Have you set up a new client ID in your Google+ Developer Dashboard?

You have to setup a new client ID for every application :D
 

Douglas Farias

Expert
Licensed User
Longtime User
its here

only strange is on my google + have 0% o_O
 

Attachments

  • 1.png
    1.png
    115.5 KB · Views: 147
  • 2.png
    2.png
    118.2 KB · Views: 149
Top