database connection established

parent 39544482
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
\ No newline at end of file
package com.sliit.blindvision;
import android.content.Context;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.sliit.blindvision", appContext.getPackageName());
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sliit.blindvision">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.BlindVision">
<activity android:name=".SplashActivity" android:theme="@style/SplashTheme"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Landing"
android:exported="true" >
</activity>
<activity
android:name=".Scan"
android:label="Scan">
</activity>
<activity
android:name=".Positions"
android:label="Calibrate">
</activity>
<activity
android:name=".Buildings"
android:label="Buildings">
</activity>
<activity
android:name=".StartingScreen"
android:exported="true"
android:label="@string/app_name">
</activity>
<activity android:name=".Locate"
android:label="Locate">
</activity>
<activity android:name=".FriendlyWifis"
android:label="Choose Friendly Wifi"/>
<activity
android:name=".Route"
android:exported="true">
</activity>
</application>
</manifest>
\ No newline at end of file
package com.sliit.blindvision;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;
public class Buildings extends Activity {
ArrayList<String> buildings;
DatabaseHelper db;
ListView buildingsList;
Button add;
EditText buildingName;
ArrayAdapter<String> arrayAdapter;
Context context = this;
private void switchToPositionsActivity(String data) {
Intent intent = new Intent(context, Positions.class);
intent.putExtra("BUILDING_NAME", data);
startActivity(intent);
}
public void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
setContentView(R.layout.buildings);
db = new DatabaseHelper(this);
add = (Button) findViewById(R.id.add);
buildingName = (EditText) findViewById(R.id.name);
buildingName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
if (charSequence.toString().equals("")) {
add.setEnabled(false);
} else {
boolean exists = false;
for (String buildingName : buildings) {
if (charSequence.toString().equals(buildingName)) {
add.setEnabled(false);
exists = true;
break;
}
}
if (!exists) {
add.setEnabled(true);
}
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
buildingName.setText("");
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
final String input = buildingName.getText().toString();
if (input.length() < 4) {
new AlertDialog.Builder(context)
.setTitle(getString(R.string.warning))
.setMessage(getString(R.string.dialog_message))
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
switchToPositionsActivity(input);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
} else {
switchToPositionsActivity(input);
}
}
});
buildings = db.getBuildings();
buildingsList = (ListView) findViewById(R.id.buildingslist);
arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, buildings);
buildingsList.setAdapter(arrayAdapter);
buildingsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
String selectedBuilding = (String) parent.getItemAtPosition(position);
Intent intent = new Intent(getApplicationContext(), Positions.class);
intent.putExtra("BUILDING_NAME", selectedBuilding);
startActivityForResult(intent, 0);
}
});
SwipeDismissListViewTouchListener touchListener =
new SwipeDismissListViewTouchListener(
buildingsList,
new SwipeDismissListViewTouchListener.DismissCallbacks() {
@Override
public boolean canDismiss(int position) {
return true;
}
@Override
public void onDismiss(ListView listView, int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
db.deleteBuilding(buildings.get(position));
buildings.remove(position);
arrayAdapter.notifyDataSetChanged();
}
}
});
buildingsList.setOnTouchListener(touchListener);
}
@Override
protected void onResume() {
buildings = db.getBuildings();
arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, buildings);
buildingsList.setAdapter(arrayAdapter);
buildingName.setText("");
add.setEnabled(false);
super.onResume();
}
}
package com.sliit.blindvision;
/**
* Created by ajnas on 1/11/17.
*/
public class Config {
public static final String BASE_URL = "http://your-server.url.com";
}
package com.sliit.blindvision;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "wifips.db";
public static final String AP_TABLE = "access_points";
public static final String READINGS_TABLE = "readings";
public static final String AP_CREATE = "CREATE TABLE 'access_points' "
+ "('building_id' TEXT NOT NULL ,'ssid' TEXT NOT NULL,'mac_id' TEXT NOT NULL )";
public static final String READINGS_CREATE = "CREATE TABLE 'readings' ('building_id' TEXT NOT NULL , "
+ "'position_id' TEXT NOT NULL ,"
+ " 'ssid' TEXT NOT NULL , 'mac_id' TEXT NOT NULL , 'rssi' INTEGER NOT NULL )";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(AP_CREATE);
db.execSQL(READINGS_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + AP_CREATE);
db.execSQL("DROP TABLE IF EXISTS " + READINGS_CREATE);
onCreate(db);
}
public int deleteReading(String building_id, String position_id) {
SQLiteDatabase db = getWritableDatabase();
String[] args = new String[] { building_id, position_id };
return db.delete(READINGS_TABLE, "building_id=? and position_id=?", args);
}
public boolean deleteBuilding(String building_id) {
SQLiteDatabase db = getWritableDatabase();
String[] args = new String[] { building_id };
db.delete(AP_TABLE,"building_id=?",args);
db.delete(READINGS_TABLE, "building_id=?", args);
return true;
}
public ArrayList<String> getBuildings() {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("select distinct building_id from " + READINGS_TABLE, null);
ArrayList<String> result = new ArrayList<String>();
cursor.moveToFirst();
while (cursor.isAfterLast() == false) {
result.add(cursor.getString(0));
cursor.moveToNext();
}
return result;
}
public ArrayList<Router> getFriendlyWifis(String building_id) {
ArrayList<Router> result = new ArrayList<Router>();
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("select ssid, mac_id from " + AP_TABLE
+ " where building_id=?", new String[] { building_id });
cursor.moveToFirst();
while (cursor.isAfterLast() == false) {
result.add(new Router(cursor.getString(0), cursor.getString(1)));
cursor.moveToNext();
}
return result;
}
public int deleteFriendlyWifis(String building_id) {
SQLiteDatabase db = getWritableDatabase();
String[] args = new String[] { building_id };
return db.delete(AP_TABLE, "building_id=?", args);
}
public boolean addFriendlyWifis(String building_id, ArrayList<Router> wifis) {
deleteFriendlyWifis(building_id);
SQLiteDatabase db = getWritableDatabase();
for (int i = 0; i < wifis.size(); i++) {
ContentValues cv = new ContentValues();
cv.put("building_id", building_id);
cv.put("ssid", wifis.get(i).getSSID());
cv.put("mac_id", wifis.get(i).getBSSID());
db.insert(AP_TABLE, null, cv);
}
return true;
}
public ArrayList<String> getPositions(String building_id) {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("select distinct position_id from " + READINGS_TABLE
+ " where building_id=?", new String[] { building_id });
ArrayList<String> result = new ArrayList<String>();
cursor.moveToFirst();
while (cursor.isAfterLast() == false) {
result.add(cursor.getString(0));
cursor.moveToNext();
}
return result;
}
public boolean addReadings(String building_id, PositionData positionData) {
deleteReading(building_id, positionData.getName());
SQLiteDatabase db = getWritableDatabase();
for (Map.Entry<String, Integer> e : positionData.getValues().entrySet()) {
ContentValues cv = new ContentValues();
cv.put("building_id", building_id);
cv.put("position_id", positionData.getName());
cv.put("ssid",positionData.routers.get(e.getKey()));
cv.put("mac_id",e.getKey());
cv.put("rssi", e.getValue());
db.insert(READINGS_TABLE, null, cv);
}
return true;
}
public ArrayList<PositionData> getReadings(String building_id) {
HashMap<String, PositionData> positions = new HashMap<String, PositionData>();
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("select distinct * from " + READINGS_TABLE
+ " where building_id='" + building_id + "'", null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
String position_id = cursor.getString(1);
Router router = new Router(cursor.getString(2), cursor.getString(3));
if (positions.containsKey(position_id)) {
positions.get(position_id).addValue(router, cursor.getInt(4));
} else {
PositionData positionData = new PositionData(cursor.getString(1));
positionData.addValue(router, cursor.getInt(4));
positions.put(position_id, positionData);
}
cursor.moveToNext();
}
ArrayList<PositionData> result = new ArrayList<PositionData>();
for (Map.Entry<String, PositionData> e : positions.entrySet())
result.add(e.getValue());
return result;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment