/** * Query all contact name from contact table. * * @return all contact name */ public String[] getContactsNames() { ArrayList<String> contactsList = new ArrayList<String>(); String columnsInbox[] = new String[] {Contacts.DISPLAY_NAME}; Cursor cursor = null; try { cursor = mContext .getContentResolver() .query(CONTACTS_URI, columnsInbox, null, null, Contacts.SORT_KEY_PRIMARY); if (cursor != null && cursor.moveToFirst()) { int count = cursor.getCount(); Log.d(TAG, "[getContactsNames] cursor count = " + count); String contact; for (int i = 0; i < count; i++) { int columnIndex = cursor.getColumnIndex(Contacts.DISPLAY_NAME); if (columnIndex >= 0) { contact = cursor.getString(columnIndex); if (!TextUtils.isEmpty(contact)) { contactsList.add(contact); } } cursor.moveToNext(); } } } finally { if (cursor != null) { cursor.close(); } } return contactsList.toArray(new String[contactsList.size()]); }
@Override public void onChange(boolean selfChange, Uri uri) { super.onChange(selfChange, uri); Log.i(TAG, "[onChange]uri = " + uri); if (mVoiceHandler.hasMessages(MSG_GET_CONTACTS_NAME)) { mVoiceHandler.removeMessages(MSG_GET_CONTACTS_NAME); } mVoiceHandler.sendEmptyMessage(MSG_GET_CONTACTS_NAME); }
/** * VoiceContactsObserver constructor. * * @param context context * @param handler the handler to run onChange(boolean) on */ public VoiceContactsObserver(Context context, Handler handler) { super(handler); Log.i(TAG, "[VoiceContactsObserver]new..."); mContext = context; mMainHandler = handler; mHandlerThread = new HandlerThread("VoiceHandlerThread"); mHandlerThread.start(); mVoiceHandler = new VoiceHandler(mHandlerThread.getLooper()); if (VoiceContactsBusiness.MTK_VOICE_CONTACT_SEARCH_SUPPORT) { mVoiceHandler.sendEmptyMessage(MSG_GET_CONTACTS_NAME); } }
@Override public View onCreateView(ViewGroup parent) { Log.i(TAG, "[onCreateView]..."); mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = mInflater.inflate(R.layout.voice_ui_preference_title, null); mPreferenceTitle = (TextView) view.findViewById(R.id.command_preference_title); if (mPreferenceTitle != null) { mPreferenceTitle.setText(mTitleValue); } return view; }
@Override public void handleMessage(Message msg) { super.handleMessage(msg); Log.d(TAG, "[handleMessage]msg.what = " + msg.what); switch (msg.what) { case MSG_GET_CONTACTS_NAME: sendToMainHandler(); break; default: break; } }
private void sendToMainHandler() { Log.d(TAG, "[sendToMainHandler]..."); if (mMainHandler.hasMessages(VoiceCommandListener.ACTION_VOICE_CONTACTS_NAME)) { mMainHandler.removeMessages(VoiceCommandListener.ACTION_VOICE_CONTACTS_NAME); } // Query database after remove the contacts msg of main handler String[] contactsNames = getContactsNames(); VoiceMessage message = new VoiceMessage(); message.mMainAction = VoiceCommandListener.ACTION_MAIN_VOICE_CONTACTS; message.mSubAction = VoiceCommandListener.ACTION_VOICE_CONTACTS_NAME; Bundle bundle = DataPackage.packageSendInfo(contactsNames, null); message.mExtraData = bundle; Message msg = mMainHandler.obtainMessage(); msg.what = VoiceCommandListener.ACTION_MAIN_VOICE_CONTACTS; msg.obj = message; mMainHandler.sendMessage(msg); }