1/*
2 * Copyright (C) 2014 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 com.android.inputmethod.compat;
18
19import android.os.Looper;
20
21import java.lang.reflect.Method;
22
23/**
24 * Helper to call Looper#quitSafely, which was introduced in API
25 * level 18 (Build.VERSION_CODES.JELLY_BEAN_MR2).
26 *
27 * In unit tests, we create lots of instances of LatinIME, which means we need to clean up
28 * some Loopers lest we leak file descriptors. In normal use on a device though, this is never
29 * necessary (although it does not hurt).
30 */
31public final class LooperCompatUtils {
32    private static final Method METHOD_quitSafely = CompatUtils.getMethod(
33            Looper.class, "quitSafely");
34
35    public static void quitSafely(final Looper looper) {
36        if (null != METHOD_quitSafely) {
37            CompatUtils.invoke(looper, null /* default return value */, METHOD_quitSafely);
38        } else {
39            looper.quit();
40        }
41    }
42}
43