/RockLock/src/com/marvin/rocklock/ScreenOnHandlerService.java

http://eyes-free.googlecode.com/ · Java · 76 lines · 44 code · 10 blank · 22 comment · 3 complexity · c0fbf94f4d7a8b0771ee0ef66e5c80dc MD5 · raw file

  1. /*
  2. * Copyright (C) 2010 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.marvin.rocklock;
  17. import android.app.Service;
  18. import android.content.BroadcastReceiver;
  19. import android.content.Context;
  20. import android.content.Intent;
  21. import android.content.IntentFilter;
  22. import android.content.SharedPreferences;
  23. import android.os.IBinder;
  24. import android.preference.PreferenceManager;
  25. /**
  26. * Service that registers a receiver to catch the screen on intent and launches
  27. * the main RockLockActivity.
  28. *
  29. * @author clchen@google.com (Charles L. Chen)
  30. */
  31. public class ScreenOnHandlerService extends Service {
  32. private BroadcastReceiver screenwakeup = new BroadcastReceiver() {
  33. public static final String TAG = "screen wakeup";
  34. public static final String SCREEN = "android.intent.action.SCREEN_ON";
  35. @Override
  36. public void onReceive(Context context, Intent intent) {
  37. if (!intent.getAction().equals(SCREEN)) {
  38. return;
  39. }
  40. SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
  41. boolean useAsPrelock = sharedPrefs.getBoolean("use_as_prelock", false);
  42. if (useAsPrelock) {
  43. Intent i = new Intent(context, RockLockActivity.class);
  44. i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  45. i.putExtra(RockLockActivity.EXTRA_STARTED_BY_SERVICE, true);
  46. context.startActivity(i);
  47. }
  48. }
  49. };
  50. @Override
  51. public IBinder onBind(Intent arg0) {
  52. // Do nothing
  53. return null;
  54. }
  55. @Override
  56. public void onCreate() {
  57. super.onCreate();
  58. SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
  59. boolean useAsPrelock = sharedPrefs.getBoolean("use_as_prelock", false);
  60. if (useAsPrelock) {
  61. this.startForeground(0, null);
  62. IntentFilter onfilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
  63. this.registerReceiver(screenwakeup, onfilter);
  64. }
  65. }
  66. }