Search in sources :

Example 1 with CallLogMutations

use of com.android.dialer.calllog.datasources.CallLogMutations in project android_packages_apps_Dialer by LineageOS.

the class RefreshAnnotatedCallLogWorker method rebuild.

// Uses try-with-resources
@TargetApi(Build.VERSION_CODES.M)
@WorkerThread
private void rebuild(Context appContext) throws RemoteException, OperationApplicationException {
    Assert.isWorkerThread();
    CallLogMutations mutations = new CallLogMutations();
    // System call log data source must go first!
    CallLogDataSource systemCallLogDataSource = dataSources.getSystemCallLogDataSource();
    String dataSourceName = getName(systemCallLogDataSource);
    LogUtil.i("RefreshAnnotatedCallLogWorker.rebuild", "filling %s", dataSourceName);
    long startTime = System.currentTimeMillis();
    systemCallLogDataSource.fill(appContext, mutations);
    LogUtil.i("RefreshAnnotatedCallLogWorker.rebuild", "%s.fill took: %dms", dataSourceName, System.currentTimeMillis() - startTime);
    for (CallLogDataSource dataSource : dataSources.getDataSourcesExcludingSystemCallLog()) {
        dataSourceName = getName(dataSource);
        LogUtil.i("RefreshAnnotatedCallLogWorker.rebuild", "filling %s", dataSourceName);
        startTime = System.currentTimeMillis();
        dataSource.fill(appContext, mutations);
        LogUtil.i("CallLogFramework.rebuild", "%s.fill took: %dms", dataSourceName, System.currentTimeMillis() - startTime);
    }
    LogUtil.i("RefreshAnnotatedCallLogWorker.rebuild", "applying mutations to database");
    startTime = System.currentTimeMillis();
    CallLogDatabaseComponent.get(appContext).mutationApplier().applyToDatabase(mutations, appContext);
    LogUtil.i("RefreshAnnotatedCallLogWorker.rebuild", "applyToDatabase took: %dms", System.currentTimeMillis() - startTime);
    for (CallLogDataSource dataSource : dataSources.getDataSourcesIncludingSystemCallLog()) {
        dataSourceName = getName(dataSource);
        LogUtil.i("RefreshAnnotatedCallLogWorker.rebuild", "onSuccessfulFill'ing %s", dataSourceName);
        startTime = System.currentTimeMillis();
        dataSource.onSuccessfulFill(appContext);
        LogUtil.i("CallLogFramework.rebuild", "%s.onSuccessfulFill took: %dms", dataSourceName, System.currentTimeMillis() - startTime);
    }
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(appContext);
    sharedPreferences.edit().putBoolean(CallLogFramework.PREF_FORCE_REBUILD, false).apply();
}
Also used : CallLogMutations(com.android.dialer.calllog.datasources.CallLogMutations) SharedPreferences(android.content.SharedPreferences) CallLogDataSource(com.android.dialer.calllog.datasources.CallLogDataSource) WorkerThread(android.support.annotation.WorkerThread) TargetApi(android.annotation.TargetApi)

Example 2 with CallLogMutations

use of com.android.dialer.calllog.datasources.CallLogMutations in project android_packages_apps_Dialer by LineageOS.

the class RefreshAnnotatedCallLogWorker method rebuild.

