Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hidden method for android #105

Open
hirbod opened this issue Mar 22, 2018 · 11 comments · May be fixed by #176
Open

hidden method for android #105

hirbod opened this issue Mar 22, 2018 · 11 comments · May be fixed by #176

Comments

@hirbod
Copy link

hirbod commented Mar 22, 2018

Dear @EddyVerbruggen,

I've been struggling a while to get Cookies working fine on iOS, but now I am stuck.
My code is working fine on iOS and android, but "hidden" is not working for android. It's opening visible. The cookies will be set as wished but as there is not even a .hide() method, everything will break on android.

Is there any chance to get hidden loading working for android too?

@EddyVerbruggen
Copy link
Owner

Hi! I'm not the one that created the Android portion of this plugin so I'm not the most qualified to dive into those details. Others perhaps?

@hirbod
Copy link
Author

hirbod commented Mar 22, 2018

I've ended up using InAppBrowser on Android, as it has the hidden feature and it is working as expected now. Thanks anyway @EddyVerbruggen

@hirbod
Copy link
Author

hirbod commented Mar 22, 2018

cordova.InAppBrowser.open(imgAdSrcWithMagicCookie, '_blank', 'location=yes,clearsessioncache=no,clearcache=no,hidden=yes');

// and now open the real url
cordova.InAppBrowser.open(anchor, '_blank', 'location=no,clearsessioncache=no,clearcache=no,hidden=no');

It's important to set clearcache and clearsessioncache to "no". Might help anyone

@FiReBlUe45
Copy link

Hello,
Same problem as @hirbod

I need to get chrome cookies because it is chrome that deals with the authentication part.
I wanted to use the method "6. Reading Safari Data and Cookies with Cordova" but the hidden world on Android does not work.

I need to recover cookies to give cookies to InAppBrowser because it allows to hide the url bar and listen to the events.

Can not customize safariviewcontroller and listen to url called

A solution?

Thank you

@jimitpatel
Copy link

This is what I made changes in plugin at my end and it is working. I followed following link of stackoverflow for how to close CustomChromeTab: https://stackoverflow.com/a/41596629/842607

I made sure that in config.xml I have added line under <platform name="android">
<preference name="AndroidLaunchMode" value="singleTask" />

This will make sure that it will have just single task of that screen, other screen will be overlapped without creating stack of activtivies

Next thing under src/android folder in ChromeCustomTabPlugin.java file, for overriden method execute() hide case is missing. For that I have added following line

case "hide": {
              cordova.getActivity().startActivity(new Intent(cordova.getActivity(), cordova.getActivity().getClass()));
              return true;
            }

Thus that method will look like this:

@Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

        switch (action) {
            case "isAvailable":
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, mCustomTabPluginHelper.isAvailable()));
                return true;

            case "show": {
                final JSONObject options = args.getJSONObject(0);
                final String url = options.optString("url");
                if(TextUtils.isEmpty(url)){
                    JSONObject result = new JSONObject();
                    result.put("error", "expected argument 'url' to be non empty string.");
                    PluginResult pluginResult = new PluginResult(PluginResult.Status.ERROR, result);
                    callbackContext.sendPluginResult(pluginResult);
                    return true;
                }

                final String toolbarColor = options.optString("toolbarColor");
                final Boolean showDefaultShareMenuItem = options.optBoolean("showDefaultShareMenuItem");
                String transition = "";
                mStartAnimationBundle = null;
                final Boolean animated = options.optBoolean("animated", true);
                if(animated) transition = options.optString("transition", "slide");

                PluginResult pluginResult;
                JSONObject result = new JSONObject();
                if(isAvailable()) {
                    try {
                        this.show(url, getColor(toolbarColor), showDefaultShareMenuItem, transition);
                        result.put("event", "loaded");
                        pluginResult = new PluginResult(PluginResult.Status.OK, result);
                        pluginResult.setKeepCallback(true);
                        this.callbackContext = callbackContext;
                    } catch (Exception ex) {
                        result.put("error", ex.getMessage());
                        pluginResult = new PluginResult(PluginResult.Status.ERROR, result);
                    }
                } else {
                    result.put("error", "custom tabs are not available");
                    pluginResult = new PluginResult(PluginResult.Status.ERROR, result);
                }
                callbackContext.sendPluginResult(pluginResult);
                return true;
            }
            case "connectToService": {
                if (bindCustomTabsService())
                    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, true));
                else
                    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "Failed to connect to service"));
                return true;
            }
            case "warmUp": {
                if (warmUp()) {
                    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, true));
                } else {
                    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "Failed to warm up service"));
                }
                return true;
            }
            case "mayLaunchUrl": {
                final String url = args.getString(0);
                if(mayLaunchUrl(url)){
                    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, true));
                } else {
                    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR,String.format("Failed prepare to launch url: %s", url)));
                }
                return true;
            }
            case "hide": {
              cordova.getActivity().startActivity(new Intent(cordova.getActivity(), cordova.getActivity().getClass()));
              return true;
            }
        }
        return false;
    }

Using this way hide started working even on Android platform.

@hirbod
Copy link
Author

hirbod commented Jun 19, 2018

@jimitpatel do you have a fork with that code somewhere on Github?

@hirbod
Copy link
Author

hirbod commented Jun 19, 2018

@jimitpatel an will your code work with "singleTop" (this is what I've set currently)

@jimitpatel
Copy link

jimitpatel commented Jun 19, 2018

@hirbod , I don't have any fork of this. But was facing this issue and found out solution so in return I have posted it over here. Additionally, I can't push on repository from my office :D :D If I do I will be out of the company.

As of singleTop it's not an issue. I have just added it because we didn't want user to access chrome tab by selecting the app from background. Crux of the code is that hide case in execute method.

@hirbod
Copy link
Author

hirbod commented Jun 21, 2018

So if I get you right, @jimitpatel, this will only give us the ability to close it, but not to start a hidden tab, right?

@plvaldes
Copy link

Next thing under src/android folder in ChromeCustomTabPlugin.java file, for overriden method execute() hide case is missing. For that I have added following line

case "hide": {
              cordova.getActivity().startActivity(new Intent(cordova.getActivity(), cordova.getActivity().getClass()));
              return true;
            }

Using this way hide started working even on Android platform.

I've tested this code and it works. It would be great having this feature available @EddyVerbruggen :-)

@EddyVerbruggen
Copy link
Owner

@plvaldes Happy to merge a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants