├── .gitignore ├── File Share ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── io │ │ └── github │ │ └── karuppiah7890 │ │ └── fileshare │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── io │ │ │ └── github │ │ │ └── karuppiah7890 │ │ │ └── fileshare │ │ │ ├── FileReceiver.java │ │ │ ├── FileSender.java │ │ │ ├── ReceiverService.java │ │ │ ├── ReceiverThread.java │ │ │ ├── SenderService.java │ │ │ └── SenderThread.java │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── io │ └── github │ └── karuppiah7890 │ └── fileshare │ └── ExampleUnitTest.java ├── FileSharerDemoApp ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── io │ │ └── github │ │ └── karuppiah7890 │ │ └── filesharer │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── io │ │ │ └── github │ │ │ └── karuppiah7890 │ │ │ └── filesharer │ │ │ ├── DashBoard.java │ │ │ ├── ReceiverActivity.java │ │ │ └── SenderActivity.java │ └── res │ │ ├── layout │ │ ├── activity_dash_board.xml │ │ ├── activity_receiver.xml │ │ └── activity_sender.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── io │ └── github │ └── karuppiah7890 │ └── filesharer │ └── ExampleUnitTest.java ├── LICENSE.md ├── README.md ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | .idea 4 | /local.properties 5 | /.idea/workspace.xml 6 | /.idea/libraries 7 | .DS_Store 8 | /build 9 | /captures 10 | .externalNativeBuild 11 | -------------------------------------------------------------------------------- /File Share/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /File Share/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.novoda.bintray-release' 3 | 4 | android { 5 | compileSdkVersion 25 6 | buildToolsVersion "25.0.0" 7 | 8 | defaultConfig { 9 | minSdkVersion 9 10 | targetSdkVersion 25 11 | versionCode 1 12 | versionName "1.0" 13 | 14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 15 | 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | } 24 | 25 | publish { 26 | groupId = 'io.github.karuppiah7890' 27 | artifactId = 'fileshare' 28 | publishVersion = '0.1.0' 29 | desc = 'An Android library to send and receive files among Android devices in a WiFi LAN.' 30 | licences = ['MIT'] 31 | uploadName='FileShare' 32 | website = 'https://github.com/Android-File-Share/FileShare' 33 | } 34 | 35 | dependencies { 36 | compile fileTree(dir: 'libs', include: ['*.jar']) 37 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 38 | exclude group: 'com.android.support', module: 'support-annotations' 39 | }) 40 | compile 'com.android.support:appcompat-v7:25.0.0' 41 | testCompile 'junit:junit:4.12' 42 | } 43 | -------------------------------------------------------------------------------- /File Share/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:\Users\3521\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /File Share/src/androidTest/java/io/github/karuppiah7890/fileshare/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package io.github.karuppiah7890.fileshare; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("io.github.karuppiah7890.fileshare.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /File Share/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 15 | 19 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /File Share/src/main/java/io/github/karuppiah7890/fileshare/FileReceiver.java: -------------------------------------------------------------------------------- 1 | package io.github.karuppiah7890.fileshare; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.os.Handler; 6 | import android.os.Messenger; 7 | import android.util.Log; 8 | 9 | import java.io.IOException; 10 | import java.net.ServerSocket; 11 | 12 | public class FileReceiver { 13 | 14 | private final boolean mDebug = true; 15 | 16 | private final int MIN_PORT_NUMBER = 1024; 17 | private final int MAX_PORT_NUMBER = 65536; 18 | private final String PORT = "PORT"; 19 | private final String MESSENGER = "MESSENGER"; 20 | 21 | // Constants start from 2001 22 | public static final int CODE = 2001; 23 | public static final int LISTENING = 2002; 24 | public static final int CONNECTED = 2003; 25 | public static final int RECEIVING_FILE = 2004; 26 | public static final int FILE_RECEIVED = 2005; 27 | public static final int RECEIVE_ERROR = 2006; 28 | 29 | private Context context; 30 | private Handler mHandler; 31 | 32 | private Intent i; 33 | 34 | public FileReceiver(Context context, Handler mHandler) { 35 | this.context = context; 36 | this.mHandler = mHandler; 37 | } 38 | 39 | private boolean isPortAvailable(int port) { 40 | 41 | boolean available; 42 | 43 | if (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER) { 44 | throw new IllegalArgumentException("Invalid port: " + port); 45 | } 46 | 47 | ServerSocket ss = null; 48 | 49 | try { 50 | ss = new ServerSocket(port); 51 | ss.setReuseAddress(true); 52 | available = true; 53 | } catch (IOException e) { 54 | available = false; 55 | } finally { 56 | 57 | if (ss != null) { 58 | try { 59 | ss.close(); 60 | } catch (IOException e) { 61 | 62 | } 63 | } 64 | } 65 | 66 | return available; 67 | } 68 | 69 | private int getRandomPort() { 70 | 71 | int port; 72 | 73 | do{ 74 | port = (int) (MIN_PORT_NUMBER + 1 + Math.floor(Math.random()*(MAX_PORT_NUMBER - MIN_PORT_NUMBER-1))); 75 | 76 | if(mDebug) 77 | Log.i("FileReceiver","Trying port : " + port); 78 | 79 | }while(!isPortAvailable(port)); 80 | 81 | return port; 82 | 83 | } 84 | 85 | public void getFile(){ 86 | 87 | int port = getRandomPort(); 88 | 89 | if(mDebug) 90 | Log.i("FileReceiver","Port : " + port); 91 | 92 | i = new Intent(context,ReceiverService.class); 93 | 94 | i.putExtra(PORT,port); 95 | i.putExtra(MESSENGER,new Messenger(mHandler)); 96 | 97 | context.startService(i); 98 | 99 | } 100 | 101 | public void close() { 102 | context.stopService(i); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /File Share/src/main/java/io/github/karuppiah7890/fileshare/FileSender.java: -------------------------------------------------------------------------------- 1 | package io.github.karuppiah7890.fileshare; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.net.DhcpInfo; 6 | import android.net.wifi.WifiManager; 7 | import android.os.Handler; 8 | import android.os.Messenger; 9 | import android.text.format.Formatter; 10 | import android.util.Log; 11 | 12 | import java.io.File; 13 | import java.net.InetAddress; 14 | 15 | public class FileSender { 16 | 17 | private final boolean mDebug = true; 18 | 19 | private final String PORT = "PORT"; 20 | private final String MESSENGER = "MESSENGER"; 21 | private final String FILE = "FILE"; 22 | private final String RECEIVER_IP = "RECEIVER_IP"; 23 | 24 | // Constants start from 1001 25 | public static final int CONNECTING = 1001; 26 | public static final int CONNECTED = 1002; 27 | public static final int SENDING_FILE = 1003; 28 | public static final int FILE_SENT = 1004; 29 | public static final int SEND_ERROR = 1005; 30 | 31 | private Context context; 32 | private int port; 33 | private Handler mHandler; 34 | 35 | private final WifiManager manager; 36 | private final DhcpInfo dhcp; 37 | 38 | Intent i; 39 | 40 | public FileSender(Context context, Handler mHandler) { 41 | this.context = context; 42 | this.mHandler = mHandler; 43 | manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 44 | dhcp = manager.getDhcpInfo(); 45 | } 46 | 47 | private InetAddress getMyIP() { 48 | final String address = Formatter.formatIpAddress(dhcp.ipAddress); // ipAddress - IP address of my device, assigned through dhcp 49 | InetAddress myIP = null; 50 | 51 | try { 52 | 53 | myIP = InetAddress.getByName(address); 54 | if(mDebug) 55 | Log.i("FileSender","My IP : " + myIP.toString()); 56 | 57 | } catch (Exception e) { 58 | if(mDebug) 59 | Log.e("FileSender","Cannot find my own IP. Error : " + e.toString()); 60 | } 61 | 62 | return myIP; 63 | } 64 | 65 | private InetAddress getReceiverIP() { 66 | final String address = Formatter.formatIpAddress(dhcp.gateway); // gateway - default gateway IP address 67 | InetAddress receiverIP = null; 68 | 69 | try { 70 | 71 | receiverIP = InetAddress.getByName(address); 72 | 73 | if(mDebug) 74 | Log.i("FileSender","Receiver IP : " + receiverIP.toString()); 75 | 76 | } catch (Exception e) { 77 | if(mDebug) 78 | Log.e("FileSender","Cannot find receiver's IP. Error : " + e.toString()); 79 | } 80 | 81 | return receiverIP; 82 | } 83 | 84 | public void sendFile(File file,int code) { 85 | 86 | if(!file.exists()){ 87 | mHandler.obtainMessage(SEND_ERROR,"File " + file.getName() + " doesn't exist").sendToTarget(); 88 | return; 89 | } 90 | 91 | if(!file.isFile()){ 92 | mHandler.obtainMessage(SEND_ERROR,file.getName() + " is a folder, not file").sendToTarget(); 93 | return; 94 | } 95 | 96 | this.port = code; 97 | 98 | InetAddress receiverIP = getReceiverIP(); 99 | 100 | i = new Intent(context,SenderService.class); 101 | 102 | i.putExtra(RECEIVER_IP,receiverIP); 103 | i.putExtra(PORT,port); 104 | i.putExtra(MESSENGER,new Messenger(mHandler)); 105 | i.putExtra(FILE,file); 106 | 107 | 108 | context.startService(i); 109 | } 110 | 111 | public void close() { 112 | if(context!=null && i!=null) 113 | context.stopService(i); 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /File Share/src/main/java/io/github/karuppiah7890/fileshare/ReceiverService.java: -------------------------------------------------------------------------------- 1 | package io.github.karuppiah7890.fileshare; 2 | 3 | import android.app.Service; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.os.IBinder; 7 | import android.os.Messenger; 8 | 9 | public class ReceiverService extends Service { 10 | 11 | private final String PORT = "PORT"; 12 | private final String MESSENGER = "MESSENGER"; 13 | 14 | private int port; 15 | private Messenger messenger; 16 | 17 | @Override 18 | public int onStartCommand(Intent intent, int flags, int startId) { 19 | 20 | Bundle b = intent.getExtras(); 21 | 22 | port = (int) b.get(PORT); 23 | messenger = (Messenger) b.get(MESSENGER); 24 | 25 | ReceiverThread receiverThread = new ReceiverThread(port,messenger); 26 | 27 | receiverThread.start(); 28 | 29 | return START_REDELIVER_INTENT; 30 | } 31 | 32 | @Override 33 | public IBinder onBind(Intent intent) { 34 | return null; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /File Share/src/main/java/io/github/karuppiah7890/fileshare/ReceiverThread.java: -------------------------------------------------------------------------------- 1 | package io.github.karuppiah7890.fileshare; 2 | 3 | import android.os.Environment; 4 | import android.os.Message; 5 | import android.os.Messenger; 6 | import android.os.RemoteException; 7 | import android.util.Log; 8 | 9 | import java.io.DataInputStream; 10 | import java.io.DataOutputStream; 11 | import java.io.File; 12 | import java.io.FileOutputStream; 13 | import java.io.IOException; 14 | import java.net.ServerSocket; 15 | import java.net.Socket; 16 | 17 | public class ReceiverThread extends Thread { 18 | 19 | private int port; 20 | private Messenger messenger; 21 | private ServerSocket listenerSocket; 22 | private Socket communicationSocket; 23 | private int PKT_SIZE = 60*1024; 24 | 25 | public ReceiverThread(int port,Messenger messenger){ 26 | this.port = port; 27 | this.messenger = messenger; 28 | File folder = new File(Environment.getExternalStorageDirectory() + "/FileSharer/"); 29 | 30 | folder.mkdirs(); 31 | } 32 | 33 | @Override 34 | public void run() { 35 | 36 | Message message; 37 | 38 | try { 39 | listenerSocket = new ServerSocket(port); 40 | 41 | message = Message.obtain(); 42 | message.what = FileReceiver.CODE; 43 | message.obj = port; 44 | messenger.send(message); 45 | 46 | message = Message.obtain(); 47 | message.what = FileReceiver.LISTENING; 48 | message.obj = ""; 49 | messenger.send(message); 50 | 51 | communicationSocket = listenerSocket.accept(); 52 | 53 | message = Message.obtain(); 54 | message.what = FileReceiver.CONNECTED; 55 | message.obj = ""; 56 | messenger.send(message); 57 | 58 | DataInputStream in = new DataInputStream(communicationSocket.getInputStream()); 59 | 60 | message = Message.obtain(); 61 | message.what = FileReceiver.RECEIVING_FILE; 62 | message.obj = ""; 63 | messenger.send(message); 64 | 65 | // Read File Name and create Output Stream 66 | String fileName = in.readUTF(); 67 | 68 | File receiveFile = new File(Environment.getExternalStorageDirectory() + "/FileSharer/" + fileName); 69 | 70 | DataOutputStream dout = new DataOutputStream(new FileOutputStream(receiveFile,true)); 71 | 72 | // Read File Size 73 | long fileSize = in.readLong(); 74 | 75 | int totalLength = 0; 76 | int length = 0; 77 | byte[] receiveData = new byte[PKT_SIZE]; 78 | 79 | long startTime = System.currentTimeMillis(); 80 | 81 | // Get the file data 82 | while( fileSize>0 && ( ( length = in.read( receiveData,0,(int) Math.min(receiveData.length,fileSize) ))!= -1) ) 83 | { 84 | dout.write(receiveData, 0, length); 85 | 86 | totalLength += length; 87 | 88 | fileSize -= length; 89 | } 90 | 91 | long stopTime = System.currentTimeMillis(); 92 | 93 | dout.close(); 94 | 95 | double time = (stopTime - startTime) / 1000.0; 96 | 97 | double speed = (totalLength / time) / 1048576.0; 98 | 99 | message = Message.obtain(); 100 | message.what = FileReceiver.FILE_RECEIVED; 101 | message.obj = receiveFile; 102 | 103 | messenger.send(message); 104 | 105 | } catch (Exception e){ 106 | 107 | e.printStackTrace(); 108 | 109 | message = Message.obtain(); 110 | message.what = FileReceiver.RECEIVE_ERROR; 111 | message.obj = e.toString(); 112 | 113 | try { 114 | messenger.send(message); 115 | } catch (RemoteException re) { 116 | Log.e("ReceiverThread","Error in sending an error message! Error : " + re.toString()); 117 | re.printStackTrace(); 118 | } 119 | 120 | } finally { 121 | 122 | try { 123 | 124 | if(communicationSocket!=null) 125 | communicationSocket.close(); 126 | 127 | if(listenerSocket!=null) 128 | listenerSocket.close(); 129 | 130 | } catch (IOException ioe) { 131 | Log.e("ReceiverThread","Error in closing sockets. Error : " + ioe.toString()); 132 | ioe.printStackTrace(); 133 | } 134 | } 135 | 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /File Share/src/main/java/io/github/karuppiah7890/fileshare/SenderService.java: -------------------------------------------------------------------------------- 1 | package io.github.karuppiah7890.fileshare; 2 | 3 | import android.app.Service; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.os.IBinder; 7 | import android.os.Messenger; 8 | 9 | import java.io.File; 10 | import java.net.InetAddress; 11 | 12 | public class SenderService extends Service { 13 | 14 | private final String PORT = "PORT"; 15 | private final String MESSENGER = "MESSENGER"; 16 | private final String FILE = "FILE"; 17 | private final String RECEIVER_IP = "RECEIVER_IP"; 18 | 19 | private InetAddress receiverIP; 20 | private int port; 21 | private Messenger messenger; 22 | private File file; 23 | 24 | @Override 25 | public int onStartCommand(Intent intent, int flags, int startId) { 26 | 27 | Bundle b = intent.getExtras(); 28 | 29 | receiverIP = (InetAddress) b.get(RECEIVER_IP); 30 | port = (int) b.get(PORT); 31 | messenger = (Messenger) b.get(MESSENGER); 32 | file = (File) b.get(FILE); 33 | 34 | SenderThread senderThread = new SenderThread(receiverIP,port,file,messenger); 35 | 36 | senderThread.start(); 37 | 38 | return START_REDELIVER_INTENT; 39 | } 40 | 41 | 42 | @Override 43 | public IBinder onBind(Intent intent) { 44 | return null; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /File Share/src/main/java/io/github/karuppiah7890/fileshare/SenderThread.java: -------------------------------------------------------------------------------- 1 | package io.github.karuppiah7890.fileshare; 2 | 3 | import android.os.Message; 4 | import android.os.Messenger; 5 | import android.os.RemoteException; 6 | import android.util.Log; 7 | 8 | import java.io.DataInputStream; 9 | import java.io.DataOutputStream; 10 | import java.io.File; 11 | import java.io.FileInputStream; 12 | import java.io.IOException; 13 | import java.net.InetAddress; 14 | import java.net.Socket; 15 | 16 | public class SenderThread extends Thread { 17 | 18 | private InetAddress receiverIP; 19 | private int port; 20 | private File fileToSend; 21 | private Messenger messenger; 22 | private Socket senderSocket; 23 | private int PKT_SIZE = 60*1024; 24 | 25 | public SenderThread(InetAddress receiverIP, int port, File fileToSend, Messenger messenger){ 26 | this.receiverIP = receiverIP; 27 | this.port = port; 28 | this.messenger = messenger; 29 | this.fileToSend = fileToSend; 30 | } 31 | 32 | @Override 33 | public void run() { 34 | Message message; 35 | 36 | try { 37 | 38 | message = Message.obtain(); 39 | message.what = FileSender.CONNECTING; 40 | message.obj = ""; 41 | messenger.send(message); 42 | 43 | senderSocket = new Socket(receiverIP,port); 44 | 45 | message = Message.obtain(); 46 | message.what = FileSender.CONNECTED; 47 | message.obj = ""; 48 | messenger.send(message); 49 | 50 | DataOutputStream out = new DataOutputStream(senderSocket.getOutputStream()); 51 | 52 | message = Message.obtain(); 53 | message.what = FileSender.SENDING_FILE; 54 | message.obj = ""; 55 | messenger.send(message); 56 | 57 | // Send File Name 58 | out.writeUTF(fileToSend.getName()); 59 | 60 | DataInputStream din = new DataInputStream(new FileInputStream(fileToSend)); 61 | 62 | // Send File Size 63 | long fileSize = fileToSend.length(); 64 | out.writeLong(fileSize); 65 | 66 | int totalLength = 0; 67 | int length = 0; 68 | byte[] sendData = new byte[PKT_SIZE]; 69 | 70 | long startTime = System.currentTimeMillis(); 71 | 72 | // Send the file data 73 | while ((length = din.read(sendData)) != -1) { 74 | 75 | out.write(sendData, 0, length); 76 | 77 | totalLength += length; 78 | 79 | } 80 | 81 | long stopTime = System.currentTimeMillis(); 82 | 83 | din.close(); 84 | 85 | double time = (stopTime - startTime) / 1000.0; 86 | 87 | double speed = (totalLength / time) / 1048576.0; 88 | 89 | message = Message.obtain(); 90 | message.what = FileSender.FILE_SENT; 91 | message.obj = ""; 92 | 93 | messenger.send(message); 94 | 95 | } catch (Exception e){ 96 | 97 | e.printStackTrace(); 98 | 99 | message = Message.obtain(); 100 | message.what = FileSender.SEND_ERROR; 101 | message.obj = e.toString(); 102 | 103 | try { 104 | messenger.send(message); 105 | } catch (RemoteException re) { 106 | Log.e("SenderThread","Error in sending an error message! Error : " + re.toString()); 107 | re.printStackTrace(); 108 | } 109 | 110 | } finally { 111 | 112 | try { 113 | 114 | if(senderSocket!=null) 115 | senderSocket.close(); 116 | 117 | } catch (IOException ioe) { 118 | Log.e("SenderThread","Error in closing sockets. Error : " + ioe.toString()); 119 | ioe.printStackTrace(); 120 | } 121 | } 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /File Share/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | File Share 3 | 4 | -------------------------------------------------------------------------------- /File Share/src/test/java/io/github/karuppiah7890/fileshare/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package io.github.karuppiah7890.fileshare; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /FileSharerDemoApp/.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | /build 3 | -------------------------------------------------------------------------------- /FileSharerDemoApp/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.0" 6 | defaultConfig { 7 | applicationId "io.github.karuppiah7890.filesharer" 8 | minSdkVersion 15 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | 28 | compile 'com.android.support:appcompat-v7:25.0.0' 29 | compile project(":File Share") 30 | compile 'com.droidninja:filepicker:1.0.6' 31 | testCompile 'junit:junit:4.12' 32 | } 33 | -------------------------------------------------------------------------------- /FileSharerDemoApp/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:\Users\3521\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /FileSharerDemoApp/src/androidTest/java/io/github/karuppiah7890/filesharer/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package io.github.karuppiah7890.filesharer; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("io.github.karuppiah7890.filesharer", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /FileSharerDemoApp/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /FileSharerDemoApp/src/main/java/io/github/karuppiah7890/filesharer/DashBoard.java: -------------------------------------------------------------------------------- 1 | package io.github.karuppiah7890.filesharer; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | import android.widget.Button; 8 | 9 | public class DashBoard extends AppCompatActivity implements View.OnClickListener { 10 | 11 | Button bSend,bReceive; 12 | 13 | @Override 14 | protected void onCreate(Bundle savedInstanceState) { 15 | 16 | super.onCreate(savedInstanceState); 17 | setContentView(R.layout.activity_dash_board); 18 | 19 | bSend = (Button) findViewById(R.id.bSend); 20 | bReceive = (Button) findViewById(R.id.bReceive); 21 | 22 | bSend.setOnClickListener(this); 23 | bReceive.setOnClickListener(this); 24 | } 25 | 26 | @Override 27 | public void onClick(View view) { 28 | int id = view.getId(); 29 | 30 | switch (id){ 31 | case R.id.bSend : 32 | startActivity(new Intent(DashBoard.this,SenderActivity.class)); 33 | break; 34 | 35 | case R.id.bReceive : 36 | startActivity(new Intent(DashBoard.this,ReceiverActivity.class)); 37 | break; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /FileSharerDemoApp/src/main/java/io/github/karuppiah7890/filesharer/ReceiverActivity.java: -------------------------------------------------------------------------------- 1 | package io.github.karuppiah7890.filesharer; 2 | 3 | import android.os.Bundle; 4 | import android.os.Handler; 5 | import android.os.Looper; 6 | import android.os.Message; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.view.View; 9 | import android.widget.TextView; 10 | import android.widget.Toast; 11 | 12 | import java.io.File; 13 | 14 | import io.github.karuppiah7890.fileshare.FileReceiver; 15 | 16 | public class ReceiverActivity extends AppCompatActivity { 17 | 18 | FileReceiver fileReceiver; 19 | TextView tvCode; 20 | 21 | private final Handler mHandler = new Handler(Looper.getMainLooper()) { 22 | @Override 23 | public void handleMessage(Message msg) { 24 | switch (msg.what) { 25 | case FileReceiver.CODE : 26 | tvCode.setText((int)msg.obj + ""); 27 | break; 28 | 29 | case FileReceiver.LISTENING : 30 | Toast.makeText(ReceiverActivity.this,"Listening...",Toast.LENGTH_SHORT).show(); 31 | break; 32 | 33 | case FileReceiver.CONNECTED: 34 | Toast.makeText(ReceiverActivity.this,"Connected!",Toast.LENGTH_SHORT).show(); 35 | break; 36 | 37 | case FileReceiver.RECEIVING_FILE : 38 | Toast.makeText(ReceiverActivity.this,"Receiving File!",Toast.LENGTH_SHORT).show(); 39 | break; 40 | 41 | case FileReceiver.FILE_RECEIVED : 42 | File file = (File) msg.obj; 43 | Toast.makeText(ReceiverActivity.this,file.getName() + " Received!",Toast.LENGTH_SHORT).show(); 44 | Toast.makeText(ReceiverActivity.this,"Stored in " + file.getAbsolutePath(),Toast.LENGTH_SHORT).show(); 45 | fileReceiver.close(); 46 | break; 47 | 48 | case FileReceiver.RECEIVE_ERROR : 49 | Toast.makeText(ReceiverActivity.this,"Error occured : " + (String)msg.obj,Toast.LENGTH_SHORT).show(); 50 | fileReceiver.close(); 51 | break; 52 | } 53 | } 54 | }; 55 | 56 | public void getFile(View view) { 57 | 58 | fileReceiver = new FileReceiver(this,mHandler); 59 | 60 | fileReceiver.getFile(); 61 | 62 | } 63 | 64 | @Override 65 | protected void onCreate(Bundle savedInstanceState) { 66 | super.onCreate(savedInstanceState); 67 | setContentView(R.layout.activity_receiver); 68 | 69 | tvCode = (TextView) findViewById(R.id.tvCode); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /FileSharerDemoApp/src/main/java/io/github/karuppiah7890/filesharer/SenderActivity.java: -------------------------------------------------------------------------------- 1 | package io.github.karuppiah7890.filesharer; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.os.Handler; 7 | import android.os.Looper; 8 | import android.os.Message; 9 | import android.support.v7.app.AppCompatActivity; 10 | import android.util.Log; 11 | import android.view.View; 12 | import android.widget.Button; 13 | import android.widget.EditText; 14 | import android.widget.Toast; 15 | 16 | import java.io.File; 17 | import java.util.ArrayList; 18 | 19 | import droidninja.filepicker.FilePickerBuilder; 20 | import droidninja.filepicker.FilePickerConst; 21 | import io.github.karuppiah7890.fileshare.FileSender; 22 | 23 | public class SenderActivity extends AppCompatActivity { 24 | 25 | private ArrayList docPaths; 26 | private EditText etCode; 27 | private FileSender fileSender; 28 | Button bPickFile; 29 | 30 | private final Handler mHandler = new Handler(Looper.getMainLooper()) { 31 | @Override 32 | public void handleMessage(Message msg) { 33 | switch (msg.what) { 34 | case FileSender.CONNECTING : 35 | Toast.makeText(SenderActivity.this,"Connecting...",Toast.LENGTH_SHORT).show(); 36 | break; 37 | 38 | case FileSender.CONNECTED : 39 | Toast.makeText(SenderActivity.this,"Connected!",Toast.LENGTH_SHORT).show(); 40 | break; 41 | 42 | case FileSender.SENDING_FILE : 43 | Toast.makeText(SenderActivity.this,"Sending File!",Toast.LENGTH_SHORT).show(); 44 | break; 45 | 46 | case FileSender.FILE_SENT : 47 | Toast.makeText(SenderActivity.this,"File Sent!",Toast.LENGTH_SHORT).show(); 48 | fileSender.close(); 49 | bPickFile.setEnabled(true); 50 | break; 51 | 52 | case FileSender.SEND_ERROR : 53 | Toast.makeText(SenderActivity.this,"Error occured : " + (String)msg.obj,Toast.LENGTH_SHORT).show(); 54 | fileSender.close(); 55 | bPickFile.setEnabled(true); 56 | break; 57 | } 58 | } 59 | }; 60 | 61 | @Override 62 | protected void onCreate(Bundle savedInstanceState) { 63 | super.onCreate(savedInstanceState); 64 | setContentView(R.layout.activity_sender); 65 | 66 | etCode = (EditText) findViewById(R.id.etCode); 67 | bPickFile = (Button) findViewById(R.id.bPickFile); 68 | } 69 | 70 | private void sendMyFile(String filePath) { 71 | 72 | Log.i("SenderActivity","File Path : " + filePath); 73 | Toast.makeText(this,filePath,Toast.LENGTH_SHORT).show(); 74 | 75 | File file = new File(filePath); 76 | 77 | fileSender = new FileSender(this,mHandler); 78 | 79 | if(!etCode.getText().toString().equals("")){ 80 | int code = Integer.parseInt(etCode.getText().toString()); 81 | fileSender.sendFile(file,code); 82 | } 83 | 84 | 85 | } 86 | 87 | public void pickAFile(View view){ 88 | FilePickerBuilder.getInstance().setMaxCount(1) 89 | .setActivityTheme(R.style.AppTheme) 90 | .pickDocument(this); 91 | 92 | view.setEnabled(false); 93 | } 94 | 95 | @Override 96 | public void onActivityResult(int requestCode, int resultCode, Intent data) { 97 | switch (requestCode) 98 | { 99 | case FilePickerConst.REQUEST_CODE_DOC: 100 | if(resultCode== Activity.RESULT_OK && data!=null) 101 | { 102 | docPaths = new ArrayList<>(); 103 | docPaths.addAll(data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_DOCS)); 104 | sendMyFile(docPaths.get(0)); 105 | } 106 | break; 107 | } 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /FileSharerDemoApp/src/main/res/layout/activity_dash_board.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 |