1/*
2 * Copyright (C) 2015 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.tv.util;
18
19import android.app.SearchManager;
20import android.content.Context;
21import android.os.Bundle;
22import android.os.UserHandle;
23import android.util.Log;
24
25import java.lang.reflect.InvocationTargetException;
26
27/**
28 * A convenience class for calling methods in android.app.SearchManager.
29 */
30public final class SearchManagerHelper {
31    private static final String TAG = "SearchManagerHelper";
32
33    private static final Object sLock = new Object();
34    private static SearchManagerHelper sInstance;
35
36    private final SearchManager mSearchManager;
37
38    private SearchManagerHelper(Context context) {
39        mSearchManager = ((android.app.SearchManager) context.getSystemService(
40                Context.SEARCH_SERVICE));
41    }
42
43    public static SearchManagerHelper getInstance(Context context) {
44        synchronized (sLock) {
45            if (sInstance == null) {
46                sInstance = new SearchManagerHelper(context.getApplicationContext());
47            }
48            return sInstance;
49        }
50    }
51
52    public void launchAssistAction() {
53        try {
54            SearchManager.class.getDeclaredMethod("launchLegacyAssist", String.class, Integer.TYPE,
55                    Bundle.class).invoke(mSearchManager, null, UserHandle.myUserId(), null);
56        }  catch (NoSuchMethodException | IllegalArgumentException | IllegalAccessException
57                | InvocationTargetException e) {
58            Log.e(TAG, "Fail to call SearchManager.launchAssistAction", e);
59        }
60    }
61}
62