• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 
17 package com.android.servicestests.apps.displaymanagertestapp;
18 
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.hardware.display.DisplayManager;
22 import android.os.Bundle;
23 import android.os.Handler;
24 import android.os.Looper;
25 import android.os.Message;
26 import android.os.Messenger;
27 import android.os.Process;
28 import android.os.RemoteException;
29 import android.util.Log;
30 
31 /**
32  * A simple activity manipulating displays and listening to corresponding display events
33  */
34 public class DisplayEventActivity extends Activity {
35     private static final String TAG = DisplayEventActivity.class.getSimpleName();
36 
37     private static final String TEST_DISPLAYS = "DISPLAYS";
38     private static final String TEST_MESSENGER = "MESSENGER";
39 
40     private static final int MESSAGE_LAUNCHED = 1;
41     private static final int MESSAGE_CALLBACK = 2;
42 
43     private static final int DISPLAY_ADDED = 1;
44     private static final int DISPLAY_CHANGED = 2;
45     private static final int DISPLAY_REMOVED = 3;
46 
47     private int mExpectedDisplayCount;
48     private int mSeenDisplayCount;
49     private Messenger mMessenger;
50     private DisplayManager mDisplayManager;
51     private DisplayManager.DisplayListener mDisplayListener;
52 
53     @Override
onCreate(Bundle savedInstanceState)54     protected void onCreate(Bundle savedInstanceState) {
55         super.onCreate(savedInstanceState);
56         Intent intent = getIntent();
57         mExpectedDisplayCount = 0;
58         mSeenDisplayCount = intent.getIntExtra(TEST_DISPLAYS, 0);
59         mMessenger = intent.getParcelableExtra(TEST_MESSENGER, Messenger.class);
60         mDisplayManager = getApplicationContext().getSystemService(DisplayManager.class);
61         mDisplayListener = new DisplayManager.DisplayListener() {
62             @Override
63             public void onDisplayAdded(int displayId) {
64                 callback(displayId, DISPLAY_ADDED);
65             }
66 
67             @Override
68             public void onDisplayRemoved(int displayId) {
69                 callback(displayId, DISPLAY_REMOVED);
70             }
71 
72             @Override
73             public void onDisplayChanged(int displayId) {
74                 callback(displayId, DISPLAY_CHANGED);
75             }
76         };
77         Handler handler = new Handler(Looper.getMainLooper());
78         mDisplayManager.registerDisplayListener(mDisplayListener, handler);
79         launched();
80     }
81 
82     @Override
onDestroy()83     protected void onDestroy() {
84         super.onDestroy();
85         mDisplayManager.unregisterDisplayListener(mDisplayListener);
86     }
87 
launched()88     private void launched() {
89         try {
90             Message msg = Message.obtain();
91             msg.what = MESSAGE_LAUNCHED;
92             msg.arg1 = Process.myPid();
93             msg.arg2 = Process.myUid();
94             Log.d(TAG, "Launched " + mSeenDisplayCount);
95             mMessenger.send(msg);
96         } catch (RemoteException e) {
97             e.rethrowAsRuntimeException();
98         }
99     }
100 
callback(int displayId, int event)101     private void callback(int displayId, int event) {
102         try {
103             Message msg = Message.obtain();
104             msg.what = MESSAGE_CALLBACK;
105             msg.arg1 = displayId;
106             msg.arg2 = event;
107             Log.d(TAG, "Msg " + msg.arg1 + " " + msg.arg2);
108             mMessenger.send(msg);
109             if (event == DISPLAY_REMOVED) {
110                 mExpectedDisplayCount++;
111                 if (mExpectedDisplayCount >= mSeenDisplayCount) {
112                     finish();
113                 }
114             }
115         } catch (RemoteException e) {
116             e.rethrowAsRuntimeException();
117         }
118     }
119 }
120