-
Notifications
You must be signed in to change notification settings - Fork 1
Droplist and current location added #2
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| #Build Properties | ||
| #Sat Nov 24 09:14:14 EST 2018 | ||
| #Wed Jan 02 20:18:46 CST 2019 | ||
| version_minor=0 | ||
| version_build=0 | ||
| version_build=4 | ||
| version_patch=3 | ||
| version_store=44 | ||
| version_major=2 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| package com.vrem.wifianalyzer.wifi.collectdata; | ||
|
|
||
| import android.content.Context; | ||
| import android.os.Bundle; | ||
| import android.os.Environment; | ||
| import android.support.annotation.NonNull; | ||
| import android.support.v4.app.Fragment; | ||
| import android.util.Log; | ||
| import android.view.LayoutInflater; | ||
| import android.view.View; | ||
| import android.view.ViewGroup; | ||
| import android.widget.Button; | ||
| import android.widget.EditText; | ||
| import android.widget.Toast; | ||
|
|
||
| import com.vrem.wifianalyzer.MainContext; | ||
| import com.vrem.wifianalyzer.R; | ||
| import com.vrem.wifianalyzer.settings.Settings; | ||
| import com.vrem.wifianalyzer.wifi.band.WiFiBand; | ||
| import com.vrem.wifianalyzer.wifi.model.SortBy; | ||
| import com.vrem.wifianalyzer.wifi.model.WiFiData; | ||
| import com.vrem.wifianalyzer.wifi.model.WiFiDetail; | ||
| import com.vrem.wifianalyzer.wifi.predicate.WiFiBandPredicate; | ||
| import com.vrem.wifianalyzer.wifi.scanner.UpdateNotifier; | ||
|
|
||
| import org.apache.commons.collections4.Predicate; | ||
| import org.json.JSONException; | ||
| import org.json.JSONObject; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.File; | ||
| import java.io.FileReader; | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| import java.util.List; | ||
|
|
||
| import static android.content.ContentValues.TAG; | ||
|
|
||
|
|
||
| public class CollectDataFragment extends Fragment implements UpdateNotifier{ | ||
|
|
||
| private Button saveBtn; | ||
| private EditText currentLocationEt; | ||
| private static String objectString = ""; | ||
| private static JSONObject jsonObject=new JSONObject(); | ||
|
|
||
|
|
||
| public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | ||
|
|
||
| View view = inflater.inflate(R.layout.collect_data, container, false); | ||
| MainContext.INSTANCE.getScannerService().register(this::update); | ||
|
|
||
| saveBtn = view.findViewById(R.id.saveCollectedData); | ||
| currentLocationEt = view.findViewById(R.id.collectDataEt); | ||
|
|
||
| // loop through wifiDetails and put() all bssid and current location | ||
| // call writeJSONtoFile with !!jsonObject!! IFF we really added any new values | ||
|
|
||
| saveBtn.setOnClickListener(new View.OnClickListener() { | ||
| @Override | ||
| public void onClick(View view) { | ||
|
|
||
| if (currentLocationEt.getText().toString().isEmpty()) | ||
| try { | ||
| Log.d(TAG, "onClick: " + WiFiData.wiFiDetails.get(0).getBSSID()+" "+WiFiData.wiFiDetails.get(0).getTitle().split(" ")[0]); | ||
| jsonObject.put(WiFiData.wiFiDetails.get(0).getBSSID(),WiFiData.wiFiDetails.get(0).getTitle().split(" ")[0]); | ||
| writeJSONtoFile(MainContext.INSTANCE.getContext(), currentLocationEt.getText().toString(), jsonObject); | ||
| } catch (JSONException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| /* readJSON(currentLocationEt.toString());*/ | ||
| return view; | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public void update(@NonNull WiFiData wiFiData) { | ||
|
|
||
| Settings settings = MainContext.INSTANCE.getSettings(); | ||
| WiFiBand wiFiBand = settings.getWiFiBand(); | ||
| Predicate<WiFiDetail> predicate = new WiFiBandPredicate(wiFiBand); | ||
| WiFiData.wiFiDetails = wiFiData.getWiFiDetails(predicate, SortBy.STRENGTH); | ||
| } | ||
|
|
||
| private String readJSON(String filename) { | ||
| File directory = Environment.getExternalStorageDirectory(); | ||
| File file = new File(directory + "/Notes/", filename); | ||
|
|
||
| StringBuilder text = new StringBuilder(); | ||
| try { | ||
| BufferedReader br = new BufferedReader(new FileReader(file)); | ||
| String line; | ||
|
|
||
| while ((line = br.readLine()) != null) { | ||
|
|
||
| text.append(line); | ||
| text.append('\n'); | ||
| } | ||
| JSONObject jsonObject = new JSONObject(String.valueOf(text)); | ||
| for (int i = 0; i<jsonObject.length(); i++) { | ||
| if (!objectString.contains(jsonObject.getString((String) jsonObject.names().get(i)))) { | ||
| objectString = objectString + " " + jsonObject.getString((String) jsonObject.names().get(i)) + "\n"; | ||
| } | ||
| } | ||
| /* Toast.makeText(mContext, objectString, Toast.LENGTH_SHORT).show();*/ | ||
| br.close(); | ||
| } catch (IOException e) { | ||
|
|
||
| } catch (JSONException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| return text.toString(); | ||
| } | ||
|
|
||
| public void writeJSONtoFile(Context context, String sFileName, JSONObject jsonObject) throws JSONException { | ||
| // this function (writeJSONtoFile) is called every 5 seconds and every time it sends values of sKey(MAC) and sValue(NAME) that I want to store in a flattened JSONObject | ||
| //sKey is MAC address of the wifi, sValue is the name of the Wifi | ||
| try { | ||
| File root = new File(Environment.getExternalStorageDirectory(), "Notes"); | ||
| if (!root.exists()) { | ||
| root.mkdirs(); | ||
| } | ||
| File gpxfile = new File(root, sFileName); | ||
| FileWriter writer = new FileWriter(gpxfile); | ||
| Log.d(TAG, "writeJSONtoFile"); | ||
| writer.append(jsonObject.toString()+"\n"); | ||
| writer.flush(); | ||
| writer.close(); | ||
| Toast.makeText(MainContext.INSTANCE.getContext(), "Saved", Toast.LENGTH_SHORT).show(); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| Toast.makeText(context, "Not saved", Toast.LENGTH_SHORT).show(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems like your code is not properly layered, both doing I/O and interacting with the frontend. This makes your code hard to reuse and test |
||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,45 +1,62 @@ | ||
| /* | ||
| * WiFiAnalyzer | ||
| * Copyright (C) 2018 VREM Software Development <VREMSoftwareDevelopment@gmail.com> | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/> | ||
| */ | ||
|
|
||
| package com.vrem.wifianalyzer.wifi.model; | ||
|
|
||
| import android.content.Context; | ||
| import android.os.Environment; | ||
| import android.support.annotation.NonNull; | ||
| import android.util.Log; | ||
| import android.widget.Toast; | ||
|
|
||
| import com.vrem.util.FileUtils; | ||
| import com.vrem.wifianalyzer.MainContext; | ||
| import com.vrem.wifianalyzer.R; | ||
| import com.vrem.wifianalyzer.vendor.model.VendorService; | ||
|
|
||
| import org.apache.commons.collections4.CollectionUtils; | ||
| import org.apache.commons.collections4.IterableUtils; | ||
| import org.apache.commons.collections4.Predicate; | ||
| import org.apache.commons.collections4.Transformer; | ||
| import org.apache.commons.lang3.builder.EqualsBuilder; | ||
|
|
||
| import org.json.JSONException; | ||
| import org.json.JSONObject; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.File; | ||
| import java.io.FileReader; | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
|
|
||
| import static android.content.ContentValues.TAG; | ||
|
|
||
| public class WiFiData { | ||
| public static final WiFiData EMPTY = new WiFiData(Collections.emptyList(), WiFiConnection.EMPTY, Collections.emptyList()); | ||
|
|
||
| private final List<WiFiDetail> wiFiDetails; | ||
| private final WiFiConnection wiFiConnection; | ||
| public static final WiFiData EMPTY = new WiFiData(Collections.emptyList(), WiFiConnection.EMPTY, Collections.emptyList()); | ||
| public static String location = ""; | ||
| public static List<WiFiDetail> wiFiDetails; | ||
| private final List<String> wiFiConfigurations; | ||
| private Context mContext = MainContext.INSTANCE.getContext(); | ||
| private final WiFiConnection wiFiConnection; | ||
| private static JSONObject wiFiLocations = new JSONObject(); | ||
|
|
||
|
|
||
| static | ||
|
|
||
| { | ||
| try { | ||
|
|
||
| String content = FileUtils.readFile(MainContext.INSTANCE.getResources(), R.raw.shanghai); | ||
| wiFiLocations = new JSONObject(content); | ||
| Toast.makeText(MainContext.INSTANCE.getContext(), "Loaded locations with " + wiFiLocations.length() + " MAC -> location mappings", Toast.LENGTH_LONG).show(); | ||
| } catch (Exception e) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. catchall exceptions are generally bad. you want to be specific |
||
| Toast.makeText(MainContext.INSTANCE.getContext(), "Failed to load locations", Toast.LENGTH_LONG).show(); | ||
| } | ||
| } | ||
|
|
||
| private static String lastNotified = ""; | ||
|
|
||
| public WiFiData(@NonNull List<WiFiDetail> wiFiDetails, @NonNull WiFiConnection wiFiConnection, @NonNull List<String> wiFiConfigurations) { | ||
| this.wiFiDetails = wiFiDetails; | ||
|
|
@@ -65,6 +82,7 @@ public List<WiFiDetail> getWiFiDetails(@NonNull Predicate<WiFiDetail> predicate, | |
| results = sortAndGroup(results, sortBy, groupBy); | ||
| } | ||
| Collections.sort(results, sortBy.comparator()); | ||
| notifyLocation(results); | ||
| return results; | ||
| } | ||
|
|
||
|
|
@@ -91,6 +109,47 @@ List<WiFiDetail> sortAndGroup(@NonNull List<WiFiDetail> wiFiDetails, @NonNull So | |
| return results; | ||
| } | ||
|
|
||
|
|
||
| private void notifyLocation(List<WiFiDetail> wifiDetails) { | ||
|
|
||
|
|
||
| if (wifiDetails.size() < 3) { | ||
| return; | ||
| } | ||
| try { | ||
| String lookupKey = (wifiDetails.get(0).getBSSID().toUpperCase()/*+" " + wifiDetails.get(0).getWiFiSignal().getStrength()+"\n"+wifiDetails.get(1).getBSSID().toUpperCase()+" " + wifiDetails.get(1).getWiFiSignal().getStrength()+"\n"+wifiDetails.get(2).getBSSID().toUpperCase()+" " + wifiDetails.get(2).getWiFiSignal().getStrength()*/); | ||
| Log.d(TAG, "Wifidata: " + lookupKey); | ||
| location = wiFiLocations.getString(lookupKey); | ||
| if (location == null) { | ||
| location = wiFiLocations.getString((wifiDetails.get(1).getBSSID() + " " + wifiDetails.get(0).getBSSID() + " " + wifiDetails.get(2).getBSSID()).toUpperCase()); | ||
| } | ||
| if (location == null) { | ||
| location = wiFiLocations.getString((wifiDetails.get(0).getBSSID() + " " + wifiDetails.get(1).getBSSID()).toUpperCase()); | ||
| } | ||
| if (location == null) { | ||
| location = wiFiLocations.getString((wifiDetails.get(1).getBSSID() + " " + wifiDetails.get(0).getBSSID()).toUpperCase()); | ||
| } | ||
| if (location == null) { | ||
| location = wiFiLocations.getString(wifiDetails.get(0).getBSSID().toUpperCase()); | ||
| } | ||
| if (location == null) { | ||
| location = wiFiLocations.getString(wifiDetails.get(1).getBSSID().toUpperCase()); | ||
| } | ||
| if (location != null) { | ||
| if (!lastNotified.equals(location)) { | ||
| Log.d(TAG, "notifyLocation: "+location); | ||
| Log.d(TAG, "notifyLocation: "+lastNotified); | ||
| Toast.makeText(MainContext.INSTANCE.getContext(), "You are in " + location, Toast.LENGTH_LONG).show(); | ||
| lastNotified = location; | ||
|
|
||
| } | ||
| } else { | ||
| //Toast.makeText(MainContext.INSTANCE.getContext(), "Nothing found " + lookupKey, Toast.LENGTH_SHORT).show(); | ||
| } | ||
| } catch (Exception e) { | ||
| } | ||
| } | ||
|
|
||
| @NonNull | ||
| private List<WiFiDetail> getWiFiDetails(@NonNull Predicate<WiFiDetail> predicate) { | ||
| Collection<WiFiDetail> selected = CollectionUtils.select(wiFiDetails, predicate); | ||
|
|
@@ -125,9 +184,9 @@ private class ConnectionPredicate implements Predicate<WiFiDetail> { | |
| @Override | ||
| public boolean evaluate(WiFiDetail wiFiDetail) { | ||
| return new EqualsBuilder() | ||
| .append(wiFiConnection.getSSID(), wiFiDetail.getSSID()) | ||
| .append(wiFiConnection.getBSSID(), wiFiDetail.getBSSID()) | ||
| .isEquals(); | ||
| .append(wiFiConnection.getSSID(), wiFiDetail.getSSID()) | ||
| .append(wiFiConnection.getBSSID(), wiFiDetail.getBSSID()) | ||
| .isEquals(); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -152,4 +211,4 @@ public WiFiDetail transform(WiFiDetail input) { | |
| } | ||
| } | ||
|
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
at least a printStackTrace here?
Ideally should wrap and rethrow in a custom exception so the UI can notify the frontend