Home | History | Annotate | Download | only in sample
      1 /*
      2  * Copyright (C) 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.google.android.car.uxr.sample;
     17 
     18 import android.annotation.DrawableRes;
     19 import android.app.Activity;
     20 import android.car.Car;
     21 import android.car.CarNotConnectedException;
     22 import android.car.content.pm.CarPackageManager;
     23 import android.car.drivingstate.CarDrivingStateEvent;
     24 import android.car.drivingstate.CarDrivingStateManager;
     25 import android.car.drivingstate.CarUxRestrictions;
     26 import android.car.drivingstate.CarUxRestrictionsManager;
     27 import android.content.ComponentName;
     28 import android.content.ServiceConnection;
     29 import android.os.Bundle;
     30 import android.os.IBinder;
     31 import android.util.Log;
     32 import android.widget.Button;
     33 import android.widget.TextView;
     34 
     35 import androidx.car.widget.ListItem;
     36 import androidx.car.widget.ListItemAdapter;
     37 import androidx.car.widget.ListItemProvider;
     38 import androidx.car.widget.PagedListView;
     39 import androidx.car.widget.TextListItem;
     40 
     41 import java.util.ArrayList;
     42 import java.util.List;
     43 
     44 /**
     45  * Sample app that uses components in car support library to demonstrate Car drivingstate UXR
     46  * status.
     47  */
     48 public class MainActivity extends Activity {
     49     public static final String TAG = "drivingstate";
     50 
     51     private Car mCar;
     52     private CarDrivingStateManager mCarDrivingStateManager;
     53     private CarUxRestrictionsManager mCarUxRestrictionsManager;
     54     private CarPackageManager mCarPackageManager;
     55     private TextView mDrvStatus;
     56     private TextView mDistractionOptStatus;
     57     private TextView mUxrStatus;
     58     private Button mToggleButton;
     59     private boolean mEnableUxR;
     60     private PagedListView mPagedListView;
     61 
     62     private final ServiceConnection mCarConnectionListener =
     63             new ServiceConnection() {
     64                 @Override
     65                 public void onServiceConnected(ComponentName name, IBinder iBinder) {
     66                     Log.d(TAG, "Connected to " + name.flattenToString());
     67                     // Get Driving State & UXR manager
     68                     try {
     69                         mCarDrivingStateManager = (CarDrivingStateManager) mCar.getCarManager(
     70                                 Car.CAR_DRIVING_STATE_SERVICE);
     71                         mCarUxRestrictionsManager = (CarUxRestrictionsManager) mCar.getCarManager(
     72                                 Car.CAR_UX_RESTRICTION_SERVICE);
     73                         mCarPackageManager = (CarPackageManager) mCar.getCarManager(
     74                                 Car.PACKAGE_SERVICE);
     75 
     76                         if (mCarDrivingStateManager != null) {
     77                             mCarDrivingStateManager.registerListener(mDrvStateChangeListener);
     78                             updateDrivingStateText(
     79                                     mCarDrivingStateManager.getCurrentCarDrivingState());
     80                         }
     81                         if (mCarUxRestrictionsManager != null) {
     82                             mCarUxRestrictionsManager.registerListener(mUxRChangeListener);
     83                             updateUxRText(mCarUxRestrictionsManager.getCurrentCarUxRestrictions());
     84                         }
     85 
     86                     } catch (CarNotConnectedException e) {
     87                         Log.e(TAG, "Failed to get a connection", e);
     88                     }
     89                 }
     90 
     91                 @Override
     92                 public void onServiceDisconnected(ComponentName name) {
     93                     Log.d(TAG, "Disconnected from " + name.flattenToString());
     94                     mCarDrivingStateManager = null;
     95                     mCarUxRestrictionsManager = null;
     96                     mCarPackageManager = null;
     97                 }
     98             };
     99 
    100     private void updateUxRText(CarUxRestrictions restrictions) {
    101         mDistractionOptStatus.setText(
    102                 restrictions.isRequiresDistractionOptimization()
    103                         ? "Requires Distraction Optimization"
    104                         : "No Distraction Optimization required");
    105 
    106         mUxrStatus.setText("Active Restrictions : 0x"
    107                 + Integer.toHexString(restrictions.getActiveRestrictions()));
    108 
    109         mDistractionOptStatus.requestLayout();
    110         mUxrStatus.requestLayout();
    111     }
    112 
    113     private void updateToggleUxREnable() {
    114         if (mCarPackageManager == null) {
    115             return;
    116         }
    117         mCarPackageManager.setEnableActivityBlocking(mEnableUxR);
    118         if (mEnableUxR) {
    119             mToggleButton.setText("Disable UX Restrictions");
    120         } else {
    121             mToggleButton.setText("Enable UX Restrictions");
    122         }
    123         mEnableUxR = !mEnableUxR;
    124         mToggleButton.requestLayout();
    125 
    126     }
    127 
    128     private void updateDrivingStateText(CarDrivingStateEvent state) {
    129         if (state == null) {
    130             return;
    131         }
    132         String displayText;
    133         switch (state.eventValue) {
    134             case CarDrivingStateEvent.DRIVING_STATE_PARKED:
    135                 displayText = "Parked";
    136                 break;
    137             case CarDrivingStateEvent.DRIVING_STATE_IDLING:
    138                 displayText = "Idling";
    139                 break;
    140             case CarDrivingStateEvent.DRIVING_STATE_MOVING:
    141                 displayText = "Moving";
    142                 break;
    143             default:
    144                 displayText = "Unknown";
    145         }
    146         mDrvStatus.setText("Driving State: " + displayText);
    147         mDrvStatus.requestLayout();
    148     }
    149 
    150     private CarUxRestrictionsManager.OnUxRestrictionsChangedListener mUxRChangeListener =
    151             this::updateUxRText;
    152 
    153 
    154     private CarDrivingStateManager.CarDrivingStateEventListener mDrvStateChangeListener =
    155             this::updateDrivingStateText;
    156 
    157     @Override
    158     public void onCreate(Bundle savedInstanceState) {
    159         super.onCreate(savedInstanceState);
    160         setContentView(R.layout.main_activity);
    161 
    162         mDrvStatus = findViewById(R.id.driving_state);
    163         mDistractionOptStatus = findViewById(R.id.do_status);
    164         mUxrStatus = findViewById(R.id.uxr_status);
    165         mToggleButton = findViewById(R.id.toggle_status);
    166         mPagedListView = findViewById(R.id.paged_list_view);
    167 
    168         setUpPagedListView();
    169 
    170         mToggleButton.setOnClickListener(v -> {
    171             updateToggleUxREnable();
    172         });
    173 
    174         // Connect to car service
    175         mCar = Car.createCar(this, mCarConnectionListener);
    176         mCar.connect();
    177     }
    178 
    179     private void setUpPagedListView() {
    180         ListItemAdapter adapter = new ListItemAdapter(this, populateData());
    181         mPagedListView.setAdapter(adapter);
    182     }
    183 
    184     private ListItemProvider populateData() {
    185         List<ListItem> items = new ArrayList<>();
    186         items.add(createMessage(android.R.drawable.ic_menu_myplaces, "alice",
    187                 "i have a really important message but it may hinder your ability to drive. "));
    188 
    189         items.add(createMessage(android.R.drawable.ic_menu_myplaces, "bob",
    190                 "hey this is a really long message that i have always wanted to say. but before " +
    191                         "saying it i feel it's only appropriate if i lay some groundwork for it. "
    192                         + ""));
    193         items.add(createMessage(android.R.drawable.ic_menu_myplaces, "mom",
    194                 "i think you are the best. i think you are the best. i think you are the best. " +
    195                         "i think you are the best. i think you are the best. i think you are the "
    196                         + "best. "
    197                         +
    198                         "i think you are the best. i think you are the best. i think you are the "
    199                         + "best. "
    200                         +
    201                         "i think you are the best. i think you are the best. i think you are the "
    202                         + "best. "
    203                         +
    204                         "i think you are the best. i think you are the best. i think you are the "
    205                         + "best. "
    206                         +
    207                         "i think you are the best. i think you are the best. "));
    208         items.add(createMessage(android.R.drawable.ic_menu_myplaces, "john", "hello world"));
    209         items.add(createMessage(android.R.drawable.ic_menu_myplaces, "jeremy",
    210                 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor " +
    211                         "incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, " +
    212                         "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo " +
    213                         "consequat. Duis aute irure dolor in reprehenderit in voluptate velit " +
    214                         "esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat " +
    215                         "cupidatat non proident, sunt in culpa qui officia deserunt mollit " +
    216                         "anim id est laborum."));
    217         return new ListItemProvider.ListProvider(items);
    218     }
    219 
    220     private TextListItem createMessage(@DrawableRes int profile, String contact, String message) {
    221         TextListItem item = new TextListItem(this);
    222         item.setPrimaryActionIcon(profile, false /* useLargeIcon */);
    223         item.setTitle(contact);
    224         item.setBody(message);
    225         item.setSupplementalIcon(android.R.drawable.stat_notify_chat, false);
    226         return item;
    227     }
    228 
    229     @Override
    230     protected void onDestroy() {
    231         try {
    232             if (mCarUxRestrictionsManager != null) {
    233                 mCarUxRestrictionsManager.unregisterListener();
    234             }
    235             if (mCarDrivingStateManager != null) {
    236                 mCarDrivingStateManager.unregisterListener();
    237             }
    238         } catch (CarNotConnectedException e) {
    239             Log.e(TAG, "Error unregistering listeners", e);
    240         }
    241         if (mCar != null) {
    242             mCar.disconnect();
    243         }
    244     }
    245 }
    246 
    247