Added a dialog for turning the WiFi radio on.

This commit is contained in:
2013-11-21 10:13:38 -04:30
parent b3229d908c
commit cd5a08753c
11 changed files with 210 additions and 212 deletions

View File

@@ -5,7 +5,7 @@
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />

View File

@@ -8,32 +8,4 @@
android:paddingTop="@dimen/activity_vertical_margin"
android:baselineAligned="false"
tools:context=".MainActivity" >
<Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="@string/start_button" />
<!-- <EditText
android:id="@+id/ipAddressField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/startButton"
android:layout_centerHorizontal="true"
android:ems="10"
android:inputType="text" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/ipAddressField"
android:layout_alignBottom="@+id/ipAddressField"
android:layout_toLeftOf="@+id/ipAddressField"
android:text="@string/ipAddressLabel" />
-->
</RelativeLayout>

View File

@@ -12,5 +12,11 @@
<string name="ipAddressLabel">Dirección IP de NxtAR</string>
<string name="badIpToast">La dirección IP no es válida</string>
<string name="emptyIpToast">Rellene el campo de dirección IP</string>
<string name="wifi_on_title">Esta app requiere WiFi</string>
<string name="wifi_on_msg">¿Encender el WiFi?</string>
<string name="cancel_button">Cancelar</string>
<string name="wifi_on_button">Encender</string>
<string name="wifi_on_success">El radio del WiFi está encendido</string>
<string name="wifi_on_fail">Esta app no puede funcionar sin wifi. Cerrando.</string>
</resources>

View File

@@ -12,5 +12,11 @@
<string name="ipAddressLabel">NxtAR IP Address</string>
<string name="badIpToast">Invalid IP address</string>
<string name="emptyIpToast">Fill out the IP address field</string>
<string name="wifi_on_title">This app requires WiFi</string>
<string name="wifi_on_msg">Turn on the WiFi?</string>
<string name="cancel_button">Cancel</string>
<string name="wifi_on_button">Turn on</string>
<string name="wifi_on_success">The Wifi radio is on now</string>
<string name="wifi_on_fail">This app cannot work without wifi. Closing.</string>
</resources>

View File

