Paul Stewart | 74fc3bf | 2016-03-16 11:40:43 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.net; |
| 18 | |
| 19 | import android.os.HandlerThread; |
| 20 | import 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 | */ |
| 29 | public final class ConnectivityThread extends HandlerThread { |
Hugo Benichi | c4fe5d3 | 2016-10-13 16:48:42 +0900 | [diff] [blame] | 30 | |
| 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 Stewart | 74fc3bf | 2016-03-16 11:40:43 -0700 | [diff] [blame] | 38 | |
| 39 | private ConnectivityThread() { |
| 40 | super("ConnectivityThread"); |
| 41 | } |
| 42 | |
Hugo Benichi | c4fe5d3 | 2016-10-13 16:48:42 +0900 | [diff] [blame] | 43 | private static ConnectivityThread createInstance() { |
| 44 | ConnectivityThread t = new ConnectivityThread(); |
| 45 | t.start(); |
| 46 | return t; |
Paul Stewart | 74fc3bf | 2016-03-16 11:40:43 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | public static ConnectivityThread get() { |
Hugo Benichi | c4fe5d3 | 2016-10-13 16:48:42 +0900 | [diff] [blame] | 50 | return Singleton.INSTANCE; |
Paul Stewart | 74fc3bf | 2016-03-16 11:40:43 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | public static Looper getInstanceLooper() { |
Hugo Benichi | c4fe5d3 | 2016-10-13 16:48:42 +0900 | [diff] [blame] | 54 | return Singleton.INSTANCE.getLooper(); |
Paul Stewart | 74fc3bf | 2016-03-16 11:40:43 -0700 | [diff] [blame] | 55 | } |
| 56 | } |