1 /*
2  * Copyright (C) 2020 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 android.server.wm.app;
18 
19 import static android.server.wm.app.Components.UnresponsiveActivity.EXTRA_ON_MOTIONEVENT_DELAY_MS;
20 import static android.server.wm.app.RenderService.BROADCAST_EMBED_CONTENT;
21 import static android.server.wm.app.RenderService.EXTRAS_BUNDLE;
22 import static android.server.wm.app.RenderService.EXTRAS_DISPLAY_ID;
23 import static android.server.wm.app.RenderService.EXTRAS_HOST_TOKEN;
24 import static android.server.wm.app.RenderService.EXTRAS_SURFACE_PACKAGE;
25 
26 import android.app.Activity;
27 import android.content.BroadcastReceiver;
28 import android.content.ComponentName;
29 import android.content.Context;
30 import android.content.Intent;
31 import android.content.IntentFilter;
32 import android.content.ServiceConnection;
33 import android.os.Bundle;
34 import android.os.IBinder;
35 import android.view.SurfaceControlViewHost.SurfacePackage;
36 import android.view.SurfaceHolder;
37 import android.view.SurfaceView;
38 import android.view.ViewGroup;
39 import android.widget.RelativeLayout;
40 
41 public class HostActivity extends Activity implements SurfaceHolder.Callback{
42     private SurfaceView mSurfaceView;
43 
44     private ServiceConnection mConnection = new ServiceConnection() {
45         @Override
46         public void onServiceConnected(ComponentName name, IBinder service) {}
47 
48         @Override
49         public void onServiceDisconnected(ComponentName className) {}
50     };
51 
52     private BroadcastReceiver receiver = new BroadcastReceiver() {
53         @Override
54         public void onReceive(Context context, Intent intent) {
55             SurfacePackage surfacePackage =
56                     intent.getParcelableExtra(EXTRAS_SURFACE_PACKAGE);
57             if (surfacePackage != null) {
58                 mSurfaceView.setChildSurfacePackage(surfacePackage);
59             }
60         }
61     };
62 
63     @Override
onCreate(Bundle savedInstanceState)64     public void onCreate(Bundle savedInstanceState) {
65         super.onCreate(savedInstanceState);
66 
67         IntentFilter filter = new IntentFilter();
68         filter.addAction(BROADCAST_EMBED_CONTENT);
69         registerReceiver(receiver, filter);
70 
71         final RelativeLayout content = new RelativeLayout(this);
72         mSurfaceView = new SurfaceView(this);
73         mSurfaceView.setZOrderOnTop(true);
74         content.addView(mSurfaceView,
75                 new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
76                         RelativeLayout.LayoutParams.MATCH_PARENT));
77         setContentView(content, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
78                 ViewGroup.LayoutParams.MATCH_PARENT));
79         mSurfaceView.getHolder().addCallback(this);
80     }
81 
82     @Override
onPause()83     protected void onPause() {
84         unregisterReceiver(receiver);
85         super.onPause();
86     }
87 
88     @Override
surfaceCreated(SurfaceHolder holder)89     public void surfaceCreated(SurfaceHolder holder) {
90         Intent mIntent =  new Intent(this, RenderService.class);
91         Bundle b = new Bundle();
92         b.putBinder(EXTRAS_HOST_TOKEN, mSurfaceView.getHostToken());
93         b.putInt(EXTRAS_DISPLAY_ID, getDisplay().getDisplayId());
94         b.putInt(EXTRA_ON_MOTIONEVENT_DELAY_MS,
95                 getIntent().getIntExtra(EXTRA_ON_MOTIONEVENT_DELAY_MS, 2000));
96         mIntent.putExtra(EXTRAS_BUNDLE, b);
97         bindService(mIntent, mConnection, Context.BIND_AUTO_CREATE|Context.BIND_IMPORTANT);
98     }
99 
100     @Override
surfaceChanged(SurfaceHolder holder, int format, int width, int height)101     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
102 
103     @Override
surfaceDestroyed(SurfaceHolder holder)104     public void surfaceDestroyed(SurfaceHolder holder) {}
105 }
106