Skip to content

Commit

Permalink
Implements issue #11. Contacts present in both lists are now marked w…
Browse files Browse the repository at this point in the history
…ith a tick which replaces the copy button.
  • Loading branch information
yeriomin committed Jun 30, 2016
1 parent ef70596 commit 8332ec7
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.github.yeriomin.dumbphoneassistant;

import android.telephony.PhoneNumberUtils;

public class Contact {

private String id = null;
private String id;
private String name;
private String number;
private String label;
Expand Down Expand Up @@ -42,33 +44,11 @@ protected Contact(String id, String name, String number, String label) {
this.label = label;
}

/**
* Null-safe string compare
*/
private boolean compareStrings(final String one, final String two) {
if (one == null ^ two == null) {
return false;
}
if (one == null && two == null) {
return true;
}
return one.compareTo(two) == 0;
}

@Override
public boolean equals(Object o) {
// if not Contact, can't be true
if(!(o instanceof Contact)) {
return false;
}
Contact c = (Contact)o;

// only if id's present, compare them
if((id != null) && (id.length()) > 0 && (c.id.length() > 0)) {
return c.id.compareTo(id) == 0;
}

// finally if numbers not equal...
return compareStrings(number, c.number);
return PhoneNumberUtils.compare(number, ((Contact) o).number);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
Expand All @@ -26,13 +29,14 @@

public class ManageContactsActivity extends TabActivity {

private ListView phoneView;
private ListView simView;
private PhoneRowAdapter phoneAdapter;
private SimRowAdapter simAdapter;
private List<Contact> phoneContacts;
private List<Contact> simContacts;

private SimUtil simUtil;
private PhoneUtil phoneUtil;

private ProgressDialog progressDialog;
private PermissionManager permissionManager;

Expand Down Expand Up @@ -73,25 +77,12 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// if this was called after editing a phone contact, refresh the view
if (requestCode == EDIT_REQUEST_CODE) {
phoneContacts = phoneUtil.get();
refreshPhoneListView();
phoneAdapter.notifyDataSetChanged();
simAdapter.notifyDataSetChanged();
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}

/**
* refreshes the sim contacts ListViews using the current values stored in simContacts
*/
private void refreshSimListView() {
simView.setAdapter(new SimRowAdapter(simContacts));
}

/**
* refreshes the phone contacts ListView using the current values stored in phoneContacts
*/
private void refreshPhoneListView() {
phoneView.setAdapter(new PhoneRowAdapter(phoneContacts));
}

/**
* initializes the Phone and SIM ListViews by reading the phoneContacts and simContacts, setting up
Expand All @@ -115,7 +106,7 @@ private void initListViews() {
progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

phoneView = (ListView) findViewById(R.id.phoneview);
ListView phoneView = (ListView) findViewById(R.id.phoneview);
View headerPhoneView = lf.inflate(R.layout.list_item_phone, phoneView, false);
headerPhoneView.findViewById(R.id.button_edit).setVisibility(View.INVISIBLE);
TextView titlePhone = (TextView) headerPhoneView.findViewById(R.id.text_contact_name);
Expand All @@ -138,7 +129,7 @@ public void onClick(View v) {
});
phoneView.addHeaderView(headerPhoneView);

simView = (ListView) findViewById(R.id.simview);
ListView simView = (ListView) findViewById(R.id.simview);
View headerSimView = lf.inflate(R.layout.list_item_sim, simView, false);
TextView titleSim = (TextView) headerSimView.findViewById(R.id.text_contact_name);
titleSim.setText(getString(R.string.title_move_all_contacts_to_phone));
Expand Down Expand Up @@ -187,9 +178,11 @@ public void onClick(DialogInterface i, int which) {
simView.addHeaderView(headerSimView);

phoneContacts = phoneUtil.get();
refreshPhoneListView();
phoneAdapter = new PhoneRowAdapter(phoneContacts);
phoneView.setAdapter(phoneAdapter);
simContacts = simUtil.get();
refreshSimListView();
simAdapter = new SimRowAdapter(simContacts);
simView.setAdapter(simAdapter);
}

private void startContactEditActivity(Contact contact) {
Expand Down Expand Up @@ -285,7 +278,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(this.listItemId, parent, false);
}
convertView.setClickable(false);

Contact contact = (Contact)this.getItem(position);

((TextView)convertView.findViewById(R.id.text_contact_name)).setText(contact.getName());
Expand Down Expand Up @@ -320,23 +313,32 @@ public void onClick(View v) {
buttonEdit.setOnClickListener(lEdit);
buttonEdit.setTag(contact);

ImageView imageTick = (ImageView) view.findViewById(R.id.tick_to_sim);
ImageButton buttonToSim = (ImageButton) view.findViewById(R.id.button_to_sim);
View.OnClickListener lToSim = new View.OnClickListener() {
@Override
public void onClick(View v) {
String message;
try {
copyToSim((Contact) v.getTag());
message = getString(R.string.confirm_sim_contact_stored);
refreshSimListView();
} catch (Exception e) {
message = e.getMessage();
if (simContacts.contains(contact)) {
buttonToSim.setVisibility(View.GONE);
imageTick.setVisibility(View.VISIBLE);
} else {
View.OnClickListener lToSim = new View.OnClickListener() {
@Override
public void onClick(View v) {
String message;
try {
copyToSim((Contact) v.getTag());
message = getString(R.string.confirm_sim_contact_stored);
phoneAdapter.notifyDataSetChanged();
simAdapter.notifyDataSetChanged();
} catch (Exception e) {
message = e.getMessage();
}
Toast.makeText(ManageContactsActivity.this, message, Toast.LENGTH_SHORT).show();
}
Toast.makeText(ManageContactsActivity.this, message, Toast.LENGTH_SHORT).show();
}
};
buttonToSim.setOnClickListener(lToSim);
buttonToSim.setTag(contact);
};
buttonToSim.setOnClickListener(lToSim);
buttonToSim.setTag(contact);
buttonToSim.setVisibility(View.VISIBLE);
imageTick.setVisibility(View.GONE);
}

return view;
}
Expand All @@ -353,24 +355,43 @@ public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
Contact contact = (Contact) this.getItem(position);

RelativeLayout.LayoutParams paramsTick = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);
paramsTick.addRule(RelativeLayout.RIGHT_OF, R.id.tick_to_phone);
RelativeLayout.LayoutParams paramsButton = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
paramsButton.addRule(RelativeLayout.RIGHT_OF, R.id.button_to_phone);

LinearLayout layout = (LinearLayout) view.findViewById(R.id.name_and_number);
ImageView imageTick = (ImageView) view.findViewById(R.id.tick_to_phone);
ImageButton buttonToPhone = (ImageButton) view.findViewById(R.id.button_to_phone);
View.OnClickListener lToPhone = new View.OnClickListener() {
@Override
public void onClick(View v) {
String message;
try {
Contact contact = (Contact) v.getTag();
copyToPhone(contact);
message = getString(R.string.confirm_phone_contact_number_stored, contact.getName());
refreshPhoneListView();
} catch (Exception e) {
message = e.getMessage();
if (phoneContacts.contains(contact)) {
buttonToPhone.setVisibility(View.GONE);
imageTick.setVisibility(View.VISIBLE);
layout.setLayoutParams(paramsTick);
} else {
View.OnClickListener lToPhone = new View.OnClickListener() {
@Override
public void onClick(View v) {
String message;
try {
Contact contact = (Contact) v.getTag();
copyToPhone(contact);
message = getString(R.string.confirm_phone_contact_number_stored, contact.getName());
phoneAdapter.notifyDataSetChanged();
simAdapter.notifyDataSetChanged();
} catch (Exception e) {
message = e.getMessage();
}
Toast.makeText(ManageContactsActivity.this, message, Toast.LENGTH_SHORT).show();
}
Toast.makeText(ManageContactsActivity.this, message, Toast.LENGTH_SHORT).show();
}
};
buttonToPhone.setOnClickListener(lToPhone);
buttonToPhone.setTag(contact);
};
buttonToPhone.setOnClickListener(lToPhone);
buttonToPhone.setTag(contact);
buttonToPhone.setVisibility(View.VISIBLE);
imageTick.setVisibility(View.GONE);
layout.setLayoutParams(paramsButton);
}

ImageButton buttonDelete = (ImageButton) view.findViewById(R.id.button_delete);
View.OnClickListener lDelete = new View.OnClickListener() {
Expand Down Expand Up @@ -407,7 +428,8 @@ public void onClick(DialogInterface dialog, int which) {
boolean success = false;
try {
success = deleteFromSim(contact);
refreshSimListView();
simAdapter.notifyDataSetChanged();
phoneAdapter.notifyDataSetChanged();
} catch (Exception e) {
// TODO: decide what to do with failed deletions
}
Expand Down Expand Up @@ -474,11 +496,8 @@ public void run() {
@Override
public void run() {
progressDialog.dismiss();
if (mode == COPY_ALL_TO_PHONE) {
refreshPhoneListView();
} else {
refreshSimListView();
}
phoneAdapter.notifyDataSetChanged();
simAdapter.notifyDataSetChanged();
if (failures > 0) {
String message = getString(R.string.error_bulk_copy, failures);
if (mode == COPY_ALL_TO_SIM) {
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/layout/list_item_phone.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,14 @@
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

<ImageView
android:id="@+id/tick_to_sim"
android:src="@drawable/ic_tick"
android:layout_width="@dimen/ImageButtonDim"
android:layout_height="@dimen/ImageButtonDim"
android:visibility="gone"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

</RelativeLayout>
9 changes: 9 additions & 0 deletions app/src/main/res/layout/list_item_sim.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
android:layout_width="match_parent"
android:layout_height="@dimen/ImageButtonDim" >


<ImageView
android:id="@+id/tick_to_phone"
android:src="@drawable/ic_tick"
android:layout_width="@dimen/ImageButtonDim"
android:layout_height="@dimen/ImageButtonDim"
android:visibility="gone" />

<ImageButton
android:id="@+id/button_to_phone"
android:src="@drawable/ic_arrow_left"
Expand All @@ -12,6 +20,7 @@
android:layout_height="@dimen/ImageButtonDim" />

<LinearLayout
android:id="@+id/name_and_number"
android:orientation="vertical"
android:gravity="center"
android:layout_width="wrap_content"
Expand Down

0 comments on commit 8332ec7

Please # to comment.