@@ -3,14 +3,12 @@ package ve.ucv.ciens.ccg.nxtcam;
import ve.ucv.ciens.ccg.nxtcam.camera.CameraPreview;
import ve.ucv.ciens.ccg.nxtcam.network.ImageTransferThread;
import ve.ucv.ciens.ccg.nxtcam.utils.Logger;
import ve.ucv.ciens.ccg.nxtcam.utils.ProjectConstants;
import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
@@ -94,7 +92,7 @@ public class CamActivity extends Activity{
if(hwCamera != null){
cPreview.setCamera(hwCamera);
}else{
Log.wtf(TAG, CLASS_NAME + ".startCameraPreview() :: CAMERA IS NULL!");
Logger.log_wtf(TAG, CLASS_NAME + ".startCameraPreview() :: CAMERA IS NULL!");
System.exit(1);
}
}
@@ -115,7 +113,7 @@ public class CamActivity extends Activity{
try{
cam = Camera.open(0);
}catch(Exception e){
if(ProjectConstants.DEBUG) Log.e(TAG, CLASS_NAME + ".doInBackground() :: Failed to open the camera.");
Logger.log_e(TAG, CLASS_NAME + ".doInBackground() :: Failed to open the camera.");
}
return cam;
}
@@ -124,9 +122,9 @@ public class CamActivity extends Activity{
protected void onPostExecute(Camera result) {
hwCamera = result;
if(result != null){
Logger.log(Logger.LOG_TYPES.DEBUG, TAG, CLASS_NAME + ".onPostExecute() :: Camera successfully opened");
Logger.log_d(TAG, CLASS_NAME + ".onPostExecute() :: Camera successfully opened");
}else{
Logger.log(Logger.LOG_TYPES.DEBUG, TAG, CLASS_NAME + ".onPostExecute() :: Camera open failed on background task.");
Logger.log_d(TAG, CLASS_NAME + ".onPostExecute() :: Camera open failed on background task.");
Toast.makeText(getApplicationContext(), R.string.camera_failure, Toast.LENGTH_LONG).show();
}
startCameraPreview();

View File

@@ -4,11 +4,13 @@ import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;
import ve.ucv.ciens.ccg.nxtcam.dialogs.WifiOnDialog;
import ve.ucv.ciens.ccg.nxtcam.dialogs.WifiOnDialog.WifiOnDialogListener;
import ve.ucv.ciens.ccg.nxtcam.utils.Logger;
import ve.ucv.ciens.ccg.nxtcam.utils.ProjectConstants;
import android.app.Activity;
import android.app.DialogFragment;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
@@ -16,9 +18,6 @@ import android.net.wifi.WifiManager.MulticastLock;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
/**
@@ -34,15 +33,11 @@ import android.widget.Toast;
* @author miky
*
*/
public class MainActivity extends Activity {
public class MainActivity extends Activity implements WifiOnDialogListener{
// Cosntant fields.
private final String TAG = "NXTCAM_MAIN";
private final String CLASS_NAME = MainActivity.class.getSimpleName();
// Gui components.
private Button startButton;
//private TextView ipField;
// Resources.
private WifiManager wifiManager;
@@ -57,23 +52,18 @@ public class MainActivity extends Activity {
// Set up fields.
wifiOnByMe = false;
// Set up gui components.
startButton = (Button)findViewById(R.id.startButton);
startButton.setOnClickListener(startClickListener);
// ipField = (TextView)findViewById(R.id.ipAddressField);
// Set up services.
wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
if(!wifiManager.isWifiEnabled())
setWifi(true);
}
@Override
public void onResume(){
super.onResume();
if(!wifiManager.isWifiEnabled())
setWifi(true);
if(!wifiManager.isWifiEnabled()){
DialogFragment wifiOn = new WifiOnDialog();
wifiOn.show(getFragmentManager(), "wifi_on");
}
}
@Override
@@ -99,12 +89,12 @@ public class MainActivity extends Activity {
*/
private void startCamActivity(boolean serverFound, String ipAddress){
if(serverFound){
Logger.log(Logger.LOG_TYPES.DEBUG, TAG, CLASS_NAME + ".startCamActivity() :: Launching camera activity.");
Logger.log_d(TAG, CLASS_NAME + ".startCamActivity() :: Launching camera activity.");
Intent intent = new Intent(this, CamActivity.class);
intent.putExtra("address", ipAddress);
startActivity(intent);
}else{
Logger.log(Logger.LOG_TYPES.DEBUG, TAG, CLASS_NAME + ".startCamActivity() :: Cannot launch camera activity.");
Logger.log_d(TAG, CLASS_NAME + ".startCamActivity() :: Cannot launch camera activity.");
Toast.makeText(this, R.string.badIpToast, Toast.LENGTH_SHORT).show();
}
}
@@ -116,36 +106,13 @@ public class MainActivity extends Activity {
*/
private void setWifi(boolean radioState){
wifiManager.setWifiEnabled(radioState);
Logger.log(Logger.LOG_TYPES.DEBUG, TAG, CLASS_NAME + ".setWifi() :: setting wifi to " + (radioState ? "on" : "off"));
Logger.log_d(TAG, CLASS_NAME + ".setWifi() :: setting wifi to " + (radioState ? "on" : "off"));
if(radioState)
wifiOnByMe = true;
else
wifiOnByMe = false;
}
/*private void validateIpAddress(){
if(ipField.getText().toString().compareTo("") != 0){
Logger.log(Logger.LOG_TYPES.DEBUG, TAG, CLASS_NAME + "validateIpAddress() :: Launching verification task.");
VerifyIpAddressTask verifyIp = new VerifyIpAddressTask();
verifyIp.execute(ipField.getText().toString());
}else{
Logger.log(Logger.LOG_TYPES.DEBUG, TAG, CLASS_NAME + "validateIpAddress() :: Ip address field is empty.");
Toast.makeText(this, R.string.emptyIpToast, Toast.LENGTH_SHORT).show();
}
}*/
/**
* Event listener for the connection button.
*/
private final View.OnClickListener startClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
//validateIpAddress();
ServiceDiscoveryTask serviceDiscovery = new ServiceDiscoveryTask();
serviceDiscovery.execute();
}
};
/**
* Asynchronous task for ad hoc UDP service discovery.
*
@@ -166,7 +133,7 @@ public class MainActivity extends Activity {
InetAddress group = InetAddress.getByName(ProjectConstants.MULTICAST_ADDRESS);
udpSocket.joinGroup(group);
}catch(IOException io){
Logger.log(Logger.LOG_TYPES.ERROR, TAG ,CLASS_NAME + ".ServiceDiscoveryTask() :: " + io.getMessage());
Logger.log_e(TAG ,CLASS_NAME + ".ServiceDiscoveryTask() :: " + io.getMessage());
}
}
@@ -187,14 +154,14 @@ public class MainActivity extends Activity {
try{
while(!done){
udpSocket.receive(packet);
Logger.log(Logger.LOG_TYPES.DEBUG, TAG, CLASS_NAME + ".run() :: Found a server at " + packet.getAddress().getHostAddress());
Logger.log_d(TAG, CLASS_NAME + ".run() :: Found a server at " + packet.getAddress().getHostAddress());
String received = new String(packet.getData());
if(received.compareTo("NxtAR server here!") == 0)
done = true;
}
result = true;
}catch(IOException io){
Logger.log(Logger.LOG_TYPES.ERROR, TAG, CLASS_NAME + ".doInBackground() :: " + io.getMessage());
Logger.log_e(TAG, CLASS_NAME + ".doInBackground() :: " + io.getMessage());
result = false;
}
@@ -216,27 +183,19 @@ public class MainActivity extends Activity {
else
startCamActivity(false, null);
}
}
/* ServiceDiscoveryTask serviceDiscovery = new ServiceDiscoveryTask();
serviceDiscovery.execute(); */
@Override
public void onWifiOnDialogPositiveClick(DialogFragment dialog) {
Toast.makeText(this, R.string.wifi_on_success, Toast.LENGTH_SHORT).show();
}
@Override
public void onWifiOnDialogNegativeClick(DialogFragment dialog) {
Toast.makeText(this, R.string.wifi_on_fail, Toast.LENGTH_SHORT).show();
System.exit(0);
};
/* private class VerifyIpAddressTask extends AsyncTask<String, Void, Boolean>{
private final String CLASS_NAME = VerifyIpAddressTask.class.getSimpleName();
@Override
protected Boolean doInBackground(String... params) {
try{
InetAddress.getByName(params[0]);
Logger.log(Logger.LOG_TYPES.DEBUG, TAG, CLASS_NAME + "doInBackground() :: IP address is valid.");
return true;
}catch(UnknownHostException uh){
Logger.log(Logger.LOG_TYPES.DEBUG, TAG, CLASS_NAME + "doInBackground() :: IP address is not valid.");
return false;
}
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
startCamActivity(result);
}
};*/
}

View File

@@ -22,29 +22,29 @@ public class CameraImageMonitor{
}
public void setImageData(byte[] image){
Logger.log(Logger.LOG_TYPES.DEBUG, TAG, CLASS_NAME + ".setImageData() :: Copying new image.");
Logger.log_d(TAG, CLASS_NAME + ".setImageData() :: Copying new image.");
synchronized(image){
this.image = new byte[image.length];
System.arraycopy(image, 0, this.image, 0, image.length);
imgChanged = true;
image.notifyAll();
}
Logger.log(Logger.LOG_TYPES.DEBUG, TAG, CLASS_NAME + ".setImageData() :: Data copy finished.");
Logger.log_d(TAG, CLASS_NAME + ".setImageData() :: Data copy finished.");
}
public byte[] getImageData(){
byte[] returnImg;
Logger.log(Logger.LOG_TYPES.DEBUG, TAG, CLASS_NAME + ".getImageData() :: Entry point.");
Logger.log_d(TAG, CLASS_NAME + ".getImageData() :: Entry point.");
synchronized(image){
while(!imgChanged){
Logger.log(Logger.LOG_TYPES.DEBUG, TAG, CLASS_NAME + ".getImageData() :: Waiting for new data.");
Logger.log_d(TAG, CLASS_NAME + ".getImageData() :: Waiting for new data.");
try{ image.wait(); }catch(InterruptedException ie){}
}
Logger.log(Logger.LOG_TYPES.DEBUG, TAG, CLASS_NAME + ".getImageData() :: Retrieving new data.");
Logger.log_d(TAG, CLASS_NAME + ".getImageData() :: Retrieving new data.");
returnImg = image.clone();
imgChanged = false;
}
Logger.log(Logger.LOG_TYPES.DEBUG, TAG, CLASS_NAME + ".getImageData() :: New data retreived.");
Logger.log_d(TAG, CLASS_NAME + ".getImageData() :: New data retreived.");
return returnImg;
}

View File

@@ -23,7 +23,7 @@ public class CameraPreview extends ViewGroup implements SurfaceHolder.Callback,
private final String CLASS_NAME = CameraPreview.class.getSimpleName();
private Size mPreviewSize;
private List<Size> mSupportedPreviewSizes;
private List<Size> mSupportedPreviewSizes;
private CameraImageMonitor camMonitor;
private Activity parentActivity;
private SurfaceView mSurfaceView;
@@ -42,7 +42,7 @@ public class CameraPreview extends ViewGroup implements SurfaceHolder.Callback,
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB)
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void setCamera(Camera camera){
mCamera = camera;
if(mCamera != null){
@@ -59,7 +59,7 @@ public class CameraPreview extends ViewGroup implements SurfaceHolder.Callback,
mCamera.setPreviewDisplay(holder);
}
} catch (IOException e) {
Logger.log(Logger.LOG_TYPES.DEBUG, TAG, "Error setting camera preview: " + e.getMessage());
Logger.log_d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
@@ -79,7 +79,7 @@ public class CameraPreview extends ViewGroup implements SurfaceHolder.Callback,
}catch (Exception e){ }
requestLayout();
Camera.Parameters camParams = mCamera.getParameters();
/*Size optimal = getOptimalPreviewSize(camParams.getSupportedPreviewSizes(), w, h);
if(ProjectConstants.DEBUG)
@@ -109,20 +109,20 @@ public class CameraPreview extends ViewGroup implements SurfaceHolder.Callback,
mCamera.setDisplayOrientation(result);
mCamera.setPreviewCallback(this);
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
}catch (Exception e){
Logger.log(Logger.LOG_TYPES.DEBUG, TAG, CLASS_NAME + ".surfaceChanged() :: Error starting camera preview: " + e.getMessage());
Logger.log_d(TAG, CLASS_NAME + ".surfaceChanged() :: Error starting camera preview: " + e.getMessage());
}
}
@Override
public void onPreviewFrame(byte[] data, Camera camera){
Logger.log(Logger.LOG_TYPES.DEBUG, TAG, CLASS_NAME + ".onPreviewFrame() :: Preview received");
Logger.log(Logger.LOG_TYPES.DEBUG, TAG, CLASS_NAME + ".onPreviewFrame() :: Frame has" + (camMonitor.hasChanged() ? "" : " not") + " changed.");
Logger.log_d(TAG, CLASS_NAME + ".onPreviewFrame() :: Preview received");
Logger.log_d(TAG, CLASS_NAME + ".onPreviewFrame() :: Frame has" + (camMonitor.hasChanged() ? "" : " not") + " changed.");
if(!camMonitor.hasChanged())
camMonitor.setImageData(data);
}
@@ -132,74 +132,74 @@ public class CameraPreview extends ViewGroup implements SurfaceHolder.Callback,
}
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) w / h;
if (sizes == null) return null;
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) w / h;
if (sizes == null) return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
int targetHeight = h;
// Try to find an size match aspect ratio and size
for (Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// Try to find an size match aspect ratio and size
for (Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if (changed && getChildCount() > 0) {
final View child = getChildAt(0);
final View child = getChildAt(0);
final int width = r - l;
final int height = b - t;
final int width = r - l;
final int height = b - t;
int previewWidth = width;
int previewHeight = height;
if (mPreviewSize != null) {
previewWidth = mPreviewSize.width;
previewHeight = mPreviewSize.height;
}
int previewWidth = width;
int previewHeight = height;
if (mPreviewSize != null) {
previewWidth = mPreviewSize.width;
previewHeight = mPreviewSize.height;
}
// Center the child SurfaceView within the parent.
if (width * previewHeight > height * previewWidth) {
final int scaledChildWidth = previewWidth * height / previewHeight;
child.layout((width - scaledChildWidth) / 2, 0,
(width + scaledChildWidth) / 2, height);
} else {
final int scaledChildHeight = previewHeight * width / previewWidth;
child.layout(0, (height - scaledChildHeight) / 2,
width, (height + scaledChildHeight) / 2);
}
}
// Center the child SurfaceView within the parent.
if (width * previewHeight > height * previewWidth) {
final int scaledChildWidth = previewWidth * height / previewHeight;
child.layout((width - scaledChildWidth) / 2, 0,
(width + scaledChildWidth) / 2, height);
} else {
final int scaledChildHeight = previewHeight * width / previewWidth;
child.layout(0, (height - scaledChildHeight) / 2,
width, (height + scaledChildHeight) / 2);
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);
setMeasuredDimension(width, height);
if (mSupportedPreviewSizes != null) {
mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);
setMeasuredDimension(width, height);
if (mSupportedPreviewSizes != null) {
mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
}
}
}

View File

@@ -0,0 +1,62 @@
package ve.ucv.ciens.ccg.nxtcam.dialogs;
import ve.ucv.ciens.ccg.nxtcam.R;
import ve.ucv.ciens.ccg.nxtcam.utils.Logger;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.net.wifi.WifiManager;
import android.os.Bundle;
public class WifiOnDialog extends DialogFragment {
private final String TAG = "NXTCAM_MAIN";
private final String CLASS_NAME = WifiOnDialog.class.getSimpleName();
private WifiManager wifiManager;
public interface WifiOnDialogListener{
public void onWifiOnDialogPositiveClick(DialogFragment dialog);
public void onWifiOnDialogNegativeClick(DialogFragment dialog);
}
public void setWifiManager(WifiManager wifiManager){
this.wifiManager = wifiManager;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.wifi_on_msg).setPositiveButton(R.string.wifi_on_button, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if(wifiManager != null){
wifiManager.setWifiEnabled(true);
Logger.log_d(TAG, CLASS_NAME + " :: setting wifi to on.");
}else
Logger.log_wtf(TAG, CLASS_NAME + " :: wifiManager is null! Doing nothing.");
}
}).setNegativeButton(R.string.cancel_button, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { }
});
// Create the AlertDialog object and return it
return builder.create();
}
@Override
public void onAttach(Activity activity){
try{
@SuppressWarnings("unused")
WifiOnDialogListener listener = (WifiOnDialogListener)activity;
}catch(ClassCastException cce){
throw new ClassCastException(activity.toString() + "Must implement WifiOnDialogListener.");
}
}
}

View File

@@ -10,14 +10,13 @@ import java.net.Socket;
import ve.ucv.ciens.ccg.nxtcam.camera.CameraImageMonitor;
import ve.ucv.ciens.ccg.nxtcam.utils.Logger;
import ve.ucv.ciens.ccg.nxtcam.utils.Logger.LOG_TYPES;
import ve.ucv.ciens.ccg.nxtcam.utils.ProjectConstants;
public class ImageTransferThread extends Thread{
private final String TAG = "IM_THREAD";
private final String CLASS_NAME = ImageTransferThread.class.getSimpleName();
private boolean pause, done, connected;
private boolean pause, done;
private Object threadPauseMonitor;
private CameraImageMonitor camMonitor;
private Socket socket;
@@ -30,7 +29,6 @@ public class ImageTransferThread extends Thread{
this.serverIp = serverIp;
pause = false;
done = false;
connected = false;
threadPauseMonitor = new Object();
socket = null;
writer = null;
@@ -41,7 +39,7 @@ public class ImageTransferThread extends Thread{
public void run(){
connectToServer();
if(socket.isConnected()){
Logger.log(Logger.LOG_TYPES.ERROR, TAG, CLASS_NAME + ".run() :: Not connected to a server. Finishing thread.");
Logger.log_e(TAG, CLASS_NAME + ".run() :: Not connected to a server. Finishing thread.");
}else{
while(!done){
checkPause();
@@ -53,13 +51,13 @@ public class ImageTransferThread extends Thread{
private void connectToServer(){
try{
Logger.log(Logger.LOG_TYPES.INFO, TAG, CLASS_NAME + ".connectToServer() :: Connecting to the server at " + serverIp);
Logger.log_i(TAG, CLASS_NAME + ".connectToServer() :: Connecting to the server at " + serverIp);
socket = new Socket(InetAddress.getByName(serverIp), ProjectConstants.SERVER_TCP_PORT_1);
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Logger.log(Logger.LOG_TYPES.INFO, TAG, CLASS_NAME + ".connectToServer() :: Connection successful.");
Logger.log_i(TAG, CLASS_NAME + ".connectToServer() :: Connection successful.");
}catch(IOException io){
Logger.log(Logger.LOG_TYPES.ERROR, TAG, CLASS_NAME + ".connectToServer() :: Connection failed with message: " + io.getMessage());
Logger.log_e(TAG, CLASS_NAME + ".connectToServer() :: Connection failed with message: " + io.getMessage());
}
}
@@ -68,20 +66,20 @@ public class ImageTransferThread extends Thread{
try{
socket.close();
}catch (IOException io) {
Logger.log(Logger.LOG_TYPES.ERROR, TAG, CLASS_NAME + ".connectToServer() :: " + io.getMessage());
Logger.log_e(TAG, CLASS_NAME + ".connectToServer() :: " + io.getMessage());
}
}
}
public synchronized void finish(){
done = true;
Logger.log(Logger.LOG_TYPES.INFO, TAG, CLASS_NAME + ".finish() :: Finishing thread.");
Logger.log_i(TAG, CLASS_NAME + ".finish() :: Finishing thread.");
}
private void checkPause(){
synchronized (threadPauseMonitor){
while(pause){
Logger.log(Logger.LOG_TYPES.DEBUG, TAG, CLASS_NAME + ".checkPause() :: Pause requested.");
Logger.log_d(TAG, CLASS_NAME + ".checkPause() :: Pause requested.");
try{ threadPauseMonitor.wait(); }catch(InterruptedException ie){}
}
}
@@ -89,11 +87,11 @@ public class ImageTransferThread extends Thread{
public synchronized void pauseThread(){
pause = true;
Logger.log(Logger.LOG_TYPES.DEBUG, TAG, CLASS_NAME + ".pauseThread() :: Pausing thread.");
Logger.log_d(TAG, CLASS_NAME + ".pauseThread() :: Pausing thread.");
}
public synchronized void resumeThread(){
Logger.log(Logger.LOG_TYPES.DEBUG, TAG, CLASS_NAME + ".resumeThread() :: Resuming thread.");
Logger.log_d(TAG, CLASS_NAME + ".resumeThread() :: Resuming thread.");
synchronized (threadPauseMonitor) {
pause = false;
threadPauseMonitor.notifyAll();

View File

@@ -3,30 +3,27 @@ package ve.ucv.ciens.ccg.nxtcam.utils;
import android.util.Log;
public abstract class Logger {
public static enum LOG_TYPES{ DEBUG, INFO, WARN, ERROR, VERBOSE, WTF }
public static void log(LOG_TYPES log_type, String tag, String msg){
if(ProjectConstants.DEBUG){
switch(log_type){
case DEBUG:
Log.d(tag, msg);
break;
case INFO:
Log.i(tag, msg);
break;
case WARN:
Log.w(tag, msg);
break;
case ERROR:
Log.e(tag, msg);
break;
case VERBOSE:
Log.v(tag, msg);
break;
case WTF:
Log.wtf(tag, msg);
break;
}
}
public static void log_d(String tag, String msg){
if(ProjectConstants.DEBUG) Log.d(tag, msg);
}
public static void log_i(String tag, String msg){
if(ProjectConstants.DEBUG) Log.i(tag, msg);
}
public static void log_v(String tag, String msg){
if(ProjectConstants.DEBUG) Log.v(tag, msg);
}
public static void log_w(String tag, String msg){
if(ProjectConstants.DEBUG) Log.w(tag, msg);
}
public static void log_e(String tag, String msg){
if(ProjectConstants.DEBUG) Log.e(tag, msg);
}
public static void log_wtf(String tag, String msg){
if(ProjectConstants.DEBUG) Log.wtf(tag, msg);
}
}