Search in sources :

Example 1 with CodenameOneActivity

use of com.codename1.impl.android.CodenameOneActivity in project CodenameOne by codenameone.

the class FacebookImpl method login.

private void login(final LoginCallback cb) {
    if (loginLock) {
        return;
    }
    loginLock = true;
    LoginManager login = LoginManager.getInstance();
    final CallbackManager mCallbackManager = CallbackManager.Factory.create();
    if (AndroidNativeUtil.getActivity() == null) {
        throw new RuntimeException("Cannot login to facebook when running in the background.");
    }
    final CodenameOneActivity activity = (CodenameOneActivity) AndroidNativeUtil.getActivity();
    activity.setIntentResultListener(new IntentResultListener() {

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            mCallbackManager.onActivityResult(requestCode, resultCode, data);
            activity.restoreIntentResultListener();
        }
    });
    login.registerCallback(mCallbackManager, new FBCallback(cb));
    login.logInWithReadPermissions(activity, permissions);
}
Also used : LoginManager(com.facebook.login.LoginManager) CodenameOneActivity(com.codename1.impl.android.CodenameOneActivity) IntentResultListener(com.codename1.impl.android.IntentResultListener) Intent(android.content.Intent) CallbackManager(com.facebook.CallbackManager)

Example 2 with CodenameOneActivity

use of com.codename1.impl.android.CodenameOneActivity in project CodenameOne by codenameone.

the class FacebookImpl method inviteFriends.

@Override
public void inviteFriends(String appLinkUrl, String previewImageUrl, final Callback cb) {
    if (AndroidNativeUtil.getActivity() == null) {
        throw new RuntimeException("Cannot invite friends while running in the background.");
    }
    if (AppInviteDialog.canShow()) {
        AppInviteContent content = new AppInviteContent.Builder().setApplinkUrl(appLinkUrl).setPreviewImageUrl(previewImageUrl).build();
        final CodenameOneActivity activity = (CodenameOneActivity) AndroidNativeUtil.getActivity();
        if (cb == null) {
            AppInviteDialog.show(activity, content);
        } else {
            AppInviteDialog appInviteDialog = new AppInviteDialog(activity);
            final CallbackManager mCallbackManager = CallbackManager.Factory.create();
            activity.setIntentResultListener(new IntentResultListener() {

                @Override
                public void onActivityResult(int requestCode, int resultCode, Intent data) {
                    mCallbackManager.onActivityResult(requestCode, resultCode, data);
                    activity.restoreIntentResultListener();
                }
            });
            appInviteDialog.registerCallback(mCallbackManager, new FacebookCallback<AppInviteDialog.Result>() {

                @Override
                public void onSuccess(AppInviteDialog.Result result) {
                    Display.getInstance().callSerially(new Runnable() {

                        @Override
                        public void run() {
                            cb.onSucess(null);
                        }
                    });
                }

                @Override
                public void onCancel() {
                    Display.getInstance().callSerially(new Runnable() {

                        @Override
                        public void run() {
                            cb.onError(null, null, -1, "User Cancelled");
                        }
                    });
                }

                @Override
                public void onError(final FacebookException e) {
                    Display.getInstance().callSerially(new Runnable() {

                        @Override
                        public void run() {
                            cb.onError(null, e, 0, e.getMessage());
                        }
                    });
                }
            });
            appInviteDialog.show(content);
        }
    }
}
Also used : CodenameOneActivity(com.codename1.impl.android.CodenameOneActivity) Intent(android.content.Intent) CallbackManager(com.facebook.CallbackManager) AppInviteDialog(com.facebook.share.widget.AppInviteDialog) IntentResultListener(com.codename1.impl.android.IntentResultListener) FacebookException(com.facebook.FacebookException) AppInviteContent(com.facebook.share.model.AppInviteContent)

Example 3 with CodenameOneActivity

use of com.codename1.impl.android.CodenameOneActivity in project CodenameOne by codenameone.

the class FacebookImpl method askPublishPermissions.

