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 | |
| 26 | /** |
| 27 | * Class used to notify user about CAN bus failure. |
| 28 | */ |
| 29 | final class CanBusErrorNotifier { |
| 30 | private static final String TAG = CarLog.TAG_CAN_BUS + ".ERROR.NOTIFIER"; |
| 31 | private static final int NOTIFICATION_ID = 1; |
| 32 | private static final boolean IS_RELEASE_BUILD = "user".equals(Build.TYPE); |
| 33 | |
| 34 | private final Context mContext; |
| 35 | private final NotificationManager mNotificationManager; |
| 36 | |
| 37 | @GuardedBy("this") |
| 38 | private boolean mFailed; |
| 39 | |
| 40 | @GuardedBy("this") |
| 41 | private Notification mNotification; |
| 42 | |
| 43 | CanBusErrorNotifier(Context context) { |
| 44 | mNotificationManager = (NotificationManager) context.getSystemService( |
| 45 | Context.NOTIFICATION_SERVICE); |
| 46 | mContext = context; |
| 47 | } |
| 48 | |
| 49 | public void setCanBusFailure(boolean failed) { |
| 50 | synchronized (this) { |
| 51 | if (mFailed == failed) { |
| 52 | return; |
| 53 | } |
| 54 | mFailed = failed; |
| 55 | } |
| 56 | Log.i(TAG, "Changing CAN bus failure state to " + failed); |
| 57 | |
| 58 | if (failed) { |
| 59 | showNotification(); |
| 60 | } else { |
| 61 | hideNotification(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | private void showNotification() { |
| 66 | if (IS_RELEASE_BUILD) { |
| 67 | // TODO: for release build we should show message to take car to the dealer. |
| 68 | return; |
| 69 | } |
| 70 | Notification notification; |
| 71 | synchronized (this) { |
| 72 | if (mNotification == null) { |
| 73 | mNotification = new Notification.Builder(mContext) |
| 74 | .setContentTitle(mContext.getString(R.string.car_can_bus_failure)) |
| 75 | .setContentText(mContext.getString(R.string.car_can_bus_failure_desc)) |
| 76 | .setSmallIcon(R.drawable.car_ic_error) |
| 77 | .setOngoing(true) |
| 78 | .build(); |
| 79 | } |
| 80 | notification = mNotification; |
| 81 | } |
| 82 | mNotificationManager.notify(TAG, NOTIFICATION_ID, notification); |
| 83 | } |
| 84 | |
| 85 | private void hideNotification() { |
| 86 | if (IS_RELEASE_BUILD) { |
| 87 | // TODO: for release build we should show message to take car to the dealer. |
| 88 | return; |
| 89 | } |
| 90 | mNotificationManager.cancel(TAG, NOTIFICATION_ID); |
| 91 | } |
| 92 | } |