Search in sources :

Example 91 with ContentProviderClient

use of android.content.ContentProviderClient in project AsmackService by rtreffer.

the class XmppTransportService method onCreate.

/**
 * Initialize the xmpp service, binding all required receivers.
 */
@Override
public void onCreate() {
    super.onCreate();
    accountManager = AccountManager.get(this);
    ContentProviderClient provider = getContentResolver().acquireContentProviderClient(ContactsContract.AUTHORITY_URI);
    ContactDataMapper mapper = new ContactDataMapper(provider);
    BroadcastReceiver receiver = new PresenceBroadcastReceiver(mapper);
    registerReceiver(receiver, new IntentFilter(XmppTransportService.XMPP_STANZA_INTENT));
    receiver = new DiscoReceiver();
    registerReceiver(receiver, new IntentFilter(XmppTransportService.XMPP_STANZA_INTENT));
    receiver = new KeepaliveActionIntentReceiver(this);
    ;
    registerReceiver(receiver, new IntentFilter(Intent.ACTION_TIME_TICK));
    receiver = new SendStanzaReceiver(this);
    registerReceiver(receiver, new IntentFilter(XMPP_STANZA_SEND_INTENT));
    receiver = new ConnectivityReceiver(this);
    registerReceiver(receiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    accountManager.addOnAccountsUpdatedListener(this, null, true);
}
Also used : ContactDataMapper(com.googlecode.asmack.contacts.ContactDataMapper) IntentFilter(android.content.IntentFilter) DiscoReceiver(com.googlecode.asmack.disco.DiscoReceiver) PresenceBroadcastReceiver(com.googlecode.asmack.contacts.PresenceBroadcastReceiver) PresenceBroadcastReceiver(com.googlecode.asmack.contacts.PresenceBroadcastReceiver) BroadcastReceiver(android.content.BroadcastReceiver) ContentProviderClient(android.content.ContentProviderClient)

Example 92 with ContentProviderClient

use of android.content.ContentProviderClient in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class BinaryDictionaryFileDumper method getWordListWordListInfos.

/**
 * Queries a content provider for the list of word lists for a specific locale
 * available to copy into Latin IME.
 */
private static List<WordListInfo> getWordListWordListInfos(final Locale locale, final Context context, final boolean hasDefaultWordList) {
    final String clientId = context.getString(R.string.dictionary_pack_client_id);
    final ContentProviderClient client = context.getContentResolver().acquireContentProviderClient(getProviderUriBuilder("").build());
    if (null == client)
        return Collections.<WordListInfo>emptyList();
    Cursor cursor = null;
    try {
        final Uri.Builder builder = getContentUriBuilderForType(clientId, client, QUERY_PATH_DICT_INFO, locale.toString());
        if (!hasDefaultWordList) {
            builder.appendQueryParameter(QUERY_PARAMETER_MAY_PROMPT_USER, QUERY_PARAMETER_TRUE);
        }
        final Uri queryUri = builder.build();
        final boolean isProtocolV2 = (QUERY_PARAMETER_PROTOCOL_VALUE.equals(queryUri.getQueryParameter(QUERY_PARAMETER_PROTOCOL)));
        cursor = client.query(queryUri, DICTIONARY_PROJECTION, null, null, null);
        if (isProtocolV2 && null == cursor) {
            reinitializeClientRecordInDictionaryContentProvider(context, client, clientId);
            cursor = client.query(queryUri, DICTIONARY_PROJECTION, null, null, null);
        }
        if (null == cursor)
            return Collections.<WordListInfo>emptyList();
        if (cursor.getCount() <= 0 || !cursor.moveToFirst()) {
            return Collections.<WordListInfo>emptyList();
        }
        final ArrayList<WordListInfo> list = new ArrayList<>();
        do {
            final String wordListId = cursor.getString(0);
            final String wordListLocale = cursor.getString(1);
            final String wordListRawChecksum = cursor.getString(2);
            if (TextUtils.isEmpty(wordListId))
                continue;
            list.add(new WordListInfo(wordListId, wordListLocale, wordListRawChecksum));
        } while (cursor.moveToNext());
        return list;
    } catch (RemoteException e) {
        // The documentation is unclear as to in which cases this may happen, but it probably
        // happens when the content provider got suddenly killed because it crashed or because
        // the user disabled it through Settings.
        Log.e(TAG, "RemoteException: communication with the dictionary pack cut", e);
        return Collections.<WordListInfo>emptyList();
    } catch (Exception e) {
        // A crash here is dangerous because crashing here would brick any encrypted device -
        // we need the keyboard to be up and working to enter the password, so we don't want
        // to die no matter what. So let's be as safe as possible.
        Log.e(TAG, "Unexpected exception communicating with the dictionary pack", e);
        return Collections.<WordListInfo>emptyList();
    } finally {
        if (null != cursor) {
            cursor.close();
        }
        client.release();
    }
}
Also used : ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) RemoteException(android.os.RemoteException) Uri(android.net.Uri) ContentProviderClient(android.content.ContentProviderClient) RemoteException(android.os.RemoteException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 93 with ContentProviderClient

use of android.content.ContentProviderClient in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class DictionaryFactory method killDictionary.

/**
 * Kills a dictionary so that it is never used again, if possible.
 * @param context The context to contact the dictionary provider, if possible.
 * @param f A file address to the dictionary to kill.
 */
public static void killDictionary(final Context context, final AssetFileAddress f) {
    if (f.pointsToPhysicalFile()) {
        f.deleteUnderlyingFile();
        // Warn the dictionary provider if the dictionary came from there.
        final ContentProviderClient providerClient;
        try {
            providerClient = context.getContentResolver().acquireContentProviderClient(BinaryDictionaryFileDumper.getProviderUriBuilder("").build());
        } catch (final SecurityException e) {
            Log.e(TAG, "No permission to communicate with the dictionary provider", e);
            return;
        }
        if (null == providerClient) {
            Log.e(TAG, "Can't establish communication with the dictionary provider");
            return;
        }
        final String wordlistId = DictionaryInfoUtils.getWordListIdFromFileName(new File(f.mFilename).getName());
        // TODO: this is a reasonable last resort, but it is suboptimal.
        // The following will remove the entry for this dictionary with the dictionary
        // provider. When the metadata is downloaded again, we will try downloading it
        // again.
        // However, in the practice that will mean the user will find themselves without
        // the new dictionary. That's fine for languages where it's included in the APK,
        // but for other languages it will leave the user without a dictionary at all until
        // the next update, which may be a few days away.
        // Ideally, we would trigger a new download right away, and use increasing retry
        // delays for this particular id/version combination.
        // Then again, this is expected to only ever happen in case of human mistake. If
        // the wrong file is on the server, the following is still doing the right thing.
        // If it's a file left over from the last version however, it's not great.
        BinaryDictionaryFileDumper.reportBrokenFileToDictionaryProvider(providerClient, context.getString(R.string.dictionary_pack_client_id), wordlistId);
    }
}
Also used : File(java.io.File) ContentProviderClient(android.content.ContentProviderClient)

Example 94 with ContentProviderClient

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

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)

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