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.
17 package com.google.ipc.invalidation.examples.android2;
19 import com.google.ipc.invalidation.external.client.InvalidationClientConfig;
20 import com.google.ipc.invalidation.external.client.contrib.AndroidListener;
21 import com.google.ipc.invalidation.external.client.contrib.MultiplexingGcmListener;
22 import com.google.ipc.invalidation.external.client.types.ObjectId;
24 import android.app.Activity;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.os.Bundle;
28 import android.util.Log;
29 import android.widget.TextView;
31 import java.util.HashMap;
33 import java.util.Map.Entry;
36 * A simple sample application that displays information about object registrations and
39 * <p>To submit invalidations, you can run the ExampleServlet:
42 * blaze run java/com/google/ipc/invalidation/examples:ExampleServlet -- \
45 --channelUri="talkgadget.google.com"
48 * <p>and open http://localhost:8888/publisher.
50 * <p>Just publish invalidations with ids similar to 'Obj1', 'Obj2', ... 'Obj3'
53 public final class MainActivity extends Activity {
55 /** Tag used in logging. */
56 private static final String TAG = "TEA2:MainActivity";
58 /** Ticl client configuration. */
59 private static final int CLIENT_TYPE = 4; // Demo client ID.
60 private static final byte[] CLIENT_NAME = "TEA2:eetrofoot".getBytes();
63 * Keep track of current registration and object status. This should probably be implemented
64 * using intents rather than static state but I don't want to distract from the invalidation
65 * client essentials in this example.
67 public static final class State {
68 private static final Map<ObjectId, String> lastInformedVersion =
69 new HashMap<ObjectId, String>();
70 private static volatile MainActivity currentActivity;
72 public static void setVersion(ObjectId objectId, String origin, String description) {
73 synchronized (lastInformedVersion) {
74 Log.i(TAG, "[setVersion] oid=" + objectId +
75 ", origin=" + origin + ", descript=" + description);
76 lastInformedVersion.put(objectId, "From " + origin + "; des=" + description);
78 Log.i(TAG, "[setVersion] calling refreshData");
84 private TextView info;
86 /** Called when the activity is first created. */
88 public void onCreate(Bundle savedInstanceState) {
89 Log.i(TAG, "[onCreate] Creating main activity");
90 super.onCreate(savedInstanceState);
92 MultiplexingGcmListener.initializeGcm(this);
94 // Create and start a notification client. When the client is available, or if there is an
95 // existing client, AndroidListener.reissueRegistrations() is called.
96 Context context = getApplicationContext();
98 // Setting this to true allows us to see invalidations that may suppress older invalidations.
99 // When this flag is 'false', AndroidListener#invalidateUnknownVersion is called instead of
100 // AndroidListener#invalidate when suppression has potentially occurred.
101 final boolean allowSuppression = true;
102 InvalidationClientConfig config = new InvalidationClientConfig(CLIENT_TYPE, CLIENT_NAME,
103 "ExampleListener", allowSuppression);
104 Intent startIntent = AndroidListener.createStartIntent(context, config);
105 context.startService(startIntent);
108 info = new TextView(this);
109 setContentView(info);
111 // Remember the current activity since the TICL service in this example communicates via
113 State.currentActivity = this;
114 Log.i(TAG, "[onCreate] Calling refresh data from main activity");
118 /** Updates UI with current registration status and object versions. */
119 private static void refreshData() {
120 final MainActivity activity = State.currentActivity;
121 if (null != activity) {
122 final StringBuilder builder = new StringBuilder();
123 builder.append("\nLast informed versions status\n");
124 builder.append("--begin-------------\n");
125 synchronized (State.lastInformedVersion) {
126 for (Entry<ObjectId, String> entry : State.lastInformedVersion.entrySet()) {
127 builder.append(entry.getKey().toString()).append(" -> ").append(entry.getValue())
131 builder.append("--end---------------\n");
132 activity.info.post(new Runnable() {
135 activity.info.setText(builder.toString());