Search in sources :

Example 1 with IntentResultListener

use of com.codename1.impl.android.IntentResultListener 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 IntentResultListener

use of com.codename1.impl.android.IntentResultListener 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 IntentResultListener

use of com.codename1.impl.android.IntentResultListener 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 IntentResultListener

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

the class GoogleImpl method nativeLoginImpl.

private void nativeLoginImpl(final GoogleApiClient client) {
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(client);
    AndroidNativeUtil.startActivityForResult(signInIntent, RC_SIGN_IN, new IntentResultListener() {

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
            if (requestCode == RC_SIGN_IN) {
                final GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
                if (result.isSuccess()) {
                    // Signed in successfully, show authenticated UI.
                    GoogleSignInAccount acct = result.getSignInAccount();
                    String displayName = acct.getDisplayName();
                    String acctId = acct.getId();
                    String email = acct.getEmail();
                    String requestIdToken = acct.getIdToken();
                    Set<Scope> grantedScopes = acct.getGrantedScopes();
                    String code = acct.getServerAuthCode();
                    String scopeStr = scope;
                    System.out.println("Token is " + acct.getIdToken());
                    if (acct.getIdToken() == null && clientId != null && clientSecret != null) {
                        Log.p("Received null ID token even though clientId and clientSecret are set.");
                    }
                    // otherwise we'll set the token to null.
                    if (clientId != null && clientSecret != null && requestIdToken != null && code != null) {
                        ConnectionRequest req = new ConnectionRequest() {

                            @Override
                            protected void readResponse(InputStream input) throws IOException {
                                Map<String, Object> json = new JSONParser().parseJSON(new InputStreamReader(input, "UTF-8"));
                                if (json.containsKey("access_token")) {
                                    setAccessToken(new AccessToken((String) json.get("access_token"), null));
                                    Display.getInstance().callSerially(new Runnable() {

                                        @Override
                                        public void run() {
                                            callback.loginSuccessful();
                                        }
                                    });
                                } else {
                                    setAccessToken(new AccessToken(null, null));
                                    Log.p("Failed to retrieve the access token from the google auth server.  Login succeeded, but access token is null, so you won't be able to use it to retrieve additional information.");
                                    Log.p("Response was " + json);
                                    Display.getInstance().callSerially(new Runnable() {

                                        @Override
                                        public void run() {
                                            callback.loginSuccessful();
                                        }
                                    });
                                }
                            }
                        };
                        req.setUrl("https://www.googleapis.com/oauth2/v4/token");
                        req.addArgument("grant_type", "authorization_code");
                        // req.addArgument("client_id", "555462747934-iujpd5saj4pjpibo7c6r9tbjfef22rh1.apps.googleusercontent.com");
                        req.addArgument("client_id", clientId);
                        // req.addArgument("client_secret", "650YqplrnAI0KXb9LMUnVNnx");
                        req.addArgument("client_secret", clientSecret);
                        req.addArgument("redirect_uri", "");
                        req.addArgument("code", code);
                        req.addArgument("id_token", requestIdToken);
                        req.setPost(true);
                        req.setReadResponseForErrors(true);
                        NetworkManager.getInstance().addToQueue(req);
                    } else {
                        setAccessToken(new AccessToken(null, null));
                        Log.p("The access token was set to null because one of clientId, clientSecret, requestIdToken, or auth were null");
                        Log.p("The login succeeded, but you won't be able to make any requests to Google's REST apis using the login token.");
                        Log.p("In order to obtain a token that can be used with Google's REST APIs, you need to set the clientId, and clientSecret of" + "the GoogleConnect instance to valid OAuth2.0 Client IDs for Web Clients.");
                        Log.p("See https://console.developers.google.com/apis/credentials");
                        Log.p("You can get the OAuth2.0 client ID for this project in your google-services.json file in the oauth_client section");
                        Display.getInstance().callSerially(new Runnable() {

                            @Override
                            public void run() {
                                callback.loginSuccessful();
                            }
                        });
                    }
                } else {
                    if (callback != null) {
                        if (callback != null) {
                            Display.getInstance().callSerially(new Runnable() {

                                @Override
                                public void run() {
                                    callback.loginFailed(GooglePlayServicesUtil.getErrorString(result.getStatus().getStatusCode()));
                                }
                            });
                        }
                    }
                }
            }
        }
    });
}
Also used : Set(java.util.Set) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Intent(android.content.Intent) IOException(java.io.IOException) GoogleSignInResult(com.google.android.gms.auth.api.signin.GoogleSignInResult) ConnectionRequest(com.codename1.io.ConnectionRequest) GoogleSignInAccount(com.google.android.gms.auth.api.signin.GoogleSignInAccount) IntentResultListener(com.codename1.impl.android.IntentResultListener) AccessToken(com.codename1.io.AccessToken) JSONParser(com.codename1.io.JSONParser) Map(java.util.Map)

