Android Question How do you access the "Add Shortcuts" dialog?

NeoTechni

Well-Known Member
Licensed User
Longtime User
I don't think it's part of my launcher, cause other apps can add to it. If it is, how would I access the list of shortcut types?

If it's not, I'd like to bring the dialog up, allow the user to make any kind of shortcut, and return the file/data to me for integration into my own launcher I'm working on.
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
I figured out it won't return the shortcut you make, but the app that is brought up will send it to the homescreen via the com.android.launcher.action.INSTALL_SHORTCUT intent. So all I really need is how to bring up the create shortcut dialog or the list of registered shortcut types and their intents.
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Or perhaps get the permissions of a given package? Then I can just look for com.android.launcher.action.INSTALL_SHORTCUT
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
B4X:
Sub ListShortcutApps 'logs all applicable apps
    Dim Packages As List = EnumShortcutPackages ,temp As Int
    For temp = 0 To packages.Size-1
        Log(Packages.Get(temp))
    Next
End Sub

Sub EnumShortcutPackages As List
    Dim PM As PackageManager, Permission As String = "com.android.launcher.permission.INSTALL_SHORTCUT", Packages As List = PM.GetInstalledPackages , temp As Int ,Permissions() As String
    For temp = Packages.size-1 To 0 Step -1
        Permissions = GetPermissions( Packages.get(temp) )
        If Not(HasPermission(Permission,Permissions)) Then Packages.RemoveAt(temp)
    Next
    Return Packages
End Sub
Sub HasPermission(Permission As String, Permissions() As String) As Boolean
    Dim temp As Int
    For temp = 0 To Permissions.Length -1
        If Permissions(temp).EqualsIgnoreCase(Permission) Then Return True
    Next
End Sub
Sub GetPermissions(Package As String) As String()
      Dim r As Reflector, permissions() As String
      r.Target = r.GetContext
      r.Target = r.RunMethod("getPackageManager")
      r.Target = r.RunMethod4("getPackageInfo", Array As Object(Package, 0x00001000), Array As String("java.lang.String", "java.lang.int")) 'get PackageInfo
      permissions = r.GetField("requestedPermissions")
      If permissions = Null Then
          Dim permissions(0) As String
    End If
      Return permissions
End Sub
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Ignore all my code before, this is what you need:

B4X:
sub Process_Globals
    Private ion As Object'Shortcut creation
End Sub

'START Shortcut creation
Sub ShortcutActivity
    Dim j As Intent , j2 As Intent
    Dim ACTION_PICK_ACTIVITY As String = "android.intent.action.PICK_ACTIVITY"
    Dim ACTION_CREATE_SHORTCUT As String = "android.intent.action.CREATE_SHORTCUT"
    Dim EXTRA_INTENT As String = "android.intent.extra.INTENT"
    Dim EXTRA_TITLE As String = "android.intent.extra.TITLE"

    j.Initialize(ACTION_PICK_ACTIVITY, "")
    j2.Initialize(ACTION_CREATE_SHORTCUT,"")
    j.PutExtra(EXTRA_INTENT, j2)
    j.PutExtra(EXTRA_TITLE, "Create a system shortcut")

    StartActivityForResult(j)

    'Intent j;
    'j = new Intent(Intent.ACTION_PICK_ACTIVITY);
    'j.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
    'j.putExtra(Intent.EXTRA_TITLE, "Create a Shortcut");
    'StartActivityForResult(j, choose_app_for_shortcut);
End Sub

Sub StartActivityForResult(i As Intent)
  Dim jo As JavaObject = GetBA
  ion =  jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
  jo.RunMethod("startActivityForResult", Array As Object(ion, i))
End Sub
Sub GetBA As Object
  Dim jo As JavaObject
  Dim cls As String = Me
  cls = cls.SubString("class ".Length)
  jo.InitializeStatic(cls)
  Return jo.GetField("processBA")
End Sub

Sub ion_Event (MethodName As String, Args() As Object) As Object
      If Args(0) = -1 Then 'resultCode = RESULT_OK
        StartActivity(Args(1))
      End If
      Return Null
End Sub
'END shortcut creation
 
Last edited:
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
I'm stuck at what to do with the resulting intent, as startactivity(i) doesn't work... Do I take the CMP out and use that to start activity?

EDIT: I'm trying startactivity(args(1)) instead

