ImsFeature.java revision 15708a316bcfafc93aa1795fae65e83035270d88
1/*
2 * Copyright (C) 2017 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
17package android.telephony.ims.feature;
18
19import android.annotation.IntDef;
20import android.os.RemoteException;
21import android.util.Log;
22
23import com.android.ims.internal.IImsFeatureStatusCallback;
24
25import java.lang.annotation.Retention;
26import java.lang.annotation.RetentionPolicy;
27import java.util.ArrayList;
28import java.util.List;
29
30/**
31 * Base class for all IMS features that are supported by the framework.
32 * @hide
33 */
34public abstract class ImsFeature {
35
36    private static final String LOG_TAG = "ImsFeature";
37
38    // Invalid feature value
39    public static final int INVALID = -1;
40    // ImsFeatures that are defined in the Manifests. Ensure that these values match the previously
41    // defined values in ImsServiceClass for compatibility purposes.
42    public static final int EMERGENCY_MMTEL = 0;
43    public static final int MMTEL = 1;
44    public static final int RCS = 2;
45    // Total number of features defined
46    public static final int MAX = 3;
47
48    // Integer values defining the state of the ImsFeature at any time.
49    @IntDef(flag = true,
50            value = {
51                    STATE_NOT_AVAILABLE,
52                    STATE_INITIALIZING,
53                    STATE_READY,
54            })
55    @Retention(RetentionPolicy.SOURCE)
56    public @interface ImsState {}
57    public static final int STATE_NOT_AVAILABLE = 0;
58    public static final int STATE_INITIALIZING = 1;
59    public static final int STATE_READY = 2;
60
61    private List<INotifyFeatureRemoved> mRemovedListeners = new ArrayList<>();
62    private IImsFeatureStatusCallback mStatusCallback;
63    private @ImsState int mState = STATE_NOT_AVAILABLE;
64
65    public interface INotifyFeatureRemoved {
66        void onFeatureRemoved(int slotId);
67    }
68
69    public void addFeatureRemovedListener(INotifyFeatureRemoved listener) {
70        synchronized (mRemovedListeners) {
71            mRemovedListeners.add(listener);
72        }
73    }
74
75    public void removeFeatureRemovedListener(INotifyFeatureRemoved listener) {
76        synchronized (mRemovedListeners) {
77            mRemovedListeners.remove(listener);
78        }
79    }
80
81    // Not final for testing.
82    public void notifyFeatureRemoved(int slotId) {
83        synchronized (mRemovedListeners) {
84            mRemovedListeners.forEach(l -> l.onFeatureRemoved(slotId));
85            onFeatureRemoved();
86        }
87    }
88
89    public int getFeatureState() {
90        return mState;
91    }
92
93    protected final void setFeatureState(@ImsState int state) {
94        if (mState != state) {
95            mState = state;
96            notifyFeatureState(state);
97        }
98    }
99
100    // Not final for testing.
101    public void setImsFeatureStatusCallback(IImsFeatureStatusCallback c) {
102        mStatusCallback = c;
103        // If we have just connected, send queued status.
104        notifyFeatureState(mState);
105    }
106
107    /**
108     * Internal method called by ImsFeature when setFeatureState has changed.
109     * @param state
110     */
111    private void notifyFeatureState(@ImsState int state) {
112        if (mStatusCallback != null) {
113            try {
114                Log.i(LOG_TAG, "notifying ImsFeatureState");
115                mStatusCallback.notifyImsFeatureStatus(state);
116            } catch (RemoteException e) {
117                mStatusCallback = null;
118                Log.w(LOG_TAG, "Couldn't notify feature state: " + e.getMessage());
119            }
120        }
121    }
122
123    /**
124     * Called when the feature is being removed and must be cleaned up.
125     */
126    public abstract void onFeatureRemoved();
127}
128