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