blob: f76b0d654d5b542c12cdf877a3add8d542dc9fa7 [file] [log] [blame]
felipealcf475572020-04-08 09:07:02 -07001/*
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 */
16package com.google.android.car.userswitchmonitor;
17
18import android.app.Notification;
19import android.app.NotificationChannel;
20import android.app.NotificationManager;
21import android.app.Service;
22import android.car.Car;
23import android.car.user.CarUserManager;
felipeal3ce01352020-04-14 15:43:09 -070024import android.car.user.CarUserManager.UserLifecycleEvent;
felipealcf475572020-04-08 09:07:02 -070025import android.content.Context;
26import android.content.Intent;
27import android.os.IBinder;
28import android.util.Log;
29
felipeal3ce01352020-04-14 15:43:09 -070030import java.io.FileDescriptor;
31import java.io.PrintWriter;
32import java.util.ArrayList;
33import java.util.List;
34
felipealcf475572020-04-08 09:07:02 -070035/**
36 * Service that users {@link CarUserManager.UserLifecycleEvent UserLifecycleEvents} to monitor
37 * user switches.
38 *
39 */
40public final class UserSwitchMonitorService extends Service {
41
felipeal3ce01352020-04-14 15:43:09 -070042 static final String TAG = "UserSwitchMonitor";
felipealcf475572020-04-08 09:07:02 -070043
felipeal3ce01352020-04-14 15:43:09 -070044 private final Object mLock = new Object();
felipealcf475572020-04-08 09:07:02 -070045
felipeal3ce01352020-04-14 15:43:09 -070046 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 };
felipealcf475572020-04-08 09:07:02 -070056
Eric Jeongaa5a6522020-05-27 18:19:55 -070057 private Context mContext;
58 private Car mCar;
felipealcf475572020-04-08 09:07:02 -070059 private CarUserManager mCarUserManager;
Eric Jeongaa5a6522020-05-27 18:19:55 -070060 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 }
felipealcf475572020-04-08 09:07:02 -070071
72 @Override
73 public int onStartCommand(Intent intent, int flags, int startId) {
felipeal3ce01352020-04-14 15:43:09 -070074 Log.d(TAG, "onStartCommand(" + mUserId + "): " + intent);
felipealcf475572020-04-08 09:07:02 -070075
felipealcf475572020-04-08 09:07:02 -070076 String channelId = "4815162342";
77 String name = "UserSwitchMonitor";
78 NotificationChannel channel = new NotificationChannel(channelId, name,
79 NotificationManager.IMPORTANCE_MIN);
Eric Jeongaa5a6522020-05-27 18:19:55 -070080 mNotificationManager.createNotificationChannel(channel);
felipealcf475572020-04-08 09:07:02 -070081
82 startForeground(startId,
Eric Jeongaa5a6522020-05-27 18:19:55 -070083 new Notification.Builder(mContext, channelId)
felipealcf475572020-04-08 09:07:02 -070084 .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() {
felipeal3ce01352020-04-14 15:43:09 -070094 Log.d(TAG, "onDestroy(" + mUserId + ")");
felipealcf475572020-04-08 09:07:02 -070095
96 if (mCarUserManager != null) {
97 mCarUserManager.removeListener(mListener);
98 } else {
99 Log.w(TAG, "Cannot remove listener because manager is null");
100 }
Eric Jeongaa5a6522020-05-27 18:19:55 -0700101 if (mCar != null && mCar.isConnected()) {
102 mCar.disconnect();
103 }
felipealcf475572020-04-08 09:07:02 -0700104 super.onDestroy();
105 }
106
107 @Override
felipeal3ce01352020-04-14 15:43:09 -0700108 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
felipealcf475572020-04-08 09:07:02 -0700125 public IBinder onBind(Intent intent) {
126 Log.d(TAG, "onBind(): " + intent);
127 return null;
128 }
129
130}