Hector Dearman | 4544f37 | 2018-09-19 12:32:36 +0100 | [diff] [blame] | 1 | // Copyright (C) 2018 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | import * as m from 'mithril'; |
| 16 | |
| 17 | import {createPage} from './pages'; |
| 18 | |
| 19 | const RECORD_COMMAND_LINE = |
| 20 | 'echo CgYIgKAGIAESIwohCgxsaW51eC5mdHJhY2UQAKIGDhIFc2NoZWQSBWlucHV0GJBOMh0KFnBlcmZldHRvLnRyYWNlZF9wcm9iZXMQgCAYBEAASAA= | base64 --decode | adb shell "perfetto -c - -o /data/misc/perfetto-traces/trace" && adb pull /data/misc/perfetto-traces/trace /tmp/trace'; |
| 21 | |
| 22 | async function copyToClipboard(text: string): Promise<void> { |
| 23 | try { |
| 24 | // TODO(hjd): Fix typescript type for navigator. |
| 25 | // tslint:disable-next-line no-any |
| 26 | await(navigator as any).clipboard.writeText(text); |
| 27 | } catch (err) { |
| 28 | console.error(`Failed to copy "${text}" to clipboard: ${err}`); |
| 29 | } |
| 30 | } |
| 31 | |
Deepanjan Roy | 97f6324 | 2018-09-20 15:32:01 -0400 | [diff] [blame] | 32 | interface CodeSampleAttrs { |
| 33 | text: string; |
| 34 | } |
| 35 | |
| 36 | class CodeSample implements m.ClassComponent<CodeSampleAttrs> { |
| 37 | view({attrs}: m.CVnode<CodeSampleAttrs>) { |
Hector Dearman | 4544f37 | 2018-09-19 12:32:36 +0100 | [diff] [blame] | 38 | return m( |
| 39 | '.example-code', |
| 40 | m('code', attrs.text), |
| 41 | m('button', |
| 42 | { |
| 43 | onclick: () => copyToClipboard(attrs.text), |
| 44 | }, |
| 45 | 'Copy to clipboard'), ); |
Deepanjan Roy | 97f6324 | 2018-09-20 15:32:01 -0400 | [diff] [blame] | 46 | } |
| 47 | } |
Hector Dearman | 4544f37 | 2018-09-19 12:32:36 +0100 | [diff] [blame] | 48 | |
| 49 | export const RecordPage = createPage({ |
| 50 | view() { |
| 51 | return m( |
| 52 | '.text-column', |
| 53 | 'To collect a 10 second Perfetto trace from an Android phone run this', |
| 54 | ' command:', |
| 55 | m(CodeSample, {text: RECORD_COMMAND_LINE}), |
| 56 | 'Then click "Open trace file" in the menu to the left and select', |
| 57 | ' "/tmp/trace".'); |
| 58 | } |
| 59 | }); |