Android Shell Code

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
thecoolpeople
Prole
Posts: 1
Joined: Sat Mar 14, 2020 4:00 pm

Android Shell Code

Post by thecoolpeople »

Hello Programmers:

I have some cool code pieces, which can help many of you! All of these are for android, so have fun. Of course, there are many more, but I think, these are the important ones. If you have some more questions, please write them below! :awesome:
When you have some improvements, write me a private message, or below.

I will improve and find more of such cool commands, and I will share them with you!

Open file with the standard program: (by thecoolpeople)

Code: Select all

local openFile = function(path, mimetype)
  path = love.filesystem.getSaveDirectory() .. "/" .. path
  path = "file://" .. path
  os.execute("am start --user 0 -a android.intent.action.VIEW -d " .. path .. " -t " .. mimetype)
end
Share file: (by thecoolpeople)

Code: Select all

local shareFile = function(path, mimetype)
  path = love.filesystem.getSaveDirectory() .. "/" .. path
  path = "file://" .. path
  os.execute("am start --user 0 -a android.intent.action.SEND -t " .. mimetype .. " --eu android.intent.extra.STREAM " .. path)
end
Kunologist
Prole
Posts: 6
Joined: Fri Mar 20, 2020 9:21 am

Re: Android Shell Code

Post by Kunologist »

Woah, so happy to see that os.execute() is working on LOVE Android!
So I guess os.time() and os.date() would work too!
User avatar
AuahDark
Party member
Posts: 107
Joined: Mon Oct 23, 2017 2:34 pm
Location: Indonesia
Contact:

Re: Android Shell Code

Post by AuahDark »

Beware that code won't work in Android Nougat and later due to file:// URI banned in that and later version of Android. Of course all os.* functions work as they should, only limited by system permissions.
Profile. Do you encounter crashes in LÖVE Android and wanna send me logcats? Please hit me up in LÖVE Discord and send the full logcat file!
Kunologist
Prole
Posts: 6
Joined: Fri Mar 20, 2020 9:21 am

Re: Android Shell Code

Post by Kunologist »

And I found a tiny bit more interesting below os.execute(). With this function we can make pur LOVE game write contents to external directories without root permission. The idea is the Linux behind every Android device.

If you want to create an external directory, use:

Code: Select all

os.execute("mkdir /sdcard/lovegame/savedata/")
(The path is just an instance of using mkdir. Avoid using /sdcard, it's a bad practice after all.)

And to write a file (if doesn't exist, will create it), use:

Code: Select all

os.execute([[echo 'Hello World!' >/sdcard/lovegame/savedata/helloworld.txt]])
And that's pretty amazing given that 11.3 or Android still left us with a backdoor to write external files.

Bad thing I haven't found out how to read, so bleh. Any ideas?
User avatar
Guard13007
Party member
Posts: 132
Joined: Sat Oct 25, 2014 3:42 am
Location: Internet, USA
Contact:

Re: Android Shell Code

Post by Guard13007 »

Can you please explain why writing to /sdcard is considered bad practise and what we should use instead? Just stating not to do it without an alternative isn't very helpful. :(
Kunologist
Prole
Posts: 6
Joined: Fri Mar 20, 2020 9:21 am

Re: Android Shell Code

Post by Kunologist »

Guard13007 wrote: Fri May 15, 2020 8:42 am Can you please explain why writing to /sdcard is considered bad practise and what we should use instead? Just stating not to do it without an alternative isn't very helpful. :(
After so many years I finally came back to this topic again, sorry for inconveniences caused.

I've been working on PC lately, some of the knowledge is just in my head, so sorry if the information below lacks reference.

Firstly, not all Android devices have /sdcard or /mnt/sdcard/... . Android itself has system API for getting such prefixes, and it's a bad practice to presume that these directories exist.

Secondly, writing to external files may be bad as you may overwrite files that are generated by phone user. Who knows whether the user of the phone has /sdcard/lovegame/untitled/save.sav or not? You're potentially erasing the user's file!

Lastly, it's Android-platform-dependent whether this way of file-writing is supported or not. As we all know, people use all versions of Android, and you don't want your software to be only working on several specific Android versions.

Therefore, better take it as a backdoor instead of a good practice~
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 16 guests