blob: 47c9b86397603ce5863999f1c7bbd3cf3d5787f7 [file] [log] [blame]
Alex Salob81472f2018-12-12 14:44:28 -08001/*
2 * Copyright (C) 2019 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
17package com.android.server.attention;
18
Alex Saloc7b9c082019-01-29 13:10:55 -080019import static android.provider.DeviceConfig.AttentionManagerService.COMPONENT_NAME;
Alex Salo440fe3d2019-01-25 11:50:38 -080020import static android.provider.DeviceConfig.AttentionManagerService.NAMESPACE;
Alex Saloc7b9c082019-01-29 13:10:55 -080021import static android.provider.DeviceConfig.AttentionManagerService.SERVICE_ENABLED;
Alex Salo440fe3d2019-01-25 11:50:38 -080022
Alex Salob81472f2018-12-12 14:44:28 -080023import android.Manifest;
24import android.annotation.Nullable;
25import android.annotation.UserIdInt;
26import android.app.ActivityManager;
27import android.attention.AttentionManagerInternal;
28import android.attention.AttentionManagerInternal.AttentionCallbackInternal;
29import android.content.BroadcastReceiver;
30import android.content.ComponentName;
31import android.content.Context;
32import android.content.Intent;
33import android.content.IntentFilter;
34import android.content.ServiceConnection;
35import android.content.pm.PackageManager;
36import android.content.pm.ResolveInfo;
37import android.content.pm.ServiceInfo;
38import android.os.Binder;
39import android.os.Handler;
40import android.os.IBinder;
41import android.os.Looper;
42import android.os.Message;
43import android.os.PowerManager;
44import android.os.RemoteException;
45import android.os.SystemClock;
46import android.os.UserHandle;
Alex Salo440fe3d2019-01-25 11:50:38 -080047import android.provider.DeviceConfig;
Alex Salob81472f2018-12-12 14:44:28 -080048import android.service.attention.AttentionService;
49import android.service.attention.AttentionService.AttentionFailureCodes;
50import android.service.attention.IAttentionCallback;
51import android.service.attention.IAttentionService;
52import android.text.TextUtils;
53import android.util.Slog;
54import android.util.SparseArray;
Alex Saloa060aee2019-01-21 14:36:41 -080055import android.util.StatsLog;
Alex Salob81472f2018-12-12 14:44:28 -080056
57import com.android.internal.R;
58import com.android.internal.annotations.GuardedBy;
59import com.android.internal.util.DumpUtils;
60import com.android.internal.util.IndentingPrintWriter;
61import com.android.internal.util.Preconditions;
62import com.android.server.SystemService;
63
64import java.io.PrintWriter;
65
66/**
67 * An attention service implementation that runs in System Server process.
68 * This service publishes a LocalService and reroutes calls to a {@link AttentionService} that it
69 * manages.
70 */
71public class AttentionManagerService extends SystemService {
72 private static final String LOG_TAG = "AttentionManagerService";
73
Alex Salo440fe3d2019-01-25 11:50:38 -080074 /** Default value in absence of {@link DeviceConfig} override. */
75 private static final boolean DEFAULT_SERVICE_ENABLED = true;
76
Alex Salob81472f2018-12-12 14:44:28 -080077 /** Service will unbind if connection is not used for that amount of time. */
78 private static final long CONNECTION_TTL_MILLIS = 60_000;
79
80 /** If the check attention called within that period - cached value will be returned. */
81 private static final long STALE_AFTER_MILLIS = 5_000;
82
83 private final Context mContext;
84 private final PowerManager mPowerManager;
Alex Salob81472f2018-12-12 14:44:28 -080085 private final Object mLock;
86 @GuardedBy("mLock")
87 private final SparseArray<UserState> mUserStates = new SparseArray<>();
88 private final AttentionHandler mAttentionHandler;
89
90 private ComponentName mComponentName;
91
92 public AttentionManagerService(Context context) {
93 super(context);
94 mContext = Preconditions.checkNotNull(context);
95 mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
Alex Salob81472f2018-12-12 14:44:28 -080096 mLock = new Object();
97 mAttentionHandler = new AttentionHandler();
98 }
99
100 @Override
101 public void onStart() {
102 publishLocalService(AttentionManagerInternal.class, new LocalService());
103 }
104
105 @Override
Alex Salocde84302019-01-21 15:31:07 -0800106 public void onSwitchUser(int userId) {
Alex Salo1e32c072019-02-13 16:58:01 -0800107 cancelAndUnbindLocked(peekUserStateLocked(userId));
Alex Salob81472f2018-12-12 14:44:28 -0800108 }
109
Alex Salo50b701e2019-02-01 13:34:24 -0800110 /** Resolves and sets up the attention service if it had not been done yet. */
111 private boolean isServiceAvailable() {
112 if (mComponentName == null) {
Alex Salob81472f2018-12-12 14:44:28 -0800113 mComponentName = resolveAttentionService(mContext);
Alex Salo50b701e2019-02-01 13:34:24 -0800114 if (mComponentName != null) {
Alex Salob81472f2018-12-12 14:44:28 -0800115 mContext.registerReceiver(new ScreenStateReceiver(),
116 new IntentFilter(Intent.ACTION_SCREEN_OFF));
117 }
118 }
Alex Salo50b701e2019-02-01 13:34:24 -0800119 return mComponentName != null;
Alex Salob81472f2018-12-12 14:44:28 -0800120 }
121
122 /**
123 * Returns {@code true} if attention service is supported on this device.
124 */
125 public boolean isAttentionServiceSupported() {
Alex Salo50b701e2019-02-01 13:34:24 -0800126 return isServiceEnabled() && isServiceAvailable();
Alex Salo440fe3d2019-01-25 11:50:38 -0800127 }
128
129 private boolean isServiceEnabled() {
Alex Saloc7b9c082019-01-29 13:10:55 -0800130 final String enabled = DeviceConfig.getProperty(NAMESPACE, SERVICE_ENABLED);
Alex Salo440fe3d2019-01-25 11:50:38 -0800131 return enabled == null ? DEFAULT_SERVICE_ENABLED : "true".equals(enabled);
Alex Salob81472f2018-12-12 14:44:28 -0800132 }
133
134 /**
135 * Checks whether user attention is at the screen and calls in the provided callback.
136 *
137 * @return {@code true} if the framework was able to send the provided callback to the service
138 */
139 public boolean checkAttention(int requestCode, long timeout,
140 AttentionCallbackInternal callback) {
141 Preconditions.checkNotNull(callback);
142
143 if (!isAttentionServiceSupported()) {
144 Slog.w(LOG_TAG, "Trying to call checkAttention() on an unsupported device.");
145 return false;
146 }
147
148 // don't allow attention check in screen off state
149 if (!mPowerManager.isInteractive()) {
150 return false;
151 }
152
153 synchronized (mLock) {
Alex Salo1e32c072019-02-13 16:58:01 -0800154 final long now = SystemClock.uptimeMillis();
155 freeIfInactiveLocked();
Alex Salob81472f2018-12-12 14:44:28 -0800156
157 final UserState userState = getOrCreateCurrentUserStateLocked();
158 // lazily start the service, which should be very lightweight to start
159 if (!userState.bindLocked()) {
160 return false;
161 }
162
163 if (userState.mService == null) {
164 // make sure every callback is called back
165 if (userState.mPendingAttentionCheck != null) {
166 userState.mPendingAttentionCheck.cancel(
167 AttentionService.ATTENTION_FAILURE_UNKNOWN);
168 }
169 userState.mPendingAttentionCheck = new PendingAttentionCheck(requestCode,
170 callback, () -> checkAttention(requestCode, timeout, callback));
171 } else {
172 try {
173 // throttle frequent requests
174 final AttentionCheckCache attentionCheckCache = userState.mAttentionCheckCache;
Alex Salo1e32c072019-02-13 16:58:01 -0800175 if (attentionCheckCache != null && now
Alex Salob81472f2018-12-12 14:44:28 -0800176 < attentionCheckCache.mLastComputed + STALE_AFTER_MILLIS) {
177 callback.onSuccess(requestCode, attentionCheckCache.mResult,
178 attentionCheckCache.mTimestamp);
179 return true;
180 }
181
182 cancelAfterTimeoutLocked(timeout);
183
184 userState.mCurrentAttentionCheckRequestCode = requestCode;
185 userState.mService.checkAttention(requestCode, new IAttentionCallback.Stub() {
186 @Override
187 public void onSuccess(int requestCode, int result, long timestamp) {
188 callback.onSuccess(requestCode, result, timestamp);
Lucas Dupin0a5d7972019-01-16 18:52:30 -0800189 synchronized (mLock) {
190 userState.mAttentionCheckCache = new AttentionCheckCache(
191 SystemClock.uptimeMillis(), result,
192 timestamp);
Alex Salo1e32c072019-02-13 16:58:01 -0800193 userState.mCurrentAttentionCheckIsFulfilled = true;
Lucas Dupin0a5d7972019-01-16 18:52:30 -0800194 }
Alex Saloa060aee2019-01-21 14:36:41 -0800195 StatsLog.write(StatsLog.ATTENTION_MANAGER_SERVICE_RESULT_REPORTED,
196 result);
Alex Salob81472f2018-12-12 14:44:28 -0800197 }
198
199 @Override
200 public void onFailure(int requestCode, int error) {
201 callback.onFailure(requestCode, error);
Alex Salo1e32c072019-02-13 16:58:01 -0800202 userState.mCurrentAttentionCheckIsFulfilled = true;
Alex Saloa060aee2019-01-21 14:36:41 -0800203 StatsLog.write(StatsLog.ATTENTION_MANAGER_SERVICE_RESULT_REPORTED,
204 error);
Alex Salob81472f2018-12-12 14:44:28 -0800205 }
Alex Salob81472f2018-12-12 14:44:28 -0800206 });
207 } catch (RemoteException e) {
208 Slog.e(LOG_TAG, "Cannot call into the AttentionService");
209 return false;
210 }
211 }
212 return true;
213 }
214 }
215
216 /** Cancels the specified attention check. */
217 public void cancelAttentionCheck(int requestCode) {
Alex Salocde84302019-01-21 15:31:07 -0800218 synchronized (mLock) {
Alex Salo1e32c072019-02-13 16:58:01 -0800219 final UserState userState = peekCurrentUserStateLocked();
220 if (userState == null) {
221 return;
222 }
Alex Salocde84302019-01-21 15:31:07 -0800223 if (userState.mService == null) {
224 if (userState.mPendingAttentionCheck != null
225 && userState.mPendingAttentionCheck.mRequestCode == requestCode) {
226 userState.mPendingAttentionCheck = null;
227 }
228 return;
229 }
230 try {
231 userState.mService.cancelAttentionCheck(requestCode);
232 } catch (RemoteException e) {
233 Slog.e(LOG_TAG, "Cannot call into the AttentionService");
234 }
Alex Salob81472f2018-12-12 14:44:28 -0800235 }
236 }
237
238 @GuardedBy("mLock")
Alex Salo1e32c072019-02-13 16:58:01 -0800239 private void freeIfInactiveLocked() {
240 // If we are called here, it means someone used the API again - reset the timer then.
241 mAttentionHandler.removeMessages(AttentionHandler.CHECK_CONNECTION_EXPIRATION);
242
243 // Schedule resources cleanup if no one calls the API again.
244 mAttentionHandler.sendEmptyMessageDelayed(AttentionHandler.CHECK_CONNECTION_EXPIRATION,
Alex Salob81472f2018-12-12 14:44:28 -0800245 CONNECTION_TTL_MILLIS);
246 }
247
248 @GuardedBy("mLock")
249 private void cancelAfterTimeoutLocked(long timeout) {
250 mAttentionHandler.sendEmptyMessageDelayed(AttentionHandler.ATTENTION_CHECK_TIMEOUT,
251 timeout);
252 }
253
254
255 @GuardedBy("mLock")
256 private UserState getOrCreateCurrentUserStateLocked() {
Alex Salocde84302019-01-21 15:31:07 -0800257 return getOrCreateUserStateLocked(ActivityManager.getCurrentUser());
Alex Salob81472f2018-12-12 14:44:28 -0800258 }
259
260 @GuardedBy("mLock")
261 private UserState getOrCreateUserStateLocked(int userId) {
262 UserState result = mUserStates.get(userId);
263 if (result == null) {
Alex Salo50b701e2019-02-01 13:34:24 -0800264 result = new UserState(userId, mContext, mLock, mComponentName);
Alex Salob81472f2018-12-12 14:44:28 -0800265 mUserStates.put(userId, result);
266 }
267 return result;
268 }
269
270 @GuardedBy("mLock")
Alex Salo1e32c072019-02-13 16:58:01 -0800271 @Nullable
272 private UserState peekCurrentUserStateLocked() {
Alex Salocde84302019-01-21 15:31:07 -0800273 return peekUserStateLocked(ActivityManager.getCurrentUser());
Alex Salob81472f2018-12-12 14:44:28 -0800274 }
275
276 @GuardedBy("mLock")
Alex Salo1e32c072019-02-13 16:58:01 -0800277 @Nullable
278 private UserState peekUserStateLocked(int userId) {
Alex Salob81472f2018-12-12 14:44:28 -0800279 return mUserStates.get(userId);
280 }
281
282 /**
283 * Provides attention service component name at runtime, making sure it's provided by the
284 * system.
285 */
286 private static ComponentName resolveAttentionService(Context context) {
Alex Saloc7b9c082019-01-29 13:10:55 -0800287 final String flag = DeviceConfig.getProperty(NAMESPACE, COMPONENT_NAME);
Alex Salo440fe3d2019-01-25 11:50:38 -0800288
289 final String componentNameString = flag != null ? flag : context.getString(
Alex Salob81472f2018-12-12 14:44:28 -0800290 R.string.config_defaultAttentionService);
291
292 if (TextUtils.isEmpty(componentNameString)) {
293 return null;
294 }
295
296 final ComponentName componentName = ComponentName.unflattenFromString(componentNameString);
297 if (componentName == null) {
298 return null;
299 }
300
301 final Intent intent = new Intent(AttentionService.SERVICE_INTERFACE).setPackage(
302 componentName.getPackageName());
303
304 // Make sure that only system apps can declare the AttentionService.
305 final ResolveInfo resolveInfo = context.getPackageManager().resolveService(intent,
306 PackageManager.MATCH_SYSTEM_ONLY);
307 if (resolveInfo == null || resolveInfo.serviceInfo == null) {
308 Slog.wtf(LOG_TAG, String.format("Service %s not found in package %s",
309 AttentionService.SERVICE_INTERFACE, componentName
310 ));
311 return null;
312 }
313
314 final ServiceInfo serviceInfo = resolveInfo.serviceInfo;
315 final String permission = serviceInfo.permission;
316 if (Manifest.permission.BIND_ATTENTION_SERVICE.equals(permission)) {
317 return serviceInfo.getComponentName();
318 }
319 Slog.e(LOG_TAG, String.format(
320 "Service %s should require %s permission. Found %s permission",
321 serviceInfo.getComponentName(),
322 Manifest.permission.BIND_ATTENTION_SERVICE,
323 serviceInfo.permission));
324 return null;
325 }
326
327 private void dumpInternal(PrintWriter pw) {
328 if (!DumpUtils.checkDumpPermission(mContext, LOG_TAG, pw)) return;
329 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
330 ipw.println("Attention Manager Service (dumpsys attention)\n");
331
332 ipw.printPair("context", mContext);
333 pw.println();
334 synchronized (mLock) {
335 int size = mUserStates.size();
336 ipw.print("Number user states: ");
337 pw.println(size);
338 if (size > 0) {
339 ipw.increaseIndent();
340 for (int i = 0; i < size; i++) {
341 UserState userState = mUserStates.valueAt(i);
342 ipw.print(i);
343 ipw.print(":");
344 userState.dump(ipw);
345 ipw.println();
346 }
347 ipw.decreaseIndent();
348 }
349 }
350 }
351
352 private final class LocalService extends AttentionManagerInternal {
353 @Override
354 public boolean isAttentionServiceSupported() {
355 return AttentionManagerService.this.isAttentionServiceSupported();
356 }
357
358 @Override
359 public boolean checkAttention(int requestCode, long timeout,
360 AttentionCallbackInternal callback) {
361 return AttentionManagerService.this.checkAttention(requestCode, timeout, callback);
362 }
363
364 @Override
365 public void cancelAttentionCheck(int requestCode) {
366 AttentionManagerService.this.cancelAttentionCheck(requestCode);
367 }
368 }
369
370 private static final class AttentionCheckCache {
371 private final long mLastComputed;
372 private final int mResult;
373 private final long mTimestamp;
374
375 AttentionCheckCache(long lastComputed, @AttentionService.AttentionSuccessCodes int result,
376 long timestamp) {
377 mLastComputed = lastComputed;
378 mResult = result;
379 mTimestamp = timestamp;
380 }
381 }
382
383 private static final class PendingAttentionCheck {
384 private final int mRequestCode;
385 private final AttentionCallbackInternal mCallback;
386 private final Runnable mRunnable;
387
388 PendingAttentionCheck(int requestCode, AttentionCallbackInternal callback,
389 Runnable runnable) {
390 mRequestCode = requestCode;
391 mCallback = callback;
392 mRunnable = runnable;
393 }
394
395 void cancel(@AttentionFailureCodes int failureCode) {
396 mCallback.onFailure(mRequestCode, failureCode);
397 }
398
399 void run() {
400 mRunnable.run();
401 }
402 }
403
404 private static final class UserState {
Alex Salo50b701e2019-02-01 13:34:24 -0800405 final ComponentName mComponentName;
Alex Salob81472f2018-12-12 14:44:28 -0800406 final AttentionServiceConnection mConnection = new AttentionServiceConnection();
407
408 @GuardedBy("mLock")
409 IAttentionService mService;
410 @GuardedBy("mLock")
411 boolean mBinding;
412 @GuardedBy("mLock")
413 int mCurrentAttentionCheckRequestCode;
414 @GuardedBy("mLock")
Alex Salo1e32c072019-02-13 16:58:01 -0800415 boolean mCurrentAttentionCheckIsFulfilled;
416 @GuardedBy("mLock")
Alex Salob81472f2018-12-12 14:44:28 -0800417 PendingAttentionCheck mPendingAttentionCheck;
418
419 @GuardedBy("mLock")
420 AttentionCheckCache mAttentionCheckCache;
421
422 @UserIdInt
423 final int mUserId;
424 final Context mContext;
425 final Object mLock;
426
Alex Salo50b701e2019-02-01 13:34:24 -0800427 private UserState(int userId, Context context, Object lock, ComponentName componentName) {
Alex Salob81472f2018-12-12 14:44:28 -0800428 mUserId = userId;
429 mContext = Preconditions.checkNotNull(context);
430 mLock = Preconditions.checkNotNull(lock);
Alex Salo50b701e2019-02-01 13:34:24 -0800431 mComponentName = Preconditions.checkNotNull(componentName);
Alex Salob81472f2018-12-12 14:44:28 -0800432 }
433
434
435 @GuardedBy("mLock")
436 private void handlePendingCallbackLocked() {
437 if (mService != null && mPendingAttentionCheck != null) {
438 mPendingAttentionCheck.run();
439 mPendingAttentionCheck = null;
440 }
441 }
442
443 /** Binds to the system's AttentionService which provides an actual implementation. */
444 @GuardedBy("mLock")
445 private boolean bindLocked() {
446 // No need to bind if service is binding or has already been bound.
447 if (mBinding || mService != null) {
448 return true;
449 }
450
451 final boolean willBind;
452 final long identity = Binder.clearCallingIdentity();
453
454 try {
Alex Salob81472f2018-12-12 14:44:28 -0800455 final Intent mServiceIntent = new Intent(
456 AttentionService.SERVICE_INTERFACE).setComponent(
Alex Salo50b701e2019-02-01 13:34:24 -0800457 mComponentName);
Alex Salob81472f2018-12-12 14:44:28 -0800458 willBind = mContext.bindServiceAsUser(mServiceIntent, mConnection,
459 Context.BIND_AUTO_CREATE, UserHandle.CURRENT);
460 mBinding = willBind;
461 } finally {
462 Binder.restoreCallingIdentity(identity);
463 }
464 return willBind;
465 }
466
467 private void dump(IndentingPrintWriter pw) {
468 pw.printPair("context", mContext);
469 pw.printPair("userId", mUserId);
470 synchronized (mLock) {
471 pw.printPair("binding", mBinding);
472 pw.printPair("isAttentionCheckPending", mPendingAttentionCheck != null);
473 }
474 }
475
476 private final class AttentionServiceConnection implements ServiceConnection {
477 @Override
478 public void onServiceConnected(ComponentName name, IBinder service) {
479 init(IAttentionService.Stub.asInterface(service));
480 }
481
482 @Override
483 public void onServiceDisconnected(ComponentName name) {
484 cleanupService();
485 }
486
487 @Override
488 public void onBindingDied(ComponentName name) {
489 cleanupService();
490 }
491
492 @Override
493 public void onNullBinding(ComponentName name) {
494 cleanupService();
495 }
496
497 void cleanupService() {
498 init(null);
499 }
500
501 private void init(@Nullable IAttentionService service) {
502 synchronized (mLock) {
503 mService = service;
504 mBinding = false;
505 handlePendingCallbackLocked();
506 }
507 }
508 }
509 }
510
511 private class AttentionHandler extends Handler {
Alex Salo1e32c072019-02-13 16:58:01 -0800512 private static final int CHECK_CONNECTION_EXPIRATION = 1;
Alex Salob81472f2018-12-12 14:44:28 -0800513 private static final int ATTENTION_CHECK_TIMEOUT = 2;
514
515 AttentionHandler() {
516 super(Looper.myLooper());
517 }
518
519 public void handleMessage(Message msg) {
520 switch (msg.what) {
521 // Do not occupy resources when not in use - unbind proactively.
Alex Salo1e32c072019-02-13 16:58:01 -0800522 case CHECK_CONNECTION_EXPIRATION: {
Alex Salob81472f2018-12-12 14:44:28 -0800523 for (int i = 0; i < mUserStates.size(); i++) {
Alex Salo1e32c072019-02-13 16:58:01 -0800524 cancelAndUnbindLocked(mUserStates.valueAt(i));
Alex Salob81472f2018-12-12 14:44:28 -0800525 }
Alex Salob81472f2018-12-12 14:44:28 -0800526 }
527 break;
528
529 // Callee is no longer interested in the attention check result - cancel.
530 case ATTENTION_CHECK_TIMEOUT: {
Alex Salo1e32c072019-02-13 16:58:01 -0800531 synchronized (mLock) {
532 final UserState userState = peekCurrentUserStateLocked();
533 if (userState != null) {
534 // If not called back already.
535 if (!userState.mCurrentAttentionCheckIsFulfilled) {
536 cancel(userState,
537 AttentionService.ATTENTION_FAILURE_TIMED_OUT);
538 }
539
540 }
541 }
Alex Salob81472f2018-12-12 14:44:28 -0800542 }
543 break;
544
545 default:
546 break;
547 }
548 }
549 }
550
Alex Salo1e32c072019-02-13 16:58:01 -0800551 private void cancel(UserState userState, @AttentionFailureCodes int failureCode) {
552 if (userState != null && userState.mService != null) {
553 try {
554 userState.mService.cancelAttentionCheck(
555 userState.mCurrentAttentionCheckRequestCode);
556 } catch (RemoteException e) {
557 Slog.e(LOG_TAG, "Unable to cancel attention check");
Alex Salob81472f2018-12-12 14:44:28 -0800558 }
Alex Salo1e32c072019-02-13 16:58:01 -0800559
560 if (userState.mPendingAttentionCheck != null) {
561 userState.mPendingAttentionCheck.cancel(failureCode);
562 }
563 }
564 }
565
566 @GuardedBy("mLock")
567 private void cancelAndUnbindLocked(UserState userState) {
568 synchronized (mLock) {
569 cancel(userState, AttentionService.ATTENTION_FAILURE_UNKNOWN);
570
571 mContext.unbindService(userState.mConnection);
572 userState.mConnection.cleanupService();
573 mUserStates.remove(userState.mUserId);
Alex Salob81472f2018-12-12 14:44:28 -0800574 }
575 }
576
577 /**
578 * Unbinds and stops the service when the screen off intent is received.
579 * Attention service only makes sense when screen is ON; disconnect and stop service otherwise.
580 */
581 private final class ScreenStateReceiver extends BroadcastReceiver {
582 @Override
583 public void onReceive(Context context, Intent intent) {
584 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Alex Salo1e32c072019-02-13 16:58:01 -0800585 cancelAndUnbindLocked(peekCurrentUserStateLocked());
Alex Salob81472f2018-12-12 14:44:28 -0800586 }
587 }
588 }
589}