public void askPublishPermissions(final LoginCallback cb) {
    if (AndroidNativeUtil.getActivity() == null) {
        throw new RuntimeException("Cannot ask for publish permissions when running in the background.");
    }
    if (loginLock) {
        return;
    }
    loginLock = true;
    LoginManager login = LoginManager.getInstance();
    final CallbackManager mCallbackManager = CallbackManager.Factory.create();
    final CodenameOneActivity activity = (CodenameOneActivity) AndroidNativeUtil.getActivity();
    activity.setIntentResultListener(new IntentResultListener() {

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            mCallbackManager.onActivityResult(requestCode, resultCode, data);
            activity.restoreIntentResultListener();
        }
    });
    login.registerCallback(mCallbackManager, new FBCallback(cb));
    login.logInWithPublishPermissions(activity, PUBLISH_PERMISSIONS);
}
Also used : LoginManager(com.facebook.login.LoginManager) CodenameOneActivity(com.codename1.impl.android.CodenameOneActivity) IntentResultListener(com.codename1.impl.android.IntentResultListener) Intent(android.content.Intent) CallbackManager(com.facebook.CallbackManager)

Example 4 with CodenameOneActivity

use of com.codename1.impl.android.CodenameOneActivity in project CodenameOne by codenameone.

the class GoogleImpl method onConnectionFailed.

public void onConnectionFailed(final ConnectionResult cr) {
    if (AndroidNativeUtil.getActivity() == null) {
        return;
    }
    final CodenameOneActivity main = (CodenameOneActivity) AndroidNativeUtil.getActivity();
    if (!mIntentInProgress && cr.hasResolution()) {
        try {
            mIntentInProgress = true;
            main.startIntentSenderForResult(cr.getResolution().getIntentSender(), 0, null, 0, 0, 0);
            main.setIntentResultListener(new com.codename1.impl.android.IntentResultListener() {

                public void onActivityResult(int requestCode, int resultCode, android.content.Intent data) {
                    mIntentInProgress = false;
                    if (!mGoogleApiClient.isConnecting()) {
                        mGoogleApiClient.connect();
                    }
                    main.restoreIntentResultListener();
                }
            });
        } catch (SendIntentException e) {
            // The intent was canceled before it was sent.  Return to the default
            // state and attempt to connect to get an updated ConnectionResult.
            mIntentInProgress = false;
            mGoogleApiClient.connect();
        }
        return;
    }
    if (callback != null) {
        Display.getInstance().callSerially(new Runnable() {

            @Override
            public void run() {
                callback.loginFailed(GooglePlayServicesUtil.getErrorString(cr.getErrorCode()));
            }
        });
    }
}
Also used : IntentResultListener(com.codename1.impl.android.IntentResultListener) CodenameOneActivity(com.codename1.impl.android.CodenameOneActivity) Intent(android.content.Intent) SendIntentException(android.content.IntentSender.SendIntentException)

Example 5 with CodenameOneActivity

use of com.codename1.impl.android.CodenameOneActivity in project CodenameOne by codenameone.

the class AndroidImplementation method init.

