Android Open Source - LibGeppa Passive Geppa Service






From Project

Back to project page LibGeppa.

License

The source code is released under:

Apache License

If you think the Android project LibGeppa 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 net.cattaka.libgeppa;
/* w ww.j a  v a  2  s.com*/
import net.cattaka.libgeppa.binder.PassiveGeppaServiceFuncs;
import net.cattaka.libgeppa.binder.async.PassiveGeppaServiceFuncsAsync;
import net.cattaka.libgeppa.data.ConnectionCode;
import net.cattaka.libgeppa.data.ConnectionState;
import net.cattaka.libgeppa.data.IPacket;
import net.cattaka.libgeppa.data.IPacketFactory;
import net.cattaka.libgeppa.data.PacketWrapper;
import net.cattaka.libgeppa.passive.IPassiveReceiver;
import net.cattaka.libgeppa.thread.ConnectionThread;
import net.cattaka.libgeppa.thread.IConnectionThreadListener;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.SparseArray;
import android.util.SparseIntArray;

public class PassiveGeppaService<T extends IPacket> extends Service implements
        PassiveGeppaServiceFuncs {
    private PassiveGeppaServiceFuncsAsync mAsync = new PassiveGeppaServiceFuncsAsync(this);

    private IConnectionThreadListener<T> mConnectionThreadListener = new IConnectionThreadListener<T>() {
        @Override
        public void onReceive(T packet) {
            onReceivePacket(packet);
        };

        public void onConnectionStateChanged(ConnectionState state, ConnectionCode code) {
            if (state == ConnectionState.CLOSED) {
                stopConnectionThread();
                if (mBindCount == 0) {
                    stopSelf();
                }
            }

            me.onConnectionStateChanged(state);
            if (code == ConnectionCode.DISCONNECTED) {
                // If other devices are alive, It will restart.
                // otherwise ConnectionThread stop.
                startConnectionThread();
            }
        }
    };

    private IPassiveGeppaService.Stub mBinder = new IPassiveGeppaService.Stub() {
        @Override
        public boolean sendPacket(PacketWrapper packet) throws RemoteException {
            return mAsync.sendPacket(packet);
        }

        @Override
        public boolean isConnected() throws RemoteException {
            return mAsync.isConnected();
        }

        @Override
        public ConnectionState getConnectionState() throws RemoteException {
            return mAsync.getConnectionState();
        }

        @Override
        public int registerGeppaServiceListener(IPassiveGeppaServiceListener listner)
                throws RemoteException {
            return mAsync.registerGeppaServiceListener(listner);
        }

        @Override
        public boolean unregisterGeppaServiceListener(int seq) throws RemoteException {
            return mAsync.unregisterGeppaServiceListener(seq);
        }
    };

    private PassiveGeppaService<T> me = this;

    private ConnectionThread<T> mConnectionThread;

    private ConnectionState mLastConnectionState = ConnectionState.UNKNOWN;

    private IPacketFactory<T> mPacketFactory;

    private IPassiveReceiver<T> mPassiveReceiver;

    private int mListenerSeq;

    private SparseArray<IPassiveGeppaServiceListener> mListenerMap;

    private boolean destroyed;

    private int mBindCount = 0;

    public PassiveGeppaService() throws NullPointerException {
        // mBluetoothAdapter = BluetoothAdapterFactory.getDefaultAdapter();
        mListenerSeq = 1;
        mListenerMap = new SparseArray<IPassiveGeppaServiceListener>();
    }

    public void setup(IPacketFactory<T> packetFactory, IPassiveReceiver<T> passiveReceiver) {
        if (packetFactory == null) {
            throw new NullPointerException("packetFactory");
        }
        if (passiveReceiver == null) {
            throw new NullPointerException("passiveReceiver");
        }
        mPacketFactory = packetFactory;
        mPassiveReceiver = passiveReceiver;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (mPacketFactory == null || mPassiveReceiver == null) {
            throw new IllegalStateException("setup method did not called.");
        }
        mPassiveReceiver.onCreateService(me, mPacketFactory);
        destroyed = false;
    }

    @Override
    public IBinder onBind(Intent paramIntent) {
        mBindCount++;
        startConnectionThread();
        return mBinder;
    }

    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
        mBindCount++;
        startConnectionThread();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        mBindCount--;
        if (mBindCount == 0 && mConnectionThread == null) {
            stopSelf();
            return false;
        } else {
            return true;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // unregisterReceiver(mBtConnReceiver);

        stopConnectionThread();
        destroyed = true;
    }

    private ConnectionThread<T> createConnectionThread(
            IConnectionThreadListener<T> connectionThreadListener) {
        return mPassiveReceiver.createConnectionThread(connectionThreadListener);
    }

    protected void startConnectionThread() {
        if (!destroyed && mConnectionThread == null) {
            mConnectionThread = createConnectionThread(mConnectionThreadListener);
            if (mConnectionThread != null) {
                try {
                    me.onConnectionStateChanged(ConnectionState.INITIAL);
                    mConnectionThread.startThread();
                } catch (InterruptedException e) {
                    // Do not interrupt to main thread.
                    throw new RuntimeException("Do not interrupt to main thread!");
                }
            } else {
                // BluetoothAdapter is disabled.
            }
        }
    }

    protected void stopConnectionThread() {
        if (mConnectionThread != null) {
            try {
                mConnectionThread.stopThread();
            } catch (InterruptedException e) {
                // Do not interrupt to main thread.
                throw new RuntimeException("Do not interrupt to main thread!");
            }
            mConnectionThread = null;
        }
    }

    protected void onReceivePacket(T packet) {
        SparseIntArray errors = new SparseIntArray();
        PacketWrapper packetWrapper = new PacketWrapper(packet);
        for (int i = 0; i < mListenerMap.size(); i++) {
            int key = mListenerMap.keyAt(i);
            IPassiveGeppaServiceListener listner = mListenerMap.valueAt(i);
            try {
                listner.onReceivePacket(packetWrapper);
            } catch (RemoteException e) {
                errors.put(errors.size(), key);
            }
        }
        for (int i = 0; i < errors.size(); i++) {
            int key = errors.keyAt(i);
            mListenerMap.remove(key);
        }
    };

    protected void onConnectionStateChanged(ConnectionState state) {
        mLastConnectionState = state;

        SparseIntArray errors = new SparseIntArray();
        for (int i = 0; i < mListenerMap.size(); i++) {
            int key = mListenerMap.keyAt(i);
            IPassiveGeppaServiceListener listner = mListenerMap.valueAt(i);
            try {
                listner.onConnectionStateChanged(state);
            } catch (RemoteException e) {
                errors.put(errors.size(), key);
            }
        }
        for (int i = 0; i < errors.size(); i++) {
            int key = errors.keyAt(i);
            mListenerMap.remove(key);
        }
    }

    public IPacketFactory<T> getPacketFactory() {
        return mPacketFactory;
    }

    /** for binder */
    @Override
    public boolean sendPacket(PacketWrapper packet) {
        if (mConnectionThread != null) {
            @SuppressWarnings("unchecked")
            T t = (T)packet.getPacket();
            return mConnectionThread.sendPacket(t);
        }
        return false;
    }

    /** for binder */
    @Override
    public boolean isConnected() {
        return (mLastConnectionState == ConnectionState.CONNECTED);
    }

    /** for binder */
    @Override
    public ConnectionState getConnectionState() {
        return mLastConnectionState;
    }

    /** for binder */
    @Override
    public int registerGeppaServiceListener(IPassiveGeppaServiceListener listner) {
        mListenerMap.put(mListenerSeq, listner);
        return mListenerSeq++;
    }

    /** for binder */
    @Override
    public boolean unregisterGeppaServiceListener(int seq) {
        boolean result = (mListenerMap != null);
        mListenerMap.remove(seq);
        return result;
    }

    @Override
    public IBinder asBinder() {
        // not used
        return null;
    }
}




