Skip to content

Commit

Permalink
Fix various Lint issues
Browse files Browse the repository at this point in the history
- Fix potential memory leak in `Stripe` `AsyncTask`s by making
  them static inner classes and using `WeakReference` for
  references to `Activity`
- Use `ConfigurationCompat` to get current locale
- Use `Locale.ROOT` when calling `String#toUpperCase()`,
  `String#toLowerCase()`, or `String#format` for constants or
  non-user visibile strings
- Create a valid `@IdRes` for `CardInputWidget.DEFAULT_READER_ID`
- Add missing constants to `@StringDef` annotation in
  `StripeApiHandler` and `Source`
- Avoid scientific notation in in vector paths because it can
  lead to crashes on some devices
  • Loading branch information
mshafrir-stripe committed Dec 3, 2018
1 parent 53d107c commit e1c4ff5
Show file tree
Hide file tree
Showing 16 changed files with 260 additions and 212 deletions.
4 changes: 2 additions & 2 deletions stripe/res/drawable-zh/ic_unionpay_template_32.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ L20.2442012,7.41236686 L20.2301013,7.01287084 Z M26.58,19 L2.42,19
C1.78347383,19.0053265 1.17090452,18.7575644 0.717061656,18.311223
C0.263218789,17.8648817 0.00528228465,17.2565265 0,16.62 L0,2.38
C0.00528228465,1.74347346 0.263218789,1.13511831 0.717061656,0.688776978
C1.17090452,0.242435646 1.78347383,-0.00532648787 2.42,4.4408921e-16
L26.58,4.4408921e-16 C27.2165262,-0.00532648787 27.8290955,0.242435646
C1.17090452,0.242435646 1.78347383,-0.00532648787 2.42,0.00000000000000044408921
L26.58,0.00000000000000044408921 C27.2165262,-0.00532648787 27.8290955,0.242435646
28.2829383,0.688776978 C28.7367812,1.13511831 28.9947177,1.74347346 29,2.38
L29,16.63 C28.9835257,17.9515828 27.9016388,19.0111167 26.58,19 Z M16.25,11.8
L16.54,10.9 L17.51,10.9 L17.23,11.8 L16.25,11.8 Z M16.7,10.4 L16.7,10.39
Expand Down
4 changes: 2 additions & 2 deletions stripe/res/drawable/ic_jcb_template_32.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
android:pathData="M26.58,19 L2.42,19 C1.78347383,19.0053265 1.17090452,18.7575644
0.717061656,18.311223 C0.263218789,17.8648817 0.00528228465,17.2565265 0,16.62
L0,2.38 C0.00528228465,1.74347346 0.263218789,1.13511831 0.717061656,0.688776978
C1.17090452,0.242435646 1.78347383,-0.00532648787 2.42,4.4408921e-16
L26.58,4.4408921e-16 C27.2165262,-0.00532648787 27.8290955,0.242435646
C1.17090452,0.242435646 1.78347383,-0.00532648787 2.42,0.00000000000000044408921
L26.58,0.00000000000000044408921 C27.2165262,-0.00532648787 27.8290955,0.242435646
28.2829383,0.688776978 C28.7367812,1.13511831 28.9947177,1.74347346 29,2.38
L29,16.63 C28.9835257,17.9515828 27.9016388,19.0111167 26.58,19 Z" />
<path
Expand Down
2 changes: 1 addition & 1 deletion stripe/res/drawable/ic_unionpay_template_32.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
0.717061656,18.3113072 C0.263218789,17.8649658 0.00528228465,17.2566107
0,16.6200841 L0,2.38008414 C0.00528228465,1.7435576 0.263218789,1.13520245
0.717061656,0.688861118 C1.17090452,0.242519786 1.78347383,-0.00524234854
2.42,8.41393314e-05 L26.58,8.41393314e-05 C27.2165262,-0.00524234854
2.42,0.0000841393314 L26.58,0.0000841393314 C27.2165262,-0.00524234854
27.8290955,0.242519786 28.2829383,0.688861118 C28.7367812,1.13520245
28.9947177,1.7435576 29,2.38008414 L29,16.6300841 C28.9835257,17.951667
27.9016388,19.0112009 26.58,19.0000841 Z M6.33,7 L5.83,9.26 C5.72,9.69 5.64,10
Expand Down
5 changes: 5 additions & 0 deletions stripe/res/values/ids.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>

<resources>
<item type="id" name="default_reader_id" />
</resources>
210 changes: 126 additions & 84 deletions stripe/src/main/java/com/stripe/android/Stripe.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.stripe.android.model.StripePaymentSource;
import com.stripe.android.model.Token;

