This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
AndroidX migration. #12
Open
kdomingo
wants to merge
2
commits into
milosmns:better-deleting
Choose a base branch
from
kdomingo:better-deleting
base: better-deleting
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
app/src/androidTest/java/me/angrybyte/contactsgenerator/test/app/MainActivityTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,10 +9,11 @@ | |
import android.net.Uri; | ||
import android.os.RemoteException; | ||
import android.provider.ContactsContract; | ||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
import android.util.Log; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
import com.squareup.sqlbrite.BriteContentResolver; | ||
import com.squareup.sqlbrite.SqlBrite; | ||
import com.venmo.cursor.IterableCursor; | ||
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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) | ||
|
@@ -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) { | ||
|
@@ -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 | ||
}; | ||
|
||
|
@@ -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; | ||
} | ||
} | ||
|
@@ -241,12 +247,36 @@ public boolean deleteNextContact() { | |
return true; | ||
} | ||
|
||
private String generatePhoneNumber() { | ||
Random rndGenerator = new Random(); | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We had |
||
} | ||
} | ||
} | ||
|
2 changes: 1 addition & 1 deletion
2
app/src/main/java/me/angrybyte/contactsgenerator/api/Gender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
app/src/main/java/me/angrybyte/contactsgenerator/api/GeneratorStats.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
app/src/main/java/me/angrybyte/contactsgenerator/parser/json/JsonParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,13 +8,14 @@ | |
import android.os.AsyncTask; | ||
import android.os.Handler; | ||
import android.os.IBinder; | ||
import android.support.annotation.FloatRange; | ||
import android.support.annotation.IntRange; | ||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
import android.support.v4.app.NotificationCompat; | ||
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 java.util.Locale; | ||
|
||
import me.angrybyte.contactsgenerator.ProgressActivity; | ||
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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); | ||
|
@@ -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; | ||
} | ||
|
||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 🙂