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);
}
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();
}
}
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);
}
}
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);
}
}
Aggregations