bool ofxAndroidAlertListBox(string title, const vector<string> & list){
	jclass javaClass = ofGetJavaOFAndroid();

	if(javaClass==0){
		ofLogError("ofxAndroidUtils") << "ofxAndroidAlertListBox(): couldn't find OFAndroid java class";
		return "";
	}

	jmethodID alertListBox = ofGetJNIEnv()->GetStaticMethodID(javaClass,"alertListBox","(Ljava/lang/String;[Ljava/lang/String;)Z");
	if(!alertListBox){
		ofLogError("ofxAndroidUtils") << "ofxAndroidAlertListBox(): couldn't find OFAndroid alertListBox method";
		return "";
	}
	jstring jTitle = ofGetJNIEnv()->NewStringUTF(title.c_str());

	jclass jStringClass = ofGetJNIEnv()->FindClass("java/lang/String");
	jobjectArray jList = ofGetJNIEnv()->NewObjectArray(list.size(), jStringClass, NULL);
	for(int i=0;i<(int)list.size(); i++){
		jstring element = ofGetJNIEnv()->NewStringUTF(list[i].c_str());
		ofGetJNIEnv()->SetObjectArrayElement(jList,i,(jobject)element);
		ofGetJNIEnv()->DeleteLocalRef((jobject)element);
	}

	jboolean res = ofGetJNIEnv()->CallStaticBooleanMethod(javaClass,alertListBox,jTitle,jList);
	ofGetJNIEnv()->DeleteLocalRef((jobject)jTitle);
	ofGetJNIEnv()->DeleteLocalRef((jobject)jList);
	return res;
}
int ofxAndroidSoundStream::getMinInBufferSize(int samplerate, int nchannels){
	jclass javaClass = ofGetJNIEnv()->FindClass("cc/openframeworks/OFAndroidSoundStream");

	if(javaClass==0){
		ofLogError("ofxAndroidSoundStream") << "getMinInBufferSize(): couldn't find OFAndroidSoundStream java class";
		return false;
	}

	jmethodID getMinBuffSize = ofGetJNIEnv()->GetStaticMethodID(javaClass,"getMinInBufferSize","(II)I");
	if(!getMinBuffSize){
		ofLogError("ofxAndroidSoundStream") << "getMinInBufferSize(): couldn't find getMinInBufferSize method";
		return false;
	}
	return ofGetJNIEnv()->CallStaticIntMethod(javaClass,getMinBuffSize,samplerate,nchannels);
}
Example #3
0
void ofxAndroidGPS::startGPS(){
	jclass OFAndroid = ofGetJavaOFAndroid();

	if(OFAndroid==0){
		ofLog(OF_LOG_ERROR,"cannot find OFAndroid java class");
		return;
	}

	jmethodID setupGPS = ofGetJNIEnv()->GetStaticMethodID(OFAndroid,"setupGPS","()V");
	if(!setupGPS){
		ofLog(OF_LOG_ERROR,"cannot find OFAndroid.setupGPS method");
		return;
	}
	ofGetJNIEnv()->CallStaticVoidMethod(OFAndroid,setupGPS);
}
void ofAppAndroidWindow::setFullscreen(bool fullscreen){
	jclass javaClass = ofGetJNIEnv()->FindClass("cc.openframeworks.OFAndroid");

	if(javaClass==0){
		ofLog(OF_LOG_ERROR,"setFullscreen: cannot find OFAndroid java class");
		return;
	}

	jmethodID setFullscreen = ofGetJNIEnv()->GetStaticMethodID(javaClass,"setFullscreen","(Z)V");
	if(!setFullscreen){
		ofLog(OF_LOG_ERROR,"cannot find OFAndroid setFullscreen method");
		return;
	}
	ofGetJNIEnv()->CallStaticObjectMethod(javaClass,setFullscreen,fullscreen);
}
void ofAppAndroidWindow::setFullscreen(bool fullscreen){
	jclass javaClass = ofGetJNIEnv()->FindClass("cc/openframeworks/OFAndroid");

	if(javaClass==0){
		ofLogError("ofAppAndroidWindow") << "setFullscreen(): couldn't find OFAndroid java class";
		return;
	}

	jmethodID setFullscreen = ofGetJNIEnv()->GetStaticMethodID(javaClass,"setFullscreen","(Z)V");
	if(!setFullscreen){
		ofLogError("ofAppAndroidWindow") << "setFullScreen(): couldn't find OFAndroid setFullscreen method";
		return;
	}
	ofGetJNIEnv()->CallStaticVoidMethod(javaClass,setFullscreen,fullscreen);
}
void ofxAndroidDismissProgressBox(int id){
	jclass javaClass = ofGetJavaOFAndroid();

	if(javaClass==0){
		ofLogError("ofxAndroidUtils") << "ofxAndroidDismissProgressBox(): couldn't find OFAndroid java class";
		return;
	}

	jmethodID dismissProgressBox = ofGetJNIEnv()->GetStaticMethodID(javaClass,"dismissProgressBox","(I)V");
	if(!dismissProgressBox){
		ofLogError("ofxAndroidUtils") << "ofxAndroidDismissProgressBox(): couldn't find OFAndroid dismissProgressBox method";
		return;
	}
	ofGetJNIEnv()->CallStaticVoidMethod(javaClass,dismissProgressBox,id);
}
void ofxAndroidMonitorNetworkState(){
	jclass javaClass = ofGetJavaOFAndroid();

	if(javaClass==0){
		ofLogError("ofxAndroidUtils") << "ofxAndroidMonitorNetworkState(): couldn't find OFAndroid java class";
		return;
	}

	jmethodID method = ofGetJNIEnv()->GetStaticMethodID(javaClass,"monitorNetworkState","()V");
	if(!method){
		ofLogError("ofxAndroidUtils") << "ofxAndroidMonitorNetworkState(): couldn't find OFAndroid monitorNetworkState method";
		return;
	}
	ofGetJNIEnv()->CallStaticVoidMethod(javaClass,method);
}
bool ofxAndroidIsMobileOnline(){
	jclass javaClass = ofGetJavaOFAndroid();

	if(javaClass==0){
		ofLogError("ofxAndroidUtils") << "ofxAndroidIsMobileOnline(): couldn't find OFAndroid java class";
		return false;
	}

	jmethodID isMobileOnline = ofGetJNIEnv()->GetStaticMethodID(javaClass,"isMobileOnline","()Z");
	if(!isMobileOnline){
		ofLogError("ofxAndroidUtils") << "ofxAndroidIsMobileOnline(): couldn't find OFAndroid isMobileOnline method";
		return false;
	}
	return ofGetJNIEnv()->CallStaticBooleanMethod(javaClass,isMobileOnline);
}
void ofxAndroidSetViewItemChecked(string item_name, bool checked){
	jclass javaClass = ofGetJavaOFAndroid();

	if(javaClass==0){
		ofLog(OF_LOG_ERROR,"ofxAndroidSetViewItemChecked: cannot find OFAndroid java class");
		return;
	}

	jmethodID setViewItemChecked = ofGetJNIEnv()->GetStaticMethodID(javaClass,"setViewItemChecked","(Ljava/lang/String;Z)V");
	if(!setViewItemChecked){
		ofLog(OF_LOG_ERROR,"cannot find OFAndroid setViewItemChecked method");
		return;
	}
	ofGetJNIEnv()->CallStaticVoidMethod(javaClass,setViewItemChecked,ofGetJNIEnv()->NewStringUTF(item_name.c_str()),checked);
}
void ofxAndroidPauseApp(){
	jclass javaClass = ofGetJavaOFAndroid();

	if(javaClass==0){
		ofLogError("ofxAndroidUtils") << "ofxAndroidPauseApp(): couldn't find OFAndroid java class";
		return;
	}

	jmethodID pauseApp = ofGetJNIEnv()->GetStaticMethodID(javaClass,"pauseApp","()V");
	if(!pauseApp){
		ofLogError("ofxAndroidUtils") << "ofxAndroidPauseApp(): couldn't find OFAndroid pauseApp method";
		return;
	}
	ofGetJNIEnv()->CallStaticVoidMethod(javaClass,pauseApp);
}
void ofxAndroidUnlockScreenSleep(){
	jclass javaClass = ofGetJavaOFAndroid();

	if(javaClass==0){
		ofLogError("ofxAndroidUtils") << "ofxAndroidUnlockScreenSleep(): couldn't find OFAndroid java class";
		return;
	}

	jmethodID unlockScreenSleep = ofGetJNIEnv()->GetStaticMethodID(javaClass,"unlockScreenSleep","()V");
	if(!unlockScreenSleep){
		ofLogError("ofxAndroidUtils") << "ofxAndroidUnlockScreenSleep(): couldn't find OFAndroid unlockScreenSleep method";
		return;
	}
	ofGetJNIEnv()->CallStaticVoidMethod(javaClass,unlockScreenSleep);
}
//------------------------------------------------------------
ofxAndroidSoundPlayer::ofxAndroidSoundPlayer(){
	JNIEnv *env = ofGetJNIEnv();
	if (!env) {
		ofLogError("ofxAndroidSoundPlayer") << "constructor: failed to get environment using GetEnv()";
		return;
	}
	jclass localClass = env->FindClass("cc/openframeworks/OFAndroidSoundPlayer");
	javaClass = (jclass) env->NewGlobalRef(localClass);

	if(!javaClass){
		ofLogError("ofxAndroidSoundPlayer") << "constructor: couldn't get java class for SoundPlayer";
		return;
	}


	jmethodID constructor = env->GetMethodID(javaClass,"<init>","()V");
	if(!constructor){
		ofLogError("ofxAndroidSoundPlayer") << "constructor: couldn't get java constructor for SoundPlayer";
		return;
	}

	javaSoundPlayer = env->NewObject(javaClass,constructor);
	if(!javaSoundPlayer){
		ofLogError("ofxAndroidSoundPlayer") << "constructor: couldn't create java SoundPlayer";
		return;
	}

	javaSoundPlayer = (jobject)env->NewGlobalRef(javaSoundPlayer);
}
void ofxAndroidSoundStream::close(){
	pause();

	// JNI: Try to find and call OFAndroidSoundStream.getInstance().stop()
	if(!ofGetJavaVMPtr()){
		ofLogError("ofxAndroidSoundStream") << "close(): couldn't find java virtual machine";
		return;
	}
	JNIEnv *env = ofGetJNIEnv();
	if (!env) {
		ofLogError("ofxAndroidSoundStream") << "close(): couldn't get environment using GetEnv()";
		return;
	}
	jclass javaClass = env->FindClass("cc/openframeworks/OFAndroidSoundStream");

	if(javaClass==0){
		ofLogError("ofxAndroidSoundStream") << "close(): couldn't find OFAndroidSoundStream java class";
		return;
	}

	jmethodID soundStreamSingleton = env->GetStaticMethodID(javaClass,"getInstance","()Lcc/openframeworks/OFAndroidSoundStream;");
	if(!soundStreamSingleton){
		ofLogError("ofxAndroidSoundStream") << "close(): couldn't find OFAndroidSoundStream singleton method";
		return;
	}
	jobject javaObject = env->CallStaticObjectMethod(javaClass,soundStreamSingleton);
	jmethodID javaStop = env->GetMethodID(javaClass,"stop","()V");
	// call stop()
	if(javaObject && javaStop)
		env->CallVoidMethod(javaObject,javaStop);
	else
		ofLogError("ofxAndroidSoundStream") << "close(): couldn't get OFAndroidSoundStream instance or stop method";
}
void ofxAndroidDisableMulticast(){

	jclass javaClass = ofGetJavaOFAndroid();

	if(javaClass==0){
		ofLogError("ofxAndroidUtils") << "ofxAndroidDisableMulticast(): couldn't find OFAndroid java class";
		return;
	}

	jmethodID method = ofGetJNIEnv()->GetStaticMethodID(javaClass,"disableMulticast","()V");
	if(!method){
		ofLogError("ofxAndroidUtils") << "ofxAndroidDisableMulticast(): couldn't find OFAndroid disableMulticast method";
		return;
	}
	ofGetJNIEnv()->CallStaticVoidMethod(javaClass,method);
}
bool ofxAndroidCheckSDCardMounted(){
	jclass javaClass = ofGetJavaOFAndroid();

	if(javaClass==0){
		ofLogError("ofxAndroidUtils") << "ofxAndroidCheckSDCardMounted(): couldn't find OFAndroid java class";
		return false;
	}

	jmethodID unlockScreenSleep = ofGetJNIEnv()->GetStaticMethodID(javaClass,"checkSDCardMounted","()Z");
	if(!unlockScreenSleep){
		ofLogError("ofxAndroidUtils") << "ofxAndroidCheckSDCardMounted(): couldn't find OFAndroid checkSDCardMounted method";
		return false;
	}
	return ofGetJNIEnv()->CallStaticBooleanMethod(javaClass,unlockScreenSleep);

}
void ofxAndroidLaunchBrowser(string url){
	jclass javaClass = ofGetJavaOFAndroid();

	if(javaClass==0){
		ofLogError("ofxAndroidUtils") << "ofxAndroidLaunchBrowser(): couldn't find OFAndroid java class";
		return;
	}

	jmethodID method = ofGetJNIEnv()->GetStaticMethodID(javaClass,"launchBrowser","(Ljava/lang/String;)V");
	if(!method){
		ofLogError("ofxAndroidUtils") << "ofxAndroidLaunchBrowser(): couldn't find OFAndroid launchBrowser method";
		return;
	}
	jstring jUrl = ofGetJNIEnv()->NewStringUTF(url.c_str());
	ofGetJNIEnv()->CallStaticVoidMethod(javaClass,method,jUrl);
	ofGetJNIEnv()->DeleteLocalRef((jobject)jUrl);
}
void ofxAndroidToast(string msg){
	jclass javaClass = ofGetJavaOFAndroid();

	if(javaClass==0){
		ofLogError("ofxAndroidUtils") << "ofxAndroidToast(): couldn't find OFAndroid java class";
		return;
	}

	jmethodID toast = ofGetJNIEnv()->GetStaticMethodID(javaClass,"toast","(Ljava/lang/String;)V");
	if(!toast){
		ofLogError("ofxAndroidUtils") << "ofxAndroidToast(): couldn't find OFAndroid toast method";
		return;
	}
	jstring jMsg = ofGetJNIEnv()->NewStringUTF(msg.c_str());
	ofGetJNIEnv()->CallStaticVoidMethod(javaClass,toast,jMsg);
	ofGetJNIEnv()->DeleteLocalRef((jobject)jMsg);
}
void ofxAndroidYesNoBox(string msg){
	jclass javaClass = ofGetJavaOFAndroid();

	if(javaClass==0){
		ofLogError("ofxAndroidUtils") << "ofxAndroidYesNoBox(): couldn't find OFAndroid java class";
		return;
	}

	jmethodID method = ofGetJNIEnv()->GetStaticMethodID(javaClass,"yesNoBox","(Ljava/lang/String;)V");
	if(!method){
		ofLogError("ofxAndroidUtils") << "ofxAndroidYesNoBox(): couldn't find OFAndroid okCancelBox method";
		return;
	}
	jstring jMsg = ofGetJNIEnv()->NewStringUTF(msg.c_str());
	ofGetJNIEnv()->CallStaticVoidMethod(javaClass,method,jMsg);
	ofGetJNIEnv()->DeleteLocalRef((jobject)jMsg);
}
void ofxJavaCallVoidMethod(jobject object, jclass classID, std::string methodName, std::string methodSignature, va_list args){
	jmethodID methodID = ofxJavaGetMethodID(classID, methodName, methodSignature);

	if (!methodID)
		return;

	ofGetJNIEnv()->CallVoidMethodV(object, methodID, args);
}
void ofAppAndroidWindow::setOrientation(ofOrientation _orientation){
	//if(orientation==_orientation) return;
	orientation = _orientation;
	jclass javaClass = ofGetJNIEnv()->FindClass("cc.openframeworks.OFAndroid");

	if(javaClass==0){
		ofLog(OF_LOG_ERROR,"setOrientation: cannot find OFAndroid java class");
		return;
	}

	jmethodID setScreenOrientation = ofGetJNIEnv()->GetStaticMethodID(javaClass,"setScreenOrientation","(I)V");
	if(!setScreenOrientation){
		ofLog(OF_LOG_ERROR,"cannot find OFAndroid setScreenOrientation method");
		return;
	}
	ofGetJNIEnv()->CallStaticObjectMethod(javaClass,setScreenOrientation,orientation);
}
int ofxAndroidProgressBox(string msg){
	jclass javaClass = ofGetJavaOFAndroid();

	if(javaClass==0){
		ofLogError("ofxAndroidUtils") << "ofxAndroidProgressBox(): couldn't find OFAndroid java class";
		return -1;
	}

	jmethodID progressBox = ofGetJNIEnv()->GetStaticMethodID(javaClass,"progressBox","(Ljava/lang/String;)I");
	if(!progressBox){
		ofLogError("ofxAndroidUtils") << "ofxAndroidProgressBox(): couldn't find OFAndroid alertBox method";
		return -1;
	}
	jstring jMsg = ofGetJNIEnv()->NewStringUTF(msg.c_str());
	int ret = ofGetJNIEnv()->CallStaticIntMethod(javaClass,progressBox,jMsg);
	ofGetJNIEnv()->DeleteLocalRef((jobject)jMsg);
	return ret;
}
//------------------------------------------------------------
bool ofxAndroidSoundPlayer::loadSound(string fileName, bool stream){
	if(!javaSoundPlayer){
		ofLogError("ofxAndroidSoundPlayer") << "loadSound(): java SoundPlayer not loaded";
		return false;
	}

	JNIEnv *env = ofGetJNIEnv();
	jmethodID javaLoadMethod = env->GetMethodID(javaClass,"loadSound","(Ljava/lang/String;Z)V");
	if(!javaLoadMethod){
		ofLogError("ofxAndroidSoundPlayer") << "loadSound(): couldn't get java loadSound for SoundPlayer";
		return false;
	}

	jstring javaFileName = ofGetJNIEnv()->NewStringUTF(ofToDataPath(fileName,true).c_str());
	env->CallVoidMethod(javaSoundPlayer,javaLoadMethod,javaFileName,stream?1:0);
	env->DeleteLocalRef((jobject)javaFileName);
	return true;
}
//------------------------------------------------------------
bool ofxAndroidSoundPlayer::loadSound(string fileName, bool stream){
	if(!javaSoundPlayer){
		ofLogError() << "cannot load sound, java soundPlayer object not created";
		return false;
	}

	JNIEnv *env = ofGetJNIEnv();
	jmethodID javaLoadMethod = env->GetMethodID(javaClass,"loadSound","(Ljava/lang/String;Z)V");
	if(!javaLoadMethod){
		ofLog(OF_LOG_ERROR,"Failed to get the java loadSound for SoundPlayer");
		return false;
	}

	jstring javaFileName = ofGetJNIEnv()->NewStringUTF(ofToDataPath(fileName,true).c_str());
	env->CallVoidMethod(javaSoundPlayer,javaLoadMethod,javaFileName,stream?1:0);
	env->DeleteLocalRef((jobject)javaFileName);
	return true;
}
jobject ofxJavaGetStaticObjectField(jclass classID, std::string fieldName, std::string fieldType) {

	jfieldID fieldID = ofxJavaGetStaticFieldID(classID, fieldName, fieldType);

	if (!fieldID)
		return NULL;

	return ofGetJNIEnv()->GetStaticObjectField(classID, fieldID);
}
jobject ofxJavaCallStaticObjectMethod(jclass classID, std::string methodName, std::string methodSignature, va_list args)
{
	jmethodID methodID = ofxJavaGetStaticMethodID(classID, methodName, methodSignature);

	if (!methodID)
		return NULL;

	return ofGetJNIEnv()->CallStaticObjectMethodV(classID, methodID, args);
}
string ofxAndroidRandomUUID(){
	jclass javaClass = ofGetJavaOFAndroid();

	if(javaClass==0){
		ofLogError("ofxAndroidUtils") << "ofxAndroidRandomUUID(): couldn't find OFAndroid java class";
		return "";
	}


	jmethodID randomUUID = ofGetJNIEnv()->GetStaticMethodID(javaClass,"getRandomUUID","()Ljava/lang/String;");
	if(!randomUUID){
		ofLogError("ofxAndroidUtils") << "ofxAndroidRandomUUID(): couldn't find OFAndroid randomUUID method";
		return "";
	}
	jstring str = (jstring)	ofGetJNIEnv()->CallStaticObjectMethod(javaClass,randomUUID);

	jboolean isCopy;
	return ofGetJNIEnv()->GetStringUTFChars(str,&isCopy);
}
string ofxAndroidGetTextBoxResult(){
	jclass javaClass = ofGetJavaOFAndroid();

	if(javaClass==0){
		ofLogError("ofxAndroidUtils") << "ofxAndroidGetTextBoxResult(): could't find OFAndroid java class";
		return "";
	}


	jmethodID getLastTextBoxResult = ofGetJNIEnv()->GetStaticMethodID(javaClass,"getLastTextBoxResult","()Ljava/lang/String;");
	if(!getLastTextBoxResult){
		ofLogError("ofxAndroidUtils") << "ofxAndroidGetTextBoxResult(): couldn't find OFAndroid getLastTextBoxResult method";
		return "";
	}
	jstring str = (jstring)	ofGetJNIEnv()->CallStaticObjectMethod(javaClass,getLastTextBoxResult);

	jboolean isCopy;
	return ofGetJNIEnv()->GetStringUTFChars(str,&isCopy);
}
void ofAppAndroidWindow::setupOpenGL(int w, int h, ofWindowMode screenMode){
	jclass javaClass = ofGetJNIEnv()->FindClass("cc/openframeworks/OFAndroid");

	if(javaClass==0){
		ofLogError("ofAppAndroidWindow") << "setupOpenGL(): couldn't find OFAndroid java class";
		return;
	}

	jmethodID method = ofGetJNIEnv()->GetStaticMethodID(javaClass,"setupGL","(I)V");
	if(!method){
		ofLogError("ofAppAndroidWindow") << "setupOpenGL(): couldn't find OFAndroid setupGL method";
		return;
	}

	if(ofGetGLProgrammableRenderer())
		ofGetJNIEnv()->CallStaticVoidMethod(javaClass,method,2);
	else
		ofGetJNIEnv()->CallStaticVoidMethod(javaClass,method,1);
}
bool ofxJavaCallBoolMethod(jobject object, jclass classID, std::string methodName, std::string methodSignature, va_list args){
	jmethodID methodID = ofxJavaGetMethodID(classID, methodName, methodSignature);

	if (!methodID){
		ofLogError() << "Couldn't find " << methodName << " for bool call";
		return false;
	}

	return ofGetJNIEnv()->CallBooleanMethodV(object, methodID, args);
}
void ofAppAndroidWindow::setOrientation(ofOrientation _orientation){
	//if(orientation==_orientation) return;
	orientation = _orientation;
	jclass javaClass = ofGetJNIEnv()->FindClass("cc/openframeworks/OFAndroid");

	if(javaClass==0){
		ofLogError("ofAppAndroidWindow") << "setOrientation(): couldn't find OFAndroid java class";
		return;
	}

	jmethodID setScreenOrientation = ofGetJNIEnv()->GetStaticMethodID(javaClass,"setScreenOrientation","(I)V");
	if(!setScreenOrientation){
		ofLogError("ofAppAndroidWindow") << "setOrientation(): couldn't find OFAndroid setScreenOrientation method";
		return;
	}
	if(orientation==OF_ORIENTATION_UNKNOWN)
		ofGetJNIEnv()->CallStaticVoidMethod(javaClass,setScreenOrientation,-1);
	else
		ofGetJNIEnv()->CallStaticVoidMethod(javaClass,setScreenOrientation,ofOrientationToDegrees(orientation));
}