PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 6ms RepoModel.GetById 34ms app.codeStats 0ms

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