Skip to content
Permalink
Browse files

Every contact now has additional editable info

  • Loading branch information...
dakhnod committed Jul 14, 2018
1 parent b3762e8 commit c3572e835349643f0aa9c77897c68a95480abd95
@@ -11,25 +11,28 @@
private String name;
private String photo;
private String identifier;
private String info;

public boolean recent = false;

private State state = State.PENDING;

public Contact(int id, String address, String name, String photo, String identifier) {
public Contact(int id, String address, String name, String info, String photo, String identifier) {
this.id = id;
this.address = address;
this.name = name;
this.photo = photo;
this.identifier = identifier;
this.info = info;
}

public Contact(String address, String name, String photo, String identifier) {
public Contact(String address, String name, String info, String photo, String identifier) {
this.id = -1;
this.address = address;
this.name = name;
this.photo = photo;
this.identifier = identifier;
this.info = info;
}

public State getState() {
@@ -75,4 +78,12 @@ public void setName(String name) {
public String getIdentifier() {
return identifier;
}

public String getInfo() {
return info;
}

public void setInfo(String info) {
this.info = info;
}
}
@@ -77,6 +77,7 @@ protected void onCreate(Bundle savedInstanceState) {
checkInit();
}


@Override
protected void onResume() {
super.onResume();
@@ -297,20 +298,16 @@ public boolean onMenuItemClick(MenuItem menuItem) {

private void showContactEditDialog(Contact c){
EditText et = new EditText(this);
et.setText(c.getName());
et.setText(c.getInfo());
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Edit contact")
.setView(et)
.setNegativeButton("cancel", null)
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String name = et.getText().toString();
if(name.isEmpty()){
Toast.makeText(ContactListActivity.this, "Name must not be empty", 0).show();
return;
}
c.setName(name);
String info = et.getText().toString();
c.setInfo(info);
mainBinder.updateContact(c);
refreshContactList();
}
@@ -327,20 +324,11 @@ public boolean onCreateOptionsMenu(Menu menu) {
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

/*if (id == R.id.action_settings) {
return true;
}
if (id == R.id.action_generate_qr) {
startActivity(new Intent(this, QRPresenterActivity.class));
if (id == R.id.action_settings) {
startActivity(new Intent(this, SettingsActivity.class));
return true;
}

if (id == R.id.action_scan_qr) {
startActivity(new Intent(this, QRScanActivity.class));
return true;
}*/

return super.onOptionsItemSelected(item);
}

@@ -43,6 +43,7 @@ public View getView(int position, @Nullable View convertView, @NonNull ViewGroup
}
String address = c.getAddress().replaceFirst("%.*", "");
((TextView)convertView.findViewById(R.id.contact_item_name)).setText(c.getName());
((TextView)convertView.findViewById(R.id.contact_item_info)).setText(c.getInfo());
((TextView)convertView.findViewById(R.id.contact_item_address)).setText(address);

if(c.getState() != Contact.State.PENDING){
@@ -21,6 +21,7 @@
private final String columnName = "name";
private final String columnPhoto = "photo";
private final String columnIdentifier = "identifier";
private final String columnInfo = "info";

public ContactSqlHelper(Context context) {
super(context, "Contacts.db", null, 1);
@@ -29,47 +30,50 @@ public ContactSqlHelper(Context context) {
}

public List<Contact> getContacts() {
Cursor cursor = this.database.query(tableName, new String[]{columnID, columnIP, columnPhoto, columnName, columnIdentifier}, "", null, "", "", "");
Cursor cursor = this.database.query(tableName, new String[]{"*"}, "", null, "", "", "");
ArrayList<Contact> contacts = new ArrayList<>(cursor.getCount());

if(cursor.moveToFirst()){
if (cursor.moveToFirst()) {
final int posID = cursor.getColumnIndex(columnID);
final int posIP = cursor.getColumnIndex(columnIP);
final int posName = cursor.getColumnIndex(columnName);
final int posPhoto = cursor.getColumnIndex(columnPhoto);
final int posIdentifier = cursor.getColumnIndex(columnIdentifier);
do{
final int posInfo = cursor.getColumnIndex(columnInfo);
do {
contacts.add(new Contact(
cursor.getInt(posID),
cursor.getString(posIP),
cursor.getString(posName),
cursor.getString(posInfo),
cursor.getString(posPhoto),
cursor.getString(posIdentifier)
));
}while(cursor.moveToNext());
} while (cursor.moveToNext());
}
cursor.close();

return contacts;
}

public void close(){
public void close() {
super.close();
database.close();
}

public void insertContact(Contact c) throws ContactAlreadyAddedException{
ContentValues values = new ContentValues(3);
public void insertContact(Contact c) throws ContactAlreadyAddedException {
ContentValues values = new ContentValues(5);
//values.put(columnID, c.getId());
values.put(columnIdentifier, c.getIdentifier());
values.put(columnIP, c.getAddress());
values.put(columnName, c.getName());
values.put(columnPhoto, c.getPhoto());
values.put(columnInfo, c.getInfo());

Cursor cur = database.query(tableName, new String[]{columnID}, columnIdentifier + "=\"" + DatabaseUtils.sqlEscapeString(c.getIdentifier()) + "\"", null, "", "", "");
int length = cur.getCount();
cur.close();
if(length > 0){
if (length > 0) {
throw new ContactAlreadyAddedException();
}

@@ -78,16 +82,17 @@ public void insertContact(Contact c) throws ContactAlreadyAddedException{
}

public void updateContact(Contact c) {
ContentValues values = new ContentValues(2);
ContentValues values = new ContentValues(5);
values.put(columnIP, c.getAddress());
values.put(columnPhoto, c.getPhoto());
values.put(columnName, c.getName());
values.put(columnIdentifier, c.getIdentifier());
values.put(columnInfo, c.getInfo());

database.update(tableName, values, columnID + "=" + DatabaseUtils.sqlEscapeString(String.valueOf(c.getId())), null);
}

public void deleteContact(Contact c){
public void deleteContact(Contact c) {
database.delete(tableName, columnID + "=" + DatabaseUtils.sqlEscapeString(String.valueOf(c.getId()))
, null);
}
@@ -102,7 +107,8 @@ private void createDatabase() {
columnIP + " TEXT, " +
columnName + " TEXT," +
columnIdentifier + " TEXT," +
columnPhoto + " TEXT" +
columnPhoto + " TEXT," +
columnInfo + " TEXT" +
");");
}

@@ -116,7 +122,7 @@ public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}

class ContactAlreadyAddedException extends Exception{
class ContactAlreadyAddedException extends Exception {
@Override
public String getMessage() {
return "Contact already added";
@@ -171,6 +171,7 @@ private void handleClient(Socket client) {
client.getInetAddress().getHostAddress(),
request.getString("username"),
"",
"",
identifier
);
try {
@@ -109,6 +109,7 @@ private void handleJson(String json) throws JSONException{
object.getString("address"),
object.getString("username"),
"",
"",
object.getString("identifier")
), object.getString("challenge"));
}
@@ -10,12 +10,29 @@
android:orientation="vertical"
android:layout_alignParentLeft="true">

<TextView
android:id="@+id/contact_item_name"
android:layout_width="wrap_content"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="30dp" />
android:orientation="horizontal">

<TextView
android:id="@+id/contact_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black" />

<TextView
android:id="@+id/contact_item_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textStyle="italic"
android:textSize="30dp"
android:paddingStart="10dp"/>


</LinearLayout>


<TextView
android:id="@+id/contact_item_address"

0 comments on commit c3572e8

Please sign in to comment.
You can’t perform that action at this time.