Java Code Examples for android.content.Loader.OnLoadCompleteListener

The following are top voted examples for showing how to use android.content.Loader.OnLoadCompleteListener. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: TenguChat   File: PhoneHelper.java   View source code 4 votes vote down vote up
public static void loadPhoneContacts(Context context, final OnPhoneContactsLoadedListener listener) {
	final List<Bundle> phoneContacts = new ArrayList<>();
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
			&& context.checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
		listener.onPhoneContactsLoaded(phoneContacts);
		return;
	}
	final String[] PROJECTION = new String[]{ContactsContract.Data._ID,
			ContactsContract.Data.DISPLAY_NAME,
			ContactsContract.Data.PHOTO_URI,
			ContactsContract.Data.LOOKUP_KEY,
			ContactsContract.CommonDataKinds.Im.DATA};

	final String SELECTION = "(" + ContactsContract.Data.MIMETYPE + "=\""
			+ ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE
			+ "\") AND (" + ContactsContract.CommonDataKinds.Im.PROTOCOL
			+ "=\"" + ContactsContract.CommonDataKinds.Im.PROTOCOL_JABBER
			+ "\")";

	CursorLoader mCursorLoader = new NotThrowCursorLoader(context,
			ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null,
			null);
	mCursorLoader.registerListener(0, new OnLoadCompleteListener<Cursor>() {

		@Override
		public void onLoadComplete(Loader<Cursor> arg0, Cursor cursor) {
			if (cursor != null) {
				while (cursor.moveToNext()) {
					Bundle contact = new Bundle();
					contact.putInt("phoneid", cursor.getInt(cursor
							.getColumnIndex(ContactsContract.Data._ID)));
					contact.putString(
							"displayname",
							cursor.getString(cursor
									.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)));
					contact.putString("photouri", cursor.getString(cursor
							.getColumnIndex(ContactsContract.Data.PHOTO_URI)));
					contact.putString("lookup", cursor.getString(cursor
							.getColumnIndex(ContactsContract.Data.LOOKUP_KEY)));

					contact.putString(
							"jid",
							cursor.getString(cursor
									.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA)));
					phoneContacts.add(contact);
				}
				cursor.close();
			}

			if (listener != null) {
				listener.onPhoneContactsLoaded(phoneContacts);
			}
		}
	});
	try {
		mCursorLoader.startLoading();
	} catch (RejectedExecutionException e) {
		if (listener != null) {
			listener.onPhoneContactsLoaded(phoneContacts);
		}
	}
}
 
Example 2
Project: messengerxmpp   File: PhoneHelper.java   View source code 4 votes vote down vote up
public static void loadPhoneContacts(Context context,final List<Bundle> phoneContacts, final OnPhoneContactsLoadedListener listener) {
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
			&& context.checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
		listener.onPhoneContactsLoaded(phoneContacts);
		return;
	}
	final String[] PROJECTION = new String[] { ContactsContract.Data._ID,
			ContactsContract.Data.DISPLAY_NAME,
			ContactsContract.Data.PHOTO_URI,
			ContactsContract.Data.LOOKUP_KEY,
			ContactsContract.CommonDataKinds.Im.DATA };

	final String SELECTION = "(" + ContactsContract.Data.MIMETYPE + "=\""
			+ ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE
			+ "\") AND (" + ContactsContract.CommonDataKinds.Im.PROTOCOL
			+ "=\"" + ContactsContract.CommonDataKinds.Im.PROTOCOL_JABBER
			+ "\")";

	CursorLoader mCursorLoader = new CursorLoader(context,
			ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null,
			null);
	mCursorLoader.registerListener(0, new OnLoadCompleteListener<Cursor>() {

		@Override
		public void onLoadComplete(Loader<Cursor> arg0, Cursor cursor) {
			if (cursor == null) {
				return;
			}
			while (cursor.moveToNext()) {
				Bundle contact = new Bundle();
				contact.putInt("phoneid", cursor.getInt(cursor
						.getColumnIndex(ContactsContract.Data._ID)));
				contact.putString(
						"displayname",
						cursor.getString(cursor
								.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)));
				contact.putString("photouri", cursor.getString(cursor
						.getColumnIndex(ContactsContract.Data.PHOTO_URI)));
				contact.putString("lookup", cursor.getString(cursor
						.getColumnIndex(ContactsContract.Data.LOOKUP_KEY)));

				contact.putString(
						"jid",
						cursor.getString(cursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA)));
				phoneContacts.add(contact);
			}
			if (listener != null) {
				listener.onPhoneContactsLoaded(phoneContacts);
			}
			cursor.close();
		}
	});
	try {
		mCursorLoader.startLoading();
	} catch (RejectedExecutionException e) {
		if (listener != null) {
			listener.onPhoneContactsLoaded(phoneContacts);
		}
	}
}
 
