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);
}
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);
}
}
}
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);
}
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()));
}
});
}
}
}
}
}
});
}
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);
}
}
Aggregations