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 all commits
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
32 changes: 16 additions & 16 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
compileSdkVersion 29
buildToolsVersion "23.0.3"

defaultConfig {
applicationId "me.angrybyte.contactsgenerator"
testApplicationId "me.angrybyte.contactsgenerator.test"

minSdkVersion 14
targetSdkVersion 23
targetSdkVersion 29
versionCode 10
versionName "2.0.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}

dexOptions {
Expand Down Expand Up @@ -49,29 +49,29 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])

compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
compile 'com.google.code.gson:gson:2.4'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.google.code.gson:gson:2.8.6'

compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.pkmmte.view:circularimageview:1.1'
implementation 'com.squareup.okhttp:okhttp:2.4.0'
implementation 'com.pkmmte.view:circularimageview:1.1'

compile 'me.angrybyte.picker:picker:1.2.0'
implementation 'me.angrybyte.picker:picker:1.2.0'

compile 'io.reactivex:rxandroid:1.2.1'
implementation 'io.reactivex:rxandroid:1.2.1'
// Because RxAndroid releases are few and far between, it is recommended you also
// explicitly depend on RxJava's latest version for bug fixes and new features.
compile 'io.reactivex:rxjava:1.2.2'
implementation 'io.reactivex:rxjava:1.2.2'

compile 'com.squareup.sqlbrite:sqlbrite:0.8.0'
compile 'com.venmo.cursor:library:0.4'
implementation 'com.squareup.sqlbrite:sqlbrite:0.8.0'
implementation 'com.venmo.cursor:library:0.4'

// set this dependency to use the JUnit instrumentation runner
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
// set this dependency to use JUnit 4 rules
androidTestCompile 'com.android.support.test:rules:0.4.1'
androidTestImplementation 'androidx.test:rules:1.3.0'
// set this dependency to build and run Espresso tests
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
// Lollipop only: set this dependency to build and run UI Automator tests
// androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

package me.angrybyte.contactsgenerator.test.app;

import android.support.v7.widget.Toolbar;
import androidx.appcompat.widget.Toolbar;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.SmallTest;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import android.net.NetworkInfo;
import android.os.Build;
import android.os.RemoteException;
import android.support.v4.app.ActivityCompat;
import androidx.core.app.ActivityCompat;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,24 @@
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresPermission;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresPermission;
import androidx.core.app.ActivityCompat;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.Toast;

import me.angrybyte.contactsgenerator.api.ContactOperations;
import me.angrybyte.contactsgenerator.api.Gender;
import me.angrybyte.contactsgenerator.api.Operations;
import me.angrybyte.contactsgenerator.parser.data.Person;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.FloatRange;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import androidx.annotation.FloatRange;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import android.graphics.Color;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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
@@ -1,7 +1,7 @@

package me.angrybyte.contactsgenerator.api;

import android.support.annotation.StringDef;
import androidx.annotation.StringDef;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

package me.angrybyte.contactsgenerator.api;

import android.support.annotation.Nullable;
import androidx.annotation.Nullable;

import java.io.Serializable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RawRes;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RawRes;
import android.util.Log;

import com.squareup.okhttp.OkHttpClient;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

package me.angrybyte.contactsgenerator.parser.json;

import android.support.annotation.NonNull;
import androidx.annotation.NonNull;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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
Loading