summaryrefslogtreecommitdiff
path: root/src/com/android/incallui/CallRecorder.java
blob: cf04d9506e9cd37d97d64a2ad90153aba40599f5 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 * Copyright (C) 2014 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.incallui;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.RemoteException;
import android.os.SystemProperties;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.android.services.callrecorder.CallRecorderService;
import com.android.services.callrecorder.CallRecordingDataStore;
import com.android.services.callrecorder.common.CallRecording;
import com.android.services.callrecorder.common.ICallRecorderService;

import java.util.Date;
import java.util.HashSet;

/**
 * InCall UI's interface to the call recorder
 *
 * Manages the call recorder service lifecycle.  We bind to the service whenever an active call
 * is established, and unbind when all calls have been disconnected.
 */
public class CallRecorder implements CallList.Listener {
    public static final String TAG = "CallRecorder";

    public static final String[] REQUIRED_PERMISSIONS = new String[] {
        android.Manifest.permission.RECORD_AUDIO,
        android.Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    private static CallRecorder sInstance = null;

    private Context mContext;
    private boolean mInitialized = false;
    private ICallRecorderService mService = null;

    private HashSet<RecordingProgressListener> mProgressListeners =
            new HashSet<RecordingProgressListener>();
    private Handler mHandler = new Handler();

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = ICallRecorderService.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
        }
    };

    public static CallRecorder getInstance() {
        if (sInstance == null) {
            sInstance = new CallRecorder();
        }
        return sInstance;
    }

    public boolean isEnabled() {
        return CallRecorderService.isEnabled(mContext);
    }

    private CallRecorder() {
        CallList.getInstance().addListener(this);
    }

    public void setUp(Context context) {
        mContext = context.getApplicationContext();
    }

    private void initialize() {
        if (isEnabled() && !mInitialized) {
            Intent serviceIntent = new Intent(mContext, CallRecorderService.class);
            mContext.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE);
            mInitialized = true;
        }
    }

    private void uninitialize() {
        if (mInitialized) {
            mContext.unbindService(mConnection);
            mInitialized = false;
        }
    }

    public boolean startRecording(final String phoneNumber, final long creationTime) {
        if (mService == null) {
            return false;
        }

        try {
            if (mService.startRecording(phoneNumber, creationTime)) {
                for (RecordingProgressListener l : mProgressListeners) {
                    l.onStartRecording();
                }
                mUpdateRecordingProgressTask.run();
                return true;
            } else {
                Toast.makeText(mContext, R.string.call_recording_failed_message,
                        Toast.LENGTH_SHORT).show();
            }
        } catch (RemoteException e) {
            Log.w(TAG, "Failed to start recording " + phoneNumber + ", " +
                    new Date(creationTime), e);
        }

        return false;
    }

    public boolean isRecording() {
        if (mService == null) {
            return false;
        }

        try {
            return mService.isRecording();
        } catch (RemoteException e) {
            Log.w(TAG, "Exception checking recording status", e);
        }
        return false;
    }

    public CallRecording getActiveRecording() {
        if (mService == null) {
            return null;
        }

        try {
            return mService.getActiveRecording();
        } catch (RemoteException e) {
            Log.w("Exception getting active recording", e);
        }
        return null;
    }

    public void finishRecording() {
        if (mService != null) {
            try {
                final CallRecording recording = mService.stopRecording();
                if (recording != null) {
                    if (!TextUtils.isEmpty(recording.phoneNumber)) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                CallRecordingDataStore dataStore = new CallRecordingDataStore();
                                dataStore.open(mContext);
                                dataStore.putRecording(recording);
                                dataStore.close();
                            }
                        }).start();
                    } else {
                        // Data store is an index by number so that we can link recordings in the
                        // call detail page.  If phone number is not available (conference call or
                        // unknown number) then just display a toast.
                        String msg = mContext.getResources().getString(
                                R.string.call_recording_file_location, recording.fileName);
                        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
                    }
                }
            } catch (RemoteException e) {
                Log.w(TAG, "Failed to stop recording", e);
            }
        }

        for (RecordingProgressListener l : mProgressListeners) {
            l.onStopRecording();
        }
        mHandler.removeCallbacks(mUpdateRecordingProgressTask);
    }

    //
    // Call list listener methods.
    //
    @Override
    public void onIncomingCall(Call call) {
        // do nothing
    }

    @Override
    public void onCallListChange(final CallList callList) {
        if (!mInitialized && callList.getActiveCall() != null) {
            // we'll come here if this is the first active call
            initialize();
        } else {
            // we can come down this branch to resume a call that was on hold
            CallRecording active = getActiveRecording();
            if (active != null) {
                Call call = callList.getCallWithStateAndNumber(Call.State.ONHOLD,
                        active.phoneNumber);
                if (call != null) {
                    // The call associated with the active recording has been placed
                    // on hold, so stop the recording.
                    finishRecording();
                }
            }
        }
    }

    @Override
    public void onDisconnect(final Call call) {
        CallRecording active = getActiveRecording();
        if (active != null && TextUtils.equals(call.getNumber(), active.phoneNumber)) {
            // finish the current recording if the call gets disconnected
            finishRecording();
        }

        // tear down the service if there are no more active calls
        if (CallList.getInstance().getActiveCall() == null) {
            uninitialize();
        }
    }

    @Override
    public void onUpgradeToVideo(Call call) {}

    // allow clients to listen for recording progress updates
    public interface RecordingProgressListener {
        public void onStartRecording();
        public void onStopRecording();
        public void onRecordingTimeProgress(long elapsedTimeMs);
    }

    public void addRecordingProgressListener(RecordingProgressListener listener) {
        mProgressListeners.add(listener);
    }

    public void removeRecordingProgressListener(RecordingProgressListener listener) {
        mProgressListeners.remove(listener);
    }

    private static final int UPDATE_INTERVAL = 500;

    private Runnable mUpdateRecordingProgressTask = new Runnable() {
        @Override
        public void run() {
            CallRecording active = getActiveRecording();
            if (active != null) {
                long elapsed = System.currentTimeMillis() - active.startRecordingTime;
                for (RecordingProgressListener l : mProgressListeners) {
                    l.onRecordingTimeProgress(elapsed);
                }
            }
            mHandler.postDelayed(mUpdateRecordingProgressTask, UPDATE_INTERVAL);
        }
    };
}