• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 final 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         mCar = Car.createCar(context);
47         mCarUxRestrictionsManager = (CarUxRestrictionsManager)
48                 mCar.getCarManager(Car.CAR_UX_RESTRICTION_SERVICE);
49         if (mCarUxRestrictionsManager != null) {
50             mCarUxRestrictionsManager.registerListener(mListener);
51             mListener.onUxRestrictionsChanged(
52                     mCarUxRestrictionsManager.getCurrentCarUxRestrictions());
53         }
54     }
55 
56     /**
57      * Stops monitoring any changes in {@link CarUxRestrictions}.
58      *
59      * <p>This method should be called from {@code Activity}'s {@link Activity#onDestroy()}, or at
60      * the time of this adapter being discarded.
61      */
destroy()62     public void destroy() {
63         try {
64             if (mCar != null && mCar.isConnected()) {
65                 mCar.disconnect();
66             }
67         } catch (IllegalStateException e) {
68             // Do nothing.
69             LOG.w("destroy(); cannot disconnect from Car");
70         }
71         if (mCarUxRestrictionsManager != null) {
72             mCarUxRestrictionsManager.unregisterListener();
73             mCarUxRestrictionsManager = null;
74         }
75     }
76 
77     /**
78      * Checks if UX_RESTRICTIONS_NO_SETUP is set or not.
79      */
isNoSetup(CarUxRestrictions carUxRestrictions)80     public static boolean isNoSetup(CarUxRestrictions carUxRestrictions) {
81         return (carUxRestrictions.getActiveRestrictions()
82                 & CarUxRestrictions.UX_RESTRICTIONS_NO_SETUP)
83                 == CarUxRestrictions.UX_RESTRICTIONS_NO_SETUP;
84     }
85 }
86