Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / third_party / cacheinvalidation / src / java / com / google / ipc / invalidation / ticl / android / AndroidResourcesFactory.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.ticl.android;
18
19 import com.google.ipc.invalidation.external.client.SystemResources;
20 import com.google.ipc.invalidation.external.client.SystemResources.NetworkChannel;
21 import com.google.ipc.invalidation.external.client.SystemResources.Scheduler;
22 import com.google.ipc.invalidation.external.client.SystemResources.Storage;
23 import com.google.ipc.invalidation.external.client.SystemResourcesBuilder;
24 import com.google.ipc.invalidation.external.client.android.service.AndroidLogger;
25 import com.google.ipc.invalidation.util.NamedRunnable;
26
27 import java.util.concurrent.Executors;
28 import java.util.concurrent.ScheduledExecutorService;
29 import java.util.concurrent.TimeUnit;
30
31
32 /**
33  * SystemResources creator for the Android system service.
34  *
35  */
36 public class AndroidResourcesFactory {
37
38   /**
39    * Implementation of {@link SystemResources.Scheduler} based on {@code ThreadExecutor}.
40    *
41    */
42   private static class ExecutorBasedScheduler implements Scheduler {
43
44     private SystemResources systemResources;
45
46     /** Scheduler for running and scheduling tasks. */
47     private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
48
49     /** Thread that belongs to the scheduler. */
50     private Thread thread = null;
51
52     private final String threadName;
53
54     /** Creates a scheduler with thread {@code name} for internal logging, etc. */
55     private ExecutorBasedScheduler(final String name) {
56       threadName = name;
57     }
58
59     @Override
60     public long getCurrentTimeMs() {
61       return System.currentTimeMillis();
62     }
63
64     @Override
65     public void schedule(final int delayMs, final Runnable runnable) {
66       // For simplicity, schedule first and then check when the event runs later if the resources
67       // have been shut down.
68       scheduler.schedule(new NamedRunnable("AndroidScheduler") {
69         @Override
70         public void run() {
71           if (thread != Thread.currentThread()) {
72             // Either at initialization or if the thread has been killed or restarted by the
73             // Executor service.
74             thread = Thread.currentThread();
75             thread.setName(threadName);
76           }
77
78           if (systemResources.isStarted()) {
79             runnable.run();
80           } else {
81             systemResources.getLogger().warning("Not running on internal thread since resources " +
82               "not started %s, %s", delayMs, runnable);
83           }
84         }
85       }, delayMs, TimeUnit.MILLISECONDS);
86     }
87
88     @Override
89     public boolean isRunningOnThread() {
90       return (thread == null) || (Thread.currentThread() == thread);
91     }
92
93     @Override
94     public void setSystemResources(SystemResources resources) {
95       this.systemResources = resources;
96     }
97   }
98
99   //
100   // End of nested classes.
101   //
102
103   /**
104    * Constructs a {@link SystemResourcesBuilder} instance using default scheduling, Android-style
105    * logging, and storage, and using {@code network} to send and receive messages.
106    */
107   public static SystemResourcesBuilder createResourcesBuilder(String logPrefix,
108       NetworkChannel network, Storage storage) {
109     SystemResourcesBuilder builder = new SystemResourcesBuilder(AndroidLogger.forPrefix(logPrefix),
110           new ExecutorBasedScheduler("ticl" + logPrefix),
111           new ExecutorBasedScheduler("ticl-listener" + logPrefix),
112           network, storage);
113     builder.setPlatform("Android-" + android.os.Build.VERSION.RELEASE);
114     return builder;
115   }
116 }