2013-12-16 14:47:33 -04:30
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2013 Miguel Angel Astor Romero
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
2013-11-21 08:10:12 -04:30
|
|
|
package ve.ucv.ciens.ccg.nxtar;
|
|
|
|
|
|
2014-01-24 12:24:51 -04:30
|
|
|
import org.opencv.android.BaseLoaderCallback;
|
|
|
|
|
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener;
|
|
|
|
|
import org.opencv.android.LoaderCallbackInterface;
|
|
|
|
|
import org.opencv.android.OpenCVLoader;
|
|
|
|
|
import org.opencv.core.Mat;
|
|
|
|
|
|
2013-11-26 17:58:26 -04:30
|
|
|
import ve.ucv.ciens.ccg.nxtar.interfaces.MulticastEnabler;
|
2013-11-28 09:01:45 -04:30
|
|
|
import ve.ucv.ciens.ccg.nxtar.interfaces.Toaster;
|
2013-11-26 17:58:26 -04:30
|
|
|
import android.content.Context;
|
2014-01-24 12:24:51 -04:30
|
|
|
import android.content.pm.ActivityInfo;
|
2013-11-26 17:58:26 -04:30
|
|
|
import android.net.wifi.WifiManager;
|
|
|
|
|
import android.net.wifi.WifiManager.MulticastLock;
|
2013-11-21 08:10:12 -04:30
|
|
|
import android.os.Bundle;
|
2013-11-28 09:01:45 -04:30
|
|
|
import android.os.Handler;
|
|
|
|
|
import android.widget.Toast;
|
2013-11-21 08:10:12 -04:30
|
|
|
|
2013-11-27 08:11:37 -04:30
|
|
|
import com.badlogic.gdx.Gdx;
|
2013-11-21 08:10:12 -04:30
|
|
|
import com.badlogic.gdx.backends.android.AndroidApplication;
|
|
|
|
|
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
|
2014-01-24 12:24:51 -04:30
|
|
|
import com.badlogic.gdx.controllers.mappings.Ouya;
|
2013-11-21 08:10:12 -04:30
|
|
|
|
2014-01-24 12:24:51 -04:30
|
|
|
public class MainActivity extends AndroidApplication implements Toaster, MulticastEnabler, CvCameraViewListener{
|
2013-11-26 17:58:26 -04:30
|
|
|
private static final String TAG = "NXTAR_ANDROID_MAIN";
|
|
|
|
|
private static final String CLASS_NAME = MainActivity.class.getSimpleName();
|
|
|
|
|
|
|
|
|
|
private WifiManager wifiManager;
|
|
|
|
|
private MulticastLock multicastLock;
|
2013-11-28 09:01:45 -04:30
|
|
|
private Handler uiHandler;
|
|
|
|
|
private Context uiContext;
|
2014-01-24 12:24:51 -04:30
|
|
|
private boolean ocvOn;
|
|
|
|
|
private BaseLoaderCallback loaderCallback;
|
2013-11-26 17:58:26 -04:30
|
|
|
|
2013-11-21 08:10:12 -04:30
|
|
|
@Override
|
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
2014-01-24 12:24:51 -04:30
|
|
|
ocvOn = false;
|
|
|
|
|
|
|
|
|
|
if(!Ouya.runningOnOuya){
|
|
|
|
|
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
|
|
|
|
|
}else{
|
|
|
|
|
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-28 09:01:45 -04:30
|
|
|
uiHandler = new Handler();
|
|
|
|
|
uiContext = this;
|
2013-11-26 17:58:26 -04:30
|
|
|
wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
|
|
|
|
|
|
2013-11-21 08:10:12 -04:30
|
|
|
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
|
|
|
|
|
cfg.useGL20 = false;
|
2014-01-21 12:21:22 -04:30
|
|
|
cfg.useAccelerometer = false;
|
|
|
|
|
cfg.useCompass = false;
|
|
|
|
|
cfg.useWakelock = true;
|
2013-11-21 08:10:12 -04:30
|
|
|
|
2014-01-24 12:24:51 -04:30
|
|
|
loaderCallback = new BaseLoaderCallback(this){
|
|
|
|
|
@Override
|
|
|
|
|
public void onManagerConnected(int status){
|
|
|
|
|
switch(status){
|
|
|
|
|
case LoaderCallbackInterface.SUCCESS:
|
|
|
|
|
ocvOn = true;
|
|
|
|
|
Toast.makeText(uiContext, R.string.ocv_success, Toast.LENGTH_LONG).show();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
Toast.makeText(uiContext, R.string.ocv_failed, Toast.LENGTH_LONG).show();
|
|
|
|
|
Gdx.app.exit();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_7, this, loaderCallback);
|
2013-11-28 09:01:45 -04:30
|
|
|
initialize(new NxtARCore(this), cfg);
|
2013-11-26 17:58:26 -04:30
|
|
|
}
|
|
|
|
|
|
2013-11-28 09:01:45 -04:30
|
|
|
////////////////////////////////
|
|
|
|
|
// Toaster interface methods. //
|
|
|
|
|
////////////////////////////////
|
|
|
|
|
@Override
|
|
|
|
|
public void showShortToast(final String msg) {
|
|
|
|
|
uiHandler.post(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
Toast.makeText(uiContext, msg, Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void showLongToast(final String msg) {
|
|
|
|
|
uiHandler.post(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
Toast.makeText(uiContext, msg, Toast.LENGTH_LONG).show();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////
|
|
|
|
|
// MulticastEnabler interface methods. //
|
|
|
|
|
/////////////////////////////////////////
|
2013-11-26 17:58:26 -04:30
|
|
|
@Override
|
|
|
|
|
public void enableMulticast(){
|
2014-01-24 12:24:51 -04:30
|
|
|
Gdx.app.log(TAG, CLASS_NAME + ".enableMulticast() :: Requesting multicast lock.");
|
2013-11-26 17:58:26 -04:30
|
|
|
multicastLock = wifiManager.createMulticastLock(TAG);
|
|
|
|
|
multicastLock.setReferenceCounted(true);
|
|
|
|
|
multicastLock.acquire();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void disableMulticast() {
|
2014-01-24 12:24:51 -04:30
|
|
|
Gdx.app.log(TAG, CLASS_NAME + ".disableMulticast() :: Releasing multicast lock.");
|
2013-11-26 17:58:26 -04:30
|
|
|
if(multicastLock != null){
|
|
|
|
|
multicastLock.release();
|
|
|
|
|
multicastLock = null;
|
|
|
|
|
}
|
2013-11-21 08:10:12 -04:30
|
|
|
}
|
2014-01-24 12:24:51 -04:30
|
|
|
|
|
|
|
|
/////////////////////////////////////////////
|
|
|
|
|
// CvCameraViewListener interface methods. //
|
|
|
|
|
/////////////////////////////////////////////
|
|
|
|
|
/**
|
|
|
|
|
* <p>This method does nothing. It is here because it must be implemented in order to use OpenCV.</p>
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void onCameraViewStarted(int width, int height){ }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <p>This method does nothing. It is here because it must be implemented in order to use OpenCV.</p>
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void onCameraViewStopped(){ }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <p>This method does nothing. It is here because it must be implemented in order to use OpenCV.</p>
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Mat onCameraFrame(Mat inputFrame){
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2013-11-21 08:10:12 -04:30
|
|
|
}
|