Somnambulator.java revision af3f32d9a460b663d8f757f771cb220cc78b190a
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 17package com.android.systemui; 18 19import android.app.Activity; 20import android.content.Intent; 21import android.os.RemoteException; 22import android.os.ServiceManager; 23import android.os.UserHandle; 24import android.provider.Settings; 25import android.service.dreams.DreamService; 26import android.service.dreams.IDreamManager; 27import android.util.Slog; 28 29public class Somnambulator extends Activity { 30 public static final String TAG = "Somnambulator"; 31 32 public static final int DEFAULT_SCREENSAVER_ENABLED = 1; 33 public static final int DEFAULT_SCREENSAVER_ACTIVATED_ON_DOCK = 1; 34 35 public Somnambulator() { 36 } 37 38 private boolean isScreenSaverEnabled() { 39 return Settings.Secure.getIntForUser(getContentResolver(), 40 Settings.Secure.SCREENSAVER_ENABLED, DEFAULT_SCREENSAVER_ENABLED, 41 UserHandle.USER_CURRENT) != 0; 42 } 43 44 private boolean isScreenSaverActivatedOnDock() { 45 return Settings.Secure.getIntForUser(getContentResolver(), 46 Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK, 47 DEFAULT_SCREENSAVER_ACTIVATED_ON_DOCK, UserHandle.USER_CURRENT) != 0; 48 } 49 50 @Override 51 public void onStart() { 52 super.onStart(); 53 final Intent launchIntent = getIntent(); 54 final String action = launchIntent.getAction(); 55 if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) { 56 Intent shortcutIntent = new Intent(this, Somnambulator.class); 57 shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS 58 | Intent.FLAG_ACTIVITY_NEW_TASK); 59 Intent resultIntent = new Intent(); 60 resultIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, 61 Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_launcher_dreams)); 62 resultIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 63 resultIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.start_dreams)); 64 setResult(RESULT_OK, resultIntent); 65 } else { 66 boolean docked = launchIntent.hasCategory(Intent.CATEGORY_DESK_DOCK); 67 68 if (docked && !(isScreenSaverEnabled() && isScreenSaverActivatedOnDock())) { 69 Slog.i(TAG, "Dreams currently disabled for docks."); 70 } else { 71 IDreamManager somnambulist = IDreamManager.Stub.asInterface( 72 ServiceManager.checkService(DreamService.DREAM_SERVICE)); 73 if (somnambulist != null) { 74 try { 75 Slog.v(TAG, "Dreaming on " + (docked ? "dock insertion" : "user request")); 76 somnambulist.dream(); 77 } catch (RemoteException e) { 78 // fine, stay asleep then 79 } 80 } 81 } 82 } 83 finish(); 84 } 85 86} 87