PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/src/com/commonsware/cwac/wakeful/WakefulIntentService.java

http://sofurry-android-app.googlecode.com/
Java | 98 lines | 54 code | 26 blank | 18 comment | 5 complexity | 1042595b677659311d72cd92e4114949 MD5 | raw file
  1. /***
  2. Copyright (c) 2009 CommonsWare, LLC
  3. Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. not use this file except in compliance with the License. You may obtain
  5. a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.commonsware.cwac.wakeful;
  14. //~--- imports ----------------------------------------------------------------
  15. import android.app.IntentService;
  16. import android.content.Context;
  17. import android.content.Intent;
  18. import android.content.pm.PackageManager;
  19. import android.os.PowerManager;
  20. //~--- classes ----------------------------------------------------------------
  21. abstract public class WakefulIntentService
  22. extends IntentService {
  23. private static final String LOCK_NAME_STATIC =
  24. "com.commonsware.cwac.wakeful.WakefulIntentService";
  25. private static PowerManager.WakeLock lockStatic = null;
  26. //~--- constructors -------------------------------------------------------
  27. public WakefulIntentService(String name) {
  28. super(name);
  29. }
  30. //~--- methods ------------------------------------------------------------
  31. abstract protected void doWakefulWork(Intent intent);
  32. @Override
  33. final protected void onHandleIntent(Intent intent) {
  34. try {
  35. doWakefulWork(intent);
  36. } finally {
  37. getLock(this).release();
  38. }
  39. }
  40. @Override
  41. public void onStart(Intent intent, int startId) {
  42. if (!getLock(this).isHeld()) { // fail-safe for crash restart
  43. getLock(this).acquire();
  44. }
  45. super.onStart(intent, startId);
  46. }
  47. public static void sendWakefulWork(Context ctxt, Class clsService) {
  48. sendWakefulWork(ctxt, new Intent(ctxt, clsService));
  49. }
  50. public static void sendWakefulWork(Context ctxt, Intent i) {
  51. if (PackageManager.PERMISSION_DENIED
  52. == ctxt.getPackageManager().checkPermission(
  53. "android.permission.WAKE_LOCK", ctxt.getPackageName())) {
  54. throw new RuntimeException(
  55. "Application requires the WAKE_LOCK permission!");
  56. }
  57. getLock(ctxt).acquire();
  58. ctxt.startService(i);
  59. }
  60. //~--- get methods --------------------------------------------------------
  61. synchronized private static PowerManager.WakeLock getLock(Context context) {
  62. if (lockStatic == null) {
  63. PowerManager mgr =
  64. (PowerManager) context.getSystemService(Context.POWER_SERVICE);
  65. lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
  66. LOCK_NAME_STATIC);
  67. lockStatic.setReferenceCounted(true);
  68. }
  69. return (lockStatic);
  70. }
  71. }