Skip to content

Commit b5a8f14

Browse files
committed
[REFACTOR] android: support bluetooth / combined with AppRTC Demo's
1 parent e98d670 commit b5a8f14

11 files changed

+1700
-301
lines changed

android/src/main/java/com/zxcpoiu/incallmanager/AppRTC/AppRTCBluetoothManager.java

Lines changed: 516 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* Copyright 2014 The WebRTC Project Authors. All rights reserved.
3+
*
4+
* Use of this source code is governed by a BSD-style license
5+
* that can be found in the LICENSE file in the root of the source
6+
* tree. An additional intellectual property rights grant can be found
7+
* in the file PATENTS. All contributing project authors may
8+
* be found in the AUTHORS file in the root of the source tree.
9+
*/
10+
11+
package com.zxcpoiu.incallmanager.AppRTC;
12+
13+
import android.content.Context;
14+
import android.hardware.Sensor;
15+
import android.hardware.SensorEvent;
16+
import android.hardware.SensorEventListener;
17+
import android.hardware.SensorManager;
18+
import android.os.Build;
19+
import android.util.Log;
20+
21+
/**
22+
* AppRTCProximitySensor manages functions related to the proximity sensor in
23+
* the AppRTC demo.
24+
* On most device, the proximity sensor is implemented as a boolean-sensor.
25+
* It returns just two values "NEAR" or "FAR". Thresholding is done on the LUX
26+
* value i.e. the LUX value of the light sensor is compared with a threshold.
27+
* A LUX-value more than the threshold means the proximity sensor returns "FAR".
28+
* Anything less than the threshold value and the sensor returns "NEAR".
29+
*/
30+
public class AppRTCProximitySensor implements SensorEventListener {
31+
private static final String TAG = "AppRTCProximitySensor";
32+
33+
// This class should be created, started and stopped on one thread
34+
// (e.g. the main thread). We use |nonThreadSafe| to ensure that this is
35+
// the case. Only active when |DEBUG| is set to true.
36+
37+
private final Runnable onSensorStateListener;
38+
private final SensorManager sensorManager;
39+
private Sensor proximitySensor = null;
40+
private boolean lastStateReportIsNear = false;
41+
42+
/** Construction */
43+
public static AppRTCProximitySensor create(Context context, Runnable sensorStateListener) {
44+
return new AppRTCProximitySensor(context, sensorStateListener);
45+
}
46+
47+
private AppRTCProximitySensor(Context context, Runnable sensorStateListener) {
48+
Log.d(TAG, "AppRTCProximitySensor");
49+
onSensorStateListener = sensorStateListener;
50+
sensorManager = ((SensorManager) context.getSystemService(Context.SENSOR_SERVICE));
51+
}
52+
53+
/**
54+
* Activate the proximity sensor. Also do initialization if called for the
55+
* first time.
56+
*/
57+
public boolean start() {
58+
Log.d(TAG, "start");
59+
if (!initDefaultSensor()) {
60+
// Proximity sensor is not supported on this device.
61+
return false;
62+
}
63+
sensorManager.registerListener(this, proximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
64+
return true;
65+
}
66+
67+
/** Deactivate the proximity sensor. */
68+
public void stop() {
69+
Log.d(TAG, "stop");
70+
if (proximitySensor == null) {
71+
return;
72+
}
73+
sensorManager.unregisterListener(this, proximitySensor);
74+
}
75+
76+
/** Getter for last reported state. Set to true if "near" is reported. */
77+
public boolean sensorReportsNearState() {
78+
return lastStateReportIsNear;
79+
}
80+
81+
@Override
82+
public final void onAccuracyChanged(Sensor sensor, int accuracy) {
83+
if (accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
84+
Log.e(TAG, "The values returned by this sensor cannot be trusted");
85+
}
86+
}
87+
88+
@Override
89+
public final void onSensorChanged(SensorEvent event) {
90+
// As a best practice; do as little as possible within this method and
91+
// avoid blocking.
92+
float distanceInCentimeters = event.values[0];
93+
if (distanceInCentimeters < proximitySensor.getMaximumRange()) {
94+
Log.d(TAG, "Proximity sensor => NEAR state");
95+
lastStateReportIsNear = true;
96+
} else {
97+
Log.d(TAG, "Proximity sensor => FAR state");
98+
lastStateReportIsNear = false;
99+
}
100+
101+
// Report about new state to listening client. Client can then call
102+
// sensorReportsNearState() to query the current state (NEAR or FAR).
103+
if (onSensorStateListener != null) {
104+
onSensorStateListener.run();
105+
}
106+
107+
Log.d(TAG, "onSensorChanged" + ": "
108+
+ "accuracy=" + event.accuracy + ", timestamp=" + event.timestamp + ", distance="
109+
+ event.values[0]);
110+
}
111+
112+
/**
113+
* Get default proximity sensor if it exists. Tablet devices (e.g. Nexus 7)
114+
* does not support this type of sensor and false will be returned in such
115+
* cases.
116+
*/
117+
private boolean initDefaultSensor() {
118+
if (proximitySensor != null) {
119+
return true;
120+
}
121+
proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
122+
if (proximitySensor == null) {
123+
return false;
124+
}
125+
logProximitySensorInfo();
126+
return true;
127+
}
128+
129+
/** Helper method for logging information about the proximity sensor. */
130+
private void logProximitySensorInfo() {
131+
if (proximitySensor == null) {
132+
return;
133+
}
134+
StringBuilder info = new StringBuilder("Proximity sensor: ");
135+
info.append("name=").append(proximitySensor.getName());
136+
info.append(", vendor: ").append(proximitySensor.getVendor());
137+
info.append(", power: ").append(proximitySensor.getPower());
138+
info.append(", resolution: ").append(proximitySensor.getResolution());
139+
info.append(", max range: ").append(proximitySensor.getMaximumRange());
140+
info.append(", min delay: ").append(proximitySensor.getMinDelay());
141+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
142+
// Added in API level 20.
143+
info.append(", type: ").append(proximitySensor.getStringType());
144+
}
145+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
146+
// Added in API level 21.
147+
info.append(", max delay: ").append(proximitySensor.getMaxDelay());
148+
info.append(", reporting mode: ").append(proximitySensor.getReportingMode());
149+
info.append(", isWakeUpSensor: ").append(proximitySensor.isWakeUpSensor());
150+
}
151+
Log.d(TAG, info.toString());
152+
}
153+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Copyright (c) 2011, The WebRTC project authors. All rights reserved.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are
5+
met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
10+
* Redistributions in binary form must reproduce the above copyright
11+
notice, this list of conditions and the following disclaimer in
12+
the documentation and/or other materials provided with the
13+
distribution.
14+
15+
* Neither the name of Google nor the names of its contributors may
16+
be used to endorse or promote products derived from this software
17+
without specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
From WebRTC Source
2+
M64
3+
4+
Files in this library with prefix "AppRTC" are all get from webrtc source.
5+
The license / patents remain it's original. see LICENSE in this directory
6+
7+
This library slightly modified original class/package name to get compiled in.
8+
You can find diffs in `diff` directory as well.
9+
10+
- examples/androidapp/src/org/appspot/apprtc:
11+
* AppRTCBluetoothManager.java
12+
* AppRTCProximitySensor.java
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
--- /home/zxcpoiu/git/webrtcbuilds/out/src/examples/androidapp/src/org/appspot/apprtc/AppRTCBluetoothManager.java 2017-11-30 16:59:50.918956062 +0800
2+
+++ AppRTCBluetoothManager.java 2017-12-08 18:01:45.348130079 +0800
3+
@@ -8,7 +8,7 @@
4+
* be found in the AUTHORS file in the root of the source tree.
5+
*/
6+
7+
-package org.appspot.apprtc;
8+
+package com.zxcpoiu.incallmanager.AppRTC;
9+
10+
import android.annotation.SuppressLint;
11+
import android.bluetooth.BluetoothAdapter;
12+
@@ -27,8 +27,8 @@
13+
import android.util.Log;
14+
import java.util.List;
15+
import java.util.Set;
16+
-import org.appspot.apprtc.util.AppRTCUtils;
17+
-import org.webrtc.ThreadUtils;
18+
+
19+
+import com.zxcpoiu.incallmanager.InCallManagerModule;
20+
21+
/**
22+
* AppRTCProximitySensor manages functions related to Bluetoth devices in the
23+
@@ -63,7 +63,7 @@
24+
}
25+
26+
private final Context apprtcContext;
27+
- private final AppRTCAudioManager apprtcAudioManager;
28+
+ private final InCallManagerModule apprtcAudioManager;
29+
private final AudioManager audioManager;
30+
private final Handler handler;
31+
32+
@@ -190,14 +190,13 @@
33+
}
34+
35+
/** Construction. */
36+
- static AppRTCBluetoothManager create(Context context, AppRTCAudioManager audioManager) {
37+
- Log.d(TAG, "create" + AppRTCUtils.getThreadInfo());
38+
+ public static AppRTCBluetoothManager create(Context context, InCallManagerModule audioManager) {
39+
+ Log.d(TAG, "create");
40+
return new AppRTCBluetoothManager(context, audioManager);
41+
}
42+
43+
- protected AppRTCBluetoothManager(Context context, AppRTCAudioManager audioManager) {
44+
+ protected AppRTCBluetoothManager(Context context, InCallManagerModule audioManager) {
45+
Log.d(TAG, "ctor");
46+
- ThreadUtils.checkIsOnMainThread();
47+
apprtcContext = context;
48+
apprtcAudioManager = audioManager;
49+
this.audioManager = getAudioManager(context);
50+
@@ -209,7 +208,6 @@
51+
52+
/** Returns the internal state. */
53+
public State getState() {
54+
- ThreadUtils.checkIsOnMainThread();
55+
return bluetoothState;
56+
}
57+
58+
@@ -227,7 +225,6 @@
59+
* change.
60+
*/
61+
public void start() {
62+
- ThreadUtils.checkIsOnMainThread();
63+
Log.d(TAG, "start");
64+
if (!hasPermission(apprtcContext, android.Manifest.permission.BLUETOOTH)) {
65+
Log.w(TAG, "Process (pid=" + Process.myPid() + ") lacks BLUETOOTH permission");
66+
@@ -275,7 +272,6 @@
67+
68+
/** Stops and closes all components related to Bluetooth audio. */
69+
public void stop() {
70+
- ThreadUtils.checkIsOnMainThread();
71+
Log.d(TAG, "stop: BT state=" + bluetoothState);
72+
if (bluetoothAdapter == null) {
73+
return;
74+
@@ -312,7 +308,6 @@
75+
* accept SCO audio without a "call".
76+
*/
77+
public boolean startScoAudio() {
78+
- ThreadUtils.checkIsOnMainThread();
79+
Log.d(TAG, "startSco: BT state=" + bluetoothState + ", "
80+
+ "attempts: " + scoConnectionAttempts + ", "
81+
+ "SCO is on: " + isScoOn());
82+
@@ -341,7 +336,6 @@
83+
84+
/** Stops Bluetooth SCO connection with remote device. */
85+
public void stopScoAudio() {
86+
- ThreadUtils.checkIsOnMainThread();
87+
Log.d(TAG, "stopScoAudio: BT state=" + bluetoothState + ", "
88+
+ "SCO is on: " + isScoOn());
89+
if (bluetoothState != State.SCO_CONNECTING && bluetoothState != State.SCO_CONNECTED) {
90+
@@ -432,21 +426,18 @@
91+
92+
/** Ensures that the audio manager updates its list of available audio devices. */
93+
private void updateAudioDeviceState() {
94+
- ThreadUtils.checkIsOnMainThread();
95+
Log.d(TAG, "updateAudioDeviceState");
96+
apprtcAudioManager.updateAudioDeviceState();
97+
}
98+
99+
/** Starts timer which times out after BLUETOOTH_SCO_TIMEOUT_MS milliseconds. */
100+
private void startTimer() {
101+
- ThreadUtils.checkIsOnMainThread();
102+
Log.d(TAG, "startTimer");
103+
handler.postDelayed(bluetoothTimeoutRunnable, BLUETOOTH_SCO_TIMEOUT_MS);
104+
}
105+
106+
/** Cancels any outstanding timer tasks. */
107+
private void cancelTimer() {
108+
- ThreadUtils.checkIsOnMainThread();
109+
Log.d(TAG, "cancelTimer");
110+
handler.removeCallbacks(bluetoothTimeoutRunnable);
111+
}
112+
@@ -456,7 +447,6 @@
113+
* happens when the BT device has been turned on during an ongoing call.
114+
*/
115+
private void bluetoothTimeout() {
116+
- ThreadUtils.checkIsOnMainThread();
117+
if (bluetoothState == State.UNINITIALIZED || bluetoothHeadset == null) {
118+
return;
119+
}

0 commit comments

Comments
 (0)