Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / third_party / cacheinvalidation / src / java / com / google / ipc / invalidation / examples / android2 / MainActivity.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
17 package com.google.ipc.invalidation.examples.android2;
18
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;
23
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;
30
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.Map.Entry;
34
35 /**
36  * A simple  sample application that displays information about object registrations and
37  * versions.
38  *
39  * <p>To submit invalidations, you can run the ExampleServlet:
40  *
41  * <p><code>
42  * blaze run java/com/google/ipc/invalidation/examples:ExampleServlet -- \
43        --publisherSpec="" \
44        --port=8888 \
45        --channelUri="talkgadget.google.com"
46  * </code>
47  *
48  * <p>and open http://localhost:8888/publisher.
49  *
50  * <p>Just publish invalidations with ids similar to 'Obj1', 'Obj2', ... 'Obj3'
51  *
52  */
53 public final class MainActivity extends Activity {
54
55   /** Tag used in logging. */
56   private static final String TAG = "TEA2:MainActivity";
57
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();
61
62   /**
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.
66    */
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;
71
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);
77       }
78       Log.i(TAG, "[setVersion] calling refreshData");
79       refreshData();
80     }
81   }
82
83   // Controls
84   private TextView info;
85
86   /** Called when the activity is first created. */
87   @Override
88   public void onCreate(Bundle savedInstanceState) {
89     Log.i(TAG, "[onCreate] Creating main activity");
90     super.onCreate(savedInstanceState);
91
92     MultiplexingGcmListener.initializeGcm(this);
93
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();
97
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);
106
107     // Setup UI.
108     info = new TextView(this);
109     setContentView(info);
110
111     // Remember the current activity since the TICL service in this example communicates via
112     // static state.
113     State.currentActivity = this;
114     Log.i(TAG, "[onCreate] Calling refresh data from main activity");
115     refreshData();
116   }
117
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())
128               .append("\n");
129         }
130       }
131       builder.append("--end---------------\n");
132       activity.info.post(new Runnable() {
133         @Override
134         public void run() {
135           activity.info.setText(builder.toString());
136         }
137       });
138     }
139   }
140 }