ex
Fork of mbed-os-example-mbed5-blinky by
dcs-sdk-java-master/app/src/main/java/com/baidu/duer/dcs/oauth/api/BaiduDialog.java@45:2aa9f933c8d2, 2017-07-18 (annotated)
- Committer:
- TMBOY
- Date:
- Tue Jul 18 16:34:48 2017 +0800
- Revision:
- 45:2aa9f933c8d2
?
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| TMBOY | 45:2aa9f933c8d2 | 1 | /* |
| TMBOY | 45:2aa9f933c8d2 | 2 | * Copyright (c) 2017 Baidu, Inc. All Rights Reserved. |
| TMBOY | 45:2aa9f933c8d2 | 3 | * |
| TMBOY | 45:2aa9f933c8d2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| TMBOY | 45:2aa9f933c8d2 | 5 | * you may not use this file except in compliance with the License. |
| TMBOY | 45:2aa9f933c8d2 | 6 | * You may obtain a copy of the License at |
| TMBOY | 45:2aa9f933c8d2 | 7 | * |
| TMBOY | 45:2aa9f933c8d2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| TMBOY | 45:2aa9f933c8d2 | 9 | * |
| TMBOY | 45:2aa9f933c8d2 | 10 | * Unless required by applicable law or agreed to in writing, software |
| TMBOY | 45:2aa9f933c8d2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| TMBOY | 45:2aa9f933c8d2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| TMBOY | 45:2aa9f933c8d2 | 13 | * See the License for the specific language governing permissions and |
| TMBOY | 45:2aa9f933c8d2 | 14 | * limitations under the License. |
| TMBOY | 45:2aa9f933c8d2 | 15 | */ |
| TMBOY | 45:2aa9f933c8d2 | 16 | package com.baidu.duer.dcs.oauth.api; |
| TMBOY | 45:2aa9f933c8d2 | 17 | |
| TMBOY | 45:2aa9f933c8d2 | 18 | import android.app.Dialog; |
| TMBOY | 45:2aa9f933c8d2 | 19 | import android.app.ProgressDialog; |
| TMBOY | 45:2aa9f933c8d2 | 20 | import android.content.Context; |
| TMBOY | 45:2aa9f933c8d2 | 21 | import android.graphics.Bitmap; |
| TMBOY | 45:2aa9f933c8d2 | 22 | import android.graphics.Color; |
| TMBOY | 45:2aa9f933c8d2 | 23 | import android.os.Bundle; |
| TMBOY | 45:2aa9f933c8d2 | 24 | import android.view.KeyEvent; |
| TMBOY | 45:2aa9f933c8d2 | 25 | import android.view.View; |
| TMBOY | 45:2aa9f933c8d2 | 26 | import android.view.ViewGroup; |
| TMBOY | 45:2aa9f933c8d2 | 27 | import android.view.Window; |
| TMBOY | 45:2aa9f933c8d2 | 28 | import android.webkit.WebView; |
| TMBOY | 45:2aa9f933c8d2 | 29 | import android.webkit.WebViewClient; |
| TMBOY | 45:2aa9f933c8d2 | 30 | import android.widget.FrameLayout; |
| TMBOY | 45:2aa9f933c8d2 | 31 | import android.widget.RelativeLayout; |
| TMBOY | 45:2aa9f933c8d2 | 32 | |
| TMBOY | 45:2aa9f933c8d2 | 33 | import com.baidu.duer.dcs.androidsystemimpl.webview.BaseWebView; |
| TMBOY | 45:2aa9f933c8d2 | 34 | import com.baidu.duer.dcs.util.LogUtil; |
| TMBOY | 45:2aa9f933c8d2 | 35 | |
| TMBOY | 45:2aa9f933c8d2 | 36 | /** |
| TMBOY | 45:2aa9f933c8d2 | 37 | * 自定义的Dialog UI类,用来展示WebView界面,即用户登录和授权的页面 |
| TMBOY | 45:2aa9f933c8d2 | 38 | * <p> |
| TMBOY | 45:2aa9f933c8d2 | 39 | * Created by zhangyan42@baidu.com on 2017/5/24. |
| TMBOY | 45:2aa9f933c8d2 | 40 | */ |
| TMBOY | 45:2aa9f933c8d2 | 41 | public class BaiduDialog extends Dialog { |
| TMBOY | 45:2aa9f933c8d2 | 42 | private static final FrameLayout.LayoutParams MATCH = new FrameLayout.LayoutParams( |
| TMBOY | 45:2aa9f933c8d2 | 43 | ViewGroup.LayoutParams.MATCH_PARENT, |
| TMBOY | 45:2aa9f933c8d2 | 44 | ViewGroup.LayoutParams.MATCH_PARENT); |
| TMBOY | 45:2aa9f933c8d2 | 45 | private static final String LOG_TAG = "BaiduDialog"; |
| TMBOY | 45:2aa9f933c8d2 | 46 | private final String mUrl; |
| TMBOY | 45:2aa9f933c8d2 | 47 | private final BaiduDialogListener mListener; |
| TMBOY | 45:2aa9f933c8d2 | 48 | private ProgressDialog mSpinner; |
| TMBOY | 45:2aa9f933c8d2 | 49 | private BaseWebView mWebView; |
| TMBOY | 45:2aa9f933c8d2 | 50 | private FrameLayout mContent; |
| TMBOY | 45:2aa9f933c8d2 | 51 | private RelativeLayout webViewContainer; |
| TMBOY | 45:2aa9f933c8d2 | 52 | |
| TMBOY | 45:2aa9f933c8d2 | 53 | /** |
| TMBOY | 45:2aa9f933c8d2 | 54 | * 构造BaiduDialog |
| TMBOY | 45:2aa9f933c8d2 | 55 | * |
| TMBOY | 45:2aa9f933c8d2 | 56 | * @param context 展示Dialog UI的上下文环境,通常是XXActivity.this |
| TMBOY | 45:2aa9f933c8d2 | 57 | * @param url 用户请求的url地址 |
| TMBOY | 45:2aa9f933c8d2 | 58 | * @param listener 用于对请求回调的Listener对象 |
| TMBOY | 45:2aa9f933c8d2 | 59 | */ |
| TMBOY | 45:2aa9f933c8d2 | 60 | public BaiduDialog(Context context, String url, BaiduDialogListener listener) { |
| TMBOY | 45:2aa9f933c8d2 | 61 | super(context, android.R.style.Theme_Translucent_NoTitleBar); |
| TMBOY | 45:2aa9f933c8d2 | 62 | mUrl = url; |
| TMBOY | 45:2aa9f933c8d2 | 63 | mListener = listener; |
| TMBOY | 45:2aa9f933c8d2 | 64 | } |
| TMBOY | 45:2aa9f933c8d2 | 65 | |
| TMBOY | 45:2aa9f933c8d2 | 66 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 67 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| TMBOY | 45:2aa9f933c8d2 | 68 | mListener.onCancel(); |
| TMBOY | 45:2aa9f933c8d2 | 69 | return super.onKeyDown(keyCode, event); |
| TMBOY | 45:2aa9f933c8d2 | 70 | } |
| TMBOY | 45:2aa9f933c8d2 | 71 | |
| TMBOY | 45:2aa9f933c8d2 | 72 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 73 | protected void onCreate(Bundle savedInstanceState) { |
| TMBOY | 45:2aa9f933c8d2 | 74 | super.onCreate(savedInstanceState); |
| TMBOY | 45:2aa9f933c8d2 | 75 | // 设置ProgressDialog的样式 |
| TMBOY | 45:2aa9f933c8d2 | 76 | mSpinner = new ProgressDialog(getContext()); |
| TMBOY | 45:2aa9f933c8d2 | 77 | mSpinner.requestWindowFeature(Window.FEATURE_NO_TITLE); |
| TMBOY | 45:2aa9f933c8d2 | 78 | mSpinner.setMessage("登录中..."); |
| TMBOY | 45:2aa9f933c8d2 | 79 | requestWindowFeature(Window.FEATURE_NO_TITLE); |
| TMBOY | 45:2aa9f933c8d2 | 80 | mContent = new FrameLayout(getContext()); |
| TMBOY | 45:2aa9f933c8d2 | 81 | setUpWebView(); |
| TMBOY | 45:2aa9f933c8d2 | 82 | addContentView(mContent, MATCH); |
| TMBOY | 45:2aa9f933c8d2 | 83 | } |
| TMBOY | 45:2aa9f933c8d2 | 84 | |
| TMBOY | 45:2aa9f933c8d2 | 85 | private void setUpWebView() { |
| TMBOY | 45:2aa9f933c8d2 | 86 | webViewContainer = new RelativeLayout(getContext()); |
| TMBOY | 45:2aa9f933c8d2 | 87 | mWebView = new BaseWebView(getContext().getApplicationContext()); |
| TMBOY | 45:2aa9f933c8d2 | 88 | mWebView.setWebViewClient(new BdWebViewClient()); |
| TMBOY | 45:2aa9f933c8d2 | 89 | mWebView.loadUrl(mUrl); |
| TMBOY | 45:2aa9f933c8d2 | 90 | mWebView.setLayoutParams(MATCH); |
| TMBOY | 45:2aa9f933c8d2 | 91 | mWebView.setVisibility(View.INVISIBLE); |
| TMBOY | 45:2aa9f933c8d2 | 92 | webViewContainer.addView(mWebView); |
| TMBOY | 45:2aa9f933c8d2 | 93 | mContent.addView(webViewContainer, MATCH); |
| TMBOY | 45:2aa9f933c8d2 | 94 | } |
| TMBOY | 45:2aa9f933c8d2 | 95 | |
| TMBOY | 45:2aa9f933c8d2 | 96 | private class BdWebViewClient extends WebViewClient { |
| TMBOY | 45:2aa9f933c8d2 | 97 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 98 | public boolean shouldOverrideUrlLoading(WebView view, String url) { |
| TMBOY | 45:2aa9f933c8d2 | 99 | LogUtil.d(LOG_TAG, "Redirect URL: " + url); |
| TMBOY | 45:2aa9f933c8d2 | 100 | /* |
| TMBOY | 45:2aa9f933c8d2 | 101 | * 如果url的地址为bdconnect://success,即使用User-Agent方式获取用户授权的redirct |
| TMBOY | 45:2aa9f933c8d2 | 102 | * url,则截取url中返回的各种token参数, |
| TMBOY | 45:2aa9f933c8d2 | 103 | * 如果出错,则通过listener的相应处理方式回调 |
| TMBOY | 45:2aa9f933c8d2 | 104 | */ |
| TMBOY | 45:2aa9f933c8d2 | 105 | if (url.startsWith(BaiduOauthImplicitGrant.SUCCESS_URI)) { |
| TMBOY | 45:2aa9f933c8d2 | 106 | Bundle values = OauthNetUtil.parseUrl(url); |
| TMBOY | 45:2aa9f933c8d2 | 107 | if (values != null && !values.isEmpty()) { |
| TMBOY | 45:2aa9f933c8d2 | 108 | String error = values.getString("error"); |
| TMBOY | 45:2aa9f933c8d2 | 109 | // 用户取消授权返回error=access_denied |
| TMBOY | 45:2aa9f933c8d2 | 110 | if ("access_denied".equals(error)) { |
| TMBOY | 45:2aa9f933c8d2 | 111 | mListener.onCancel(); |
| TMBOY | 45:2aa9f933c8d2 | 112 | BaiduDialog.this.dismiss(); |
| TMBOY | 45:2aa9f933c8d2 | 113 | return true; |
| TMBOY | 45:2aa9f933c8d2 | 114 | } |
| TMBOY | 45:2aa9f933c8d2 | 115 | // 请求出错时返回error=1100&errorDesp=error_desp |
| TMBOY | 45:2aa9f933c8d2 | 116 | String errorDesp = values.getString("error_description"); |
| TMBOY | 45:2aa9f933c8d2 | 117 | if (error != null && errorDesp != null) { |
| TMBOY | 45:2aa9f933c8d2 | 118 | mListener.onBaiduException(new BaiduException(error, errorDesp)); |
| TMBOY | 45:2aa9f933c8d2 | 119 | BaiduDialog.this.dismiss(); |
| TMBOY | 45:2aa9f933c8d2 | 120 | return true; |
| TMBOY | 45:2aa9f933c8d2 | 121 | } |
| TMBOY | 45:2aa9f933c8d2 | 122 | mListener.onComplete(values); |
| TMBOY | 45:2aa9f933c8d2 | 123 | BaiduDialog.this.dismiss(); |
| TMBOY | 45:2aa9f933c8d2 | 124 | return true; |
| TMBOY | 45:2aa9f933c8d2 | 125 | } |
| TMBOY | 45:2aa9f933c8d2 | 126 | } else if (url.startsWith(BaiduOauthImplicitGrant.CANCEL_URI)) { |
| TMBOY | 45:2aa9f933c8d2 | 127 | mListener.onCancel(); |
| TMBOY | 45:2aa9f933c8d2 | 128 | BaiduDialog.this.dismiss(); |
| TMBOY | 45:2aa9f933c8d2 | 129 | return true; |
| TMBOY | 45:2aa9f933c8d2 | 130 | } |
| TMBOY | 45:2aa9f933c8d2 | 131 | return false; |
| TMBOY | 45:2aa9f933c8d2 | 132 | } |
| TMBOY | 45:2aa9f933c8d2 | 133 | |
| TMBOY | 45:2aa9f933c8d2 | 134 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 135 | public void onReceivedError(WebView view, int errorCode, String description, |
| TMBOY | 45:2aa9f933c8d2 | 136 | String failingUrl) { |
| TMBOY | 45:2aa9f933c8d2 | 137 | super.onReceivedError(view, errorCode, description, failingUrl); |
| TMBOY | 45:2aa9f933c8d2 | 138 | mListener.onError(new BaiduDialogError(description, errorCode, failingUrl)); |
| TMBOY | 45:2aa9f933c8d2 | 139 | BaiduDialog.this.dismiss(); |
| TMBOY | 45:2aa9f933c8d2 | 140 | } |
| TMBOY | 45:2aa9f933c8d2 | 141 | |
| TMBOY | 45:2aa9f933c8d2 | 142 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 143 | public void onPageStarted(WebView view, String url, Bitmap favicon) { |
| TMBOY | 45:2aa9f933c8d2 | 144 | LogUtil.d(LOG_TAG, "Webview loading URL: " + url); |
| TMBOY | 45:2aa9f933c8d2 | 145 | super.onPageStarted(view, url, favicon); |
| TMBOY | 45:2aa9f933c8d2 | 146 | mSpinner.show(); |
| TMBOY | 45:2aa9f933c8d2 | 147 | } |
| TMBOY | 45:2aa9f933c8d2 | 148 | |
| TMBOY | 45:2aa9f933c8d2 | 149 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 150 | public void onPageFinished(WebView view, String url) { |
| TMBOY | 45:2aa9f933c8d2 | 151 | super.onPageFinished(view, url); |
| TMBOY | 45:2aa9f933c8d2 | 152 | mSpinner.dismiss(); |
| TMBOY | 45:2aa9f933c8d2 | 153 | mContent.setBackgroundColor(Color.TRANSPARENT); |
| TMBOY | 45:2aa9f933c8d2 | 154 | mWebView.setVisibility(View.VISIBLE); |
| TMBOY | 45:2aa9f933c8d2 | 155 | } |
| TMBOY | 45:2aa9f933c8d2 | 156 | } |
| TMBOY | 45:2aa9f933c8d2 | 157 | |
| TMBOY | 45:2aa9f933c8d2 | 158 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 159 | public void dismiss() { |
| TMBOY | 45:2aa9f933c8d2 | 160 | super.dismiss(); |
| TMBOY | 45:2aa9f933c8d2 | 161 | webViewContainer.removeView(mWebView); |
| TMBOY | 45:2aa9f933c8d2 | 162 | mWebView.removeAllViews(); |
| TMBOY | 45:2aa9f933c8d2 | 163 | mWebView.destroy(); |
| TMBOY | 45:2aa9f933c8d2 | 164 | } |
| TMBOY | 45:2aa9f933c8d2 | 165 | |
| TMBOY | 45:2aa9f933c8d2 | 166 | /** |
| TMBOY | 45:2aa9f933c8d2 | 167 | * 用于BaiduDialog回调接口 |
| TMBOY | 45:2aa9f933c8d2 | 168 | */ |
| TMBOY | 45:2aa9f933c8d2 | 169 | public interface BaiduDialogListener { |
| TMBOY | 45:2aa9f933c8d2 | 170 | /** |
| TMBOY | 45:2aa9f933c8d2 | 171 | * BaiduDailog请求成功时,执行该法方法,实现逻辑包括存储value信息,跳转activity的过程 |
| TMBOY | 45:2aa9f933c8d2 | 172 | * |
| TMBOY | 45:2aa9f933c8d2 | 173 | * @param values token信息的key-value存储 |
| TMBOY | 45:2aa9f933c8d2 | 174 | */ |
| TMBOY | 45:2aa9f933c8d2 | 175 | void onComplete(Bundle values); |
| TMBOY | 45:2aa9f933c8d2 | 176 | |
| TMBOY | 45:2aa9f933c8d2 | 177 | /** |
| TMBOY | 45:2aa9f933c8d2 | 178 | * 当BaiduDialog执行发生BaiduException时执行的方法。 |
| TMBOY | 45:2aa9f933c8d2 | 179 | * |
| TMBOY | 45:2aa9f933c8d2 | 180 | * @param e BaiduException信息 |
| TMBOY | 45:2aa9f933c8d2 | 181 | */ |
| TMBOY | 45:2aa9f933c8d2 | 182 | void onBaiduException(BaiduException e); |
| TMBOY | 45:2aa9f933c8d2 | 183 | |
| TMBOY | 45:2aa9f933c8d2 | 184 | /** |
| TMBOY | 45:2aa9f933c8d2 | 185 | * 发生DialogError时执行的方法 |
| TMBOY | 45:2aa9f933c8d2 | 186 | * |
| TMBOY | 45:2aa9f933c8d2 | 187 | * @param e BaiduDialogError异常类 |
| TMBOY | 45:2aa9f933c8d2 | 188 | */ |
| TMBOY | 45:2aa9f933c8d2 | 189 | void onError(BaiduDialogError e); |
| TMBOY | 45:2aa9f933c8d2 | 190 | |
| TMBOY | 45:2aa9f933c8d2 | 191 | /** |
| TMBOY | 45:2aa9f933c8d2 | 192 | * 当BaiduDialog执行取消操作时,执行该方法 |
| TMBOY | 45:2aa9f933c8d2 | 193 | */ |
| TMBOY | 45:2aa9f933c8d2 | 194 | void onCancel(); |
| TMBOY | 45:2aa9f933c8d2 | 195 | } |
| TMBOY | 45:2aa9f933c8d2 | 196 | } |
