Skip to content

Commit

Permalink
Merge pull request #2351 from marunjar/reduce_contact_reloads
Browse files Browse the repository at this point in the history
reduce contact reloads
  • Loading branch information
marunjar authored Dec 15, 2024
2 parents 4f924dd + 9800df6 commit 7db7c2f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
6 changes: 3 additions & 3 deletions app/src/main/java/fr/neamar/kiss/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -812,15 +812,15 @@ public void updateSearchRecords() {
* @param query the query on which to search
*/
private void updateSearchRecords(boolean isRefresh, String query) {
resetTask();
dismissPopup();

if (isRefresh && isViewingAllApps()) {
// Refreshing while viewing all apps (for instance app installed or uninstalled in the background)
runTask(new ApplicationsSearcher(this, isRefresh));
return;
}

resetTask();
dismissPopup();

forwarderManager.updateSearchRecords(isRefresh, query);

if (query.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package fr.neamar.kiss.dataprovider;

import android.database.ContentObserver;
import android.net.Uri;
import android.provider.ContactsContract;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import fr.neamar.kiss.loader.LoadContactsPojos;
import fr.neamar.kiss.normalizer.PhoneNormalizer;
import fr.neamar.kiss.normalizer.StringNormalizer;
Expand All @@ -18,8 +26,26 @@ public class ContactsProvider extends Provider<ContactsPojo> {

@Override
public void onChange(boolean selfChange) {
Log.v(TAG, "Contact changed, reloading provider.");
onChange(selfChange, null);
}

@Override
public void onChange(boolean selfChange, @Nullable Uri uri) {
Log.v(TAG, "Contact changed, reloading provider: " + uri);
onChange(selfChange, uri, 0);
}

@Override
public void onChange(boolean selfChange, @Nullable Uri uri, int flags) {
Log.v(TAG, "Contact changed, reloading provider: " + uri + ", flags: " + flags);
onChange(selfChange, Collections.singletonList(uri), flags);
}

@Override
public void onChange(boolean selfChange, @NonNull Collection<Uri> uris, int flags) {
//reload contacts
Log.i(TAG, "Contacts changed, reloading provider.");
Log.v(TAG, "Contacts changed, reloading provider: " + uris + ", flags: " + flags);
reload();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ void initialize(LoadPojos<T> loader) {
private void cancelInitialize() {
if (this.loader != null) {
this.loader.cancel(false);
this.loader.setProvider(null);
this.loader = null;
Log.i(TAG, "Cancelling provider: " + this.getClass().getSimpleName());
}
Expand Down
11 changes: 7 additions & 4 deletions app/src/main/java/fr/neamar/kiss/loader/LoadPojos.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public abstract class LoadPojos<T extends Pojo> extends AsyncTask<Void, Void, Li

final WeakReference<Context> context;
String pojoScheme = "(none)://";
private WeakReference<Provider<T>> provider;
private WeakReference<Provider<T>> providerReference;

LoadPojos(Context context, String pojoScheme) {
super();
Expand All @@ -22,7 +22,7 @@ public abstract class LoadPojos<T extends Pojo> extends AsyncTask<Void, Void, Li
}

public void setProvider(Provider<T> provider) {
this.provider = new WeakReference<>(provider);
this.providerReference = new WeakReference<>(provider);
}

public String getPojoScheme() {
Expand All @@ -32,8 +32,11 @@ public String getPojoScheme() {
@Override
protected void onPostExecute(List<T> result) {
super.onPostExecute(result);
if (provider != null && !isCancelled()) {
provider.get().loadOver(result);
if (providerReference != null) {
Provider<T> provider = providerReference.get();
if (provider != null && !isCancelled()) {
provider.loadOver(result);
}
}
}

Expand Down

0 comments on commit 7db7c2f

Please sign in to comment.