• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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.topologytestapp;
18 
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.hardware.display.DisplayManager;
22 import android.hardware.display.DisplayTopology;
23 import android.os.Bundle;
24 import android.os.Message;
25 import android.os.Messenger;
26 import android.os.Process;
27 import android.os.RemoteException;
28 import android.util.Log;
29 
30 import java.util.function.Consumer;
31 
32 /**
33  * A simple activity listening to topology updates
34  */
35 public class TopologyUpdateActivity extends Activity {
36     public static final int MESSAGE_LAUNCHED = 1;
37     public static final int MESSAGE_CALLBACK = 2;
38 
39     private static final String TAG = TopologyUpdateActivity.class.getSimpleName();
40 
41     private static final String TEST_MESSENGER = "MESSENGER";
42 
43     private Messenger mMessenger;
44     private DisplayManager mDisplayManager;
45     private final Consumer<DisplayTopology> mTopologyListener = this::callback;
46 
47     @Override
onCreate(Bundle savedInstanceState)48     protected void onCreate(Bundle savedInstanceState) {
49         super.onCreate(savedInstanceState);
50         Intent intent = getIntent();
51         mMessenger = intent.getParcelableExtra(TEST_MESSENGER, Messenger.class);
52         mDisplayManager = getApplicationContext().getSystemService(DisplayManager.class);
53         mDisplayManager.registerTopologyListener(getMainExecutor(), mTopologyListener);
54         launched();
55     }
56 
57     @Override
onDestroy()58     protected void onDestroy() {
59         super.onDestroy();
60         mDisplayManager.unregisterTopologyListener(mTopologyListener);
61     }
62 
launched()63     private void launched() {
64         try {
65             Message msg = Message.obtain();
66             msg.what = MESSAGE_LAUNCHED;
67             msg.arg1 = android.os.Process.myPid();
68             msg.arg2 = Process.myUid();
69             Log.d(TAG, "Launched");
70             mMessenger.send(msg);
71         } catch (RemoteException e) {
72             e.rethrowAsRuntimeException();
73         }
74     }
75 
callback(DisplayTopology topology)76     private void callback(DisplayTopology topology) {
77         try {
78             Message msg = Message.obtain();
79             msg.what = MESSAGE_CALLBACK;
80             msg.obj = topology;
81             Log.d(TAG, "Msg " + topology);
82             mMessenger.send(msg);
83         } catch (RemoteException e) {
84             e.rethrowAsRuntimeException();
85         }
86     }
87 }
88