Android Open Source - sony-camera-remote-java Rpc






From Project

Back to project page sony-camera-remote-java.

License

The source code is released under:

Apache License

If you think the Android project sony-camera-remote-java 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 com.praetoriandroid.cameraremote.app;
//w w w  .j a va  2s.co m
import android.util.Log;

import com.google.common.primitives.Ints;
import com.praetoriandroid.cameraremote.DeviceDescription;
import com.praetoriandroid.cameraremote.LiveViewDisconnectedException;
import com.praetoriandroid.cameraremote.LiveViewFetcher;
import com.praetoriandroid.cameraremote.ParseException;
import com.praetoriandroid.cameraremote.RpcClient;
import com.praetoriandroid.cameraremote.RpcException;
import com.praetoriandroid.cameraremote.SsdpClient;
import com.praetoriandroid.cameraremote.rpc.BaseRequest;
import com.praetoriandroid.cameraremote.rpc.BaseResponse;
import com.praetoriandroid.cameraremote.rpc.GetAvailableSelfTimerRequest;
import com.praetoriandroid.cameraremote.rpc.GetAvailableSelfTimerResponse;
import com.praetoriandroid.cameraremote.rpc.StartLiveViewRequest;
import com.praetoriandroid.cameraremote.rpc.StartLiveViewResponse;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.UiThread;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@EBean (scope = EBean.Scope.Singleton)
public class Rpc {
    private static final String RPC_NETWORK = "RPC network";
    private static final int SSDP_TIMEOUT = 1000;
    private static final int CONNECTION_TIMEOUT = 1000;

    public interface ConnectionListener {
        void onConnected();
        void onConnectionFailed(Throwable e);
    }

    public interface ResponseHandler<Response extends BaseResponse<?>> {
        void onSuccess(Response response);
        void onFail(Throwable e);
    }

    public interface LiveViewCallback {
        void onNextFrame(LiveViewFetcher.Frame frame);
        void onError(Throwable e);
    }

    private RpcClient rpcClient;
    private Throwable initializationError;
    private final Set<ConnectionListener> connectionListeners = new HashSet<ConnectionListener>();
    private boolean initialized;
    private final Map<Object, ResponseHandler<?>> responseHandlers = new HashMap<Object, ResponseHandler<?>>();
    private LiveViewFetcher liveViewFetcher = new LiveViewFetcher();
    private volatile boolean liveViewInProgress;
    private List<Integer> availableSelfTimers = Collections.emptyList();

    public Rpc() {
        liveViewFetcher.setConnectionTimeout(CONNECTION_TIMEOUT);
        connect();
    }

    public List<Integer> getAvailableSelfTimers() {
        return availableSelfTimers;
    }

    @Background (serial = RPC_NETWORK)
    public void connect() {
        try {
            initialized = false;
            initializationError = null;
            SsdpClient ssdpClient = new SsdpClient();
            ssdpClient.setSearchTimeout(SSDP_TIMEOUT);
            String deviceDescriptionUrl = ssdpClient.getDeviceDescriptionUrl();
            DeviceDescription description = new DeviceDescription.Fetcher()
                    .setConnectionTimeout(CONNECTION_TIMEOUT)
                    .fetch(deviceDescriptionUrl);
            String cameraServiceUrl = description.getServiceUrl(DeviceDescription.CAMERA);
            rpcClient = new RpcClient(cameraServiceUrl);
            rpcClient.setConnectionTimeout(CONNECTION_TIMEOUT);
            rpcClient.sayHello();
            GetAvailableSelfTimerResponse selfTimers = rpcClient.send(new GetAvailableSelfTimerRequest());
            if (selfTimers.isOk()) {
                availableSelfTimers = Ints.asList(selfTimers.getAvailableTimers());
            }
            onConnected(cameraServiceUrl);
        } catch (IOException e) {
            onConnectionFailed(e);
        } catch (RpcException e) {
            onConnectionFailed(e);
        }
    }

    @UiThread
    void onConnected(String cameraServiceUrl) {
        initialized = true;
        for (ConnectionListener callback : connectionListeners) {
            callback.onConnected();
        }
    }

    @UiThread
    void onConnectionFailed(Throwable e) {
        Log.e("@@@@@", "RPC connect failed", e);
        initialized = true;
        initializationError = e;
        for (ConnectionListener callback : connectionListeners) {
            callback.onConnectionFailed(e);
        }
    }

