Author Topic: 'am' command?  (Read 3817 times)

Pete G.

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
    • http://www.GoodeveCA.net
'am' command?
« on: June 28, 2018, 06:04:19 pm »
I would very much like to be able to launch Android apps (in particular, Firefox) from a Termux command line.  This seems to be possible through the 'am' command (included in basic Termux, though I think it's actually part of the SDK(?)), but I'm not having any luck.

I found a topic on StackOverflow that seems to show how to do it -- using the Android Browser as an example:

Code: [Select]
   am start -a android.intent.action.MAIN -n com.android.browser/com.android.browser.BrowserActivity
This works fine -- except that I refuse to use that browser...

I've tried all sorts of similar lines for Firefox, such as:

 
Code: [Select]
am start -a android.intent.action.MAIN -n org.mozilla.gecko/org.mozilla.gecko.LauncherActivity
but I always get back something like:
Code: [Select]
Starting: Intent { act=android.intent.action.MAIN cmp=org.mozilla.gecko/.LauncherActivity }
 Error: Activity not started, unknown error code -2

I've been getting the possibilities from the Firefox AndroidManifest.xml (extracted from the APK and unpacked from the binary, but I don't know anything about it, really, so I'm pretty much lost.  Here's a severely extracted section of the file:

[div class=\'codetop\']CODE[/div][div class=\'codemain\' style=\'height:200px;white-space:pre;overflow:auto\']<?xml version="1.0" encoding="utf-8"?>
<manifest
.....
   <application
.....
         android:value="com.google.android.gms.version" />
      <service
         android:name="org.mozilla.gecko.RemotePresentationService"
         android:exported="com.google.android.gms.version" />
      <activity
         android:theme="com.google.android.gms.version"
         android:name="org.mozilla.gecko.LauncherActivity"
         android:exported="com.google.android.gms.version"
         android:taskAffinity=""
         android:excludeFromRecents="com.google.android.gms.version"
         android:relinquishTaskIdentity="com.google.android.gms.version" />
      <activity-alias
         android:label="Firefox"
         android:name=".App"
         android:targetActivity="org.mozilla.gecko.LauncherActivity">
         <intent-filter android:priority="com.google.android.gms.version">
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="android.intent.category.MULTIWINDOW_LAUNCHER" />
            <category android:name="android.intent.category.APP_BROWSER" />
            <category android:name="android.intent.category.DEFAULT" />
         </intent-filter>
         <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="http" />
            <data android:scheme="https" />
            <data android:scheme="about" />
            <data android:scheme="javascript" />
            <data android:scheme="firefox" />
         </intent-filter>
.....
      </activity-alias>
      <activity
         android:theme="com.google.android.gms.version"
         android:label="com.google.android.gms.version"
         android:name="org.mozilla.gecko.BrowserApp"
         android:exported="com.google.android.gms.version"
         android:taskAffinity="org.mozilla.firefox.BROWSER"
         android:launchMode="com.google.android.gms.version"
         android:configChanges="com.google.android.gms.version"
         android:alwaysRetainTaskState="com.google.android.gms.version"
         android:windowSoftInputMode="com.google.android.gms.version" />
      <activity-alias
         android:name="com.android.internal.app.ResolverActivity"
         android:exported="com.google.android.gms.version"
         android:targetActivity="org.mozilla.gecko.BrowserApp" />
.....
   </application>
</manifest>
[/div]

Anybody deep into Android enough to know what I might do?

Pete G.

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
    • http://www.GoodeveCA.net
'am' command?
« Reply #1 on: June 28, 2018, 11:34:58 pm »
Quote from: Pete G.
I would very much like to be able to launch Android apps (in particular, Firefox) from a Termux command line. ...
Anybody deep into Android enough to know what I might do?

Ohh... Wow...  I got it working!

With one serendipitous accident, I got an initial success. A simple:
   
Code: [Select]
$ am start -n org.mozilla.firefox/.Appstarted the browser.  I'd been using "org.mozilla.gecko" because that's what the AndroidManifest.xml has, but I's also tried "...firefox" previously. By sheer amazing chance, I chose that to edit down to it's simplest form.  And it worked!  All the extra options I'd thought were necessary seem to make it fail.

I had a bit more trouble after that to find a command string that actually fed it the test file, but in the end I found this:
   
Code: [Select]
$ am start -a android.intent.action.VIEW -n org.mozilla.firefox/.App -d /storage/emulated/0/Documents/wp.htmlwhich displayed the file!

Now I can move forward....  

Pete G.

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
    • http://www.GoodeveCA.net
'am' command?
« Reply #2 on: June 29, 2018, 10:08:34 pm »
(Talking to myself a lot, here, but...  )

I'm not quite where I want to be, yet, but I can't see what's blocking me.  As in my previous post, I can now invoke Firefox on an html file in .../Documents, but I find I can't do that from my Termux home directory.  I've tried setting read permission to everybody and made sure home itself is generally readable, but I still get Access Denied.

I see that the Documents group is 'everybody', but using that on the file doesn't help.  I can of course move the file to Documents in my script, but it would be nice if I didn't have to.

Any suggestions?

KF6GPE

  • Newbie
  • *
  • Posts: 31
    • View Profile
'am' command?
« Reply #3 on: June 29, 2018, 11:22:09 pm »
It could be that the Termux home directory is in the Android package`s file space, which would only be visible to Termux regardless of file modes, if I recall correctly

Pete G.

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
    • http://www.GoodeveCA.net
'am' command?
« Reply #4 on: June 30, 2018, 08:47:38 pm »
Quote from: KF6GPE
It could be that the Termux home directory is in the Android package`s file space, which would only be visible to Termux regardless of file modes, if I recall correctly
I think it must be something like that.  What puzzles me a bit is that termux-open -- which also uses the 'am' command -- has no trouble sharing files from the termux area.  (Unfortunately, for some reason, it won't share them to Firefox.)
« Last Edit: June 30, 2018, 08:48:19 pm by Pete G. »

gidds

  • Sr. Member
  • ****
  • Posts: 324
    • View Profile
'am' command?
« Reply #5 on: July 02, 2018, 04:31:18 am »
(I too am thinking out loud here...  Sometimes it's a good way to work stuff out!)

I don't really understand Android yet, but my vague impression is that it works around the restriction on which apps can see which directories, by passing some sort of file reference so that an app can open the data in a file without knowing the filename or having permissions for it directly.  (I think files opened in such a way can't be resaved.)

Perhaps this is connected to 'intents' (which is something else I don't understand)?

Can anyone clarify?
   Andy/
Psion 3a → Psion 5 → Psion 5mx → Gemini → Astro