1 /* 2 * Copyright (C) 2014 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 17 package android.os; 18 19 import com.android.ide.common.rendering.api.ILayoutLog; 20 import com.android.layoutlib.bridge.Bridge; 21 import com.android.layoutlib.bridge.impl.DelegateManager; 22 import com.android.tools.layoutlib.annotations.LayoutlibDelegate; 23 24 import java.util.Map; 25 26 /** 27 * Delegate implementing the native methods of android.os.SystemProperties 28 * 29 * Through the layoutlib_create tool, the original native methods of SystemProperties have been 30 * replaced by calls to methods of the same name in this delegate class. 31 * 32 * Because it's a stateless class to start with, there's no need to keep a {@link DelegateManager} 33 * around to map int to instance of the delegate. 34 */ 35 public class SystemProperties_Delegate { 36 37 @LayoutlibDelegate native_get(String key, String def)38 /*package*/ static String native_get(String key, String def) { 39 Map<String, String> properties = Bridge.getPlatformProperties(); 40 String value = properties.get(key); 41 if (value != null) { 42 return value; 43 } 44 45 return def; 46 } 47 @LayoutlibDelegate native_get_int(String key, int def)48 /*package*/ static int native_get_int(String key, int def) { 49 Map<String, String> properties = Bridge.getPlatformProperties(); 50 String value = properties.get(key); 51 if (value != null) { 52 return Integer.decode(value); 53 } 54 55 return def; 56 } 57 58 @LayoutlibDelegate native_get_long(String key, long def)59 /*package*/ static long native_get_long(String key, long def) { 60 Map<String, String> properties = Bridge.getPlatformProperties(); 61 String value = properties.get(key); 62 if (value != null) { 63 return Long.decode(value); 64 } 65 66 return def; 67 } 68 69 /** 70 * Values 'n', 'no', '0', 'false' or 'off' are considered false. 71 * Values 'y', 'yes', '1', 'true' or 'on' are considered true. 72 */ 73 @LayoutlibDelegate native_get_boolean(String key, boolean def)74 /*package*/ static boolean native_get_boolean(String key, boolean def) { 75 Map<String, String> properties = Bridge.getPlatformProperties(); 76 String value = properties.get(key); 77 78 if ("n".equals(value) || "no".equals(value) || "0".equals(value) || "false".equals(value) 79 || "off".equals(value)) { 80 return false; 81 } 82 //noinspection SimplifiableIfStatement 83 if ("y".equals(value) || "yes".equals(value) || "1".equals(value) || "true".equals(value) 84 || "on".equals(value)) { 85 return true; 86 } 87 88 return def; 89 } 90 91 @LayoutlibDelegate native_set(String key, String def)92 /*package*/ static void native_set(String key, String def) { 93 Map<String, String> properties = Bridge.getPlatformProperties(); 94 properties.put(key, def); 95 } 96 97 @LayoutlibDelegate native_add_change_callback()98 /*package*/ static void native_add_change_callback() { 99 // pass. 100 } 101 102 @LayoutlibDelegate native_report_sysprop_change()103 /*package*/ static void native_report_sysprop_change() { 104 // pass. 105 } 106 107 @LayoutlibDelegate native_get(long handle)108 /*package*/ static String native_get(long handle) { 109 Bridge.getLog().fidelityWarning(ILayoutLog.TAG_UNSUPPORTED, 110 "Layoutlib does not support SystemProperties Handle", null, null, null); 111 return null; 112 } 113 114 @LayoutlibDelegate native_get_int(long handle, int def)115 /*package*/ static int native_get_int(long handle, int def) { 116 Bridge.getLog().fidelityWarning(ILayoutLog.TAG_UNSUPPORTED, 117 "Layoutlib does not support SystemProperties Handle", null, null, null); 118 return def; 119 } 120 121 @LayoutlibDelegate native_get_long(long handle, long def)122 /*package*/ static long native_get_long(long handle, long def) { 123 Bridge.getLog().fidelityWarning(ILayoutLog.TAG_UNSUPPORTED, 124 "Layoutlib does not support SystemProperties Handle", null, null, null); 125 return def; 126 } 127 128 @LayoutlibDelegate native_get_boolean(long handle, boolean def)129 /*package*/ static boolean native_get_boolean(long handle, boolean def) { 130 Bridge.getLog().fidelityWarning(ILayoutLog.TAG_UNSUPPORTED, 131 "Layoutlib does not support SystemProperties Handle", null, null, null); 132 return def; 133 } 134 135 @LayoutlibDelegate native_find(String name)136 /*package*/ static long native_find(String name) { 137 Bridge.getLog().fidelityWarning(ILayoutLog.TAG_UNSUPPORTED, 138 "Layoutlib does not support SystemProperties Handle", null, null, null); 139 return 0; 140 } 141 } 142