Android Open Source - C2Framework C2 Content Service






From Project

Back to project page C2Framework.

License

The source code is released under:

Apache License

If you think the Android project C2Framework listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package gaia.c2.content;
//from   w w w. j  av  a2  s  . co m
import android.app.IntentService;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import java.util.Map;
import java.util.TreeMap;


public class C2ContentService extends IntentService {
    public static String ACTION_QUERY = "gaia.c2.ActionQuery";
    public static String FIELD_QUERY_PARAMETERS = "gaia.c2.ActionQuery.Parameters";

    private Map<String, C2ContentProvider> contentProviders;
    private IBinder binder;

    public C2ContentService() {
        super("C2ContentService");
        contentProviders = new TreeMap<String, C2ContentProvider>();
        binder = new C2ContentServiceBinder();

    }

    public class C2ContentServiceBinder extends Binder {
        public C2ContentService getService() {
            return C2ContentService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    public void onCreateAllCp() {
        for (C2ContentProvider cp : this.contentProviders.values()) {
            cp.onCreate();
        }
    }

    public void onDestroyAllCp() {
        for (C2ContentProvider cp : this.contentProviders.values()) {
            cp.onDestroy();
        }
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.i("[C2ContentService]", "Received Intent");
        if (intent != null) {
            final String action = intent.getAction();
            if (action != null) {
                Log.i("[C2ContentService]", ">> Action: " + intent.getAction());
                if (action.equals(ACTION_QUERY)) {
                    // query format: c2://Authority/Method
                    String query = intent.getStringExtra(ACTION_QUERY);
                    Bundle params = intent.getBundleExtra(FIELD_QUERY_PARAMETERS);

                    Log.i("[C2ContentService]", "Query: " + query);
                    if (isValidQuery(query)) {
                        Log.i("[C2ContentService]", "Handling query...");
                        handle(query, params);
                    }
                }
            }
        }
    }

    private boolean isValidQuery(String query) {
        boolean result = true;

        result &= query.startsWith("c2://");
        result &= contentProviders.containsKey(query.substring(5, query.indexOf('/', 5)));
        result &= (query.length() - query.replace("/", "").length()) == 3;

        return result;
    }

    protected void handle(String query, Bundle parameters) {
        C2ContentProvider c2cp = contentProviders.get(query.substring(5, query.indexOf('/', 5)));
        c2cp.handleQuery(query.substring(query.lastIndexOf('/') + 1), parameters);
    }

    public void addContentProvider(C2ContentProvider c2cp) {
        if (!contentProviders.containsKey(c2cp.getAuthority())) {
            contentProviders.put(c2cp.getAuthority(), c2cp);
        }
    }
}




Java Source Code List

gaia.c2.content.C2ContentProvider.java
gaia.c2.content.C2ContentService.java
gaia.c2.content.QueryHandler.java
gaia.c2.content.android.CallContentProvider.java
gaia.c2.content.android.DialContentProvider.java
gaia.c2.content.android.VibrationContentProvider.java
gaia.c2.content.android.model.Call.java
gaia.c2.content.android.model.Dial.java
gaia.c2.content.android.model.Vibration.java
gaia.c2.content.model.ContextDependentModel.java
gaia.c2.content.sqlite.C2SQLiteContentProvider.java
gaia.c2.content.sqlite.C2SQLiteHelper.java
gaia.c2.content.tools.C2DownloadsContentProvider.java
gaia.c2.content.tools.model.DownloadStatus.java
gaia.c2.context.C2Context.java
gaia.c2.context.DefaultC2Context.java
gaia.c2.context.view.C2Message.java
gaia.c2.context.view.C2View.java
gaia.c2.receivers.ContextBroadcastReceiver.java
gaia.c2.receivers.ContextEventReceiver.java