Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions FileSystemSharing.iml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
<module external.linked.project.id="FileSystemSharing" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="java-gradle" name="Java-Gradle">
<configuration>
<option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
<option name="BUILDABLE" value="false" />
</configuration>
</facet>
</component>
Expand All @@ -15,5 +16,4 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

</module>
8 changes: 4 additions & 4 deletions app/app.iml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="FileSystemSharing" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
<module external.linked.project.id=":app" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="FileSystemSharing" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="android-gradle" name="Android-Gradle">
<configuration>
Expand All @@ -12,8 +12,9 @@
<option name="SELECTED_TEST_ARTIFACT" value="_android_test_" />
<option name="ASSEMBLE_TASK_NAME" value="assembleDebug" />
<option name="COMPILE_JAVA_TASK_NAME" value="compileDebugSources" />
<option name="ASSEMBLE_TEST_TASK_NAME" value="assembleDebugAndroidTest" />
<option name="SOURCE_GEN_TASK_NAME" value="generateDebugSources" />
<option name="ASSEMBLE_TEST_TASK_NAME" value="assembleDebugAndroidTest" />
<option name="COMPILE_JAVA_TEST_TASK_NAME" value="compileDebugAndroidTestSources" />
<option name="TEST_SOURCE_GEN_TASK_NAME" value="generateDebugAndroidTestSources" />
<option name="ALLOW_USER_CONFIGURATION" value="false" />
<option name="MANIFEST_FILE_RELATIVE_PATH" value="/src/main/AndroidManifest.xml" />
Expand Down Expand Up @@ -89,5 +90,4 @@
<orderEntry type="library" exported="" name="android-query-full.0.26.8" level="project" />
<orderEntry type="library" exported="" name="gson-2.3.1-javadoc" level="project" />
</component>
</module>

</module>
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ android {

defaultConfig {
applicationId "com.example.vivek.filesystemsharing"
minSdkVersion 21
minSdkVersion 16
targetSdkVersion 21
versionCode 1
versionName "1.0"
versionCode 2
versionName "2.0"
}
buildTypes {
release {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/mtp/connection/manager/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +30,6 @@ public Device(String ip, Boolean isSharingFS){

@Override
public String toString(){
return ip+": FSSharing-"+ Boolean.toString(isSharingFS);
return ip;
}
}
31 changes: 30 additions & 1 deletion app/src/main/java/com/mtp/connection/manager/DeviceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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;
}
}


10 changes: 9 additions & 1 deletion app/src/main/java/com/mtp/filesystemsharing/FileAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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!!!

Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/com/mtp/filesystemsharing/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
public class ExternalFSManager {
Gson gson;
public MyFile root;
public int id = -1;

public ExternalFSManager(String fs) {
gson = new Gson();
Expand All @@ -32,17 +33,18 @@ public synchronized void logChanges(String log) {
}.getType();
ArrayList<Snapshot> fsSnapshots = gson.fromJson(log, collectionType);
for (Snapshot snap : fsSnapshots) {
id = snap.snapshot_id;
for (Changes cha : snap.change) {
applyChange(cha);
}
}
}

/* 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);
Expand All @@ -57,7 +59,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) {
Expand Down
42 changes: 34 additions & 8 deletions app/src/main/java/com/mtp/fsmanager/internal/FSLogger.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -21,6 +25,7 @@ public class FSLogger {

private ArrayList<Snapshot> fsSnapshots = new ArrayList<Snapshot>();

private boolean activeInLastUpdate = false;
public FSLogger() {
idAllocator = new FSIdAllocator();
gson = new Gson();
Expand All @@ -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<Snapshot> l = fsSnapshots.subList(prev_snapID + 1, fsSnapshots.size());
String result = gson.toJson(l);
Log.d("logger serialization", result);
return result;
}

Expand All @@ -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()));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class LocalFSManager {

public MyFile root;

private FSLogger logger;
public FSLogger logger;

private int count;

Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
}
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/com/mtp/transmission/FSMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
39 changes: 33 additions & 6 deletions app/src/main/java/com/mtp/transmission/MessageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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");
}
Expand Down
Loading