From 190c8588e97470968425cef62ce266568be55511 Mon Sep 17 00:00:00 2001 From: Greg Alexander Date: Sun, 26 May 2019 19:07:33 -0400 Subject: [PATCH] request the SD card permission if it hasn't already been granted or requested in the past. --- NOTES | 53 ++++++++++++++++++- .../main/java/org/galexander/sshd/Prefs.java | 8 +++ .../java/org/galexander/sshd/SimpleSSHD.java | 20 +++++++ 3 files changed, 80 insertions(+), 1 deletion(-) diff --git a/NOTES b/NOTES index 9298d27..0f5388f 100644 --- a/NOTES +++ b/NOTES @@ -698,7 +698,58 @@ Anyways, there's a bunch of stuff remaining to do, and then I will release it. -XXX - call checkSelfPermission/requestPermission to get WRITE_EXTERNAL_STORAGE (it only works now because it's grandfathered in) +May 26, 2019. + +I got an AVD (emulated android) running SDK 26 (Oreo 8.0) x86. It uses +kvm virtualization so it's much faster than the old emulated ARM I used +to be shackled with. + +Anyways, right off, it says (in a toast): + Developer warning for package "org.galexander.sshd" + Failed to post notification on channel "null" + See log for more details +adb logcat says: + 05-26 17:13:49.877 1741 1764 E NotificationService: No Channel found + for pkg=org.galexander.sshd, channelId=null, id=1, tag=null, + opPkg=org.galexander.sshd, callingUid=10085, userId=0, incomingUserId=0, + notificationUid=10085, notification=Notification(channel=null pri=0 + contentView=org.galexander.sshd/0x7f030001 vibrate=null sound=null + tick defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE) + +This gives a kind of deja vu -- I think from BluntMP3? I'm just adding +it to the todo list. + +To access SimpleSSHD, run + adb forward tcp:2222 tcp:2222 + ssh -p 2222 localhost + +And it demonstrates that the sdcard access is forbidden. In fact, I can +toggle it inside the System Settings -> Applications -> SimpleSSHD -> +permissions -> Storage, and it takes effect more or less immediately. + +Android development is such a constant stream of astonishing garbage. +When it requests the permission, which happens just as I would want it +to, it then pops up a "System UI has stopped" dialog. But look, it isn't +my fault: + + https://stackoverflow.com/questions/49261555/system-ui-has-stopped-emulator-when-requesting-permissions + +For real. There's a bug in the emulator you fix by "increasing the DPI." +I had tried some older 854x480 sort of emulated device because I didn't +want all those pixels, but I'll just use Pixel 2 (the default, and one +Google surely tests). It has a different skin where the soft buttons are +part of the skin, which is I bet what the bug is around. + +So, the permissions request works now. It just asks once, and no matter +what the answer is, it never asks again. And if it is grandfathered in +because the previous version of the app used an older SDK, then it will +never ask and it'll just have the permission. And, at least on an +emulated Pixel 2, it can access /sdcard, which is "internal storage." +It does emulate an "SD card", but it can't access that. + + + +XXX - assign a channel for the service notification XXX - upgrade to newest dropbear diff --git a/app/src/main/java/org/galexander/sshd/Prefs.java b/app/src/main/java/org/galexander/sshd/Prefs.java index da579ac..1ea4ae3 100644 --- a/app/src/main/java/org/galexander/sshd/Prefs.java +++ b/app/src/main/java/org/galexander/sshd/Prefs.java @@ -50,6 +50,14 @@ public class Prefs { public static String get_env() { return pref.getString("env", ""); } + public static boolean get_requested() { /* already requested perms */ + return pref.getBoolean("requested", false); + } + public static void set_requested() { + SharedPreferences.Editor edit = pref.edit(); + edit.putBoolean("requested", true); + edit.commit(); + } /* NB - other defaults can be filled in by either Prefs or Settings as * needed */ diff --git a/app/src/main/java/org/galexander/sshd/SimpleSSHD.java b/app/src/main/java/org/galexander/sshd/SimpleSSHD.java index c1778c6..b01e9b7 100644 --- a/app/src/main/java/org/galexander/sshd/SimpleSSHD.java +++ b/app/src/main/java/org/galexander/sshd/SimpleSSHD.java @@ -7,6 +7,7 @@ import android.content.Context; import android.content.SharedPreferences; import android.content.Intent; import android.content.DialogInterface; +import android.content.pm.PackageManager; import android.text.ClipboardManager; import android.view.View; import android.view.Menu; @@ -24,6 +25,7 @@ import java.net.NetworkInterface; import java.net.InetAddress; import java.util.Collections; import java.util.List; +import android.Manifest; public class SimpleSSHD extends Activity { @@ -50,6 +52,7 @@ public class SimpleSSHD extends Activity synchronized (lock) { curr = this; } + permission(); update_startstop_prime(); updater = new UpdaterThread(); updater.start(); @@ -294,4 +297,21 @@ public class SimpleSSHD extends Activity return "UNKNOWN"; } } + + public void permission() { + if (android.os.Build.VERSION.SDK_INT < 23) { + return; + } + if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { + return; + } + if (Prefs.get_requested()) { /* already asked once */ + return; + } + requestPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0); + } + + public void onRequestPermissionsResult(int code, String[] perms, int[] results) { + Prefs.set_requested(); /* whatever result, don't ask again */ + } }