VancedMicroG/play-services-core/src/main/java/org/microg/gms/gcm/McsService.java

397 lines
15 KiB
Java
Raw Normal View History

/*
* Copyright 2013-2015 microG 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.
*/
package org.microg.gms.gcm;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.PowerManager;
import android.os.SystemClock;
2015-12-04 19:26:59 +00:00
import android.preference.PreferenceManager;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;
import com.squareup.wire.Message;
import org.microg.gms.checkin.LastCheckinInfo;
import org.microg.gms.gcm.mcs.AppData;
import org.microg.gms.gcm.mcs.Close;
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;
import org.microg.gms.gcm.mcs.LoginResponse;
import org.microg.gms.gcm.mcs.Setting;
import java.net.Socket;
import java.util.Arrays;
2015-03-23 01:14:07 +00:00
import java.util.Collections;
import javax.net.ssl.SSLContext;
import static android.os.Build.VERSION.SDK_INT;
import static org.microg.gms.gcm.Constants.ACTION_CONNECT;
import static org.microg.gms.gcm.Constants.ACTION_HEARTBEAT;
import static org.microg.gms.gcm.Constants.ACTION_RECONNECT;
import static org.microg.gms.gcm.Constants.EXTRA_REASON;
import static org.microg.gms.gcm.Constants.MSG_CONNECT;
import static org.microg.gms.gcm.Constants.MSG_HEARTBEAT;
import static org.microg.gms.gcm.Constants.MSG_INPUT;
import static org.microg.gms.gcm.Constants.MSG_INPUT_ERROR;
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 McsService extends Service implements Handler.Callback {
private static final String TAG = "GmsGcmMcsSvc";
public static final String PREFERENCES_NAME = "mcs";
public static final String PREF_LAST_PERSISTENT_ID = "last_persistent_id";
public static final String SELF_CATEGORY = "com.google.android.gsf.gtalkservice";
public static final String IDLE_NOTIFICATION = "IdleNotification";
public static final String FROM_FIELD = "gcm@android.com";
public static final String SERVICE_HOST = "mtalk.google.com";
public static final int SERVICE_PORT = 5228;
2015-12-04 19:26:59 +00:00
private static final String PREF_GCM_HEARTBEAT = "gcm_heartbeat_interval";
public int heartbeatMs = 60000;
private static Socket sslSocket;
private static McsInputStream inputStream;
private static McsOutputStream outputStream;
private PendingIntent heartbeatIntent;
private static HandlerThread handlerThread;
private static Handler rootHandler;
private AlarmManager alarmManager;
private PowerManager powerManager;
private static PowerManager.WakeLock wakeLock;
private static long currentDelay = 0;
private Intent connectIntent;
private class HandlerThread extends Thread {
@Override
public void run() {
Looper.prepare();
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "mcs");
wakeLock.setReferenceCounted(false);
synchronized (McsService.class) {
rootHandler = new Handler(Looper.myLooper(), McsService.this);
if (connectIntent != null) {
rootHandler.sendMessage(rootHandler.obtainMessage(MSG_CONNECT, connectIntent));
WakefulBroadcastReceiver.completeWakefulIntent(connectIntent);
}
}
Looper.loop();
}
}
@Override
public void onCreate() {
super.onCreate();
2015-12-04 19:26:59 +00:00
updateHeartbeatMs();
heartbeatIntent = PendingIntent.getService(this, 0, new Intent(ACTION_HEARTBEAT, null, this, McsService.class), 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
powerManager = (PowerManager) getSystemService(POWER_SERVICE);
synchronized (McsService.class) {
if (handlerThread == null) {
handlerThread = new HandlerThread();
handlerThread.start();
}
}
}
2015-12-04 19:26:59 +00:00
private void updateHeartbeatMs() {
heartbeatMs = Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(this).getString(PREF_GCM_HEARTBEAT, "60")) * 1000;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
public synchronized static boolean isConnected() {
return inputStream != null && inputStream.isAlive() && outputStream != null && outputStream.isAlive();
}
2015-10-03 22:15:24 +00:00
public static void scheduleReconnect(Context context) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
long delay = getCurrentDelay();
Log.d(TAG, "Scheduling reconnect in " + delay / 1000 + " seconds...");
alarmManager.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + delay,
PendingIntent.getBroadcast(context, 1, new Intent(ACTION_RECONNECT, null, context, TriggerReceiver.class), 0));
2015-10-03 22:15:24 +00:00
}
public synchronized static long getCurrentDelay() {
long delay = currentDelay == 0 ? 5000 : currentDelay;
if (currentDelay < 60000) currentDelay += 10000;
if (currentDelay >= 60000 && currentDelay < 600000) currentDelay += 60000;
return delay;
}
public synchronized static void resetCurrentDelay() {
currentDelay = 0;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
2015-12-04 19:26:59 +00:00
updateHeartbeatMs();
synchronized (McsService.class) {
if (rootHandler != null) {
2015-12-04 02:49:53 +00:00
wakeLock.acquire(5000);
Object reason = intent == null ? "I am so sticky!" :
intent.hasExtra(EXTRA_REASON) ? intent.getExtras().get(EXTRA_REASON) : intent;
if (ACTION_CONNECT.equals(intent.getAction())) {
rootHandler.sendMessage(rootHandler.obtainMessage(MSG_CONNECT, reason));
} else if (ACTION_HEARTBEAT.equals(intent.getAction())) {
rootHandler.sendMessage(rootHandler.obtainMessage(MSG_HEARTBEAT, reason));
}
WakefulBroadcastReceiver.completeWakefulIntent(intent);
} else if (connectIntent == null) {
connectIntent = intent;
} else {
WakefulBroadcastReceiver.completeWakefulIntent(intent);
2015-03-23 01:14:07 +00:00
}
}
return START_STICKY;
2015-03-23 01:14:07 +00:00
}
private synchronized void connect() {
try {
Log.d(TAG, "Starting MCS connection...");
Socket socket = new Socket(SERVICE_HOST, SERVICE_PORT);
Log.d(TAG, "Connected to " + SERVICE_HOST + ":" + SERVICE_PORT);
sslSocket = SSLContext.getDefault().getSocketFactory().createSocket(socket, SERVICE_HOST, SERVICE_PORT, true);
Log.d(TAG, "Activated SSL with " + SERVICE_HOST + ":" + SERVICE_PORT);
inputStream = new McsInputStream(sslSocket.getInputStream(), rootHandler);
outputStream = new McsOutputStream(sslSocket.getOutputStream(), rootHandler);
inputStream.start();
outputStream.start();
2015-12-04 19:26:59 +00:00
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), heartbeatMs, heartbeatIntent);
} catch (Exception e) {
Log.w(TAG, "Exception while connecting!", e);
rootHandler.sendMessage(rootHandler.obtainMessage(MSG_TEARDOWN, e));
}
}
private void handleClose(Close close) {
throw new RuntimeException("Server requested close!");
}
private void handleLoginResponse(LoginResponse loginResponse) {
if (loginResponse.error == null) {
getSharedPreferences().edit().putString(PREF_LAST_PERSISTENT_ID, "").apply();
Log.d(TAG, "Logged in");
wakeLock.release();
} else {
throw new RuntimeException("Could not login: " + loginResponse.error);
2015-03-23 01:14:07 +00:00
}
}
private void handleCloudMessage(DataMessageStanza message) {
if (message.persistent_id != null) {
2015-04-03 22:21:47 +00:00
String old = getSharedPreferences().getString(PREF_LAST_PERSISTENT_ID, "");
if (!old.isEmpty()) {
old += "|";
}
getSharedPreferences().edit()
.putString(PREF_LAST_PERSISTENT_ID, old + message.persistent_id).apply();
}
if (SELF_CATEGORY.equals(message.category)) {
handleSelfMessage(message);
} else {
handleAppMessage(message);
}
}
private void handleHearbeatPing(HeartbeatPing ping) {
HeartbeatAck.Builder ack = new HeartbeatAck.Builder().status(ping.status);
if (inputStream.newStreamIdAvailable()) {
ack.last_stream_id_received(inputStream.getStreamId());
}
send(ack.build());
}
private void handleHeartbeatAck(HeartbeatAck ack) {
wakeLock.release();
}
private LoginRequest buildLoginRequest() {
LastCheckinInfo info = LastCheckinInfo.read(this);
return new LoginRequest.Builder()
.adaptive_heartbeat(false)
.auth_service(LoginRequest.AuthService.ANDROID_ID)
.auth_token(Long.toString(info.securityToken))
.id("android-" + SDK_INT)
.domain("mcs.android.com")
.device_id("android-" + Long.toHexString(info.androidId))
.network_type(1)
.resource(Long.toString(info.androidId))
.user(Long.toString(info.androidId))
.use_rmq2(true)
.setting(Collections.singletonList(new Setting("new_vc", "1")))
.received_persistent_id(Arrays.asList(getSharedPreferences().getString(PREF_LAST_PERSISTENT_ID, "").split("\\|")))
.build();
}
private void handleAppMessage(DataMessageStanza msg) {
Intent intent = new Intent();
intent.setAction("com.google.android.c2dm.intent.RECEIVE");
intent.addCategory(msg.category);
2015-10-20 17:04:01 +00:00
intent.putExtra("from", msg.from);
for (AppData appData : msg.app_data) {
intent.putExtra(appData.key, appData.value);
}
sendOrderedBroadcast(intent, msg.category + ".permission.C2D_MESSAGE");
}
private void handleSelfMessage(DataMessageStanza msg) {
for (AppData appData : msg.app_data) {
if (IDLE_NOTIFICATION.equals(appData.key)) {
DataMessageStanza.Builder msgResponse = new DataMessageStanza.Builder()
.from(FROM_FIELD)
.sent(System.currentTimeMillis() / 1000)
.ttl(0)
.category(SELF_CATEGORY)
2015-03-23 01:14:07 +00:00
.app_data(Collections.singletonList(new AppData(IDLE_NOTIFICATION, "false")));
if (inputStream.newStreamIdAvailable()) {
msgResponse.last_stream_id_received(inputStream.getStreamId());
}
send(msgResponse.build());
}
}
}
private SharedPreferences getSharedPreferences() {
return getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
}
private void send(Message message) {
rootHandler.sendMessage(rootHandler.obtainMessage(MSG_OUTPUT, message));
}
private void sendOutputStream(int what, Object obj) {
McsOutputStream os = outputStream;
if (os != null) {
Handler outputHandler = os.getHandler();
if (outputHandler != null)
outputHandler.sendMessage(outputHandler.obtainMessage(what, obj));
}
}
@Override
public boolean handleMessage(android.os.Message msg) {
switch (msg.what) {
case MSG_INPUT:
handleInput((Message) msg.obj);
return true;
case MSG_OUTPUT:
sendOutputStream(MSG_OUTPUT, msg.obj);
return true;
case MSG_INPUT_ERROR:
case MSG_OUTPUT_ERROR:
Log.d(TAG, "I/O error: " + msg.obj);
rootHandler.sendMessage(rootHandler.obtainMessage(MSG_TEARDOWN, msg.obj));
return true;
case MSG_TEARDOWN:
Log.d(TAG, "Teardown initiated, reason: " + msg.obj);
handleTeardown(msg);
return true;
case MSG_CONNECT:
Log.d(TAG, "Connect initiated, reason: " + msg.obj);
if (!isConnected()) {
connect();
}
return true;
case MSG_HEARTBEAT:
Log.d(TAG, "Heartbeat initiated, reason: " + msg.obj);
if (isConnected()) {
HeartbeatPing.Builder ping = new HeartbeatPing.Builder();
if (inputStream.newStreamIdAvailable()) {
ping.last_stream_id_received(inputStream.getStreamId());
}
send(ping.build());
} else {
Log.d(TAG, "Ignoring heartbeat, not connected!");
scheduleReconnect(this);
}
return true;
case MSG_OUTPUT_READY:
Log.d(TAG, "Sending login request...");
send(buildLoginRequest());
return true;
}
Log.w(TAG, "Unknown message: " + msg);
return false;
}
private void handleInput(Message message) {
try {
if (message instanceof DataMessageStanza) {
handleCloudMessage((DataMessageStanza) message);
} else if (message instanceof HeartbeatPing) {
handleHearbeatPing((HeartbeatPing) message);
} else if (message instanceof Close) {
handleClose((Close) message);
} else if (message instanceof LoginResponse) {
handleLoginResponse((LoginResponse) message);
} else if (message instanceof HeartbeatAck) {
handleHeartbeatAck((HeartbeatAck) message);
} else {
Log.w(TAG, "Unknown message: " + message);
}
resetCurrentDelay();
} catch (Exception e) {
rootHandler.sendMessage(rootHandler.obtainMessage(MSG_TEARDOWN, e));
}
}
private void handleTeardown(android.os.Message msg) {
sendOutputStream(MSG_TEARDOWN, msg.obj);
if (inputStream != null) {
inputStream.close();
inputStream = null;
}
try {
sslSocket.close();
} catch (Exception ignored) {
}
2015-12-04 19:26:59 +00:00
scheduleReconnect(this);
2015-12-04 19:26:59 +00:00
alarmManager.cancel(heartbeatIntent);
if (wakeLock != null) {
wakeLock.release();
}
}
}