Home | History | Annotate | Download | only in sample
      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.car.cluster.sample;
     18 
     19 import static com.android.car.cluster.sample.DebugUtil.DEBUG;
     20 
     21 import android.content.Intent;
     22 import android.os.Binder;
     23 import android.os.IBinder;
     24 import android.telecom.Call;
     25 import android.telecom.Call.Callback;
     26 import android.telecom.InCallService;
     27 import android.util.Log;
     28 
     29 import java.lang.ref.WeakReference;
     30 
     31 /**
     32  * Monitors call state and reports it to the listeners that was registered using
     33  * {@link #registerListener} method.
     34  */
     35 public class ClusterInCallService extends InCallService {
     36 
     37     private static final String TAG = DebugUtil.getTag(ClusterInCallService.class);
     38 
     39     static final String ACTION_LOCAL_BINDING = "local_binding";
     40 
     41     private final PhoneCallback mPhoneCallback = new PhoneCallback(this);
     42     private volatile Callback mListener;
     43 
     44     @Override
     45     public void onCallAdded(Call call) {
     46         if (DEBUG) {
     47             Log.d(TAG, "onCallAdded");
     48         }
     49         call.registerCallback(mPhoneCallback);
     50         mPhoneCallback.onStateChanged(call, call.getState());
     51     }
     52 
     53     @Override
     54     public void onCallRemoved(Call call) {
     55         if (DEBUG) {
     56             Log.d(TAG, "onCallRemoved");
     57         }
     58         call.unregisterCallback(mPhoneCallback);
     59     }
     60 
     61     private void doStateChanged(Call call, int state) {
     62         if (DEBUG) {
     63             Log.d(TAG, "doStateChanged, call: " + call + ", state: " + state);
     64         }
     65         if (mListener != null) {
     66             mListener.onStateChanged(call, state);
     67         }
     68     }
     69 
     70     @Override
     71     public IBinder onBind(Intent intent) {
     72         Log.d(TAG, "onBind, intent:" + intent);
     73         return ACTION_LOCAL_BINDING.equals(intent.getAction())
     74             ? new LocalBinder() : super.onBind(intent);
     75     }
     76 
     77     public class LocalBinder extends Binder {
     78         ClusterInCallService getService() {
     79             return ClusterInCallService.this;
     80         }
     81     }
     82 
     83     public void registerListener(Callback listener) {
     84         Log.d(TAG, "registerListener, listener: " + listener);
     85         mListener = listener;
     86     }
     87 
     88     private static class PhoneCallback extends Callback {
     89         private final WeakReference<ClusterInCallService> mServiceRef;
     90 
     91         private PhoneCallback(ClusterInCallService service) {
     92             mServiceRef = new WeakReference<>(service);
     93         }
     94 
     95         @Override
     96         public void onStateChanged(Call call, int state) {
     97             Log.d(TAG, "PhoneCallback#onStateChanged, call: " + call + ", state: " + state);
     98             ClusterInCallService service = mServiceRef.get();
     99             if (service != null) {
    100                 service.doStateChanged(call, state);
    101             }
    102         }
    103     }
    104 }
    105