├── .idea
├── copyright
│ └── profiles_settings.xml
├── encodings.xml
├── vcs.xml
├── modules.xml
├── runConfigurations.xml
├── gradle.xml
├── compiler.xml
└── misc.xml
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── src
└── main
│ ├── res
│ └── values
│ │ ├── strings.xml
│ │ └── styles.xml
│ ├── aidl
│ └── com
│ │ └── github
│ │ └── martoreto
│ │ └── aauto
│ │ └── vex
│ │ ├── ICarStatsListener.aidl
│ │ ├── IVexProxyListener.aidl
│ │ ├── IVexProxy.aidl
│ │ └── ICarStats.aidl
│ ├── AndroidManifest.xml
│ └── java
│ └── com
│ └── github
│ └── martoreto
│ └── aauto
│ └── vex
│ ├── PermissionsActivity.java
│ ├── FieldSchema.java
│ ├── VexProxyService.java
│ └── CarStatsClient.java
├── proguard-rules.pro
├── gradle.properties
├── gradlew.bat
└── gradlew
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ns130291/aauto-vex-base/master/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | Denied car access permission, funtionality will be limited.
4 |
5 |
--------------------------------------------------------------------------------
/src/main/aidl/com/github/martoreto/aauto/vex/ICarStatsListener.aidl:
--------------------------------------------------------------------------------
1 | package com.github.martoreto.aauto.vex;
2 |
3 | oneway interface ICarStatsListener {
4 | void onNewMeasurements(long timestamp, in Map values);
5 | void onSchemaChanged();
6 | }
7 |
--------------------------------------------------------------------------------
/src/main/aidl/com/github/martoreto/aauto/vex/IVexProxyListener.aidl:
--------------------------------------------------------------------------------
1 | package com.github.martoreto.aauto.vex;
2 |
3 | oneway interface IVexProxyListener {
4 | void onConnected();
5 | void onData(in byte[] data);
6 | void onDisconnected();
7 | }
8 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue Dec 05 22:15:23 CET 2017
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
7 |
--------------------------------------------------------------------------------
/src/main/aidl/com/github/martoreto/aauto/vex/IVexProxy.aidl:
--------------------------------------------------------------------------------
1 | package com.github.martoreto.aauto.vex;
2 |
3 | import com.github.martoreto.aauto.vex.IVexProxyListener;
4 |
5 | interface IVexProxy {
6 | void registerListener(IVexProxyListener listener);
7 | void unregisterListener(IVexProxyListener listener);
8 | void sendData(in byte[] data);
9 | }
10 |
--------------------------------------------------------------------------------
/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/src/main/aidl/com/github/martoreto/aauto/vex/ICarStats.aidl:
--------------------------------------------------------------------------------
1 | package com.github.martoreto.aauto.vex;
2 |
3 | import com.github.martoreto.aauto.vex.ICarStatsListener;
4 |
5 | interface ICarStats {
6 | void registerListener(ICarStatsListener listener);
7 | void unregisterListener(ICarStatsListener listener);
8 | Map getMergedMeasurements();
9 |
10 | boolean needsPermissions();
11 | void requestPermissions();
12 |
13 | Map getSchema();
14 | }
15 |
--------------------------------------------------------------------------------
/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
11 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add any project specific keep options here:
2 |
3 | # If your project uses WebView with JS, uncomment the following
4 | # and specify the fully qualified class name to the JavaScript interface
5 | # class:
6 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
7 | # public *;
8 | #}
9 |
10 | # Uncomment this to preserve the line number information for
11 | # debugging stack traces.
12 | #-keepattributes SourceFile,LineNumberTable
13 |
14 | # If you keep the line number information, uncomment this to
15 | # hide the original source file name.
16 | #-renamesourcefileattribute SourceFile
17 |
18 | -keepnames class com.github.martoreto.aauto.vex.FieldSchema
19 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/src/main/java/com/github/martoreto/aauto/vex/PermissionsActivity.java:
--------------------------------------------------------------------------------
1 | package com.github.martoreto.aauto.vex;
2 |
3 | import android.app.Activity;
4 | import android.content.pm.PackageManager;
5 | import android.support.annotation.NonNull;
6 | import android.support.v4.app.ActivityCompat;
7 | import android.widget.Toast;
8 |
9 | public class PermissionsActivity extends Activity {
10 | private static final int REQUEST_PERMISSION = 1;
11 |
12 | @Override
13 | protected void onStart() {
14 | super.onStart();
15 |
16 | if (VexProxyService.needsPermissions(this)) {
17 | requestPermission();
18 | } else {
19 | finish();
20 | }
21 | }
22 |
23 | private void requestPermission() {
24 | ActivityCompat.requestPermissions(this,
25 | new String[] {VexProxyService.PERMISSION_VEX},
26 | REQUEST_PERMISSION);
27 | }
28 |
29 | @Override
30 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
31 | super.onRequestPermissionsResult(requestCode, permissions, grantResults);
32 |
33 | switch (requestCode) {
34 | case REQUEST_PERMISSION:
35 | if (grantResults.length == 1 && grantResults[0] != PackageManager.PERMISSION_GRANTED) {
36 | Toast.makeText(this, R.string.vex_permission_not_granted, Toast.LENGTH_SHORT).show();
37 | }
38 | finish();
39 | break;
40 | }
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/src/main/java/com/github/martoreto/aauto/vex/FieldSchema.java:
--------------------------------------------------------------------------------
1 | package com.github.martoreto.aauto.vex;
2 |
3 | import android.os.Parcel;
4 | import android.os.Parcelable;
5 | import android.support.annotation.Nullable;
6 |
7 | public class FieldSchema implements Parcelable {
8 | public static final int TYPE_STRING = 0;
9 | public static final int TYPE_INTEGER = 1;
10 | public static final int TYPE_FLOAT = 2;
11 | public static final int TYPE_BOOLEAN = 3;
12 |
13 | private int type;
14 | private @Nullable String description;
15 | private @Nullable String unit;
16 | private float min;
17 | private float max;
18 | private float resolution;
19 |
20 | public FieldSchema(int type, @Nullable String description, @Nullable String unit, float min,
21 | float max, float resolution) {
22 | this.type = type;
23 | this.description = description;
24 | this.unit = unit;
25 | this.min = min;
26 | this.max = max;
27 | this.resolution = resolution;
28 | }
29 |
30 | public FieldSchema(Parcel in) {
31 | this.type = in.readInt();
32 | this.description = in.readString();
33 | this.unit = in.readString();
34 | this.min = in.readFloat();
35 | this.max = in.readFloat();
36 | this.resolution = in.readFloat();
37 | }
38 |
39 | public int getType() {
40 | return type;
41 | }
42 |
43 | @Nullable
44 | public String getDescription() {
45 | return description;
46 | }
47 |
48 | @Nullable
49 | public String getUnit() {
50 | return unit;
51 | }
52 |
53 | public float getMin() {
54 | return min;
55 | }
56 |
57 | public float getMax() {
58 | return max;
59 | }
60 |
61 | public float getResolution() {
62 | return resolution;
63 | }
64 |
65 | @Override
66 | public int describeContents() {
67 | return 0;
68 | }
69 |
70 | @Override
71 | public void writeToParcel(Parcel out, int flags) {
72 | out.writeInt(type);
73 | out.writeString(description);
74 | out.writeString(unit);
75 | out.writeFloat(min);
76 | out.writeFloat(max);
77 | out.writeFloat(resolution);
78 | }
79 |
80 | public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
81 | public FieldSchema createFromParcel(Parcel in) {
82 | return new FieldSchema(in);
83 | }
84 |
85 | public FieldSchema[] newArray(int size) {
86 | return new FieldSchema[size];
87 | }
88 | };
89 | }
90 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/src/main/java/com/github/martoreto/aauto/vex/VexProxyService.java:
--------------------------------------------------------------------------------
1 | package com.github.martoreto.aauto.vex;
2 |
3 | import android.app.Service;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.content.pm.PackageManager;
7 | import android.os.Handler;
8 | import android.os.HandlerThread;
9 | import android.os.IBinder;
10 | import android.os.RemoteCallbackList;
11 | import android.os.RemoteException;
12 | import android.support.annotation.Nullable;
13 | import android.support.car.Car;
14 | import android.support.car.CarConnectionCallback;
15 | import android.support.car.CarNotConnectedException;
16 | import android.support.v4.content.ContextCompat;
17 | import android.util.Log;
18 |
19 | import com.google.android.apps.auto.sdk.service.CarVendorExtensionManagerLoader;
20 | import com.google.android.apps.auto.sdk.service.vec.CarVendorExtensionManager;
21 |
22 | import java.io.IOException;
23 |
24 | public abstract class VexProxyService extends Service {
25 | private static final String TAG = "VexProxy";
26 |
27 | public static final String PERMISSION_VEX = "com.google.android.gms.permission.CAR_VENDOR_EXTENSION";
28 |
29 | private static final long RETRY_DELAY_MS = 16000;
30 |
31 | private Car mCar;
32 | private CarVendorExtensionManager mVexManager;
33 | private HandlerThread mHandlerThread;
34 | private Handler mHandler;
35 | private RemoteCallbackList mListeners;
36 |
37 | @Override
38 | public void onCreate() {
39 | super.onCreate();
40 |
41 | Log.d(TAG, "Service starting.");
42 |
43 | mHandlerThread = new HandlerThread(TAG);
44 | mHandlerThread.start();
45 | mHandler = new Handler(mHandlerThread.getLooper());
46 |
47 | mListeners = new RemoteCallbackList<>();
48 |
49 | mCar = Car.createCar(this, mCarConnectionCallback, mHandler);
50 | mHandler.post(mConnectToCar);
51 | }
52 |
53 | private final Runnable mConnectToCar = new Runnable() {
54 | @Override
55 | public void run() {
56 | mCar.connect();
57 | }
58 | };
59 |
60 | private final CarConnectionCallback mCarConnectionCallback = new CarConnectionCallback() {
61 | @Override
62 | public void onConnected(Car car) {
63 | if (car != mCar) {
64 | Log.d(TAG, "onConnected: wrong car");
65 | return;
66 | }
67 |
68 | Log.i(TAG, "Car connected.");
69 | try {
70 | CarVendorExtensionManagerLoader vexLoader =
71 | (CarVendorExtensionManagerLoader)mCar.getCarManager(
72 | CarVendorExtensionManagerLoader.VENDOR_EXTENSION_LOADER_SERVICE);
73 | mVexManager = vexLoader.getManager(getVendorChannelName());
74 | if (mVexManager == null) {
75 | throw new RuntimeException("Exlap channel not available");
76 | }
77 | } catch (Exception e) {
78 | Log.e(TAG, "Error initializing VEX channel", e);
79 | return;
80 | }
81 |
82 | mVexManager.registerListener(mVexListener);
83 | dispatchOnConnected();
84 | }
85 |
86 | @Override
87 | public void onDisconnected(Car car) {
88 | if (car != mCar) {
89 | Log.d(TAG, "onDisconnected: wrong car");
90 | return;
91 | }
92 |
93 | Log.i(TAG, "Car disconnected.");
94 | dispatchOnDisconnected();
95 | carDisconnected();
96 | }
97 | };
98 |
99 | private void carDisconnected() {
100 | mHandler.postDelayed(mConnectToCar, RETRY_DELAY_MS);
101 | }
102 |
103 | @Nullable
104 | @Override
105 | public IBinder onBind(Intent intent) {
106 | return mBinder;
107 | }
108 |
109 | @Override
110 | public void onDestroy() {
111 | Log.d(TAG, "Service stopping.");
112 | mHandlerThread.quitSafely();
113 | if (mVexManager != null) {
114 | mVexManager.release();
115 | mVexManager = null;
116 | }
117 | if (mCar.isConnected()) {
118 | mCar.disconnect();
119 | }
120 | super.onDestroy();
121 | }
122 |
123 | private final IVexProxy.Stub mBinder = new IVexProxy.Stub() {
124 | @Override
125 | public void registerListener(final IVexProxyListener listener) throws RemoteException {
126 | mListeners.register(listener);
127 |
128 | // If we are already connected, we send the onConnected() event to the newly
129 | // registered listener.
130 | mHandler.post(new Runnable() {
131 | @Override
132 | public void run() {
133 | if (mVexManager != null) {
134 | try {
135 | listener.onConnected();
136 | } catch (Exception e) {
137 | Log.d(TAG, "Exception sending initial onConnected()", e);
138 | }
139 | }
140 | }
141 | });
142 | }
143 |
144 | @Override
145 | public void unregisterListener(final IVexProxyListener listener) throws RemoteException {
146 | mListeners.unregister(listener);
147 | }
148 |
149 | @Override
150 | public void sendData(byte[] data) throws RemoteException {
151 | try {
152 | mVexManager.sendData(data);
153 | } catch (CarNotConnectedException e) {
154 | throw new RemoteException("Car not connected");
155 | } catch (IOException e) {
156 | Log.w(TAG, "IOException in sendData", e);
157 | throw new RemoteException("I/O Error sending data");
158 | }
159 | }
160 | };
161 |
162 | private CarVendorExtensionManager.CarVendorExtensionListener mVexListener = new CarVendorExtensionManager.CarVendorExtensionListener() {
163 | @Override
164 | public void onData(CarVendorExtensionManager carVendorExtensionManager, byte[] bytes) {
165 | dispatchOnData(bytes);
166 | }
167 | };
168 |
169 | private void dispatchOnData(byte[] data) {
170 | int i = mListeners.beginBroadcast();
171 | while (i > 0) {
172 | i--;
173 | try {
174 | mListeners.getBroadcastItem(i).onData(data);
175 | } catch (RemoteException e) {
176 | Log.d(TAG, "Exception from callback", e);
177 | }
178 | }
179 | mListeners.finishBroadcast();
180 | }
181 |
182 | private void dispatchOnConnected() {
183 | int i = mListeners.beginBroadcast();
184 | while (i > 0) {
185 | i--;
186 | try {
187 | mListeners.getBroadcastItem(i).onConnected();
188 | } catch (RemoteException e) {
189 | Log.d(TAG, "Exception from callback", e);
190 | }
191 | }
192 | mListeners.finishBroadcast();
193 | }
194 |
195 | private void dispatchOnDisconnected() {
196 | int i = mListeners.beginBroadcast();
197 | while (i > 0) {
198 | i--;
199 | try {
200 | mListeners.getBroadcastItem(i).onDisconnected();
201 | } catch (RemoteException e) {
202 | Log.d(TAG, "Exception from callback", e);
203 | }
204 | }
205 | mListeners.finishBroadcast();
206 | }
207 |
208 | protected abstract String getVendorChannelName();
209 |
210 | public static boolean needsPermissions(Context context) {
211 | return ContextCompat.checkSelfPermission(context, PERMISSION_VEX)
212 | != PackageManager.PERMISSION_GRANTED;
213 | }
214 |
215 | public static void requestPermissions(Context context) {
216 | Intent i = new Intent(context, PermissionsActivity.class);
217 | i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
218 | context.startActivity(i);
219 | }
220 | }
221 |
--------------------------------------------------------------------------------
/src/main/java/com/github/martoreto/aauto/vex/CarStatsClient.java:
--------------------------------------------------------------------------------
1 | package com.github.martoreto.aauto.vex;
2 |
3 | import android.content.ComponentName;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.content.ServiceConnection;
7 | import android.content.pm.PackageManager;
8 | import android.content.pm.ResolveInfo;
9 | import android.os.IBinder;
10 | import android.os.RemoteException;
11 | import android.util.Log;
12 |
13 | import java.util.ArrayList;
14 | import java.util.Collection;
15 | import java.util.Collections;
16 | import java.util.Date;
17 | import java.util.HashMap;
18 | import java.util.Iterator;
19 | import java.util.List;
20 | import java.util.Map;
21 |
22 | public class CarStatsClient {
23 | private static final String TAG = "CarStatsClient";
24 |
25 | private static final String ACTION_CAR_STATS_PROVIDER = "com.github.martoreto.aauto.vex.CAR_STATS_PROVIDER";
26 |
27 | private Context mContext;
28 | private Map mServiceConnections = new HashMap<>();
29 | private Map mProviders = new HashMap<>();
30 | private List mProvidersByPriority = new ArrayList<>(); // earlier is better
31 | private Map mProvidersByKey = new HashMap<>();
32 | private Map mRemoteListeners = new HashMap<>();
33 | private List mListeners = new ArrayList<>();
34 | private Map mSchema = Collections.emptyMap();
35 |
36 | public CarStatsClient(Context context) {
37 | this.mContext = context;
38 | }
39 |
40 | public interface Listener {
41 | void onNewMeasurements(String provider, Date timestamp, Map values);
42 | void onSchemaChanged();
43 | }
44 |
45 | public void start() {
46 | for (Intent i: getProviderIntents(mContext)) {
47 | //noinspection ConstantConditions
48 | String provider = i.getComponent().flattenToShortString();
49 | ServiceConnection sc = createServiceConnection(provider);
50 | mServiceConnections.put(provider, sc);
51 | mProvidersByPriority.add(provider);
52 | Log.d(TAG, "Binding to " + provider);
53 | mContext.bindService(i, sc, Context.BIND_AUTO_CREATE);
54 | }
55 | }
56 |
57 | private ServiceConnection createServiceConnection(final String provider) {
58 | return new ServiceConnection() {
59 | @Override
60 | public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
61 | Log.v(TAG, "Connected to " + provider);
62 | ICarStats stats = ICarStats.Stub.asInterface(iBinder);
63 | mProviders.put(provider, stats);
64 | ICarStatsListener listener = createListener(provider);
65 | mRemoteListeners.put(provider, listener);
66 | try {
67 | stats.registerListener(listener);
68 | } catch (RemoteException e) {
69 | Log.w(TAG, provider + ": Error registering listener", e);
70 | }
71 | updateSchema();
72 | }
73 |
74 | @Override
75 | public void onServiceDisconnected(ComponentName componentName) {
76 | Log.v(TAG, "Disconnected from " + provider);
77 | mProviders.remove(provider);
78 | }
79 | };
80 | }
81 |
82 | private ICarStatsListener createListener(final String provider) {
83 | return new ICarStatsListener.Stub() {
84 | @SuppressWarnings("unchecked")
85 | @Override
86 | public void onNewMeasurements(long timestamp, Map values) throws RemoteException {
87 | for (Listener listener: mListeners) {
88 | try {
89 | listener.onNewMeasurements(provider, new Date(timestamp),
90 | filterValues(provider, values));
91 | } catch (Exception e) {
92 | Log.e(TAG, "Error calling listener", e);
93 | }
94 | }
95 | }
96 |
97 | @Override
98 | public void onSchemaChanged() throws RemoteException {
99 | updateSchema();
100 | }
101 | };
102 | }
103 |
104 | private Map filterValues(String provider, Map values) {
105 | Map providersByKey = mProvidersByKey;
106 | Iterator iter = values.keySet().iterator();
107 | while (iter.hasNext()) {
108 | if (!provider.equals(providersByKey.get(iter.next()))) {
109 | iter.remove();
110 | }
111 | }
112 | return values;
113 | }
114 |
115 | @SuppressWarnings("unchecked")
116 | private synchronized void updateSchema() {
117 | Map schema = new HashMap<>();
118 | Map providersByKey = new HashMap<>();
119 | for (String provider: mProvidersByPriority) {
120 | try {
121 | ICarStats providerInterface = mProviders.get(provider);
122 | if (providerInterface == null) {
123 | // Not connected at the moment.
124 | continue;
125 | }
126 | Map providerSchema = providerInterface.getSchema();
127 | for (String key: providerSchema.keySet()) {
128 | if (!providersByKey.containsKey(key)) {
129 | providersByKey.put(key, provider);
130 | }
131 | }
132 | schema.putAll(providerSchema);
133 | } catch (RemoteException e) {
134 | Log.w(TAG, provider + ": Error getting schema", e);
135 | }
136 | }
137 | mProvidersByKey = providersByKey;
138 | mSchema = schema;
139 |
140 | dispatchSchemaChanged();
141 | }
142 |
143 | private void dispatchSchemaChanged() {
144 | for (Listener listener: mListeners) {
145 | try {
146 | listener.onSchemaChanged();
147 | } catch (Exception e) {
148 | Log.e(TAG, "Error calling listener", e);
149 | }
150 | }
151 | }
152 |
153 | public void stop() {
154 | for (Map.Entry e: mProviders.entrySet()) {
155 | try {
156 | e.getValue().unregisterListener(mRemoteListeners.get(e.getKey()));
157 | } catch (RemoteException e1) {
158 | Log.w(TAG, e.getKey() + ": Error unregistering listener", e1);
159 | }
160 | }
161 | for (ServiceConnection sc: mServiceConnections.values()) {
162 | mContext.unbindService(sc);
163 | }
164 |
165 | mProviders.clear();
166 | mProvidersByPriority.clear();
167 | mProvidersByKey.clear();
168 | mRemoteListeners.clear();
169 | mServiceConnections.clear();
170 | }
171 |
172 | @SuppressWarnings("unchecked")
173 | public Map getMergedMeasurements() {
174 | Map measurements = new HashMap<>();
175 | for (Map.Entry e: mProviders.entrySet()) {
176 | String provider = e.getKey();
177 | try {
178 | measurements.putAll(filterValues(provider, e.getValue().getMergedMeasurements()));
179 | } catch (RemoteException e1) {
180 | Log.w(TAG, provider + ": Error getting measurements", e1);
181 | }
182 | }
183 | return measurements;
184 | }
185 |
186 | public synchronized Map getSchema() {
187 | return Collections.unmodifiableMap(mSchema);
188 | }
189 |
190 | public void registerListener(Listener listener) {
191 | mListeners.add(listener);
192 | }
193 |
194 | public void unregisterListener(Listener listener) {
195 | mListeners.remove(listener);
196 | }
197 |
198 | public static Collection getProviderInfos(Context context) {
199 | PackageManager pm = context.getPackageManager();
200 | Intent implicitIntent = new Intent(ACTION_CAR_STATS_PROVIDER);
201 | return pm.queryIntentServices(implicitIntent, 0);
202 | }
203 |
204 | public static Collection getProviderIntents(Context context) {
205 | Collection resolveInfos = getProviderInfos(context);
206 | List intents = new ArrayList<>(resolveInfos.size());
207 | for (ResolveInfo ri: resolveInfos) {
208 | ComponentName cn = new ComponentName(ri.serviceInfo.packageName, ri.serviceInfo.name);
209 | Intent explicitIntent = new Intent(ACTION_CAR_STATS_PROVIDER);
210 | explicitIntent.setComponent(cn);
211 | intents.add(explicitIntent);
212 | }
213 | return intents;
214 | }
215 |
216 | public static void requestPermissions(final Context context) {
217 | for (final Intent i: getProviderIntents(context)) {
218 | final ServiceConnection sc = new ServiceConnection() {
219 | @Override
220 | public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
221 | ICarStats stats = ICarStats.Stub.asInterface(iBinder);
222 | try {
223 | if (stats.needsPermissions()) {
224 | stats.requestPermissions();
225 | }
226 | } catch (RemoteException e) {
227 | //noinspection ConstantConditions
228 | String provider = i.getComponent().flattenToShortString();
229 | Log.w(TAG, provider + ": Error requesting permissions", e);
230 | }
231 | context.unbindService(this);
232 | }
233 |
234 | @Override
235 | public void onServiceDisconnected(ComponentName componentName) {
236 | }
237 | };
238 | context.bindService(i, sc, Context.BIND_AUTO_CREATE);
239 | }
240 | }
241 | }
242 |
--------------------------------------------------------------------------------