Merge "Add setting for assistant" into mnc-dev

This commit is contained in:
Xiyuan Xia
2015-06-09 22:00:30 +00:00
committed by Android (Google) Code Review
2 changed files with 69 additions and 25 deletions

View File

@@ -5548,6 +5548,15 @@ public final class Settings {
*/ */
public static final String DOUBLE_TAP_TO_WAKE = "double_tap_to_wake"; public static final String DOUBLE_TAP_TO_WAKE = "double_tap_to_wake";
/**
* The current assistant component. It could be a voice interaction service,
* or an activity that handles ACTION_ASSIST, or empty which means using the default
* handling.
*
* @hide
*/
public static final String ASSISTANT = "assistant";
/** /**
* This are the settings to be backed up. * This are the settings to be backed up.
* *

View File

@@ -9,14 +9,15 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.res.Resources; import android.content.res.Resources;
import android.database.ContentObserver;
import android.graphics.PixelFormat; import android.graphics.PixelFormat;
import android.media.AudioAttributes; import android.media.AudioAttributes;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.RemoteException; import android.os.RemoteException;
import android.os.ServiceManager; import android.os.ServiceManager;
import android.os.UserHandle; import android.os.UserHandle;
import android.os.Vibrator;
import android.provider.Settings; import android.provider.Settings;
import android.util.Log; import android.util.Log;
import android.view.Gravity; import android.view.Gravity;
@@ -56,6 +57,8 @@ public class AssistManager {
private final PhoneStatusBar mBar; private final PhoneStatusBar mBar;
private final IVoiceInteractionManagerService mVoiceInteractionManagerService; private final IVoiceInteractionManagerService mVoiceInteractionManagerService;
private ComponentName mAssistComponent;
private IVoiceInteractionSessionShowCallback mShowCallback = private IVoiceInteractionSessionShowCallback mShowCallback =
new IVoiceInteractionSessionShowCallback.Stub() { new IVoiceInteractionSessionShowCallback.Stub() {
@@ -78,12 +81,24 @@ public class AssistManager {
} }
}; };
private final ContentObserver mAssistSettingsObserver = new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange) {
updateAssistInfo();
}
};
public AssistManager(PhoneStatusBar bar, Context context) { public AssistManager(PhoneStatusBar bar, Context context) {
mContext = context; mContext = context;
mBar = bar; mBar = bar;
mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
mVoiceInteractionManagerService = IVoiceInteractionManagerService.Stub.asInterface( mVoiceInteractionManagerService = IVoiceInteractionManagerService.Stub.asInterface(
ServiceManager.getService(Context.VOICE_INTERACTION_MANAGER_SERVICE)); ServiceManager.getService(Context.VOICE_INTERACTION_MANAGER_SERVICE));
mContext.getContentResolver().registerContentObserver(
Settings.Secure.getUriFor(Settings.Secure.ASSISTANT), false,
mAssistSettingsObserver);
mAssistSettingsObserver.onChange(false);
} }
public void onConfigurationChanged() { public void onConfigurationChanged() {
@@ -108,16 +123,17 @@ public class AssistManager {
} }
public void onGestureInvoked(boolean vibrate) { public void onGestureInvoked(boolean vibrate) {
boolean isVoiceInteractorActive = getVoiceInteractorSupportsAssistGesture(); if (mAssistComponent == null) {
if (!isVoiceInteractorActive && !isAssistantIntentAvailable()) {
return; return;
} }
if (vibrate) { if (vibrate) {
vibrate(); vibrate();
} }
if (!isVoiceInteractorActive || !isVoiceSessionRunning()) { final boolean isService = isAssistantService();
if (isService || !isVoiceSessionRunning()) {
showOrb(); showOrb();
mView.postDelayed(mHideRunnable, isVoiceInteractorActive mView.postDelayed(mHideRunnable, isService
? TIMEOUT_SERVICE ? TIMEOUT_SERVICE
: TIMEOUT_ACTIVITY); : TIMEOUT_ACTIVITY);
} }
@@ -157,10 +173,12 @@ public class AssistManager {
} }
private void startAssist() { private void startAssist() {
if (getVoiceInteractorSupportsAssistGesture()) { if (mAssistComponent != null) {
startVoiceInteractor(); if (isAssistantService()) {
} else { startVoiceInteractor();
startAssistActivity(); } else {
startAssistActivity();
}
} }
} }
@@ -178,6 +196,9 @@ public class AssistManager {
if (intent == null) { if (intent == null) {
return; return;
} }
if (mAssistComponent != null) {
intent.setComponent(mAssistComponent);
}
try { try {
final ActivityOptions opts = ActivityOptions.makeCustomAnimation(mContext, final ActivityOptions opts = ActivityOptions.makeCustomAnimation(mContext,
@@ -255,19 +276,9 @@ public class AssistManager {
} }
private void maybeSwapSearchIcon() { private void maybeSwapSearchIcon() {
Intent intent = ((SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE)) if (mAssistComponent != null) {
.getAssistIntent(mContext, false, UserHandle.USER_CURRENT); replaceDrawable(mView.getOrb().getLogo(), mAssistComponent, ASSIST_ICON_METADATA_NAME,
ComponentName component = null; isAssistantService());
boolean isService = false;
if (getVoiceInteractorSupportsAssistGesture()) {
component = getVoiceInteractorComponentName();
isService = true;
} else if (intent != null) {
component = intent.getComponent();
}
if (component != null) {
replaceDrawable(mView.getOrb().getLogo(), component, ASSIST_ICON_METADATA_NAME,
isService);
} else { } else {
mView.getOrb().getLogo().setImageDrawable(null); mView.getOrb().getLogo().setImageDrawable(null);
} }
@@ -308,8 +319,32 @@ public class AssistManager {
mView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY); mView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
} }
public boolean isAssistantIntentAvailable() { private boolean isAssistantService() {
return ((SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE)) return mAssistComponent == null ?
.getAssistIntent(mContext, false, UserHandle.USER_CURRENT) != null; false : mAssistComponent.equals(getVoiceInteractorComponentName());
}
private void updateAssistInfo() {
final String setting = Settings.Secure.getStringForUser(mContext.getContentResolver(),
Settings.Secure.ASSISTANT, UserHandle.USER_CURRENT);
if (setting != null) {
mAssistComponent = ComponentName.unflattenFromString(setting);
return;
}
// Fallback to keep backward compatible behavior when there is no user setting.
if (getVoiceInteractorSupportsAssistGesture()) {
mAssistComponent = getVoiceInteractorComponentName();
return;
}
Intent intent = ((SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE))
.getAssistIntent(mContext, false, UserHandle.USER_CURRENT);
if (intent != null) {
mAssistComponent = intent.getComponent();
return;
}
mAssistComponent = null;
} }
} }