    @UiThread (propagation = UiThread.Propagation.REUSE)
    public void registerInitCallback(ConnectionListener callback) {
        if (initialized) {
            if (initializationError == null) {
                callback.onConnected();
            } else {
                callback.onConnectionFailed(initializationError);
            }
        }
        connectionListeners.add(callback);
    }

    @UiThread (propagation = UiThread.Propagation.REUSE)
    public void unregisterInitCallback(ConnectionListener callback) {
        connectionListeners.remove(callback);
    }

    @UiThread (propagation = UiThread.Propagation.REUSE)
    public <Response extends BaseResponse<?>>
    void sendRequest(BaseRequest<?, Response> request, Object tag) {
        if (!initialized) {
            throw new IllegalStateException();
        }
        sendRequestInt(request, tag);
    }

    @UiThread (propagation = UiThread.Propagation.REUSE)
    public <Response extends BaseResponse<?>>
    void sendRequest(BaseRequest<?, Response> request,
                     Object tag,
                     ResponseHandler<Response> responseHandler) {
        if (!initialized) {
            throw new IllegalStateException();
        }
        responseHandlers.put(tag, responseHandler);
        sendRequestInt(request, tag);
    }

    @UiThread
    public void cancelRequest(Object tag) {
        responseHandlers.remove(tag);
    }

    @Background (serial = RPC_NETWORK)
    <Response extends BaseResponse<?>>
    void sendRequestInt(BaseRequest<?, Response> request, Object tag) {
        try {
            Response response = rpcClient.send(request);
            if (response.isOk()) {
                onResponseSuccess(tag, response);
            } else {
                throw new ErrorResponseException(response.getErrorCode());
            }
        } catch (RpcException e) {
            onResponseFail(tag, e);
        }
    }

    @UiThread
    <Response extends BaseResponse<?>> void onResponseSuccess(Object tag, Response response) {
        @SuppressWarnings("unchecked")
        ResponseHandler<Response> handler = (ResponseHandler<Response>) responseHandlers.get(tag);
        if (handler != null) {
            handler.onSuccess(response);
        }
    }

    @UiThread
    <Response extends BaseResponse<?>> void onResponseFail(Object tag, Throwable e) {
        @SuppressWarnings("unchecked")
        ResponseHandler<Response> handler = (ResponseHandler<Response>) responseHandlers.get(tag);
        if (handler != null) {
            handler.onFail(e);
        }
    }

    public void startLiveView(final LiveViewCallback callback) {
        liveViewInProgress = true;
        sendRequest(new StartLiveViewRequest(), liveViewFetcher, new ResponseHandler<StartLiveViewResponse>() {
            @Override
            public void onSuccess(StartLiveViewResponse response) {
                onLiveViewStarted(response.getUrl(), callback);
            }

            @Override
            public void onFail(Throwable e) {
                callback.onError(e);
            }
        });
    }

