• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 package android.app.stubs;
17 
18 import static android.view.Gravity.LEFT;
19 import static android.view.Gravity.TOP;
20 import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
21 import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
22 import static android.view.WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
23 import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
24 
25 import android.app.Service;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.graphics.Color;
29 import android.graphics.Point;
30 import android.hardware.display.DisplayManager;
31 import android.os.IBinder;
32 import android.view.Display;
33 import android.view.View;
34 import android.view.WindowManager;
35 import android.widget.TextView;
36 
37 import com.android.compatibility.common.util.UserHelper;
38 
39 public class LocalAlertService extends Service {
40     public static final String COMMAND_SHOW_ALERT = "show";
41     public static final String COMMAND_HIDE_ALERT = "hide";
42 
43     private static View mAlertWindow = null;
44 
45     private final UserHelper mUserHelper = new UserHelper();
46 
47     @Override
onStartCommand(Intent intent, int flags, int startId)48     public int onStartCommand(Intent intent, int flags, int startId) {
49         String action = intent.getAction();
50 
51         if (COMMAND_SHOW_ALERT.equals(action)) {
52             mAlertWindow = showAlertWindow(getPackageName());
53         } else if (COMMAND_HIDE_ALERT.equals(action)) {
54             hideAlertWindow(mAlertWindow);
55             mAlertWindow = null;
56             stopSelf();
57         }
58         return START_NOT_STICKY;
59     }
60 
61     @Override
onBind(Intent intent)62     public IBinder onBind(Intent intent) {
63         return null;
64     }
65 
showAlertWindow(String windowName)66     private View showAlertWindow(String windowName) {
67         final Point size = new Point();
68         final WindowManager wm = mUserHelper.isVisibleBackgroundUser()
69                 ? getContextForSaw(this).getSystemService(WindowManager.class)
70                 : getSystemService(WindowManager.class);
71         wm.getDefaultDisplay().getSize(size);
72 
73         WindowManager.LayoutParams params = new WindowManager.LayoutParams(
74                 TYPE_APPLICATION_OVERLAY, FLAG_NOT_FOCUSABLE | FLAG_WATCH_OUTSIDE_TOUCH |
75                 FLAG_NOT_TOUCHABLE);
76         params.width = size.x / 3;
77         params.height = size.y / 3;
78         params.gravity = TOP | LEFT;
79         params.setTitle(windowName);
80 
81         final TextView view = new TextView(this);
82         view.setText(windowName);
83         view.setBackgroundColor(Color.RED);
84         wm.addView(view, params);
85         return view;
86     }
87 
hideAlertWindow(View window)88     private void hideAlertWindow(View window) {
89         final WindowManager wm = getSystemService(WindowManager.class);
90         wm.removeViewImmediate(window);
91     }
92 
getContextForSaw(Context context)93     private Context getContextForSaw(Context context) {
94         final DisplayManager displayManager = context.getSystemService(DisplayManager.class);
95         final Display display = displayManager.getDisplay(mUserHelper.getMainDisplayId());
96         final Context displayContext = context.createDisplayContext(display);
97         return displayContext.createWindowContext(TYPE_APPLICATION_OVERLAY, null);
98     }
99 }
100