Skip to content

Commit 9f385cb

Browse files
committed
- Project structure updated
- Added Google SignIn
1 parent 91ddf31 commit 9f385cb

31 files changed

+486
-71
lines changed

.gitignore

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,18 @@ android/gen/
2020
out/
2121
com_crashlytics_export_strings.xml
2222

23-
## Gradle
23+
## Project specific
24+
core/build/
25+
android/google-services.json
26+
android/build/
27+
android/*.jks
2428

25-
.gradle
29+
## Gradle
2630
gradle-app.setting
2731
build/
28-
build*
32+
.gradle/
33+
local.properties
34+
2935

3036
## OS Specific
3137
.DS_Store

android/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="com.hmack.circlespawn"
3+
package="de.reimerm.libgdxkotlinexample"
44
android:versionCode="1"
55
android:versionName="1.0">
66

@@ -10,7 +10,7 @@
1010
android:label="@string/app_name"
1111
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
1212
<activity
13-
android:name="com.hmack.circlespawn.AndroidLauncher"
13+
android:name="de.reimerm.libgdxkotlinexample.AndroidLauncher"
1414
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
1515
android:label="@string/app_name"
1616
android:screenOrientation="landscape"

android/build.gradle

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
apply plugin: 'com.android.application'
2+
apply plugin: 'kotlin-android'
3+
apply plugin: 'kotlin-android-extensions'
4+
5+
android {
6+
7+
buildToolsVersion "24.0.2"
8+
compileSdkVersion 24
9+
10+
defaultConfig {
11+
applicationId "de.reimerm.libgdxkotlinexample"
12+
minSdkVersion 11
13+
targetSdkVersion 24
14+
// Enabling multidex support.
15+
multiDexEnabled true
16+
}
17+
18+
buildTypes {
19+
release {
20+
minifyEnabled false
21+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
22+
zipAlignEnabled true
23+
}
24+
}
25+
26+
sourceSets {
27+
main {
28+
manifest.srcFile 'AndroidManifest.xml'
29+
java.srcDirs = ['src']
30+
aidl.srcDirs = ['src']
31+
renderscript.srcDirs = ['src']
32+
res.srcDirs = ['res']
33+
assets.srcDirs = ['assets']
34+
jniLibs.srcDirs = ['libs']
35+
}
36+
37+
instrumentTest.setRoot('tests')
38+
}
39+
40+
// signingConfigs {
41+
// debug {
42+
// keyAlias ''
43+
// keyPassword ''
44+
// storeFile file('')
45+
// storePassword ''
46+
// }
47+
// }
48+
49+
dexOptions {
50+
javaMaxHeapSize "2g"
51+
}
52+
}
53+
54+
configurations { natives }
55+
56+
dependencies {
57+
sourceCompatibility = 1.7
58+
targetCompatibility = 1.7
59+
60+
compile project(":core")
61+
compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
62+
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
63+
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
64+
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a"
65+
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
66+
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64"
67+
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi"
68+
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a"
69+
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-arm64-v8a"
70+
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86"
71+
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86_64"
72+
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi"
73+
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
74+
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a"
75+
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
76+
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64"
77+
78+
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
79+
compile 'com.android.support:appcompat-v7:24.2.0'
80+
compile 'com.google.android.gms:play-services-auth:9.4.0'
81+
}
82+
83+
apply plugin: 'com.google.gms.google-services'
84+
85+
// called every time gradle gets executed, takes the native dependencies of
86+
// the natives configuration, and extracts them to the proper libs/ folders
87+
// so they get packed with the APK.
88+
task copyAndroidNatives() {
89+
file("libs/armeabi/").mkdirs();
90+
file("libs/armeabi-v7a/").mkdirs();
91+
file("libs/arm64-v8a/").mkdirs();
92+
file("libs/x86_64/").mkdirs();
93+
file("libs/x86/").mkdirs();
94+
95+
configurations.natives.files.each { jar ->
96+
def outputDir = null
97+
if (jar.name.endsWith("natives-arm64-v8a.jar")) outputDir = file("libs/arm64-v8a")
98+
if (jar.name.endsWith("natives-armeabi-v7a.jar")) outputDir = file("libs/armeabi-v7a")
99+
if (jar.name.endsWith("natives-armeabi.jar")) outputDir = file("libs/armeabi")
100+
if (jar.name.endsWith("natives-x86_64.jar")) outputDir = file("libs/x86_64")
101+
if (jar.name.endsWith("natives-x86.jar")) outputDir = file("libs/x86")
102+
if (outputDir != null) {
103+
copy {
104+
from zipTree(jar)
105+
into outputDir
106+
include "*.so"
107+
}
108+
}
109+
}
110+
}
111+
112+
task run(type: Exec) {
113+
def path
114+
def localProperties = project.file("../local.properties")
115+
if (localProperties.exists()) {
116+
Properties properties = new Properties()
117+
localProperties.withInputStream { instr ->
118+
properties.load(instr)
119+
}
120+
def sdkDir = properties.getProperty('sdk.dir')
121+
if (sdkDir) {
122+
path = sdkDir
123+
} else {
124+
path = "$System.env.ANDROID_HOME"
125+
}
126+
} else {
127+
path = "$System.env.ANDROID_HOME"
128+
}
129+
130+
def adb = path + "/platform-tools/adb"
131+
commandLine "$adb", 'shell', 'am', 'start', '-n', 'de.reimerm.libgdxkotlinexample/AndroidLauncher'
132+
}
133+
134+
// sets up the Android Idea project, using the old Ant based build.
135+
idea {
136+
module {
137+
sourceDirs += file("src");
138+
scopes = [COMPILE: [plus: [project.configurations.compile]]]
139+
140+
iml {
141+
withXml {
142+
def node = it.asNode()
143+
def builder = NodeBuilder.newInstance();
144+
builder.current = node;
145+
builder.component(name: "FacetManager") {
146+
facet(type: "android", name: "Android") {
147+
configuration {
148+
option(name: "UPDATE_PROPERTY_FILES", value: "true")
149+
}
150+
}
151+
}
152+
}
153+
}
154+
}
155+
}

android/ic_launcher-web.png

-50.2 KB
Binary file not shown.

android/res/values/strings.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
33

4-
<string name="app_name">circlespawn</string>
4+
<string name="app_name">LibGDX Kotlin Example</string>
5+
<string name="logout_succeed">Logout Succeeded</string>
6+
<string name="login_succeed">Login Succeeded</string>
7+
<string name="login_failed">Login Failed</string>
58

69
</resources>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package de.reimerm.libgdxkotlinexample
2+
3+
import de.reimerm.libgdxkotlinexample.utils.GameEventListener
4+
5+
/**
6+
* Created by Marius Reimer on 20-Sep-16.
7+
*/
8+
class AndroidGameEventListener : GameEventListener {
9+
override fun login() {
10+
LoginHandler.getInstance().login()
11+
}
12+
13+
override fun logout() {
14+
LoginHandler.getInstance().logout()
15+
}
16+
17+
override fun isLoggedIn(): Boolean {
18+
return LoginHandler.getInstance().isConnected
19+
}
20+
}