    public void stopLiveView() {
        try {
            liveViewInProgress = false;
            liveViewFetcher.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void onLiveViewStarted(final String url, final LiveViewCallback callback) {
        new Thread() {
            @Override
            public void run() {
                try {
                    liveViewFetcher.connect(url);
                    while (liveViewInProgress) {
                        LiveViewFetcher.Frame frame = liveViewFetcher.getNextFrame();
                        callback.onNextFrame(frame);
                    }
                } catch (IOException e) {
                    callback.onError(e);
                } catch (ParseException e) {
                    callback.onError(e);
                } catch (LiveViewDisconnectedException ignored) {
                }
            }
        }.start();
    }

    public static class ErrorResponseException extends RpcException {
        private int errorCode;

        public ErrorResponseException(int errorCode) {
            this.errorCode = errorCode;
        }

        public int getErrorCode() {
            return errorCode;
        }
    }
}




Java Source Code List

com.praetoriandroid.cameraremote.BadHttpStatusException.java
com.praetoriandroid.cameraremote.DeviceDescription.java
com.praetoriandroid.cameraremote.HttpClient.java
com.praetoriandroid.cameraremote.InvalidDataFormatException.java
com.praetoriandroid.cameraremote.LiveViewDisconnectedException.java
com.praetoriandroid.cameraremote.LiveViewFetcher.java
com.praetoriandroid.cameraremote.Logger.java
com.praetoriandroid.cameraremote.ParseException.java
com.praetoriandroid.cameraremote.RpcClient.java
com.praetoriandroid.cameraremote.RpcException.java
com.praetoriandroid.cameraremote.ServiceNotSupportedException.java
com.praetoriandroid.cameraremote.SsdpClient.java
com.praetoriandroid.cameraremote.SsdpException.java
com.praetoriandroid.cameraremote.app.LiveView.java
com.praetoriandroid.cameraremote.app.MainActivity.java
com.praetoriandroid.cameraremote.app.Rpc.java
com.praetoriandroid.cameraremote.app.SelfTimerSelector.java
com.praetoriandroid.cameraremote.app.ThisApp.java
com.praetoriandroid.cameraremote.rpc.ActTakePictureRequest.java
com.praetoriandroid.cameraremote.rpc.ActTakePictureResponse.java
com.praetoriandroid.cameraremote.rpc.AwaitTakePictureRequest.java
com.praetoriandroid.cameraremote.rpc.BaseRequest.java
com.praetoriandroid.cameraremote.rpc.BaseResponse.java
com.praetoriandroid.cameraremote.rpc.CustomizableEntity.java
com.praetoriandroid.cameraremote.rpc.EventEntity.java
com.praetoriandroid.cameraremote.rpc.GetApplicationInfoRequest.java
com.praetoriandroid.cameraremote.rpc.GetApplicationInfoResponse.java
com.praetoriandroid.cameraremote.rpc.GetAvailableApiListRequest.java
com.praetoriandroid.cameraremote.rpc.GetAvailableApiListResponse.java
com.praetoriandroid.cameraremote.rpc.GetAvailableSelfTimerRequest.java
com.praetoriandroid.cameraremote.rpc.GetAvailableSelfTimerResponse.java
com.praetoriandroid.cameraremote.rpc.GetEventRequest.java
com.praetoriandroid.cameraremote.rpc.GetEventResponse.java
com.praetoriandroid.cameraremote.rpc.GetExposureModeRequest.java
com.praetoriandroid.cameraremote.rpc.GetMethodTypesRequest.java
com.praetoriandroid.cameraremote.rpc.GetMethodTypesResponse.java
com.praetoriandroid.cameraremote.rpc.GetSelfTimerRequest.java
com.praetoriandroid.cameraremote.rpc.GetSupportedSelfTimerRequest.java
com.praetoriandroid.cameraremote.rpc.GetSupportedSelfTimerResponse.java
com.praetoriandroid.cameraremote.rpc.GetVersionsRequest.java
com.praetoriandroid.cameraremote.rpc.GetVersionsResponse.java
com.praetoriandroid.cameraremote.rpc.IllegalResultSizeException.java
com.praetoriandroid.cameraremote.rpc.MethodTypesEntity.java
com.praetoriandroid.cameraremote.rpc.RpcMethod.java
com.praetoriandroid.cameraremote.rpc.SetSelfTimerRequest.java
com.praetoriandroid.cameraremote.rpc.SimpleResponse.java
com.praetoriandroid.cameraremote.rpc.StartLiveViewRequest.java
com.praetoriandroid.cameraremote.rpc.StartLiveViewResponse.java
com.praetoriandroid.cameraremote.rpc.StartMovieRecRequest.java
com.praetoriandroid.cameraremote.rpc.StartRecModeRequest.java
com.praetoriandroid.cameraremote.rpc.StopLiveviewRequest.java
com.praetoriandroid.cameraremote.rpc.StopMovieRecRequest.java
com.praetoriandroid.cameraremote.rpc.StopMovieRecResponse.java
com.praetoriandroid.cameraremote.rpc.StopRecModeRequest.java
com.praetoriandroid.cameraremote.rpc.ValidationException.java
com.praetoriandroid.cameraremote.rpc._DummyResponse.java
com.praetoriandroid.cameraremote.tool.Cache.java
com.praetoriandroid.cameraremote.tool.RemoteControl.java
com.praetoriandroid.widget.ItemViewCreator.java
com.praetoriandroid.widget.RadialFreeSpace.java
com.praetoriandroid.widget.RadialSelector.java