@Override
public void init(Object m) {
    if (m instanceof CodenameOneActivity) {
        setContext(null);
        setActivity((CodenameOneActivity) m);
    } else {
        setActivity(null);
        setContext((Context) m);
    }
    instance = this;
    if (getActivity() != null && getActivity().hasUI()) {
        if (!hasActionBar()) {
            try {
                getActivity().requestWindowFeature(Window.FEATURE_NO_TITLE);
            } catch (Exception e) {
            // Log.d("Codename One", "No idea why this throws a Runtime Error", e);
            }
        } else {
            getActivity().invalidateOptionsMenu();
            try {
                getActivity().requestWindowFeature(Window.FEATURE_ACTION_BAR);
                getActivity().requestWindowFeature(Window.FEATURE_PROGRESS);
                if (android.os.Build.VERSION.SDK_INT >= 21) {
                    // WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
                    getActivity().getWindow().addFlags(-2147483648);
                }
            } catch (Exception e) {
            // Log.d("Codename One", "No idea why this throws a Runtime Error", e);
            }
            NotifyActionBar notify = new NotifyActionBar(getActivity(), false);
            notify.run();
        }
        if (statusBarHidden) {
            getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            getActivity().getWindow().setStatusBarColor(android.graphics.Color.TRANSPARENT);
        }
        if (Display.getInstance().getProperty("StatusbarHidden", "").equals("true")) {
            getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        if (Display.getInstance().getProperty("KeepScreenOn", "").equals("true")) {
            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
        if (Display.getInstance().getProperty("DisableScreenshots", "").equals("true")) {
            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
        }
        if (m instanceof CodenameOneActivity) {
            ((CodenameOneActivity) m).setDefaultIntentResultListener(this);
            ((CodenameOneActivity) m).setIntentResultListener(this);
        }
        /**
         * translate our default font height depending on the screen density.
         * this is required for new high resolution devices. otherwise
         * everything looks awfully small.
         *
         * we use our default font height value of 16 and go from there. i
         * thought about using new Paint().getTextSize() for this value but if
         * some new version of android suddenly returns values already tranlated
         * to the screen then we might end up with too large fonts. the
         * documentation is not very precise on that.
         */
        final int defaultFontPixelHeight = 16;
        this.defaultFontHeight = this.translatePixelForDPI(defaultFontPixelHeight);
        this.defaultFont = (CodenameOneTextPaint) ((NativeFont) this.createFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM)).font;
        Display.getInstance().setTransitionYield(-1);
        initSurface();
        /**
         * devices are extremely sensitive so dragging should start a little
         * later than suggested by default implementation.
         */
        this.setDragStartPercentage(1);
        VirtualKeyboardInterface vkb = new AndroidKeyboard(this);
        Display.getInstance().registerVirtualKeyboard(vkb);
        Display.getInstance().setDefaultVirtualKeyboard(vkb);
        InPlaceEditView.endEdit();
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        if (nativePeers.size() > 0) {
            for (int i = 0; i < nativePeers.size(); i++) {
                ((AndroidImplementation.AndroidPeer) nativePeers.elementAt(i)).init();
            }
        }
    } else {
        /**
         * translate our default font height depending on the screen density.
         * this is required for new high resolution devices. otherwise
         * everything looks awfully small.
         *
         * we use our default font height value of 16 and go from there. i
         * thought about using new Paint().getTextSize() for this value but if
         * some new version of android suddenly returns values already tranlated
         * to the screen then we might end up with too large fonts. the
         * documentation is not very precise on that.
         */
        final int defaultFontPixelHeight = 16;
        this.defaultFontHeight = this.translatePixelForDPI(defaultFontPixelHeight);
        this.defaultFont = (CodenameOneTextPaint) ((NativeFont) this.createFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM)).font;
    }
    HttpURLConnection.setFollowRedirects(false);
    CookieHandler.setDefault(null);
}
Also used : VirtualKeyboardInterface(com.codename1.impl.VirtualKeyboardInterface) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ParseException(java.text.ParseException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) Paint(android.graphics.Paint)

Aggregations

Intent (android.content.Intent)4 CodenameOneActivity (com.codename1.impl.android.CodenameOneActivity)4 IntentResultListener (com.codename1.impl.android.IntentResultListener)4 CallbackManager (com.facebook.CallbackManager)3 LoginManager (com.facebook.login.LoginManager)2 SendIntentException (android.content.IntentSender.SendIntentException)1 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)1 Paint (android.graphics.Paint)1 VirtualKeyboardInterface (com.codename1.impl.VirtualKeyboardInterface)1 FacebookException (com.facebook.FacebookException)1 AppInviteContent (com.facebook.share.model.AppInviteContent)1 AppInviteDialog (com.facebook.share.widget.AppInviteDialog)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 ParseException (java.text.ParseException)1