LibreShift / red-moon

Android screen filter app for night time phone use.
GNU General Public License v3.0
642 stars 80 forks source link

automatically enable settings on return from granting special permission #233

Closed sikaiser closed 5 years ago

sikaiser commented 5 years ago

I noticed that when coming back to the main activity after granting the special permission (either "draw over other apps", or "change system settings") the setting that triggered the permission request has to be enabled again by hand. The settings in question that I've identified so far are the overall on/off toggle, and the "Lower Brightness" setting.

It would be possible to do this automatically in onActivityResult by handling the requestCode. I've detailed the basic logic in a Stackoverflow Answer.

Unfortunately I don't know Kotlin, so I can't make a pull request. But I thought someone might be interested in implementing this. So I'm posting it here.

In Java, an example of the logic for the on/off Toggle and the "draw over other apps" permission would be:

private static void REQUEST_FROM_TOGGLE = 1

toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    if (checkOverlayPermisison()) {
                        enable();
                    } else {
                        toggle.setChecked(false)
                        startActivityForResult(overlayPermissionIntent, REQUEST_FROM_TOGGLE);
                    }
                } else {
                    disable();
                }
            }
        });

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_CANCELED) {
            switch (requestCode){
                case REQUEST_FROM_TOGGLE:
                    if(checkOverlayPermission()) {
                        toggle.setChecked = true;
                    }
                    break;
            }
        }
  }
smichel17 commented 5 years ago

Hi, thanks for the suggestion! I agree this would be an improvement over the current situation. However, I'm planning to implement what I think is an even bigger improvement, so I'm going to close this.

See #114 and #189, but the gist is that turning on a setting that requires permissions will show a bar at the bottom asking the user to grant permissions, but the setting will still be enabled It just won't do anything). This lets me turn on scheduling and automatic pause by default and then just prompt for permissions, instead of requiring people to look through all the settings just to get Red Moon to work (while also avoiding locking people out of the app if they don't grant permissions, which I really dislike as a design pattern).

I'm not sure exactly when I'll get back to that, but along with fading in over a longer period of time, it's the first thing I'm placing to do when I get back to actively adding new features to Red Moon.

Unfortunately I don't know Kotlin, so I can't make a pull request.

If you have the time and you would be interested in contributing here, I'd be happy to walk you through the relevant parts of the code base. If you know Java, Kotlin has a really gentle learning curve (slightly different syntax, but you can convert java->kotlin line-by-line with no semantic changes and it will work). Red Moon has a bit more Kotlin specific stuff than strictly necessary, because I was using it as a chance to learn the language, but it's really not that bad.

If you're interested, ping me over in the chat room (any of irc/matrix/gitter; they're all bridged and I'm on all 3 services).