private ListenableFuture<RefreshResult> rebuild(boolean isBuilt) {
    CallLogMutations mutations = new CallLogMutations();
    // Start by filling the data sources--the system call log data source must go first!
    CallLogDataSource systemCallLogDataSource = dataSources.getSystemCallLogDataSource();
    ListenableFuture<Void> fillFuture = systemCallLogDataSource.fill(mutations);
    String systemEventName = eventNameForFill(systemCallLogDataSource, isBuilt);
    futureTimer.applyTiming(fillFuture, systemEventName);
    // passed from source to source.
    for (CallLogDataSource dataSource : dataSources.getDataSourcesExcludingSystemCallLog()) {
        fillFuture = Futures.transformAsync(fillFuture, unused -> {
            ListenableFuture<Void> dataSourceFuture = dataSource.fill(mutations);
            String eventName = eventNameForFill(dataSource, isBuilt);
            futureTimer.applyTiming(dataSourceFuture, eventName);
            return dataSourceFuture;
        }, lightweightExecutorService);
    }
    futureTimer.applyTiming(fillFuture, eventNameForOverallFill(isBuilt));
    // After all data sources are filled, apply mutations (at this point "fillFuture" is the result
    // of filling the last data source).
    ListenableFuture<Void> applyMutationsFuture = Futures.transformAsync(fillFuture, unused -> {
        ListenableFuture<Void> mutationApplierFuture = mutationApplier.applyToDatabase(mutations, appContext);
        futureTimer.applyTiming(mutationApplierFuture, eventNameForApplyMutations(isBuilt));
        return mutationApplierFuture;
    }, lightweightExecutorService);
    Futures.addCallback(Futures.transformAsync(applyMutationsFuture, unused -> callLogCacheUpdater.updateCache(mutations), MoreExecutors.directExecutor()), new DefaultFutureCallback<>(), MoreExecutors.directExecutor());
    // After mutations applied, call onSuccessfulFill for each data source (in parallel).
    ListenableFuture<List<Void>> onSuccessfulFillFuture = Futures.transformAsync(applyMutationsFuture, unused -> {
        List<ListenableFuture<Void>> onSuccessfulFillFutures = new ArrayList<>();
        for (CallLogDataSource dataSource : dataSources.getDataSourcesIncludingSystemCallLog()) {
            ListenableFuture<Void> dataSourceFuture = dataSource.onSuccessfulFill();
            onSuccessfulFillFutures.add(dataSourceFuture);
            String eventName = eventNameForOnSuccessfulFill(dataSource, isBuilt);
            futureTimer.applyTiming(dataSourceFuture, eventName);
        }
        ListenableFuture<List<Void>> allFutures = Futures.allAsList(onSuccessfulFillFutures);
        futureTimer.applyTiming(allFutures, eventNameForOverallOnSuccessfulFill(isBuilt));
        return allFutures;
    }, lightweightExecutorService);
    // After onSuccessfulFill is called for every data source, write the shared pref.
    return Futures.transform(onSuccessfulFillFuture, unused -> {
        sharedPreferences.edit().putBoolean(SharedPrefKeys.FORCE_REBUILD, false).apply();
        callLogState.markBuilt();
        return mutations.isEmpty() ? RefreshResult.REBUILT_BUT_NO_CHANGES_NEEDED : RefreshResult.REBUILT_AND_CHANGES_NEEDED;
    }, backgroundExecutorService);
}
Also used : MutationApplier(com.android.dialer.calllog.database.MutationApplier) Context(android.content.Context) LogUtil(com.android.dialer.common.LogUtil) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Metrics(com.android.dialer.metrics.Metrics) Singleton(javax.inject.Singleton) CallLogDataSource(com.android.dialer.calllog.datasources.CallLogDataSource) CallLogMutations(com.android.dialer.calllog.datasources.CallLogMutations) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) BackgroundExecutor(com.android.dialer.common.concurrent.Annotations.BackgroundExecutor) SharedPrefKeys(com.android.dialer.calllog.constants.SharedPrefKeys) Unencrypted(com.android.dialer.storage.Unencrypted) DialerFutureSerializer(com.android.dialer.common.concurrent.DialerFutureSerializer) ApplicationContext(com.android.dialer.inject.ApplicationContext) DataSources(com.android.dialer.calllog.datasources.DataSources) DialerFutures(com.android.dialer.common.concurrent.DialerFutures) LightweightExecutor(com.android.dialer.common.concurrent.Annotations.LightweightExecutor) LogCatMode(com.android.dialer.metrics.FutureTimer.LogCatMode) FutureTimer(com.android.dialer.metrics.FutureTimer) Futures(com.google.common.util.concurrent.Futures) List(java.util.List) SharedPreferences(android.content.SharedPreferences) DefaultFutureCallback(com.android.dialer.common.concurrent.DefaultFutureCallback) Preconditions(com.google.common.base.Preconditions) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) CallLogMutations(com.android.dialer.calllog.datasources.CallLogMutations) ArrayList(java.util.ArrayList) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ArrayList(java.util.ArrayList) List(java.util.List) CallLogDataSource(com.android.dialer.calllog.datasources.CallLogDataSource)

Example 3 with CallLogMutations

use of com.android.dialer.calllog.datasources.CallLogMutations in project android_packages_apps_Dialer by LineageOS.

the class CallLogCacheUpdater method updateCacheInternal.

