Android Question Change the default device assist app

Multiverse app

Active Member
Licensed User
Longtime User
After downloading Cortana, it lets you set as the default device assist app. The app does that by firing up the specific settings page via intent. So, what is the specific intent for doing so?
I have asked the same question to many forums, but I got the same answer- It is not possible... Turns out, it is.

Can anyone help me?
Screenshot_20180328-181958_Cortana.jpg
Screenshot_20180328-182010_Cortana.jpg
 

Multiverse app

Active Member
Licensed User
Longtime User
The answer HERE says:

B4X:
public ComponentName getCurrentAssistWithReflection(Context context) {
   try {
       Method myUserIdMethod = UserHandle.class.getDeclaredMethod("myUserId");
       myUserIdMethod.setAccessible(true);
       Integer userId = (Integer) myUserIdMethod.invoke(null);

       if (userId != null) {
           Constructor constructor = Class.forName("com.android.internal.app.AssistUtils").getConstructor(Context.class);
           Object assistUtils = constructor.newInstance(context);

           Method getAssistComponentForUserMethod = assistUtils.getClass().getDeclaredMethod("getAssistComponentForUser", int.class);
           getAssistComponentForUserMethod.setAccessible(true);
           return (ComponentName) getAssistComponentForUserMethod.invoke(assistUtils, userId);
       }
   } catch (Exception e) {
       e.printStackTrace();
   }

   return null;
}
 
Upvote 0

Multiverse app

Active Member
Licensed User
Longtime User
Got it working. Code:
B4X:
    Dim i As Intent
    i.Initialize("android.settings.VOICE_INPUT_SETTINGS", "")
    i.SetComponent("com.android.settings/com.android.settings.Settings$ManageAssistActivity")
    StartActivity(i)
Works on marshmallow and above.
 
Upvote 0

Multiverse app

Active Member
Licensed User
Longtime User
Now, I just need to check if the current app is the default assistant app. How can we do this in B4A?
JAVA Code:
B4X:
String assistant = Settings.Secure.getString(getContentResolver(), "voice_interaction_service");
    ComponentName cn = assistant == null ? null : ComponentName.unflattenFromString(assistant);
    boolean isDefaultAssistant = cn != null && cn.getPackageName().equals(getPackageName());
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Note that it is better to post smaller images. The large images makes it difficult to understand the post.

Try this:
B4X:
Dim p As Phone
Dim Assistant As String = p.GetSetting("voice_interaction_service")
Log(Assistant)
Dim IsDefaultAssistant As Boolean = Assistant.Contains(App.PackageName)
 
Upvote 0

Multiverse app

Active Member
Licensed User
Longtime User
Thank you for the answer!
p.GetSetting("voice_interaction_service")
When the default is set to my app, it returns nothing. But when Google assistant is set, it returns: com.google.android.googlequicksearchbox/com.google.android.voiceinteraction.GsaVoiceInteractionService

I am not able to register the Assistant service properly...
 
Upvote 0

Multiverse app

Active Member
Licensed User
Longtime User
Declare the following in the manifest:
B4X:
'For launching the app on long pressing home button
AddActivityText(Main,
<intent-filter>
<meta-data android:name="com.android.systemui.action_assist_icon"
                    android:icon="@drawable/ic_action_assist" />
                    android:resource="@drawable/ic_action_assist" />
<action android:name="android.intent.action.ASSIST" />

<category android:name="android.intent.category.DEFAULT">
</category>
</intent-filter>)
Where main is the activity to launch at the long press of home button.

Add the following to activity create:
B4X:
Sub Activity_Create(FirstTime As Boolean)

    If FirstTime then 
        Dim r As Reflector
        If r.GetStaticField("android.os.Build$VERSION", "SDK_INT")>22 Then            'IF API>22 THEN
            Dim p As Phone
            Log(p.GetSettings("voice_interaction_service"))
            If p.GetSettings("voice_interaction_service")="com.google.android.googlequicksearchbox/com.google.android.voiceinteraction.GsaVoiceInteractionService" Then SetDefaultAssistant 'If the Default is Google Assistant
        Else            'IF API<22 FOR DEVICES RUNNING LESS THAN MARSHMALLOW
            Msgbox("Long press home button and select '<YOU APP NAME>'", "Set your default Assistant")
        End If
    End If
End Sub


B4X:
Sub SetDefaultAssistant
    Try
        Msgbox("Tap 'Assist app' in the next screen, and choose '<Your App Name>'", "Set your default Assistant")
        Dim i As Intent
        i.Initialize("android.settings.VOICE_INPUT_SETTINGS", "")
        i.SetComponent("com.android.settings/com.android.settings.Settings$ManageAssistActivity")
        StartActivity(i)
    Catch
        Log(LastException)
    End Try

End Sub

In the above Example, I set a different Transparent Activity named"Assist" as the default.
 
Upvote 0
Top