ex
Fork of mbed-os-example-mbed5-blinky by
dcs-sdk-java-master/app/src/main/java/com/baidu/duer/dcs/androidapp/DcsSampleMainActivity.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.ActivityNotFoundException; |
| TMBOY | 45:2aa9f933c8d2 | 19 | import android.content.Intent; |
| TMBOY | 45:2aa9f933c8d2 | 20 | import android.graphics.Bitmap; |
| TMBOY | 45:2aa9f933c8d2 | 21 | import android.net.Uri; |
| TMBOY | 45:2aa9f933c8d2 | 22 | import android.os.Bundle; |
| TMBOY | 45:2aa9f933c8d2 | 23 | import android.view.View; |
| TMBOY | 45:2aa9f933c8d2 | 24 | import android.webkit.WebView; |
| TMBOY | 45:2aa9f933c8d2 | 25 | import android.widget.Button; |
| TMBOY | 45:2aa9f933c8d2 | 26 | import android.widget.LinearLayout; |
| TMBOY | 45:2aa9f933c8d2 | 27 | import android.widget.TextView; |
| TMBOY | 45:2aa9f933c8d2 | 28 | import android.widget.Toast; |
| TMBOY | 45:2aa9f933c8d2 | 29 | |
| TMBOY | 45:2aa9f933c8d2 | 30 | import com.baidu.duer.dcs.R; |
| TMBOY | 45:2aa9f933c8d2 | 31 | import com.baidu.duer.dcs.androidsystemimpl.PlatformFactoryImpl; |
| TMBOY | 45:2aa9f933c8d2 | 32 | import com.baidu.duer.dcs.androidsystemimpl.webview.BaseWebView; |
| TMBOY | 45:2aa9f933c8d2 | 33 | import com.baidu.duer.dcs.devicemodule.voiceinput.VoiceInputDeviceModule; |
| TMBOY | 45:2aa9f933c8d2 | 34 | import com.baidu.duer.dcs.framework.DcsFramework; |
| TMBOY | 45:2aa9f933c8d2 | 35 | import com.baidu.duer.dcs.framework.DeviceModuleFactory; |
| TMBOY | 45:2aa9f933c8d2 | 36 | import com.baidu.duer.dcs.framework.IResponseListener; |
| TMBOY | 45:2aa9f933c8d2 | 37 | import com.baidu.duer.dcs.http.HttpConfig; |
| TMBOY | 45:2aa9f933c8d2 | 38 | import com.baidu.duer.dcs.oauth.api.IOauth; |
| TMBOY | 45:2aa9f933c8d2 | 39 | import com.baidu.duer.dcs.oauth.api.OauthImpl; |
| TMBOY | 45:2aa9f933c8d2 | 40 | import com.baidu.duer.dcs.systeminterface.IMediaPlayer; |
| TMBOY | 45:2aa9f933c8d2 | 41 | import com.baidu.duer.dcs.systeminterface.IPlatformFactory; |
| TMBOY | 45:2aa9f933c8d2 | 42 | import com.baidu.duer.dcs.systeminterface.IWakeUp; |
| TMBOY | 45:2aa9f933c8d2 | 43 | import com.baidu.duer.dcs.util.CommonUtil; |
| TMBOY | 45:2aa9f933c8d2 | 44 | import com.baidu.duer.dcs.util.FileUtil; |
| TMBOY | 45:2aa9f933c8d2 | 45 | import com.baidu.duer.dcs.util.LogUtil; |
| TMBOY | 45:2aa9f933c8d2 | 46 | import com.baidu.duer.dcs.util.NetWorkUtil; |
| TMBOY | 45:2aa9f933c8d2 | 47 | import com.baidu.duer.dcs.wakeup.WakeUp; |
| TMBOY | 45:2aa9f933c8d2 | 48 | |
| TMBOY | 45:2aa9f933c8d2 | 49 | import java.io.File; |
| TMBOY | 45:2aa9f933c8d2 | 50 | |
| TMBOY | 45:2aa9f933c8d2 | 51 | /** |
| TMBOY | 45:2aa9f933c8d2 | 52 | * 主界面 activity |
| TMBOY | 45:2aa9f933c8d2 | 53 | * <p> |
| TMBOY | 45:2aa9f933c8d2 | 54 | * Created by zhangyan42@baidu.com on 2017/5/18. |
| TMBOY | 45:2aa9f933c8d2 | 55 | */ |
| TMBOY | 45:2aa9f933c8d2 | 56 | public class DcsSampleMainActivity extends DcsSampleBaseActivity implements View.OnClickListener { |
| TMBOY | 45:2aa9f933c8d2 | 57 | public static final String TAG = "DcsDemoActivity"; |
| TMBOY | 45:2aa9f933c8d2 | 58 | private Button voiceButton; |
| TMBOY | 45:2aa9f933c8d2 | 59 | private TextView textViewTimeStopListen; |
| TMBOY | 45:2aa9f933c8d2 | 60 | private Button pauseOrPlayButton; |
| TMBOY | 45:2aa9f933c8d2 | 61 | private BaseWebView webView; |
| TMBOY | 45:2aa9f933c8d2 | 62 | private LinearLayout mTopLinearLayout; |
| TMBOY | 45:2aa9f933c8d2 | 63 | private DcsFramework dcsFramework; |
| TMBOY | 45:2aa9f933c8d2 | 64 | private DeviceModuleFactory deviceModuleFactory; |
| TMBOY | 45:2aa9f933c8d2 | 65 | private IPlatformFactory platformFactory; |
| TMBOY | 45:2aa9f933c8d2 | 66 | private boolean isPause = true; |
| TMBOY | 45:2aa9f933c8d2 | 67 | private long startTimeStopListen; |
| TMBOY | 45:2aa9f933c8d2 | 68 | private boolean isStopListenReceiving; |
| TMBOY | 45:2aa9f933c8d2 | 69 | private String mHtmlUrl; |
| TMBOY | 45:2aa9f933c8d2 | 70 | // 唤醒 |
| TMBOY | 45:2aa9f933c8d2 | 71 | private WakeUp wakeUp; |
| TMBOY | 45:2aa9f933c8d2 | 72 | |
| TMBOY | 45:2aa9f933c8d2 | 73 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 74 | protected void onCreate(Bundle savedInstanceState) { |
| TMBOY | 45:2aa9f933c8d2 | 75 | super.onCreate(savedInstanceState); |
| TMBOY | 45:2aa9f933c8d2 | 76 | setContentView(R.layout.dcs_sample_activity_main); |
| TMBOY | 45:2aa9f933c8d2 | 77 | initView(); |
| TMBOY | 45:2aa9f933c8d2 | 78 | initOauth(); |
| TMBOY | 45:2aa9f933c8d2 | 79 | initFramework(); |
| TMBOY | 45:2aa9f933c8d2 | 80 | } |
| TMBOY | 45:2aa9f933c8d2 | 81 | |
| TMBOY | 45:2aa9f933c8d2 | 82 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 83 | protected void onStart() { |
| TMBOY | 45:2aa9f933c8d2 | 84 | super.onStart(); |
| TMBOY | 45:2aa9f933c8d2 | 85 | initOauth(); |
| TMBOY | 45:2aa9f933c8d2 | 86 | } |
| TMBOY | 45:2aa9f933c8d2 | 87 | |
| TMBOY | 45:2aa9f933c8d2 | 88 | private void initView() { |
| TMBOY | 45:2aa9f933c8d2 | 89 | Button openLogBtn = (Button) findViewById(R.id.openLogBtn); |
| TMBOY | 45:2aa9f933c8d2 | 90 | openLogBtn.setOnClickListener(this); |
| TMBOY | 45:2aa9f933c8d2 | 91 | voiceButton = (Button) findViewById(R.id.voiceBtn); |
| TMBOY | 45:2aa9f933c8d2 | 92 | voiceButton.setOnClickListener(this); |
| TMBOY | 45:2aa9f933c8d2 | 93 | |
| TMBOY | 45:2aa9f933c8d2 | 94 | textViewTimeStopListen = (TextView) findViewById(R.id.id_tv_time_0); |
| TMBOY | 45:2aa9f933c8d2 | 95 | mTopLinearLayout = (LinearLayout) findViewById(R.id.topLinearLayout); |
| TMBOY | 45:2aa9f933c8d2 | 96 | |
| TMBOY | 45:2aa9f933c8d2 | 97 | webView = new BaseWebView(DcsSampleMainActivity.this.getApplicationContext()); |
| TMBOY | 45:2aa9f933c8d2 | 98 | webView.setWebViewClientListen(new BaseWebView.WebViewClientListener() { |
| TMBOY | 45:2aa9f933c8d2 | 99 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 100 | public BaseWebView.LoadingWebStatus shouldOverrideUrlLoading(WebView view, String url) { |
| TMBOY | 45:2aa9f933c8d2 | 101 | // 拦截处理不让其点击 |
| TMBOY | 45:2aa9f933c8d2 | 102 | return BaseWebView.LoadingWebStatus.STATUS_TRUE; |
| TMBOY | 45:2aa9f933c8d2 | 103 | } |
| TMBOY | 45:2aa9f933c8d2 | 104 | |
| TMBOY | 45:2aa9f933c8d2 | 105 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 106 | public void onPageStarted(WebView view, String url, Bitmap favicon) { |
| TMBOY | 45:2aa9f933c8d2 | 107 | |
| TMBOY | 45:2aa9f933c8d2 | 108 | } |
| TMBOY | 45:2aa9f933c8d2 | 109 | |
| TMBOY | 45:2aa9f933c8d2 | 110 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 111 | public void onPageFinished(WebView view, String url) { |
| TMBOY | 45:2aa9f933c8d2 | 112 | if (!url.equals(mHtmlUrl) && !"about:blank".equals(mHtmlUrl)) { |
| TMBOY | 45:2aa9f933c8d2 | 113 | platformFactory.getWebView().linkClicked(url); |
| TMBOY | 45:2aa9f933c8d2 | 114 | } |
| TMBOY | 45:2aa9f933c8d2 | 115 | |
| TMBOY | 45:2aa9f933c8d2 | 116 | mHtmlUrl = url; |
| TMBOY | 45:2aa9f933c8d2 | 117 | } |
| TMBOY | 45:2aa9f933c8d2 | 118 | |
| TMBOY | 45:2aa9f933c8d2 | 119 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 120 | public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { |
| TMBOY | 45:2aa9f933c8d2 | 121 | |
| TMBOY | 45:2aa9f933c8d2 | 122 | } |
| TMBOY | 45:2aa9f933c8d2 | 123 | }); |
| TMBOY | 45:2aa9f933c8d2 | 124 | mTopLinearLayout.addView(webView); |
| TMBOY | 45:2aa9f933c8d2 | 125 | webView.loadUrl("about:blank"); |
| TMBOY | 45:2aa9f933c8d2 | 126 | |
| TMBOY | 45:2aa9f933c8d2 | 127 | Button mPreviousSongBtn = (Button) findViewById(R.id.previousSongBtn); |
| TMBOY | 45:2aa9f933c8d2 | 128 | pauseOrPlayButton = (Button) findViewById(R.id.pauseOrPlayBtn); |
| TMBOY | 45:2aa9f933c8d2 | 129 | Button mNextSongBtn = (Button) findViewById(R.id.nextSongBtn); |
| TMBOY | 45:2aa9f933c8d2 | 130 | mPreviousSongBtn.setOnClickListener(this); |
| TMBOY | 45:2aa9f933c8d2 | 131 | pauseOrPlayButton.setOnClickListener(this); |
| TMBOY | 45:2aa9f933c8d2 | 132 | mNextSongBtn.setOnClickListener(this); |
| TMBOY | 45:2aa9f933c8d2 | 133 | } |
| TMBOY | 45:2aa9f933c8d2 | 134 | |
| TMBOY | 45:2aa9f933c8d2 | 135 | private void initFramework() { |
| TMBOY | 45:2aa9f933c8d2 | 136 | platformFactory = new PlatformFactoryImpl(this); |
| TMBOY | 45:2aa9f933c8d2 | 137 | platformFactory.setWebView(webView); |
| TMBOY | 45:2aa9f933c8d2 | 138 | dcsFramework = new DcsFramework(platformFactory); |
| TMBOY | 45:2aa9f933c8d2 | 139 | deviceModuleFactory = dcsFramework.getDeviceModuleFactory(); |
| TMBOY | 45:2aa9f933c8d2 | 140 | |
| TMBOY | 45:2aa9f933c8d2 | 141 | deviceModuleFactory.createVoiceOutputDeviceModule(); |
| TMBOY | 45:2aa9f933c8d2 | 142 | deviceModuleFactory.createVoiceInputDeviceModule(); |
| TMBOY | 45:2aa9f933c8d2 | 143 | deviceModuleFactory.getVoiceInputDeviceModule().addVoiceInputListener( |
| TMBOY | 45:2aa9f933c8d2 | 144 | new VoiceInputDeviceModule.IVoiceInputListener() { |
| TMBOY | 45:2aa9f933c8d2 | 145 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 146 | public void onStartRecord() { |
| TMBOY | 45:2aa9f933c8d2 | 147 | LogUtil.d(TAG, "onStartRecord"); |
| TMBOY | 45:2aa9f933c8d2 | 148 | startRecording(); |
| TMBOY | 45:2aa9f933c8d2 | 149 | } |
| TMBOY | 45:2aa9f933c8d2 | 150 | |
| TMBOY | 45:2aa9f933c8d2 | 151 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 152 | public void onFinishRecord() { |
| TMBOY | 45:2aa9f933c8d2 | 153 | LogUtil.d(TAG, "onFinishRecord"); |
| TMBOY | 45:2aa9f933c8d2 | 154 | stopRecording(); |
| TMBOY | 45:2aa9f933c8d2 | 155 | } |
| TMBOY | 45:2aa9f933c8d2 | 156 | |
| TMBOY | 45:2aa9f933c8d2 | 157 | public void onSucceed(int statusCode) { |
| TMBOY | 45:2aa9f933c8d2 | 158 | LogUtil.d(TAG, "onSucceed-statusCode:" + statusCode); |
| TMBOY | 45:2aa9f933c8d2 | 159 | if (statusCode != 200) { |
| TMBOY | 45:2aa9f933c8d2 | 160 | stopRecording(); |
| TMBOY | 45:2aa9f933c8d2 | 161 | Toast.makeText(DcsSampleMainActivity.this, |
| TMBOY | 45:2aa9f933c8d2 | 162 | getResources().getString(R.string.voice_err_msg), |
| TMBOY | 45:2aa9f933c8d2 | 163 | Toast.LENGTH_SHORT) |
| TMBOY | 45:2aa9f933c8d2 | 164 | .show(); |
| TMBOY | 45:2aa9f933c8d2 | 165 | } |
| TMBOY | 45:2aa9f933c8d2 | 166 | } |
| TMBOY | 45:2aa9f933c8d2 | 167 | |
| TMBOY | 45:2aa9f933c8d2 | 168 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 169 | public void onFailed(String errorMessage) { |
| TMBOY | 45:2aa9f933c8d2 | 170 | LogUtil.d(TAG, "onFailed-errorMessage:" + errorMessage); |
| TMBOY | 45:2aa9f933c8d2 | 171 | stopRecording(); |
| TMBOY | 45:2aa9f933c8d2 | 172 | Toast.makeText(DcsSampleMainActivity.this, |
| TMBOY | 45:2aa9f933c8d2 | 173 | getResources().getString(R.string.voice_err_msg), |
| TMBOY | 45:2aa9f933c8d2 | 174 | Toast.LENGTH_SHORT) |
| TMBOY | 45:2aa9f933c8d2 | 175 | .show(); |
| TMBOY | 45:2aa9f933c8d2 | 176 | } |
| TMBOY | 45:2aa9f933c8d2 | 177 | }); |
| TMBOY | 45:2aa9f933c8d2 | 178 | |
| TMBOY | 45:2aa9f933c8d2 | 179 | deviceModuleFactory.createAlertsDeviceModule(); |
| TMBOY | 45:2aa9f933c8d2 | 180 | |
| TMBOY | 45:2aa9f933c8d2 | 181 | deviceModuleFactory.createAudioPlayerDeviceModule(); |
| TMBOY | 45:2aa9f933c8d2 | 182 | deviceModuleFactory.getAudioPlayerDeviceModule().addAudioPlayListener( |
| TMBOY | 45:2aa9f933c8d2 | 183 | new IMediaPlayer.SimpleMediaPlayerListener() { |
| TMBOY | 45:2aa9f933c8d2 | 184 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 185 | public void onPaused() { |
| TMBOY | 45:2aa9f933c8d2 | 186 | super.onPaused(); |
| TMBOY | 45:2aa9f933c8d2 | 187 | pauseOrPlayButton.setText(getResources().getString(R.string.audio_paused)); |
| TMBOY | 45:2aa9f933c8d2 | 188 | isPause = true; |
| TMBOY | 45:2aa9f933c8d2 | 189 | } |
| TMBOY | 45:2aa9f933c8d2 | 190 | |
| TMBOY | 45:2aa9f933c8d2 | 191 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 192 | public void onPlaying() { |
| TMBOY | 45:2aa9f933c8d2 | 193 | super.onPlaying(); |
| TMBOY | 45:2aa9f933c8d2 | 194 | pauseOrPlayButton.setText(getResources().getString(R.string.audio_playing)); |
| TMBOY | 45:2aa9f933c8d2 | 195 | isPause = false; |
| TMBOY | 45:2aa9f933c8d2 | 196 | } |
| TMBOY | 45:2aa9f933c8d2 | 197 | |
| TMBOY | 45:2aa9f933c8d2 | 198 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 199 | public void onCompletion() { |
| TMBOY | 45:2aa9f933c8d2 | 200 | super.onCompletion(); |
| TMBOY | 45:2aa9f933c8d2 | 201 | pauseOrPlayButton.setText(getResources().getString(R.string.audio_default)); |
| TMBOY | 45:2aa9f933c8d2 | 202 | isPause = false; |
| TMBOY | 45:2aa9f933c8d2 | 203 | } |
| TMBOY | 45:2aa9f933c8d2 | 204 | |
| TMBOY | 45:2aa9f933c8d2 | 205 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 206 | public void onStopped() { |
| TMBOY | 45:2aa9f933c8d2 | 207 | super.onStopped(); |
| TMBOY | 45:2aa9f933c8d2 | 208 | pauseOrPlayButton.setText(getResources().getString(R.string.audio_default)); |
| TMBOY | 45:2aa9f933c8d2 | 209 | isPause = true; |
| TMBOY | 45:2aa9f933c8d2 | 210 | } |
| TMBOY | 45:2aa9f933c8d2 | 211 | }); |
| TMBOY | 45:2aa9f933c8d2 | 212 | |
| TMBOY | 45:2aa9f933c8d2 | 213 | deviceModuleFactory.createSystemDeviceModule(); |
| TMBOY | 45:2aa9f933c8d2 | 214 | deviceModuleFactory.createSpeakControllerDeviceModule(); |
| TMBOY | 45:2aa9f933c8d2 | 215 | deviceModuleFactory.createPlaybackControllerDeviceModule(); |
| TMBOY | 45:2aa9f933c8d2 | 216 | deviceModuleFactory.createScreenDeviceModule(); |
| TMBOY | 45:2aa9f933c8d2 | 217 | |
| TMBOY | 45:2aa9f933c8d2 | 218 | // init唤醒 |
| TMBOY | 45:2aa9f933c8d2 | 219 | wakeUp = new WakeUp(platformFactory.getWakeUp(), |
| TMBOY | 45:2aa9f933c8d2 | 220 | platformFactory.getAudioRecord()); |
| TMBOY | 45:2aa9f933c8d2 | 221 | wakeUp.addWakeUpListener(wakeUpListener); |
| TMBOY | 45:2aa9f933c8d2 | 222 | // 开始录音,监听是否说了唤醒词 |
| TMBOY | 45:2aa9f933c8d2 | 223 | wakeUp.startWakeUp(); |
| TMBOY | 45:2aa9f933c8d2 | 224 | } |
| TMBOY | 45:2aa9f933c8d2 | 225 | |
| TMBOY | 45:2aa9f933c8d2 | 226 | private IWakeUp.IWakeUpListener wakeUpListener = new IWakeUp.IWakeUpListener() { |
| TMBOY | 45:2aa9f933c8d2 | 227 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 228 | public void onWakeUpSucceed() { |
| TMBOY | 45:2aa9f933c8d2 | 229 | Toast.makeText(DcsSampleMainActivity.this, |
| TMBOY | 45:2aa9f933c8d2 | 230 | getResources().getString(R.string.wakeup_succeed), |
| TMBOY | 45:2aa9f933c8d2 | 231 | Toast.LENGTH_SHORT) |
| TMBOY | 45:2aa9f933c8d2 | 232 | .show(); |
| TMBOY | 45:2aa9f933c8d2 | 233 | voiceButton.performClick(); |
| TMBOY | 45:2aa9f933c8d2 | 234 | } |
| TMBOY | 45:2aa9f933c8d2 | 235 | }; |
| TMBOY | 45:2aa9f933c8d2 | 236 | |
| TMBOY | 45:2aa9f933c8d2 | 237 | private void doUserActivity() { |
| TMBOY | 45:2aa9f933c8d2 | 238 | deviceModuleFactory.getSystemProvider().userActivity(); |
| TMBOY | 45:2aa9f933c8d2 | 239 | } |
| TMBOY | 45:2aa9f933c8d2 | 240 | |
| TMBOY | 45:2aa9f933c8d2 | 241 | private void initOauth() { |
| TMBOY | 45:2aa9f933c8d2 | 242 | IOauth baiduOauth = new OauthImpl(); |
| TMBOY | 45:2aa9f933c8d2 | 243 | if (baiduOauth.isSessionValid()) { |
| TMBOY | 45:2aa9f933c8d2 | 244 | HttpConfig.setAccessToken(baiduOauth.getAccessToken()); |
| TMBOY | 45:2aa9f933c8d2 | 245 | } else { |
| TMBOY | 45:2aa9f933c8d2 | 246 | baiduOauth.authorize(); |
| TMBOY | 45:2aa9f933c8d2 | 247 | } |
| TMBOY | 45:2aa9f933c8d2 | 248 | } |
| TMBOY | 45:2aa9f933c8d2 | 249 | |
| TMBOY | 45:2aa9f933c8d2 | 250 | private void stopRecording() { |
| TMBOY | 45:2aa9f933c8d2 | 251 | wakeUp.startWakeUp(); |
| TMBOY | 45:2aa9f933c8d2 | 252 | isStopListenReceiving = false; |
| TMBOY | 45:2aa9f933c8d2 | 253 | voiceButton.setText(getResources().getString(R.string.stop_record)); |
| TMBOY | 45:2aa9f933c8d2 | 254 | long t = System.currentTimeMillis() - startTimeStopListen; |
| TMBOY | 45:2aa9f933c8d2 | 255 | textViewTimeStopListen.setText(getResources().getString(R.string.time_record, t)); |
| TMBOY | 45:2aa9f933c8d2 | 256 | } |
| TMBOY | 45:2aa9f933c8d2 | 257 | |
| TMBOY | 45:2aa9f933c8d2 | 258 | private void startRecording() { |
| TMBOY | 45:2aa9f933c8d2 | 259 | wakeUp.stopWakeUp(); |
| TMBOY | 45:2aa9f933c8d2 | 260 | isStopListenReceiving = true; |
| TMBOY | 45:2aa9f933c8d2 | 261 | deviceModuleFactory.getSystemProvider().userActivity(); |
| TMBOY | 45:2aa9f933c8d2 | 262 | voiceButton.setText(getResources().getString(R.string.start_record)); |
| TMBOY | 45:2aa9f933c8d2 | 263 | textViewTimeStopListen.setText(""); |
| TMBOY | 45:2aa9f933c8d2 | 264 | } |
| TMBOY | 45:2aa9f933c8d2 | 265 | |
| TMBOY | 45:2aa9f933c8d2 | 266 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 267 | public void onClick(View v) { |
| TMBOY | 45:2aa9f933c8d2 | 268 | switch (v.getId()) { |
| TMBOY | 45:2aa9f933c8d2 | 269 | case R.id.voiceBtn: |
| TMBOY | 45:2aa9f933c8d2 | 270 | if (!NetWorkUtil.isNetworkConnected(this)) { |
| TMBOY | 45:2aa9f933c8d2 | 271 | Toast.makeText(this, |
| TMBOY | 45:2aa9f933c8d2 | 272 | getResources().getString(R.string.err_net_msg), |
| TMBOY | 45:2aa9f933c8d2 | 273 | Toast.LENGTH_SHORT).show(); |
| TMBOY | 45:2aa9f933c8d2 | 274 | return; |
| TMBOY | 45:2aa9f933c8d2 | 275 | } |
| TMBOY | 45:2aa9f933c8d2 | 276 | if (CommonUtil.isFastDoubleClick()) { |
| TMBOY | 45:2aa9f933c8d2 | 277 | return; |
| TMBOY | 45:2aa9f933c8d2 | 278 | } |
| TMBOY | 45:2aa9f933c8d2 | 279 | if (isStopListenReceiving) { |
| TMBOY | 45:2aa9f933c8d2 | 280 | platformFactory.getVoiceInput().stopRecord(); |
| TMBOY | 45:2aa9f933c8d2 | 281 | isStopListenReceiving = false; |
| TMBOY | 45:2aa9f933c8d2 | 282 | return; |
| TMBOY | 45:2aa9f933c8d2 | 283 | } |
| TMBOY | 45:2aa9f933c8d2 | 284 | isStopListenReceiving = true; |
| TMBOY | 45:2aa9f933c8d2 | 285 | startTimeStopListen = System.currentTimeMillis(); |
| TMBOY | 45:2aa9f933c8d2 | 286 | platformFactory.getVoiceInput().startRecord(); |
| TMBOY | 45:2aa9f933c8d2 | 287 | doUserActivity(); |
| TMBOY | 45:2aa9f933c8d2 | 288 | break; |
| TMBOY | 45:2aa9f933c8d2 | 289 | case R.id.openLogBtn: |
| TMBOY | 45:2aa9f933c8d2 | 290 | openAssignFolder(FileUtil.getLogFilePath()); |
| TMBOY | 45:2aa9f933c8d2 | 291 | break; |
| TMBOY | 45:2aa9f933c8d2 | 292 | case R.id.previousSongBtn: |
| TMBOY | 45:2aa9f933c8d2 | 293 | platformFactory.getPlayback().previous(nextPreResponseListener); |
| TMBOY | 45:2aa9f933c8d2 | 294 | doUserActivity(); |
| TMBOY | 45:2aa9f933c8d2 | 295 | break; |
| TMBOY | 45:2aa9f933c8d2 | 296 | case R.id.nextSongBtn: |
| TMBOY | 45:2aa9f933c8d2 | 297 | platformFactory.getPlayback().next(nextPreResponseListener); |
| TMBOY | 45:2aa9f933c8d2 | 298 | doUserActivity(); |
| TMBOY | 45:2aa9f933c8d2 | 299 | break; |
| TMBOY | 45:2aa9f933c8d2 | 300 | case R.id.pauseOrPlayBtn: |
| TMBOY | 45:2aa9f933c8d2 | 301 | if (isPause) { |
| TMBOY | 45:2aa9f933c8d2 | 302 | platformFactory.getPlayback().play(playPauseResponseListener); |
| TMBOY | 45:2aa9f933c8d2 | 303 | } else { |
| TMBOY | 45:2aa9f933c8d2 | 304 | platformFactory.getPlayback().pause(playPauseResponseListener); |
| TMBOY | 45:2aa9f933c8d2 | 305 | } |
| TMBOY | 45:2aa9f933c8d2 | 306 | doUserActivity(); |
| TMBOY | 45:2aa9f933c8d2 | 307 | break; |
| TMBOY | 45:2aa9f933c8d2 | 308 | default: |
| TMBOY | 45:2aa9f933c8d2 | 309 | break; |
| TMBOY | 45:2aa9f933c8d2 | 310 | } |
| TMBOY | 45:2aa9f933c8d2 | 311 | } |
| TMBOY | 45:2aa9f933c8d2 | 312 | |
| TMBOY | 45:2aa9f933c8d2 | 313 | private IResponseListener playPauseResponseListener = new IResponseListener() { |
| TMBOY | 45:2aa9f933c8d2 | 314 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 315 | public void onSucceed(int statusCode) { |
| TMBOY | 45:2aa9f933c8d2 | 316 | if (statusCode == 204) { |
| TMBOY | 45:2aa9f933c8d2 | 317 | Toast.makeText(DcsSampleMainActivity.this, |
| TMBOY | 45:2aa9f933c8d2 | 318 | getResources().getString(R.string.no_directive), |
| TMBOY | 45:2aa9f933c8d2 | 319 | Toast.LENGTH_SHORT) |
| TMBOY | 45:2aa9f933c8d2 | 320 | .show(); |
| TMBOY | 45:2aa9f933c8d2 | 321 | } |
| TMBOY | 45:2aa9f933c8d2 | 322 | } |
| TMBOY | 45:2aa9f933c8d2 | 323 | |
| TMBOY | 45:2aa9f933c8d2 | 324 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 325 | public void onFailed(String errorMessage) { |
| TMBOY | 45:2aa9f933c8d2 | 326 | Toast.makeText(DcsSampleMainActivity.this, |
| TMBOY | 45:2aa9f933c8d2 | 327 | getResources().getString(R.string.request_error), |
| TMBOY | 45:2aa9f933c8d2 | 328 | Toast.LENGTH_SHORT) |
| TMBOY | 45:2aa9f933c8d2 | 329 | .show(); |
| TMBOY | 45:2aa9f933c8d2 | 330 | } |
| TMBOY | 45:2aa9f933c8d2 | 331 | }; |
| TMBOY | 45:2aa9f933c8d2 | 332 | |
| TMBOY | 45:2aa9f933c8d2 | 333 | private IResponseListener nextPreResponseListener = new IResponseListener() { |
| TMBOY | 45:2aa9f933c8d2 | 334 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 335 | public void onSucceed(int statusCode) { |
| TMBOY | 45:2aa9f933c8d2 | 336 | if (statusCode == 204) { |
| TMBOY | 45:2aa9f933c8d2 | 337 | Toast.makeText(DcsSampleMainActivity.this, |
| TMBOY | 45:2aa9f933c8d2 | 338 | getResources().getString(R.string.no_audio), |
| TMBOY | 45:2aa9f933c8d2 | 339 | Toast.LENGTH_SHORT) |
| TMBOY | 45:2aa9f933c8d2 | 340 | .show(); |
| TMBOY | 45:2aa9f933c8d2 | 341 | } |
| TMBOY | 45:2aa9f933c8d2 | 342 | } |
| TMBOY | 45:2aa9f933c8d2 | 343 | |
| TMBOY | 45:2aa9f933c8d2 | 344 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 345 | public void onFailed(String errorMessage) { |
| TMBOY | 45:2aa9f933c8d2 | 346 | Toast.makeText(DcsSampleMainActivity.this, |
| TMBOY | 45:2aa9f933c8d2 | 347 | getResources().getString(R.string.request_error), |
| TMBOY | 45:2aa9f933c8d2 | 348 | Toast.LENGTH_SHORT) |
| TMBOY | 45:2aa9f933c8d2 | 349 | .show(); |
| TMBOY | 45:2aa9f933c8d2 | 350 | } |
| TMBOY | 45:2aa9f933c8d2 | 351 | }; |
| TMBOY | 45:2aa9f933c8d2 | 352 | |
| TMBOY | 45:2aa9f933c8d2 | 353 | /** |
| TMBOY | 45:2aa9f933c8d2 | 354 | * 打开日志 |
| TMBOY | 45:2aa9f933c8d2 | 355 | * |
| TMBOY | 45:2aa9f933c8d2 | 356 | * @param path 文件的绝对路径 |
| TMBOY | 45:2aa9f933c8d2 | 357 | */ |
| TMBOY | 45:2aa9f933c8d2 | 358 | private void openAssignFolder(String path) { |
| TMBOY | 45:2aa9f933c8d2 | 359 | File file = new File(path); |
| TMBOY | 45:2aa9f933c8d2 | 360 | if (!file.exists()) { |
| TMBOY | 45:2aa9f933c8d2 | 361 | Toast.makeText(DcsSampleMainActivity.this, |
| TMBOY | 45:2aa9f933c8d2 | 362 | getResources().getString(R.string.no_log), |
| TMBOY | 45:2aa9f933c8d2 | 363 | Toast.LENGTH_SHORT) |
| TMBOY | 45:2aa9f933c8d2 | 364 | .show(); |
| TMBOY | 45:2aa9f933c8d2 | 365 | return; |
| TMBOY | 45:2aa9f933c8d2 | 366 | } |
| TMBOY | 45:2aa9f933c8d2 | 367 | Intent intent = new Intent(Intent.ACTION_VIEW); |
| TMBOY | 45:2aa9f933c8d2 | 368 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| TMBOY | 45:2aa9f933c8d2 | 369 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| TMBOY | 45:2aa9f933c8d2 | 370 | intent.setDataAndType(Uri.fromFile(file), "text/plain"); |
| TMBOY | 45:2aa9f933c8d2 | 371 | try { |
| TMBOY | 45:2aa9f933c8d2 | 372 | startActivity(Intent.createChooser(intent, |
| TMBOY | 45:2aa9f933c8d2 | 373 | getResources().getString(R.string.open_file_title))); |
| TMBOY | 45:2aa9f933c8d2 | 374 | } catch (ActivityNotFoundException e) { |
| TMBOY | 45:2aa9f933c8d2 | 375 | e.printStackTrace(); |
| TMBOY | 45:2aa9f933c8d2 | 376 | } |
| TMBOY | 45:2aa9f933c8d2 | 377 | } |
| TMBOY | 45:2aa9f933c8d2 | 378 | |
| TMBOY | 45:2aa9f933c8d2 | 379 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 380 | protected void onDestroy() { |
| TMBOY | 45:2aa9f933c8d2 | 381 | super.onDestroy(); |
| TMBOY | 45:2aa9f933c8d2 | 382 | // 先remove listener 停止唤醒,释放资源 |
| TMBOY | 45:2aa9f933c8d2 | 383 | wakeUp.removeWakeUpListener(wakeUpListener); |
| TMBOY | 45:2aa9f933c8d2 | 384 | wakeUp.stopWakeUp(); |
| TMBOY | 45:2aa9f933c8d2 | 385 | wakeUp.releaseWakeUp(); |
| TMBOY | 45:2aa9f933c8d2 | 386 | |
| TMBOY | 45:2aa9f933c8d2 | 387 | if (dcsFramework != null) { |
| TMBOY | 45:2aa9f933c8d2 | 388 | dcsFramework.release(); |
| TMBOY | 45:2aa9f933c8d2 | 389 | } |
| TMBOY | 45:2aa9f933c8d2 | 390 | webView.setWebViewClientListen(null); |
| TMBOY | 45:2aa9f933c8d2 | 391 | mTopLinearLayout.removeView(webView); |
| TMBOY | 45:2aa9f933c8d2 | 392 | webView.removeAllViews(); |
| TMBOY | 45:2aa9f933c8d2 | 393 | webView.destroy(); |
| TMBOY | 45:2aa9f933c8d2 | 394 | } |
| TMBOY | 45:2aa9f933c8d2 | 395 | } |