private void updateCacheInternal(CallLogMutations mutations) {
    ArrayList<ContentProviderOperation> operations = new ArrayList<>();
    Stream.concat(mutations.getInserts().entrySet().stream(), mutations.getUpdates().entrySet().stream()).limit(CACHE_UPDATE_LIMIT).forEach(entry -> {
        ContentValues values = entry.getValue();
        if (!values.containsKey(AnnotatedCallLog.NUMBER_ATTRIBUTES) || !values.containsKey(AnnotatedCallLog.NUMBER)) {
            return;
        }
        DialerPhoneNumber dialerPhoneNumber = ProtoParsers.getTrusted(values, AnnotatedCallLog.NUMBER, DialerPhoneNumber.getDefaultInstance());
        NumberAttributes numberAttributes = ProtoParsers.getTrusted(values, AnnotatedCallLog.NUMBER_ATTRIBUTES, NumberAttributes.getDefaultInstance());
        operations.add(ContentProviderOperation.newUpdate(ContentUris.withAppendedId(Calls.CONTENT_URI, entry.getKey())).withValue(Calls.CACHED_FORMATTED_NUMBER, values.getAsString(AnnotatedCallLog.FORMATTED_NUMBER)).withValue(Calls.CACHED_LOOKUP_URI, numberAttributes.getLookupUri()).withValue(Calls.CACHED_NAME, numberAttributes.getName()).withValue(Calls.CACHED_NORMALIZED_NUMBER, dialerPhoneNumber.getNormalizedNumber()).withValue(Calls.CACHED_NUMBER_LABEL, numberAttributes.getNumberTypeLabel()).withValue(Calls.CACHED_NUMBER_TYPE, Phone.TYPE_CUSTOM).withValue(Calls.CACHED_PHOTO_ID, numberAttributes.getPhotoId()).withValue(Calls.CACHED_PHOTO_URI, numberAttributes.getPhotoUri()).withSelection(Calls.CACHED_NAME + " IS NOT ?", new String[] { numberAttributes.getName() }).build());
    });
    try {
        int count = Arrays.stream(appContext.getContentResolver().applyBatch(CallLog.AUTHORITY, operations)).mapToInt(result -> result.count).sum();
        LogUtil.i("CallLogCacheUpdater.updateCache", "updated %d rows", count);
    } catch (OperationApplicationException | RemoteException e) {
        throw new IllegalStateException(e);
    }
}
Also used : ContentValues(android.content.ContentValues) ContentProviderOperation(android.content.ContentProviderOperation) NumberAttributes(com.android.dialer.NumberAttributes) Context(android.content.Context) LogUtil(com.android.dialer.common.LogUtil) Arrays(java.util.Arrays) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) AnnotatedCallLog(com.android.dialer.calllog.database.contract.AnnotatedCallLogContract.AnnotatedCallLog) RemoteException(android.os.RemoteException) CallLogMutations(com.android.dialer.calllog.datasources.CallLogMutations) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) VisibleForTesting(android.support.annotation.VisibleForTesting) BackgroundExecutor(com.android.dialer.common.concurrent.Annotations.BackgroundExecutor) ProtoParsers(com.android.dialer.protos.ProtoParsers) ApplicationContext(com.android.dialer.inject.ApplicationContext) CallLog(android.provider.CallLog) Phone(android.provider.ContactsContract.CommonDataKinds.Phone) DialerPhoneNumber(com.android.dialer.DialerPhoneNumber) OperationApplicationException(android.content.OperationApplicationException) Futures(com.google.common.util.concurrent.Futures) Stream(java.util.stream.Stream) Calls(android.provider.CallLog.Calls) ContentValues(android.content.ContentValues) ContentUris(android.content.ContentUris) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) ContentProviderOperation(android.content.ContentProviderOperation) ArrayList(java.util.ArrayList) DialerPhoneNumber(com.android.dialer.DialerPhoneNumber) RemoteException(android.os.RemoteException) NumberAttributes(com.android.dialer.NumberAttributes) OperationApplicationException(android.content.OperationApplicationException)

Example 4 with CallLogMutations

use of com.android.dialer.calllog.datasources.CallLogMutations in project android_packages_apps_Dialer by MoKee.

the class RefreshAnnotatedCallLogWorker method rebuild.

