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