1 /*
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 
17 package com.android.contacts;
18 
19 import android.app.Service;
20 import android.content.Intent;
21 import android.content.Loader;
22 import android.content.Loader.OnLoadCompleteListener;
23 import android.os.IBinder;
24 import android.util.Log;
25 
26 import com.android.contacts.model.Contact;
27 import com.android.contacts.model.ContactLoader;
28 
29 
30 /**
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  */
34 public class ViewNotificationService extends Service {
35     private static final String TAG = ViewNotificationService.class.getSimpleName();
36 
37     private static final boolean DEBUG = false;
38 
39     @Override
onStartCommand(Intent intent, int flags, final int startId)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.
45         final ContactLoader contactLoader = new ContactLoader(this, intent.getData(), true);
46         contactLoader.registerListener(0, new OnLoadCompleteListener<Contact>() {
47             @Override
48             public void onLoadComplete(Loader<Contact> loader, Contact data) {
49                 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
onBind(Intent intent)72     public IBinder onBind(Intent intent) {
73         return null;
74     }
75 }
76