VoltAir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
AndroidActivity.h
1 /*
2  * Copyright (C) 2014 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ANDROIDACTIVITY_H
18 #define ANDROIDACTIVITY_H
19 
20 #include <QObject>
21 
22 #if defined(Q_OS_ANDROID)
23 
24 #include <GameInput/JoystickAxisCodes.h>
25 #include <GameInput/KeyCodes.h>
26 #include <GameInput/TriggerAxisCodes.h>
27 #include <QEvent>
28 #include <QMap>
29 #include <QSet>
30 #include <jni.h>
31 #include "SelfDetachingJNIEnv.h"
32 
33 class ControllerEvent;
34 class QKeyEvent;
35 
53 public:
59  static SelfDetachingJNIEnv getEnv(JavaVM* vm = nullptr);
63  static JavaVM* getJavaVM() { return sJavaVM; }
64 
68  static jobject getActivity() { return sActivity; }
77  static jclass getActivityClass(JNIEnv* env, jobject activity = nullptr);
78 
83  static bool isStarted() { return sActivity; }
84 
91  static void onCreate(JNIEnv* jni, jobject activity);
96  static void onStart();
101  static void onResume();
106  static void onPause();
111  static void onStop();
118  static void onDestroy(JNIEnv* jni, jobject activity);
119 
126  static void onControllerDisconnect(JNIEnv*, jobject, int deviceId);
135  static bool onGamepadMotionEvent(JNIEnv* jni, jobject, jobject motionEvent);
144  static bool onGamepadKeyEvent(JNIEnv* jni, jobject, jobject keyEvent);
153  static bool onTouchNavigationMotionEvent(JNIEnv* jni, jobject, jobject motionEvent);
162  static bool onTouchNavigationKeyEvent(JNIEnv* jni, jobject, jobject keyEvent);
171  static bool onKeyboardKeyEvent(JNIEnv* jni, jobject, jobject keyEvent);
178  static void onSignedIntoCloudChanged(JNIEnv* , jobject, bool signedIntoCloud);
187  static void onCloudDataLoaded(JNIEnv* jni, jobject, int statusCode, jstring javaData);
197  static jstring onCloudDataConflict(JNIEnv* jni, jobject, jstring local, jstring cloud);
198 
202  static bool isSignedIntoCloud();
206  static bool cloudSignInFailed();
210  static void signIntoCloud();
214  static void signOutOfCloud();
220  static bool ensureCloudSync();
225  static void saveToCloud(const QString& data);
229  static void clearCloudData();
235  static bool revealAchievement(const QString& name);
241  static bool unlockAchievement(const QString& name);
248  static bool incrementAchievement(const QString& name, int numSteps);
255  static bool setAchievementSteps(const QString& name, int minSteps);
259  static void showAchievementsRequested();
265  static void resetAchievementsRequested();
269  static QString getVersionName();
273  static long getMemoryUsed();
279  static int getTouchScreenDeviceId();
285  static void clearBGMTrack(int priority);
292  static void setBGMTrack(int priority, const QString& track);
297  static bool isBGMMuted();
302  static void setBGMMuted(bool value);
308  static void setBGMPaused(bool value);
314  static void setTrackerScreenName(const QString& screenName, bool sendScreenView = true);
320  static void sendTrackerEvent(const QString& category, const QString& action);
327  static void sendTrackerEvent(const QString& category, const QString& action,
328  const QString& label);
336  static void sendTrackerEvent(const QString& category, const QString& action,
337  const QString& label, long value);
344  static void sendTrackerEvent(const QString& category, const QString& action, long value);
345 
346 private:
347  static bool onKeyEvent(JNIEnv* jni, jobject keyEvent, ControllerEvent* controllerEvent);
348  static jclass getSoundManagerClass(JNIEnv* jni, jobject soundManager = nullptr);
349  static jclass getMotionEventClass(JNIEnv* jni, jobject motionEvent = nullptr);
350  static jclass getKeyEventClass(JNIEnv* jni, jobject keyEvent = nullptr);
351  static jclass getInputDeviceClass(JNIEnv* jni, jobject inputDevice = nullptr);
352  static jclass getMotionRangeClass(JNIEnv* jni, jobject motionRange = nullptr);
353  static jclass getDebugClass(JNIEnv* jni);
354 
355  static int getMotionEventDeviceId(JNIEnv* jni, jobject motionEvent);
356  static KeyCode getKeyCode(JNIEnv* jni, jobject keyEvent);
357  static int getKeyAction(JNIEnv* jni, jobject keyEvent);
358  static int getKeyEventDeviceId(JNIEnv* jni, jobject keyEvent);
359  static jobject getInputDevice(JNIEnv* jni, jobject motionEvent);
360  static jobject getMotionRange(JNIEnv* jni, jobject motionEvent, int axis);
365  static float getAxisValue(JNIEnv* jni, jobject motionEvent, int axis, float* flatOut = nullptr,
366  float* minOut = nullptr, float* rangeOut = nullptr);
370  static float getNormalizedJoystickAxisValue(JNIEnv* jni, jobject motionEvent, int axis);
375  static bool shouldConsumeTranslatedEvents();
376  static QKeyEvent* convertAndroidKeyEvent(int action, KeyCode keyCode);
377  static jobject getSoundManager();
378 
379  static QMap<int, JoystickAxisCode> sGamepadJoystickAxisConversionMap;
380  static QMap<int, TriggerAxisCode> sGamepadTriggerAxisConversionMap;
381  static QMap<int, JoystickAxisCode> sTouchNavigationJoystickAxisConversionMap;
382  static QMap<KeyCode, int> sKeycodeConversionMap;
383  static QMap<int, QEvent::Type> sActionConversionMap;
384  static QSet<KeyCode> sUnmappableKeycodes;
385 
386  static jobject sActivity;
387 
388  static jclass sActivityClass;
389  static jclass sSoundManagerClass;
390  static jclass sMotionEventClass;
391  static jclass sKeyEventClass;
392  static jclass sInputDeviceClass;
393  static jclass sMotionRangeClass;
394  static jclass sDebugClass;
395 
396  static JavaVM* sJavaVM;
397 };
398 
399 #endif // Q_OS_ANDROID
400 #endif // ANDROIDACTIVITY_H
static void onDestroy(JNIEnv *jni, jobject activity)
Android activity onDestroy lifecycle callback.
static void onStart()
Android activity onStart lifecycle callback.
static bool isStarted()
Returns whether or not the Android activity has been started previously, meaning all future JNI calls...
Definition: AndroidActivity.h:83
static void onControllerDisconnect(JNIEnv *, jobject, int deviceId)
Android activity onControllerDisconnect callback indicating that a device identified by deviceId has ...
static void onResume()
Android activity onResume lifecycle callback.
static bool isBGMMuted()
Returns whether or not the Android SoundManager's background music is muted.
static SelfDetachingJNIEnv getEnv(JavaVM *vm=nullptr)
Returns the current JNI environment.
static void onStop()
Android activity onStop lifecycle callback.
static void signIntoCloud()
Begins asynchronous cloud (i.e. GPGS) sign in.
static void setBGMTrack(int priority, const QString &track)
Sets the Android SoundManager's BGM track at the specified priority level to track.
static bool unlockAchievement(const QString &name)
Unlocks a GPGS achievement.
static void setTrackerScreenName(const QString &screenName, bool sendScreenView=true)
Sets the Google Analytics screen name and optionally sends a screen view hit.
static jclass getActivityClass(JNIEnv *env, jobject activity=nullptr)
Returns a cached global reference to the Android activity class.
static bool incrementAchievement(const QString &name, int numSteps)
Increments a GPGS incremental achievement.
static bool onTouchNavigationKeyEvent(JNIEnv *jni, jobject, jobject keyEvent)
Android activity onTouchNavigationKeyEvent callback for an Android KeyEvent that originated from a to...
static jstring onCloudDataConflict(JNIEnv *jni, jobject, jstring local, jstring cloud)
Android activity onCloudDataConflict callback indicating a conflict of cloud (i.e. GPGS) save data has been detected while loading.
static void sendTrackerEvent(const QString &category, const QString &action)
Sends an event hit to Google Analytics.
static void setBGMPaused(bool value)
Sets the Android SoundManager's paused state.
Smart wrapper around JNIEnv which retains information of how the environment was loaded.
Definition: SelfDetachingJNIEnv.h:33
static QString getVersionName()
Returns the version string of the Android application.
static void signOutOfCloud()
Signs out of cloud (i.e. GPGS).
static bool onTouchNavigationMotionEvent(JNIEnv *jni, jobject, jobject motionEvent)
Android activity onTouchNavigationMotionEvent callback for an Android MotionEvent that originated fro...
static bool cloudSignInFailed()
Returns true if the most recent cloud (i.e. GPGS) sign in attempt failed.
JNI interface for the main Android activity.
Definition: AndroidActivity.h:52
A platform independent event generated by controller device.
Definition: ControllerEvent.h:34
static void onCreate(JNIEnv *jni, jobject activity)
Android activity onCreate lifecycle callback.
static bool ensureCloudSync()
Starts a cloud (i.e. GPGS) save game data sync.
static void setBGMMuted(bool value)
Sets the Android SoundManager's muted state.
static JavaVM * getJavaVM()
Returns the currently cached reference to the JVM.
Definition: AndroidActivity.h:63
static void saveToCloud(const QString &data)
Saves p data to the cloud (i.e. GPGS).
static void onPause()
Android activity onPause lifecycle callback.
static void onSignedIntoCloudChanged(JNIEnv *, jobject, bool signedIntoCloud)
Android activity onSignedIntoCloudChanged callback indicating that cloud (i.e. GPGS) sign-in status h...
static int getTouchScreenDeviceId()
Returns the hardware deviceId of the touch screen input device, or -1 if none.
static long getMemoryUsed()
Returns the number of bytes allocated for the native heap.
static void clearBGMTrack(int priority)
Clears the Android SoundManager's BGM track at the specified priority level.
static bool setAchievementSteps(const QString &name, int minSteps)
Sets a GPGS incremental achievement to have a minimum number of steps.
static jobject getActivity()
Returns the currently cached reference to the Android activity.
Definition: AndroidActivity.h:68
KeyCode
Key codes used to uniquely identify hardware input keys.
Definition: KeyCodes.h:26
static bool onKeyboardKeyEvent(JNIEnv *jni, jobject, jobject keyEvent)
Android activity onKeyboardKeyEvent callback for an Android KeyEvent that originated from a keyboard ...
static void clearCloudData()
Resets any cloud (i.e. GPGS) save game data.
static void onCloudDataLoaded(JNIEnv *jni, jobject, int statusCode, jstring javaData)
Android activity onCloudDataLoaded callback when cloud (i.e. GPGS) save data has been loaded...
static bool onGamepadMotionEvent(JNIEnv *jni, jobject, jobject motionEvent)
Android activity onGamepadMotionEvent callback for an Android MotionEvent that originated from a game...
static void showAchievementsRequested()
Launch an intent to show the GPGS achievements activity screen.
static bool isSignedIntoCloud()
Returns true if currently signed into the cloud (i.e. GPGS).
static bool revealAchievement(const QString &name)
Reveals a GPGS achievement.
static void resetAchievementsRequested()
Resets all GPGS achievement progress for the currently signed in user account.
static bool onGamepadKeyEvent(JNIEnv *jni, jobject, jobject keyEvent)
Android activity onGamepadKeyEvent callback for an Android KeyEvent that originated from a gamepad de...