aboutsummaryrefslogtreecommitdiff
path: root/tests/UxRestrictionsSample
diff options
context:
space:
mode:
authorYao, Yuxing <yaoyx@google.com>2018-02-02 12:12:08 -0800
committerYao, Yuxing <yaoyx@google.com>2018-02-06 10:04:10 -0800
commite2fa79d543139c2b70dddc85b136bf8fbce14278 (patch)
tree83b7b09d3068b8228163e7f7fea6812e4684068c /tests/UxRestrictionsSample
parente363360f39c5da38396f459926fa9ce0efd97aa4 (diff)
downloadCar-e2fa79d543139c2b70dddc85b136bf8fbce14278.tar.gz
Create a sample app for UxRestrictions.
This sample app depends on Car Support Lib to support UXR. Currently it has a button that mocks toggling UXR. Test: make UxRestrictions Bug: 69859979 Change-Id: I45af609ff03faf94a8ca6667271f851eae00e8ba
Diffstat (limited to 'tests/UxRestrictionsSample')
-rw-r--r--tests/UxRestrictionsSample/Android.mk64
-rw-r--r--tests/UxRestrictionsSample/AndroidManifest.xml31
-rw-r--r--tests/UxRestrictionsSample/res/layout/main_activity.xml54
-rw-r--r--tests/UxRestrictionsSample/src/com/google/android/car/uxr/sample/MainActivity.java192
4 files changed, 341 insertions, 0 deletions
diff --git a/tests/UxRestrictionsSample/Android.mk b/tests/UxRestrictionsSample/Android.mk
new file mode 100644
index 0000000000..65932576dd
--- /dev/null
+++ b/tests/UxRestrictionsSample/Android.mk
@@ -0,0 +1,64 @@
+# Copyright (C) 2018 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+#
+
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_RESOURCE_DIR := \
+ $(LOCAL_PATH)/res \
+ $(TOP)/frameworks/support/v7/recyclerview/res
+
+LOCAL_AAPT_FLAGS := \
+ --auto-add-overlay \
+ --extra-packages android.support.v7.recyclerview
+
+LOCAL_PACKAGE_NAME := UxRestrictionsSample
+
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_JAVA_VERSION := 1.8
+
+LOCAL_PROGUARD_ENABLED := disabled
+
+LOCAL_DEX_PREOPT := false
+
+LOCAL_USE_AAPT2 := true
+
+LOCAL_PRIVILEGED_MODULE := false
+
+LOCAL_STATIC_JAVA_LIBRARIES += \
+ vehicle-hal-support-lib \
+ android-support-v4 \
+ android-support-v7-recyclerview \
+ android-support-car
+
+LOCAL_STATIC_ANDROID_LIBRARIES += \
+ android-support-car\
+ android-support-design \
+ android-support-v4 \
+ android-support-v7-appcompat \
+ android-support-v7-cardview \
+ android-support-v7-recyclerview
+
+LOCAL_JAVA_LIBRARIES += android.car
+
+include $(BUILD_PACKAGE)
+
+# Use the following include to make our test apk.
+include $(call all-makefiles-under,$(LOCAL_PATH))
diff --git a/tests/UxRestrictionsSample/AndroidManifest.xml b/tests/UxRestrictionsSample/AndroidManifest.xml
new file mode 100644
index 0000000000..31127252ad
--- /dev/null
+++ b/tests/UxRestrictionsSample/AndroidManifest.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2017 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.google.android.car.uxr.sample">
+
+ <uses-sdk
+ android:minSdkVersion="20"
+ android:targetSdkVersion="20" />
+
+ <application android:label="UxRestrictions Sample">
+ <activity android:name=".MainActivity">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN"/>
+ <category android:name="android.intent.category.LAUNCHER"/>
+ </intent-filter>
+ </activity>
+ </application>
+</manifest>
diff --git a/tests/UxRestrictionsSample/res/layout/main_activity.xml b/tests/UxRestrictionsSample/res/layout/main_activity.xml
new file mode 100644
index 0000000000..190dd68727
--- /dev/null
+++ b/tests/UxRestrictionsSample/res/layout/main_activity.xml
@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2017 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<androidx.car.moderator.SpeedBumpView
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent">
+
+ <LinearLayout
+ android:orientation="horizontal"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent">
+
+ <LinearLayout
+ android:layout_height="match_parent"
+ android:layout_width="0dp"
+ android:layout_weight="1"
+ android:orientation="vertical"
+ android:gravity="center">
+ <TextView
+ android:id="@+id/uxr_status"
+ android:layout_gravity="center"
+ android:layout_width="match_parent"
+ android:textAppearance="?android:textAppearanceLarge"
+ android:layout_height="wrap_content" />
+ <Button
+ android:id="@+id/toggle_status"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:text="toggle status" />
+ </LinearLayout>
+
+ <androidx.car.widget.PagedListView
+ android:id="@+id/paged_list_view"
+ android:layout_width="0dp"
+ android:layout_height="match_parent"
+ android:layout_weight="4" />
+ </LinearLayout>
+
+</androidx.car.moderator.SpeedBumpView>
+
+
diff --git a/tests/UxRestrictionsSample/src/com/google/android/car/uxr/sample/MainActivity.java b/tests/UxRestrictionsSample/src/com/google/android/car/uxr/sample/MainActivity.java
new file mode 100644
index 0000000000..9bb6fc9847
--- /dev/null
+++ b/tests/UxRestrictionsSample/src/com/google/android/car/uxr/sample/MainActivity.java
@@ -0,0 +1,192 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.google.android.car.uxr.sample;
+
+import static android.os.SystemClock.elapsedRealtimeNanos;
+
+import android.annotation.DrawableRes;
+import android.app.Activity;
+import android.car.Car;
+import android.car.CarNotConnectedException;
+import android.car.drivingstate.CarUxRestrictions;
+import android.car.drivingstate.CarUxRestrictionsManager;
+import android.content.ComponentName;
+import android.content.ServiceConnection;
+import android.os.Bundle;
+import android.os.IBinder;
+import android.util.Log;
+import android.widget.Button;
+import android.widget.TextView;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import androidx.car.widget.ListItem;
+import androidx.car.widget.ListItemAdapter;
+import androidx.car.widget.ListItemProvider;
+import androidx.car.widget.PagedListView;
+import androidx.car.widget.TextListItem;
+
+/**
+ * Sample app that uses components in car support library to demonstrate Car drivingstate UXR status.
+ */
+public class MainActivity extends Activity {
+ public static final String TAG = "drivingstate";
+
+ private Car mCar;
+ private CarUxRestrictionsManager mCarUxRestrictionsManager;
+ private TextView mUxrStatus;
+ private Button mToggleButton;
+ private PagedListView mPagedListView;
+
+ private final ServiceConnection mCarConnectionListener =
+ new ServiceConnection() {
+ @Override
+ public void onServiceConnected(ComponentName name, IBinder iBinder) {
+ Log.d(TAG, "Connected to " + name.flattenToString());
+ // Get a UXR manager
+ try {
+ mCarUxRestrictionsManager = (CarUxRestrictionsManager) mCar.getCarManager(
+ Car.CAR_UX_RESTRICTION_SERVICE);
+
+ } catch (CarNotConnectedException e) {
+ Log.e(TAG, "Failed to get a connection", e);
+ }
+
+ // Register listener
+ try {
+ mCarUxRestrictionsManager.registerListener(uxrChangeListener);
+ } catch (CarNotConnectedException e) {
+ e.printStackTrace();
+ }
+
+ // Show current status
+ try {
+ updateWidgetText(mCarUxRestrictionsManager.getCurrentCarUxRestrictions());
+ } catch (CarNotConnectedException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ @Override
+ public void onServiceDisconnected(ComponentName name) {
+ Log.d(TAG, "Disconnected from " + name.flattenToString());
+ try {
+ mCarUxRestrictionsManager.unregisterListener();
+ } catch (CarNotConnectedException e) {
+ e.printStackTrace();
+ }
+
+ mCar = null;
+ mCarUxRestrictionsManager = null;
+ }
+ };
+
+ private void updateWidgetText(CarUxRestrictions restrictions) {
+ mToggleButton.setText(
+ restrictions.isRequiresDistractionOptimization()
+ ? "Switch to Park" : "Switch to Drive");
+ mUxrStatus.setText(
+ restrictions.isRequiresDistractionOptimization()
+ ? "Requires Distraction Optimization" : "No restriction");
+
+ mToggleButton.requestLayout();
+ mUxrStatus.requestLayout();
+ }
+
+ private CarUxRestrictionsManager.onUxRestrictionsChangedListener uxrChangeListener = restrictions -> {
+ updateWidgetText(restrictions);
+ };
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.main_activity);
+
+ mUxrStatus = findViewById(R.id.uxr_status);
+ mToggleButton = findViewById(R.id.toggle_status);
+ mPagedListView = findViewById(R.id.paged_list_view);
+
+ setUpPagedListView();
+
+ final boolean[] requiresDO = {false};
+ mToggleButton.setOnClickListener(v -> {
+ // Create a mock UXR change.
+ requiresDO[0] = !requiresDO[0];
+ CarUxRestrictions restrictions = new CarUxRestrictions(
+ requiresDO[0],
+ requiresDO[0]
+ ? CarUxRestrictions.UX_RESTRICTIONS_FULLY_RESTRICTED
+ : CarUxRestrictions.UX_RESTRICTIONS_UNRESTRICTED,
+ elapsedRealtimeNanos());
+ updateWidgetText(restrictions);
+ });
+
+ // Connect to car service
+ mCar = Car.createCar(this, mCarConnectionListener);
+ mCar.connect();
+ }
+
+ private void setUpPagedListView() {
+ ListItemAdapter adapter = new ListItemAdapter(this, populateData());
+ mPagedListView.setAdapter(adapter);
+ }
+
+ private ListItemProvider populateData() {
+ List<ListItem> items = new ArrayList<>();
+ items.add(createMessage(android.R.drawable.ic_menu_myplaces, "alice",
+ "i have a really important message but it may hinder your ability to drive. "));
+
+ items.add(createMessage(android.R.drawable.ic_menu_myplaces, "bob",
+ "hey this is a really long message that i have always wanted to say. but before " +
+ "saying it i feel it's only appropriate if i lay some groundwork for it. "));
+ items.add(createMessage(android.R.drawable.ic_menu_myplaces, "mom",
+ "i think you are the best. i think you are the best. i think you are the best. " +
+ "i think you are the best. i think you are the best. i think you are the best. " +
+ "i think you are the best. i think you are the best. i think you are the best. " +
+ "i think you are the best. i think you are the best. i think you are the best. " +
+ "i think you are the best. i think you are the best. i think you are the best. " +
+ "i think you are the best. i think you are the best. "));
+ items.add(createMessage(android.R.drawable.ic_menu_myplaces, "john", "hello world"));
+ items.add(createMessage(android.R.drawable.ic_menu_myplaces, "jeremy",
+ "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor " +
+ "incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, " +
+ "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo " +
+ "consequat. Duis aute irure dolor in reprehenderit in voluptate velit " +
+ "esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat " +
+ "cupidatat non proident, sunt in culpa qui officia deserunt mollit " +
+ "anim id est laborum."));
+ return new ListItemProvider.ListProvider(items);
+ }
+
+ private TextListItem createMessage(@DrawableRes int profile, String contact, String message) {
+ TextListItem item = new TextListItem(this);
+ item.setPrimaryActionIcon(profile, false /* useLargeIcon */);
+ item.setTitle(contact);
+ item.setBody(message);
+ item.setSupplementalIcon(android.R.drawable.stat_notify_chat, false);
+ return item;
+ }
+
+ @Override
+ protected void onDestroy() {
+ if (mCar != null) {
+ mCar.disconnect();
+ }
+ }
+}
+