Browse Source

Add Android support to hasBackgroundMusic

Tiago Koji Castro Shibata 8 years ago
parent
commit
4e9212b152

+ 16 - 0
src/common/android.cpp

@@ -225,6 +225,22 @@ bool createStorageDirectories()
 	return true;
 }
 
+bool hasBackgroundMusic()
+{
+	JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv();
+	jobject activity = (jobject) SDL_AndroidGetActivity();
+
+	jclass clazz(env->GetObjectClass(activity));
+	jmethodID method_id = env->GetMethodID(clazz, "hasBackgroundMusic", "()Z");
+
+	jboolean result = env->CallBooleanMethod(activity, method_id);
+
+	env->DeleteLocalRef(activity);
+	env->DeleteLocalRef(clazz);
+
+	return result;
+}
+
 } // android
 } // love
 

+ 2 - 0
src/common/android.h

@@ -66,6 +66,8 @@ bool mkdir(const char *path);
 
 bool createStorageDirectories();
 
+bool hasBackgroundMusic();
+
 } // android
 } // love
 

+ 0 - 11
src/modules/audio/wrap_Audio.cpp

@@ -307,16 +307,6 @@ int w_setMixMode(lua_State *)
 	return 0;
 }
 
-int w_getShouldBeSilenced(lua_State *L)
-{
-#ifdef LOVE_IOS
-	lua_pushboolean(L, love::ios::audioShouldBeSilenced());
-#else
-	lua_pushboolean(L, 0);
-#endif
-	return 1;
-}
-
 // List of functions to wrap.
 static const luaL_Reg functions[] =
 {
@@ -343,7 +333,6 @@ static const luaL_Reg functions[] =
 	{ "setDistanceModel", w_setDistanceModel },
 	{ "getDistanceModel", w_getDistanceModel },
 	{ "setMixMode", w_setMixMode },
-	{ "getShouldBeSilenced", w_getShouldBeSilenced },
 	{ 0, 0 }
 };
 

+ 11 - 0
src/modules/system/System.cpp

@@ -184,6 +184,17 @@ void System::vibrate(double seconds) const
 #endif
 }
 
+bool System::hasBackgroundMusic() const
+{
+#if defined(LOVE_ANDROID)
+	return love::android::hasBackgroundMusic();
+#elif defined(LOVE_IOS)
+	return love::ios::audioShouldBeSilenced();
+#else
+	throw love::Exception("Unsupported platform.");
+#endif
+}
+
 bool System::getConstant(const char *in, System::PowerState &out)
 {
 	return powerStates.find(in, out);

+ 8 - 0
src/modules/system/System.h

@@ -106,6 +106,14 @@ public:
 	 */
 	virtual void vibrate(double seconds) const;
 
+	/**
+	 * Gets if the user is playing music on background.
+	 * Throws an exception on unsupported platforms.
+	 *
+	 * @return Whether a music is playing on background.
+	 **/
+	bool hasBackgroundMusic() const;
+
 	static bool getConstant(const char *in, PowerState &out);
 	static bool getConstant(PowerState in, const char *&out);
 

+ 14 - 0
src/modules/system/wrap_System.cpp

@@ -93,6 +93,19 @@ int w_vibrate(lua_State *L)
 	return 0;
 }
 
+int w_hasBackgroundMusic(lua_State *L)
+{
+	try
+	{
+		lua_pushboolean(L, instance()->hasBackgroundMusic());
+	}
+	catch (love::Exception &)
+	{
+		lua_pushnil(L);
+	}
+	return 1;
+}
+
 static const luaL_Reg functions[] =
 {
 	{ "getOS", w_getOS },
@@ -102,6 +115,7 @@ static const luaL_Reg functions[] =
 	{ "getPowerInfo", w_getPowerInfo },
 	{ "openURL", w_openURL },
 	{ "vibrate", w_vibrate },
+	{ "hasBackgroundMusic", w_hasBackgroundMusic },
 	{ 0, 0 }
 };