org.deviceconnect.message.intent.event.IntentEventManager.java Source code

Java tutorial

Introduction

Here is the source code for org.deviceconnect.message.intent.event.IntentEventManager.java

Source

/*
 IntentEventManager.java
 Copyright (c) 2014 NTT DOCOMO,INC.
 Released under the MIT license
 http://opensource.org/licenses/mit-license.php
 */
package org.deviceconnect.message.intent.event;

import java.io.IOException;
import java.util.logging.Logger;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.deviceconnect.message.HttpHeaders;
import org.deviceconnect.message.event.AbstractEventManager;
import org.deviceconnect.message.event.EventHandler;
import org.deviceconnect.message.intent.impl.client.DefaultIntentClient;
import org.deviceconnect.message.intent.params.IntentConnectionParams;
import org.deviceconnect.message.intent.util.JSONFactory;
import org.deviceconnect.utils.URIBuilder;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;

/**
 * ?.
 * 
 * <p>
 * Intent?????????<br/>
 * ??? AndroidManifest.xml??{@link org.deviceconnect.message.intent.impl.io.IntentResponseReceiver}
 * ?????
 * </p>
 * 
 * 
 * @author NTT DOCOMO, INC.
 */
public final class IntentEventManager extends AbstractEventManager {

    /**
     * IntentEventManager??.
     */
    public static final IntentEventManager INSTANCE = new IntentEventManager();

    /**
     * .
     */
    private Logger mLogger = Logger.getLogger("org.deviceconnect.sdk.android");

    /**
     * .
     */
    private Context mContext;

    /**
     * ?????.
     */
    private ComponentName mDConnectComponentName;

    /**
     * ???private.
     */
    private IntentEventManager() {
    }

    /**
     * ?.
     * <p>
     * {@link #registerEvent(org.deviceconnect.utils.URIBuilder, 
     * org.deviceconnect.message.event.EventHandler)}? 
     * {@link #unregisterEvent(org.deviceconnect.utils.URIBuilder)}
     * ????? ?????
     * ????????????Device Connect???Intent??
     * {@link #disconnect()}??????
     * </p>
     * 
     * @param context 
     * @param componentName Device Connect Manager????
     */
    public synchronized void connect(final Context context, final ComponentName componentName) {
        mContext = context;
        mDConnectComponentName = componentName;
    }

    @Override
    public synchronized void disconnect() {
        mContext = null;
        mDConnectComponentName = null;
    }

    @Override
    public HttpResponse registerEvent(final URIBuilder builder, final EventHandler handler) throws IOException {

        // ??ComponentName?????????????????????
        builder.setHost("localhost");
        builder.setPort(0);
        builder.setScheme("http");

        return super.registerEvent(builder, handler);
    }

    @Override
    public HttpResponse unregisterEvent(final URIBuilder builder) throws IOException {

        // ??ComponentName?????????????????????
        builder.setHost("localhost");
        builder.setPort(0);
        builder.setScheme("http");

        return super.unregisterEvent(builder);
    }

    /**
     * ????.
     * 
     * @param event 
     */
    public void sendEvent(final Intent event) {

        try {
            JSONObject json = JSONFactory.convertBundleToJSON(event.getExtras());
            sendEvent(json);
        } catch (JSONException e) {
            mLogger.warning("IntentEventManager#sendEvent. Exception : " + e.getMessage());
        }

    }

    @Override
    protected synchronized HttpResponse execute(final HttpUriRequest request) throws IOException {

        if (mContext == null || mDConnectComponentName == null) {
            throw new IllegalStateException("Connection is not established. "
                    + "Call connect method before call registerEvent/unregisterEvent method.");
        }

        HttpParams params = new BasicHttpParams();
        IntentConnectionParams.setContext(params, mContext);
        IntentConnectionParams.setComponent(params, mDConnectComponentName);
        DefaultIntentClient client = new DefaultIntentClient(params);
        // DefaultIntentClient?????Host?????????????????
        request.setHeader(HttpHeaders.HOST, mDConnectComponentName.flattenToShortString());
        HttpResponse response = client.execute(request);
        // shutdown??Entity??????????
        HttpResponse retRes = copyResponse(response);
        client.getConnectionManager().shutdown();
        return retRes;
    }
}