summaryrefslogtreecommitdiff
path: root/rcs/rcsservice/src/com/android/service/ims/presence/PresenceCapabilityTask.java
blob: f93b317c9ba44d924eeef363c6c3fa8401d3f859 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * Copyright (c) 2015, Motorola Mobility LLC
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     - Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     - Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     - Neither the name of Motorola Mobility nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MOTOROLA MOBILITY LLC BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */

package com.android.service.ims.presence;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;

import com.android.ims.internal.Logger;
import com.android.service.ims.TaskManager;

/**
 * PresenceCapabilityTask
 */
public class PresenceCapabilityTask extends PresenceTask{
    /*
     * The logger
     */
    private Logger logger = Logger.getLogger(this.getClass().getName());

    /**
     * PendingIntent Action that will be scheduled via AlarmManager as a task timeout.
     */
    public static final String ACTION_TASK_TIMEOUT_ALARM =
            "com.android.service.ims.presence.task.timeout";

    private Context mContext = null;

    // The result code will be used for retry.
    public int mResultCode;

    // The alarm manager.
    static AlarmManager sAlarmManager = null;
    PendingIntent mAlarmIntent = null;
    boolean mTimerStarted = false;

    // it will be set to true after got sip response.
    public boolean mWaitingForNotify;

    // The time when created the task.
    private long mCreatedTimeStamp;

    private long mTimeout;

    public PresenceCapabilityTask(Context context, int taskId, int cmdId,
            ContactCapabilityResponse listener, String[] contacts,
            long timeout){
        super(taskId, cmdId, listener, contacts);
        mContext = context;
        mWaitingForNotify = false;

        mCreatedTimeStamp = System.currentTimeMillis();
        mTimeout = timeout;

        if(mTimeout <=0){
            // The terminal notification may be received shortly after the time limit of
            // the subscription due to network delays or retransmissions.
            // Device shall wait for 3sec after the end of the subscription period in order to
            // accept such notifications without returning spurious errors (e.g. SIP 481).
            mTimeout = 36000;
        }

        if(listener != null){
            startTimer();
        } //else it will be removed after got sip response.
    }

    public String toString(){
        return super.toString() +
                " mCreatedTimeStamp=" + mCreatedTimeStamp +
                " mTimeout=" + mTimeout;
    }

    private void startTimer(){
        if(mContext == null){
            logger.error("startTimer mContext is null");
            return;
        }

        Intent intent = new Intent(ACTION_TASK_TIMEOUT_ALARM);
        intent.setPackage(mContext.getPackageName());
        intent.putExtra("taskId", mTaskId);
        PendingIntent mAlarmIntent = PendingIntent.getBroadcast(mContext, 0, intent,
                PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_ONE_SHOT);

        if(sAlarmManager == null){
            sAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
        }

        long triggerAt = SystemClock.elapsedRealtime() + mTimeout;
        logger.debug("startTimer taskId=" + mTaskId + " mTimeout=" + mTimeout +
                " triggerAt=" + triggerAt + " mAlarmIntent=" + mAlarmIntent);
        sAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAt, mAlarmIntent);
        mTimerStarted = true;
    }

    public void cancelTimer(){
        if(mTimerStarted){
            logger.debug("cancelTimer, taskId=" + mTaskId);
            if(mAlarmIntent != null && sAlarmManager != null) {
                sAlarmManager.cancel(mAlarmIntent);
            }
            mTimerStarted = false;
        }
    }

    public void onTimeout(){
        logger.debug("onTimeout, taskId=" + mTaskId);
        if(mListener != null){
            mListener.onTimeout(mTaskId);
        }
        TaskManager.getDefault().removeTask(mTaskId);
    }

    public void setWaitingForNotify(boolean waitingForNotify){
        mWaitingForNotify = waitingForNotify;
    }

    public boolean isWaitingForNotify(){
        return mWaitingForNotify;
    }

    public void onTerminated(String reason){
        if(!mWaitingForNotify){
            logger.debug("onTerminated mWaitingForNotify is false. task=" + this);
            return;
        }

        cancelTimer();
        if(mListener != null){
            mListener.onFinish(mTaskId);
        }

        TaskManager.getDefault().removeTask(mTaskId);
    }
}