Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.phone; |
| 18 | |
| 19 | import com.android.internal.telephony.DebugService; |
Pavel Zhamaitsiak | a94b97e | 2016-01-12 11:37:36 -0800 | [diff] [blame] | 20 | import com.android.internal.telephony.ITelephonyDebug; |
Pavel Zhamaitsiak | db5c84f | 2016-01-28 12:06:45 -0800 | [diff] [blame^] | 21 | import com.android.internal.telephony.ITelephonyDebugSubscriber; |
| 22 | import com.android.internal.telephony.TelephonyEvent; |
Pavel Zhamaitsiak | a94b97e | 2016-01-12 11:37:36 -0800 | [diff] [blame] | 23 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 24 | import android.app.Service; |
| 25 | import android.content.Intent; |
Pavel Zhamaitsiak | a94b97e | 2016-01-12 11:37:36 -0800 | [diff] [blame] | 26 | import android.os.Bundle; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 27 | import android.os.IBinder; |
Pavel Zhamaitsiak | db5c84f | 2016-01-28 12:06:45 -0800 | [diff] [blame^] | 28 | import android.os.RemoteException; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 29 | import android.util.Log; |
| 30 | |
| 31 | import java.io.FileDescriptor; |
| 32 | import java.io.PrintWriter; |
Pavel Zhamaitsiak | a94b97e | 2016-01-12 11:37:36 -0800 | [diff] [blame] | 33 | import java.util.ArrayList; |
Pavel Zhamaitsiak | a94b97e | 2016-01-12 11:37:36 -0800 | [diff] [blame] | 34 | import java.util.List; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * A debug service for telephony. |
| 38 | */ |
| 39 | public class TelephonyDebugService extends Service { |
| 40 | private static String TAG = "TelephonyDebugService"; |
Pavel Zhamaitsiak | a94b97e | 2016-01-12 11:37:36 -0800 | [diff] [blame] | 41 | private static final boolean DBG = true; |
| 42 | private static final boolean VDBG = true; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 43 | private DebugService mDebugService = new DebugService(); |
| 44 | |
| 45 | /** Constructor */ |
| 46 | public TelephonyDebugService() { |
Pavel Zhamaitsiak | a94b97e | 2016-01-12 11:37:36 -0800 | [diff] [blame] | 47 | if (DBG) Log.d(TAG, "TelephonyDebugService()"); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /** |
| 51 | * {@inheritDoc} |
| 52 | */ |
| 53 | @Override |
| 54 | public IBinder onBind(Intent intent) { |
Pavel Zhamaitsiak | a94b97e | 2016-01-12 11:37:36 -0800 | [diff] [blame] | 55 | if (DBG) Log.d(TAG, "onBind()"); |
| 56 | return mBinder; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Override |
| 60 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Pavel Zhamaitsiak | db5c84f | 2016-01-28 12:06:45 -0800 | [diff] [blame^] | 61 | mDebugService.dump(fd, pw, args); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 62 | } |
Pavel Zhamaitsiak | a94b97e | 2016-01-12 11:37:36 -0800 | [diff] [blame] | 63 | |
Pavel Zhamaitsiak | db5c84f | 2016-01-28 12:06:45 -0800 | [diff] [blame^] | 64 | 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 Zhamaitsiak | a94b97e | 2016-01-12 11:37:36 -0800 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Implementation of the ITelephonyDebug interface. |
| 71 | */ |
| 72 | private final ITelephonyDebug.Stub mBinder = new ITelephonyDebug.Stub() { |
Pavel Zhamaitsiak | db5c84f | 2016-01-28 12:06:45 -0800 | [diff] [blame^] | 73 | |
| 74 | private final List<ITelephonyDebugSubscriber> mSubscribers = new ArrayList<>(); |
| 75 | |
Pavel Zhamaitsiak | a94b97e | 2016-01-12 11:37:36 -0800 | [diff] [blame] | 76 | public void writeEvent(long timestamp, int phoneId, int tag, |
| 77 | int param1, int param2, Bundle data) { |
Pavel Zhamaitsiak | db5c84f | 2016-01-28 12:06:45 -0800 | [diff] [blame^] | 78 | final TelephonyEvent ev = new TelephonyEvent(timestamp, phoneId, tag, |
| 79 | param1, param2, data); |
| 80 | TelephonyEvent[] events = null; |
| 81 | |
Pavel Zhamaitsiak | a94b97e | 2016-01-12 11:37:36 -0800 | [diff] [blame] | 82 | if (VDBG) { |
Pavel Zhamaitsiak | db5c84f | 2016-01-28 12:06:45 -0800 | [diff] [blame^] | 83 | Log.v(TAG, "writeEvent(" + ev.toString() + ")"); |
Pavel Zhamaitsiak | a94b97e | 2016-01-12 11:37:36 -0800 | [diff] [blame] | 84 | } |
Pavel Zhamaitsiak | db5c84f | 2016-01-28 12:06:45 -0800 | [diff] [blame^] | 85 | |
Pavel Zhamaitsiak | a94b97e | 2016-01-12 11:37:36 -0800 | [diff] [blame] | 86 | synchronized (mEvents) { |
Pavel Zhamaitsiak | db5c84f | 2016-01-28 12:06:45 -0800 | [diff] [blame^] | 87 | 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 Zhamaitsiak | a94b97e | 2016-01-12 11:37:36 -0800 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | }; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 140 | } |
| 141 | |