-
-
Save shehabic/d74ae813d285f97ef19a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends FragmentActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener | |
{ | |
private WarpListener listener; | |
WarpClient warpClient; | |
int sessionId = 0; | |
boolean listenersAdded = false; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
listenersAdded = false; | |
Log.d("Main Activity ", "On Create"); | |
initWarpRelated(); | |
try { | |
exposeWarpClient(); // the one that does the reflection to see what we have currently. | |
} catch (Exception e) { | |
e.printStackTrace(); | |
Log.e("CRITICAL", e.getMessage()); | |
} | |
} | |
private void initWarpRelated() | |
{ | |
listener = new WarpListener(new Logger()); | |
initWarpClient(); | |
initUser(); | |
} | |
protected void initWarpClient() | |
{ | |
Log.d("Warp", "Initializing client"); | |
warpClient = WarpFactory.createWarpClient(getString(R.string.warp_api_key), getString(R.string.warp_secret)); | |
} | |
protected void initUser() | |
{ | |
Log.d("User", "Initialising user"); | |
OAuthToken token = null != myUser ? myUser.oAuthToken : new OAuthToken(); | |
GsonParser parser = new GsonParser(); | |
GameApi gameApi = ApiFactory.createGameApi(this, token); | |
try { | |
myUser = configManager.getUser(); | |
if (null == myUser) { | |
userStatus = status.NO_USER; | |
} else { | |
userStatus = status.VALID_USER; | |
if (warpClient.getConnectionState() > 1) { | |
if (!listenersAdded) { | |
try { | |
removeAllListeners(); // this is an extra thing here. | |
} catch (Exception e) { | |
Log.d("Warp", "Failed removing listeners"); | |
Crashlytics.logException(e); | |
e.printStackTrace(); | |
} | |
Log.d("Warp", "Adding listeners"); | |
warpClient.addRoomRequestListener(listener); | |
warpClient.addConnectionRequestListener(listener); | |
warpClient.addNotificationListener(listener); | |
warpClient.addZoneRequestListener(listener); | |
listenersAdded = true; | |
} | |
connectClient(); | |
} | |
} | |
} catch (SocialTokenExpiredException e) { | |
Crashlytics.logException(e); | |
userStatus = status.SOCIAL_TOKEN_EXPIRED; | |
} catch (UserExpiredException x) { | |
Crashlytics.logException(x); | |
userStatus = status.USER_EXPIRED; | |
} | |
gameManager = new GameManager( | |
this, | |
new WarpMessenger(warpClient, parser), | |
myUser, | |
new RoomManager(warpClient), | |
gameApi, | |
listener, | |
new Logger() | |
); | |
} | |
public void connectClient() | |
{ | |
warpClient.connectWithUserName(myUser.email); | |
} | |
protected void removeAllListeners() | |
{ | |
warpClient.removeRoomRequestListener(listener); | |
warpClient.removeConnectionRequestListener(listener); | |
warpClient.removeNotificationListener(listener); | |
warpClient.removeZoneRequestListener(listener); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment