Home | History | Annotate | Download | only in uceservice
      1 /*
      2  * Copyright (c) 2016 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.ims.internal.uce.uceservice;
     18 
     19 import android.content.Context;
     20 import android.content.Intent;
     21 
     22 import android.os.Handler;
     23 import android.os.HandlerThread;
     24 import android.os.IBinder;
     25 import android.os.Message;
     26 import android.os.ServiceManager;
     27 import android.os.RemoteException;
     28 
     29 import java.util.HashMap;
     30 import android.util.Log;
     31 
     32 /**
     33  * ImsUceManager Declaration
     34  * @hide
     35  */
     36 public class ImsUceManager {
     37 
     38     private static final String LOG_TAG = "ImsUceManager";
     39     /**
     40      * Uce Service name Internal Uce only
     41      * @hide
     42      */
     43     private static final String UCE_SERVICE = "uce";
     44 
     45     /**
     46      * IUceService object
     47      * @hide
     48      */
     49     private IUceService mUceService = null;
     50     private UceServiceDeathRecipient mDeathReceipient = new UceServiceDeathRecipient();
     51     private Context mContext;
     52     private int mPhoneId;
     53     /**
     54      * Stores the UceManager instaces of Clients identified by
     55      * phoneId
     56      * @hide
     57      */
     58     private static HashMap<Integer, ImsUceManager> sUceManagerInstances =
     59                                                    new HashMap<Integer, ImsUceManager>();
     60 
     61     public static final String ACTION_UCE_SERVICE_UP =
     62                                        "com.android.ims.internal.uce.UCE_SERVICE_UP";
     63     public static final String ACTION_UCE_SERVICE_DOWN =
     64                                         "com.android.ims.internal.uce.UCE_SERVICE_DOWN";
     65 
     66     /** Uce Service status received in IUceListener.setStatus()
     67      *  callback
     68      *  @hide
     69      */
     70     public static final int UCE_SERVICE_STATUS_FAILURE = 0;
     71     /** indicate UI to call Presence/Options API.   */
     72     public static final int UCE_SERVICE_STATUS_ON = 1;
     73     /** Indicate UI destroy Presence/Options   */
     74     public static final int UCE_SERVICE_STATUS_CLOSED = 2;
     75     /** Service up and trying to register for network events  */
     76     public static final int UCE_SERVICE_STATUS_READY = 3;
     77 
     78     /**
     79      * Part of the ACTION_UCE_SERVICE_UP or _DOWN intents. A long
     80      * value; the phone ID corresponding to the IMS service coming up or down.
     81      * Internal use only.
     82      * @hide
     83      */
     84     public static final String EXTRA_PHONE_ID = "android:phone_id";
     85 
     86 
     87     /**
     88      * Gets the instance of UCE Manager
     89      * @hide
     90      */
     91     public static ImsUceManager getInstance(Context context, int phoneId) {
     92         //if (DBG) Log.d (LOG_TAG, "GetInstance Called");
     93         synchronized (sUceManagerInstances) {
     94             if (sUceManagerInstances.containsKey(phoneId)) {
     95                 return sUceManagerInstances.get(phoneId);
     96             } else {
     97                 ImsUceManager uceMgr =  new ImsUceManager(context, phoneId);
     98                 sUceManagerInstances.put(phoneId, uceMgr);
     99                 return uceMgr;
    100             }
    101         }
    102     }
    103 
    104     /**
    105      * Constructor
    106      * @hide
    107      */
    108     private ImsUceManager(Context context, int phoneId) {
    109         //if (DBG) Log.d (LOG_TAG, "Constructor");
    110         mContext = context;
    111         mPhoneId = phoneId;
    112         createUceService(true);
    113     }
    114 
    115     /**
    116      * Gets the Uce service Instance
    117      *
    118      * client should call this API only after  createUceService()
    119      * this instance is deleted when ACTION_UCE_SERVICE_DOWN event
    120      * is received.
    121      * @hide
    122      */
    123     public IUceService getUceServiceInstance() {
    124         //if (DBG) Log.d (LOG_TAG, "GetUceServiceInstance Called");
    125         return mUceService;
    126     }
    127 
    128     /**
    129      * Gets the UCE service name
    130      * @hide
    131      */
    132     private String getUceServiceName(int phoneId) {
    133         return UCE_SERVICE;
    134     }
    135 
    136     /**
    137      * Gets the IBinder to UCE service
    138      *
    139      * Client should call this after receving ACTION_UCE_SERVICE_UP
    140      * event.
    141      * @hide
    142      */
    143     public void createUceService(boolean checkService) {
    144         //if (DBG) Log.d (LOG_TAG, "CreateUceService Called");
    145         if (checkService) {
    146             IBinder binder = ServiceManager.checkService(getUceServiceName(mPhoneId));
    147 
    148             if (binder == null) {
    149                 //if (DBG)Log.d (LOG_TAG, "Unable to find IBinder");
    150                 return;
    151             }
    152         }
    153         IBinder b = ServiceManager.getService(getUceServiceName(mPhoneId));
    154 
    155         if (b != null) {
    156             try {
    157                 b.linkToDeath(mDeathReceipient, 0);
    158             } catch (RemoteException e) {
    159             }
    160         }
    161 
    162         this.mUceService = IUceService.Stub.asInterface(b);
    163     }
    164 
    165 
    166     /**
    167      * Death recipient class for monitoring IMS service.
    168      *
    169      * After receiving ACTION_UCE_SERVICE_DOWN event, the client
    170      * should wait to receive ACTION_UCE_SERVICE_UP and call
    171      * createUceService inorder to create mUceService instance.
    172      * @hide
    173      */
    174     private class UceServiceDeathRecipient implements IBinder.DeathRecipient {
    175         @Override
    176         public void binderDied() {
    177             //if (DBG) Log.d (LOG_TAG, "found IBinder/IUceService Service Died");
    178             mUceService = null;
    179 
    180             if (mContext != null) {
    181                 Intent intent = new Intent(ACTION_UCE_SERVICE_DOWN);
    182                 intent.putExtra(EXTRA_PHONE_ID, mPhoneId);
    183                 mContext.sendBroadcast(new Intent(intent));
    184             }
    185         }
    186     }
    187 }
    188