DreamManagerService.java revision cef440f2a2bb8b6e8d082d12a67dc21f2ee65e3c
1f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock/* 2f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock * Copyright (C) 2012 The Android Open Source Project 3f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock * 4f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock * Licensed under the Apache License, Version 2.0 (the "License"); 5f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock * you may not use this file except in compliance with the License. 6f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock * You may obtain a copy of the License at 7f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock * 8f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock * http://www.apache.org/licenses/LICENSE-2.0 9f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock * 10f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock * Unless required by applicable law or agreed to in writing, software 11f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock * distributed under the License is distributed on an "AS IS" BASIS, 12f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock * See the License for the specific language governing permissions and 14f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock * limitations under the License. 15f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock */ 16f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 17cef440f2a2bb8b6e8d082d12a67dc21f2ee65e3cJeff Brownpackage com.android.server.dreams; 18f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 19f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlockimport static android.provider.Settings.Secure.SCREENSAVER_COMPONENTS; 20f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlockimport static android.provider.Settings.Secure.SCREENSAVER_DEFAULT_COMPONENT; 21f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 22f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlockimport android.app.ActivityManagerNative; 23f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlockimport android.content.BroadcastReceiver; 24f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlockimport android.content.ComponentName; 25f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlockimport android.content.Context; 26f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlockimport android.content.Intent; 27f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlockimport android.content.IntentFilter; 28f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlockimport android.content.ServiceConnection; 29f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlockimport android.content.pm.PackageManager; 30f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlockimport android.os.Binder; 31f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlockimport android.os.Handler; 32f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlockimport android.os.IBinder; 33f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlockimport android.os.RemoteException; 34f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlockimport android.os.UserHandle; 35f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlockimport android.provider.Settings; 36f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlockimport android.service.dreams.Dream; 37f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlockimport android.service.dreams.IDreamManager; 38f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlockimport android.util.Slog; 39f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 40f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlockimport java.io.FileDescriptor; 41f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlockimport java.io.PrintWriter; 42f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 43f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock/** 44f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock * Service api for managing dreams. 45f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock * 46f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock * @hide 47f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock */ 48f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlockpublic final class DreamManagerService 49f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock extends IDreamManager.Stub 50f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock implements ServiceConnection { 51f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock private static final boolean DEBUG = true; 52f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock private static final String TAG = DreamManagerService.class.getSimpleName(); 53f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 54f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock private static final Intent mDreamingStartedIntent = new Intent(Dream.ACTION_DREAMING_STARTED) 55f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); 56f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock private static final Intent mDreamingStoppedIntent = new Intent(Dream.ACTION_DREAMING_STOPPED) 57f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); 58f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 59f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock private final Object mLock = new Object(); 60f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock private final DreamController mController; 61f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock private final DreamControllerHandler mHandler; 62f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock private final Context mContext; 63f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 64f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock private final CurrentUserManager mCurrentUserManager = new CurrentUserManager(); 65f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 66f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock private final DeathRecipient mAwakenOnBinderDeath = new DeathRecipient() { 67f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock @Override 68f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public void binderDied() { 69f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock if (DEBUG) Slog.v(TAG, "binderDied()"); 70f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock awaken(); 71f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 72f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock }; 73f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 74f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock private final DreamController.Listener mControllerListener = new DreamController.Listener() { 75f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock @Override 76f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public void onDreamStopped(boolean wasTest) { 77f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock synchronized(mLock) { 78f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock setDreamingLocked(false, wasTest); 79f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 80f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock }}; 81f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 82f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock private boolean mIsDreaming; 83f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 84f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public DreamManagerService(Context context) { 85f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock if (DEBUG) Slog.v(TAG, "DreamManagerService startup"); 86f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock mContext = context; 87f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock mController = new DreamController(context, mAwakenOnBinderDeath, this, mControllerListener); 88f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock mHandler = new DreamControllerHandler(mController); 89f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock mController.setHandler(mHandler); 90f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 91f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 92f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public void systemReady() { 93f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock mCurrentUserManager.init(mContext); 94f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 95f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock if (DEBUG) Slog.v(TAG, "Ready to dream!"); 96f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 97f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 98f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock @Override 99f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 100f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); 101f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 102f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock pw.println("Dreamland:"); 103f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock mController.dump(pw); 104f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock mCurrentUserManager.dump(pw); 105f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 106f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 107f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock // begin IDreamManager api 108f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock @Override 109f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public ComponentName[] getDreamComponents() { 110f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock checkPermission(android.Manifest.permission.READ_DREAM_STATE); 111f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock int userId = UserHandle.getCallingUserId(); 112f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 113f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock final long ident = Binder.clearCallingIdentity(); 114f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock try { 115f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock return getDreamComponentsForUser(userId); 116f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } finally { 117f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock Binder.restoreCallingIdentity(ident); 118f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 119f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 120f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 121f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock @Override 122f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public void setDreamComponents(ComponentName[] componentNames) { 123f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock checkPermission(android.Manifest.permission.WRITE_DREAM_STATE); 124f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock int userId = UserHandle.getCallingUserId(); 125f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 126f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock final long ident = Binder.clearCallingIdentity(); 127f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock try { 128f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock Settings.Secure.putStringForUser(mContext.getContentResolver(), 129f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock SCREENSAVER_COMPONENTS, 130f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock componentsToString(componentNames), 131f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock userId); 132f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } finally { 133f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock Binder.restoreCallingIdentity(ident); 134f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 135f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 136f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 137f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock @Override 138f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public ComponentName getDefaultDreamComponent() { 139f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock checkPermission(android.Manifest.permission.READ_DREAM_STATE); 140f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock int userId = UserHandle.getCallingUserId(); 141f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 142f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock final long ident = Binder.clearCallingIdentity(); 143f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock try { 144f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock String name = Settings.Secure.getStringForUser(mContext.getContentResolver(), 145f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock SCREENSAVER_DEFAULT_COMPONENT, 146f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock userId); 147f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock return name == null ? null : ComponentName.unflattenFromString(name); 148f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } finally { 149f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock Binder.restoreCallingIdentity(ident); 150f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 151f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 152f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 153f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 154f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock @Override 155f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public boolean isDreaming() { 156f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock checkPermission(android.Manifest.permission.READ_DREAM_STATE); 157f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 158f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock return mIsDreaming; 159f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 160f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 161f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock @Override 162f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public void dream() { 163f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock checkPermission(android.Manifest.permission.WRITE_DREAM_STATE); 164f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 165f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock final long ident = Binder.clearCallingIdentity(); 166f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock try { 167f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock if (DEBUG) Slog.v(TAG, "Dream now"); 168f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock ComponentName[] dreams = getDreamComponentsForUser(mCurrentUserManager.getCurrentUserId()); 169f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock ComponentName firstDream = dreams != null && dreams.length > 0 ? dreams[0] : null; 170f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock if (firstDream != null) { 171f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock mHandler.requestStart(firstDream, false /*isTest*/); 172f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock synchronized (mLock) { 173f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock setDreamingLocked(true, false /*isTest*/); 174f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 175f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 176f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } finally { 177f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock Binder.restoreCallingIdentity(ident); 178f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 179f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 180f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 181f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock @Override 182f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public void testDream(ComponentName dream) { 183f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock checkPermission(android.Manifest.permission.WRITE_DREAM_STATE); 184f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 185f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock final long ident = Binder.clearCallingIdentity(); 186f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock try { 187f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock if (DEBUG) Slog.v(TAG, "Test dream name=" + dream); 188f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock if (dream != null) { 189f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock mHandler.requestStart(dream, true /*isTest*/); 190f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock synchronized (mLock) { 191f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock setDreamingLocked(true, true /*isTest*/); 192f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 193f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 194f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } finally { 195f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock Binder.restoreCallingIdentity(ident); 196f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 197f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 198f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 199f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 200f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock @Override 201f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public void awaken() { 202f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock checkPermission(android.Manifest.permission.WRITE_DREAM_STATE); 203f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 204f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock final long ident = Binder.clearCallingIdentity(); 205f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock try { 206f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock if (DEBUG) Slog.v(TAG, "Wake up"); 207f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock mHandler.requestStop(); 208f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } finally { 209f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock Binder.restoreCallingIdentity(ident); 210f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 211f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 212f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 213f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock @Override 214f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public void awakenSelf(IBinder token) { 215f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock // requires no permission, called by Dream from an arbitrary process 216f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 217f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock final long ident = Binder.clearCallingIdentity(); 218f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock try { 219f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock if (DEBUG) Slog.v(TAG, "Wake up from dream: " + token); 220f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock if (token != null) { 221f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock mHandler.requestStopSelf(token); 222f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 223f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } finally { 224f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock Binder.restoreCallingIdentity(ident); 225f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 226f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 227f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock // end IDreamManager api 228f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 229f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock // begin ServiceConnection 230f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock @Override 231f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public void onServiceConnected(ComponentName name, IBinder dream) { 232f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock if (DEBUG) Slog.v(TAG, "Service connected: " + name + " binder=" + 233f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock dream + " thread=" + Thread.currentThread().getId()); 234f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock mHandler.requestAttach(name, dream); 235f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 236f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 237f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock @Override 238f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public void onServiceDisconnected(ComponentName name) { 239f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock if (DEBUG) Slog.v(TAG, "Service disconnected: " + name); 240f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock // Only happens in exceptional circumstances, awaken just to be safe 241f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock awaken(); 242f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 243f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock // end ServiceConnection 244f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 245f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock private void checkPermission(String permission) { 246f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock if (PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission(permission)) { 247f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock throw new SecurityException("Access denied to process: " + Binder.getCallingPid() 248f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock + ", must have permission " + permission); 249f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 250f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 251f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 252f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock private void setDreamingLocked(boolean isDreaming, boolean isTest) { 253f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock boolean wasDreaming = mIsDreaming; 254f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock if (!isTest) { 255f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock if (!wasDreaming && isDreaming) { 256f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock if (DEBUG) Slog.v(TAG, "Firing ACTION_DREAMING_STARTED"); 257f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock mContext.sendBroadcast(mDreamingStartedIntent); 258f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } else if (wasDreaming && !isDreaming) { 259f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock if (DEBUG) Slog.v(TAG, "Firing ACTION_DREAMING_STOPPED"); 260f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock mContext.sendBroadcast(mDreamingStoppedIntent); 261f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 262f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 263f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock mIsDreaming = isDreaming; 264f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 265f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 266f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock private ComponentName[] getDreamComponentsForUser(int userId) { 267f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock String names = Settings.Secure.getStringForUser(mContext.getContentResolver(), 268f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock SCREENSAVER_COMPONENTS, 269f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock userId); 270f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock return names == null ? null : componentsFromString(names); 271f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 272f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 273f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock private static String componentsToString(ComponentName[] componentNames) { 274f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock StringBuilder names = new StringBuilder(); 275f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock if (componentNames != null) { 276f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock for (ComponentName componentName : componentNames) { 277f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock if (names.length() > 0) { 278f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock names.append(','); 279f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 280f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock names.append(componentName.flattenToString()); 281f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 282f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 283f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock return names.toString(); 284f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 285f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 286f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock private static ComponentName[] componentsFromString(String names) { 287f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock String[] namesArray = names.split(","); 288f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock ComponentName[] componentNames = new ComponentName[namesArray.length]; 289f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock for (int i = 0; i < namesArray.length; i++) { 290f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock componentNames[i] = ComponentName.unflattenFromString(namesArray[i]); 291f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 292f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock return componentNames; 293f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 294f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 295f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock /** 296f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock * Keeps track of the current user, since dream() uses the current user's configuration. 297f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock */ 298f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock private static class CurrentUserManager { 299f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock private final Object mLock = new Object(); 300f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock private int mCurrentUserId; 301f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 302f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public void init(Context context) { 303f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock IntentFilter filter = new IntentFilter(); 304f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock filter.addAction(Intent.ACTION_USER_SWITCHED); 305f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock context.registerReceiver(new BroadcastReceiver() { 306f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock @Override 307f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public void onReceive(Context context, Intent intent) { 308f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock String action = intent.getAction(); 309f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock if (Intent.ACTION_USER_SWITCHED.equals(action)) { 310f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock synchronized(mLock) { 311f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock mCurrentUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); 312f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock if (DEBUG) Slog.v(TAG, "userId " + mCurrentUserId + " is in the house"); 313f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 314f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 315f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock }}, filter); 316f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock try { 317f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock synchronized (mLock) { 318f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock mCurrentUserId = ActivityManagerNative.getDefault().getCurrentUser().id; 319f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 320f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } catch (RemoteException e) { 321f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e); 322f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 323f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 324f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 325f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public void dump(PrintWriter pw) { 326f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock pw.print(" user="); pw.println(getCurrentUserId()); 327f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 328f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 329f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public int getCurrentUserId() { 330f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock synchronized(mLock) { 331f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock return mCurrentUserId; 332f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 333f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 334f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 335f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 336f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock /** 337f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock * Handler for asynchronous operations performed by the dream manager. 338f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock * 339f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock * Ensures operations to {@link DreamController} are single-threaded. 340f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock */ 341f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock private static final class DreamControllerHandler extends Handler { 342f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock private final DreamController mController; 343f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock private final Runnable mStopRunnable = new Runnable() { 344f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock @Override 345f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public void run() { 346f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock mController.stop(); 347f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock }}; 348f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 349f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public DreamControllerHandler(DreamController controller) { 350f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock super(true /*async*/); 351f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock mController = controller; 352f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 353f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 354f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public void requestStart(final ComponentName name, final boolean isTest) { 355f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock post(new Runnable(){ 356f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock @Override 357f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public void run() { 358f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock mController.start(name, isTest); 359f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock }}); 360f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 361f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 362f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public void requestAttach(final ComponentName name, final IBinder dream) { 363f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock post(new Runnable(){ 364f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock @Override 365f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public void run() { 366f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock mController.attach(name, dream); 367f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock }}); 368f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 369f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 370f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public void requestStopSelf(final IBinder token) { 371f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock post(new Runnable(){ 372f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock @Override 373f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public void run() { 374f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock mController.stopSelf(token); 375f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock }}); 376f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 377f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 378f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock public void requestStop() { 379f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock post(mStopRunnable); 380f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 381f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 382f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock } 383f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock 384f4f6b4c8b0fcf77d46567f13b409255948fe107bJohn Spurlock} 385