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 insert --uri content://settings/secure |
| 28 | * --bind name:s:new_setting --bind value:s:new_value |
| 29 | */ |
| 30 | public class InsertCommand extends Command { |
| 31 | private static final String[] INSERT_SETTINGS = { |
| 32 | "adb", "shell", "content", "insert", "--uri" }; |
| 33 | |
| 34 | private String targetUri; |
| 35 | private Setting targetSetting; |
| 36 | |
| 37 | public InsertCommand(String targetUri, Setting targetSetting) { |
| 38 | this.targetUri = targetUri; |
| 39 | this.targetSetting = targetSetting; |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public void run() { |
| 44 | System.out.println("\nWriting setting " + targetSetting.getKey() + " for authority " |
| 45 | + getAuthority() + " for target uri " + targetUri + "..."); |
| 46 | insert(targetUri, targetSetting); |
| 47 | synchronized (this) { |
| 48 | notifyAll(); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | private void insert(String uri, Setting setting) { |
| 53 | String[] commands = INSERT_SETTINGS; |
| 54 | List<String> commandList = new ArrayList<String>( |
| 55 | INSERT_SETTINGS.length + 1); |
| 56 | commandList.addAll(Arrays.asList(commands)); |
| 57 | commandList.add(SettingsConstants.CONTENT_URI + getAuthority() + uri); |
| 58 | commandList.add("--bind name:" + setting.getKeyType() + ":" + setting.getKey()); |
| 59 | commandList.add("--bind value:" + setting.getValueType() + ":" |
| 60 | + "\"" + setting.getValue() + "\""); |
Paul Keith | c9f1d67 | 2019-02-17 05:20:04 +0100 | [diff] [blame] | 61 | commands = commandList.toArray(new String[0]); |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 62 | if (MigrationTest.DEBUG) { |
| 63 | System.out.println("Using commands: " + Arrays.toString(commands)); |
| 64 | } |
| 65 | try { |
| 66 | final Process process = Runtime.getRuntime().exec(commands); |
| 67 | final InputStream err = process.getErrorStream(); |
| 68 | |
| 69 | // Send error output to stderr. |
| 70 | Thread errThread = new Thread() { |
| 71 | @Override |
| 72 | public void run() { |
| 73 | copy(err, System.err); |
| 74 | } |
| 75 | }; |
| 76 | errThread.setDaemon(true); |
| 77 | errThread.start(); |
| 78 | |
| 79 | BufferedReader in = new BufferedReader( |
| 80 | new InputStreamReader(process.getInputStream())); |
| 81 | String rx = in.readLine(); |
| 82 | if (rx != null) { |
| 83 | if (MigrationTest.DEBUG) { |
| 84 | System.out.println("Received response " + rx); |
| 85 | } |
| 86 | } |
| 87 | in.close(); |
| 88 | err.close(); |
| 89 | process.destroy(); |
| 90 | } catch (IOException e) { |
| 91 | System.err.println("Error "); |
| 92 | e.printStackTrace(); |
| 93 | } |
| 94 | } |
| 95 | } |