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.android.car.settings.common; 17 18 import android.app.Activity; 19 import android.car.Car; 20 import android.car.drivingstate.CarUxRestrictions; 21 import android.car.drivingstate.CarUxRestrictionsManager; 22 import android.content.Context; 23 24 import androidx.annotation.Nullable; 25 26 /** 27 * Class that helps registering {@link CarUxRestrictionsManager.OnUxRestrictionsChangedListener} and 28 * managing car connection. 29 */ 30 public class CarUxRestrictionsHelper { 31 private static final Logger LOG = new Logger(CarUxRestrictionsHelper.class); 32 33 // mCar is created in the constructor, but can be null if connection to the car is not 34 // successful. 35 @Nullable private Car mCar; 36 @Nullable private CarUxRestrictionsManager mCarUxRestrictionsManager; 37 38 private final CarUxRestrictionsManager.OnUxRestrictionsChangedListener mListener; 39 CarUxRestrictionsHelper(Context context, CarUxRestrictionsManager.OnUxRestrictionsChangedListener listener)40 public CarUxRestrictionsHelper(Context context, 41 CarUxRestrictionsManager.OnUxRestrictionsChangedListener listener) { 42 if (listener == null) { 43 throw new IllegalArgumentException("Listener cannot be null."); 44 } 45 mListener = listener; 46 Car.createCar(context, /* handler= */ null, Car.CAR_WAIT_TIMEOUT_WAIT_FOREVER, 47 (car, ready) -> { 48 if (!ready) { 49 return; 50 } 51 mCar = car; 52 mCarUxRestrictionsManager = (CarUxRestrictionsManager) car.getCarManager( 53 Car.CAR_UX_RESTRICTION_SERVICE); 54 if (mCarUxRestrictionsManager != null) { 55 mCarUxRestrictionsManager.registerListener(mListener); 56 mListener.onUxRestrictionsChanged( 57 mCarUxRestrictionsManager.getCurrentCarUxRestrictions()); 58 } 59 }); 60 } 61 62 /** 63 * Stops monitoring any changes in {@link CarUxRestrictions}. 64 * 65 * <p>This method should be called from {@code Activity}'s {@link Activity#onDestroy()}, or at 66 * the time of this adapter being discarded. 67 */ destroy()68 public void destroy() { 69 try { 70 if (mCar != null && mCar.isConnected()) { 71 mCar.disconnect(); 72 } 73 } catch (IllegalStateException e) { 74 // Do nothing. 75 LOG.w("destroy(); cannot disconnect from Car"); 76 } 77 if (mCarUxRestrictionsManager != null) { 78 mCarUxRestrictionsManager.unregisterListener(); 79 mCarUxRestrictionsManager = null; 80 } 81 } 82 83 /** 84 * Checks if UX_RESTRICTIONS_NO_SETUP is set or not. 85 */ isNoSetup(CarUxRestrictions carUxRestrictions)86 public static boolean isNoSetup(CarUxRestrictions carUxRestrictions) { 87 return (carUxRestrictions.getActiveRestrictions() 88 & CarUxRestrictions.UX_RESTRICTIONS_NO_SETUP) 89 == CarUxRestrictions.UX_RESTRICTIONS_NO_SETUP; 90 } 91 92 /** 93 * Get the current CarUxRestrictions 94 */ getCarUxRestrictions()95 public CarUxRestrictions getCarUxRestrictions() { 96 if (mCarUxRestrictionsManager == null) { 97 return null; 98 } 99 return mCarUxRestrictionsManager.getCurrentCarUxRestrictions(); 100 } 101 } 102