private ListenableFuture<RefreshResult> rebuild(boolean isBuilt) {
    CallLogMutations mutations = new CallLogMutations();
    // Start by filling the data sources--the system call log data source must go first!
    CallLogDataSource systemCallLogDataSource = dataSources.getSystemCallLogDataSource();
    ListenableFuture<Void> fillFuture = systemCallLogDataSource.fill(appContext, mutations);
    String systemEventName = eventNameForFill(systemCallLogDataSource, isBuilt);
    futureTimer.applyTiming(fillFuture, systemEventName);
    // passed from source to source.
    for (CallLogDataSource dataSource : dataSources.getDataSourcesExcludingSystemCallLog()) {
        fillFuture = Futures.transformAsync(fillFuture, unused -> {
            ListenableFuture<Void> dataSourceFuture = dataSource.fill(appContext, mutations);
            String eventName = eventNameForFill(dataSource, isBuilt);
            futureTimer.applyTiming(dataSourceFuture, eventName);
            return dataSourceFuture;
        }, lightweightExecutorService);
    }
    futureTimer.applyTiming(fillFuture, eventNameForOverallFill(isBuilt));
    // After all data sources are filled, apply mutations (at this point "fillFuture" is the result
    // of filling the last data source).
    ListenableFuture<Void> applyMutationsFuture = Futures.transformAsync(fillFuture, unused -> {
        ListenableFuture<Void> mutationApplierFuture = mutationApplier.applyToDatabase(mutations, appContext);
        futureTimer.applyTiming(mutationApplierFuture, eventNameForApplyMutations(isBuilt));
        return mutationApplierFuture;
    }, lightweightExecutorService);
    // After mutations applied, call onSuccessfulFill for each data source (in parallel).
    ListenableFuture<List<Void>> onSuccessfulFillFuture = Futures.transformAsync(applyMutationsFuture, unused -> {
        List<ListenableFuture<Void>> onSuccessfulFillFutures = new ArrayList<>();
        for (CallLogDataSource dataSource : dataSources.getDataSourcesIncludingSystemCallLog()) {
            ListenableFuture<Void> dataSourceFuture = dataSource.onSuccessfulFill(appContext);
            onSuccessfulFillFutures.add(dataSourceFuture);
            String eventName = eventNameForOnSuccessfulFill(dataSource, isBuilt);
            futureTimer.applyTiming(dataSourceFuture, eventName);
        }
        ListenableFuture<List<Void>> allFutures = Futures.allAsList(onSuccessfulFillFutures);
        futureTimer.applyTiming(allFutures, eventNameForOverallOnSuccessfulFill(isBuilt));
        return allFutures;
    }, lightweightExecutorService);
    // After onSuccessfulFill is called for every data source, write the shared pref.
    return Futures.transform(onSuccessfulFillFuture, unused -> {
        sharedPreferences.edit().putBoolean(SharedPrefKeys.FORCE_REBUILD, false).apply();
        callLogState.markBuilt();
        return mutations.isEmpty() ? RefreshResult.REBUILT_BUT_NO_CHANGES_NEEDED : RefreshResult.REBUILT_AND_CHANGES_NEEDED;
    }, backgroundExecutorService);
}
Also used : MutationApplier(com.android.dialer.calllog.database.MutationApplier) Context(android.content.Context) LogUtil(com.android.dialer.common.LogUtil) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Metrics(com.android.dialer.metrics.Metrics) Singleton(javax.inject.Singleton) CallLogDataSource(com.android.dialer.calllog.datasources.CallLogDataSource) CallLogMutations(com.android.dialer.calllog.datasources.CallLogMutations) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) BackgroundExecutor(com.android.dialer.common.concurrent.Annotations.BackgroundExecutor) SharedPrefKeys(com.android.dialer.calllog.constants.SharedPrefKeys) Unencrypted(com.android.dialer.storage.Unencrypted) DialerFutureSerializer(com.android.dialer.common.concurrent.DialerFutureSerializer) ApplicationContext(com.android.dialer.inject.ApplicationContext) DataSources(com.android.dialer.calllog.datasources.DataSources) DialerFutures(com.android.dialer.common.concurrent.DialerFutures) LightweightExecutor(com.android.dialer.common.concurrent.Annotations.LightweightExecutor) LogCatMode(com.android.dialer.metrics.FutureTimer.LogCatMode) FutureTimer(com.android.dialer.metrics.FutureTimer) Futures(com.google.common.util.concurrent.Futures) List(java.util.List) SharedPreferences(android.content.SharedPreferences) Preconditions(com.google.common.base.Preconditions) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) CallLogMutations(com.android.dialer.calllog.datasources.CallLogMutations) ArrayList(java.util.ArrayList) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ArrayList(java.util.ArrayList) List(java.util.List) CallLogDataSource(com.android.dialer.calllog.datasources.CallLogDataSource)

Aggregations

CallLogMutations (com.android.dialer.calllog.datasources.CallLogMutations)4 Context (android.content.Context)3 SharedPreferences (android.content.SharedPreferences)3 CallLogDataSource (com.android.dialer.calllog.datasources.CallLogDataSource)3 LogUtil (com.android.dialer.common.LogUtil)3 BackgroundExecutor (com.android.dialer.common.concurrent.Annotations.BackgroundExecutor)3 ApplicationContext (com.android.dialer.inject.ApplicationContext)3 Futures (com.google.common.util.concurrent.Futures)3 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)3 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)3 ArrayList (java.util.ArrayList)3 Inject (javax.inject.Inject)3 SharedPrefKeys (com.android.dialer.calllog.constants.SharedPrefKeys)2 MutationApplier (com.android.dialer.calllog.database.MutationApplier)2 DataSources (com.android.dialer.calllog.datasources.DataSources)2 LightweightExecutor (com.android.dialer.common.concurrent.Annotations.LightweightExecutor)2 DialerFutureSerializer (com.android.dialer.common.concurrent.DialerFutureSerializer)2 DialerFutures (com.android.dialer.common.concurrent.DialerFutures)2 FutureTimer (com.android.dialer.metrics.FutureTimer)2 LogCatMode (com.android.dialer.metrics.FutureTimer.LogCatMode)2