mirror of https://github.com/jitsi/jitsi-meet
parent
b7b43e8d9c
commit
38517127c3
@ -0,0 +1,181 @@ |
||||
package org.jitsi.meet.sdk.dropbox; |
||||
|
||||
import android.app.Activity; |
||||
import android.content.Context; |
||||
import android.content.pm.ApplicationInfo; |
||||
import android.content.pm.PackageInfo; |
||||
import android.content.pm.PackageManager; |
||||
import android.text.TextUtils; |
||||
import android.util.Log; |
||||
|
||||
import com.dropbox.core.DbxException; |
||||
import com.dropbox.core.DbxRequestConfig; |
||||
import com.dropbox.core.v2.DbxClientV2; |
||||
import com.dropbox.core.v2.users.FullAccount; |
||||
import com.dropbox.core.v2.users.SpaceAllocation; |
||||
import com.dropbox.core.v2.users.SpaceUsage; |
||||
import com.facebook.react.bridge.Arguments; |
||||
import com.facebook.react.bridge.LifecycleEventListener; |
||||
import com.facebook.react.bridge.Promise; |
||||
import com.facebook.react.bridge.ReactApplicationContext; |
||||
import com.facebook.react.bridge.ReactContextBaseJavaModule; |
||||
import com.dropbox.core.android.Auth; |
||||
import com.facebook.react.bridge.ReactMethod; |
||||
import com.facebook.react.bridge.WritableMap; |
||||
import org.jitsi.meet.sdk.R; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* Implements the react-native module for the dropbox integration. |
||||
*/ |
||||
public class Dropbox extends ReactContextBaseJavaModule implements LifecycleEventListener { |
||||
|
||||
private Promise promise = null; |
||||
private String clientId; |
||||
private String appID; |
||||
private boolean isEnabled = false; |
||||
|
||||
public Dropbox(ReactApplicationContext reactContext) { |
||||
super(reactContext); |
||||
reactContext.addLifecycleEventListener(this); |
||||
clientId = generateClientId(); |
||||
appID = reactContext.getString(R.string.dropbox_app_key); |
||||
if (!TextUtils.isEmpty(appID)) { |
||||
isEnabled = true; |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public String getName() { |
||||
return "Dropbox"; |
||||
} |
||||
|
||||
/** |
||||
* Executes the dropbox auth flow. |
||||
* |
||||
* @param promise The promise used to return the result of the auth flow. |
||||
*/ |
||||
@ReactMethod |
||||
public void authorize(final Promise promise) { |
||||
if (!isEnabled) { |
||||
promise.reject(new Exception("Dropbox integration isn't configured.")); |
||||
return; |
||||
} |
||||
Auth.startOAuth2Authentication(this.getCurrentActivity(), appID); |
||||
this.promise = promise; |
||||
} |
||||
|
||||
@Override |
||||
public Map<String, Object> getConstants() { |
||||
final Map<String, Object> constants = new HashMap<>(); |
||||
constants.put("ENABLED", isEnabled); |
||||
return constants; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Resolves the current user dropbox display name. |
||||
* |
||||
* @param token A dropbox access token. |
||||
* @param promise The promise used to return the result of the auth flow. |
||||
*/ |
||||
@ReactMethod |
||||
public void getDisplayName(final String token, final Promise promise) { |
||||
DbxRequestConfig config |
||||
= DbxRequestConfig.newBuilder(clientId).build(); |
||||
DbxClientV2 client = new DbxClientV2(config, token); |
||||
// Get current account info
|
||||
try { |
||||
FullAccount account = client.users().getCurrentAccount(); |
||||
promise.resolve(account.getName().getDisplayName()); |
||||
} catch (DbxException e) { |
||||
promise.reject(e); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Resolves the current user space usage. |
||||
* |
||||
* @param token A dropbox access token. |
||||
* @param promise The promise used to return the result of the auth flow. |
||||
*/ |
||||
@ReactMethod |
||||
public void getSpaceUsage(final String token, final Promise promise) { |
||||
DbxRequestConfig config |
||||
= DbxRequestConfig.newBuilder(clientId).build(); |
||||
DbxClientV2 client = new DbxClientV2(config, token); |
||||
try { |
||||
SpaceUsage spaceUsage = client.users().getSpaceUsage(); |
||||
WritableMap map = Arguments.createMap(); |
||||
map.putString("used", String.valueOf(spaceUsage.getUsed())); |
||||
SpaceAllocation allocation = spaceUsage.getAllocation(); |
||||
long allocated = 0; |
||||
if(allocation.isIndividual()) { |
||||
allocated += allocation.getIndividualValue().getAllocated(); |
||||
} |
||||
|
||||
if(allocation.isTeam()) { |
||||
allocated += allocation.getTeamValue().getAllocated(); |
||||
} |
||||
map.putString("allocated", String.valueOf(allocated)); |
||||
promise.resolve(map); |
||||
} catch (DbxException e) { |
||||
promise.reject(e); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Generate a client identifier for the dropbox sdk. |
||||
* |
||||
* @returns a client identifier for the dropbox sdk. |
||||
* @see {https://dropbox.github.io/dropbox-sdk-java/api-docs/v3.0.x/com/dropbox/core/DbxRequestConfig.html#getClientIdentifier--}
|
||||
*/ |
||||
private String generateClientId() { |
||||
Context context = getReactApplicationContext(); |
||||
PackageManager packageManager = context.getPackageManager(); |
||||
ApplicationInfo applicationInfo = null; |
||||
PackageInfo packageInfo = null; |
||||
|
||||
try { |
||||
String packageName = context.getPackageName(); |
||||
|
||||
applicationInfo |
||||
= packageManager.getApplicationInfo(packageName, 0); |
||||
packageInfo = packageManager.getPackageInfo(packageName, 0); |
||||
} catch (PackageManager.NameNotFoundException e) { |
||||
} |
||||
|
||||
String applicationLabel |
||||
= applicationInfo == null |
||||
? "JitsiMeet" |
||||
: packageManager.getApplicationLabel(applicationInfo) |
||||
.toString().replaceAll("\\s", ""); |
||||
String version = packageInfo == null ? "dev" : packageInfo.versionName; |
||||
|
||||
return applicationLabel + "/" + version; |
||||
} |
||||
|
||||
@Override |
||||
public void onHostResume() { |
||||
final String token = Auth.getOAuth2Token(); |
||||
if (token == null) |
||||
return; |
||||
|
||||
if (this.promise != null) { |
||||
this.promise.resolve(token); |
||||
this.promise = null; |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void onHostPause() { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void onHostDestroy() { |
||||
|
||||
} |
||||
} |
@ -1,3 +1,4 @@ |
||||
<resources> |
||||
<string name="app_name">Jitsi Meet SDK</string> |
||||
<string name="dropbox_app_key"></string> |
||||
</resources> |
||||
|
@ -0,0 +1,27 @@ |
||||
/*
|
||||
* Copyright @ 2018-present Atlassian Pty Ltd |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
#import <React/RCTBridge.h> |
||||
|
||||
@interface Dropbox : NSObject<RCTBridgeModule> |
||||
|
||||
+ (BOOL)application:(UIApplication *)app |
||||
openURL:(NSURL *)url |
||||
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options; |
||||
|
||||
+ (void)setAppKey; |
||||
|
||||
@end |
@ -0,0 +1,163 @@ |
||||
/* |
||||
* Copyright @ 2017-present Atlassian Pty Ltd |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0 |
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
#import <React/RCTBridgeModule.h> |
||||
#import <ObjectiveDropboxOfficial/ObjectiveDropboxOfficial.h> |
||||
#import "Dropbox.h" |
||||
|
||||
RCTPromiseResolveBlock currentResolve = nil; |
||||
RCTPromiseRejectBlock currentReject = nil; |
||||
|
||||
@implementation Dropbox |
||||
|
||||
+ (NSString *)getAppKey{ |
||||
NSArray *urlTypes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleURLTypes"]; |
||||
for(NSDictionary<NSString *, NSArray *> *urlType in urlTypes) { |
||||
NSArray *urlSchemes = urlType[@"CFBundleURLSchemes"]; |
||||
if(urlSchemes != nil) { |
||||
for(NSString *urlScheme in urlSchemes) { |
||||
if(urlScheme != nil) { |
||||
if ([urlScheme hasPrefix:@"db-"]) { |
||||
return [urlScheme substringFromIndex:3]; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
||||
return nil; |
||||
} |
||||
|
||||
RCT_EXPORT_MODULE(); |
||||
|
||||
- (NSDictionary *)constantsToExport { |
||||
BOOL enabled = [Dropbox getAppKey] != nil; |
||||
|
||||
return @{ |
||||
@"ENABLED": [NSNumber numberWithBool:enabled] |
||||
}; |
||||
}; |
||||
|
||||
RCT_EXPORT_METHOD(authorize: (RCTPromiseResolveBlock)resolve |
||||
reject:(__unused RCTPromiseRejectBlock)reject) { |
||||
currentResolve = resolve; |
||||
currentReject = reject; |
||||
|
||||
dispatch_async(dispatch_get_main_queue(), ^{ |
||||
[DBClientsManager authorizeFromController:[UIApplication sharedApplication] |
||||
controller:[[self class] topMostController] |
||||
openURL:^(NSURL *url) { |
||||
[[UIApplication sharedApplication] openURL:url]; |
||||
}]; |
||||
}); |
||||
} |
||||
|
||||
RCT_EXPORT_METHOD(getDisplayName: (NSString *)token |
||||
resolve: (RCTPromiseResolveBlock)resolve |
||||
reject:(RCTPromiseRejectBlock)reject) { |
||||
DBUserClient *client = [[DBUserClient alloc] initWithAccessToken:token]; |
||||
[[client.usersRoutes getCurrentAccount] setResponseBlock:^(DBUSERSFullAccount *result, DBNilObject *routeError, DBRequestError *networkError) { |
||||
if (result) { |
||||
resolve(result.name.displayName); |
||||
} else { |
||||
NSString *msg = @"Failed!"; |
||||
if (networkError != nil) { |
||||
msg = [NSString stringWithFormat:@"Failed! Error: %@", networkError]; |
||||
} |
||||
reject(@"getDisplayName", @"Failed", nil); |
||||
} |
||||
}]; |
||||
|
||||
} |
||||
|
||||
RCT_EXPORT_METHOD(getSpaceUsage: (NSString *)token |
||||
resolve: (RCTPromiseResolveBlock)resolve |
||||
reject:(RCTPromiseRejectBlock)reject) { |
||||
DBUserClient *client = [[DBUserClient alloc] initWithAccessToken:token]; |
||||
[[client.usersRoutes getSpaceUsage] setResponseBlock:^(DBUSERSSpaceUsage *result, DBNilObject *routeError, DBRequestError *networkError) { |
||||
if (result) { |
||||
DBUSERSSpaceAllocation *allocation = result.allocation; |
||||
NSNumber *allocated = 0; |
||||
NSNumber *used = 0; |
||||
if([allocation isIndividual]) { |
||||
allocated = allocation.individual.allocated; |
||||
used = result.used; |
||||
} else if ([allocation isTeam]) { |
||||
allocated = allocation.team.allocated; |
||||
used = allocation.team.used; |
||||
} |
||||
id objects[] = { used, allocated }; |
||||
id keys[] = { @"used", @"allocated" }; |
||||
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects |
||||
forKeys:keys |
||||
count:2]; |
||||
resolve(dictionary); |
||||
} else { |
||||
NSString *msg = @"Failed!"; |
||||
if (networkError != nil) { |
||||
msg = [NSString stringWithFormat:@"Failed! Error: %@", networkError]; |
||||
} |
||||
reject(@"getSpaceUsage", msg, nil); |
||||
} |
||||
}]; |
||||
|
||||
} |
||||
|
||||
+ (BOOL)application:(UIApplication *)app openURL:(NSURL *)url |
||||
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options { |
||||
DBOAuthResult *authResult = [DBClientsManager handleRedirectURL:url]; |
||||
if (authResult != nil) { |
||||
if ([authResult isSuccess]) { |
||||
currentResolve(authResult.accessToken.accessToken); |
||||
currentResolve = nil; |
||||
currentReject = nil; |
||||
return YES; |
||||
} else { |
||||
NSString *msg; |
||||
if ([authResult isError]) { |
||||
msg = [NSString stringWithFormat:@"%@, error type: %ld",[authResult errorDescription], [authResult errorType]]; |
||||
} else { |
||||
msg = @"OAuth canceled!"; |
||||
} |
||||
currentReject(@"authorize", msg, nil); |
||||
currentResolve = nil; |
||||
currentReject = nil; |
||||
} |
||||
} |
||||
return NO; |
||||
} |
||||
|
||||
+ (UIViewController*)topMostController |
||||
{ |
||||
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController; |
||||
|
||||
while (topController.presentedViewController) { |
||||
topController = topController.presentedViewController; |
||||
} |
||||
|
||||
return topController; |
||||
} |
||||
|
||||
+ (void)setAppKey { |
||||
NSString *appKey = [self getAppKey]; |
||||
if (appKey != nil) { |
||||
[DBClientsManager setupWithAppKey:appKey]; |
||||
} |
||||
} |
||||
|
||||
|
||||
@end |
Loading…
Reference in new issue