blob: 02f424fe1787b8172127683acc7d874915a2176d [file] [log] [blame]
Adnan Begovicd5fdee92015-11-13 15:28:21 -08001/*
2 * Copyright (C) 2015 The CyanogenMod 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
17import java.io.BufferedReader;
18import java.io.IOException;
19import java.io.InputStream;
20import java.io.InputStreamReader;
21import java.util.ArrayList;
22import java.util.Arrays;
23import java.util.List;
Adnan Begovicd5fdee92015-11-13 15:28:21 -080024
25/**
26 * Essentially:
27 * adb shell content query --uri content://settings/secure --projection name:value
28 * --where \"name=\'new_setting\'\" --sort \"name ASC\"\n"
29 */
30public class QueryCommand extends Command {
31 private static final String[] QUERY_SETTINGS = {
32 "adb", "shell", "content", "query", "--uri" };
33 private static final String PROJECTION = "name:value";
34
35 private ArrayList<Setting> targetList;
36 private String targetUri;
37
38 protected QueryCommand(String targetUri,
39 ArrayList<Setting> targetList) {
40 this.targetUri = targetUri;
41 this.targetList = targetList;
42 }
43
44 @Override
45 public void run() {
46 System.out.println("\nQuerying settings for authority "
47 + getAuthority() + " for target uri " + targetUri + "...");
48 query(targetUri, targetList);
49 synchronized (this) {
50 notifyAll();
51 }
52 }
53
54 private void query(String uri, ArrayList<Setting> arrayList) {
55 String[] commands = QUERY_SETTINGS;
56 List<String> commandList = new ArrayList<String>(
57 QUERY_SETTINGS.length + 1);
58 commandList.addAll(Arrays.asList(commands));
59 commandList.add(SettingsConstants.CONTENT_URI + getAuthority() + uri);
60 commandList.add("--projection");
61 commandList.add(PROJECTION);
Sam Mortimer542742b2017-09-18 19:44:11 -070062 commandList.add("--show-type"); //this is totally awesomely lineage specific
Adnan Begovicd5fdee92015-11-13 15:28:21 -080063 commandList.add("true");
Paul Keithc9f1d672019-02-17 05:20:04 +010064 commands = commandList.toArray(new String[0]);
Adnan Begovicd5fdee92015-11-13 15:28:21 -080065 if (MigrationTest.DEBUG) {
66 System.out.println("Using commands: " + Arrays.toString(commands));
67 }
68
69 try {
70 final Process process = Runtime.getRuntime().exec(commands);
71 final InputStream err = process.getErrorStream();
72
73 // Send error output to stderr.
74 Thread errThread = new Thread() {
75 @Override
76 public void run() {
77 copy(err, System.err);
78 }
79 };
80 errThread.setDaemon(true);
81 errThread.start();
82
83 BufferedReader in = new BufferedReader(
84 new InputStreamReader(process.getInputStream()));
85
86 String line;
87 while ((line = in.readLine()) != null) {
88 if (!line.startsWith("Row: ")) {
89 throw new IOException("Unable to read settings");
90 }
91 if (MigrationTest.DEBUG) {
92 System.out.println("LINE: " + line);
93 }
94 Setting setting = RowParser.parseAndPopulate(true, line);
95 if (filter(uri, setting)) {
96 continue;
97 }
98 arrayList.add(setting);
99 }
100 in.close();
101 err.close();
102 process.destroy();
103 } catch (IOException e) {
104 System.err.println("Error ");
105 e.printStackTrace();
106 }
107 }
108}