blob: df4ec78967747242fd01377180106af106c87c7e [file] [log] [blame]
Craig Mautner4504de52013-12-20 09:06:56 -08001/*
2 * Copyright (C) 2013 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
17package android.app;
18
19import android.content.Context;
20import android.content.ContextWrapper;
21import android.content.Intent;
Craig Mautnerdf88d732014-01-27 09:21:32 -080022import android.content.IntentSender;
Craig Mautner4504de52013-12-20 09:06:56 -080023import android.graphics.SurfaceTexture;
24import android.os.IBinder;
25import android.os.RemoteException;
26import android.util.AttributeSet;
27import android.util.DisplayMetrics;
Craig Mautner34b73df2014-01-12 21:11:08 -080028import android.util.Log;
Craig Mautner4504de52013-12-20 09:06:56 -080029import android.view.Surface;
30import android.view.TextureView;
31import android.view.TextureView.SurfaceTextureListener;
Craig Mautnerdf88d732014-01-27 09:21:32 -080032import android.view.View;
Craig Mautner4504de52013-12-20 09:06:56 -080033import android.view.ViewGroup;
34import android.view.WindowManager;
35
36public class ActivityView extends ViewGroup {
Craig Mautner34b73df2014-01-12 21:11:08 -080037 private final String TAG = "ActivityView";
38
Craig Mautner4504de52013-12-20 09:06:56 -080039 private final TextureView mTextureView;
40 private IActivityContainer mActivityContainer;
41 private Activity mActivity;
Craig Mautner4504de52013-12-20 09:06:56 -080042 private int mWidth;
43 private int mHeight;
Craig Mautner34b73df2014-01-12 21:11:08 -080044 private Surface mSurface;
Craig Mautner4504de52013-12-20 09:06:56 -080045
46 public ActivityView(Context context) {
47 this(context, null);
48 }
49
50 public ActivityView(Context context, AttributeSet attrs) {
51 this(context, attrs, 0);
52 }
53
54 public ActivityView(Context context, AttributeSet attrs, int defStyle) {
55 super(context, attrs, defStyle);
56
57 while (context instanceof ContextWrapper) {
58 if (context instanceof Activity) {
59 mActivity = (Activity)context;
60 break;
61 }
62 context = ((ContextWrapper)context).getBaseContext();
63 }
64 if (mActivity == null) {
65 throw new IllegalStateException("The ActivityView's Context is not an Activity.");
66 }
67
68 mTextureView = new TextureView(context);
69 mTextureView.setSurfaceTextureListener(new ActivityViewSurfaceTextureListener());
70 addView(mTextureView);
71 }
72
73 @Override
74 protected void onLayout(boolean changed, int l, int t, int r, int b) {
75 mTextureView.layout(l, t, r, b);
76 }
77
78 @Override
79 protected void onAttachedToWindow() {
80 try {
81 final IBinder token = mActivity.getActivityToken();
82 mActivityContainer =
83 ActivityManagerNative.getDefault().createActivityContainer(token, null);
84 } catch (RemoteException e) {
85 throw new IllegalStateException("ActivityView: Unable to create ActivityContainer. "
86 + e);
87 }
88
Craig Mautnerdf88d732014-01-27 09:21:32 -080089 attachToSurfaceWhenReady();
Craig Mautner4504de52013-12-20 09:06:56 -080090 }
91
92 @Override
93 protected void onDetachedFromWindow() {
Craig Mautnerdf88d732014-01-27 09:21:32 -080094 if (mActivityContainer != null) {
95 detach();
96 mActivityContainer = null;
97 }
Craig Mautner34b73df2014-01-12 21:11:08 -080098 }
99
100 @Override
Craig Mautnerdf88d732014-01-27 09:21:32 -0800101 protected void onWindowVisibilityChanged(int visibility) {
102 super.onWindowVisibilityChanged(visibility);
103 if (visibility == View.VISIBLE) {
104 attachToSurfaceWhenReady();
105 } else {
106 detach();
107 }
108 }
109
110 public boolean isAttachedToDisplay() {
111 return mSurface != null;
Craig Mautner4504de52013-12-20 09:06:56 -0800112 }
113
114 public void startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800115 if (mSurface != null) {
Craig Mautner4504de52013-12-20 09:06:56 -0800116 try {
117 mActivityContainer.startActivity(intent);
118 } catch (RemoteException e) {
119 throw new IllegalStateException("ActivityView: Unable to startActivity. " + e);
120 }
121 }
122 }
123
Craig Mautnerdf88d732014-01-27 09:21:32 -0800124 public void startActivity(IntentSender intentSender) {
125 if (mSurface != null) {
126 try {
127 mActivityContainer.startActivityIntentSender(intentSender.getTarget());
128 } catch (RemoteException e) {
129 throw new IllegalStateException(
130 "ActivityView: Unable to startActivity from IntentSender. " + e);
131 }
132 }
133 }
134
135 public void startActivity(PendingIntent pendingIntent) {
136 if (mSurface != null) {
137 try {
138 mActivityContainer.startActivityIntentSender(pendingIntent.getTarget());
139 } catch (RemoteException e) {
140 throw new IllegalStateException(
141 "ActivityView: Unable to startActivity from PendingIntent. " + e);
142 }
143 }
144 }
145
146 private void attachToSurfaceWhenReady() {
147 final SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
148 if (mActivityContainer == null || surfaceTexture == null || mSurface != null) {
149 // Either not ready to attach, or already attached.
150 return;
151 }
152
Craig Mautner4504de52013-12-20 09:06:56 -0800153 WindowManager wm = (WindowManager)mActivity.getSystemService(Context.WINDOW_SERVICE);
154 DisplayMetrics metrics = new DisplayMetrics();
155 wm.getDefaultDisplay().getMetrics(metrics);
156
Craig Mautner34b73df2014-01-12 21:11:08 -0800157 mSurface = new Surface(surfaceTexture);
Craig Mautner4504de52013-12-20 09:06:56 -0800158 try {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800159 mActivityContainer.attachToSurface(mSurface, mWidth, mHeight, metrics.densityDpi);
Craig Mautner4504de52013-12-20 09:06:56 -0800160 } catch (RemoteException e) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800161 mSurface.release();
162 mSurface = null;
Craig Mautner4504de52013-12-20 09:06:56 -0800163 throw new IllegalStateException(
164 "ActivityView: Unable to create ActivityContainer. " + e);
165 }
Craig Mautner4504de52013-12-20 09:06:56 -0800166 }
167
Craig Mautnerdf88d732014-01-27 09:21:32 -0800168 private void detach() {
169 if (mSurface != null) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800170 try {
171 mActivityContainer.detachFromDisplay();
172 } catch (RemoteException e) {
173 }
Craig Mautner34b73df2014-01-12 21:11:08 -0800174 mSurface.release();
175 mSurface = null;
176 }
Craig Mautner34b73df2014-01-12 21:11:08 -0800177 }
178
Craig Mautner4504de52013-12-20 09:06:56 -0800179 private class ActivityViewSurfaceTextureListener implements SurfaceTextureListener {
180 @Override
181 public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width,
182 int height) {
183 mWidth = width;
184 mHeight = height;
185 if (mActivityContainer != null) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800186 attachToSurfaceWhenReady();
Craig Mautner4504de52013-12-20 09:06:56 -0800187 }
188 }
189
190 @Override
191 public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width,
192 int height) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800193 Log.d(TAG, "onSurfaceTextureSizeChanged: w=" + width + " h=" + height);
Craig Mautner4504de52013-12-20 09:06:56 -0800194 }
195
196 @Override
197 public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800198 Log.d(TAG, "onSurfaceTextureDestroyed");
Craig Mautnerdf88d732014-01-27 09:21:32 -0800199 detach();
Craig Mautner34b73df2014-01-12 21:11:08 -0800200 return true;
Craig Mautner4504de52013-12-20 09:06:56 -0800201 }
202
203 @Override
204 public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800205// Log.d(TAG, "onSurfaceTextureUpdated");
Craig Mautner4504de52013-12-20 09:06:56 -0800206 }
207
208 }
209}