Search in sources :

Example 1 with ContactsProvider

use of fr.neamar.kiss.dataprovider.ContactsProvider in project KISS by Neamar.

the class IncomingSmsHandler method onReceive.

@Override
public void onReceive(Context context, Intent intent) {
    // Only handle SMS received
    if (!intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
        return;
    }
    // Stop if contacts are not enabled
    DataHandler dataHandler = KissApplication.getDataHandler(context);
    ContactsProvider contactsProvider = dataHandler.getContactsProvider();
    if (contactsProvider == null) {
        // Contacts have been disabled from settings
        return;
    }
    // Get the SMS message passed in, if any
    Bundle bundle = intent.getExtras();
    if (bundle == null) {
        return;
    }
    // Retrieve the SMS message received.
    // Since we're not interested in content, we can safely discard
    // all records but the first one
    Object[] pdus = (Object[]) bundle.get("pdus");
    if (pdus == null) {
        return;
    }
    SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdus[0]);
    // Now, retrieve the contact by its lookup key on our contactsProvider
    ContactsPojo contactPojo = contactsProvider.findByPhone(msg.getOriginatingAddress());
    if (contactPojo != null) {
        // We have a match!
        dataHandler.addToHistory(contactPojo.id);
    }
}
Also used : SmsMessage(android.telephony.SmsMessage) ContactsProvider(fr.neamar.kiss.dataprovider.ContactsProvider) Bundle(android.os.Bundle) ContactsPojo(fr.neamar.kiss.pojo.ContactsPojo) DataHandler(fr.neamar.kiss.DataHandler)

Example 2 with ContactsProvider

use of fr.neamar.kiss.dataprovider.ContactsProvider in project KISS by Neamar.

the class IncomingCallHandler method onReceive.

@Override
public void onReceive(final Context context, Intent intent) {
    try {
        DataHandler dataHandler = KissApplication.getDataHandler(context);
        ContactsProvider contactsProvider = dataHandler.getContactsProvider();
        // Stop if contacts are not enabled
        if (contactsProvider == null) {
            return;
        }
        if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            String phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
            if (phoneNumber == null) {
                // Skipping (private call)
                return;
            }
            ContactsPojo contactPojo = contactsProvider.findByPhone(phoneNumber);
            if (contactPojo != null) {
                dataHandler.addToHistory(contactPojo.id);
            }
        }
    } catch (Exception e) {
        Log.e("Phone Receive Error", " " + e);
    }
}
Also used : ContactsProvider(fr.neamar.kiss.dataprovider.ContactsProvider) ContactsPojo(fr.neamar.kiss.pojo.ContactsPojo) DataHandler(fr.neamar.kiss.DataHandler)

Example 3 with ContactsProvider

use of fr.neamar.kiss.dataprovider.ContactsProvider in project KISS by Neamar.

the class DataHandler method connectToProvider.

/**
     * Require the data handler to be connected to the data provider with the given name
     *
     * @param name Data provider name (i.e.: `ContactsProvider` → `"contacts"`)
     */
protected void connectToProvider(final String name) {
    // Do not continue if this provider has already been connected to
    if (this.providers.containsKey(name)) {
        return;
    }
    // Find provider class for the given service name
    Intent intent = this.providerName2Intent(name);
    if (intent == null) {
        return;
    }
    // Send "start service" command first so that the service can run independently
    // of the activity
    this.context.startService(intent);
    final ProviderEntry entry = new ProviderEntry();
    // Connect and bind to provider service
    this.context.bindService(intent, new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            Provider.LocalBinder binder = (Provider.LocalBinder) service;
            IProvider provider = binder.getService();
            // Update provider info so that it contains something useful
            entry.provider = provider;
            entry.connection = this;
            if (provider.isLoaded()) {
                handleProviderLoaded();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
        }
    }, Context.BIND_AUTO_CREATE);
    // Add empty provider object to list of providers
    this.providers.put(name, entry);
}
Also used : ServiceConnection(android.content.ServiceConnection) IBinder(android.os.IBinder) IProvider(fr.neamar.kiss.dataprovider.IProvider) Intent(android.content.Intent) ComponentName(android.content.ComponentName) IProvider(fr.neamar.kiss.dataprovider.IProvider) Provider(fr.neamar.kiss.dataprovider.Provider) ShortcutsProvider(fr.neamar.kiss.dataprovider.ShortcutsProvider) AppProvider(fr.neamar.kiss.dataprovider.AppProvider) ContactsProvider(fr.neamar.kiss.dataprovider.ContactsProvider)

Aggregations

ContactsProvider (fr.neamar.kiss.dataprovider.ContactsProvider)3 DataHandler (fr.neamar.kiss.DataHandler)2 ContactsPojo (fr.neamar.kiss.pojo.ContactsPojo)2 ComponentName (android.content.ComponentName)1 Intent (android.content.Intent)1 ServiceConnection (android.content.ServiceConnection)1 Bundle (android.os.Bundle)1 IBinder (android.os.IBinder)1 SmsMessage (android.telephony.SmsMessage)1 AppProvider (fr.neamar.kiss.dataprovider.AppProvider)1 IProvider (fr.neamar.kiss.dataprovider.IProvider)1 Provider (fr.neamar.kiss.dataprovider.Provider)1 ShortcutsProvider (fr.neamar.kiss.dataprovider.ShortcutsProvider)1