Skip to content
This repository was archived by the owner on Oct 7, 2024. It is now read-only.

Conversation

@langsmith
Copy link
Contributor

PR adds oAuth2 flow for non-mandatory account login/creation before viewing examples.

@langsmith
Copy link
Contributor Author

@mapbox/android , would love 👀 , review, and feedback.

All networking that isn't browser based in Chrome Custom Tabs, is happening in the background service named AccountRetrievalService

Small stuff still remains like adding checks (and alert dialogs) for internet connectivity. Wanted to get the ball rolling on this though...

@langsmith langsmith requested review from Guardiola31337 and tobrun and removed request for Guardiola31337 and tobrun May 12, 2017 18:58
@zugaldia zugaldia requested a review from Guardiola31337 May 12, 2017 19:04
Copy link
Contributor

@Guardiola31337 Guardiola31337 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build is broken 😖
I pointed out some details to discuss before landing.
BTW well done 👍

compile 'com.github.javiersantos:MaterialStyledDialogs:2.1'
compile 'com.getbase:floatingactionbutton:1.10.1'
compile 'com.squareup.picasso:picasso:2.5.2'
// Firebase crash report
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

compile 'com.squareup.retrofit2:retrofit:2.2.0'
compile 'com.squareup.retrofit2:converter-gson:2.2.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:customtabs:25.3.1'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not using ${supportLibVersion} here?
Same thing happens with other dependencies.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

compile 'com.google.code.gson:gson:2.8'
compile 'com.squareup.retrofit2:retrofit:2.2.0'
compile 'com.squareup.retrofit2:converter-gson:2.2.0'
// Other dependencies
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some Google dependencies intermingled. What about rearranging them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

private String username;

