Search in sources :

Example 81 with ContentProviderClient

use of android.content.ContentProviderClient in project AnExplorer by 1hakr.

the class DirectoryLoader method loadInBackground.

@Override
public final DirectoryResult loadInBackground() {
    synchronized (this) {
        if (isLoadInBackgroundCanceled()) {
            throw new OperationCanceledException();
        }
        mSignal = new CancellationSignal();
    }
    final ContentResolver resolver = getContext().getContentResolver();
    final String authority = mUri.getAuthority();
    final DirectoryResult result = new DirectoryResult();
    int userMode = State.MODE_UNKNOWN;
    int userSortOrder = State.SORT_ORDER_UNKNOWN;
    // Use default document when searching
    if (mType == DirectoryFragment.TYPE_SEARCH) {
        final Uri docUri = DocumentsContract.buildDocumentUri(mRoot.authority, mRoot.documentId);
        try {
            mDoc = DocumentInfo.fromUri(resolver, docUri);
        } catch (FileNotFoundException e) {
            Log.w(TAG, "Failed to query", e);
            result.exception = e;
            CrashReportingManager.logException(e);
            return result;
        }
    }
    // Pick up any custom modes requested by user
    Cursor cursor = null;
    try {
        final Uri stateUri = RecentsProvider.buildState(mRoot.authority, mRoot.rootId, mDoc.documentId);
        cursor = resolver.query(stateUri, null, null, null, null);
        if (cursor.moveToFirst()) {
            userMode = getCursorInt(cursor, StateColumns.MODE);
            userSortOrder = getCursorInt(cursor, StateColumns.SORT_ORDER);
        }
    } finally {
        IoUtils.closeQuietly(cursor);
    }
    if (userMode != State.MODE_UNKNOWN) {
        result.mode = userMode;
    } else {
        if ((mDoc.flags & Document.FLAG_DIR_PREFERS_GRID) != 0) {
            result.mode = State.MODE_GRID;
        } else {
            result.mode = State.MODE_LIST;
        }
    }
    if (userSortOrder != State.SORT_ORDER_UNKNOWN) {
        result.sortOrder = userSortOrder;
    } else {
        if ((mDoc.flags & Document.FLAG_DIR_PREFERS_LAST_MODIFIED) != 0) {
            result.sortOrder = State.SORT_ORDER_LAST_MODIFIED;
        } else {
            result.sortOrder = State.SORT_ORDER_DISPLAY_NAME;
        }
    }
    // Search always uses ranking from provider
    if (mType == DirectoryFragment.TYPE_SEARCH) {
    // result.sortOrder = State.SORT_ORDER_UNKNOWN;
    }
    Log.d(TAG, "userMode=" + userMode + ", userSortOrder=" + userSortOrder + " --> mode=" + result.mode + ", sortOrder=" + result.sortOrder);
    ContentProviderClient client = null;
    try {
        client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, authority);
        cursor = client.query(mUri, null, null, null, getQuerySortOrder(result.sortOrder));
        cursor.registerContentObserver(mObserver);
        cursor = new RootCursorWrapper(mUri.getAuthority(), mRoot.rootId, cursor, -1);
        if (mType == DirectoryFragment.TYPE_SEARCH) {
            cursor = new SortingCursorWrapper(cursor, result.sortOrder);
            // Filter directories out of search results, for now
            cursor = new FilteringCursorWrapper(cursor, null, SEARCH_REJECT_MIMES);
        } else {
            // Normal directories should have sorting applied
            cursor = new SortingCursorWrapper(cursor, result.sortOrder);
        }
        result.client = client;
        result.cursor = cursor;
    } catch (Exception e) {
        Log.w(TAG, "Failed to query", e);
        CrashReportingManager.logException(e);
        result.exception = e;
        ContentProviderClientCompat.releaseQuietly(client);
    } finally {
        synchronized (this) {
            mSignal = null;
        }
    }
    return result;
}
Also used : RootCursorWrapper(dev.dworks.apps.anexplorer.cursor.RootCursorWrapper) OperationCanceledException(android.os.OperationCanceledException) FileNotFoundException(java.io.FileNotFoundException) Cursor(android.database.Cursor) Uri(android.net.Uri) SortingCursorWrapper(dev.dworks.apps.anexplorer.cursor.SortingCursorWrapper) OperationCanceledException(android.os.OperationCanceledException) FileNotFoundException(java.io.FileNotFoundException) ContentResolver(android.content.ContentResolver) DirectoryResult(dev.dworks.apps.anexplorer.model.DirectoryResult) FilteringCursorWrapper(dev.dworks.apps.anexplorer.cursor.FilteringCursorWrapper) CancellationSignal(android.os.CancellationSignal) ContentProviderClient(android.content.ContentProviderClient)

Example 82 with ContentProviderClient

use of android.content.ContentProviderClient in project VirtualXposed by android-hacker.

the class ContentProviderCompat method crazyAcquireContentProvider.

public static ContentProviderClient crazyAcquireContentProvider(Context context, String name) {
    ContentProviderClient client = acquireContentProviderClient(context, name);
    if (client == null) {
        int retry = 0;
        while (retry < 5 && client == null) {
            SystemClock.sleep(100);
            retry++;
            client = acquireContentProviderClient(context, name);
        }
    }
    return client;
}
Also used : ContentProviderClient(android.content.ContentProviderClient)

Example 83 with ContentProviderClient

use of android.content.ContentProviderClient in project Signal-Android by WhisperSystems.

the class ContactsSyncAdapter method onPerformSync.