Java Source Code List

net.cattaka.libgeppa.ActiveGeppaService.java
net.cattaka.libgeppa.AdkPassiveGeppaService.java
net.cattaka.libgeppa.BluetoothPassiveGeppaService.java
net.cattaka.libgeppa.Constants.java
net.cattaka.libgeppa.GeppaService.java
net.cattaka.libgeppa.IRawSocket.java
net.cattaka.libgeppa.PassiveGeppaService.java
net.cattaka.libgeppa.adapter.AbsConnectionAdapter.java
net.cattaka.libgeppa.adapter.DummyDeviceAdapter.java
net.cattaka.libgeppa.adapter.IDeviceAdapterListener.java
net.cattaka.libgeppa.adapter.IDeviceAdapter.java
net.cattaka.libgeppa.adapter.IDeviceCommandAdapter.java
net.cattaka.libgeppa.adapter.LocalDeviceAdapter.java
net.cattaka.libgeppa.adapter.RemoteDeviceAdapter.java
net.cattaka.libgeppa.binder.ActiveGeppaServiceFuncs.java
net.cattaka.libgeppa.binder.PassiveGeppaServiceFuncs.java
net.cattaka.libgeppa.binder.async.ActiveGeppaServiceFuncsAsync.java
net.cattaka.libgeppa.binder.async.AsyncInterfaceException.java
net.cattaka.libgeppa.binder.async.PassiveGeppaServiceFuncsAsync.java
net.cattaka.libgeppa.bluetooth.BluetoothAdapterFactory.java
net.cattaka.libgeppa.bluetooth.BluetoothAdapterWrapper.java
net.cattaka.libgeppa.bluetooth.BluetoothDeviceWrapper.java
net.cattaka.libgeppa.bluetooth.BluetoothSocketWrapper.java
net.cattaka.libgeppa.bluetooth.IBluetoothAdapter.java
net.cattaka.libgeppa.bluetooth.IBluetoothDevice.java
net.cattaka.libgeppa.bluetooth.IBluetoothSocket.java
net.cattaka.libgeppa.data.BaudRate.java
net.cattaka.libgeppa.data.ConnectionCode.java
net.cattaka.libgeppa.data.ConnectionState.java
net.cattaka.libgeppa.data.DeviceEventCode.java
net.cattaka.libgeppa.data.DeviceInfo.java
net.cattaka.libgeppa.data.DeviceState.java
net.cattaka.libgeppa.data.IPacketFactory.java
net.cattaka.libgeppa.data.IPacket.java
net.cattaka.libgeppa.data.PacketWrapper.java
net.cattaka.libgeppa.data.SocketState.java
net.cattaka.libgeppa.exception.NotImplementedException.java
net.cattaka.libgeppa.net.DummySocketPrepareTask.java
net.cattaka.libgeppa.net.DummySocket.java
net.cattaka.libgeppa.net.PhysicaloidSocketPrepareTask.java
net.cattaka.libgeppa.net.PhysicaloidSocket.java
net.cattaka.libgeppa.net.RemoteSocketPrepareTask.java
net.cattaka.libgeppa.net.RemoteSocket.java
net.cattaka.libgeppa.net.UsbClass.java
net.cattaka.libgeppa.passive.AdkPassiveReceiver.java
net.cattaka.libgeppa.passive.BluetoothPassiveReceiver.java
net.cattaka.libgeppa.passive.IPassiveReceiverListener.java
net.cattaka.libgeppa.passive.IPassiveReceiver.java
net.cattaka.libgeppa.socket.AdkRawSocket.java
net.cattaka.libgeppa.socket.BtRawSocket.java
net.cattaka.libgeppa.thread.ClientThread.java
net.cattaka.libgeppa.thread.ConnectionThread.java
net.cattaka.libgeppa.thread.IConnectionThreadListener.java
net.cattaka.libgeppa.thread.ReceiveThread.java
net.cattaka.libgeppa.thread.ServerThread.java
net.cattaka.libgeppa.util.AidlUtil.java
net.cattaka.libgeppa.util.DeviceUtil.java