1 /*
2  * Copyright (C) 2018 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 package com.android.dumpviewer.pickers;
17 
18 import com.android.dumpviewer.utils.Exec;
19 import com.android.dumpviewer.utils.Utils;
20 
21 import java.io.IOException;
22 import java.util.concurrent.atomic.AtomicBoolean;
23 import java.util.concurrent.atomic.AtomicReference;
24 
25 public class PackageNamePicker extends PickerActivity {
26     // "[^\]]" doesn't seem to work with Android's sed..?
27     final String COMMAND =
28             "dumpsys package packages | sed -ne 's/^ *Package *\\[\\(.*\\)\\].*/\\1/p'" +
29                     " | sort -u 2>&1";
30 
31     @Override
getList()32     protected String[] getList() {
33         final AtomicBoolean timedOut = new AtomicBoolean();
34         final AtomicReference<String> message = new AtomicReference<>();
35 
36         try {
37             return Exec.runForStrings(
38                     COMMAND,
39                     message::set,
40                     () -> timedOut.set(true),
41                     (e) -> {throw new RuntimeException(e.getMessage(), e);},
42                     5);
43         } catch (IOException e) {
44             if (timedOut.get()) {
45                 return new String[]{"Command timed out"};
46             }
47             Utils.toast(this, "Error: " + e.getMessage());
48             return null;
49         }
50     }
51 }
52