felipeal | cf47557 | 2020-04-08 09:07:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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.google.android.car.userswitchmonitor; |
| 17 | |
| 18 | import android.app.Notification; |
| 19 | import android.app.NotificationChannel; |
| 20 | import android.app.NotificationManager; |
| 21 | import android.app.Service; |
| 22 | import android.car.Car; |
| 23 | import android.car.user.CarUserManager; |
felipeal | 3ce0135 | 2020-04-14 15:43:09 -0700 | [diff] [blame] | 24 | import android.car.user.CarUserManager.UserLifecycleEvent; |
felipeal | cf47557 | 2020-04-08 09:07:02 -0700 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.os.IBinder; |
| 28 | import android.util.Log; |
| 29 | |
felipeal | 3ce0135 | 2020-04-14 15:43:09 -0700 | [diff] [blame] | 30 | import java.io.FileDescriptor; |
| 31 | import java.io.PrintWriter; |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.List; |
| 34 | |
felipeal | cf47557 | 2020-04-08 09:07:02 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Service that users {@link CarUserManager.UserLifecycleEvent UserLifecycleEvents} to monitor |
| 37 | * user switches. |
| 38 | * |
| 39 | */ |
| 40 | public final class UserSwitchMonitorService extends Service { |
| 41 | |
felipeal | 3ce0135 | 2020-04-14 15:43:09 -0700 | [diff] [blame] | 42 | static final String TAG = "UserSwitchMonitor"; |
felipeal | cf47557 | 2020-04-08 09:07:02 -0700 | [diff] [blame] | 43 | |
felipeal | 3ce0135 | 2020-04-14 15:43:09 -0700 | [diff] [blame] | 44 | private final Object mLock = new Object(); |
felipeal | cf47557 | 2020-04-08 09:07:02 -0700 | [diff] [blame] | 45 | |
felipeal | 3ce0135 | 2020-04-14 15:43:09 -0700 | [diff] [blame] | 46 | private final int mUserId = android.os.Process.myUserHandle().getIdentifier(); |
| 47 | |
| 48 | private final List<UserLifecycleEvent> mEvents = new ArrayList<>(); |
| 49 | |
| 50 | private final CarUserManager.UserLifecycleListener mListener = (e) -> { |
| 51 | Log.d(TAG, "onEvent(" + mUserId + "): " + e); |
| 52 | synchronized (mLock) { |
| 53 | mEvents.add(e); |
| 54 | } |
| 55 | }; |
felipeal | cf47557 | 2020-04-08 09:07:02 -0700 | [diff] [blame] | 56 | |
Eric Jeong | aa5a652 | 2020-05-27 18:19:55 -0700 | [diff] [blame^] | 57 | private Context mContext; |
| 58 | private Car mCar; |
felipeal | cf47557 | 2020-04-08 09:07:02 -0700 | [diff] [blame] | 59 | private CarUserManager mCarUserManager; |
Eric Jeong | aa5a652 | 2020-05-27 18:19:55 -0700 | [diff] [blame^] | 60 | private NotificationManager mNotificationManager; |
| 61 | |
| 62 | @Override |
| 63 | public void onCreate() { |
| 64 | mContext = getApplicationContext(); |
| 65 | mCar = Car.createCar(mContext); |
| 66 | mCarUserManager = (CarUserManager) mCar.getCarManager(Car.CAR_USER_SERVICE); |
| 67 | mCarUserManager.addListener((r)-> r.run(), mListener); |
| 68 | |
| 69 | mNotificationManager = mContext.getSystemService(NotificationManager.class); |
| 70 | } |
felipeal | cf47557 | 2020-04-08 09:07:02 -0700 | [diff] [blame] | 71 | |
| 72 | @Override |
| 73 | public int onStartCommand(Intent intent, int flags, int startId) { |
felipeal | 3ce0135 | 2020-04-14 15:43:09 -0700 | [diff] [blame] | 74 | Log.d(TAG, "onStartCommand(" + mUserId + "): " + intent); |
felipeal | cf47557 | 2020-04-08 09:07:02 -0700 | [diff] [blame] | 75 | |
felipeal | cf47557 | 2020-04-08 09:07:02 -0700 | [diff] [blame] | 76 | String channelId = "4815162342"; |
| 77 | String name = "UserSwitchMonitor"; |
| 78 | NotificationChannel channel = new NotificationChannel(channelId, name, |
| 79 | NotificationManager.IMPORTANCE_MIN); |
Eric Jeong | aa5a652 | 2020-05-27 18:19:55 -0700 | [diff] [blame^] | 80 | mNotificationManager.createNotificationChannel(channel); |
felipeal | cf47557 | 2020-04-08 09:07:02 -0700 | [diff] [blame] | 81 | |
| 82 | startForeground(startId, |
Eric Jeong | aa5a652 | 2020-05-27 18:19:55 -0700 | [diff] [blame^] | 83 | new Notification.Builder(mContext, channelId) |
felipeal | cf47557 | 2020-04-08 09:07:02 -0700 | [diff] [blame] | 84 | .setContentText(name) |
| 85 | .setContentTitle(name) |
| 86 | .setSmallIcon(R.drawable.ic_launcher) |
| 87 | .build()); |
| 88 | |
| 89 | return super.onStartCommand(intent, flags, startId); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void onDestroy() { |
felipeal | 3ce0135 | 2020-04-14 15:43:09 -0700 | [diff] [blame] | 94 | Log.d(TAG, "onDestroy(" + mUserId + ")"); |
felipeal | cf47557 | 2020-04-08 09:07:02 -0700 | [diff] [blame] | 95 | |
| 96 | if (mCarUserManager != null) { |
| 97 | mCarUserManager.removeListener(mListener); |
| 98 | } else { |
| 99 | Log.w(TAG, "Cannot remove listener because manager is null"); |
| 100 | } |
Eric Jeong | aa5a652 | 2020-05-27 18:19:55 -0700 | [diff] [blame^] | 101 | if (mCar != null && mCar.isConnected()) { |
| 102 | mCar.disconnect(); |
| 103 | } |
felipeal | cf47557 | 2020-04-08 09:07:02 -0700 | [diff] [blame] | 104 | super.onDestroy(); |
| 105 | } |
| 106 | |
| 107 | @Override |
felipeal | 3ce0135 | 2020-04-14 15:43:09 -0700 | [diff] [blame] | 108 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 109 | pw.printf("User id: %d\n", mUserId); |
| 110 | synchronized (mLock) { |
| 111 | if (mEvents.isEmpty()) { |
| 112 | pw.println("Did not receive any event yet"); |
| 113 | return; |
| 114 | } |
| 115 | int size = mEvents.size(); |
| 116 | String indent = " "; |
| 117 | pw.printf("Received %d events:\n", size); |
| 118 | for (int i = 0; i < size; i++) { |
| 119 | pw.printf("%s%d: %s\n", indent, (i + 1), mEvents.get(i)); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | @Override |
felipeal | cf47557 | 2020-04-08 09:07:02 -0700 | [diff] [blame] | 125 | public IBinder onBind(Intent intent) { |
| 126 | Log.d(TAG, "onBind(): " + intent); |
| 127 | return null; |
| 128 | } |
| 129 | |
| 130 | } |