public AccountRetrievalService() {
super("AccountRetrievalService");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to increase readability, what about replacing magic numbers with constants with a particular meaning?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you talking about these numbers that are passed through to the service via the intent?

If so, they're needed as is because they're part of the oAuth flow. Specifically given to me by our API team.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when I say "numbers", I actually meant Strings. You get my point...

* Service for making call to Mapbox API for account information
*/

public interface MapboxAccountRetrofitService {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Access can be package-private.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. It's now:

interface MapboxAccountRetrofitService {

@@ -0,0 +1,105 @@
package com.mapbox.mapboxandroiddemo.model.usermodel;

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thanks. Switched it to:

/**
 * TODO: Add a class header comment!
 */

@@ -0,0 +1,47 @@
package com.mapbox.mapboxandroiddemo.model.usermodel;

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


public class Plan {
private String satellite;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why blank line between all variables?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used http://www.jsonschema2pojo.org/ and that's how they deliver the classes. I've removed the blank lines in all of the usermodel classes.


public class UserResponse {

@SerializedName("authorizations")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why @SerializedName? It's normally used for customizing field names (here the names are equal).
Further information on @SerializedName.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeaaa, no reason to have them. I thought I might have to use it but it turned out I didn't. Forgot to change. Removed

@langsmith
Copy link
Contributor Author

@Guardiola31337 , all requested changes have been addressed. Except for the okhttp3/retrofit question.

Copy link
Contributor

@Guardiola31337 Guardiola31337 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good @langsmith! Added some remarks.


// Mapbox Android UI
// Mapbox Android UI

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit new lines and reformat code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

<uses-feature android:name="android.hardware.location.gps"/>

<application
android:name=".MapboxApplication"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should clean/remove all unnecessary stuff from this manifest and take advantage of Manifest Merger to add/merge what differs from the root manifest (MapboxAndroidDemo/src/main/AndroidManifest.xml).

<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<action android:name="android.intent.action.VIEW"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't <intent-filter> needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, not really sure why it's being displayed that way on Github. On this branch, studio shows:

  <activity
            android:name=".account.LandingActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data
                    android:host="authorize"
                    android:scheme="mapbox-android-dev-preview"/>
            </intent-filter>
        </activity>

This is what we can use moving forward

Copy link
Contributor

@Guardiola31337 Guardiola31337 May 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@langsmith I'm still seeing previous code without the <intent-filter> 😞
Remember that there are 2 manifests MapboxAndroidDemo/src/gpservices/AndroidManifest.xml and MapboxAndroidDemo/src/main/AndroidManifest.xml. It seems that only the first one was updated.

Copy link
Contributor Author

@langsmith langsmith May 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. Ok, both manifest files now have:

        <activity
            android:name=".account.LandingActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>

                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>

                <data
                    android:host="authorize"
                    android:scheme="mapbox-android-dev-preview"/>
            </intent-filter>
        </activity>

private String username;

public AccountRetrievalService() {
super("AccountRetrievalService");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you talking about these numbers that are passed through to the service via the intent?

Literals in general. Further information: Replace Magic Number with Symbolic Constant.


JSONObject data = null;
try {
data = new JSONObject(json);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Referring to Retrofit's issue in branch ls-switching-last-okhttp3-to-retrofit-for-login.
As you can see in the logcat:

05-22 12:45:17.376 11938-11938/com.mapbox.mapboxandroiddemo.debug D/RetrievalService: onResponse: response received
05-22 12:45:17.376 11938-11938/com.mapbox.mapboxandroiddemo.debug D/RetrievalService: onResponse: json = Response{protocol=http/1.1, code=404, message=Not Found, url=https://api.mapbox.com/oauth/access_token/grant_type=authorization_code&client_id=%7BclientId%7D&client_secret=%7BclientSecret%7D&redirect_uri=%7BredirectUri%7D&code=%7Bcode%7D}
05-22 12:45:17.376 11938-11938/com.mapbox.mapboxandroiddemo.debug D/RetrievalService: onResponse: jsonString = retrofit2.OkHttpCall$NoContentResponseBody@79f9c5f
05-22 12:45:17.376 11938-11938/com.mapbox.mapboxandroiddemo.debug W/System.err: org.json.JSONException: Value Response of type java.lang.String cannot be converted to JSONObject
05-22 12:45:17.376 11938-11938/com.mapbox.mapboxandroiddemo.debug W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
05-22 12:45:17.376 11938-11938/com.mapbox.mapboxandroiddemo.debug W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:160)
05-22 12:45:17.376 11938-11938/com.mapbox.mapboxandroiddemo.debug W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:173)
05-22 12:45:17.376 11938-11938/com.mapbox.mapboxandroiddemo.debug W/System.err:     at com.mapbox.mapboxandroiddemo.account.AccountRetrievalService$1.onResponse(AccountRetrievalService.java:78)
05-22 12:45:17.376 11938-11938/com.mapbox.mapboxandroiddemo.debug W/System.err:     at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
05-22 12:45:17.376 11938-11938/com.mapbox.mapboxandroiddemo.debug W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
05-22 12:45:17.377 11938-11938/com.mapbox.mapboxandroiddemo.debug W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
05-22 12:45:17.377 11938-11938/com.mapbox.mapboxandroiddemo.debug W/System.err:     at android.os.Looper.loop(Looper.java:148)
05-22 12:45:17.377 11938-11938/com.mapbox.mapboxandroiddemo.debug W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
05-22 12:45:17.377 11938-11938/com.mapbox.mapboxandroiddemo.debug W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
05-22 12:45:17.377 11938-11938/com.mapbox.mapboxandroiddemo.debug W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
05-22 12:45:17.377 11938-11938/com.mapbox.mapboxandroiddemo.debug W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Retrofit was able to communicate with the given server, but the server could not find what was requested (code=404, message=Not Found) and data can't be created (see org.json.JSONException: Value Response of type java.lang.String cannot be converted to JSONObject).
So it seems that isn't a problem with Retrofit but with how the request is created/configured.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I knew that my configuration was off. I guess the question is/was whether it's worth spending the time to fiddle with the configuration until it works or just leave the okhttp3 as is. I think I know your answer 😉


String json = response.body().string();

JSONObject data = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable data initializer null is redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed to:

try {
          JSONObject data = new JSONObject(json);

setUpSkipDialog();
}

private void setUpButtons() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rearrange methods order (public, protected, private, etc.).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

}

private void openChromeCustomTab(boolean creatingAccount) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit new lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


if (getIntent() != null && getIntent().getAction().equals(Intent.ACTION_VIEW)) {
Uri uri = getIntent().getData();
if (uri.getQueryParameter("error") != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not extracting uri.getQueryParameter("error") into a variable String error and being able to reuse it when logging (no need to query again)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I believe I've fixed with:

if (uri.getQueryParameter("error") != null) {
        Toast.makeText(this, R.string.whoops_error_message_on_app_return, Toast.LENGTH_SHORT).show();
        String error = uri.getQueryParameter("error");
        Log.d("LandingActivity", "onResume: error = " + error);

Copy link
Contributor

@Guardiola31337 Guardiola31337 May 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@langsmith
Sorry, I meant replacing all 2 occurrences of uri.getQueryParameter("error"), define a new variable to store the value before the if check so it'll be available to reuse it inside and remove unnecessary String error inside the if.
Basically, move String error definition out of the if and reuse it as much as possible:

String error = uri.getQueryParameter("error");
if (error != null) {
    Toast.makeText(this, R.string.whoops_error_message_on_app_return, Toast.LENGTH_SHORT).show();
    Log.d("LandingActivity", "onResume: error = " + error);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@@ -0,0 +1,44 @@
package com.mapbox.mapboxandroiddemo.model.usermodel;

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove file headers.

Done. Thanks. Switched it to:

/**
 * TODO: Add a class header comment!
 */

You can leave it blank if you want.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, fixed. Template is blank.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@langsmith
👀 There are still classes with file headers: Mapviews, Plan, Services, UserDirections and UserResponse.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed from those classes

@Guardiola31337
Copy link
Contributor

One more thing @langsmith rebase/fix up the commits to represent what would be merged into master for easier review 😉

@zugaldia
Copy link
Member

Thank you @langsmith @Guardiola31337, this is looking great. In order to unblock #336 I suggest that, if the OkHttp/Retrofit conversation is the one thing left here, that we move that work to a separate PR that allows us to merge this one. Happy to help reviewing that separate PR.

@langsmith
Copy link
Contributor Author

Ok @zugaldia .
How about we move the following two remaining "issues" to separate future PRs so that this one can land:

I believe I've fixed all of the issues that @Guardiola31337 has mentioned in this PR and in our Slack DMs, besides the two above.

@@ -0,0 +1,93 @@
package com.mapbox.mapboxandroiddemo.model.usermodel;

public class Authorizations {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@langsmith
Access can be package-private.
👀 the same thing happens in other classes inside usermodel package.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@langsmith
Copy link
Contributor Author

Made adjustments for your latest round of review @Guardiola31337

This was referenced May 30, 2017
@langsmith
Copy link
Contributor Author

Next steps? @zugaldia , I've made separate tickets in this repo for the okHttp/Retrofit switch and for cleaning up the two manifest files, so that we don't forget about them.

I've made changes for all of @Guardiola31337 's requests besides rebasing/fixing up the commits to represent what would be merged into master for easier review.

Copy link
Contributor

@Guardiola31337 Guardiola31337 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done @langsmith :shipit:

@langsmith langsmith merged commit bff835f into master Jun 2, 2017
@Guardiola31337 Guardiola31337 deleted the ls-login/create branch June 4, 2017 15:18
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants