blob: 8ec76a2273fbaf807f8a66ef5330cc7034445e69 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2012 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 com.android.phone;
18
19import com.android.internal.telephony.DebugService;
Pavel Zhamaitsiaka94b97e2016-01-12 11:37:36 -080020import com.android.internal.telephony.ITelephonyDebug;
Pavel Zhamaitsiakdb5c84f2016-01-28 12:06:45 -080021import com.android.internal.telephony.ITelephonyDebugSubscriber;
22import com.android.internal.telephony.TelephonyEvent;
Pavel Zhamaitsiaka94b97e2016-01-12 11:37:36 -080023
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.app.Service;
25import android.content.Intent;
Pavel Zhamaitsiaka94b97e2016-01-12 11:37:36 -080026import android.os.Bundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070027import android.os.IBinder;
Pavel Zhamaitsiakdb5c84f2016-01-28 12:06:45 -080028import android.os.RemoteException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.util.Log;
30
31import java.io.FileDescriptor;
32import java.io.PrintWriter;
Pavel Zhamaitsiaka94b97e2016-01-12 11:37:36 -080033import java.util.ArrayList;
Pavel Zhamaitsiaka94b97e2016-01-12 11:37:36 -080034import java.util.List;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070035
36/**
37 * A debug service for telephony.
38 */
39public class TelephonyDebugService extends Service {
40 private static String TAG = "TelephonyDebugService";
Pavel Zhamaitsiaka94b97e2016-01-12 11:37:36 -080041 private static final boolean DBG = true;
42 private static final boolean VDBG = true;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043 private DebugService mDebugService = new DebugService();
44
45 /** Constructor */
46 public TelephonyDebugService() {
Pavel Zhamaitsiaka94b97e2016-01-12 11:37:36 -080047 if (DBG) Log.d(TAG, "TelephonyDebugService()");
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048 }
49
50 /**
51 * {@inheritDoc}
52 */
53 @Override
54 public IBinder onBind(Intent intent) {
Pavel Zhamaitsiaka94b97e2016-01-12 11:37:36 -080055 if (DBG) Log.d(TAG, "onBind()");
56 return mBinder;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057 }
58
59 @Override
60 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Pavel Zhamaitsiakdb5c84f2016-01-28 12:06:45 -080061 mDebugService.dump(fd, pw, args);
Santos Cordon7d4ddf62013-07-10 11:58:08 -070062 }
Pavel Zhamaitsiaka94b97e2016-01-12 11:37:36 -080063
Pavel Zhamaitsiakdb5c84f2016-01-28 12:06:45 -080064 private final int MAX_NUMBER_OF_EVENTS = 100;
65 private final int MIN_TIME_OFFSET = 900000; // 15 minutes
66 private final List<TelephonyEvent> mEvents = new ArrayList<TelephonyEvent>();
67 private long mLastSentEventTimeMillis = System.currentTimeMillis();
Pavel Zhamaitsiaka94b97e2016-01-12 11:37:36 -080068
69 /**
70 * Implementation of the ITelephonyDebug interface.
71 */
72 private final ITelephonyDebug.Stub mBinder = new ITelephonyDebug.Stub() {
Pavel Zhamaitsiakdb5c84f2016-01-28 12:06:45 -080073
74 private final List<ITelephonyDebugSubscriber> mSubscribers = new ArrayList<>();
75
Pavel Zhamaitsiaka94b97e2016-01-12 11:37:36 -080076 public void writeEvent(long timestamp, int phoneId, int tag,
77 int param1, int param2, Bundle data) {
Pavel Zhamaitsiakdb5c84f2016-01-28 12:06:45 -080078 final TelephonyEvent ev = new TelephonyEvent(timestamp, phoneId, tag,
79 param1, param2, data);
80 TelephonyEvent[] events = null;
81
Pavel Zhamaitsiaka94b97e2016-01-12 11:37:36 -080082 if (VDBG) {
Pavel Zhamaitsiakdb5c84f2016-01-28 12:06:45 -080083 Log.v(TAG, "writeEvent(" + ev.toString() + ")");
Pavel Zhamaitsiaka94b97e2016-01-12 11:37:36 -080084 }
Pavel Zhamaitsiakdb5c84f2016-01-28 12:06:45 -080085
Pavel Zhamaitsiaka94b97e2016-01-12 11:37:36 -080086 synchronized (mEvents) {
Pavel Zhamaitsiakdb5c84f2016-01-28 12:06:45 -080087 mEvents.add(ev);
88
89 final long currentTimeMillis = System.currentTimeMillis();
90 final long timeOffset = currentTimeMillis - mLastSentEventTimeMillis;
91 if (timeOffset > MIN_TIME_OFFSET
92 || timeOffset < 0 // system time has changed
93 || mEvents.size() >= MAX_NUMBER_OF_EVENTS) {
94 // batch events
95 mLastSentEventTimeMillis = currentTimeMillis;
96 events = new TelephonyEvent[mEvents.size()];
97 mEvents.toArray(events);
98 mEvents.clear();
99 }
100 }
101
102 if (events != null) {
103 synchronized (mSubscribers) {
104 for (ITelephonyDebugSubscriber s : mSubscribers) {
105 try {
106 s.onEvents(events);
107 } catch (RemoteException ex) {
108 Log.e(TAG, "RemoteException " + ex);
109 }
110 }
111 }
112 }
113 }
114
115 public void subscribe(ITelephonyDebugSubscriber subscriber) {
116 if (VDBG) Log.v(TAG, "subscribe");
117 synchronized (mSubscribers) {
118 mSubscribers.add(subscriber);
119 }
120
121 synchronized (mEvents) {
122 try {
123 // send cached events
124 TelephonyEvent[] events = new TelephonyEvent[mEvents.size()];
125 mEvents.toArray(events);
126 subscriber.onEvents(events);
127 } catch (RemoteException ex) {
128 Log.e(TAG, "RemoteException " + ex);
129 }
130 }
131 }
132
133 public void unsubscribe(ITelephonyDebugSubscriber subscriber) {
134 if (VDBG) Log.v(TAG, "unsubscribe");
135 synchronized (mSubscribers) {
136 mSubscribers.remove(subscriber);
Pavel Zhamaitsiaka94b97e2016-01-12 11:37:36 -0800137 }
138 }
139 };
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700140}
141