Skip to content

Commit

Permalink
Add menu to copy metadata of the opened file/device to the clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
mlopatkin committed Jun 27, 2021
1 parent fe0427a commit 5e5a8ca
Show file tree
Hide file tree
Showing 20 changed files with 450 additions and 16 deletions.
6 changes: 5 additions & 1 deletion src/name/mlopatkin/andlogview/AppGlobals.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import name.mlopatkin.andlogview.config.ConfigModule;
import name.mlopatkin.andlogview.config.ConfigStorage;
import name.mlopatkin.andlogview.ui.GlobalClipboard;
import name.mlopatkin.andlogview.ui.SwingUiModule;
import name.mlopatkin.andlogview.utils.UiThreadScheduler;

import dagger.BindsInstance;
Expand All @@ -35,7 +37,7 @@
* Global application services available for all.
*/
@Singleton
@Component(modules = {GlobalsModule.class, ConfigModule.class, AppExecutors.ExecutorsModule.class})
@Component(modules = {GlobalsModule.class, ConfigModule.class, AppExecutors.ExecutorsModule.class, SwingUiModule.class})
public interface AppGlobals {
Main getMain();

Expand All @@ -49,6 +51,8 @@ public interface AppGlobals {

UiThreadScheduler getUiTimer();

GlobalClipboard getClipboard();

@Component.Factory
interface Factory {
AppGlobals create(GlobalsModule module, @BindsInstance CommandLine cmdline);
Expand Down
2 changes: 2 additions & 0 deletions src/name/mlopatkin/andlogview/liblogcat/DataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,6 @@ public interface DataSource {
* bookmarks become invalid)
*/
boolean reset();

SourceMetadata getMetadata();
}
25 changes: 25 additions & 0 deletions src/name/mlopatkin/andlogview/liblogcat/SourceMetadata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2021 Mikhail Lopatkin
*
* 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 name.mlopatkin.andlogview.liblogcat;

import java.util.Collection;

/** A collection of metadata that describes the data source. */
public interface SourceMetadata {
/** @return the collection of metadata items available for the data source */
Collection<SourceMetadataItem> getMetadataItems();
}
35 changes: 35 additions & 0 deletions src/name/mlopatkin/andlogview/liblogcat/SourceMetadataItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2021 Mikhail Lopatkin
*
* 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 name.mlopatkin.andlogview.liblogcat;

public class SourceMetadataItem {
private final String displayName;
private final String value;

public SourceMetadataItem(String displayName, String value) {
this.displayName = displayName;
this.value = value;
}

public String getDisplayName() {
return displayName;
}

public String getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import name.mlopatkin.andlogview.liblogcat.LogRecord;
import name.mlopatkin.andlogview.liblogcat.LogRecord.Buffer;
import name.mlopatkin.andlogview.liblogcat.RecordListener;
import name.mlopatkin.andlogview.liblogcat.SourceMetadata;
import name.mlopatkin.andlogview.liblogcat.ddmlib.AdbBuffer.BufferReceiver;

import com.android.ddmlib.AndroidDebugBridge.IDeviceChangeListener;
Expand All @@ -42,8 +43,9 @@ public final class AdbDataSource implements DataSource, BufferReceiver {
private final IDevice device;
private final AdbPidToProcessConverter converter;
private final EnumSet<Buffer> availableBuffers = EnumSet.noneOf(Buffer.class);
private final SourceMetadata sourceMetadata;

public AdbDataSource(final IDevice device) {
public AdbDataSource(IDevice device) {
assert device != null;
assert device.isOnline();
this.device = device;
Expand All @@ -52,6 +54,7 @@ public AdbDataSource(final IDevice device) {
setUpStream(buffer);
}
AdbDeviceManager.addDeviceChangeListener(deviceListener);
sourceMetadata = new AdbSourceMetadata(device);
}

private boolean closed = false;
Expand Down Expand Up @@ -148,6 +151,11 @@ public String toString() {
}
}

@Override
public SourceMetadata getMetadata() {
return sourceMetadata;
}

private IDeviceChangeListener deviceListener = new AdbDeviceManager.AbstractDeviceListener() {
@Override
public void deviceDisconnected(IDevice device) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2021 Mikhail Lopatkin
*
* 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 name.mlopatkin.andlogview.liblogcat.ddmlib;

import name.mlopatkin.andlogview.liblogcat.SourceMetadata;
import name.mlopatkin.andlogview.liblogcat.SourceMetadataItem;

import com.android.ddmlib.IDevice;

import java.util.Collection;
import java.util.Collections;

class AdbSourceMetadata implements SourceMetadata {
private final IDevice device;

public AdbSourceMetadata(IDevice device) {
this.device = device;
}

@Override
public Collection<SourceMetadataItem> getMetadataItems() {
return Collections.singleton(new SourceMetadataItem("serial", device.getSerialNumber()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import name.mlopatkin.andlogview.liblogcat.LogRecordParser;
import name.mlopatkin.andlogview.liblogcat.ProcessListParser;
import name.mlopatkin.andlogview.liblogcat.RecordListener;
import name.mlopatkin.andlogview.liblogcat.SourceMetadata;
import name.mlopatkin.andlogview.liblogcat.file.ParsingStrategies.Strategy;

import com.google.common.base.CharMatcher;
Expand All @@ -31,6 +32,7 @@
import org.checkerframework.checker.nullness.qual.Nullable;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
Expand All @@ -52,10 +54,12 @@ public final class DumpstateFileDataSource implements DataSource {
private EnumSet<Buffer> buffers = EnumSet.noneOf(Buffer.class);
private @Nullable RecordListener<LogRecord> logcatListener;

private String fileName;
private final String fileName;
private final SourceMetadata sourceMetadata;

public DumpstateFileDataSource(String fileName, BufferedReader in) throws IOException, ParseException {
this.fileName = fileName;
sourceMetadata = new FileSourceMetadata(new File(fileName));
initSectionHandlers();
parseFile(in);
}
Expand Down Expand Up @@ -311,4 +315,9 @@ private boolean isEnd(String line) {
public String toString() {
return fileName;
}

@Override
public SourceMetadata getMetadata() {
return sourceMetadata;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2021 Mikhail Lopatkin
*
* 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 name.mlopatkin.andlogview.liblogcat.file;

import name.mlopatkin.andlogview.liblogcat.SourceMetadata;
import name.mlopatkin.andlogview.liblogcat.SourceMetadataItem;

import com.google.common.collect.ImmutableList;

import java.io.File;
import java.util.Collection;

class FileSourceMetadata implements SourceMetadata {
private final ImmutableList<SourceMetadataItem> metadataItems;

public FileSourceMetadata(File path) {
metadataItems = ImmutableList.of(
new SourceMetadataItem("name", path.getName()),
new SourceMetadataItem("path", path.getAbsolutePath()));
}

@Override
public Collection<SourceMetadataItem> getMetadataItems() {
return metadataItems;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
import name.mlopatkin.andlogview.liblogcat.LogRecord.Buffer;
import name.mlopatkin.andlogview.liblogcat.LogRecordParser;
import name.mlopatkin.andlogview.liblogcat.RecordListener;
import name.mlopatkin.andlogview.liblogcat.SourceMetadata;

import com.google.common.base.CharMatcher;

import org.apache.log4j.Logger;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -44,13 +46,16 @@ public class LogfileDataSource implements DataSource {
private static final Logger logger = Logger.getLogger(LogfileDataSource.class);

private @Nullable RecordListener<LogRecord> listener;
private ParsingStrategies.Strategy strategy;
private List<LogRecord> records = new ArrayList<>();
private String fileName;
private final ParsingStrategies.Strategy strategy;
private final List<LogRecord> records = new ArrayList<>();
private final String fileName;
private final SourceMetadata sourceMetadata;


private LogfileDataSource(String fileName, ParsingStrategies.Strategy strategy) {
this.strategy = strategy;
this.fileName = fileName;
this.sourceMetadata = new FileSourceMetadata(new File(fileName));
logger.debug("Strategy implemented: " + strategy);
}

Expand Down Expand Up @@ -122,4 +127,9 @@ public boolean reset() {
public String toString() {
return fileName;
}

@Override
public SourceMetadata getMetadata() {
return sourceMetadata;
}
}
37 changes: 37 additions & 0 deletions src/name/mlopatkin/andlogview/ui/AwtClipboard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2021 Mikhail Lopatkin
*
* 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 name.mlopatkin.andlogview.ui;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;

import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
class AwtClipboard implements GlobalClipboard {
@Inject
AwtClipboard() {
}

@Override
public void setText(String text) {
Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
systemClipboard.setContents(new StringSelection(text), null);
}
}
21 changes: 21 additions & 0 deletions src/name/mlopatkin/andlogview/ui/GlobalClipboard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2021 Mikhail Lopatkin
*
* 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 name.mlopatkin.andlogview.ui;

public interface GlobalClipboard {
void setText(String text);
}
26 changes: 26 additions & 0 deletions src/name/mlopatkin/andlogview/ui/SwingUiModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2021 Mikhail Lopatkin
*
* 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 name.mlopatkin.andlogview.ui;

import dagger.Binds;
import dagger.Module;

@Module
public abstract class SwingUiModule {
@Binds
abstract GlobalClipboard getGlobalClipboard(AwtClipboard awtClipboard);
}
Loading

0 comments on commit 5e5a8ca

Please sign in to comment.