Skip to content

Commit

Permalink
added easier classification lolll
Browse files Browse the repository at this point in the history
  • Loading branch information
xingyiz committed Apr 22, 2021
1 parent 2e073bf commit ef134f5
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 67 deletions.
Binary file modified Trackie/.idea/caches/build_file_checksums.ser
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -25,63 +25,104 @@ public class StorageDownloader implements FirestoreExecute {

private String name;
private File goodSSIDs;
private File legalPoints;
private String LEGAL_POINTS;
private Context context;
private ArrayList<String> goodBSSIDs;
private int size;
private String type;

private String contents;

private static final int READ_BLOCK_SIZE = 100;

public StorageDownloader(String name, Context context) {
this.name = name + "_good_ssids2.txt";
public StorageDownloader(String name, String type, Context context) {
this.name = name;
storageReference = firebaseStorage.getReference(this.name);
this.context = context;
this.type = type;
}

@Override
public void execute(OnCompleteCallback callback) {
try {
goodSSIDs = new File(context.getFilesDir(), name);
storageReference.getFile(goodSSIDs).addOnCompleteListener(new OnCompleteListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<FileDownloadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
try {
FileInputStream fileIn = context.openFileInput(name);
InputStreamReader inputReader = new InputStreamReader(fileIn, StandardCharsets.UTF_8);

StringBuilder stringBuilder = new StringBuilder();
BufferedReader reader = new BufferedReader(inputReader);
String line = reader.readLine();
while (line != null) {
stringBuilder.append(line).append('\n');
line = reader.readLine();
}

contents = stringBuilder.toString();
inputReader.close();
switch(type) {
case "ssids":
goodSSIDs = new File(context.getFilesDir(), name);
storageReference.getFile(goodSSIDs).addOnCompleteListener(new OnCompleteListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<FileDownloadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
try {
FileInputStream fileIn = context.openFileInput(name);
InputStreamReader inputReader = new InputStreamReader(fileIn, StandardCharsets.UTF_8);

StringBuilder stringBuilder = new StringBuilder();
BufferedReader reader = new BufferedReader(inputReader);
String line = reader.readLine();
while (line != null) {
stringBuilder.append(line).append('\n');
line = reader.readLine();
}

contents = stringBuilder.toString();
inputReader.close();
// Toast.makeText(context, "Contents: " + contents, Toast.LENGTH_LONG).show();

goodBSSIDs = new ArrayList<>(Arrays.asList(contents.split(", ")));
goodBSSIDs.remove(goodBSSIDs.size() - 1);
goodBSSIDs = new ArrayList<>(Arrays.asList(contents.split(", ")));
goodBSSIDs.remove(goodBSSIDs.size() - 1);
// goodBSSIDs = new ArrayList<>(goodBSSIDs.subList(0, 60));
size = goodBSSIDs.size();
size = goodBSSIDs.size();

// Toast.makeText(context, "Good BSSIDs: " + goodBSSIDs.get(0), Toast.LENGTH_LONG).show();
// Toast.makeText(context, "Size: " + size, Toast.LENGTH_LONG).show();

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

callback.onSuccess();
callback.onSuccess();

} else {
callback.onFailure();
}
}
});
} else {
callback.onFailure();
}
}
});
break;

case "legal_points":
legalPoints = new File(context.getFilesDir(), name);
storageReference.getFile(legalPoints).addOnCompleteListener(new OnCompleteListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<FileDownloadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
try {
FileInputStream fileIn = context.openFileInput(name);
InputStreamReader inputReader = new InputStreamReader(fileIn, StandardCharsets.UTF_8);

StringBuilder stringBuilder = new StringBuilder();
BufferedReader reader = new BufferedReader(inputReader);
String line = reader.readLine();
while (line != null) {
stringBuilder.append(line).append('\n');
line = reader.readLine();
}

LEGAL_POINTS = stringBuilder.toString();
inputReader.close();
System.out.println(LEGAL_POINTS);
} catch (IOException e) {
e.printStackTrace();
}

callback.onSuccess();

} else {
callback.onFailure();
}
}
});
break;
}
} catch (Exception e) {
callback.onError();
}
Expand All @@ -98,4 +139,8 @@ public int getSize() {
public String getContents() {
return contents;
}

public String getLEGAL_POINTS() {
return LEGAL_POINTS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public void onError() {
}*/

public void getBSSIDs() {
StorageDownloader storageDownloader = new StorageDownloader(Prefs.getCurrentLocation(getContext()), getContext());
StorageDownloader storageDownloader = new StorageDownloader(Prefs.getCurrentLocation(getContext()) + "_good_ssids2.txt", "ssids", getContext());
storageDownloader.execute(new OnCompleteCallback() {
@Override
public void onSuccess() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void run() {
// You should have already deployed a model and a version.
// For reference, see https://cloud.google.com/ml-engine/docs/deploying-models.
String modelId = "B2L2_XTC";
String versionId = "B2L2_XTC";
String versionId = "B2L2_XTC256";
param.set(
"name", String.format("projects/%s/models/%s/versions/%s", projectId, modelId, versionId));

Expand Down Expand Up @@ -129,7 +129,7 @@ public void run() {
System.out.println("Response: " + response);

try {
callback.onReceiveResults(parsePredictionJSONForResult(response, "classification"));
callback.onReceiveResults(parsePredictionJSONForResult(response, "clf"));
} catch (JSONException e) {
e.printStackTrace();
callback.onError();
Expand All @@ -141,32 +141,30 @@ public void run() {

public double[] parsePredictionJSONForResult(String jsonResult, String type) throws JSONException {
double[] result;
try {
switch (type) {
case "regression":
JSONObject json = new JSONObject(jsonResult);
JSONArray jsonArray = json.getJSONArray("predictions");
result = new double[]{jsonArray.getJSONArray(0).getDouble(0),
jsonArray.getJSONArray(0).getDouble(1)};

case "classfication":
JSONObject json2 = new JSONObject(jsonResult);
JSONArray jsonArray2 = json2.getJSONArray("predictions");
int result2 = jsonArray2.getInt(0);

JSONObject legalPoints = new JSONObject(LEGAL_POINTS);
JSONArray legalPointsArray = legalPoints.getJSONArray("LEGAL_POINTS");
result = new double[]{legalPointsArray.getJSONArray(result2).getDouble(0),
legalPointsArray.getJSONArray(result2).getDouble(1)};
break;

default:
throw new IllegalStateException("Unexpected value: " + type);
}
} catch (Exception e) {
JSONObject json = new JSONObject(jsonResult);
String error = json.getString("error");
return new double[]{0.0, 0.0};
switch (type) {
case "regression":
JSONObject json = new JSONObject(jsonResult);
JSONArray jsonArray = json.getJSONArray("predictions");
result = new double[]{jsonArray.getJSONArray(0).getDouble(0),
jsonArray.getJSONArray(0).getDouble(1)};
break;

case "clf":
JSONObject json2 = new JSONObject(jsonResult);
JSONArray jsonArray2 = json2.getJSONArray("predictions");
int result2 = jsonArray2.getInt(0);
System.out.println("result2: " + result2);

System.out.println("LEGAL_POINTS MP; " + LEGAL_POINTS);
JSONObject legalPoints = new JSONObject(LEGAL_POINTS);
JSONArray legalPointsArray = legalPoints.getJSONArray("LEGAL_POINTS");
result = new double[]{legalPointsArray.getJSONArray(result2).getDouble(0),
legalPointsArray.getJSONArray(result2).getDouble(1)};
System.out.println("CLF: " + result[0] + ", " + result[1]);
break;

default:
throw new IllegalStateException("Unexpected value: " + type);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class TestingMainFragment extends Fragment {
private ModelPrediction modelPrediction;
private ArrayList<String> goodBSSIDs;
private int size;
private String LEGAL_POINTS;
private PointF currentPoint;

private boolean alreadyCheckedWrongLocation = false;
Expand Down Expand Up @@ -108,16 +109,35 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,

startTime = System.currentTimeMillis();

StorageDownloader getLegalPoints = new StorageDownloader("LEGAL_POINTS.json", "legal_points", getContext());
getLegalPoints.execute(new OnCompleteCallback() {
@Override
public void onSuccess() {
System.out.println("GET LEGAL POINTS: " + getLegalPoints.getLEGAL_POINTS());
LEGAL_POINTS = getLegalPoints.getLEGAL_POINTS();
Toast.makeText(getContext(), "LEGAL_POINTS retrieval success!", Toast.LENGTH_SHORT).show();
}

@Override
public void onFailure() {
Toast.makeText(getContext(), "LEGAL_POINTS retrieval failed!", Toast.LENGTH_SHORT).show();
}

@Override
public void onError() {
Toast.makeText(getContext(), "LEGAL_POINTS retrieval error!", Toast.LENGTH_SHORT).show();
}
});

// long way first :(
StorageDownloader storageDownloader = new StorageDownloader(Prefs.getCurrentLocation(getContext()), getContext());
StorageDownloader storageDownloader = new StorageDownloader(Prefs.getCurrentLocation(getContext()) + "_good_ssids2.txt", "ssids", getContext());
storageDownloader.execute(new OnCompleteCallback() {
@Override
public void onSuccess() {
goodBSSIDs = storageDownloader.getGoodBSSIDs();
retrievedBSSID = true;
String credentials = getString(R.string.credentials_key);
String legal_points = getString(R.string.LEGAL_POINTS);
modelPrediction = new ModelPrediction(credentials, legal_points);
modelPrediction = new ModelPrediction(credentials, LEGAL_POINTS);
size = storageDownloader.getSize();
Toast.makeText(getContext(), "GOOD_BSSIDS file retrieved :)", Toast.LENGTH_SHORT).show();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class TestingViewModel extends ViewModel {
public TestingViewModel() {}

public void loadGoodBSSIDs(String name, Context context) {
StorageDownloader storageDownloader = new StorageDownloader(name, context);
StorageDownloader storageDownloader = new StorageDownloader(name, "ssids", context);
storageDownloader.execute(new OnCompleteCallback() {
@Override
public void onSuccess() {
Expand Down

0 comments on commit ef134f5

Please sign in to comment.