From b301ad42bb5943afe663b512c084d394c6622207 Mon Sep 17 00:00:00 2001 From: mittalvivek07 Date: Mon, 8 Jun 2015 20:52:00 +0530 Subject: [PATCH 1/4] minor bug in sending changes, UI change --- .../main/java/com/mtp/connection/manager/Device.java | 2 +- .../java/com/mtp/filesystemsharing/FileAdapter.java | 10 +++++++++- .../com/mtp/fsmanager/external/ExternalFSManager.java | 2 +- app/src/main/res/layout/devicelist.xml | 1 + 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/mtp/connection/manager/Device.java b/app/src/main/java/com/mtp/connection/manager/Device.java index b5a3e2d..f1a0fba 100644 --- a/app/src/main/java/com/mtp/connection/manager/Device.java +++ b/app/src/main/java/com/mtp/connection/manager/Device.java @@ -29,6 +29,6 @@ public Device(String ip, Boolean isSharingFS){ @Override public String toString(){ - return ip+": FSSharing-"+ Boolean.toString(isSharingFS); + return ip; } } diff --git a/app/src/main/java/com/mtp/filesystemsharing/FileAdapter.java b/app/src/main/java/com/mtp/filesystemsharing/FileAdapter.java index 9e09075..0cf3961 100644 --- a/app/src/main/java/com/mtp/filesystemsharing/FileAdapter.java +++ b/app/src/main/java/com/mtp/filesystemsharing/FileAdapter.java @@ -203,7 +203,15 @@ public View getView(int position, View convertView, ViewGroup parent) { } MyFile item = getItem(position); TextView tv = (TextView)v.findViewById(R.id.grid_text); - tv.setText(item.name); + if(isRoot & position > 0){ + Device dev = MainActivity.deviceManager.getDevice(extFsManagers.get(position-1)); + String name = dev.ip; + String s[] = name.split("\\."); + tv.setText(s[2]+"."+s[3]+":"+item.name); + } + else { + tv.setText(item.name); + } // /tv.setHeight(10); // Add The Image!!! diff --git a/app/src/main/java/com/mtp/fsmanager/external/ExternalFSManager.java b/app/src/main/java/com/mtp/fsmanager/external/ExternalFSManager.java index 4f74e97..fb25553 100644 --- a/app/src/main/java/com/mtp/fsmanager/external/ExternalFSManager.java +++ b/app/src/main/java/com/mtp/fsmanager/external/ExternalFSManager.java @@ -57,7 +57,7 @@ private void applyChange(Changes change) { MyFile f = new MyFile(); f.isDirectory = (change.event & Changes.ISDIR) > 0 ? true : false; f.name = filename; - f.path = change.path; + f.path = parent.path+"/"+f.name; f.parent = parent; parent.child.add(f); } else if ((change.event & Changes.DELETED) > 0) { diff --git a/app/src/main/res/layout/devicelist.xml b/app/src/main/res/layout/devicelist.xml index 538eb9b..d6009e1 100644 --- a/app/src/main/res/layout/devicelist.xml +++ b/app/src/main/res/layout/devicelist.xml @@ -4,5 +4,6 @@ android:layout_height="fill_parent" android:padding="20dp" android:textSize="18sp" + android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="#800080"> \ No newline at end of file From ee7b50c69a6f13be4d78d6930a660f41f02e2be4 Mon Sep 17 00:00:00 2001 From: vivek07 Date: Wed, 17 Jun 2015 17:36:58 +0530 Subject: [PATCH 2/4] active/inactive state --- .../com/mtp/connection/manager/Device.java | 1 + .../mtp/connection/manager/DeviceManager.java | 31 +++++++++++++- .../mtp/filesystemsharing/MainActivity.java | 16 +++++++ .../fsmanager/external/ExternalFSManager.java | 6 ++- .../com/mtp/fsmanager/internal/FSLogger.java | 42 +++++++++++++++---- .../fsmanager/internal/LocalFSManager.java | 6 +-- .../java/com/mtp/transmission/FSMessage.java | 7 +++- .../com/mtp/transmission/MessageHandler.java | 39 ++++++++++++++--- 8 files changed, 126 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/mtp/connection/manager/Device.java b/app/src/main/java/com/mtp/connection/manager/Device.java index f1a0fba..99eba3e 100644 --- a/app/src/main/java/com/mtp/connection/manager/Device.java +++ b/app/src/main/java/com/mtp/connection/manager/Device.java @@ -21,6 +21,7 @@ public class Device { public ClientConnectionManager conToClient = null; public SocketServerReplyThread conToServer = null; public ExternalFSManager extFs = null; + public boolean isActive = true; public Device(String ip, Boolean isSharingFS){ this.ip = ip; diff --git a/app/src/main/java/com/mtp/connection/manager/DeviceManager.java b/app/src/main/java/com/mtp/connection/manager/DeviceManager.java index 0f9eac5..b2e7cf9 100644 --- a/app/src/main/java/com/mtp/connection/manager/DeviceManager.java +++ b/app/src/main/java/com/mtp/connection/manager/DeviceManager.java @@ -107,7 +107,28 @@ public synchronized void addToAdaptor(FileAdapter adap){ public synchronized void sendUpdates(FSMessage msg){ for(Device dev: deviceList){ - dev.conToServer.sendMsg(msg); + if(dev.conToServer == null) + continue; + if(dev.isActive) + dev.conToServer.sendMsg(msg); + } + } + public synchronized void changeToInactive(FSMessage msg){ + + for(Device dev: deviceList){ + if(dev.conToClient == null) + continue; + + dev.conToClient.sendMessage(msg); + } + } + public synchronized void changeToActive(){ + + for(Device dev: deviceList){ + if(dev.conToClient == null) + continue; + FSMessage msg = new FSMessage(FSMessage.BEACTIVE,Integer.toString(dev.extFs.id)); + dev.conToClient.sendMessage(msg); } } @@ -130,6 +151,14 @@ public Device getDevice(ExternalFSManager ext){ Log.e("Remove ServConn","device not found"); return null; } + + public synchronized boolean activeDevicesPresent(){ + for(Device dev:deviceList){ + if(dev.isActive) + return true; + } + return false; + } } diff --git a/app/src/main/java/com/mtp/filesystemsharing/MainActivity.java b/app/src/main/java/com/mtp/filesystemsharing/MainActivity.java index c5184ad..cff98a5 100644 --- a/app/src/main/java/com/mtp/filesystemsharing/MainActivity.java +++ b/app/src/main/java/com/mtp/filesystemsharing/MainActivity.java @@ -26,6 +26,7 @@ import com.mtp.fsmanager.external.ExternalFSManager; import com.mtp.fsmanager.internal.FSService; import com.mtp.fsmanager.internal.LocalFSManager; +import com.mtp.transmission.FSMessage; import java.util.ArrayList; @@ -89,6 +90,21 @@ public void onBackPressed(){ } } + @Override + protected void onPause(){ + super.onPause(); + if(deviceManager == null) + return; + deviceManager.changeToInactive(new FSMessage(FSMessage.BEINACTIVE,"")); + } + + @Override + protected void onResume(){ + super.onResume(); + if(deviceManager == null) + return; + deviceManager.changeToActive(); + } @Override diff --git a/app/src/main/java/com/mtp/fsmanager/external/ExternalFSManager.java b/app/src/main/java/com/mtp/fsmanager/external/ExternalFSManager.java index fb25553..59b990a 100644 --- a/app/src/main/java/com/mtp/fsmanager/external/ExternalFSManager.java +++ b/app/src/main/java/com/mtp/fsmanager/external/ExternalFSManager.java @@ -20,6 +20,7 @@ public class ExternalFSManager { Gson gson; public MyFile root; + public int id = -1; public ExternalFSManager(String fs) { gson = new Gson(); @@ -32,6 +33,7 @@ public synchronized void logChanges(String log) { }.getType(); ArrayList fsSnapshots = gson.fromJson(log, collectionType); for (Snapshot snap : fsSnapshots) { + id = snap.snapshot_id; for (Changes cha : snap.change) { applyChange(cha); } @@ -39,10 +41,10 @@ public synchronized void logChanges(String log) { } /* single change */ - public synchronized void logChange(String c){ + /* public synchronized void logChange(String c){ Changes ch = gson.fromJson(c, Changes.class); applyChange(ch); - } + }*/ private void applyChange(Changes change) { File r = new File(change.path); diff --git a/app/src/main/java/com/mtp/fsmanager/internal/FSLogger.java b/app/src/main/java/com/mtp/fsmanager/internal/FSLogger.java index fe2cefd..c169213 100644 --- a/app/src/main/java/com/mtp/fsmanager/internal/FSLogger.java +++ b/app/src/main/java/com/mtp/fsmanager/internal/FSLogger.java @@ -1,11 +1,15 @@ package com.mtp.fsmanager.internal; +import android.util.Log; + import com.google.gson.Gson; +import com.mtp.filesystemsharing.MainActivity; import com.mtp.transmission.FSMessage; import com.mtp.transmission.MessageHandler; import java.io.Serializable; import java.util.ArrayList; +import java.util.List; /** * Created by vivek on 30/4/15. @@ -21,6 +25,7 @@ public class FSLogger { private ArrayList fsSnapshots = new ArrayList(); + private boolean activeInLastUpdate = false; public FSLogger() { idAllocator = new FSIdAllocator(); gson = new Gson(); @@ -36,8 +41,9 @@ public String serialize(int prev_snapID) { if (prev_snapID == idAllocator.getFSId()) return null; - - String result = gson.toJson(fsSnapshots.subList(prev_snapID + 1, fsSnapshots.size())); + List l = fsSnapshots.subList(prev_snapID + 1, fsSnapshots.size()); + String result = gson.toJson(l); + Log.d("logger serialization", result); return result; } @@ -48,14 +54,34 @@ public void deserialize(String changes) { public void addLog(MyFile file, int event) { int id = idAllocator.getFSId(); - //TODO check if there are active devices. If true then "id=idAllocator.incrementFSId()" - // Now there ar no active inactive devices so no need to increment Changes change = new Changes(event, file.path); - Snapshot snap = fsSnapshots.get(id); - snap.change.add(change); + if(!MainActivity.deviceManager.activeDevicesPresent()){ + // Now there ar no active inactive devices so no need to increment + Snapshot snap = fsSnapshots.get(id); + if(activeInLastUpdate) { + id = idAllocator.incrementFSId(); + snap = new Snapshot(); + snap.snapshot_id = id; + fsSnapshots.add(snap); + } + activeInLastUpdate = false; + + snap.change.add(change); + + } + else { + + //TODO check if there are active devices. If true then "id=idAllocator.incrementFSId()" + id = idAllocator.incrementFSId(); + activeInLastUpdate = true; + Snapshot snap = new Snapshot(); + snap.snapshot_id = id; + fsSnapshots.add(snap); + snap.change.add(change); + MainActivity.deviceManager.sendUpdates(new FSMessage(FSMessage.CHANGES, serialize(id-1))); + + } - MessageHandler msgHandler = new MessageHandler(); - msgHandler.respond(new FSMessage(FSMessage.CHANGE, change.serialize())); } diff --git a/app/src/main/java/com/mtp/fsmanager/internal/LocalFSManager.java b/app/src/main/java/com/mtp/fsmanager/internal/LocalFSManager.java index 00be3ed..2f757b8 100644 --- a/app/src/main/java/com/mtp/fsmanager/internal/LocalFSManager.java +++ b/app/src/main/java/com/mtp/fsmanager/internal/LocalFSManager.java @@ -17,7 +17,7 @@ public class LocalFSManager { public MyFile root; - private FSLogger logger; + public FSLogger logger; private int count; @@ -136,7 +136,7 @@ public synchronized void create(MyFile parent, String file, boolean isdir) { logger.addLog(f, event); - Log.d("changes ", logger.serialize(-1)); + // Log.d("changes ", logger.serialize(-1)); } public synchronized void delete(MyFile parent, String file) { @@ -147,7 +147,7 @@ public synchronized void delete(MyFile parent, String file) { if (child.name.equals(file)) { childList.remove(child); logger.addLog(child, Changes.DELETED); - Log.d("changes ", logger.serialize(-1)); + // Log.d("changes ", logger.serialize(-1)); return; } } diff --git a/app/src/main/java/com/mtp/transmission/FSMessage.java b/app/src/main/java/com/mtp/transmission/FSMessage.java index 32eb235..589a1ea 100644 --- a/app/src/main/java/com/mtp/transmission/FSMessage.java +++ b/app/src/main/java/com/mtp/transmission/FSMessage.java @@ -12,11 +12,14 @@ public class FSMessage implements Serializable { /* To be sent by Client 1-20*/ final public static transient int REQUESTFS = 1; final public static transient int REQUESTFILE = 2; + final public static transient int BEINACTIVE = 3; + final public static transient int BEACTIVE = 4; /* To be sent by Server 21+ */ final public static transient int LOCALFS = 21; - final public static transient int CHANGE = 22; - final public static transient int REQUESTEDFILE = 23; + //final public static transient int CHANGE = 22; + final public static transient int CHANGES = 23; + //TODO need to ensure that this pattern is not present in the data being sent. need to find other initialization /* this is to be sent with every message to mark its end */ diff --git a/app/src/main/java/com/mtp/transmission/MessageHandler.java b/app/src/main/java/com/mtp/transmission/MessageHandler.java index ce225b5..10b30c7 100644 --- a/app/src/main/java/com/mtp/transmission/MessageHandler.java +++ b/app/src/main/java/com/mtp/transmission/MessageHandler.java @@ -44,14 +44,14 @@ public MessageHandler(){ respond(msg); }*/ - public void respond(FSMessage msg){ - if(msg.msgType == FSMessage.CHANGE){ + /* public void respond(FSMessage msg){ + if(msg.msgType == FSMessage.CHANGES){ MainActivity.deviceManager.sendUpdates(msg); }else { Log.e("msg Handler","wrong msg"); } - } + }*/ private class ExtFS implements Runnable{ private ExternalFSManager extFS; @@ -91,7 +91,7 @@ public void respond(ClientListener client, FSMessage msg){ new Handler(Looper.getMainLooper()).post(new ExtFS(extFSMan)); break; - case FSMessage.CHANGE: + /*case FSMessage.CHANGE: Log.d("Handler", "change received"); Device dev = MainActivity.deviceManager.getDevice(client.serverIP); assert dev != null; @@ -102,8 +102,23 @@ public void run() { MainActivity.fileAdapter.refresh(); } }); - break; + break;*/ + case FSMessage.CHANGES: + //TODO handle multiple changes + Log.d("Handler", "change received"); + Device dev1 = MainActivity.deviceManager.getDevice(client.serverIP); + assert dev1 != null; + if(msg.msg == null) + break; + dev1.extFs.logChanges(msg.msg); + new Handler(Looper.getMainLooper()).post(new Runnable() { + @Override + public void run() { + MainActivity.fileAdapter.refresh(); + } + }); + break; /*case FSMessage.REQUESTEDFILE: Gson g = new Gson(); Log.d("file received",msg.msg); @@ -193,7 +208,19 @@ public void respond(SocketServerReplyThread server, FSMessage msg){ sendFile(msg,server.getIP()); break; - + case FSMessage.BEINACTIVE: + Device dev = MainActivity.deviceManager.getDevice(server.getIP()); + dev.isActive = false; + //TODO change state to inactive + break; + case FSMessage.BEACTIVE: + //message will contain the previous id + Device dev2 = MainActivity.deviceManager.getDevice(server.getIP()); + dev2.isActive = true; + FSMessage m1 = new FSMessage(FSMessage.CHANGES, MainActivity.fsManager.logger.serialize(Integer.valueOf(msg.msg))); + server.sendMsg(m1); + //TODO change state to active + break; default: Log.d("message handler:"+server.getName(),"unhandled message"); } From fe9d25ada247a2ecab0d72b8197a7aa8ccc2b62e Mon Sep 17 00:00:00 2001 From: atuljangra Date: Sat, 12 Sep 2015 15:33:10 +0530 Subject: [PATCH 3/4] Changed the miSDK version --- FileSystemSharing.iml | 6 +++--- app/app.iml | 8 ++++---- app/build.gradle | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/FileSystemSharing.iml b/FileSystemSharing.iml index 0bb6048..8242596 100644 --- a/FileSystemSharing.iml +++ b/FileSystemSharing.iml @@ -1,9 +1,10 @@ - + @@ -15,5 +16,4 @@ - - + \ No newline at end of file diff --git a/app/app.iml b/app/app.iml index 6c1d311..1ed9f07 100644 --- a/app/app.iml +++ b/app/app.iml @@ -1,5 +1,5 @@ - + @@ -12,8 +12,9 @@ - - + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index b81546a..2fe9158 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,7 +6,7 @@ android { defaultConfig { applicationId "com.example.vivek.filesystemsharing" - minSdkVersion 21 + minSdkVersion 16 targetSdkVersion 21 versionCode 1 versionName "1.0" From 684bb5a1649e704f54b8beb7d54dc15086a9f0ce Mon Sep 17 00:00:00 2001 From: atuljangra Date: Sat, 12 Sep 2015 15:40:06 +0530 Subject: [PATCH 4/4] Updated version --- app/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 2fe9158..1f85459 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,8 +8,8 @@ android { applicationId "com.example.vivek.filesystemsharing" minSdkVersion 16 targetSdkVersion 21 - versionCode 1 - versionName "1.0" + versionCode 2 + versionName "2.0" } buildTypes { release {