Search in sources :

Example 1 with IProvider

use of fr.neamar.kiss.dataprovider.IProvider 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

ComponentName (android.content.ComponentName)1 Intent (android.content.Intent)1 ServiceConnection (android.content.ServiceConnection)1 IBinder (android.os.IBinder)1 AppProvider (fr.neamar.kiss.dataprovider.AppProvider)1 ContactsProvider (fr.neamar.kiss.dataprovider.ContactsProvider)1 IProvider (fr.neamar.kiss.dataprovider.IProvider)1 Provider (fr.neamar.kiss.dataprovider.Provider)1 ShortcutsProvider (fr.neamar.kiss.dataprovider.ShortcutsProvider)1