Back to project page C2Framework.
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.
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); } } }