Files
NxtAR-cam/src/ve/ucv/ciens/ccg/nxtcam/MainActivity.java

202 lines
6.2 KiB
Java
Raw Normal View History

2013-11-05 11:44:26 -04:30
package ve.ucv.ciens.ccg.nxtcam;
2013-11-05 18:22:25 -04:30
import java.io.IOException;
import java.net.DatagramPacket;
2013-11-05 11:44:26 -04:30
import java.net.InetAddress;
2013-11-05 18:22:25 -04:30
import java.net.MulticastSocket;
2013-11-05 11:44:26 -04:30
import ve.ucv.ciens.ccg.nxtcam.dialogs.WifiOnDialog;
import ve.ucv.ciens.ccg.nxtcam.dialogs.WifiOnDialog.WifiOnDialogListener;
2013-11-05 11:44:26 -04:30
import ve.ucv.ciens.ccg.nxtcam.utils.Logger;
2013-11-05 18:22:25 -04:30
import ve.ucv.ciens.ccg.nxtcam.utils.ProjectConstants;
2013-11-05 11:44:26 -04:30
import android.app.Activity;
import android.app.DialogFragment;
2013-11-05 11:44:26 -04:30
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
2013-11-05 18:22:25 -04:30
import android.net.wifi.WifiManager.MulticastLock;
2013-11-05 11:44:26 -04:30
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;
2013-11-05 18:22:25 -04:30
/**
* Entry point por the NxtCAM application.
*
* This activity shows a splashscreen and handles the search for the controller device
* via a simle ad hoc UDP based service discovery method. Basically, it just listens for
* multicast packets sent by the controller device on the multicast address defined in
* ProjectConstants.java. When the packet is received the next activity of the application
* is launched with the ip address found. The service discovery process continues until a
* datagram carrying the string "NxtAR server here!" is received.
*
* @author miky
*
*/
public class MainActivity extends Activity implements WifiOnDialogListener{
2013-11-05 18:22:25 -04:30
// Cosntant fields.
2013-11-05 11:44:26 -04:30
private final String TAG = "NXTCAM_MAIN";
private final String CLASS_NAME = MainActivity.class.getSimpleName();
2013-11-05 18:22:25 -04:30
// Resources.
2013-11-05 11:44:26 -04:30
private WifiManager wifiManager;
2013-11-05 18:22:25 -04:30
// Variables.
2013-11-05 11:44:26 -04:30
private boolean wifiOnByMe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
2013-11-05 18:22:25 -04:30
// Set up fields.
wifiOnByMe = false;
// Set up services.
2013-11-05 11:44:26 -04:30
wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
}
@Override
public void onResume(){
super.onResume();
if(!wifiManager.isWifiEnabled()){
DialogFragment wifiOn = new WifiOnDialog();
wifiOn.show(getFragmentManager(), "wifi_on");
}
2013-11-05 11:44:26 -04:30
}
@Override
public void onPause(){
super.onPause();
2013-11-05 18:22:25 -04:30
if(wifiManager.isWifiEnabled() && wifiOnByMe)
setWifi(false);
2013-11-05 11:44:26 -04:30
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
2013-11-05 18:22:25 -04:30
/**
* Start the camera capture activity if a server was found through service discovery.
*
* @param serverFound Indicates if a server was found, doh!
* @param ipAddress The ip address of the server.
*/
private void startCamActivity(boolean serverFound, String ipAddress){
if(serverFound){
Logger.log_d(TAG, CLASS_NAME + ".startCamActivity() :: Launching camera activity.");
2013-11-05 11:44:26 -04:30
Intent intent = new Intent(this, CamActivity.class);
2013-11-05 18:22:25 -04:30
intent.putExtra("address", ipAddress);
2013-11-05 11:44:26 -04:30
startActivity(intent);
}else{
Logger.log_d(TAG, CLASS_NAME + ".startCamActivity() :: Cannot launch camera activity.");
2013-11-05 11:44:26 -04:30
Toast.makeText(this, R.string.badIpToast, Toast.LENGTH_SHORT).show();
}
}
2013-11-05 18:22:25 -04:30
/**
* Sets the state of the device's WiFi radio.
*
* @param radioState The state to set the radio to; true for on, false for off.
*/
private void setWifi(boolean radioState){
wifiManager.setWifiEnabled(radioState);
Logger.log_d(TAG, CLASS_NAME + ".setWifi() :: setting wifi to " + (radioState ? "on" : "off"));
2013-11-05 18:22:25 -04:30
if(radioState)
2013-11-05 11:44:26 -04:30
wifiOnByMe = true;
else
wifiOnByMe = false;
}
2013-11-05 18:22:25 -04:30
/**
* Asynchronous task for ad hoc UDP service discovery.
*
* @author Miguel Angel Astor Romero
*/
private class ServiceDiscoveryTask extends AsyncTask<Void, Void, Boolean>{
private final String CLASS_NAME = ServiceDiscoveryTask.class.getSimpleName();
private MulticastSocket udpSocket;
private DatagramPacket packet;
private MulticastLock multicastLock;
public ServiceDiscoveryTask(){
// Open a multicast socket and join the project's multicast group.
try{
udpSocket = new MulticastSocket(ProjectConstants.SERVER_UDP_PORT);
InetAddress group = InetAddress.getByName(ProjectConstants.MULTICAST_ADDRESS);
udpSocket.joinGroup(group);
}catch(IOException io){
Logger.log_e(TAG ,CLASS_NAME + ".ServiceDiscoveryTask() :: " + io.getMessage());
2013-11-05 18:22:25 -04:30
}
}
@Override
protected Boolean doInBackground(Void... params){
boolean result, done = false;
byte[] buffer = (new String("Server is here")).getBytes();
// Create a buffer and tell Android we want to receive multicast datagrams.
packet = new DatagramPacket(buffer, buffer.length);
multicastLock = wifiManager.createMulticastLock(TAG);
multicastLock.setReferenceCounted(true);
multicastLock.acquire();
// Listen for a UDP datagram on the multicast group.
// If the datagram received contains a string with it's content equal to "NxtAR server here!"
// then assume the server found is a valid controller device.
try{
while(!done){
udpSocket.receive(packet);
Logger.log_d(TAG, CLASS_NAME + ".run() :: Found a server at " + packet.getAddress().getHostAddress());
2013-11-05 18:22:25 -04:30
String received = new String(packet.getData());
if(received.compareTo("NxtAR server here!") == 0)
done = true;
}
result = true;
}catch(IOException io){
Logger.log_e(TAG, CLASS_NAME + ".doInBackground() :: " + io.getMessage());
2013-11-05 18:22:25 -04:30
result = false;
}
// Tell Android we do not want to receive more UDP datagrams to save battery life.
if(multicastLock != null){
multicastLock.release();
multicastLock = null;
}
return result;
}
@Override
protected void onPostExecute(Boolean result){
super.onPostExecute(result);
// If a server was found then start the next activity.
if(packet != null)
startCamActivity(result, packet.getAddress().getHostAddress());
else
startCamActivity(false, null);
2013-11-05 11:44:26 -04:30
}
}
2013-11-05 11:44:26 -04:30
/* ServiceDiscoveryTask serviceDiscovery = new ServiceDiscoveryTask();
serviceDiscovery.execute(); */
2013-11-05 11:44:26 -04:30
@Override
public void onWifiOnDialogPositiveClick(DialogFragment dialog) {
Toast.makeText(this, R.string.wifi_on_success, Toast.LENGTH_SHORT).show();
}
2013-11-05 11:44:26 -04:30
@Override
public void onWifiOnDialogNegativeClick(DialogFragment dialog) {
Toast.makeText(this, R.string.wifi_on_fail, Toast.LENGTH_SHORT).show();
System.exit(0);
};
2013-11-05 11:44:26 -04:30
}