Example 5 with IntentResultListener

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

the class AndroidImplementation method onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == ZOOZ_PAYMENT) {
        ((IntentResultListener) pur).onActivityResult(requestCode, resultCode, intent);
        return;
    }
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == CAPTURE_IMAGE) {
            try {
                String imageUri = (String) Storage.getInstance().readObject("imageUri");
                Vector pathandId = StringUtil.tokenizeString(imageUri, ";");
                String path = (String) pathandId.get(0);
                String lastId = (String) pathandId.get(1);
                Storage.getInstance().deleteStorageFile("imageUri");
                clearMediaDB(lastId, path);
                callback.fireActionEvent(new ActionEvent(path));
                return;
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestCode == CAPTURE_VIDEO) {
            String path = (String) Storage.getInstance().readObject("videoUri");
            Storage.getInstance().deleteStorageFile("videoUri");
            callback.fireActionEvent(new ActionEvent(path));
            return;
        } else if (requestCode == CAPTURE_AUDIO) {
            Uri data = intent.getData();
            String path = convertImageUriToFilePath(data, getContext());
            callback.fireActionEvent(new ActionEvent(path));
            return;
        } else if (requestCode == OPEN_GALLERY) {
            Uri selectedImage = intent.getData();
            String scheme = intent.getScheme();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContext().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            // this happens on Android devices, not exactly sure what the use case is
            if (cursor == null) {
                callback.fireActionEvent(null);
                return;
            }
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();
            if (filePath == null && "content".equals(scheme)) {
                // locally
                try {
                    InputStream inputStream = getContext().getContentResolver().openInputStream(selectedImage);
                    if (inputStream != null) {
                        String name = getContentName(getContext().getContentResolver(), selectedImage);
                        if (name != null) {
                            filePath = getAppHomePath() + getFileSystemSeparator() + name;
                            File f = new File(filePath);
                            OutputStream tmp = createFileOuputStream(f);
                            byte[] buffer = new byte[1024];
                            int read = -1;
                            while ((read = inputStream.read(buffer)) > -1) {
                                tmp.write(buffer, 0, read);
                            }
                            tmp.close();
                            inputStream.close();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            callback.fireActionEvent(new ActionEvent(filePath));
            return;
        } else {
            if (callback != null) {
                callback.fireActionEvent(new ActionEvent("ok"));
            }
            return;
        }
    }
    // clean imageUri
    String imageUri = (String) Storage.getInstance().readObject("imageUri");
    if (imageUri != null) {
        Storage.getInstance().deleteStorageFile("imageUri");
    }
    if (callback != null) {
        callback.fireActionEvent(null);
    }
}
Also used : ActionEvent(com.codename1.ui.events.ActionEvent) BufferedInputStream(com.codename1.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BufferedOutputStream(com.codename1.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) Cursor(android.database.Cursor) Vector(java.util.Vector) Uri(android.net.Uri) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ParseException(java.text.ParseException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException)

Aggregations

Intent (android.content.Intent)4 IntentResultListener (com.codename1.impl.android.IntentResultListener)4 CodenameOneActivity (com.codename1.impl.android.CodenameOneActivity)3 CallbackManager (com.facebook.CallbackManager)3 LoginManager (com.facebook.login.LoginManager)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)1 Cursor (android.database.Cursor)1 Uri (android.net.Uri)1 AccessToken (com.codename1.io.AccessToken)1 BufferedInputStream (com.codename1.io.BufferedInputStream)1 BufferedOutputStream (com.codename1.io.BufferedOutputStream)1 ConnectionRequest (com.codename1.io.ConnectionRequest)1 JSONParser (com.codename1.io.JSONParser)1 ActionEvent (com.codename1.ui.events.ActionEvent)1 FacebookException (com.facebook.FacebookException)1 AppInviteContent (com.facebook.share.model.AppInviteContent)1 AppInviteDialog (com.facebook.share.widget.AppInviteDialog)1 GoogleSignInAccount (com.google.android.gms.auth.api.signin.GoogleSignInAccount)1