Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

AndroidX migration. #12

Open
wants to merge 2 commits into
base: better-deleting
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
import android.net.Uri;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.util.Log;

import com.squareup.sqlbrite.BriteContentResolver;
import com.squareup.sqlbrite.SqlBrite;
Expand All @@ -22,7 +23,9 @@
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import me.angrybyte.contactsgenerator.parser.data.Person;
import rx.Observable;
Expand All @@ -42,15 +45,15 @@ public class ContactOperations {
//<editor-fold desc="Contact query constants">
private static final String EXAMPLE_DOMAIN = "@example.com";
private static final Uri CONTENT_URI = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
private static final String[] PROJECTION = new String[] {
private static final String[] PROJECTION = new String[]{
ContactsContract.CommonDataKinds.Email.CONTACT_ID,
ContactsContract.CommonDataKinds.Email.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.ADDRESS,
ContactsContract.CommonDataKinds.Email.PHOTO_THUMBNAIL_URI,
ContactsContract.CommonDataKinds.Email.LOOKUP_KEY
};
private static final String SELECTION = ContactsContract.CommonDataKinds.Email.ADDRESS + " LIKE ?";
private static final String[] SELECTION_ARGS = new String[] {"%" + EXAMPLE_DOMAIN};
private static final String[] SELECTION_ARGS = new String[]{"%" + EXAMPLE_DOMAIN};

private static final int ID = 0;
private static final int DISPLAY_NAME = 1;
Expand Down Expand Up @@ -94,16 +97,20 @@ public void storeContact(@NonNull Person person) throws RemoteException, Operati

operations.add(providerOperation.build());

providerOperation = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,
person.getPhone())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_HOME);
// Phone
final List<String> phoneNumbers = Arrays.asList(person.getPhone(), generatePhoneNumber());
final Random rndPhoneTypeGenerator = new Random();

operations.add(providerOperation.build());
for (String phoneNumber : phoneNumbers) {
operations.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,
phoneNumber)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
getPhoneType(rndPhoneTypeGenerator)).build());
}

providerOperation = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
Expand Down Expand Up @@ -158,7 +165,6 @@ public void storeContacts(@NonNull List<Person> persons) throws RemoteException,
* @param matchingEmail You can parametrize the search query of the cursor with an email address. Sending {@code
* null} will find all the contacts, while sending in a valid email address will find contacts
* with only that email address associated with them
*
* @return {@code True} if the cursor has been opened successfully, {@code false} otherwise
*/
public boolean prepareCursorForScrubbing(@Nullable String matchingEmail) {
Expand All @@ -168,16 +174,16 @@ public boolean prepareCursorForScrubbing(@Nullable String matchingEmail) {
String[] whereArgs;
if (matchingEmail != null) {
mMatchingEmail = matchingEmail;
projection = new String[] {
projection = new String[]{
ContactsContract.Contacts.LOOKUP_KEY, ContactsContract.CommonDataKinds.Email.ADDRESS
};

where = ContactsContract.CommonDataKinds.Email.ADDRESS + " LIKE ?";
whereArgs = new String[] {
whereArgs = new String[]{
"%" + matchingEmail
};
} else {
projection = new String[] {
projection = new String[]{
ContactsContract.Contacts.LOOKUP_KEY
};

Expand Down Expand Up @@ -225,7 +231,7 @@ public boolean deleteNextContact() {
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
int deletedRows = mContentResolver.delete(uri, null, null);
if (deletedRows < 1) {
Log.w(TAG, "Nothing deleted for URI " + String.valueOf(uri));
Log.w(TAG, "Nothing deleted for URI " + uri);
return false;
}
}
Expand All @@ -241,12 +247,36 @@ public boolean deleteNextContact() {
return true;
}

private String generatePhoneNumber() {
Random rndGenerator = new Random();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's already a rndPhoneTypeGenerator which can be a generic random generator. There's no security concern with this app, I believe we can just reuse it 🙂

StringBuilder stringBuilder = new StringBuilder();

while (stringBuilder.length() < 11) {
stringBuilder.append(rndGenerator.nextInt(9));
}

return stringBuilder.toString();
}

private int getPhoneType(Random rndPhoneTypeGenerator) {
switch (rndPhoneTypeGenerator.nextInt(2) + 1) {

case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
return ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE;

case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
return ContactsContract.CommonDataKinds.Phone.TYPE_WORK;

default: return ContactsContract.CommonDataKinds.Phone.TYPE_HOME;
}
}

private void close(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (Exception e) {
Log.w(TAG, "Cannot close the closeable " + String.valueOf(closeable));
Log.w(TAG, "Cannot close the closeable " + closeable);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had String.valueOf() calls to explicitly convert null values into "null" (string value of 4 chars).
Why do you think it's not necessary anymore?

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
import android.os.AsyncTask;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.FloatRange;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import android.util.Log;

import java.util.Locale;

Expand All @@ -29,6 +30,7 @@ public class GeneratorService extends Service implements ServiceApi, OnGenerateP

public static final String TAG = GeneratorService.class.getSimpleName();
public static final int NOTIFICATION_ID = 1475369;
public static final String CONTACTS_GENERATOR_CHANNEL = "CONTACTS_GENERATOR_CHANNEL";

private Person mLastGenerated;
private Handler mHandler;
Expand All @@ -40,6 +42,7 @@ public class GeneratorService extends Service implements ServiceApi, OnGenerateP
private OnGenerateResultListener mResultListener;
private OnGenerateProgressListener mProgressListener;
private AsyncTask<Void, Void, Void> mDeletionTask;

private boolean mIsForceStopped;
private boolean mIsGenerating;
private boolean mIsDeleting;
Expand All @@ -57,7 +60,7 @@ public void onCreate() {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Starting " + TAG + " with intent " + String.valueOf(intent));
Log.d(TAG, "Starting " + TAG + " with intent " + intent);

if (ServiceApi.DELETE_CONTACTS_ACTION.equals(intent.getAction())) {
mContactOperations = new ContactOperations(this);
Expand All @@ -72,7 +75,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "Binding " + TAG + " with intent " + String.valueOf(intent));
Log.d(TAG, "Binding " + TAG + " with intent " + intent);
return mBinder;
}

Expand Down Expand Up @@ -108,7 +111,7 @@ public void onTaskRemoved(Intent rootIntent) {
}

private void showNotification() {
mBuilder = new NotificationCompat.Builder(this);
mBuilder = new NotificationCompat.Builder(this, CONTACTS_GENERATOR_CHANNEL);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

mBuilder.setCategory(NotificationCompat.CATEGORY_SERVICE);
mBuilder.setSmallIcon(R.drawable.ic_stat_generator);
mBuilder.setContentTitle(getString(R.string.app_name));
Expand Down