diff --git a/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-play.png.REMOVED.git-id b/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-play.png.REMOVED.git-id index 4684ca15ef..8cd80b63be 100644 --- a/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-play.png.REMOVED.git-id +++ b/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-play.png.REMOVED.git-id @@ -1 +1 @@ -349baa101ddbdf4edf6d20b17362bd22c0008284 \ No newline at end of file +ba6bcddd1ff6445feb931434c6a04546bc936a4e \ No newline at end of file diff --git a/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-run-dis.png.REMOVED.git-id b/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-run-dis.png.REMOVED.git-id index 85b4d8e898..4c4883b063 100644 --- a/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-run-dis.png.REMOVED.git-id +++ b/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-run-dis.png.REMOVED.git-id @@ -1 +1 @@ -fe7e93e8e974f39426858d2bfbed7dff26c2b9c4 \ No newline at end of file +e386fa2dd33381cb9905ec5472ba7b48bdd60808 \ No newline at end of file diff --git a/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-run-down.png.REMOVED.git-id b/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-run-down.png.REMOVED.git-id new file mode 100644 index 0000000000..9d76dc4ada --- /dev/null +++ b/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-run-down.png.REMOVED.git-id @@ -0,0 +1 @@ +263d1eb95a83c1320c7c988e633f348479d38fb5 \ No newline at end of file diff --git a/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-run.png.REMOVED.git-id b/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-run.png.REMOVED.git-id index 9c17067805..4a66e6a540 100644 --- a/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-run.png.REMOVED.git-id +++ b/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_btn-run.png.REMOVED.git-id @@ -1 +1 @@ -20213456280fa1bced942496820fd71cb35ddb64 \ No newline at end of file +823b99b16a9fdddb84bf8af36e2be53cf928ea04 \ No newline at end of file diff --git a/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_logo.png.REMOVED.git-id b/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_logo.png.REMOVED.git-id new file mode 100644 index 0000000000..3f37153ef2 --- /dev/null +++ b/samples/Javascript/CocosPlayer/Resources/resources-xlarge/ccb_logo.png.REMOVED.git-id @@ -0,0 +1 @@ +7b5dab861d78877bb1354fc832404f9dfb68b0ee \ No newline at end of file diff --git a/samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CCBFileUtilsHelper.java b/samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CCBFileUtilsHelper.java index 6b191f0469..63ff5957d3 100644 --- a/samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CCBFileUtilsHelper.java +++ b/samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CCBFileUtilsHelper.java @@ -1,7 +1,9 @@ package org.cocos2dx.cocosplayer; import org.cocos2dx.lib.Cocos2dxGLSurfaceView; +import org.cocos2dx.lib.Cocos2dxHelper; import java.util.Arrays; +import java.util.Enumeration; import java.io.IOException; import android.app.Service; import android.content.Intent; @@ -16,6 +18,8 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipException; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.InputStream; import java.io.FileOutputStream; @@ -24,131 +28,112 @@ import java.io.File; public class CCBFileUtilsHelper { - public static final String TAG = CocosPlayerSocket.class.getSimpleName(); - private static boolean isValid(final File file) { + public static final String TAG = CocosPlayerSocket.class.getSimpleName(); + private static boolean isValid(final File file) { - ZipFile zipfile = null; - try { - zipfile = new ZipFile(file); - return true; - } catch (ZipException e) { - Log.i(TAG, "ERROR: "+e.toString()); - return false; - } catch (IOException e) { - Log.i(TAG, "ERROR: "+e.toString()); - return false; - } finally { - try { - if (zipfile != null) { - zipfile.close(); - zipfile = null; + ZipFile zipfile = null; + try { + zipfile = new ZipFile(file); + return true; + } catch (ZipException e) { + Log.i(TAG, "ERROR: "+e.toString()); + return false; + } catch (IOException e) { + Log.i(TAG, "ERROR: "+e.toString()); + return false; + } finally { + try { + if (zipfile != null) { + zipfile.close(); + zipfile = null; + } + } catch (IOException e) { + Log.i(TAG, "ERROR: "+e.toString()); + } } - } catch (IOException e) { - Log.i(TAG, "ERROR: "+e.toString()); - } } - } + private static boolean containsDirectory(String name) { + if(name.contains("/")) { + return true; + } return false; + } - private static void unzip(InputStream is, Context cw) { - try { + private static void unzip(InputStream is, Context cw) { + try { - File path = new File(CCBFileUtilsHelper.getBaseDirectory(cw)); - - ZipInputStream zin = new ZipInputStream(is); - ZipEntry ze = null; - byte[] buffer = new byte[1024]; - int length; - while ((ze = zin.getNextEntry()) != null) { - Log.v(TAG, "Unzipping " + ze.getName()); - - if(ze.isDirectory()) { - _dirChecker(path, ze.getName()); - } else { - File fileToWrite = new File(path, ze.getName()); - FileOutputStream fout = new FileOutputStream(fileToWrite); + File path = new File(CCBFileUtilsHelper.getBaseDirectory(cw)); - while ((length = zin.read(buffer))>0) { - fout.write(buffer, 0, length); - } - zin.closeEntry(); - fout.close(); + ZipInputStream zin = new ZipInputStream(is); + ZipEntry ze = null; + byte[] buffer = new byte[1024]; + int length; + while ((ze = zin.getNextEntry()) != null) { + Log.v(TAG, "Unzipping " + ze.getName()); + + if(ze.isDirectory()) { + _dirChecker(path, ze.getName()); + } else { + File fileToWrite = new File(path, ze.getName()); + File parent = fileToWrite.getParentFile(); + if(!parent.exists() && !parent.mkdirs()){ + throw new IllegalStateException("Couldn't create dir: " + parent); + } + FileOutputStream fout = new FileOutputStream(fileToWrite); + + while ((length = zin.read(buffer))>0) { + fout.write(buffer, 0, length); + } + zin.closeEntry(); + fout.close(); + } + + } + zin.close(); + } catch(Exception e) { + Log.e(TAG, "unzip", e); } - - } - zin.close(); - } catch(Exception e) { - Log.e(TAG, "unzip", e); + } - - } -// public static void unzip(String zipname) { -// try { + private static void _dirChecker(File path, String dir) { -// String filename; -// //Context cw = getApplicationContext(); -// //ContextWrapper cw = new ContextWrapper(this); - -// File path = new File(CCBFileUtilsHelper.getBaseDirectory(cw)); -// //File path = android.os.Environment.getExternalStorageDirectory(); //Environment.getExternalFilesDir(); -// File ccbFile = new File(path, zipname); + File f = new File(path, dir); -// if(!isValid(ccbFile)) -// Log.i(TAG, "Zip file recieved is INVALID: "); -// else { -// Log.i(TAG, "Zip file recieved is VALID: "); -// } - -// FileInputStream fin = new FileInputStream(ccbFile); -// ZipInputStream zin = new ZipInputStream(fin); -// ZipEntry ze = null; -// byte[] buffer = new byte[1024]; -// int length; -// while ((ze = zin.getNextEntry()) != null) { -// Log.v(TAG, "Unzipping " + ze.getName()); - -// if(ze.isDirectory()) { -// _dirChecker(path, ze.getName()); -// } else { -// File fileToWrite = new File(path, ze.getName()); -// FileOutputStream fout = new FileOutputStream(fileToWrite); - -// while ((length = zin.read(buffer))>0) { -// fout.write(buffer, 0, length); -// } -// zin.closeEntry(); -// fout.close(); -// } - -// } -// zin.close(); -// } catch(Exception e) { -// Log.e(TAG, "unzip", e); -// } - -// } - - private static void _dirChecker(File path, String dir) { - File f = new File(path, dir); - - if(!f.isDirectory()) { - f.mkdirs(); + if(!f.isDirectory()) { + f.mkdirs(); + } } - } - public static void unzipCCB(byte[] data, Context cw) { - String zipname = "ccb.zip"; - unzip(new ByteArrayInputStream(data), cw); - } - - public static String getBaseDirectory(Context cw) { - return cw.getCacheDir().getAbsolutePath(); - } + private static void list( String path ) { - private static File getBasePath() { - return android.os.Environment.getExternalStorageDirectory(); - } + File root = new File( path ); + File[] list = root.listFiles(); + + for ( File f : list ) { + if ( f.isDirectory() ) { + list( f.getAbsolutePath() ); + Log.i(TAG, "Dir:" + f.getAbsoluteFile() ); + } + else { + Log.i(TAG, "File:" + f.getAbsoluteFile() ); + } + } + } + + public static void unzipCCB(byte[] data, Context cw) { + String zipname = "ccb.zip"; + File path = new File(CCBFileUtilsHelper.getBaseDirectory(cw)); + unzip(new ByteArrayInputStream(data), cw); + } + + public static String getBaseDirectory(Context cw) { + return Cocos2dxHelper.getCocos2dxWritablePath(); + } + + private static File getBasePath() { + return android.os.Environment.getExternalStorageDirectory(); + } } diff --git a/samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CCBStreamHandler.java b/samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CCBStreamHandler.java index e79123977d..62ceec93f6 100644 --- a/samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CCBStreamHandler.java +++ b/samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CCBStreamHandler.java @@ -17,101 +17,145 @@ import com.dd.plist.UID; import com.dd.plist.PropertyListParser; import com.dd.plist.BinaryPropertyListParser; import com.dd.plist.BinaryPropertyListWriter; - +import java.net.Socket; +import java.io.PrintWriter; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import java.util.Random; public class CCBStreamHandler { - private static String resolution = "xlarge"; - public static final String TAG = CocosPlayerSocket.class.getSimpleName(); - - public static NSDictionary parseBinaryPLIST(InputStream is) { - try { + private static String resolution = "xlarge"; + public static final String TAG = CocosPlayerSocket.class.getSimpleName(); - NSDictionary rootDict = (NSDictionary)BinaryPropertyListParser.parse(is); - String name = rootDict.objectForKey("cmd").toString(); - Log.i(TAG, "Cmd: "+name); + public static NSDictionary parseBinaryPLIST(InputStream is) { + try { - return rootDict; + NSDictionary rootDict = (NSDictionary)BinaryPropertyListParser.parse(is); + String name = rootDict.objectForKey("cmd").toString(); + Log.i(TAG, "Cmd: "+name); - } catch(Exception ex) { - Log.i(TAG, "ERROR: "+ex.toString()); - ex.printStackTrace(); - } - return null; - } + return rootDict; - public static NSDictionary parseBinaryPLIST(byte[] is) { - try { - NSDictionary rootDict = (NSDictionary)BinaryPropertyListParser.parse(is); - String name = rootDict.objectForKey("cmd").toString(); - Log.i(TAG, "Cmd: "+name); - return rootDict; - - } catch(Exception ex) { - Log.i(TAG, "Error parsing BinaryPLIST: "+ex.toString()); - ex.printStackTrace(); - } - return null; - } - - private static boolean isKeepAlive(byte[] header) { - for(int i = 0; i < header.length; ++i) { - if((header[i] & 0xff) != 0) { - return false; - } - } - return true; - } - - public static int processHeader(byte[] header) { - - int length = 0; - - if(isKeepAlive(header)) { - Log.v(TAG, "Keepalive Recieved"); - return length; + } catch(Exception ex) { + Log.i(TAG, "ERROR: "+ex.toString()); + ex.printStackTrace(); + } + return null; } - try { - ByteBuffer bb = ByteBuffer.wrap(header); - bb.order(ByteOrder.BIG_ENDIAN); // CocosBuilder transmits in Little Endian - - length = bb.getInt(); + public static NSDictionary parseBinaryPLIST(byte[] is) { + try { + NSDictionary rootDict = (NSDictionary)BinaryPropertyListParser.parse(is); + String name = rootDict.objectForKey("cmd").toString(); + Log.i(TAG, "Cmd: "+name); + return rootDict; - } catch (Exception e) { - Log.i(TAG, "Exception: "+e.toString()); - e.printStackTrace(); + } catch(Exception ex) { + Log.i(TAG, "Error parsing BinaryPLIST: "+ex.toString()); + ex.printStackTrace(); + } + return null; } - Log.i(TAG, "Payload Length: "+length); - return length; - } - - - public static String getDeviceInfo() { - try { - NSDictionary root = new NSDictionary(); - root.put("cmd", "deviceinfo"); - root.put("devicename", android.os.Build.MODEL); - root.put("devicetype", "Android"); - root.put("preferredresourcetype", resolution); - String payload = root.toXMLPropertyList(); - - // String data = new String(header, 0 , header.length); - return payload; - } catch(Exception e) { + private static boolean isKeepAlive(byte[] header) { + for(int i = 0; i < header.length; ++i) { + if((header[i] & 0xff) != 0) { + return false; + } + } + return true; } - return null; - } - public static void setDeviceResolution(String res) { - CocosPlayerSocket server = new CocosPlayerSocket(); - Log.i(TAG, "Starting with resolution: "+res); - resolution = res; - server.setContext(CocosPlayer.c); - server.createServer(); - } + public static int processHeader(byte[] header) { + + int length = 0; + + if(isKeepAlive(header)) { + Log.v(TAG, "Keepalive Recieved"); + return length; + } + + try { + ByteBuffer bb = ByteBuffer.wrap(header); + bb.order(ByteOrder.BIG_ENDIAN); // CocosBuilder transmits in Little Endian + + length = bb.getInt(); + + } catch (Exception e) { + Log.i(TAG, "Exception: "+e.toString()); + e.printStackTrace(); + } + + Log.i(TAG, "Payload Length: "+length); + return length; + } + + public static void sendString(String data, Socket client, PrintWriter out) { + try { + byte[] header = getHeader(data); + Log.i(TAG, "Sending string "+data); + client.getOutputStream().write(header, 0, header.length); + out.write(data); + out.flush(); + } catch(Exception e) { + } + } + + public static byte[] getHeader(String data) { + byte[] header; + ByteBuffer b = ByteBuffer.allocate(4); + try { + b.order(ByteOrder.BIG_ENDIAN); + Log.i(TAG, "Payload Length: "+data.length()); + b.putInt(data.length()); + } catch(Exception e) { + } + header = b.array(); + + return header; + } + + public static String getDeviceInfo() { + try { + NSDictionary root = new NSDictionary(); + root.put("cmd", "deviceinfo"); + root.put("devicename", android.os.Build.MODEL); + root.put("devicetype", "Android"); + root.put("preferredresourcetype", resolution); + Random randomGenerator = new Random(); + root.put("uuid", ""+ randomGenerator.nextInt(100000)); + String payload = root.toXMLPropertyList(); + + // String data = new String(header, 0 , header.length); + return payload; + } catch(Exception e) { + } + return null; + } + + + public static String getFileSystem() { + try { + NSDictionary root = new NSDictionary(); + root.put("cmd", "filelist"); + NSDictionary fileList = new NSDictionary(); + root.put("filelist", fileList); + String payload = root.toXMLPropertyList(); + // String data = new String(header, 0 , header.length); + return payload; + } catch(Exception e) { + } + return null; + } + + + public static void setDeviceResolution(String res) { + CocosPlayerSocket server = new CocosPlayerSocket(); + Log.i(TAG, "Starting with resolution: "+res); + resolution = res; + server.setContext(CocosPlayer.c); + server.createServer(); + } } \ No newline at end of file diff --git a/samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CocosPlayer.java b/samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CocosPlayer.java index 94d32cebbb..e70c0c6b0e 100644 --- a/samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CocosPlayer.java +++ b/samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CocosPlayer.java @@ -26,6 +26,7 @@ package org.cocos2dx.cocosplayer; import org.cocos2dx.lib.Cocos2dxActivity; import android.os.Bundle; +import android.app.Activity; import android.content.Intent; import android.content.ContextWrapper; import android.content.Context; @@ -33,12 +34,20 @@ import android.content.Context; public class CocosPlayer extends Cocos2dxActivity{ public static Context c; - protected void onCreate(Bundle savedInstanceState){ - super.onCreate(savedInstanceState); - c = getApplicationContext(); + + public static Activity activity; + + public static void setOrientation(int orient) { + activity.setRequestedOrientation(orient); + } + + protected void onCreate(Bundle savedInstanceState){ + super.onCreate(savedInstanceState); + activity = this; + c = getApplicationContext(); } static { - System.loadLibrary("cocosplayer"); + System.loadLibrary("cocosplayer"); } } diff --git a/samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CocosPlayerSocket.java b/samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CocosPlayerSocket.java index 1597f42323..4ab7a66d79 100644 --- a/samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CocosPlayerSocket.java +++ b/samples/Javascript/CocosPlayer/proj.android/src/org/cocos2dx/cocosplayer/CocosPlayerSocket.java @@ -1,5 +1,7 @@ package org.cocos2dx.cocosplayer; import org.cocos2dx.lib.Cocos2dxGLSurfaceView; +import org.cocos2dx.lib.Cocos2dxActivity; +import org.cocos2dx.cocosplayer.CocosPlayer; import java.util.Arrays; import java.io.BufferedReader; @@ -15,6 +17,7 @@ import android.os.IBinder; import android.util.Log; import android.content.ContextWrapper; import android.content.Context; +import android.content.pm.ActivityInfo; import android.os.Environment; import android.os.AsyncTask; import java.io.OutputStream; @@ -23,6 +26,7 @@ import java.io.InputStream; import java.io.ByteArrayOutputStream; import java.io.File; +import com.dd.plist.NSArray; import com.dd.plist.NSDictionary; import com.dd.plist.NSData; import com.dd.plist.UID; @@ -32,262 +36,262 @@ import java.nio.ByteOrder; public class CocosPlayerSocket { - public static final String TAG = CocosPlayerSocket.class.getSimpleName(); - private static boolean running = false; - private static Context cw = null; + public static final String TAG = CocosPlayerSocket.class.getSimpleName(); + private static boolean running = false; + private static Context cw = null; - private HashMap mConnections; - private static ServerSocket server; - private static int mPairingCode = -1; - private static CocosPlayerPresence presence = null; + private HashMap mConnections; + private static ServerSocket server; + private static int mPairingCode = -1; + private static CocosPlayerPresence presence = null; - private void runCCB() { - Cocos2dxGLSurfaceView.getInstance().queueEvent(new Runnable() { - @Override - public void run() { - nativeRunCCB(); - } - }); - } + private void runCCB() { + Cocos2dxGLSurfaceView.getInstance().queueEvent(new Runnable() { + @Override + public void run() { + nativeRunCCB(); + } + }); + } - private void handleConnected() { - nativeConnected(); - } + private void handleConnected() { + nativeConnected(); + } - private void handleDisconnected() { - nativeDisconnected(); - } + private void handleDisconnected() { + nativeDisconnected(); + } - private void stopCCB() { - nativeStopCCB(); - } - - private void runScript(String script) { - nativeRunScript(script); - } + private void stopCCB() { + nativeStopCCB(); + } - private static native void nativeRunCCB(); - private static native void nativeConnected(); - private static native void nativeDisconnected(); - private static native void nativeStopCCB(); - private static native void nativeRunScript(final String script); + private void runScript(String script) { + nativeRunScript(script); + } - private void switchCmd(NSDictionary data) { - try { - String cmd = data.objectForKey("cmd").toString(); - String [] keys = data.allKeys(); - for(int i =0 ; i < keys.length; ++i ) { - } - if(cmd.equalsIgnoreCase("zip")) { - cleanCache(); + private static native void nativeRunCCB(); + private static native void nativeConnected(); + private static native void nativeDisconnected(); + private static native void nativeStopCCB(); + private static native void nativeRunScript(final String script); + + private static void setOrientation(String isPortrait) { + CocosPlayer.setOrientation(isPortrait.equalsIgnoreCase("true") ? + ActivityInfo.SCREEN_ORIENTATION_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); + } + + private void switchCmd(NSDictionary data) { try { - Log.i(TAG, "Size of NSDATA payload: "+((NSData)data.objectForKey("data")).bytes().length); - CCBFileUtilsHelper.unzipCCB(((NSData)data.objectForKey("data")).bytes(), cw); + String cmd = data.objectForKey("cmd").toString(); + String [] keys = data.allKeys(); + for(int i =0 ; i < keys.length; ++i ) { + } + if(cmd.equalsIgnoreCase("zip")) { + cleanCache(); + try { + Log.i(TAG, "Size of NSDATA payload: "+((NSData)data.objectForKey("data")).bytes().length); + CCBFileUtilsHelper.unzipCCB(((NSData)data.objectForKey("data")).bytes(), cw); + } catch(Exception e) { + Log.i(TAG, "Size of UID payload: "+((UID)data.objectForKey("data")).getBytes().length); + CCBFileUtilsHelper.unzipCCB(((UID)data.objectForKey("data")).getBytes(), cw); + } + } else if(cmd.equalsIgnoreCase("run")) { + runCCB(); + } else if(cmd.equalsIgnoreCase("stop")) { + stopCCB(); + } else if(cmd.equalsIgnoreCase("script")) { + runScript(data.objectForKey("script").toString()); + } else if(cmd.equalsIgnoreCase("settings")) { + NSArray orient = (NSArray)data.objectForKey("orientations"); + setOrientation(orient.objectAtIndex(0).toString()); + } } catch(Exception e) { - Log.i(TAG, "Size of UID payload: "+((UID)data.objectForKey("data")).getBytes().length); - CCBFileUtilsHelper.unzipCCB(((UID)data.objectForKey("data")).getBytes(), cw); - } - } else if(cmd.equalsIgnoreCase("run")) { - runCCB(); - } else if(cmd.equalsIgnoreCase("stop")) { - stopCCB(); - } else if(cmd.equalsIgnoreCase("script")) { - runScript(data.objectForKey("script").toString()); - } - } catch(Exception e) { - Log.i(TAG, "JSON Error: "+e.toString()); - e.printStackTrace(); - } - } - - private static void cleanDir(File dir) { - - long bytesDeleted = 0; - File[] files = dir.listFiles(); - - for (File file : files) { - if(file.isDirectory()) { - cleanDir(file); - continue; - } - Log.i(TAG, "Deleting file: "+file.getName()); - bytesDeleted += file.length(); - file.delete(); - } - } - - public static void cleanCache() { - File path = new File(CCBFileUtilsHelper.getBaseDirectory(cw)); - cleanDir(path); - } - - private void parsePayload(byte[] b) { - try { - NSDictionary rootDict = CCBStreamHandler.parseBinaryPLIST(b); - switchCmd(rootDict); - } catch(Exception e) { - Log.d(TAG, "Error writing payload to file"); - } - } - - private class StreamHandler extends AsyncTask { - - private void processPayload(final Socket client,final int length) { - - try { - final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - - int dataLength = ((length/4) > 16000) ? 16000 : (length/4); - byte[] data = new byte[dataLength]; - - int totalRead = 0, nRead; - while ((nRead = client.getInputStream().read(data, 0, ((length - totalRead) > data.length) ? data.length : (length - totalRead))) != -1 && totalRead < length) { - totalRead += nRead; - buffer.write(data, 0, nRead); - } - - Log.i(TAG, "Parsing payload. Totaly bytes read: "+totalRead); - - data = null; - - publishProgress(buffer); - - buffer.flush(); - buffer.close(); - - } catch (Exception e) { - Log.i(TAG, "Exception: "+e.toString()); - e.printStackTrace(); - } - - } - - protected Void doInBackground(ServerSocket... args) { - try { - - handleDisconnected(); - ServerSocket server = args[0]; - while(true) { - - Socket client = server.accept(); - - Log.i(TAG,"New connection from "+ client.getInetAddress()); - handleConnected(); - - if(client == null) { - Log.i(TAG, "Client null"); - break; - } - - try { - - String data = CCBStreamHandler.getDeviceInfo(); - ByteBuffer b = ByteBuffer.allocate(4); - b.order(ByteOrder.BIG_ENDIAN); - Log.i(TAG, "Payload Length: "+data.length()); - b.putInt(data.length()); - byte[] header = b.array(); - - Log.i(TAG, "Sending name "+data); - - client.getOutputStream().write(header, 0 , header.length); - PrintWriter out = new PrintWriter(client.getOutputStream(), true); - out.write(data); - out.flush(); - - processStream(client); - - } catch (Exception e) { - Log.i(TAG, "Server socket start fail!"); + Log.i(TAG, "JSON Error: "+e.toString()); e.printStackTrace(); - } } - Log.i(TAG, "Server Closing"); - server.close(); - - } catch(Exception e) { - } - return null; } - - private void processStream(Socket client) { - try { - - int nRead, count = 0; - - byte[] header = new byte[4]; - - while ((nRead = client.getInputStream().read(header, 0, header.length)) != -1) { - - int lengthToRead = CCBStreamHandler.processHeader(header); - - if(lengthToRead > 0) { - processPayload(client, lengthToRead); - } + + private static void cleanDir(File dir) { + + long bytesDeleted = 0; + File[] files = dir.listFiles(); + + for (File file : files) { + if(file.isDirectory()) { + cleanDir(file); + continue; + } + Log.i(TAG, "Deleting file: "+file.getName()); + bytesDeleted += file.length(); + file.delete(); } - - } catch (Exception e) { - Log.i(TAG, "Exception: "+e.toString()); - e.printStackTrace(); - } - } - - /** The system calls this to perform work in the UI thread and delivers - * the result from doInBackground() */ - protected void onPostExecute() { - try { - server.close(); - } catch(Exception e) { - } } - protected void onProgressUpdate(ByteArrayOutputStream... buffer) { - try { - byte[] b = buffer[0].toByteArray(); - buffer[0].close(); - System.gc(); - parsePayload(b); - } catch(Exception e) { - } + public static void cleanCache() { + File path = new File(CCBFileUtilsHelper.getBaseDirectory(cw)); + cleanDir(path); } - } - - public void setContext(Context c) { - cw = c; - } - - public static void setPairingCode(int code) { - //mPresenceAsync.cancel(true); - if(presence != null) { - presence.unregisterService(); + private void parsePayload(byte[] b) { + try { + NSDictionary rootDict = CCBStreamHandler.parseBinaryPLIST(b); + switchCmd(rootDict); + } catch(Exception e) { + Log.d(TAG, "Error writing payload to file"); + } } - mPairingCode = code; + private class StreamHandler extends AsyncTask { - presence = new CocosPlayerPresence(); - Log.i("CocosPlayerSocket", "Registering Bonjour on Port: "+server.getLocalPort()+" With pairing code: "+code); - presence.setContext(cw); - presence.startPresence(server.getLocalPort(), mPairingCode); + private void processPayload(final Socket client,final int length) { - //new PresenceStarter().execute(server.getLocalPort(), mPairingCode); - } + try { + final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - public void createServer() { - - Log.i(TAG, "Creating server "+running); - try { - server = new ServerSocket(0); - Log.i(TAG, "IP " + server.getInetAddress() - + ", running on port " + server.getLocalPort()); - - presence = new CocosPlayerPresence(); - presence.setContext(cw); - presence.startPresence(server.getLocalPort(), mPairingCode); - new StreamHandler().execute(server); + int dataLength = ((length/4) > 16000) ? 16000 : (length/4); + byte[] data = new byte[dataLength]; - } catch(Exception e) { + int totalRead = 0, nRead; + while ((nRead = client.getInputStream().read(data, 0, ((length - totalRead) > data.length) ? data.length : (length - totalRead))) != -1 && totalRead < length) { + totalRead += nRead; + buffer.write(data, 0, nRead); + } + + Log.i(TAG, "Parsing payload. Totaly bytes read: "+totalRead); + + data = null; + + publishProgress(buffer); + + buffer.flush(); + buffer.close(); + + } catch (Exception e) { + Log.i(TAG, "Exception: "+e.toString()); + e.printStackTrace(); + } + + } + + protected Void doInBackground(ServerSocket... args) { + try { + + handleDisconnected(); + ServerSocket server = args[0]; + while(true) { + + Socket client = server.accept(); + + Log.i(TAG,"New connection from "+ client.getInetAddress()); + handleConnected(); + + if(client == null) { + Log.i(TAG, "Client null"); + break; + } + try { + + // Send deviceInfo and filelist + PrintWriter out = new PrintWriter(client.getOutputStream(), true); + CCBStreamHandler.sendString(CCBStreamHandler.getDeviceInfo(), client, out); + CCBStreamHandler.sendString(CCBStreamHandler.getFileSystem(), client, out); + //out.close(); + + // Process CocosBuilder input data stream + processStream(client); + + } catch (Exception e) { + Log.i(TAG, "Server socket start fail!"); + e.printStackTrace(); + } + } + Log.i(TAG, "Server Closing"); + server.close(); + + } catch(Exception e) { + } + return null; + } + + private void processStream(Socket client) { + try { + + int nRead, count = 0; + + byte[] header = new byte[4]; + + while ((nRead = client.getInputStream().read(header, 0, header.length)) != -1) { + + int lengthToRead = CCBStreamHandler.processHeader(header); + + if(lengthToRead > 0) { + processPayload(client, lengthToRead); + } + } + + } catch (Exception e) { + Log.i(TAG, "Exception: "+e.toString()); + e.printStackTrace(); + } + } + + /** The system calls this to perform work in the UI thread and delivers + * the result from doInBackground() */ + protected void onPostExecute() { + try { + server.close(); + } catch(Exception e) { + } + } + + protected void onProgressUpdate(ByteArrayOutputStream... buffer) { + try { + byte[] b = buffer[0].toByteArray(); + buffer[0].close(); + System.gc(); + parsePayload(b); + } catch(Exception e) { + } + } + } + + + public void setContext(Context c) { + cw = c; + } + + public static void setPairingCode(int code) { + //mPresenceAsync.cancel(true); + if(presence != null) { + presence.unregisterService(); + } + mPairingCode = code; + + + presence = new CocosPlayerPresence(); + Log.i("CocosPlayerSocket", "Registering Bonjour on Port: "+server.getLocalPort()+" With pairing code: "+code); + presence.setContext(cw); + presence.startPresence(server.getLocalPort(), mPairingCode); + + //new PresenceStarter().execute(server.getLocalPort(), mPairingCode); + } + + public void createServer() { + + Log.i(TAG, "Creating server "+running); + try { + server = new ServerSocket(0); + Log.i(TAG, "IP " + server.getInetAddress() + + ", running on port " + server.getLocalPort()); + + presence = new CocosPlayerPresence(); + presence.setContext(cw); + presence.startPresence(server.getLocalPort(), mPairingCode); + new StreamHandler().execute(server); + + } catch(Exception e) { + } } - } } \ No newline at end of file