/*
*
* * Copyright 2015 Van Shu
* *
* * 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.
*
*/
package com.mobimvp.cliques.ui;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.android.volley.Request;
import com.android.volley.Response;
import com.mobimvp.cliques.R;
import com.mobimvp.cliques.model.AccessToken;
import com.mobimvp.cliques.service.DribbbleApi;
import com.mobimvp.cliques.service.GsonRequest;
import com.mobimvp.cliques.service.RequestManager;
import com.mobimvp.cliques.util.LogUtils;
import com.mobimvp.cliques.util.PrefUtils;
import static com.mobimvp.cliques.util.LogUtils.makeLogTag;
public class LoginActivity extends BaseActivity {
private static final String TAG = makeLogTag(LoginActivity.class);
boolean loadingFinished = true;
boolean redirect = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
WebView mWebView = (WebView) findViewById(R.id.webview);
LogUtils.LOGI(TAG, DribbbleApi.getOauthAuthorizeUrl());
mWebView.loadUrl(DribbbleApi.getOauthAuthorizeUrl());
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
loadingFinished = false;
Log.e(TAG, "onPageStarted: ");
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (!redirect) {
loadingFinished = true;
}
if (loadingFinished && !redirect) {
Log.e(TAG, "onPageFinished: " + url);
if (url.contains("?code=")) {
Uri uri = Uri.parse(url);
String authCode = uri.getQueryParameter("code");
RequestManager.addRequest(new GsonRequest<>(Request.Method.POST, DribbbleApi.getOauthTokenUrl(authCode),
AccessToken.class, null, responseListener, errorListener));
}
} else {
redirect = false;
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
view.loadUrl(url);
Log.e(TAG, "Loading web view...");
return true;
}
});
}
private Response.Listener<AccessToken> responseListener = new Response.Listener<AccessToken>() {
@Override
public void onResponse(AccessToken accessToken) {
LogUtils.LOGI(TAG, "get the accessToken:" + accessToken.accessToken);
PrefUtils.putString(LoginActivity.this, PrefUtils.ACCESS_TOKEN, accessToken.accessToken);
startActivity(new Intent(LoginActivity.this,HomeActivity.class));
finish();
}
};
@Override
protected void onResume() {
super.onResume();
}
}