Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
11 changes: 7 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ dependencies {
androidTestCompile('androidx.test.espresso:espresso-core:3.1.0', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'androidx.appcompat:appcompat:1.0.0'
compile 'com.google.code.gson:gson:2.8.0'
testCompile 'junit:junit:4.12'
compile project(':ayanda')

api 'com.squareup.okhttp3:okhttp:3.10.0'
api 'org.nanohttpd:nanohttpd:2.3.1'
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'com.google.code.gson:gson:2.8.0'
testImplementation 'junit:junit:4.12'
implementation project(':ayanda')
}
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="sintulabs.ayanda">
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="sintulabs.ayanda">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Expand All @@ -18,8 +18,8 @@

</activity>
<activity android:name="sample.BluetoothActivity" android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">

android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity
android:name="sample.WifiDirectActivity"
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/java/sample/MyClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public MyClient(Context applicationContext) {
}
}


public OkHttpClient getHTTPClient() {
return mClient;
}
Expand Down Expand Up @@ -146,7 +145,6 @@ else if (mimeType.startsWith("video"))
else if (mimeType.startsWith("audio"))
fileExt = "m4a";
}

return fileExt;
}

Expand All @@ -159,5 +157,4 @@ private String buildUrl(String url, String path) {
}
return sbUrl.toString();
}

}
2 changes: 1 addition & 1 deletion app/src/main/java/sample/MyServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void run() {
return NanoHTTPD.newFixedLengthResponse(Response.Status.OK, "text/plain", fileToShare.getMetadataJson());

} else {
String msg = "<html><body><h1>Hello server</h1>\n";
String msg = "<html><body><h1 >Hello server</h1>\n";
Map<String, String> parms = session.getParms();
if (parms.get("username") == null) {
msg += "<form action='?' method='get'>\n <p>Your name: <input type='text' name='username'></p>\n" + "</form>\n";
Expand Down
42 changes: 8 additions & 34 deletions app/src/main/java/sample/WifiDirectActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class WifiDirectActivity extends AppCompatActivity {
private List peers = new ArrayList();
private List peerNames = new ArrayList();
private ArrayAdapter<String> peersAdapter = null;
private int defualtPort = 8080;

private Button btnWdShareFile;
private Button btnWdDiscover;
Expand Down Expand Up @@ -91,53 +92,33 @@ public void wifiP2pThisDeviceChangedAction(Intent intent) {

@Override
public void onConnectedAsServer(Server server) {

try {
a.setServer(new MyServer(WifiDirectActivity.this, defualtPort));
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void onConnectedAsClient(final InetAddress groupOwnerAddress) {
// This is essentially polling,
new Thread(new Runnable() {
@Override
public void run() {
MyClient client = new MyClient(WifiDirectActivity.this);
try {

final String response = client
.get(groupOwnerAddress.getHostAddress() + ":" + Integer.toString(8080));
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(WifiDirectActivity.this, response, Toast.LENGTH_LONG).show();
}
});


} catch (IOException e) {
e.printStackTrace();
}

try {
final File file = client.getFile(groupOwnerAddress.getHostAddress() + ":" + Integer.toString(8080));
} catch (IOException e) {
e.printStackTrace();
}


if (nearbyMedia != null) {

client.uploadFile(groupOwnerAddress.getHostAddress() + ":" + Integer.toString(8080), nearbyMedia);
nearbyMedia = null;
}
}
}).start();

}
});
try {
int defualtPort = 8080;
a.setServer(new MyServer(this, defualtPort));
} catch (IOException e) {
e.printStackTrace();
}
}

private void createView() {
Expand Down Expand Up @@ -188,28 +169,21 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
// Load the selected image into a preview
ImageView ivPreview = (ImageView) findViewById(R.id.ivPreviewWD);
ivPreview.setImageBitmap(selectedImage);

String filePath = getRealPathFromURI(photoUri);


try {
nearbyMedia = new NearbyMedia();
nearbyMedia.setMimeType("image/jpeg");
nearbyMedia.setTitle("pic");

nearbyMedia.setFileMedia(new File(filePath));

//get a JSON reprecation of the metadata we want to share
Gson gson = new GsonBuilder()
.setDateFormat(DateFormat.FULL, DateFormat.FULL).create();
nearbyMedia.mMetadataJson = gson.toJson("key:value");

a.wdShareFile(nearbyMedia);
} catch (IOException e) {
nearbyMedia = null;
e.printStackTrace();
}

}
}

Expand Down
10 changes: 5 additions & 5 deletions ayanda/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.squareup.okhttp3:okhttp:3.10.0'
compile 'org.nanohttpd:nanohttpd:2.3.1'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'org.nanohttpd:nanohttpd:2.3.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
testImplementation 'org.robolectric:robolectric:4.3'
implementation 'com.google.android.gms:play-services-location:11.8.0'
compile 'androidx.appcompat:appcompat:1.0.0'
testCompile 'junit:junit:4.12'
implementation 'androidx.appcompat:appcompat:1.0.0'
testImplementation 'junit:junit:4.12'
}
1 change: 1 addition & 0 deletions ayanda/src/main/java/sintulabs/p2p/IWifiDirect.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public interface IWifiDirect {
void wifiP2pPeersChangedAction();
void wifiP2pConnectionChangedAction(Intent intent);
void wifiP2pThisDeviceChangedAction(Intent intent);
// This can be overriden with a custom Server class
void onConnectedAsServer(Server server);
void onConnectedAsClient(InetAddress groupOwnerAddress);
}
13 changes: 1 addition & 12 deletions ayanda/src/main/java/sintulabs/p2p/Lan.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,21 @@
import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.webkit.MimeTypeMap;
import android.widget.Toast;
import fi.iki.elonen.NanoHTTPD;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okio.BufferedSink;
import okio.Okio;

import java.io.ByteArrayOutputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

/**
Expand Down
4 changes: 2 additions & 2 deletions ayanda/src/main/java/sintulabs/p2p/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public int getPort() {
}

public void setFileToShare(NearbyMedia media) {
mServer.setFileToShare(media);
}
mServer.setFileToShare(media); }

public static Server getInstance() throws IOException {
if (server == null) {
throw new IOException("Server not defined");
}

return server;
}

Expand Down
4 changes: 2 additions & 2 deletions ayanda/src/main/java/sintulabs/p2p/WifiDirect.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ public void discover() {
*/
@Override
public Boolean isSupported() {
return null;
return wiFiP2pEnabled;
}

/**
Expand All @@ -364,6 +364,6 @@ public Boolean isSupported() {
*/
@Override
public Boolean isEnabled() {
return null;
return wiFiP2pEnabled;
}
}