That worked. Done. Thank you.
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
I got that part done. Now I'm trying to set up the broadcast listener for the com.android.launcher.action.INSTALL_SHORTCUT event

I've tried the manifest:
AddReceiverText(stimer,
<intent-filter>
<action android:name="com.android.launcher.action.INSTALL_SHORTCUT" />
</intent-filter>)

and a broadcast receiver:

B4X:
BCR.Initialize("BCR")
    BCR.addAction("com.android.launcher.action.INSTALL_SHORTCUT")
    BCR.SetPriority(2147483647)
    BCR.registerReceiver("" )

Neither seems to be firing
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
I keep googling for more information (I hate being a bother...) but all I find is info on how to make a shortcut from the app side, not how to command from the launcher side...
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
I've also tried:

AddPermission("com.android.launcher.permission.INSTALL_SHORTCUT")
AddReceiverText(stimer,
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="com.android.launcher.action.INSTALL_SHORTCUT" />
</intent-filter>)

To no avail. stimer gets no intents...
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Turns out it's the gmail app being uncooperative... Other apps send it fine, though I still need the gmail app to work...

I got this from another app:
Bundle[{android.intent.extra.shortcut.INTENT=Intent { act=android.intent.action.MAIN cmp=com.nkahoang.screenstandby/.ManualBrightnessChangerActivity (has extras) }, duplicate=True, android.Intent.extra.Shortcut.NAME=23% Brightness, android.Intent.extra.Shortcut.ICON_RESOURCE=com.nkahoang.screenstandby:drawable/brightnessico}]

I'm having problems getting any data from it (of course)

B4X:
  Log("Has Extra: " & StartingIntent.HasExtra("android.intent.extra.shortcut.INTENT")) ' returns true
  Log("I: " & StartingIntent.GetExtra("android.intent.extra.shortcut.INTENT"))'returns null

Even when it has the extra, I can only get null from it...

EDIT: Google shows I should look for Intent.getParcelableExtra which you've helped me on before so I will try that.
 
Last edited:
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
B4X:
Sub GetParceableExtra(StartingIntent As Intent, Extra As String) As String
   Dim r As Reflector
   r.Target = StartingIntent
   Return r.RunMethod2("getParcelableExtra", Extra, "java.lang.String")
End Sub


Dim E_Intent As String = GetParceableExtra(StartingIntent, "android.intent.extra.shortcut.INTENT")
    Dim E_Duplicate As String = GetParceableExtra(StartingIntent, "duplicate")
    Dim E_Name As String = GetParceableExtra(StartingIntent, "android.Intent.extra.Shortcut.NAME")
    Dim E_Icon As String = GetParceableExtra(StartingIntent, "android.Intent.extra.Shortcut.ICON_RESOURCE")

ARRRRRGGGG. It works for android.intent.extra.shortcut.INTENT but none of the others!
EDIT: It's cause despite copy/pasting the code from somewhere else, the others have I uppercased in Intent instead of lowercased...

And worse, very few apps work with the listening system I have set up!
 
Last edited:
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
I'm getting really infuriated. How the hell do you check for null?

I wrote an extrastostring parser cause I was getting pissed off with getting null, and I pass the data from the parser into the Default parameter

B4X:
'VarTypes: java.lang.String java.lang.Boolean
Sub GetParceableExtra(StartingIntent As Intent, Extra As String, Default As String) As Object
    If Not(startingintent.HasExtra(extra)) Then Return Default
    Dim r As Reflector, tempstr As String , IsTempstrNull As Boolean
    r.Target = StartingIntent
    tempstr = r.RunMethod2("getParcelableExtra", Extra, "java.lang.String")
    IsTempstrNull= tempstr=Null OR (tempstr & "" ="null")
    If tempstr = Null OR IsTempstrNull Then Return Default
    Log("tempstr=null" & (tempstr=Null) & " " & IsTempstrNull)
    Try
        If tempstr.Length  = 0 Then Return Default
        Log("GetParceableExtra: " & tempstr & " " & Default)
    Catch
        Return Default
    End Try
 
    Return tempstr
End Sub

it keeps logging tempstr as null but no matter how I try to compare it to null (even by forcing an error) it still says it's not null, thus returning null instead of Default

And I've only found one app that actually works with the broadcast receiver...
 
Upvote 0
Top