Example 3
Project: Pix-Art-Messenger   File: PhoneHelper.java   View source code 4 votes vote down vote up
public static void loadPhoneContacts(Context context, final OnPhoneContactsLoadedListener listener) {
    final List<Bundle> phoneContacts = new ArrayList<>();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
            && context.checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
        listener.onPhoneContactsLoaded(phoneContacts);
        return;
    }
    final String[] PROJECTION = new String[]{ContactsContract.Data._ID,
            ContactsContract.Data.DISPLAY_NAME,
            ContactsContract.Data.PHOTO_URI,
            ContactsContract.Data.LOOKUP_KEY,
            ContactsContract.CommonDataKinds.Im.DATA};

    final String SELECTION = "(" + ContactsContract.Data.MIMETYPE + "=\""
            + ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE
            + "\") AND (" + ContactsContract.CommonDataKinds.Im.PROTOCOL
            + "=\"" + ContactsContract.CommonDataKinds.Im.PROTOCOL_JABBER
            + "\")";

    CursorLoader mCursorLoader = new NotThrowCursorLoader(context,
            ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null,
            null);
    mCursorLoader.registerListener(0, new OnLoadCompleteListener<Cursor>() {

        @Override
        public void onLoadComplete(Loader<Cursor> arg0, Cursor cursor) {
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    Bundle contact = new Bundle();
                    contact.putInt("phoneid", cursor.getInt(cursor
                            .getColumnIndex(ContactsContract.Data._ID)));
                    contact.putString(
                            "displayname",
                            cursor.getString(cursor
                                    .getColumnIndex(ContactsContract.Data.DISPLAY_NAME)));
                    contact.putString("photouri", cursor.getString(cursor
                            .getColumnIndex(ContactsContract.Data.PHOTO_URI)));
                    contact.putString("lookup", cursor.getString(cursor
                            .getColumnIndex(ContactsContract.Data.LOOKUP_KEY)));

                    contact.putString(
                            "jid",
                            cursor.getString(cursor
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA)));
                    phoneContacts.add(contact);
                }
                cursor.close();
            }

            if (listener != null) {
                listener.onPhoneContactsLoaded(phoneContacts);
            }
        }
    });
    try {
        mCursorLoader.startLoading();
    } catch (RejectedExecutionException e) {
        if (listener != null) {
            listener.onPhoneContactsLoaded(phoneContacts);
        }
    }
}
 
Example 4
Project: android-plugin-host-sdk-for-locale   File: Junit4LoaderTestCase.java   View source code 4 votes vote down vote up
/**
 * Runs a Loader synchronously and returns the result of the load. The loader will
 * be started, stopped, and destroyed by this method so it cannot be reused.
 *
 * @param loader The loader to run synchronously
 * @return The result from the loader
 */
public <T> T getLoaderResultSynchronously(final Loader<T> loader) {
    // The test thread blocks on this queue until the loader puts it's result in
    final ArrayBlockingQueue<T> queue = new ArrayBlockingQueue<T>(1);

    // This callback runs on the "main" thread and unblocks the test thread
    // when it puts the result into the blocking queue
    final OnLoadCompleteListener<T> listener = new OnLoadCompleteListener<T>() {
        @Override
        public void onLoadComplete(Loader<T> completedLoader, T data) {
            // Shut the loader down
            completedLoader.unregisterListener(this);
            completedLoader.stopLoading();
            completedLoader.reset();

            // Store the result, unblocking the test thread
            queue.add(data);
        }
    };

    // This handler runs on the "main" thread of the process since AsyncTask
    // is documented as needing to run on the main thread and many Loaders use
    // AsyncTask
    final Handler mainThreadHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            loader.registerListener(0, listener);
            loader.startLoading();
        }
    };

    // Ask the main thread to start the loading process
    mainThreadHandler.sendEmptyMessage(0);

    // Block on the queue waiting for the result of the load to be inserted
    T result;
    while (true) {
        try {
            result = queue.take();
            break;
        } catch (InterruptedException e) {
            throw new RuntimeException("waiting thread interrupted", e);
        }
    }

    return result;
}
 
