Skip to content

Commit

Permalink
Issue #2 points 1 and 3 resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
xfarrow committed Oct 27, 2022
1 parent 884996c commit 86cf54f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

androidkey.jks
1 change: 0 additions & 1 deletion src/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

<uses-permission android:name="android.permission.VIBRATE" />

<application
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public class RingerActivity extends AppCompatActivity {

private Ringtone ringtoneManager;
private Vibrator v;
private Vibrator vibrator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -60,7 +60,7 @@ private void startRinging(){
ringtoneManager.setVolume(1f);
ringtoneManager.play();

v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

// Start without a delay
// Vibrate for 500 milliseconds
Expand All @@ -70,17 +70,17 @@ private void startRinging(){
// '0' is actually the index at which the pattern keeps repeating from (the start)
// To repeat the pattern from any other point, you could increase the index, e.g. '1'
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
v.vibrate(VibrationEffect.createWaveform(pattern, 0));
vibrator.vibrate(VibrationEffect.createWaveform(pattern, 0));
}
else{
v.vibrate(pattern,0);
vibrator.vibrate(pattern,0);
}
}

private void stopRinging(){
ringtoneManager.stop();
v.cancel();
finishAffinity();
vibrator.cancel();
finishAndRemoveTask();
}

@Override
Expand Down
5 changes: 5 additions & 0 deletions src/app/src/main/java/com/xfarrow/locatemydevice/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,9 @@ public static String extractCountryCodeFromPhoneNumber(String phoneNumber){
return String.valueOf(numberProto.getCountryCode());
}

// We'll remove parenthesis, dashes and whitespaces
public static String normalizePhoneNumber(String phoneNo){
return phoneNo.replaceAll("[-()\\s]", "");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public class WhitelistContactsActivity extends AppCompatActivity {

private ListView contactsListView;
private ArrayList<String> contacts;
private ArrayList<String> contactsListView_datasource;
private ArrayAdapter<String> listviewAdapter;
private final WhitelistDbHandler whitelistDbHandler = new WhitelistDbHandler(this);

Expand All @@ -35,10 +35,10 @@ protected void onCreate(Bundle savedInstanceState) {
setViews();
setListeners();

contacts = whitelistDbHandler.getAllContacts();
contactsListView_datasource = whitelistDbHandler.getAllContacts();
listviewAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
contacts);
contactsListView_datasource);
contactsListView.setAdapter(listviewAdapter);
}

Expand All @@ -58,9 +58,7 @@ public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
String number = (String)adapterView.getItemAtPosition(i);
contacts.remove(number);
listviewAdapter.notifyDataSetChanged();
whitelistDbHandler.deleteContact(number);
removeNumberFromWhiteList(number);
return true;
}
});
Expand Down Expand Up @@ -98,23 +96,27 @@ protected void onActivityResult (int requestCode, int resultCode, Intent data) {
int phoneIndex = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int contactNameIndex = c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
String number = c.getString(phoneIndex);
String contactName = c.getString(contactNameIndex);
contactSelected(number);
String contactName = c.getString(contactNameIndex); // planned to show contact's name as well
addNumberToWhiteList(number);
}
c.close();
}
}

private void contactSelected(String phoneNo){
// We'll replace parenthesis, dashes and whitespaces to obtain a valid phone number
phoneNo = phoneNo.replaceAll("[-()\\s]", "");

if(contacts.contains(phoneNo)){
private void addNumberToWhiteList(String phoneNo){
phoneNo = Utils.normalizePhoneNumber(phoneNo);
if(contactsListView_datasource.contains(phoneNo)){
Toast.makeText(this, "Contact already in the list", Toast.LENGTH_SHORT).show();
return;
}
whitelistDbHandler.addContact(phoneNo);
contacts.add(phoneNo);
contactsListView_datasource.add(phoneNo);
listviewAdapter.notifyDataSetChanged();
}

private void removeNumberFromWhiteList(String phoneNo){
whitelistDbHandler.deleteContact(phoneNo);
contactsListView_datasource.remove(phoneNo);
listviewAdapter.notifyDataSetChanged();
}
}

0 comments on commit 86cf54f

Please sign in to comment.