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.CarNotConnectedException; 21 import android.car.drivingstate.CarUxRestrictions; 22 import android.car.drivingstate.CarUxRestrictionsManager; 23 import android.content.ComponentName; 24 import android.content.Context; 25 import android.content.ServiceConnection; 26 import android.os.IBinder; 27 import android.support.annotation.Nullable; 28 29 /** 30 * Class that helps registering {@link CarUxRestrictionsManager.OnUxRestrictionsChangedListener} and 31 * managing car connection. 32 */ 33 public class CarUxRestrictionsHelper { 34 private static final Logger LOG = new Logger(CarUxRestrictionsHelper.class); 35 36 // mCar is created in the constructor, but can be null if connection to the car is not 37 // successful. 38 @Nullable private final Car mCar; 39 @Nullable private CarUxRestrictionsManager mCarUxRestrictionsManager; 40 41 private final CarUxRestrictionsManager.OnUxRestrictionsChangedListener mListener; 42 43 public CarUxRestrictionsHelper(Context context, 44 CarUxRestrictionsManager.OnUxRestrictionsChangedListener listener) { 45 if (listener == null) { 46 throw new IllegalArgumentException("Listener cannot be null."); 47 } 48 mListener = listener; 49 mCar = Car.createCar(context, mServiceConnection); 50 }; 51 52 /** 53 * Starts monitoring any changes in {@link CarUxRestrictions}. 54 * 55 * <p>This method can be called from {@code Activity}'s {@link Activity#onStart()}, or at the 56 * time of construction. 57 * 58 * <p>This method must be accompanied with a matching {@link #stop()} to avoid leak. 59 */ 60 public void start() { 61 try { 62 if (mCar != null && !mCar.isConnected()) { 63 mCar.connect(); 64 } 65 } catch (IllegalStateException e) { 66 // Do nothing. 67 LOG.w("start(); cannot connect to Car"); 68 } 69 } 70 71 /** 72 * Stops monitoring any changes in {@link CarUxRestrictions}. 73 * 74 * <p>This method should be called from {@code Activity}'s {@link Activity#onStop()}, or at the 75 * time of this adapter being discarded. 76 */ 77 public void stop() { 78 try { 79 if (mCar != null && mCar.isConnected()) { 80 mCar.disconnect(); 81 } 82 } catch (IllegalStateException e) { 83 // Do nothing. 84 LOG.w("stop(); cannot disconnect from Car"); 85 } 86 } 87 88 /** 89 * Checks if UX_RESTRICTIONS_NO_SETUP is set or not. 90 */ 91 public static boolean isNoSetup(CarUxRestrictions carUxRestrictions) { 92 return (carUxRestrictions.getActiveRestrictions() 93 & CarUxRestrictions.UX_RESTRICTIONS_NO_SETUP) 94 == CarUxRestrictions.UX_RESTRICTIONS_NO_SETUP; 95 } 96 97 private final ServiceConnection mServiceConnection = new ServiceConnection() { 98 @Override 99 public void onServiceConnected(ComponentName name, IBinder service) { 100 try { 101 mCarUxRestrictionsManager = (CarUxRestrictionsManager) 102 mCar.getCarManager(Car.CAR_UX_RESTRICTION_SERVICE); 103 mCarUxRestrictionsManager.registerListener(mListener); 104 105 mListener.onUxRestrictionsChanged( 106 mCarUxRestrictionsManager.getCurrentCarUxRestrictions()); 107 } catch (CarNotConnectedException e) { 108 e.printStackTrace(); 109 } 110 } 111 112 @Override 113 public void onServiceDisconnected(ComponentName name) { 114 try { 115 mCarUxRestrictionsManager.unregisterListener(); 116 mCarUxRestrictionsManager = null; 117 } catch (CarNotConnectedException e) { 118 e.printStackTrace(); 119 } 120 } 121 }; 122 } 123