Example 5
Project: Conversations   File: PhoneHelper.java   View source code 4 votes vote down vote up
public static void loadPhoneContacts(Context context, final OnPhoneContactsLoadedListener listener) {
	final List<Bundle> phoneContacts = new ArrayList<>();
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
			&& context.checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
		listener.onPhoneContactsLoaded(phoneContacts);
		return;
	}
	final String[] PROJECTION = new String[]{ContactsContract.Data._ID,
			ContactsContract.Data.DISPLAY_NAME,
			ContactsContract.Data.PHOTO_URI,
			ContactsContract.Data.LOOKUP_KEY,
			ContactsContract.CommonDataKinds.Im.DATA};

	final String SELECTION = "(" + ContactsContract.Data.MIMETYPE + "=\""
			+ ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE
			+ "\") AND (" + ContactsContract.CommonDataKinds.Im.PROTOCOL
			+ "=\"" + ContactsContract.CommonDataKinds.Im.PROTOCOL_JABBER
			+ "\")";

	CursorLoader mCursorLoader = new NotThrowCursorLoader(context,
			ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null,
			null);
	mCursorLoader.registerListener(0, new OnLoadCompleteListener<Cursor>() {

		@Override
		public void onLoadComplete(Loader<Cursor> arg0, Cursor cursor) {
			if (cursor != null) {
				while (cursor.moveToNext()) {
					Bundle contact = new Bundle();
					contact.putInt("phoneid", cursor.getInt(cursor
							.getColumnIndex(ContactsContract.Data._ID)));
					contact.putString(
							"displayname",
							cursor.getString(cursor
									.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)));
					contact.putString("photouri", cursor.getString(cursor
							.getColumnIndex(ContactsContract.Data.PHOTO_URI)));
					contact.putString("lookup", cursor.getString(cursor
							.getColumnIndex(ContactsContract.Data.LOOKUP_KEY)));

					contact.putString(
							"jid",
							cursor.getString(cursor
									.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA)));
					phoneContacts.add(contact);
				}
				cursor.close();
			}

			if (listener != null) {
				listener.onPhoneContactsLoaded(phoneContacts);
			}
		}
	});
	try {
		mCursorLoader.startLoading();
	} catch (RejectedExecutionException e) {
		if (listener != null) {
			listener.onPhoneContactsLoaded(phoneContacts);
		}
	}
}
 
Example 6
Project: frozenchat   File: PhoneHelper.java   View source code 4 votes vote down vote up
public static void loadPhoneContacts(Context context, final List<Bundle> phoneContacts, final OnPhoneContactsLoadedListener listener) {
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
			&& context.checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
		listener.onPhoneContactsLoaded(phoneContacts);
		return;
	}
	final String[] PROJECTION = new String[]{ContactsContract.Data._ID,
			ContactsContract.Data.DISPLAY_NAME,
			ContactsContract.Data.PHOTO_URI,
			ContactsContract.Data.LOOKUP_KEY,
			ContactsContract.CommonDataKinds.Im.DATA};

	final String SELECTION = "(" + ContactsContract.Data.MIMETYPE + "=\""
			+ ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE
			+ "\") AND (" + ContactsContract.CommonDataKinds.Im.PROTOCOL
			+ "=\"" + ContactsContract.CommonDataKinds.Im.PROTOCOL_JABBER
			+ "\")";

	CursorLoader mCursorLoader = new NotThrowCursorLoader(context,
			ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null,
			null);
	mCursorLoader.registerListener(0, new OnLoadCompleteListener<Cursor>() {

		@Override
		public void onLoadComplete(Loader<Cursor> arg0, Cursor cursor) {
			if (cursor != null) {
				while (cursor.moveToNext()) {
					Bundle contact = new Bundle();
					contact.putInt("phoneid", cursor.getInt(cursor
							.getColumnIndex(ContactsContract.Data._ID)));
					contact.putString(
							"displayname",
							cursor.getString(cursor
									.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)));
					contact.putString("photouri", cursor.getString(cursor
							.getColumnIndex(ContactsContract.Data.PHOTO_URI)));
					contact.putString("lookup", cursor.getString(cursor
							.getColumnIndex(ContactsContract.Data.LOOKUP_KEY)));

					contact.putString(
							"jid",
							cursor.getString(cursor
									.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA)));
					phoneContacts.add(contact);
				}
				cursor.close();
			}

			if (listener != null) {
				listener.onPhoneContactsLoaded(phoneContacts);
			}
		}
	});
	try {
		mCursorLoader.startLoading();
	} catch (RejectedExecutionException e) {
		if (listener != null) {
			listener.onPhoneContactsLoaded(phoneContacts);
		}
	}
}
 
