2 * Copyright 2011 Google Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.google.ipc.invalidation.ticl.android2.channel;
18 import com.google.common.base.Preconditions;
19 import com.google.ipc.invalidation.external.client.SystemResources;
20 import com.google.ipc.invalidation.ticl.TestableNetworkChannel;
21 import com.google.ipc.invalidation.ticl.android2.ProtocolIntents;
22 import com.google.ipc.invalidation.ticl.android2.ResourcesFactory.AndroidResources;
23 import com.google.protos.ipc.invalidation.ChannelCommon.NetworkEndpointId;
25 import android.content.Context;
26 import android.content.Intent;
29 * A network channel for Android that receives messages by GCM and that sends messages
33 public class AndroidNetworkChannel implements TestableNetworkChannel {
34 private final Context context;
35 private AndroidResources resources;
37 public AndroidNetworkChannel(Context context) {
38 this.context = Preconditions.checkNotNull(context);
42 public void sendMessage(byte[] outgoingMessage) {
43 Intent intent = ProtocolIntents.newOutboundMessageIntent(outgoingMessage);
44 intent.setClassName(context, AndroidMessageSenderService.class.getName());
45 context.startService(intent);
49 public void setListener(NetworkListener listener) {
50 resources.setNetworkListener(listener);
54 public void setSystemResources(SystemResources resources) {
55 this.resources = (AndroidResources) Preconditions.checkNotNull(resources);
59 public NetworkEndpointId getNetworkIdForTest() {
60 return AndroidMessageSenderService.getNetworkEndpointId(context, resources.getLogger());