13483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng/*
23483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng * Copyright (C) 2012 The Android Open Source Project
33483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng *
43483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng * Licensed under the Apache License, Version 2.0 (the "License");
53483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng * you may not use this file except in compliance with the License.
63483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng * You may obtain a copy of the License at
73483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng *
83483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng *      http://www.apache.org/licenses/LICENSE-2.0
93483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng *
103483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng * Unless required by applicable law or agreed to in writing, software
113483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng * distributed under the License is distributed on an "AS IS" BASIS,
123483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng * See the License for the specific language governing permissions and
143483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng * limitations under the License.
153483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng */
163483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng
173483b810714749b4eb1a34f71c0446b6d2f2602bChiao Chengpackage com.android.contacts;
183483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng
193483b810714749b4eb1a34f71c0446b6d2f2602bChiao Chengimport android.app.Service;
203483b810714749b4eb1a34f71c0446b6d2f2602bChiao Chengimport android.content.Intent;
213483b810714749b4eb1a34f71c0446b6d2f2602bChiao Chengimport android.content.Loader;
223483b810714749b4eb1a34f71c0446b6d2f2602bChiao Chengimport android.content.Loader.OnLoadCompleteListener;
233483b810714749b4eb1a34f71c0446b6d2f2602bChiao Chengimport android.os.IBinder;
243483b810714749b4eb1a34f71c0446b6d2f2602bChiao Chengimport android.util.Log;
253483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng
263483b810714749b4eb1a34f71c0446b6d2f2602bChiao Chengimport com.android.contacts.model.Contact;
273483b810714749b4eb1a34f71c0446b6d2f2602bChiao Chengimport com.android.contacts.model.ContactLoader;
283483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng
293483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng
303483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng/**
313483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng * Service that sends out a view notification for a contact. At the moment, this is only
323483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng * supposed to be used by the Phone app
333483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng */
343483b810714749b4eb1a34f71c0446b6d2f2602bChiao Chengpublic class ViewNotificationService extends Service {
353483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng    private static final String TAG = ViewNotificationService.class.getSimpleName();
363483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng
373483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng    private static final boolean DEBUG = false;
383483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng
393483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng    @Override
403483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng    public int onStartCommand(Intent intent, int flags, final int startId) {
413483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        if (DEBUG) { Log.d(TAG, "onHandleIntent(). Intent: " + intent); }
423483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng
433483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        // We simply need to start a Loader here. When its done, it will send out the
443483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        // View-Notification automatically.
453483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        final ContactLoader contactLoader = new ContactLoader(this, intent.getData(), true);
463483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        contactLoader.registerListener(0, new OnLoadCompleteListener<Contact>() {
473483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng            @Override
483483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng            public void onLoadComplete(Loader<Contact> loader, Contact data) {
493483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng                try {
503483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng                    loader.reset();
513483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng                } catch (RuntimeException e) {
523483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng                    Log.e(TAG, "Error reseting loader", e);
533483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng                }
543483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng                try {
553483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng                    // This is not 100% accurate actually. If we get several calls quickly,
563483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng                    // we might be stopping out-of-order, in which case the call with the last
573483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng                    // startId will stop this service. In practice, this shouldn't be a problem,
583483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng                    // as this service is supposed to be called by the Phone app which only sends
593483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng                    // out the notification once per phonecall. And even if there is a problem,
603483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng                    // the worst that should happen is a missing view notification
613483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng                    stopSelfResult(startId);
623483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng                } catch (RuntimeException e) {
633483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng                    Log.e(TAG, "Error stopping service", e);
643483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng                }
653483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng            }
663483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        });
673483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        contactLoader.startLoading();
683483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        return START_REDELIVER_INTENT;
693483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng    }
703483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng
713483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng    @Override
723483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng    public IBinder onBind(Intent intent) {
733483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng        return null;
743483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng    }
753483b810714749b4eb1a34f71c0446b6d2f2602bChiao Cheng}
76