Skip to content

Commit

Permalink
Merge pull request #3 from Flutter-Auto-Technologies/init_car
Browse files Browse the repository at this point in the history
Integrated car speed sensor
  • Loading branch information
viralkachhadiya authored Jan 24, 2022
2 parents 556dea1 + 45a94bd commit 8a60376
Show file tree
Hide file tree
Showing 21 changed files with 314 additions and 56 deletions.
9 changes: 9 additions & 0 deletions .idea/libraries/Flutter_Plugins.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 52 additions & 30 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions android/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions android/.idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions android/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions android/.idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions android/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions android/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions android/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion android/.settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
arguments=
auto.sync=false
build.scans.enabled=false
connection.gradle.distribution=GRADLE_DISTRIBUTION(VERSION(7.0-rc-1))
connection.gradle.distribution=GRADLE_DISTRIBUTION(VERSION(7.1.1))
connection.project.dir=
eclipse.preferences.version=1
gradle.user.home=
Expand Down
3 changes: 3 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@ android {
defaultConfig {
minSdkVersion 16
}

// android.car exists since Android 10 (API level 29) Revision 5.
useLibrary 'android.car'
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,45 @@
package com.example.flutterandroidautoos.flutter_android_auto_os;

import android.app.Activity;
import android.car.Car;
import android.car.VehiclePropertyIds;
import android.car.hardware.CarPropertyValue;
import android.car.hardware.CarSensorManager;
import android.car.hardware.property.CarPropertyManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener;

/** FlutterAndroidAutoOsPlugin */
public class FlutterAndroidAutoOsPlugin implements FlutterPlugin, MethodCallHandler {
public class FlutterAndroidAutoOsPlugin implements FlutterPlugin, ActivityAware, MethodCallHandler , RequestPermissionsResultListener{
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
private MethodChannel channel;
private Car car;
private CarPropertyManager carPropertyManager;
private EventChannel carPropertyManagerChannel;
private FlutterPluginBinding flutterPluginBinding;
private ActivityPluginBinding activityPluginBinding;

@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "flutter_android_auto_os");
channel.setMethodCallHandler(this);
this.flutterPluginBinding = flutterPluginBinding;
}

@Override
Expand All @@ -33,6 +53,81 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {

@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
this.flutterPluginBinding = null;
}

private void setupChannels(Context context, BinaryMessenger messenger) {
// ActivityCompat.requestPermissions(activityPluginBinding.getActivity(),new String[] {
// Car.PERMISSION_POWERTRAIN
// },0);

ActivityCompat.requestPermissions(activityPluginBinding.getActivity(),new String[] {
Car.PERMISSION_SPEED
},0);

car=Car.createCar(context);
carPropertyManager= (CarPropertyManager) car.getCarManager(Car.PROPERTY_SERVICE);
carPropertyManagerChannel=new EventChannel(messenger, "car_gear");
carPropertyManagerChannel.setStreamHandler(new EventChannel.StreamHandler() {
@Override
public void onListen(Object arguments, EventChannel.EventSink events) {
carPropertyManager.registerCallback(new CarPropertyManager.CarPropertyEventCallback(){
@Override
public void onChangeEvent(CarPropertyValue carPropertyValue) {
Log.d("MainActivity:", String.valueOf(carPropertyValue.getValue()));
Log.d("MainActivity:", String.valueOf(carPropertyValue.describeContents()));
Log.d("MainActivity:", String.valueOf(carPropertyValue.toString()));
events.success(carPropertyValue.getValue());
}

@Override
public void onErrorEvent(int i, int i1) {
Log.d("MainActivity:", "Received error car property event, propId="+i);
}
}, VehiclePropertyIds.PERF_VEHICLE_SPEED,CarPropertyManager.SENSOR_RATE_NORMAL);
Log.d("MainActivity:", "listening");
}
@Override
public void onCancel(Object arguments) {
Log.d("MainActivity:", "cancel");
}
});

channel = new MethodChannel(messenger, "flutter_android_auto_os");
channel.setMethodCallHandler(this);
}

private void teardownEventChannels() {
channel.setMethodCallHandler(null);
carPropertyManagerChannel.setStreamHandler(null);
}

@Override
public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
this.activityPluginBinding = binding;
setupChannels(flutterPluginBinding.getApplicationContext(), flutterPluginBinding.getBinaryMessenger());
}

@Override
public void onDetachedFromActivityForConfigChanges() {
onDetachedFromActivity();
}

@Override
public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
onAttachedToActivity(binding);
}

@Override
public void onDetachedFromActivity() {
teardownEventChannels();
}

@Override
public boolean onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (permissions[0] == Car.PERMISSION_SPEED && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
return true;
}
return false;
}
}
4 changes: 4 additions & 0 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.flutterandroidautoos.flutter_android_auto_os_example">

<uses-permission android:name="android.car.permission.CAR_POWERTRAIN" />
<uses-permission android:name="android.car.permission.CAR_SPEED" />

<application
android:label="flutter_android_auto_os_example"
android:icon="@mipmap/ic_launcher">
Expand Down
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath 'com.android.tools.build:gradle:7.0.4'
}
}

Expand Down
2 changes: 1 addition & 1 deletion example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip
Loading

0 comments on commit 8a60376

Please sign in to comment.