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