2015-03-22 13:32:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013-2015 µg Project Team
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2015-04-02 21:46:47 +00:00
|
|
|
package org.microg.gms.gcm;
|
2015-03-22 13:32:51 +00:00
|
|
|
|
2015-08-04 11:05:47 +00:00
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
2015-03-22 13:32:51 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import com.squareup.wire.Message;
|
|
|
|
|
2015-04-02 21:46:47 +00:00
|
|
|
import org.microg.gms.gcm.mcs.DataMessageStanza;
|
|
|
|
import org.microg.gms.gcm.mcs.HeartbeatAck;
|
|
|
|
import org.microg.gms.gcm.mcs.HeartbeatPing;
|
|
|
|
import org.microg.gms.gcm.mcs.LoginRequest;
|
|
|
|
|
2015-03-22 13:32:51 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
2015-08-04 11:05:47 +00:00
|
|
|
import static org.microg.gms.gcm.Constants.MCS_DATA_MESSAGE_STANZA_TAG;
|
|
|
|
import static org.microg.gms.gcm.Constants.MCS_HEARTBEAT_ACK_TAG;
|
|
|
|
import static org.microg.gms.gcm.Constants.MCS_HEARTBEAT_PING_TAG;
|
|
|
|
import static org.microg.gms.gcm.Constants.MCS_LOGIN_REQUEST_TAG;
|
|
|
|
import static org.microg.gms.gcm.Constants.MCS_VERSION_CODE;
|
|
|
|
import static org.microg.gms.gcm.Constants.MSG_OUTPUT;
|
|
|
|
import static org.microg.gms.gcm.Constants.MSG_OUTPUT_ERROR;
|
|
|
|
import static org.microg.gms.gcm.Constants.MSG_OUTPUT_READY;
|
|
|
|
import static org.microg.gms.gcm.Constants.MSG_TEARDOWN;
|
|
|
|
|
|
|
|
public class McsOutputStream extends Thread implements Handler.Callback {
|
2015-03-22 13:32:51 +00:00
|
|
|
private static final String TAG = "GmsGcmMcsOutput";
|
|
|
|
|
|
|
|
private final OutputStream os;
|
|
|
|
private boolean initialized;
|
|
|
|
private int version = MCS_VERSION_CODE;
|
|
|
|
private int streamId = 0;
|
|
|
|
|
2015-08-04 11:05:47 +00:00
|
|
|
private Handler mainHandler;
|
|
|
|
private Handler myHandler;
|
|
|
|
|
|
|
|
public McsOutputStream(OutputStream os, Handler mainHandler) {
|
|
|
|
this(os, mainHandler, false);
|
2015-03-22 13:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-04 11:05:47 +00:00
|
|
|
public McsOutputStream(OutputStream os, Handler mainHandler, boolean initialized) {
|
2015-03-22 13:32:51 +00:00
|
|
|
this.os = os;
|
2015-08-04 11:05:47 +00:00
|
|
|
this.mainHandler = mainHandler;
|
2015-03-22 13:32:51 +00:00
|
|
|
this.initialized = initialized;
|
2015-08-04 11:05:47 +00:00
|
|
|
setName("McsOutputStream");
|
2015-03-22 13:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getStreamId() {
|
|
|
|
return streamId;
|
|
|
|
}
|
|
|
|
|
2015-08-04 11:05:47 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
Looper.prepare();
|
|
|
|
myHandler = new Handler(this);
|
|
|
|
mainHandler.dispatchMessage(mainHandler.obtainMessage(MSG_OUTPUT_READY));
|
|
|
|
Looper.loop();
|
2015-03-22 13:32:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-04 11:05:47 +00:00
|
|
|
@Override
|
|
|
|
public boolean handleMessage(android.os.Message msg) {
|
|
|
|
switch (msg.what) {
|
|
|
|
case MSG_OUTPUT:
|
|
|
|
try {
|
|
|
|
Message message = (Message) msg.obj;
|
|
|
|
if (msg.obj instanceof DataMessageStanza) {
|
|
|
|
writeInternal(message, MCS_DATA_MESSAGE_STANZA_TAG);
|
|
|
|
} else if (msg.obj instanceof LoginRequest) {
|
|
|
|
writeInternal(message, MCS_LOGIN_REQUEST_TAG);
|
|
|
|
} else if (msg.obj instanceof HeartbeatAck) {
|
|
|
|
writeInternal(message, MCS_HEARTBEAT_ACK_TAG);
|
|
|
|
} else if (msg.obj instanceof HeartbeatPing) {
|
|
|
|
writeInternal(message, MCS_HEARTBEAT_PING_TAG);
|
|
|
|
} else {
|
|
|
|
Log.w(TAG, "Unknown message: " + msg.obj);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
mainHandler.dispatchMessage(mainHandler.obtainMessage(MSG_OUTPUT_ERROR, e));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case MSG_TEARDOWN:
|
|
|
|
try {
|
|
|
|
os.close();
|
|
|
|
} catch (IOException ignored) {
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
Looper.myLooper().quit();
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2015-03-23 01:14:07 +00:00
|
|
|
}
|
|
|
|
|
2015-08-04 11:05:47 +00:00
|
|
|
private synchronized void writeInternal(Message message, int tag) throws IOException {
|
2015-03-22 13:32:51 +00:00
|
|
|
if (!initialized) {
|
|
|
|
Log.d(TAG, "Write MCS version code: " + version);
|
|
|
|
os.write(version);
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
os.write(tag);
|
|
|
|
writeVarint(os, message.getSerializedSize());
|
|
|
|
os.write(message.toByteArray());
|
|
|
|
os.flush();
|
|
|
|
streamId++;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void writeVarint(OutputStream os, int value) throws IOException {
|
|
|
|
while (true) {
|
|
|
|
if ((value & ~0x7FL) == 0) {
|
|
|
|
os.write(value);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
os.write((value & 0x7F) | 0x80);
|
|
|
|
value >>>= 7;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-04 11:05:47 +00:00
|
|
|
|
|
|
|
public Handler getHandler() {
|
|
|
|
return myHandler;
|
|
|
|
}
|
2015-03-22 13:32:51 +00:00
|
|
|
}
|