1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package org.chromium.components.gcm_driver;
6
7import android.content.Intent;
8
9import com.google.ipc.invalidation.external.client.contrib.MultiplexingGcmListener;
10
11import org.chromium.base.ThreadUtils;
12
13/**
14 * Receives GCM registration events and messages rebroadcast by MultiplexingGcmListener.
15 */
16public class GCMListener extends MultiplexingGcmListener.AbstractListener {
17    /**
18     * Receiver for broadcasts by the multiplexed GCM service. It forwards them to
19     * GCMListener.
20     *
21     * This class is public so that it can be instantiated by the Android runtime.
22     */
23    public static class Receiver extends MultiplexingGcmListener.AbstractListener.Receiver {
24        @Override
25        protected Class<?> getServiceClass() {
26            return GCMListener.class;
27        }
28    }
29
30    private static final String TAG = "GCMListener";
31
32    // Used as a fallback since GCM doesn't yet give us the app ID.
33    // TODO(johnme): Get real app IDs from GCM, and remove this.
34    private static final String UNKNOWN_APP_ID = "push#https://www.gcmlistenerfake.com#0";
35
36    public GCMListener() {
37        super(TAG);
38    }
39
40    @Override
41    protected void onRegistered(final String registrationId) {
42        ThreadUtils.runOnUiThread(new Runnable() {
43            @Override public void run() {
44                GCMDriver.onRegisterFinished(UNKNOWN_APP_ID, registrationId);
45            }
46        });
47    }
48
49    @Override
50    protected void onUnregistered(String registrationId) {
51        ThreadUtils.runOnUiThread(new Runnable() {
52            @Override public void run() {
53                GCMDriver.onUnregisterFinished(UNKNOWN_APP_ID);
54            }
55        });
56    }
57
58    @Override
59    protected void onMessage(final Intent intent) {
60        ThreadUtils.runOnUiThread(new Runnable() {
61            @Override public void run() {
62                GCMDriver.onMessageReceived(getApplicationContext(), UNKNOWN_APP_ID,
63                    intent.getExtras());
64            }
65        });
66    }
67
68    @Override
69    protected void onDeletedMessages(int total) {
70        ThreadUtils.runOnUiThread(new Runnable() {
71            @Override public void run() {
72                GCMDriver.onMessagesDeleted(getApplicationContext(), UNKNOWN_APP_ID);
73            }
74        });
75    }
76}
77