Example 7
Project: txtr   File: PhoneHelper.java   View source code 4 votes vote down vote up
public static void loadPhoneContacts(Context context,final List<Bundle> phoneContacts, final OnPhoneContactsLoadedListener listener) {
	final String[] PROJECTION = new String[] { ContactsContract.Data._ID,
			ContactsContract.Data.DISPLAY_NAME,
			ContactsContract.Data.PHOTO_URI,
			ContactsContract.Data.LOOKUP_KEY,
			ContactsContract.CommonDataKinds.Im.DATA };

	final String SELECTION = "(" + ContactsContract.Data.MIMETYPE + "=\""
			+ ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE
			+ "\") AND (" + ContactsContract.CommonDataKinds.Im.PROTOCOL
			+ "=\"" + ContactsContract.CommonDataKinds.Im.PROTOCOL_JABBER
			+ "\")";

	CursorLoader mCursorLoader = new CursorLoader(context,
			ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null,
			null);
	mCursorLoader.registerListener(0, new OnLoadCompleteListener<Cursor>() {

		@Override
		public void onLoadComplete(Loader<Cursor> arg0, Cursor cursor) {
			if (cursor == null) {
				return;
			}
			while (cursor.moveToNext()) {
				Bundle contact = new Bundle();
				contact.putInt("phoneid", cursor.getInt(cursor
						.getColumnIndex(ContactsContract.Data._ID)));
				contact.putString(
						"displayname",
						cursor.getString(cursor
								.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)));
				contact.putString("photouri", cursor.getString(cursor
						.getColumnIndex(ContactsContract.Data.PHOTO_URI)));
				contact.putString("lookup", cursor.getString(cursor
						.getColumnIndex(ContactsContract.Data.LOOKUP_KEY)));

				contact.putString(
						"jid",
						cursor.getString(cursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA)));
				phoneContacts.add(contact);
			}
			if (listener != null) {
				listener.onPhoneContactsLoaded(phoneContacts);
			}
			cursor.close();
		}
	});
	try {
		mCursorLoader.startLoading();
	} catch (RejectedExecutionException e) {
		if (listener != null) {
			listener.onPhoneContactsLoaded(phoneContacts);
		}
	}
}
 
Example 8
Project: SyncChatAndroid   File: PhoneHelper.java   View source code 4 votes vote down vote up
public static void loadPhoneContacts(Context context,
		final OnPhoneContactsLoadedListener listener) {
	final List<Bundle> phoneContacts = new ArrayList<Bundle>();
	
	final String[] PROJECTION = new String[] { ContactsContract.Data._ID,
			ContactsContract.Data.DISPLAY_NAME,
			ContactsContract.Data.PHOTO_THUMBNAIL_URI,
			ContactsContract.Data.LOOKUP_KEY,
			ContactsContract.CommonDataKinds.Im.DATA };

	final String SELECTION = "(" + ContactsContract.Data.MIMETYPE + "=\""
			+ ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE
			+ "\") AND (" + ContactsContract.CommonDataKinds.Im.PROTOCOL
			+ "=\"" + ContactsContract.CommonDataKinds.Im.PROTOCOL_JABBER
			+ "\")";

	CursorLoader mCursorLoader = new CursorLoader(context,
			ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null,
			null);
	mCursorLoader.registerListener(0, new OnLoadCompleteListener<Cursor>() {

		@Override
		public void onLoadComplete(Loader<Cursor> arg0, Cursor cursor) {
			while (cursor.moveToNext()) {
				Bundle contact = new Bundle();
				contact.putInt("phoneid", cursor.getInt(cursor
						.getColumnIndex(ContactsContract.Data._ID)));
				contact.putString(
						"displayname",
						cursor.getString(cursor
								.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)));
				contact.putString(
						"photouri",
						cursor.getString(cursor
								.getColumnIndex(ContactsContract.Data.PHOTO_THUMBNAIL_URI)));
				contact.putString("lookup", cursor.getString(cursor
						.getColumnIndex(ContactsContract.Data.LOOKUP_KEY)));
				
				contact.putString("jid",cursor.getString(cursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA)));
				phoneContacts.add(contact);
			}
			if (listener != null) {
				listener.onPhoneContactsLoaded(phoneContacts);
			}
		}
	});
	mCursorLoader.startLoading();
}