ex
Fork of mbed-os-example-mbed5-blinky by
dcs-sdk-java-master/app/src/main/java/com/baidu/duer/dcs/oauth/api/OauthImpl.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.content.Intent; |
| TMBOY | 45:2aa9f933c8d2 | 19 | import android.text.TextUtils; |
| TMBOY | 45:2aa9f933c8d2 | 20 | |
| TMBOY | 45:2aa9f933c8d2 | 21 | import com.baidu.duer.dcs.androidapp.DcsSampleApplication; |
| TMBOY | 45:2aa9f933c8d2 | 22 | import com.baidu.duer.dcs.androidapp.DcsSampleOAuthActivity; |
| TMBOY | 45:2aa9f933c8d2 | 23 | |
| TMBOY | 45:2aa9f933c8d2 | 24 | /** |
| TMBOY | 45:2aa9f933c8d2 | 25 | * 用户认证接口 |
| TMBOY | 45:2aa9f933c8d2 | 26 | * <p> |
| TMBOY | 45:2aa9f933c8d2 | 27 | * Created by zhangyan42@baidu.com on 2017/6/8. |
| TMBOY | 45:2aa9f933c8d2 | 28 | */ |
| TMBOY | 45:2aa9f933c8d2 | 29 | public class OauthImpl implements IOauth { |
| TMBOY | 45:2aa9f933c8d2 | 30 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 31 | public String getAccessToken() { |
| TMBOY | 45:2aa9f933c8d2 | 32 | return OauthPreferenceUtil.getAccessToken(DcsSampleApplication.getInstance()); |
| TMBOY | 45:2aa9f933c8d2 | 33 | } |
| TMBOY | 45:2aa9f933c8d2 | 34 | |
| TMBOY | 45:2aa9f933c8d2 | 35 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 36 | public void authorize() { |
| TMBOY | 45:2aa9f933c8d2 | 37 | Intent intent = new Intent(DcsSampleApplication.getInstance(), DcsSampleOAuthActivity.class); |
| TMBOY | 45:2aa9f933c8d2 | 38 | intent.putExtra("START_TAG", "RESTART"); |
| TMBOY | 45:2aa9f933c8d2 | 39 | intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); |
| TMBOY | 45:2aa9f933c8d2 | 40 | DcsSampleApplication.getInstance().startActivity(intent); |
| TMBOY | 45:2aa9f933c8d2 | 41 | } |
| TMBOY | 45:2aa9f933c8d2 | 42 | |
| TMBOY | 45:2aa9f933c8d2 | 43 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 44 | public boolean isSessionValid() { |
| TMBOY | 45:2aa9f933c8d2 | 45 | String accessToken = getAccessToken(); |
| TMBOY | 45:2aa9f933c8d2 | 46 | long createTime = OauthPreferenceUtil.getCreateTime(DcsSampleApplication.getInstance()); |
| TMBOY | 45:2aa9f933c8d2 | 47 | long expires = OauthPreferenceUtil.getExpires(DcsSampleApplication.getInstance()) + createTime; |
| TMBOY | 45:2aa9f933c8d2 | 48 | return !TextUtils.isEmpty(accessToken) && expires != 0 && System.currentTimeMillis() < expires; |
| TMBOY | 45:2aa9f933c8d2 | 49 | } |
| TMBOY | 45:2aa9f933c8d2 | 50 | } |
