blob: 0b218e738b7793fbef23c2f51e1b7beb92d3d9eb [file] [log] [blame]
Paul Stewart74fc3bf2016-03-16 11:40:43 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
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
17package android.net;
18
19import android.os.HandlerThread;
20import android.os.Looper;
21
22/**
23 * Shared singleton connectivity thread for the system. This is a thread for
24 * connectivity operations such as AsyncChannel connections to system services.
25 * Various connectivity manager objects can use this singleton as a common
26 * resource for their handlers instead of creating separate threads of their own.
27 * @hide
28 */
29public final class ConnectivityThread extends HandlerThread {
Hugo Benichic4fe5d32016-10-13 16:48:42 +090030
31 // A class implementing the lazy holder idiom: the unique static instance
32 // of ConnectivityThread is instantiated in a thread-safe way (guaranteed by
33 // the language specs) the first time that Singleton is referenced in get()
34 // or getInstanceLooper().
35 private static class Singleton {
36 private static final ConnectivityThread INSTANCE = createInstance();
37 }
Paul Stewart74fc3bf2016-03-16 11:40:43 -070038
39 private ConnectivityThread() {
40 super("ConnectivityThread");
41 }
42
Hugo Benichic4fe5d32016-10-13 16:48:42 +090043 private static ConnectivityThread createInstance() {
44 ConnectivityThread t = new ConnectivityThread();
45 t.start();
46 return t;
Paul Stewart74fc3bf2016-03-16 11:40:43 -070047 }
48
49 public static ConnectivityThread get() {
Hugo Benichic4fe5d32016-10-13 16:48:42 +090050 return Singleton.INSTANCE;
Paul Stewart74fc3bf2016-03-16 11:40:43 -070051 }
52
53 public static Looper getInstanceLooper() {
Hugo Benichic4fe5d32016-10-13 16:48:42 +090054 return Singleton.INSTANCE.getLooper();
Paul Stewart74fc3bf2016-03-16 11:40:43 -070055 }
56}