Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / third_party / cacheinvalidation / src / java / com / google / ipc / invalidation / ticl / android2 / channel / AndroidNetworkChannel.java
1 /*
2  * Copyright 2011 Google Inc.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.google.ipc.invalidation.ticl.android2.channel;
17
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;
24
25 import android.content.Context;
26 import android.content.Intent;
27
28 /**
29  * A network channel for Android that receives messages by GCM and that sends messages
30  * using HTTP.
31  *
32  */
33 public class AndroidNetworkChannel implements TestableNetworkChannel {
34   private final Context context;
35   private AndroidResources resources;
36
37   public AndroidNetworkChannel(Context context) {
38     this.context = Preconditions.checkNotNull(context);
39   }
40
41   @Override
42   public void sendMessage(byte[] outgoingMessage) {
43     Intent intent = ProtocolIntents.newOutboundMessageIntent(outgoingMessage);
44     intent.setClassName(context, AndroidMessageSenderService.class.getName());
45     context.startService(intent);
46   }
47
48   @Override
49   public void setListener(NetworkListener listener) {
50     resources.setNetworkListener(listener);
51   }
52
53   @Override
54   public void setSystemResources(SystemResources resources) {
55     this.resources = (AndroidResources) Preconditions.checkNotNull(resources);
56   }
57
58   @Override
59   public NetworkEndpointId getNetworkIdForTest() {
60     return AndroidMessageSenderService.getNetworkEndpointId(context, resources.getLogger());
61   }
62 }