blob: fef4597e4e4e79bf95d4c590fdb85be4ee2adf22 [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;
27import android.view.Surface;
28import android.view.TextureView;
29import android.view.TextureView.SurfaceTextureListener;
30import android.view.ViewGroup;
31import android.view.WindowManager;
32
33public class ActivityView extends ViewGroup {
34 private final TextureView mTextureView;
35 private IActivityContainer mActivityContainer;
36 private Activity mActivity;
37 private boolean mAttached;
38 private int mWidth;
39 private int mHeight;
40
41 public ActivityView(Context context) {
42 this(context, null);
43 }
44
45 public ActivityView(Context context, AttributeSet attrs) {
46 this(context, attrs, 0);
47 }
48
49 public ActivityView(Context context, AttributeSet attrs, int defStyle) {
50 super(context, attrs, defStyle);
51
52 while (context instanceof ContextWrapper) {
53 if (context instanceof Activity) {
54 mActivity = (Activity)context;
55 break;
56 }
57 context = ((ContextWrapper)context).getBaseContext();
58 }
59 if (mActivity == null) {
60 throw new IllegalStateException("The ActivityView's Context is not an Activity.");
61 }
62
63 mTextureView = new TextureView(context);
64 mTextureView.setSurfaceTextureListener(new ActivityViewSurfaceTextureListener());
65 addView(mTextureView);
66 }
67
68 @Override
69 protected void onLayout(boolean changed, int l, int t, int r, int b) {
70 mTextureView.layout(l, t, r, b);
71 }
72
73 @Override
74 protected void onAttachedToWindow() {
75 try {
76 final IBinder token = mActivity.getActivityToken();
77 mActivityContainer =
78 ActivityManagerNative.getDefault().createActivityContainer(token, null);
79 } catch (RemoteException e) {
80 throw new IllegalStateException("ActivityView: Unable to create ActivityContainer. "
81 + e);
82 }
83
84 final SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
85 if (surfaceTexture != null) {
86 createActivityView(surfaceTexture);
87 }
88 }
89
90 @Override
91 protected void onDetachedFromWindow() {
92 if (mActivityContainer != null) {
93 try {
94 mActivityContainer.deleteActivityView();
95 } catch (RemoteException e) {
96 }
97 mActivityContainer = null;
98 }
99 mAttached = false;
100 }
101
102 public void startActivity(Intent intent) {
103 if (mActivityContainer != null && mAttached) {
104 try {
105 mActivityContainer.startActivity(intent);
106 } catch (RemoteException e) {
107 throw new IllegalStateException("ActivityView: Unable to startActivity. " + e);
108 }
109 }
110 }
111
112 /** Call when both mActivityContainer and mTextureView's SurfaceTexture are not null */
113 private void createActivityView(SurfaceTexture surfaceTexture) {
114 WindowManager wm = (WindowManager)mActivity.getSystemService(Context.WINDOW_SERVICE);
115 DisplayMetrics metrics = new DisplayMetrics();
116 wm.getDefaultDisplay().getMetrics(metrics);
117
118 try {
119 mActivityContainer.createActivityView(new Surface(surfaceTexture), mWidth, mHeight,
120 metrics.densityDpi);
121 } catch (RemoteException e) {
122 mActivityContainer = null;
123 throw new IllegalStateException(
124 "ActivityView: Unable to create ActivityContainer. " + e);
125 }
126 mAttached = true;
127 }
128
129 private class ActivityViewSurfaceTextureListener implements SurfaceTextureListener {
130 @Override
131 public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width,
132 int height) {
133 mWidth = width;
134 mHeight = height;
135 if (mActivityContainer != null) {
136 createActivityView(surfaceTexture);
137 }
138 }
139
140 @Override
141 public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width,
142 int height) {
143 }
144
145 @Override
146 public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
147 try {
148 mActivityContainer.deleteActivityView();
149 // TODO: Add binderDied to handle this nullification.
150 mActivityContainer = null;
151 } catch (RemoteException r) {
152 }
153 mAttached = false;
154 return false;
155 }
156
157 @Override
158 public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
159
160 }
161
162 }
163}