001package com.gigya.android.sdk.ui.plugin;
002
003
004import android.os.Bundle;
005import android.support.v4.app.FragmentTransaction;
006import android.support.v7.app.AppCompatActivity;
007
008import com.gigya.android.sdk.Config;
009import com.gigya.android.sdk.GigyaLoginCallback;
010import com.gigya.android.sdk.GigyaPluginCallback;
011import com.gigya.android.sdk.account.models.GigyaAccount;
012import com.gigya.android.sdk.api.IBusinessApiService;
013import com.gigya.android.sdk.ui.Presenter;
014import com.gigya.android.sdk.ui.WebViewFragment;
015import com.gigya.android.sdk.ui.provider.ProviderFragment;
016
017import org.json.JSONObject;
018
019import java.util.Map;
020
021public class WebViewFragmentFactory<A extends GigyaAccount> implements IWebViewFragmentFactory<A> {
022
023    final private Config _config;
024    final private IGigyaWebBridge<A> _gigyaWebBridge;
025
026    public WebViewFragmentFactory(Config config, IGigyaWebBridge<A> gigyaWebBridge) {
027        _config = config;
028        _gigyaWebBridge = gigyaWebBridge;
029    }
030
031    @Override
032    public void showPluginFragment(AppCompatActivity activity,
033                                   String plugin, Map<String, Object> params,
034                                   Bundle args,
035                                   GigyaPluginCallback<A> gigyaPluginCallback) {
036        params.put("containerID", Presenter.Consts.CONTAINER_ID);
037
038        if (params.containsKey("commentsUI")) {
039            params.put("hideShareButtons", true);
040            if (params.get("version") != null && (int) params.get("version") == -1) {
041                params.put("version", 2);
042            }
043        }
044
045        if (params.containsKey("RatingUI") && params.get("showCommentButton") == null) {
046            params.put("showCommentButton", false);
047        }
048
049        final String template =
050                "<head>" +
051                        "<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />" +
052                        "<script>" +
053                        "function onJSException(ex) {" +
054                        "document.location.href = '%s://%s?ex=' + encodeURIComponent(ex);" +
055                        "}" +
056                        "function onJSLoad() {" +
057                        "if (gigya && gigya.isGigya)" +
058                        "window.__wasSocializeLoaded = true;" +
059                        "}" +
060                        "setTimeout(function() {" +
061                        "if (!window.__wasSocializeLoaded)" +
062                        "document.location.href = '%s://%s';" +
063                        "}, %s);" +
064                        "</script>" +
065                        "<script src='https://cdns." + _config.getApiDomain() + "/JS/gigya.js?apikey=%s' type='text/javascript' onLoad='onJSLoad();'>" +
066                        "{" +
067                        "deviceType: 'mobile'" +
068                        "}" +
069                        "</script>" +
070                        "</head>" +
071                        "<body>" +
072                        "<div id='%s'></div>" +
073                        "<script>" +
074                        "%s" +
075                        "try {" +
076                        "gigya._.apiAdapters.mobile.showPlugin('%s', %s);" +
077                        "} catch (ex) { onJSException(ex); }" +
078                        "</script>" +
079                        "</body>";
080
081        final GigyaPluginFragment<A> fragment = new GigyaPluginFragment<>();
082        fragment.setArguments(args);
083        fragment.setConfig(_config);
084        fragment.setWebBridge(_gigyaWebBridge);
085        fragment.setCallback(gigyaPluginCallback);
086        fragment.setHtml(String.format(template,
087                Presenter.Consts.REDIRECT_URL_SCHEME,
088                Presenter.Consts.ON_JS_EXCEPTION,
089                Presenter.Consts.REDIRECT_URL_SCHEME,
090                Presenter.Consts.ON_JS_LOAD_ERROR,
091                Presenter.Consts.JS_TIMEOUT,
092                _config.getApiKey(),
093                Presenter.Consts.CONTAINER_ID,
094                "", // js script before showing the plugin
095                plugin,
096                new JSONObject(params).toString()));
097        FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction();
098        fragmentTransaction.add(fragment, "GigyaPluginFragment");
099        fragmentTransaction.commitAllowingStateLoss();
100    }
101
102    @Override
103    public void showProviderFragment(final AppCompatActivity activity,
104                                     final IBusinessApiService businessApiService,
105                                     final Map<String, Object> params, Bundle args,
106                                     final GigyaLoginCallback gigyaLoginCallback) {
107        ProviderFragment.present(activity, args, new WebViewFragment.WebViewFragmentLifecycleCallbacks() {
108
109            @Override
110            public void onWebViewResult(Map<String, Object> result) {
111                // Handle result.
112                final String providerName = (String) result.get("provider");
113                if (providerName == null) {
114                    // Internal check. Should not happen if SDK implementation is correct.
115                    return;
116                }
117
118                // Okay to release activity.
119                activity.finish();
120
121                businessApiService.login(providerName, params, gigyaLoginCallback);
122            }
123
124            @Override
125            public void onWebViewCancel() {
126
127                // User cancelled WebView.
128                gigyaLoginCallback.onOperationCanceled();
129            }
130        });
131    }
132}