Skip to content

Instantly share code, notes, and snippets.

@EXJUSTICE
Created January 4, 2017 07:35
Show Gist options
  • Save EXJUSTICE/850ad02f81c03b29b141b3e19f4515c9 to your computer and use it in GitHub Desktop.
Save EXJUSTICE/850ad02f81c03b29b141b3e19f4515c9 to your computer and use it in GitHub Desktop.
package com.xu.consent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
/**
* //moved asyncTask code here if we ever need to call it from consent activity?
*/
public class FileServerAsyncTask extends AsyncTask<Void, Void, String> {
private Context context;
private TextView statusText;
String incoming;
ArrayList<String> receivedStrings;
SharedPreferences received;
SharedPreferences.Editor receivedEditor;
public FileServerAsyncTask(Context context){
this.context = context;
received = context.getSharedPreferences("received", Context.MODE_PRIVATE);
receivedEditor = received.edit();
}
@Override
protected String doInBackground(Void... params){
try {
ServerSocket serverSocket = new ServerSocket(8988);
Log.d(WIFIDirectActivity.TAG, "Server: Socket opened");
Socket client = serverSocket.accept();
Log.d(WIFIDirectActivity.TAG, "Server: connectction done");
DataInputStream ds= new DataInputStream(client.getInputStream());
//could potentially get client IP and name passed from FB?
//How to separate inputstream?
incoming= ds.readUTF();
//split received hueg string thing by arrays
receivedStrings = new ArrayList<String>(Arrays.asList(incoming.split(",")));
for (int i = 0; i<receivedStrings.size();i++){
//add receivedStrings into sharedprefs storage
receivedEditor.putString(Integer.toString(i),receivedStrings.get(i));
}
Log.d(WIFIDirectActivity.TAG, "server: copying strings to local list");
serverSocket.close();
return incoming;
}catch (IOException e){
Log.e(WIFIDirectActivity.TAG, e.getMessage());
return null;
}
}
//After copying finished, view the image
@Override
protected void onPostExecute(String result){
return;
}
@Override
protected void onPreExecute(){
statusText.setText("Opening server socket");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment