PollingService.java revision 7011540781e2e683ed34bc970f2f9ac3e248f092
1/*
2 * Copyright (c) 2015, Motorola Mobility LLC
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *     - Redistributions of source code must retain the above copyright
8 *       notice, this list of conditions and the following disclaimer.
9 *     - Redistributions in binary form must reproduce the above copyright
10 *       notice, this list of conditions and the following disclaimer in the
11 *       documentation and/or other materials provided with the distribution.
12 *     - Neither the name of Motorola Mobility nor the
13 *       names of its contributors may be used to endorse or promote products
14 *       derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MOTOROLA MOBILITY LLC BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 * DAMAGE.
27 */
28
29package com.android.service.ims.presence;
30
31import android.app.Service;
32import android.content.Context;
33import android.content.Intent;
34import android.telephony.CarrierConfigManager;
35import android.os.Handler;
36import android.os.IBinder;
37import android.os.PersistableBundle;
38import android.os.RemoteException;
39import android.os.ServiceManager;
40import android.os.SystemProperties;
41
42import com.android.ims.internal.Logger;
43
44public class PollingService extends Service {
45    /**
46     * The logger
47     */
48    private Logger logger = Logger.getLogger(this.getClass().getName());
49
50    private CapabilityPolling mCapabilityPolling = null;
51
52    /**
53     * Constructor
54     */
55    public PollingService() {
56        logger.debug("PollingService()");
57    }
58
59    @Override
60    public void onCreate() {
61        logger.debug("onCreate()");
62
63        if (isEabSupported(this)) {
64            mCapabilityPolling = CapabilityPolling.getInstance(this);
65            mCapabilityPolling.start();
66        }
67    }
68
69    @Override
70    public int onStartCommand(Intent intent, int flags, int startId) {
71        logger.debug("onStartCommand(), intent: " + intent +
72                ", flags: " + flags + ", startId: " + startId);
73
74        if (!isRcsSupported(this)) {
75            stopSelf();
76            return START_NOT_STICKY;
77        }
78
79        return super.onStartCommand(intent, flags, startId);
80    }
81
82    /**
83      * Cleans up when the service is destroyed
84      */
85    @Override
86    public void onDestroy() {
87        logger.debug("onDestroy()");
88
89        if (mCapabilityPolling != null) {
90            mCapabilityPolling.stop();
91            mCapabilityPolling = null;
92        }
93
94        super.onDestroy();
95    }
96
97    @Override
98    public IBinder onBind(Intent intent) {
99        logger.debug("onBind(), intent: " + intent);
100
101        if (!isRcsSupported(this)) {
102            return null;
103        }
104
105        logger.debug("onBind add services here");
106        return null;
107    }
108
109    static boolean isRcsSupported(Context context) {
110        return isRcsSupportedByDevice() && isRcsSupportedByCarrier(context);
111    }
112
113    private static boolean isEabSupported(Context context) {
114        return isEabSupportedByDevice() && isRcsSupportedByCarrier(context);
115    }
116
117    private static boolean isRcsSupportedByCarrier(Context context) {
118        CarrierConfigManager configManager = context.getSystemService(CarrierConfigManager.class);
119        if (configManager != null) {
120            PersistableBundle b = configManager.getConfig();
121            if (b != null) {
122                return b.getBoolean(CarrierConfigManager.KEY_USE_RCS_PRESENCE_BOOL, false);
123            }
124        }
125        return true;
126    }
127
128    private static boolean isRcsSupportedByDevice() {
129        String rcsSupported = SystemProperties.get("persist.rcs.supported");
130        return "1".equals(rcsSupported);
131    }
132
133    private static boolean isEabSupportedByDevice() {
134        String eabSupported = SystemProperties.get("persist.eab.supported");
135        return ("0".equals(eabSupported)) ? false : true;
136    }
137}
138