Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | import java.io.BufferedReader; |
| 18 | import java.io.IOException; |
| 19 | import java.io.InputStream; |
| 20 | import java.io.InputStreamReader; |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.Arrays; |
| 23 | import java.util.List; |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 24 | |
| 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 | */ |
| 30 | public 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 Mortimer | 542742b | 2017-09-18 19:44:11 -0700 | [diff] [blame] | 62 | commandList.add("--show-type"); //this is totally awesomely lineage specific |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 63 | commandList.add("true"); |
Paul Keith | c9f1d67 | 2019-02-17 05:20:04 +0100 | [diff] [blame] | 64 | commands = commandList.toArray(new String[0]); |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 65 | 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 | } |