Skip to content

Commit 07d1b28

Browse files
Amith YamasaniAndroid (Google) Code Review
authored andcommitted
Merge "Launch ASSIST intent on the current user" into jb-mr1-dev
2 parents 22a6516 + c1d07a4 commit 07d1b28

File tree

6 files changed

+101
-18
lines changed

6 files changed

+101
-18
lines changed

core/java/android/app/ISearchManager.aidl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ interface ISearchManager {
3030
List<ResolveInfo> getGlobalSearchActivities();
3131
ComponentName getGlobalSearchActivity();
3232
ComponentName getWebSearchActivity();
33+
ComponentName getAssistIntent(int userHandle);
3334
}

core/java/android/app/SearchManager.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import android.os.Handler;
3232
import android.os.RemoteException;
3333
import android.os.ServiceManager;
34+
import android.os.UserId;
3435
import android.text.TextUtils;
3536
import android.util.Log;
3637
import android.util.Slog;
@@ -845,14 +846,28 @@ public List<SearchableInfo> getSearchablesInGlobalSearch() {
845846
*
846847
* @hide
847848
*/
848-
public static final Intent getAssistIntent(Context context) {
849-
PackageManager pm = context.getPackageManager();
850-
Intent intent = new Intent(Intent.ACTION_ASSIST);
851-
ComponentName component = intent.resolveActivity(pm);
852-
if (component != null) {
853-
intent.setComponent(component);
849+
public Intent getAssistIntent(Context context) {
850+
return getAssistIntent(context, UserId.myUserId());
851+
}
852+
853+
/**
854+
* Gets an intent for launching installed assistant activity, or null if not available.
855+
* @return The assist intent.
856+
*
857+
* @hide
858+
*/
859+
public Intent getAssistIntent(Context context, int userHandle) {
860+
try {
861+
ComponentName comp = mService.getAssistIntent(userHandle);
862+
if (comp == null) {
863+
return null;
864+
}
865+
Intent intent = new Intent(Intent.ACTION_ASSIST);
866+
intent.setComponent(comp);
854867
return intent;
868+
} catch (RemoteException re) {
869+
Log.e(TAG, "getAssistIntent() failed: " + re);
870+
return null;
855871
}
856-
return null;
857872
}
858873
}

core/java/android/server/search/SearchManagerService.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
import com.android.internal.content.PackageMonitor;
2020

21+
import android.app.ActivityManager;
22+
import android.app.ActivityManagerNative;
23+
import android.app.AppGlobals;
2124
import android.app.ISearchManager;
2225
import android.app.SearchManager;
2326
import android.app.SearchableInfo;
@@ -27,14 +30,18 @@
2730
import android.content.Context;
2831
import android.content.Intent;
2932
import android.content.IntentFilter;
33+
import android.content.pm.IPackageManager;
34+
import android.content.pm.PackageManager;
3035
import android.content.pm.ResolveInfo;
3136
import android.database.ContentObserver;
3237
import android.os.Binder;
3338
import android.os.Process;
39+
import android.os.RemoteException;
3440
import android.os.UserId;
3541
import android.os.UserManager;
3642
import android.provider.Settings;
3743
import android.util.Log;
44+
import android.util.Slog;
3845
import android.util.SparseArray;
3946

4047
import java.util.List;
@@ -207,4 +214,48 @@ public ComponentName getWebSearchActivity() {
207214
return getSearchables(UserId.getCallingUserId()).getWebSearchActivity();
208215
}
209216

217+
@Override
218+
public ComponentName getAssistIntent(int userHandle) {
219+
try {
220+
if (userHandle != UserId.getCallingUserId()) {
221+
// Requesting a different user, make sure that they have the permission
222+
if (ActivityManager.checkComponentPermission(
223+
android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
224+
Binder.getCallingUid(), -1, true)
225+
== PackageManager.PERMISSION_GRANTED) {
226+
// Translate to the current user id, if caller wasn't aware
227+
if (userHandle == UserId.USER_CURRENT) {
228+
long identity = Binder.clearCallingIdentity();
229+
userHandle = ActivityManagerNative.getDefault().getCurrentUser().id;
230+
Binder.restoreCallingIdentity(identity);
231+
}
232+
} else {
233+
String msg = "Permission Denial: "
234+
+ "Request to getAssistIntent for " + userHandle
235+
+ " but is calling from user " + UserId.getCallingUserId()
236+
+ "; this requires "
237+
+ android.Manifest.permission.INTERACT_ACROSS_USERS_FULL;
238+
Slog.w(TAG, msg);
239+
return null;
240+
}
241+
}
242+
IPackageManager pm = AppGlobals.getPackageManager();
243+
Intent assistIntent = new Intent(Intent.ACTION_ASSIST);
244+
ResolveInfo info =
245+
pm.resolveIntent(assistIntent,
246+
assistIntent.resolveTypeIfNeeded(mContext.getContentResolver()),
247+
PackageManager.MATCH_DEFAULT_ONLY, userHandle);
248+
if (info != null) {
249+
return new ComponentName(
250+
info.activityInfo.applicationInfo.packageName,
251+
info.activityInfo.name);
252+
}
253+
} catch (RemoteException re) {
254+
// Local call
255+
Log.e(TAG, "RemoteException in getAssistIntent: " + re);
256+
} catch (Exception e) {
257+
Log.e(TAG, "Exception in getAssistIntent: " + e);
258+
}
259+
return null;
260+
}
210261
}

packages/SystemUI/src/com/android/systemui/SearchPanelView.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import android.content.Context;
2525
import android.content.Intent;
2626
import android.content.res.Resources;
27+
import android.os.UserId;
2728
import android.os.Vibrator;
2829
import android.provider.Settings;
2930
import android.util.AttributeSet;
@@ -73,14 +74,15 @@ private void startAssistActivity() {
7374
// Close Recent Apps if needed
7475
mBar.animateCollapse(CommandQueue.FLAG_EXCLUDE_SEARCH_PANEL);
7576
// Launch Assist
76-
Intent intent = SearchManager.getAssistIntent(mContext);
77+
Intent intent = ((SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE))
78+
.getAssistIntent(mContext, UserId.USER_CURRENT);
7779
if (intent == null) return;
7880
try {
7981
ActivityOptions opts = ActivityOptions.makeCustomAnimation(mContext,
8082
R.anim.search_launch_enter, R.anim.search_launch_exit,
8183
getHandler(), this);
8284
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
83-
mContext.startActivity(intent, opts.toBundle());
85+
mContext.startActivityAsUser(intent, opts.toBundle(), UserId.USER_CURRENT);
8486
} catch (ActivityNotFoundException e) {
8587
Slog.w(TAG, "Activity not found for " + intent.getAction());
8688
onAnimationStarted();
@@ -140,7 +142,8 @@ protected void onFinishInflate() {
140142
}
141143

142144
private void maybeSwapSearchIcon() {
143-
Intent intent = SearchManager.getAssistIntent(mContext);
145+
Intent intent = ((SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE))
146+
.getAssistIntent(mContext, UserId.USER_CURRENT);
144147
if (intent != null) {
145148
ComponentName component = intent.getComponent();
146149
if (component == null || !mGlowPadView.replaceTargetDrawablesIfPresent(component,
@@ -277,6 +280,7 @@ private LayoutTransition createLayoutTransitioner() {
277280
}
278281

279282
public boolean isAssistantAvailable() {
280-
return SearchManager.getAssistIntent(mContext) != null;
283+
return ((SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE))
284+
.getAssistIntent(mContext, UserId.USER_CURRENT) != null;
281285
}
282286
}

policy/src/com/android/internal/policy/impl/LockScreen.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import android.content.Intent;
3333
import android.content.res.Configuration;
3434
import android.content.res.Resources;
35+
import android.os.UserId;
3536
import android.os.Vibrator;
3637
import android.view.KeyEvent;
3738
import android.view.LayoutInflater;
@@ -275,7 +276,8 @@ public void updateResources() {
275276

276277
// Update the search icon with drawable from the search .apk
277278
if (!mSearchDisabled) {
278-
Intent intent = SearchManager.getAssistIntent(mContext);
279+
Intent intent = ((SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE))
280+
.getAssistIntent(mContext, UserId.USER_CURRENT);
279281
if (intent != null) {
280282
// XXX Hack. We need to substitute the icon here but haven't formalized
281283
// the public API. The "_google" metadata will be going away, so
@@ -309,7 +311,9 @@ public void onTrigger(View v, int target) {
309311
final int resId = mGlowPadView.getResourceIdForTarget(target);
310312
switch (resId) {
311313
case com.android.internal.R.drawable.ic_action_assist_generic:
312-
Intent assistIntent = SearchManager.getAssistIntent(mContext);
314+
Intent assistIntent =
315+
((SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE))
316+
.getAssistIntent(mContext, UserId.USER_CURRENT);
313317
if (assistIntent != null) {
314318
launchActivity(assistIntent);
315319
} else {
@@ -335,6 +339,10 @@ public void onTrigger(View v, int target) {
335339
}
336340
}
337341

342+
/**
343+
* Launches the said intent for the current foreground user.
344+
* @param intent
345+
*/
338346
private void launchActivity(Intent intent) {
339347
intent.setFlags(
340348
Intent.FLAG_ACTIVITY_NEW_TASK
@@ -346,7 +354,7 @@ private void launchActivity(Intent intent) {
346354
Log.w(TAG, "can't dismiss keyguard on launch");
347355
}
348356
try {
349-
mContext.startActivity(intent);
357+
mContext.startActivityAsUser(intent, UserId.USER_CURRENT);
350358
} catch (ActivityNotFoundException e) {
351359
Log.w(TAG, "Activity not found for intent + " + intent.getAction());
352360
}
@@ -522,7 +530,9 @@ private void updateTargets() {
522530
} else if (disabledBySimState) {
523531
Log.v(TAG, "Camera disabled by Sim State");
524532
}
525-
boolean searchActionAvailable = SearchManager.getAssistIntent(mContext) != null;
533+
boolean searchActionAvailable =
534+
((SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE))
535+
.getAssistIntent(mContext, UserId.USER_CURRENT) != null;
526536
mCameraDisabled = disabledByAdmin || disabledBySimState || !cameraTargetPresent;
527537
mSearchDisabled = disabledBySimState || !searchActionAvailable || !searchTargetPresent;
528538
mUnlockWidgetMethods.updateResources();

policy/src/com/android/internal/policy/impl/PhoneWindowManager.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import android.os.SystemClock;
5656
import android.os.SystemProperties;
5757
import android.os.UEventObserver;
58+
import android.os.UserId;
5859
import android.os.Vibrator;
5960
import android.provider.Settings;
6061

@@ -2110,21 +2111,22 @@ private void launchAssistLongPressAction() {
21102111
if (searchManager != null) {
21112112
searchManager.stopSearch();
21122113
}
2113-
mContext.startActivity(intent);
2114+
mContext.startActivityAsUser(intent, UserId.USER_CURRENT);
21142115
} catch (ActivityNotFoundException e) {
21152116
Slog.w(TAG, "No activity to handle assist long press action.", e);
21162117
}
21172118
}
21182119

21192120
private void launchAssistAction() {
21202121
sendCloseSystemWindows(SYSTEM_DIALOG_REASON_ASSIST);
2121-
Intent intent = SearchManager.getAssistIntent(mContext);
2122+
Intent intent = ((SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE))
2123+
.getAssistIntent(mContext, UserId.USER_CURRENT);
21222124
if (intent != null) {
21232125
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
21242126
| Intent.FLAG_ACTIVITY_SINGLE_TOP
21252127
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
21262128
try {
2127-
mContext.startActivity(intent);
2129+
mContext.startActivityAsUser(intent, UserId.USER_CURRENT);
21282130
} catch (ActivityNotFoundException e) {
21292131
Slog.w(TAG, "No activity to handle assist action.", e);
21302132
}

0 commit comments

Comments
 (0)