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