@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
    Log.i(TAG, "onPerformSync(" + authority + ")");
    Context context = getContext();
    if (SignalStore.account().getE164() == null) {
        Log.i(TAG, "No local number set, skipping all sync operations.");
        return;
    }
    if (!SignalStore.account().isRegistered()) {
        Log.i(TAG, "Not push registered. Just syncing contact info.");
        DirectoryHelper.syncRecipientInfoWithSystemContacts(context);
        return;
    }
    Set<String> allSystemNumbers = ContactAccessor.getInstance().getAllContactsWithNumbers(context);
    Set<String> knownSystemNumbers = SignalDatabase.recipients().getAllPhoneNumbers();
    Set<String> unknownSystemNumbers = SetUtil.difference(allSystemNumbers, knownSystemNumbers);
    if (unknownSystemNumbers.size() > FULL_SYNC_THRESHOLD) {
        Log.i(TAG, "There are " + unknownSystemNumbers.size() + " unknown contacts. Doing a full sync.");
        try {
            DirectoryHelper.refreshDirectory(context, true);
        } catch (IOException e) {
            Log.w(TAG, e);
        }
    } else if (unknownSystemNumbers.size() > 0) {
        Log.i(TAG, "There are " + unknownSystemNumbers.size() + " unknown contacts. Doing an individual sync.");
        List<Recipient> recipients = Stream.of(unknownSystemNumbers).filter(s -> s.startsWith("+")).map(s -> Recipient.external(getContext(), s)).toList();
        try {
            DirectoryHelper.refreshDirectoryFor(context, recipients, true);
        } catch (IOException e) {
            Log.w(TAG, "Failed to refresh! Scheduling for later.", e);
            ApplicationDependencies.getJobManager().add(new DirectoryRefreshJob(true));
        }
    } else {
        Log.i(TAG, "No new contacts. Just syncing system contact data.");
        DirectoryHelper.syncRecipientInfoWithSystemContacts(context);
    }
}
Also used : Context(android.content.Context) SignalStore(org.thoughtcrime.securesms.keyvalue.SignalStore) Context(android.content.Context) SyncResult(android.content.SyncResult) SignalDatabase(org.thoughtcrime.securesms.database.SignalDatabase) Bundle(android.os.Bundle) DirectoryRefreshJob(org.thoughtcrime.securesms.jobs.DirectoryRefreshJob) Stream(com.annimon.stream.Stream) DirectoryHelper(org.thoughtcrime.securesms.contacts.sync.DirectoryHelper) Account(android.accounts.Account) ApplicationDependencies(org.thoughtcrime.securesms.dependencies.ApplicationDependencies) AbstractThreadedSyncAdapter(android.content.AbstractThreadedSyncAdapter) Set(java.util.Set) IOException(java.io.IOException) SetUtil(org.thoughtcrime.securesms.util.SetUtil) Log(org.signal.core.util.logging.Log) List(java.util.List) ContentProviderClient(android.content.ContentProviderClient) Recipient(org.thoughtcrime.securesms.recipients.Recipient) DirectoryRefreshJob(org.thoughtcrime.securesms.jobs.DirectoryRefreshJob) List(java.util.List) IOException(java.io.IOException)

Example 84 with ContentProviderClient

use of android.content.ContentProviderClient in project robolectric by robolectric.

the class ContentProviderControllerTest method withoutManifest_shouldRegisterWithContentResolver.

@Test
public void withoutManifest_shouldRegisterWithContentResolver() throws Exception {
    ProviderInfo providerInfo = new ProviderInfo();
    providerInfo.authority = "some-authority";
    controller.create(providerInfo);
    ContentProviderClient client = contentResolver.acquireContentProviderClient(providerInfo.authority);
    client.query(Uri.parse("something"), new String[] { "title" }, "*", new String[] {}, "created");
    assertThat(controller.get().transcript).containsExactly("onCreate", "query for something");
    close(client);
}
Also used : ProviderInfo(android.content.pm.ProviderInfo) ContentProviderClient(android.content.ContentProviderClient) Test(org.junit.Test)

Example 85 with ContentProviderClient

use of android.content.ContentProviderClient in project robolectric by robolectric.

the class ShadowContentResolver method getContentProviderClient.

private ContentProviderClient getContentProviderClient(ContentProvider provider, boolean stable) {
    ContentProviderClient client = ReflectionHelpers.callConstructor(ContentProviderClient.class, ClassParameter.from(ContentResolver.class, realContentResolver), ClassParameter.from(IContentProvider.class, provider.getIContentProvider()), ClassParameter.from(boolean.class, stable));
    ShadowContentProviderClient shadowContentProviderClient = Shadow.extract(client);
    shadowContentProviderClient.setContentProvider(provider);
    return client;
}
Also used : IContentProvider(android.content.IContentProvider) ContentProviderClient(android.content.ContentProviderClient) ContentResolver(android.content.ContentResolver)

Aggregations

ContentProviderClient (android.content.ContentProviderClient)94 RemoteException (android.os.RemoteException)30 Cursor (android.database.Cursor)28 Uri (android.net.Uri)24 Bundle (android.os.Bundle)19 ContentResolver (android.content.ContentResolver)18 FileNotFoundException (java.io.FileNotFoundException)15 ArrayList (java.util.ArrayList)13 IOException (java.io.IOException)10 Test (org.junit.Test)9 OperationCanceledException (android.os.OperationCanceledException)8 CancellationSignal (android.os.CancellationSignal)7 IContentProvider (android.content.IContentProvider)6 ActivityManager (android.app.ActivityManager)5 AlertDialog (android.app.AlertDialog)5 ClipData (android.content.ClipData)5 ComponentName (android.content.ComponentName)5 Intent (android.content.Intent)5 ServiceConnection (android.content.ServiceConnection)5 UriPermission (android.content.UriPermission)5