1 /* 2 * Copyright (C) 2024 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.app.viewcapture; 18 19 import android.tracing.perfetto.CreateIncrementalStateArgs; 20 import android.tracing.perfetto.DataSource; 21 import android.tracing.perfetto.DataSourceInstance; 22 import android.tracing.perfetto.FlushCallbackArguments; 23 import android.tracing.perfetto.StartCallbackArguments; 24 import android.tracing.perfetto.StopCallbackArguments; 25 import android.util.proto.ProtoInputStream; 26 27 import java.util.HashMap; 28 import java.util.Map; 29 30 class ViewCaptureDataSource 31 extends DataSource<DataSourceInstance, Void, ViewCaptureDataSource.IncrementalState> { 32 public static String DATA_SOURCE_NAME = "android.viewcapture"; 33 34 private final Runnable mOnStartStaticCallback; 35 private final Runnable mOnFlushStaticCallback; 36 private final Runnable mOnStopStaticCallback; 37 ViewCaptureDataSource(Runnable onStart, Runnable onFlush, Runnable onStop)38 ViewCaptureDataSource(Runnable onStart, Runnable onFlush, Runnable onStop) { 39 super(DATA_SOURCE_NAME); 40 this.mOnStartStaticCallback = onStart; 41 this.mOnFlushStaticCallback = onFlush; 42 this.mOnStopStaticCallback = onStop; 43 } 44 45 @Override createIncrementalState( CreateIncrementalStateArgs<DataSourceInstance> args)46 public IncrementalState createIncrementalState( 47 CreateIncrementalStateArgs<DataSourceInstance> args) { 48 return new IncrementalState(); 49 } 50 51 public static class IncrementalState { 52 public final Map<String, Integer> mInternMapPackageName = new HashMap<>(); 53 public final Map<String, Integer> mInternMapWindowName = new HashMap<>(); 54 public final Map<String, Integer> mInternMapViewId = new HashMap<>(); 55 public final Map<String, Integer> mInternMapClassName = new HashMap<>(); 56 public boolean mHasNotifiedClearedState = false; 57 } 58 59 @Override createInstance(ProtoInputStream configStream, int instanceIndex)60 public DataSourceInstance createInstance(ProtoInputStream configStream, int instanceIndex) { 61 return new DataSourceInstance(this, instanceIndex) { 62 @Override 63 protected void onStart(StartCallbackArguments args) { 64 mOnStartStaticCallback.run(); 65 } 66 67 @Override 68 protected void onFlush(FlushCallbackArguments args) { 69 mOnFlushStaticCallback.run(); 70 } 71 72 @Override 73 protected void onStop(StopCallbackArguments args) { 74 mOnStopStaticCallback.run(); 75 } 76 }; 77 } 78 } 79