John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
Jeff Brown | cef440f | 2012-09-25 18:58:48 -0700 | [diff] [blame] | 17 | package com.android.server.dreams; |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 18 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.ServiceConnection; |
| 23 | import android.os.Binder; |
| 24 | import android.os.Handler; |
| 25 | import android.os.IBinder; |
| 26 | import android.os.RemoteException; |
| 27 | import android.os.IBinder.DeathRecipient; |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 28 | import android.os.UserHandle; |
Dianne Hackborn | be87e2f | 2012-09-28 16:31:34 -0700 | [diff] [blame] | 29 | import android.service.dreams.DreamService; |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 30 | import android.service.dreams.IDreamService; |
| 31 | import android.util.Slog; |
| 32 | import android.view.IWindowManager; |
| 33 | import android.view.WindowManager; |
| 34 | import android.view.WindowManagerGlobal; |
| 35 | |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 36 | import java.io.PrintWriter; |
| 37 | import java.util.NoSuchElementException; |
| 38 | |
| 39 | /** |
| 40 | * Internal controller for starting and stopping the current dream and managing related state. |
| 41 | * |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 42 | * Assumes all operations are called from the dream handler thread. |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 43 | */ |
| 44 | final class DreamController { |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 45 | private static final String TAG = "DreamController"; |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 46 | |
| 47 | private final Context mContext; |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 48 | private final Handler mHandler; |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 49 | private final Listener mListener; |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 50 | private final IWindowManager mIWindowManager; |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 51 | |
Dianne Hackborn | be87e2f | 2012-09-28 16:31:34 -0700 | [diff] [blame] | 52 | private final Intent mDreamingStartedIntent = new Intent(Intent.ACTION_DREAMING_STARTED) |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 53 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Dianne Hackborn | be87e2f | 2012-09-28 16:31:34 -0700 | [diff] [blame] | 54 | private final Intent mDreamingStoppedIntent = new Intent(Intent.ACTION_DREAMING_STOPPED) |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 55 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 56 | |
John Spurlock | 591a9e8 | 2012-09-28 12:15:08 -0400 | [diff] [blame] | 57 | private final Intent mCloseNotificationShadeIntent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
| 58 | |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 59 | private DreamRecord mCurrentDream; |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 60 | |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 61 | public DreamController(Context context, Handler handler, Listener listener) { |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 62 | mContext = context; |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 63 | mHandler = handler; |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 64 | mListener = listener; |
| 65 | mIWindowManager = WindowManagerGlobal.getWindowManagerService(); |
| 66 | } |
| 67 | |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 68 | public void dump(PrintWriter pw) { |
| 69 | pw.println("Dreamland:"); |
| 70 | if (mCurrentDream != null) { |
| 71 | pw.println(" mCurrentDream:"); |
| 72 | pw.println(" mToken=" + mCurrentDream.mToken); |
| 73 | pw.println(" mName=" + mCurrentDream.mName); |
| 74 | pw.println(" mIsTest=" + mCurrentDream.mIsTest); |
| 75 | pw.println(" mUserId=" + mCurrentDream.mUserId); |
| 76 | pw.println(" mBound=" + mCurrentDream.mBound); |
| 77 | pw.println(" mService=" + mCurrentDream.mService); |
| 78 | pw.println(" mSentStartBroadcast=" + mCurrentDream.mSentStartBroadcast); |
| 79 | } else { |
| 80 | pw.println(" mCurrentDream: null"); |
| 81 | } |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 82 | } |
| 83 | |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 84 | public void startDream(Binder token, ComponentName name, boolean isTest, int userId) { |
| 85 | stopDream(); |
| 86 | |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 87 | // Close the notification shade. Don't need to send to all, but better to be explicit. |
| 88 | mContext.sendBroadcastAsUser(mCloseNotificationShadeIntent, UserHandle.ALL); |
John Spurlock | 591a9e8 | 2012-09-28 12:15:08 -0400 | [diff] [blame] | 89 | |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 90 | Slog.i(TAG, "Starting dream: name=" + name + ", isTest=" + isTest + ", userId=" + userId); |
| 91 | |
| 92 | mCurrentDream = new DreamRecord(token, name, isTest, userId); |
| 93 | |
| 94 | try { |
| 95 | mIWindowManager.addWindowToken(token, WindowManager.LayoutParams.TYPE_DREAM); |
| 96 | } catch (RemoteException ex) { |
| 97 | Slog.e(TAG, "Unable to add window token for dream.", ex); |
| 98 | stopDream(); |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 99 | return; |
| 100 | } |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 101 | |
Dianne Hackborn | be87e2f | 2012-09-28 16:31:34 -0700 | [diff] [blame] | 102 | Intent intent = new Intent(DreamService.SERVICE_INTERFACE); |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 103 | intent.setComponent(name); |
| 104 | intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
| 105 | try { |
| 106 | if (!mContext.bindService(intent, mCurrentDream, |
| 107 | Context.BIND_AUTO_CREATE, userId)) { |
| 108 | Slog.e(TAG, "Unable to bind dream service: " + intent); |
| 109 | stopDream(); |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 110 | return; |
| 111 | } |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 112 | } catch (SecurityException ex) { |
| 113 | Slog.e(TAG, "Unable to bind dream service: " + intent, ex); |
| 114 | stopDream(); |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 115 | return; |
| 116 | } |
| 117 | |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 118 | mCurrentDream.mBound = true; |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 119 | } |
| 120 | |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 121 | public void stopDream() { |
| 122 | if (mCurrentDream == null) { |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 123 | return; |
| 124 | } |
| 125 | |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 126 | final DreamRecord oldDream = mCurrentDream; |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 127 | mCurrentDream = null; |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 128 | Slog.i(TAG, "Stopping dream: name=" + oldDream.mName |
| 129 | + ", isTest=" + oldDream.mIsTest + ", userId=" + oldDream.mUserId); |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 130 | |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 131 | if (oldDream.mSentStartBroadcast) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 132 | mContext.sendBroadcastAsUser(mDreamingStoppedIntent, UserHandle.ALL); |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | if (oldDream.mService != null) { |
Daniel Sandler | 2d78490 | 2012-10-03 23:04:50 -0400 | [diff] [blame] | 136 | // Tell the dream that it's being stopped so that |
| 137 | // it can shut down nicely before we yank its window token out from |
| 138 | // under it. |
| 139 | try { |
| 140 | oldDream.mService.detach(); |
| 141 | } catch (RemoteException ex) { |
| 142 | // we don't care; this thing is on the way out |
| 143 | } |
| 144 | |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 145 | try { |
| 146 | oldDream.mService.asBinder().unlinkToDeath(oldDream, 0); |
| 147 | } catch (NoSuchElementException ex) { |
| 148 | // don't care |
| 149 | } |
| 150 | oldDream.mService = null; |
| 151 | } |
| 152 | |
| 153 | if (oldDream.mBound) { |
| 154 | mContext.unbindService(oldDream); |
| 155 | } |
| 156 | |
| 157 | try { |
| 158 | mIWindowManager.removeWindowToken(oldDream.mToken); |
| 159 | } catch (RemoteException ex) { |
| 160 | Slog.w(TAG, "Error removing window token for dream.", ex); |
| 161 | } |
| 162 | |
| 163 | mHandler.post(new Runnable() { |
| 164 | @Override |
| 165 | public void run() { |
| 166 | mListener.onDreamStopped(oldDream.mToken); |
| 167 | } |
| 168 | }); |
| 169 | } |
| 170 | |
| 171 | private void attach(IDreamService service) { |
| 172 | try { |
| 173 | service.asBinder().linkToDeath(mCurrentDream, 0); |
| 174 | service.attach(mCurrentDream.mToken); |
| 175 | } catch (RemoteException ex) { |
| 176 | Slog.e(TAG, "The dream service died unexpectedly.", ex); |
| 177 | stopDream(); |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | mCurrentDream.mService = service; |
| 182 | |
| 183 | if (!mCurrentDream.mIsTest) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 184 | mContext.sendBroadcastAsUser(mDreamingStartedIntent, UserHandle.ALL); |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 185 | mCurrentDream.mSentStartBroadcast = true; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Callback interface to be implemented by the {@link DreamManagerService}. |
| 191 | */ |
| 192 | public interface Listener { |
| 193 | void onDreamStopped(Binder token); |
| 194 | } |
| 195 | |
| 196 | private final class DreamRecord implements DeathRecipient, ServiceConnection { |
| 197 | public final Binder mToken; |
| 198 | public final ComponentName mName; |
| 199 | public final boolean mIsTest; |
| 200 | public final int mUserId; |
| 201 | |
| 202 | public boolean mBound; |
| 203 | public IDreamService mService; |
| 204 | public boolean mSentStartBroadcast; |
| 205 | |
| 206 | public DreamRecord(Binder token, ComponentName name, |
| 207 | boolean isTest, int userId) { |
| 208 | mToken = token; |
| 209 | mName = name; |
| 210 | mIsTest = isTest; |
| 211 | mUserId = userId; |
| 212 | } |
| 213 | |
| 214 | // May be called on any thread. |
| 215 | @Override |
| 216 | public void binderDied() { |
| 217 | mHandler.post(new Runnable() { |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 218 | @Override |
| 219 | public void run() { |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 220 | mService = null; |
| 221 | if (mCurrentDream == DreamRecord.this) { |
| 222 | stopDream(); |
| 223 | } |
| 224 | } |
| 225 | }); |
| 226 | } |
| 227 | |
| 228 | // May be called on any thread. |
| 229 | @Override |
| 230 | public void onServiceConnected(ComponentName name, final IBinder service) { |
| 231 | mHandler.post(new Runnable() { |
| 232 | @Override |
| 233 | public void run() { |
| 234 | if (mCurrentDream == DreamRecord.this && mService == null) { |
| 235 | attach(IDreamService.Stub.asInterface(service)); |
| 236 | } |
| 237 | } |
| 238 | }); |
| 239 | } |
| 240 | |
| 241 | // May be called on any thread. |
| 242 | @Override |
| 243 | public void onServiceDisconnected(ComponentName name) { |
| 244 | mHandler.post(new Runnable() { |
| 245 | @Override |
| 246 | public void run() { |
| 247 | mService = null; |
| 248 | if (mCurrentDream == DreamRecord.this) { |
| 249 | stopDream(); |
| 250 | } |
| 251 | } |
| 252 | }); |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 253 | } |
| 254 | } |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 255 | } |