ex
Fork of mbed-os-example-mbed5-blinky by
dcs-sdk-java-master/app/src/main/java/com/baidu/duer/dcs/androidapp/DcsSampleOAuthActivity.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.androidapp; |
| TMBOY | 45:2aa9f933c8d2 | 17 | |
| TMBOY | 45:2aa9f933c8d2 | 18 | import android.content.Intent; |
| TMBOY | 45:2aa9f933c8d2 | 19 | import android.os.Bundle; |
| TMBOY | 45:2aa9f933c8d2 | 20 | import android.text.TextUtils; |
| TMBOY | 45:2aa9f933c8d2 | 21 | import android.view.View; |
| TMBOY | 45:2aa9f933c8d2 | 22 | import android.widget.Button; |
| TMBOY | 45:2aa9f933c8d2 | 23 | import android.widget.EditText; |
| TMBOY | 45:2aa9f933c8d2 | 24 | import android.widget.Toast; |
| TMBOY | 45:2aa9f933c8d2 | 25 | |
| TMBOY | 45:2aa9f933c8d2 | 26 | import com.baidu.duer.dcs.R; |
| TMBOY | 45:2aa9f933c8d2 | 27 | import com.baidu.duer.dcs.oauth.api.BaiduDialog; |
| TMBOY | 45:2aa9f933c8d2 | 28 | import com.baidu.duer.dcs.oauth.api.BaiduDialogError; |
| TMBOY | 45:2aa9f933c8d2 | 29 | import com.baidu.duer.dcs.oauth.api.BaiduException; |
| TMBOY | 45:2aa9f933c8d2 | 30 | import com.baidu.duer.dcs.oauth.api.BaiduOauthImplicitGrant; |
| TMBOY | 45:2aa9f933c8d2 | 31 | import com.baidu.duer.dcs.util.LogUtil; |
| TMBOY | 45:2aa9f933c8d2 | 32 | |
| TMBOY | 45:2aa9f933c8d2 | 33 | /** |
| TMBOY | 45:2aa9f933c8d2 | 34 | * 用户认证界面 |
| TMBOY | 45:2aa9f933c8d2 | 35 | * <p> |
| TMBOY | 45:2aa9f933c8d2 | 36 | * Created by zhangyan42@baidu.com on 2017/5/18. |
| TMBOY | 45:2aa9f933c8d2 | 37 | */ |
| TMBOY | 45:2aa9f933c8d2 | 38 | public class DcsSampleOAuthActivity extends DcsSampleBaseActivity implements View.OnClickListener { |
| TMBOY | 45:2aa9f933c8d2 | 39 | // 需要开发者自己申请client_id |
| TMBOY | 45:2aa9f933c8d2 | 40 | // client_id,就是oauth的client_id |
| TMBOY | 45:2aa9f933c8d2 | 41 | private static final String CLIENT_ID = "{{nc72pRBp2VbSBZk0LSvqHGjFrbjOvQNH}}"; |
| TMBOY | 45:2aa9f933c8d2 | 42 | // 是否每次授权都强制登陆 |
| TMBOY | 45:2aa9f933c8d2 | 43 | private boolean isForceLogin = false; |
| TMBOY | 45:2aa9f933c8d2 | 44 | // 是否每次都确认登陆 |
| TMBOY | 45:2aa9f933c8d2 | 45 | private boolean isConfirmLogin = true; |
| TMBOY | 45:2aa9f933c8d2 | 46 | private EditText editTextClientId; |
| TMBOY | 45:2aa9f933c8d2 | 47 | private Button oauthLoginButton; |
| TMBOY | 45:2aa9f933c8d2 | 48 | private BaiduOauthImplicitGrant baiduOauthImplicitGrant; |
| TMBOY | 45:2aa9f933c8d2 | 49 | |
| TMBOY | 45:2aa9f933c8d2 | 50 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 51 | protected void onCreate(Bundle savedInstanceState) { |
| TMBOY | 45:2aa9f933c8d2 | 52 | super.onCreate(savedInstanceState); |
| TMBOY | 45:2aa9f933c8d2 | 53 | setContentView(R.layout.dcs_sample_activity_oauth); |
| TMBOY | 45:2aa9f933c8d2 | 54 | initView(); |
| TMBOY | 45:2aa9f933c8d2 | 55 | setOnClickListener(); |
| TMBOY | 45:2aa9f933c8d2 | 56 | } |
| TMBOY | 45:2aa9f933c8d2 | 57 | |
| TMBOY | 45:2aa9f933c8d2 | 58 | private void setOnClickListener() { |
| TMBOY | 45:2aa9f933c8d2 | 59 | oauthLoginButton.setOnClickListener(this); |
| TMBOY | 45:2aa9f933c8d2 | 60 | } |
| TMBOY | 45:2aa9f933c8d2 | 61 | |
| TMBOY | 45:2aa9f933c8d2 | 62 | private void initView() { |
| TMBOY | 45:2aa9f933c8d2 | 63 | editTextClientId = (EditText) findViewById(R.id.edit_client_id); |
| TMBOY | 45:2aa9f933c8d2 | 64 | oauthLoginButton = (Button) findViewById(R.id.btn_login); |
| TMBOY | 45:2aa9f933c8d2 | 65 | |
| TMBOY | 45:2aa9f933c8d2 | 66 | editTextClientId.setText(CLIENT_ID); |
| TMBOY | 45:2aa9f933c8d2 | 67 | } |
| TMBOY | 45:2aa9f933c8d2 | 68 | |
| TMBOY | 45:2aa9f933c8d2 | 69 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 70 | public void onClick(View view) { |
| TMBOY | 45:2aa9f933c8d2 | 71 | switch (view.getId()) { |
| TMBOY | 45:2aa9f933c8d2 | 72 | case R.id.btn_login: |
| TMBOY | 45:2aa9f933c8d2 | 73 | String clientId = editTextClientId.getText().toString(); |
| TMBOY | 45:2aa9f933c8d2 | 74 | if (!TextUtils.isEmpty(clientId) && !TextUtils.isEmpty(clientId)) { |
| TMBOY | 45:2aa9f933c8d2 | 75 | baiduOauthImplicitGrant = new BaiduOauthImplicitGrant(clientId, DcsSampleOAuthActivity.this.getApplication()); |
| TMBOY | 45:2aa9f933c8d2 | 76 | baiduOauthImplicitGrant.authorize(DcsSampleOAuthActivity.this, isForceLogin, isConfirmLogin, new BaiduDialog |
| TMBOY | 45:2aa9f933c8d2 | 77 | .BaiduDialogListener() { |
| TMBOY | 45:2aa9f933c8d2 | 78 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 79 | public void onComplete(Bundle values) { |
| TMBOY | 45:2aa9f933c8d2 | 80 | Toast.makeText(DcsSampleOAuthActivity.this.getApplicationContext(), |
| TMBOY | 45:2aa9f933c8d2 | 81 | getResources().getString(R.string.login_succeed), |
| TMBOY | 45:2aa9f933c8d2 | 82 | Toast.LENGTH_SHORT).show(); |
| TMBOY | 45:2aa9f933c8d2 | 83 | startMainActivity(); |
| TMBOY | 45:2aa9f933c8d2 | 84 | } |
| TMBOY | 45:2aa9f933c8d2 | 85 | |
| TMBOY | 45:2aa9f933c8d2 | 86 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 87 | public void onBaiduException(BaiduException e) { |
| TMBOY | 45:2aa9f933c8d2 | 88 | |
| TMBOY | 45:2aa9f933c8d2 | 89 | } |
| TMBOY | 45:2aa9f933c8d2 | 90 | |
| TMBOY | 45:2aa9f933c8d2 | 91 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 92 | public void onError(BaiduDialogError e) { |
| TMBOY | 45:2aa9f933c8d2 | 93 | if (null != e) { |
| TMBOY | 45:2aa9f933c8d2 | 94 | String toastString = TextUtils.isEmpty(e.getMessage()) |
| TMBOY | 45:2aa9f933c8d2 | 95 | ? DcsSampleOAuthActivity.this.getResources() |
| TMBOY | 45:2aa9f933c8d2 | 96 | .getString(R.string.err_net_msg) : e.getMessage(); |
| TMBOY | 45:2aa9f933c8d2 | 97 | Toast.makeText(DcsSampleOAuthActivity.this.getApplicationContext(), toastString, |
| TMBOY | 45:2aa9f933c8d2 | 98 | Toast.LENGTH_SHORT).show(); |
| TMBOY | 45:2aa9f933c8d2 | 99 | } |
| TMBOY | 45:2aa9f933c8d2 | 100 | } |
| TMBOY | 45:2aa9f933c8d2 | 101 | |
| TMBOY | 45:2aa9f933c8d2 | 102 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 103 | public void onCancel() { |
| TMBOY | 45:2aa9f933c8d2 | 104 | LogUtil.d("cancle", "I am back"); |
| TMBOY | 45:2aa9f933c8d2 | 105 | } |
| TMBOY | 45:2aa9f933c8d2 | 106 | }); |
| TMBOY | 45:2aa9f933c8d2 | 107 | } else { |
| TMBOY | 45:2aa9f933c8d2 | 108 | Toast.makeText(DcsSampleOAuthActivity.this.getApplicationContext(), |
| TMBOY | 45:2aa9f933c8d2 | 109 | getResources().getString(R.string.client_id_empty), |
| TMBOY | 45:2aa9f933c8d2 | 110 | Toast.LENGTH_SHORT).show(); |
| TMBOY | 45:2aa9f933c8d2 | 111 | } |
| TMBOY | 45:2aa9f933c8d2 | 112 | break; |
| TMBOY | 45:2aa9f933c8d2 | 113 | default: |
| TMBOY | 45:2aa9f933c8d2 | 114 | break; |
| TMBOY | 45:2aa9f933c8d2 | 115 | } |
| TMBOY | 45:2aa9f933c8d2 | 116 | } |
| TMBOY | 45:2aa9f933c8d2 | 117 | |
| TMBOY | 45:2aa9f933c8d2 | 118 | private void startMainActivity() { |
| TMBOY | 45:2aa9f933c8d2 | 119 | Intent intent = new Intent(DcsSampleOAuthActivity.this, DcsSampleMainActivity.class); |
| TMBOY | 45:2aa9f933c8d2 | 120 | intent.putExtra("baidu", baiduOauthImplicitGrant); |
| TMBOY | 45:2aa9f933c8d2 | 121 | startActivity(intent); |
| TMBOY | 45:2aa9f933c8d2 | 122 | finish(); |
| TMBOY | 45:2aa9f933c8d2 | 123 | } |
| TMBOY | 45:2aa9f933c8d2 | 124 | } |
