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.ticl.android;
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;
27 import java.util.concurrent.Executors;
28 import java.util.concurrent.ScheduledExecutorService;
29 import java.util.concurrent.TimeUnit;
33 * SystemResources creator for the Android system service.
36 public class AndroidResourcesFactory {
39 * Implementation of {@link SystemResources.Scheduler} based on {@code ThreadExecutor}.
42 private static class ExecutorBasedScheduler implements Scheduler {
44 private SystemResources systemResources;
46 /** Scheduler for running and scheduling tasks. */
47 private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
49 /** Thread that belongs to the scheduler. */
50 private Thread thread = null;
52 private final String threadName;
54 /** Creates a scheduler with thread {@code name} for internal logging, etc. */
55 private ExecutorBasedScheduler(final String name) {
60 public long getCurrentTimeMs() {
61 return System.currentTimeMillis();
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") {
71 if (thread != Thread.currentThread()) {
72 // Either at initialization or if the thread has been killed or restarted by the
74 thread = Thread.currentThread();
75 thread.setName(threadName);
78 if (systemResources.isStarted()) {
81 systemResources.getLogger().warning("Not running on internal thread since resources " +
82 "not started %s, %s", delayMs, runnable);
85 }, delayMs, TimeUnit.MILLISECONDS);
89 public boolean isRunningOnThread() {
90 return (thread == null) || (Thread.currentThread() == thread);
94 public void setSystemResources(SystemResources resources) {
95 this.systemResources = resources;
100 // End of nested classes.
104 * Constructs a {@link SystemResourcesBuilder} instance using default scheduling, Android-style
105 * logging, and storage, and using {@code network} to send and receive messages.
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),
113 builder.setPlatform("Android-" + android.os.Build.VERSION.RELEASE);