• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2023 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 android.media.cts;
17 
18 import android.content.ComponentName;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.os.Handler;
22 import android.os.Looper;
23 import android.os.Messenger;
24 import android.util.Log;
25 
26 /** Helper class to interact with the {@link LocalMediaProjectionService}. */
27 public final class ForegroundServiceUtil {
28     private static final String TAG = "LocalMediaProjectionServiceUtil";
29 
30     static final int MSG_START_FOREGROUND_DONE = 1;
31     static final int MSG_SERVICE_DESTROYED = 2;
32     static final String EXTRA_MESSENGER = "messenger";
33 
34     /** Helper function to request to start the foreground service. */
requestStartForegroundService( Context context, ComponentName name, Runnable serviceStarted, Runnable serviceStopped)35     public static void requestStartForegroundService(
36             Context context, ComponentName name, Runnable serviceStarted, Runnable serviceStopped) {
37         final Messenger messenger =
38                 new Messenger(
39                         new Handler(
40                                 Looper.getMainLooper(),
41                                 msg -> {
42                                     switch (msg.what) {
43                                         case MSG_START_FOREGROUND_DONE:
44                                             if (serviceStarted != null) {
45                                                 serviceStarted.run();
46                                             }
47                                             return true;
48                                         case MSG_SERVICE_DESTROYED:
49                                             if (serviceStopped != null) {
50                                                 serviceStopped.run();
51                                             }
52                                             return true;
53                                     }
54                                     Log.e(
55                                             TAG,
56                                             "Unknown message from the LocalMediaProjectionService: "
57                                                     + msg.what);
58                                     return false;
59                                 }));
60         final Intent intent = new Intent().setComponent(name).putExtra(EXTRA_MESSENGER, messenger);
61         context.startForegroundService(intent);
62     }
63 }
64