2013-10-25 15:19:00 +00:00
|
|
|
/*
|
2013-12-20 19:25:49 +00:00
|
|
|
* This is the source code of Telegram for Android v. 1.3.2.
|
2013-10-25 15:19:00 +00:00
|
|
|
* It is licensed under GNU GPL v. 2 or later.
|
|
|
|
* You should have received a copy of the license in this archive (see LICENSE).
|
|
|
|
*
|
|
|
|
* Copyright Nikolai Kudashov, 2013.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.telegram.messenger;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
2014-02-28 22:28:25 +00:00
|
|
|
import android.content.BroadcastReceiver;
|
2013-10-25 15:19:00 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2014-02-28 22:28:25 +00:00
|
|
|
import android.os.PowerManager;
|
2013-10-25 15:19:00 +00:00
|
|
|
|
2014-03-22 22:31:55 +00:00
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.telegram.ui.ApplicationLoader;
|
|
|
|
|
2014-02-28 22:28:25 +00:00
|
|
|
public class GcmBroadcastReceiver extends BroadcastReceiver {
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
public static final int NOTIFICATION_ID = 1;
|
2014-03-10 09:27:49 +00:00
|
|
|
private static PowerManager.WakeLock wakeLock = null;
|
|
|
|
private static final Integer sync = 1;
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onReceive(final Context context, final Intent intent) {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.d("tmessages", "GCM received intent: " + intent);
|
2014-02-28 22:28:25 +00:00
|
|
|
|
|
|
|
if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {
|
2014-03-10 09:27:49 +00:00
|
|
|
synchronized (sync) {
|
|
|
|
try {
|
|
|
|
if (wakeLock == null) {
|
|
|
|
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
|
|
|
|
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "lock");
|
|
|
|
}
|
|
|
|
if (!wakeLock.isHeld()) {
|
|
|
|
wakeLock.acquire(5000);
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
try {
|
|
|
|
if (wakeLock != null) {
|
|
|
|
wakeLock.release();
|
|
|
|
}
|
|
|
|
} catch (Exception e2) {
|
|
|
|
FileLog.e("tmessages", e2);
|
|
|
|
}
|
|
|
|
FileLog.e("tmessages", e);
|
|
|
|
}
|
|
|
|
}
|
2014-02-28 22:28:25 +00:00
|
|
|
|
2014-03-22 22:31:55 +00:00
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
ApplicationLoader.postInitApplication();
|
|
|
|
|
|
|
|
try {
|
|
|
|
String key = intent.getStringExtra("loc_key");
|
|
|
|
if ("DC_UPDATE".equals(key)) {
|
|
|
|
String data = intent.getStringExtra("custom");
|
|
|
|
JSONObject object = new JSONObject(data);
|
|
|
|
int dc = object.getInt("dc");
|
|
|
|
String addr = object.getString("addr");
|
|
|
|
String[] parts = addr.split(":");
|
|
|
|
if (parts.length != 2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
String ip = parts[0];
|
|
|
|
int port = Integer.parseInt(parts[1]);
|
|
|
|
ConnectionsManager.getInstance().applyDcPushUpdate(dc, ip, port);
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
FileLog.e("tmessages", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*SharedPreferences preferences = context.getSharedPreferences("Notifications", Context.MODE_PRIVATE);
|
|
|
|
boolean globalEnabled = preferences.getBoolean("EnableAll", true);
|
|
|
|
if (!globalEnabled) {
|
|
|
|
FileLog.d("tmessages", "GCM disabled");
|
|
|
|
return;
|
|
|
|
}*/
|
2014-02-28 22:28:25 +00:00
|
|
|
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().resumeNetworkMaybe();
|
|
|
|
}
|
|
|
|
});
|
2014-02-28 22:28:25 +00:00
|
|
|
} else if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) {
|
|
|
|
String registration = intent.getStringExtra("registration_id");
|
|
|
|
if (intent.getStringExtra("error") != null) {
|
|
|
|
FileLog.e("tmessages", "Registration failed, should try again later.");
|
|
|
|
} else if (intent.getStringExtra("unregistered") != null) {
|
|
|
|
FileLog.e("tmessages", "unregistration done, new messages from the authorized sender will be rejected");
|
|
|
|
} else if (registration != null) {
|
|
|
|
FileLog.e("tmessages", "registration id = " + registration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
setResultCode(Activity.RESULT_OK);
|
|
|
|
}
|
|
|
|
}
|