package com.algobase.activity; import android.app.Activity; import android.app.AlertDialog; import android.content.Intent; import android.content.DialogInterface; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.view.Window; import android.graphics.Bitmap; import android.webkit.WebView; import android.webkit.WebSettings; import android.webkit.WebViewClient; import android.webkit.CookieManager; import android.webkit.ValueCallback; import android.widget.ImageView; import android.widget.Toast; import com.algobase.share.dialog.*; import com.algobase.stracks.sTracksActivity; import com.algobase.stracks.R; public class StravaWebView extends Activity { public final static String EXTRA_ACCESS_CODE = "extra_access_code"; public final static String EXTRA_REFRESH_CODE = "extra_refresh_code"; public final static String EXTRA_EXPIRES_AT = "extra_expires_at"; final String url = "https://www.strava.com/oauth/authorize" + "?client_id=" + sTracksActivity.STRAVA_CLIENT_ID + "&response_type=code" + "&redirect_uri=" + sTracksActivity.STRAVA_REDIRECT_URI + "&scope=activity:write,activity:read" + "&state=mystate" + "&approval_prompt=auto"; WebView webview; Handler handler; void show_toast(final String toast) { final Activity act = this; handler.post(new Runnable() { public void run() { Toast.makeText(act, toast, Toast.LENGTH_SHORT).show(); } }); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //requestWindowFeature(Window.FEATURE_NO_TITLE); setTheme(android.R.style.Theme_NoTitleBar); handler = new Handler(); webview = new WebView(this); setContentView(webview); webview.setWebViewClient(new MyWebViewClient(this)); WebSettings web_settings = webview.getSettings(); /* String user_agent = web_settings.getUserAgentString(); user_agent += String.format(" [%s/%s]","App Android", Build.VERSION.RELEASE); show_toast(user_agent); web_settings.setUserAgentString(user_agent); */ web_settings.setLoadWithOverviewMode(true); web_settings.setUseWideViewPort(true); //web_settings.setJavaScriptEnabled(true); webview.setVerticalScrollBarEnabled(false); webview.setHorizontalScrollBarEnabled(false); //webview.setInitialScale(initial_scale); CookieManager.getInstance().removeAllCookie(); //CookieManager.getInstance().setAcceptCookie(false); webview.loadUrl(url); } @Override public void onBackPressed() { super.onBackPressed(); finish(); } private class MyWebViewClient extends WebViewClient { Activity activity; MyWebViewClient(Activity act) { activity = act; } @Override public void onPageStarted(WebView view, String url, Bitmap bmp) {} @Override public void onPageFinished(WebView view, String url) {} @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.startsWith(sTracksActivity.STRAVA_REDIRECT_URI)) { /* MyDialog diag = new MyDialog(activity,"Strava"); diag.setMessage(url); diag.setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface d, int which) { activity.finish(); } }); diag.show(); */ int p = url.indexOf("code="); if (p > 0) { String code = url.substring(p+5); Intent extra = new Intent(); extra.putExtra(EXTRA_ACCESS_CODE,code); activity.setResult(RESULT_OK, extra); activity.finish(); } return true; } return false; } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { show_toast("Error: " + description); } } }