diff options
author | Siva Velusamy <vsiva@google.com> | 2011-08-25 14:21:17 -0700 |
---|---|---|
committer | Siva Velusamy <vsiva@google.com> | 2011-09-01 13:18:06 -0700 |
commit | 4f830e0754e925eb33bae188ade858d10dd9f2d3 (patch) | |
tree | b97b49df9bb3923e709dd58f61268f7d1a8f6ebd /eclipse/plugins/com.android.ide.eclipse.ddms | |
parent | bfde12592b821ef00973e0eedf663752ca789087 (diff) | |
download | sdk-4f830e0754e925eb33bae188ade858d10dd9f2d3.zip sdk-4f830e0754e925eb33bae188ade858d10dd9f2d3.tar.gz sdk-4f830e0754e925eb33bae188ade858d10dd9f2d3.tar.bz2 |
Activate logcat view at appropriate times.
- When applications are launched (run/debug) on a device, start
monitoring the logcat output on that device.
- If some important message comes across, and the logcat view is not
currently displayed, then activate it.
Change-Id: Ib88973badde4cb2860379129dfe0eb03a3f955dc
Diffstat (limited to 'eclipse/plugins/com.android.ide.eclipse.ddms')
3 files changed, 171 insertions, 0 deletions
diff --git a/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/DdmsPlugin.java b/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/DdmsPlugin.java index 510e8ab..8900366 100644 --- a/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/DdmsPlugin.java +++ b/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/DdmsPlugin.java @@ -27,6 +27,10 @@ import com.android.ddmlib.Log.LogLevel; import com.android.ddmuilib.DdmUiPreferences; import com.android.ddmuilib.StackTracePanel; import com.android.ddmuilib.DevicePanel.IUiSelectionListener; +import com.android.ddmuilib.logcat.ILogCatMessageEventListener; +import com.android.ddmuilib.logcat.LogCatMessage; +import com.android.ddmuilib.logcat.LogCatReceiver; +import com.android.ddmuilib.logcat.LogCatReceiverFactory; import com.android.ide.eclipse.ddms.i18n.Messages; import com.android.ide.eclipse.ddms.preferences.PreferenceInitializer; @@ -58,6 +62,7 @@ import org.osgi.framework.BundleContext; import java.io.File; import java.util.ArrayList; import java.util.Calendar; +import java.util.List; /** * The activator class controls the plug-in life cycle @@ -779,4 +784,13 @@ public final class DdmsPlugin extends AbstractUIPlugin implements IDeviceChangeL return false; } + + private LogCatMonitor mLogCatMonitor; + public void startLogCatMonitor(IDevice device) { + if (mLogCatMonitor == null) { + mLogCatMonitor = new LogCatMonitor(getDebuggerConnectors(), getPreferenceStore()); + } + + mLogCatMonitor.monitorDevice(device); + } } diff --git a/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/IDebuggerConnector.java b/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/IDebuggerConnector.java index b271dd8..c22dfd2 100644 --- a/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/IDebuggerConnector.java +++ b/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/IDebuggerConnector.java @@ -21,6 +21,13 @@ package com.android.ide.eclipse.ddms; * on a connected device. */ public interface IDebuggerConnector { + /** + * Is this application from a project present in the workspace? + * @param appName name of the application. This is typically the application's package, but + * can be different if the component was setup to run in its own process. + * @return true if there is a project in the workspace containing the given app. + */ + boolean isWorkspaceApp(String appName); /** * Connects a debugger to a VM identified by its appName. diff --git a/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/LogCatMonitor.java b/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/LogCatMonitor.java new file mode 100644 index 0000000..7ce6503 --- /dev/null +++ b/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/LogCatMonitor.java @@ -0,0 +1,150 @@ +/* + * Copyright (C) 2011 The Android Open Source 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.ide.eclipse.ddms; + +import com.android.ddmlib.AndroidDebugBridge; +import com.android.ddmlib.AndroidDebugBridge.IDeviceChangeListener; +import com.android.ddmlib.IDevice; +import com.android.ddmlib.Log.LogLevel; +import com.android.ddmuilib.logcat.ILogCatMessageEventListener; +import com.android.ddmuilib.logcat.LogCatMessage; +import com.android.ddmuilib.logcat.LogCatReceiver; +import com.android.ddmuilib.logcat.LogCatReceiverFactory; +import com.android.ide.eclipse.ddms.views.LogCatView; + +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.swt.widgets.Display; +import org.eclipse.ui.IViewPart; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.PartInitException; +import org.eclipse.ui.PlatformUI; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * LogCatMonitor helps in monitoring the logcat output from a set of devices. + * It scans through the received logcat messages, and activates the logcat view + * if any message is deemed important. + */ +public class LogCatMonitor { + private IPreferenceStore mPrefStore; + private Set<String> mMonitoredDevices; + private IDebuggerConnector[] mConnectors; + + public LogCatMonitor(IDebuggerConnector[] debuggerConnectors, IPreferenceStore prefStore) { + mConnectors = debuggerConnectors; + mPrefStore = prefStore; + + mMonitoredDevices = new HashSet<String>(); + + AndroidDebugBridge.addDeviceChangeListener(new IDeviceChangeListener() { + public void deviceDisconnected(IDevice device) { + unmonitorDevice(device); + } + + public void deviceConnected(IDevice device) { + } + + public void deviceChanged(IDevice device, int changeMask) { + } + }); + } + + private synchronized void unmonitorDevice(IDevice device) { + mMonitoredDevices.remove(device.getSerialNumber()); + } + + public void monitorDevice(final IDevice device) { + if (mMonitoredDevices.contains(device.getSerialNumber())) { + // the device is already monitored + return; + } + + LogCatReceiver r = LogCatReceiverFactory.INSTANCE.newReceiver(device, mPrefStore); + r.addMessageReceivedEventListener(new ILogCatMessageEventListener() { + public void messageReceived(List<LogCatMessage> receivedMessages) { + checkMessages(receivedMessages, device); + } + }); + + mMonitoredDevices.add(device.getSerialNumber()); + } + + private void checkMessages(List<LogCatMessage> receivedMessages, IDevice device) { + // check the received list of messages to see if any of them are + // significant enough to be seen by the user. If so, activate the logcat view + // to display those messages + for (LogCatMessage m : receivedMessages) { + if (isImportantMessage(m)) { + focusLogCatView(device, m.getAppName()); + break; + } + } + } + + /** + * Check whether a message is "important". Currently, we assume that a message is important if + * it is of severity level error or higher, and it belongs to an app currently in the workspace. + */ + private boolean isImportantMessage(LogCatMessage m) { + if (m.getLogLevel().getPriority() < LogLevel.ERROR.getPriority()) { + return false; + } + + String app = m.getAppName(); + for (IDebuggerConnector c : mConnectors) { + if (c.isWorkspaceApp(app)) { + return true; + } + } + + return false; + } + + private void focusLogCatView(final IDevice device, final String appName) { + Display.getDefault().asyncExec(new Runnable() { + public void run() { + IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); + if (window == null) { + return; + } + + IWorkbenchPage page = window.getActivePage(); + if (page == null) { + return; + } + + // if the view is already in the page, just bring it to the front + // without giving it focus. + IViewPart logCatView = page.findView(LogCatView.ID); + if (logCatView != null) { + page.bringToTop(logCatView); + return; + } + + // if the view is not in the page, then create and show it. + try { + page.showView(LogCatView.ID); + } catch (PartInitException e) { + return; + } + } + }); + } +} |