Vitalii Tomkiv | e836ac3 | 2016-04-05 17:26:41 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.car; |
| 17 | |
| 18 | import android.app.Notification; |
| 19 | import android.app.NotificationManager; |
| 20 | import android.content.Context; |
| 21 | import android.os.Build; |
| 22 | import android.util.Log; |
| 23 | |
| 24 | import com.android.internal.annotations.GuardedBy; |
| 25 | |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 26 | import java.util.HashSet; |
| 27 | import java.util.Set; |
| 28 | |
Vitalii Tomkiv | e836ac3 | 2016-04-05 17:26:41 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Class used to notify user about CAN bus failure. |
| 31 | */ |
| 32 | final 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 Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 40 | // 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 Tomkiv | e836ac3 | 2016-04-05 17:26:41 -0700 | [diff] [blame] | 42 | @GuardedBy("this") |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 43 | private final Set<Object> mReportedObjects = new HashSet<>(); |
Vitalii Tomkiv | e836ac3 | 2016-04-05 17:26:41 -0700 | [diff] [blame] | 44 | |
| 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 Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 54 | 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 Tomkiv | e836ac3 | 2016-04-05 17:26:41 -0700 | [diff] [blame] | 64 | synchronized (this) { |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 65 | boolean changed = failed |
| 66 | ? mReportedObjects.add(sender) : mReportedObjects.remove(sender); |
| 67 | |
| 68 | if (!changed) { |
Vitalii Tomkiv | e836ac3 | 2016-04-05 17:26:41 -0700 | [diff] [blame] | 69 | return; |
| 70 | } |
Vitalii Tomkiv | e836ac3 | 2016-04-05 17:26:41 -0700 | [diff] [blame] | 71 | |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 72 | shouldShowNotification = !mReportedObjects.isEmpty(); |
| 73 | } |
| 74 | Log.i(TAG, "Changing CAN bus failure state to " + shouldShowNotification); |
| 75 | |
| 76 | if (shouldShowNotification) { |
Vitalii Tomkiv | e836ac3 | 2016-04-05 17:26:41 -0700 | [diff] [blame] | 77 | showNotification(); |
| 78 | } else { |
| 79 | hideNotification(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | private void showNotification() { |
| 84 | if (IS_RELEASE_BUILD) { |
Keun-young Park | f921520 | 2016-10-10 12:34:08 -0700 | [diff] [blame] | 85 | // TODO: for user, we should show message to take car to the dealer. bug:32096297 |
Vitalii Tomkiv | e836ac3 | 2016-04-05 17:26:41 -0700 | [diff] [blame] | 86 | 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 Park | f921520 | 2016-10-10 12:34:08 -0700 | [diff] [blame] | 105 | // TODO: for user, we should show message to take car to the dealer. bug:32096297 |
Vitalii Tomkiv | e836ac3 | 2016-04-05 17:26:41 -0700 | [diff] [blame] | 106 | return; |
| 107 | } |
| 108 | mNotificationManager.cancel(TAG, NOTIFICATION_ID); |
| 109 | } |
| 110 | } |