import java.lang.ref.WeakReference;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
Expand All @@ -36,90 +37,36 @@
*/
public class Stripe {

SourceCreator mSourceCreator = new SourceCreator() {
private SourceCreator mSourceCreator = new SourceCreator() {
@Override
public void create(
@NonNull final SourceParams sourceParams,
@NonNull final String publishableKey,
@Nullable final String stripeAccount,
@Nullable Executor executor,
@NonNull final SourceCallback sourceCallback) {
AsyncTask<Void, Void, ResponseWrapper> task =
new AsyncTask<Void, Void, ResponseWrapper>() {
@Override
protected ResponseWrapper doInBackground(Void... params) {
try {
Source source = StripeApiHandler.createSource(
null,
mContext,
sourceParams,
publishableKey,
stripeAccount,
null);
return new ResponseWrapper(source);
} catch (StripeException stripeException) {
return new ResponseWrapper(stripeException);
}
}

@Override
protected void onPostExecute(ResponseWrapper responseWrapper) {
if (responseWrapper.source != null) {
sourceCallback.onSuccess(responseWrapper.source);
} else if (responseWrapper.error != null) {
sourceCallback.onError(responseWrapper.error);
}
}
};

executeTask(executor, task);
executeTask(executor, new CreateSourceTask(mContext, sourceParams, publishableKey,
stripeAccount, sourceCallback));
}
};

@VisibleForTesting
TokenCreator mTokenCreator = new TokenCreator() {
@Override
public void create(
final Map<String, Object> tokenParams,
final String publishableKey,
final String stripeAccount,
final @NonNull @Token.TokenType String tokenType,
final Executor executor,
final TokenCallback callback) {
AsyncTask<Void, Void, ResponseWrapper> task =
new AsyncTask<Void, Void, ResponseWrapper>() {
@Override
protected ResponseWrapper doInBackground(Void... params) {
try {
RequestOptions requestOptions =
RequestOptions.builder(
publishableKey,
stripeAccount,
RequestOptions.TYPE_QUERY).build();
Token token = StripeApiHandler.createToken(
mContext,
tokenParams,
requestOptions,
tokenType,
mLoggingResponseListener);
return new ResponseWrapper(token);
} catch (StripeException e) {
return new ResponseWrapper(e);
}
}

@Override
protected void onPostExecute(ResponseWrapper result) {
tokenTaskPostExecution(result, callback);
}
};

executeTask(executor, task);
@NonNull final Map<String, Object> tokenParams,
@NonNull final String publishableKey,
@Nullable final String stripeAccount,
@NonNull @Token.TokenType final String tokenType,
@Nullable final Executor executor,
@NonNull final TokenCallback callback) {
executeTask(executor, new CreateTokenTask(mContext, tokenParams, publishableKey,
stripeAccount, tokenType, callback, mLoggingResponseListener));
}
};

private Context mContext;
private StripeApiHandler.LoggingResponseListener mLoggingResponseListener;
@NonNull private final Context mContext;
@Nullable private StripeApiHandler.LoggingResponseListener mLoggingResponseListener;
private String mDefaultPublishableKey;
private String mStripeAccount;

Expand Down Expand Up @@ -392,7 +339,6 @@ public void createToken(
* @throws AuthenticationException failure to properly authenticate yourself (check your key)
* @throws InvalidRequestException your request has invalid parameters
* @throws APIConnectionException failure to connect to Stripe's API
* @throws CardException the card cannot be charged for some reason
* @throws APIException any other type of problem (for instance, a temporary issue with
* Stripe's servers
*/
Expand All @@ -401,7 +347,6 @@ public Source createSourceSynchronous(@NonNull SourceParams params)
throws AuthenticationException,
InvalidRequestException,
APIConnectionException,
CardException,
APIException {
return createSourceSynchronous(params, null);
}
Expand Down Expand Up @@ -678,7 +623,7 @@ public void logEventSynchronous(
}
RequestOptions options = builder.build();

Map<String, Object> loggingMap = null;
final Map<String, Object> loggingMap;
if (paymentSource instanceof Token) {
Token token = (Token) paymentSource;
loggingMap = LoggingUtils.getTokenCreationParams(
Expand Down Expand Up @@ -719,7 +664,6 @@ public Source retrieveSourceSynchronous(
throws AuthenticationException,
InvalidRequestException,
APIConnectionException,
CardException,
APIException {
return retrieveSourceSynchronous(sourceId, clientSecret, null);
}
Expand Down Expand Up @@ -748,7 +692,6 @@ public Source retrieveSourceSynchronous(
throws AuthenticationException,
InvalidRequestException,
APIConnectionException,
CardException,
APIException {
String apiKey = publishableKey == null ? mDefaultPublishableKey : publishableKey;
if (apiKey == null) {
Expand Down Expand Up @@ -820,17 +763,6 @@ private void validateKey(@NonNull @Size(min = 1) String publishableKey) {
}
}

private void tokenTaskPostExecution(ResponseWrapper result, TokenCallback callback) {
if (result.token != null) {
callback.onSuccess(result.token);
} else if (result.error != null) {
callback.onError(result.error);
} else {
callback.onError(new RuntimeException("Somehow got neither a token response or an " +
"error response"));
}
}

private void executeTask(Executor executor, AsyncTask<Void, Void, ResponseWrapper> task) {
if (executor != null) {
task.executeOnExecutor(executor);
Expand All @@ -839,7 +771,7 @@ private void executeTask(Executor executor, AsyncTask<Void, Void, ResponseWrappe
}
}

private class ResponseWrapper {
private static class ResponseWrapper {
final Source source;
final Token token;
final Exception error;
Expand Down Expand Up @@ -881,4 +813,114 @@ void create(Map<String, Object> params,
Executor executor,
TokenCallback callback);
}

private static class CreateSourceTask extends AsyncTask<Void, Void, ResponseWrapper> {
@NonNull private final WeakReference<Context> mContextRef;
@NonNull private final SourceParams mSourceParams;
@NonNull private final String mPublishableKey;
@Nullable private final String mStripeAccount;
@NonNull private final WeakReference<SourceCallback> mSourceCallbackRef;

CreateSourceTask(@NonNull Context context,
@NonNull SourceParams sourceParams,
@NonNull String publishableKey,
@Nullable String stripeAccount,
@NonNull SourceCallback sourceCallback) {
mContextRef = new WeakReference<>(context);
mSourceParams = sourceParams;
mPublishableKey = publishableKey;
mStripeAccount = stripeAccount;
mSourceCallbackRef = new WeakReference<>(sourceCallback);
}

@Override
protected ResponseWrapper doInBackground(Void... params) {
try {
final Source source = StripeApiHandler.createSource(
null,
mContextRef.get(),
mSourceParams,
mPublishableKey,
mStripeAccount,
null);
return new ResponseWrapper(source);
} catch (StripeException stripeException) {
return new ResponseWrapper(stripeException);
}
}

@Override
protected void onPostExecute(ResponseWrapper responseWrapper) {
final SourceCallback sourceCallback = mSourceCallbackRef.get();
if (sourceCallback != null) {
if (responseWrapper.source != null) {
sourceCallback.onSuccess(responseWrapper.source);
} else if (responseWrapper.error != null) {
sourceCallback.onError(responseWrapper.error);
}
}
}
}

private static class CreateTokenTask extends AsyncTask<Void, Void, ResponseWrapper> {
@NonNull private final WeakReference<Context> mContextRef;
@NonNull private final Map<String, Object> mTokenParams;
@NonNull private final String mPublishableKey;
@Nullable private final String mStripeAccount;
@NonNull @Token.TokenType private final String mTokenType;
@NonNull private final WeakReference<TokenCallback> mCallbackRef;
@Nullable private final StripeApiHandler.LoggingResponseListener mLoggingResponseListener;

CreateTokenTask(@NonNull Context context,
@NonNull final Map<String, Object> tokenParams,
@NonNull final String publishableKey,
@Nullable final String stripeAccount,
@NonNull @Token.TokenType final String tokenType,
@Nullable final TokenCallback callback,
@Nullable final StripeApiHandler.LoggingResponseListener loggingResponseListener) {
mContextRef = new WeakReference<>(context);
mTokenParams = tokenParams;
mPublishableKey = publishableKey;
mStripeAccount = stripeAccount;
mTokenType = tokenType;
mLoggingResponseListener = loggingResponseListener;
mCallbackRef = new WeakReference<>(callback);
}

@Override
protected ResponseWrapper doInBackground(Void... params) {
try {
final RequestOptions requestOptions = RequestOptions.builder(mPublishableKey,
mStripeAccount, RequestOptions.TYPE_QUERY).build();
final Token token = StripeApiHandler.createToken(
mContextRef.get(),
mTokenParams,
requestOptions,
mTokenType,
mLoggingResponseListener);
return new ResponseWrapper(token);
} catch (StripeException e) {
return new ResponseWrapper(e);
}
}

@Override
protected void onPostExecute(@NonNull ResponseWrapper result) {
tokenTaskPostExecution(result);
}

private void tokenTaskPostExecution(@NonNull ResponseWrapper result) {
final TokenCallback callback = mCallbackRef.get();
if (callback != null) {
if (result.token != null) {
callback.onSuccess(result.token);
} else if (result.error != null) {
callback.onError(result.error);
} else {
callback.onError(new RuntimeException("Somehow got neither a token response or"
+ " an error response"));
}
}
}
}
}
Loading

0 comments on commit e1c4ff5

Please sign in to comment.