PageRenderTime 28ms CodeModel.GetById 12ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/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
17package com.marvin.rocklock;
18
19import android.app.Service;
20import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
24import android.content.SharedPreferences;
25import android.os.IBinder;
26import android.preference.PreferenceManager;
27
28/**
29 * Service that registers a receiver to catch the screen on intent and launches
30 * the main RockLockActivity.
31 * 
32 * @author clchen@google.com (Charles L. Chen)
33 */
34public class ScreenOnHandlerService extends Service {
35    private BroadcastReceiver screenwakeup = new BroadcastReceiver() {
36
37        public static final String TAG = "screen wakeup";
38
39        public static final String SCREEN = "android.intent.action.SCREEN_ON";
40
41        @Override
42        public void onReceive(Context context, Intent intent) {
43            if (!intent.getAction().equals(SCREEN)) {
44                return;
45            }
46
47            SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
48            boolean useAsPrelock = sharedPrefs.getBoolean("use_as_prelock", false);
49            if (useAsPrelock) {
50                Intent i = new Intent(context, RockLockActivity.class);
51                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
52                i.putExtra(RockLockActivity.EXTRA_STARTED_BY_SERVICE, true);
53                context.startActivity(i);
54            }
55        }
56    };
57
58    @Override
59    public IBinder onBind(Intent arg0) {
60        // Do nothing
61        return null;
62    }
63
64    @Override
65    public void onCreate() {
66        super.onCreate();
67        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
68        boolean useAsPrelock = sharedPrefs.getBoolean("use_as_prelock", false);
69        if (useAsPrelock) {
70            this.startForeground(0, null);
71            IntentFilter onfilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
72            this.registerReceiver(screenwakeup, onfilter);
73        }
74    }
75
76}