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.Intent;
27 import android.graphics.Color;
28 import android.graphics.Point;
29 import android.os.IBinder;
30 import android.view.View;
31 import android.view.WindowManager;
32 import android.widget.TextView;
33 
34 public class LocalAlertService extends Service {
35     public static final String COMMAND_SHOW_ALERT = "show";
36     public static final String COMMAND_HIDE_ALERT = "hide";
37 
38     private static View mAlertWindow = null;
39 
40     @Override
onStartCommand(Intent intent, int flags, int startId)41     public int onStartCommand(Intent intent, int flags, int startId) {
42         String action = intent.getAction();
43 
44         if (COMMAND_SHOW_ALERT.equals(action)) {
45             mAlertWindow = showAlertWindow(getPackageName());
46         } else if (COMMAND_HIDE_ALERT.equals(action)) {
47             hideAlertWindow(mAlertWindow);
48             mAlertWindow = null;
49             stopSelf();
50         }
51         return START_NOT_STICKY;
52     }
53 
54     @Override
onBind(Intent intent)55     public IBinder onBind(Intent intent) {
56         return null;
57     }
58 
showAlertWindow(String windowName)59     private View showAlertWindow(String windowName) {
60         final Point size = new Point();
61         final WindowManager wm = getSystemService(WindowManager.class);
62         wm.getDefaultDisplay().getSize(size);
63 
64         WindowManager.LayoutParams params = new WindowManager.LayoutParams(
65                 TYPE_APPLICATION_OVERLAY, FLAG_NOT_FOCUSABLE | FLAG_WATCH_OUTSIDE_TOUCH |
66                 FLAG_NOT_TOUCHABLE);
67         params.width = size.x / 3;
68         params.height = size.y / 3;
69         params.gravity = TOP | LEFT;
70         params.setTitle(windowName);
71 
72         final TextView view = new TextView(this);
73         view.setText(windowName);
74         view.setBackgroundColor(Color.RED);
75         wm.addView(view, params);
76         return view;
77     }
78 
hideAlertWindow(View window)79     private void hideAlertWindow(View window) {
80         final WindowManager wm = getSystemService(WindowManager.class);
81         wm.removeViewImmediate(window);
82     }
83 }
84