summaryrefslogtreecommitdiff
path: root/same-android/src/main/java/com/orbekk/same/android/ClientInterfaceBridge.java
blob: 7da6c94e4a7b3e1b6e51522d03ee428a1c6653e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
 * Copyright 2012 Kjetil Ørbekk <kjetil.orbekk@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.orbekk.same.android;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;

import com.orbekk.same.ClientInterface;
import com.orbekk.same.ConnectionState;
import com.orbekk.same.ConnectionStateListener;
import com.orbekk.same.State;
import com.orbekk.same.State.Component;
import com.orbekk.same.StateChangedListener;
import com.orbekk.same.VariableFactory;
import com.orbekk.util.DelayedOperation;

public class ClientInterfaceBridge implements ClientInterface {
    private State state;
    private final List<StateChangedListener> listeners = 
            new CopyOnWriteArrayList<StateChangedListener>();
    private final Map<Integer, DelayedOperation> ongoingOperations =
            new HashMap<Integer, DelayedOperation>();
    /** This is used to queue operations until connected to the service. */
    private final ArrayList<Message> pendingOperations = new ArrayList<Message>();
    private volatile int nextOperationNumber = 0;
    
    class ResponseHandler extends Handler {
        @Override public synchronized void handleMessage(Message message) {
            logger.info("ResponseHandler: Got here. Message: {}", message);
            if (serviceMessenger == null) {
                logger.warn("Ignoring message to disabled ResponseHandler.");
                return;
            }
            switch (message.what) {
            case SameService.UPDATED_STATE_CALLBACK:
                State.Component component =
                        new ComponentBundle(message.getData()).getComponent();
                updateState(component);
                break;
            case SameService.OPERATION_STATUS_CALLBACK:
                int operationNumber = message.arg1;
                logger.info("Received callback for operation {}", operationNumber);
                int statusCode = message.getData().getInt("statusCode");
                String statusMessage = message.getData().getString("statusMessage");
                DelayedOperation.Status status =
                        new DelayedOperation.Status(statusCode, statusMessage);
                completeOperation(operationNumber, status);
                break;
            default:
                logger.warn("Received unknown message from service: {}",
                        message);
            }
        }
    }

    private Logger logger = LoggerFactory.getLogger(getClass());
    private volatile Messenger serviceMessenger = null;
    private volatile Messenger responseMessenger = new Messenger(new ResponseHandler());
    private volatile Context context;
    
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            synchronized (ClientInterfaceBridge.this) {
                serviceMessenger = new Messenger(service);
                Message message = Message.obtain(null,
                        SameService.ADD_STATE_RECEIVER);
                message.replyTo = responseMessenger;
                try {
                    serviceMessenger.send(message);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                sendPendingOperations();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            serviceMessenger = null;
        }
    };
    
    private void updateState(State.Component component) {
        state.forceUpdate(component.getName(), component.getData(),
                component.getRevision());
        for (StateChangedListener listener : listeners) {
            listener.stateChanged(component);
        }
    }
    
    private synchronized DelayedOperation createOperation() {
        DelayedOperation op = new DelayedOperation();
        op.setIdentifier(nextOperationNumber);
        nextOperationNumber += 1;
        ongoingOperations.put(op.getIdentifier(), op);
        return op;
    }
    
    private synchronized void completeOperation(int operationNumber,
        DelayedOperation.Status status) {
        DelayedOperation op = ongoingOperations.remove(operationNumber);
        if (op != null) {
            op.complete(status);
        }
    }
    
    public ClientInterfaceBridge(Context context) {
        this.context = context;
    }
    
    public synchronized void connect() {
        state = new State();
        Intent intent = new Intent(context, SameService.class);
        context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }

    private void disconnectFromService() {
        Message message = Message.obtain(null, SameService.REMOVE_STATE_RECEIVER);
        message.obj = responseMessenger;
        try {
            serviceMessenger.send(message);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
    
    public synchronized void disconnect() {
        if (serviceMessenger != null) {
            disconnectFromService();
        }
        if (serviceConnection != null) {
            context.unbindService(serviceConnection);
        }
    }

    @Override
    public State getState() {
        return new State(state);
    }

    @Override
    public synchronized DelayedOperation set(Component component) {
        DelayedOperation op = createOperation();
        Message message = Message.obtain(null, SameService.SET_STATE);
        message.arg1 = op.getIdentifier();
        message.setData(new ComponentBundle(component).getBundle());
        message.replyTo = responseMessenger;
        pendingOperations.add(message);
        sendPendingOperations();
        return op;
    }
    
    private synchronized void sendPendingOperations() {
        if (serviceMessenger == null) {
            logger.warn("Not connected to service. Delaying operations {}",
                    pendingOperations);
            return;
        }
        for (Message message : pendingOperations) {
            try {
                serviceMessenger.send(message);
            } catch (RemoteException e) {
                e.printStackTrace();
                completeOperation(message.arg1, 
                        DelayedOperation.Status.createError(
                                "Error contacting service: " + e.getMessage()));
            }
        }
        pendingOperations.clear();
    }

    @Override
    public void addStateListener(StateChangedListener listener) {
        listeners.add(listener);
    }

    @Override
    public void removeStateListener(StateChangedListener listener) {
        listeners.remove(listener);
    }

    public VariableFactory createVariableFactory() {
        return VariableFactory.create(this);
    }

    /** Doesn't work yet. Maybe we needs a listener interface? */
    @Override
    public ConnectionState getConnectionState() {
        return null;
    }

    @Override
    public void addConnectionStateListener(ConnectionStateListener listener) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void removeConnectionStateListener(ConnectionStateListener listener) {
        throw new UnsupportedOperationException();
    }
}