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