blob: 25cde8cf8f95b2ed3bc2a2777b0e179495d5df03 [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;
22import android.graphics.SurfaceTexture;
23import android.os.IBinder;
24import android.os.RemoteException;
25import android.util.AttributeSet;
26import android.util.DisplayMetrics;
Craig Mautner34b73df2014-01-12 21:11:08 -080027import android.util.Log;
Craig Mautner4504de52013-12-20 09:06:56 -080028import android.view.Surface;
29import android.view.TextureView;
30import android.view.TextureView.SurfaceTextureListener;
31import android.view.ViewGroup;
32import android.view.WindowManager;
33
34public class ActivityView extends ViewGroup {
Craig Mautner34b73df2014-01-12 21:11:08 -080035 private final String TAG = "ActivityView";
36
Craig Mautner4504de52013-12-20 09:06:56 -080037 private final TextureView mTextureView;
38 private IActivityContainer mActivityContainer;
39 private Activity mActivity;
40 private boolean mAttached;
41 private int mWidth;
42 private int mHeight;
Craig Mautner34b73df2014-01-12 21:11:08 -080043 private Surface mSurface;
Craig Mautner4504de52013-12-20 09:06:56 -080044
45 public ActivityView(Context context) {
46 this(context, null);
47 }
48
49 public ActivityView(Context context, AttributeSet attrs) {
50 this(context, attrs, 0);
51 }
52
53 public ActivityView(Context context, AttributeSet attrs, int defStyle) {
54 super(context, attrs, defStyle);
55
56 while (context instanceof ContextWrapper) {
57 if (context instanceof Activity) {
58 mActivity = (Activity)context;
59 break;
60 }
61 context = ((ContextWrapper)context).getBaseContext();
62 }
63 if (mActivity == null) {
64 throw new IllegalStateException("The ActivityView's Context is not an Activity.");
65 }
66
67 mTextureView = new TextureView(context);
68 mTextureView.setSurfaceTextureListener(new ActivityViewSurfaceTextureListener());
69 addView(mTextureView);
70 }
71
72 @Override
73 protected void onLayout(boolean changed, int l, int t, int r, int b) {
74 mTextureView.layout(l, t, r, b);
75 }
76
77 @Override
78 protected void onAttachedToWindow() {
79 try {
80 final IBinder token = mActivity.getActivityToken();
81 mActivityContainer =
82 ActivityManagerNative.getDefault().createActivityContainer(token, null);
83 } catch (RemoteException e) {
84 throw new IllegalStateException("ActivityView: Unable to create ActivityContainer. "
85 + e);
86 }
87
88 final SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
89 if (surfaceTexture != null) {
Craig Mautner34b73df2014-01-12 21:11:08 -080090 attachToSurface(surfaceTexture);
Craig Mautner4504de52013-12-20 09:06:56 -080091 }
92 }
93
94 @Override
95 protected void onDetachedFromWindow() {
Craig Mautner34b73df2014-01-12 21:11:08 -080096 detachFromSurface();
97 }
98
99 @Override
100 public boolean isAttachedToWindow() {
101 return mAttached;
Craig Mautner4504de52013-12-20 09:06:56 -0800102 }
103
104 public void startActivity(Intent intent) {
105 if (mActivityContainer != null && mAttached) {
106 try {
107 mActivityContainer.startActivity(intent);
108 } catch (RemoteException e) {
109 throw new IllegalStateException("ActivityView: Unable to startActivity. " + e);
110 }
111 }
112 }
113
114 /** Call when both mActivityContainer and mTextureView's SurfaceTexture are not null */
Craig Mautner34b73df2014-01-12 21:11:08 -0800115 private void attachToSurface(SurfaceTexture surfaceTexture) {
Craig Mautner4504de52013-12-20 09:06:56 -0800116 WindowManager wm = (WindowManager)mActivity.getSystemService(Context.WINDOW_SERVICE);
117 DisplayMetrics metrics = new DisplayMetrics();
118 wm.getDefaultDisplay().getMetrics(metrics);
119
Craig Mautner34b73df2014-01-12 21:11:08 -0800120 mSurface = new Surface(surfaceTexture);
Craig Mautner4504de52013-12-20 09:06:56 -0800121 try {
Craig Mautner34b73df2014-01-12 21:11:08 -0800122 mActivityContainer.attachToSurface(mSurface, mWidth, mHeight,
Craig Mautner4504de52013-12-20 09:06:56 -0800123 metrics.densityDpi);
124 } catch (RemoteException e) {
125 mActivityContainer = null;
Craig Mautner34b73df2014-01-12 21:11:08 -0800126 mSurface.release();
127 mSurface = null;
128 mAttached = false;
Craig Mautner4504de52013-12-20 09:06:56 -0800129 throw new IllegalStateException(
130 "ActivityView: Unable to create ActivityContainer. " + e);
131 }
132 mAttached = true;
133 }
134
Craig Mautner34b73df2014-01-12 21:11:08 -0800135 private void detachFromSurface() {
136 if (mActivityContainer != null) {
137 try {
138 mActivityContainer.detachFromDisplay();
139 } catch (RemoteException e) {
140 }
141 mActivityContainer = null;
142 }
143 if (mSurface != null) {
144 mSurface.release();
145 mSurface = null;
146 }
147 mAttached = false;
148 }
149
Craig Mautner4504de52013-12-20 09:06:56 -0800150 private class ActivityViewSurfaceTextureListener implements SurfaceTextureListener {
151 @Override
152 public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width,
153 int height) {
154 mWidth = width;
155 mHeight = height;
156 if (mActivityContainer != null) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800157 attachToSurface(surfaceTexture);
Craig Mautner4504de52013-12-20 09:06:56 -0800158 }
159 }
160
161 @Override
162 public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width,
163 int height) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800164 Log.d(TAG, "onSurfaceTextureSizeChanged: w=" + width + " h=" + height);
Craig Mautner4504de52013-12-20 09:06:56 -0800165 }
166
167 @Override
168 public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800169 Log.d(TAG, "onSurfaceTextureDestroyed");
170 detachFromSurface();
171 return true;
Craig Mautner4504de52013-12-20 09:06:56 -0800172 }
173
174 @Override
175 public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800176// Log.d(TAG, "onSurfaceTextureUpdated");
Craig Mautner4504de52013-12-20 09:06:56 -0800177 }
178
179 }
180}