android/src/com/hmack/circlespawn/AndroidLauncher.kt renamed to android/src/de/reimerm/libgdxkotlinexample/AndroidLauncher.kt

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.hmack.circlespawn
17+
package de.reimerm.libgdxkotlinexample
1818

19+
import android.content.Intent
1920
import android.os.Bundle
2021
import android.view.WindowManager
22+
import android.widget.Toast
2123
import com.badlogic.gdx.backends.android.AndroidApplication
2224
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration
23-
import com.hmack.circlespawn.main.MainGame
25+
import com.google.android.gms.auth.api.Auth
26+
import com.google.android.gms.auth.api.signin.GoogleSignInResult
27+
import de.reimerm.libgdxkotlinexample.main.MainGame
28+
import de.reimerm.libgdxkotlinexample.utils.GameManager
2429

2530
class AndroidLauncher : AndroidApplication() {
2631

@@ -30,8 +35,28 @@ class AndroidLauncher : AndroidApplication() {
3035
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
3136

3237
initialize(MainGame, AndroidApplicationConfiguration())
38+
39+
LoginHandler.getInstance().setContext(context)
40+
LoginHandler.getInstance().startApiClient()
41+
GameManager.listener = AndroidGameEventListener()
3342
}
3443

3544
override fun onBackPressed() {
3645
}
46+
47+
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
48+
if (requestCode.equals(LoginHandler.RC_SIGN_IN)) {
49+
val result = Auth.GoogleSignInApi.getSignInResultFromIntent(data)
50+
handleSignInResult(result)
51+
}
52+
}
53+
54+
private fun handleSignInResult(result: GoogleSignInResult) {
55+
if (result.isSuccess) {
56+
// Signed in successfully, connect
57+
val acct = result.signInAccount
58+
Toast.makeText(context, "Hello, " + acct?.displayName, Toast.LENGTH_SHORT).show()
59+
LoginHandler.getInstance().connect();
60+
}
61+
}
3762
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package de.reimerm.libgdxkotlinexample;
2+
3+
import android.content.Context;
4+
import android.content.Intent;
5+
import android.os.Bundle;
6+
import android.support.annotation.NonNull;
7+
import android.util.Log;
8+
import android.widget.Toast;
9+
10+
import com.google.android.gms.auth.api.Auth;
11+
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
12+
import com.google.android.gms.common.ConnectionResult;
13+
import com.google.android.gms.common.api.GoogleApiClient;
14+
import com.google.android.gms.common.api.ResultCallback;
15+
import com.google.android.gms.common.api.Status;
16+
17+
import de.reimerm.bubblefling.R;
18+
19+
/**
20+
* Created by Marius Reimer on 19-Sep-16.
21+
*/
22+
public class LoginHandler {
23+
24+
private static final LoginHandler INSTANCE = new LoginHandler();
25+
private static final String TAG = "LoginHandler";
26+
public static int RC_SIGN_IN = 9001; // Request code to use when launching the resolution activity
27+
private GoogleApiClient mGoogleApiClient;
28+
private Context context;
29+
30+
private LoginHandler() {
31+
}
32+
33+
public static LoginHandler getInstance() {
34+
return INSTANCE;
35+
}
36+
37+
public void setContext(Context context) {
38+
this.context = context;
39+
}
40+
41+
public void startApiClient() {
42+
if (mGoogleApiClient == null) {
43+
44+
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
45+
.requestEmail()
46+
.build();
47+
48+
PlayServiceListener playServiceListener = new PlayServiceListener();
49+
mGoogleApiClient = new GoogleApiClient.Builder(context)
50+
.addConnectionCallbacks(playServiceListener)
51+
.addOnConnectionFailedListener(playServiceListener)
52+
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
53+
.build();
54+
}
55+
}
56+
57+
public void login() {
58+
if (!isConnected()) {
59+
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
60+
61+
if (context instanceof AndroidLauncher) {
62+
((AndroidLauncher) context).startActivityForResult(signInIntent, RC_SIGN_IN);
63+
}
64+
}
65+
}
66+
67+
public void logout() {
68+
if (isConnected()) {
69+
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
70+
new ResultCallback<Status>() {
71+
@Override
72+
public void onResult(@NonNull Status status) {
73+
if (status.isSuccess()) {
74+
mGoogleApiClient.disconnect();
75+
Toast.makeText(context, R.string.logout_succeed, Toast.LENGTH_SHORT).show();
76+
}
77+
}
78+
});
79+
}
80+
}
81+
82+
public boolean isConnected() {
83+
return mGoogleApiClient != null && mGoogleApiClient.isConnected();
84+
}
85+
86+
public void connect() {
87+
if (!isConnected()) {
88+
mGoogleApiClient.connect();
89+
}
90+
}
91+
92+
private class PlayServiceListener implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
93+
@Override
94+
public void onConnected(Bundle bundle) {
95+
Log.d(TAG, "onConnected");
96+
}
97+
98+
@Override
99+
public void onConnectionSuspended(int i) {
100+
Log.d(TAG, "onConnectionSuspended");
101+
// Attempt to reconnect
102+
mGoogleApiClient.connect();
103+
}
104+
105+
@Override
106+
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
107+
Log.d(TAG, "onConnectionFailed" + connectionResult);
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)