blob: 584176d1a32f446b9f385c3f537cf3bb6584ef13 [file] [log] [blame]
Daniel Lehmann93597c32012-04-19 17:01:17 -07001/*
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.contacts;
18
Daniel Lehmann93597c32012-04-19 17:01:17 -070019import android.app.Service;
20import android.content.Intent;
21import android.content.Loader;
22import android.content.Loader.OnLoadCompleteListener;
23import android.os.IBinder;
24import android.util.Log;
25
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070026import com.android.contacts.ContactLoader.Result;
27
Daniel Lehmann93597c32012-04-19 17:01:17 -070028/**
29 * Service that sends out a view notification for a contact. At the moment, this is only
30 * supposed to be used by the Phone app
31 */
32public class ViewNotificationService extends Service {
33 private static final String TAG = ViewNotificationService.class.getSimpleName();
34
35 private static final boolean DEBUG = false;
36
37 @Override
38 public int onStartCommand(Intent intent, int flags, final int startId) {
39 if (DEBUG) { Log.d(TAG, "onHandleIntent(). Intent: " + intent); }
40
41 // We simply need to start a Loader here. When its done, it will send out the
42 // View-Notification automatically.
Makoto Onuki45ee8722012-05-21 16:33:25 -070043 final ContactLoader contactLoader = new ContactLoader(this, intent.getData(), true);
Daniel Lehmann93597c32012-04-19 17:01:17 -070044 contactLoader.registerListener(0, new OnLoadCompleteListener<ContactLoader.Result>() {
45 @Override
46 public void onLoadComplete(Loader<Result> loader, Result data) {
47 try {
48 loader.reset();
49 } catch (RuntimeException e) {
50 Log.e(TAG, "Error reseting loader", e);
51 }
52 try {
53 // This is not 100% accurate actually. If we get several calls quickly,
54 // we might be stopping out-of-order, in which case the call with the last
55 // startId will stop this service. In practice, this shouldn't be a problem,
56 // as this service is supposed to be called by the Phone app which only sends
57 // out the notification once per phonecall. And even if there is a problem,
58 // the worst that should happen is a missing view notification
59 stopSelfResult(startId);
60 } catch (RuntimeException e) {
61 Log.e(TAG, "Error stopping service", e);
62 }
63 }
64 });
65 contactLoader.startLoading();
66 return START_REDELIVER_INTENT;
67 }
68
69 @Override
70 public IBinder onBind(Intent intent) {
71 return null;
72 }
73}