blob: 7caa1a8a41615525d1618c747c09ae937af2ad3c [file] [log] [blame]
Vitalii Tomkive836ac32016-04-05 17:26:41 -07001/*
2 * Copyright (C) 2015 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;
17
18import android.app.Notification;
19import android.app.NotificationManager;
20import android.content.Context;
21import android.os.Build;
22import android.util.Log;
23
24import com.android.internal.annotations.GuardedBy;
25
Pavel Maltsevec83b632017-01-05 15:10:55 -080026import java.util.HashSet;
27import java.util.Set;
28
Vitalii Tomkive836ac32016-04-05 17:26:41 -070029/**
30 * Class used to notify user about CAN bus failure.
31 */
32final class CanBusErrorNotifier {
33 private static final String TAG = CarLog.TAG_CAN_BUS + ".ERROR.NOTIFIER";
34 private static final int NOTIFICATION_ID = 1;
35 private static final boolean IS_RELEASE_BUILD = "user".equals(Build.TYPE);
36
37 private final Context mContext;
38 private final NotificationManager mNotificationManager;
39
Pavel Maltsevec83b632017-01-05 15:10:55 -080040 // Contains a set of objects that reported failure. The notification will be hidden only when
41 // this set is empty (all reported objects are in love and peace with the vehicle).
Vitalii Tomkive836ac32016-04-05 17:26:41 -070042 @GuardedBy("this")
Pavel Maltsevec83b632017-01-05 15:10:55 -080043 private final Set<Object> mReportedObjects = new HashSet<>();
Vitalii Tomkive836ac32016-04-05 17:26:41 -070044
45 @GuardedBy("this")
46 private Notification mNotification;
47
48 CanBusErrorNotifier(Context context) {
49 mNotificationManager = (NotificationManager) context.getSystemService(
50 Context.NOTIFICATION_SERVICE);
51 mContext = context;
52 }
53
Pavel Maltsevec83b632017-01-05 15:10:55 -080054 public void removeFailureReport(Object sender) {
55 setCanBusFailure(false, sender);
56 }
57
58 public void reportFailure(Object sender) {
59 setCanBusFailure(true, sender);
60 }
61
62 private void setCanBusFailure(boolean failed, Object sender) {
63 boolean shouldShowNotification;
Vitalii Tomkive836ac32016-04-05 17:26:41 -070064 synchronized (this) {
Pavel Maltsevec83b632017-01-05 15:10:55 -080065 boolean changed = failed
66 ? mReportedObjects.add(sender) : mReportedObjects.remove(sender);
67
68 if (!changed) {
Vitalii Tomkive836ac32016-04-05 17:26:41 -070069 return;
70 }
Vitalii Tomkive836ac32016-04-05 17:26:41 -070071
Pavel Maltsevec83b632017-01-05 15:10:55 -080072 shouldShowNotification = !mReportedObjects.isEmpty();
73 }
74 Log.i(TAG, "Changing CAN bus failure state to " + shouldShowNotification);
75
76 if (shouldShowNotification) {
Vitalii Tomkive836ac32016-04-05 17:26:41 -070077 showNotification();
78 } else {
79 hideNotification();
80 }
81 }
82
83 private void showNotification() {
84 if (IS_RELEASE_BUILD) {
Keun-young Parkf9215202016-10-10 12:34:08 -070085 // TODO: for user, we should show message to take car to the dealer. bug:32096297
Vitalii Tomkive836ac32016-04-05 17:26:41 -070086 return;
87 }
88 Notification notification;
89 synchronized (this) {
90 if (mNotification == null) {
91 mNotification = new Notification.Builder(mContext)
92 .setContentTitle(mContext.getString(R.string.car_can_bus_failure))
93 .setContentText(mContext.getString(R.string.car_can_bus_failure_desc))
94 .setSmallIcon(R.drawable.car_ic_error)
95 .setOngoing(true)
96 .build();
97 }
98 notification = mNotification;
99 }
100 mNotificationManager.notify(TAG, NOTIFICATION_ID, notification);
101 }
102
103 private void hideNotification() {
104 if (IS_RELEASE_BUILD) {
Keun-young Parkf9215202016-10-10 12:34:08 -0700105 // TODO: for user, we should show message to take car to the dealer. bug:32096297
Vitalii Tomkive836ac32016-04-05 17:26:41 -0700106 return;
107 }
108 mNotificationManager.cancel(TAG, NOTIFICATION_ID);
109 }
110}