am f5569413: docs: Fix broken link in Common Intents
* commit 'f556941357ea391cae6dcd2e22b71a837a216d06':
docs: Fix broken link in Common Intents
diff --git a/core/java/android/os/storage/IMountService.java b/core/java/android/os/storage/IMountService.java
index cf407f4..116110e 100644
--- a/core/java/android/os/storage/IMountService.java
+++ b/core/java/android/os/storage/IMountService.java
@@ -856,6 +856,38 @@
}
return _result;
}
+
+ @Override
+ public long lastMaintenance() throws RemoteException {
+ Parcel _data = Parcel.obtain();
+ Parcel _reply = Parcel.obtain();
+ long _result;
+ try {
+ _data.writeInterfaceToken(DESCRIPTOR);
+ mRemote.transact(Stub.TRANSACTION_lastMaintenance, _data, _reply, 0);
+ _reply.readException();
+ _result = _reply.readLong();
+ } finally {
+ _reply.recycle();
+ _data.recycle();
+ }
+ return _result;
+ }
+
+ @Override
+ public void runMaintenance() throws RemoteException {
+ Parcel _data = Parcel.obtain();
+ Parcel _reply = Parcel.obtain();
+ try {
+ _data.writeInterfaceToken(DESCRIPTOR);
+ mRemote.transact(Stub.TRANSACTION_runMaintenance, _data, _reply, 0);
+ _reply.readException();
+ } finally {
+ _reply.recycle();
+ _data.recycle();
+ }
+ return;
+ }
}
private static final String DESCRIPTOR = "IMountService";
@@ -942,6 +974,10 @@
static final int TRANSACTION_resizeSecureContainer = IBinder.FIRST_CALL_TRANSACTION + 40;
+ static final int TRANSACTION_lastMaintenance = IBinder.FIRST_CALL_TRANSACTION + 41;
+
+ static final int TRANSACTION_runMaintenance = IBinder.FIRST_CALL_TRANSACTION + 42;
+
/**
* Cast an IBinder object into an IMountService interface, generating a
* proxy if needed.
@@ -1347,6 +1383,19 @@
reply.writeInt(resultCode);
return true;
}
+ case TRANSACTION_lastMaintenance: {
+ data.enforceInterface(DESCRIPTOR);
+ long lastMaintenance = lastMaintenance();
+ reply.writeNoException();
+ reply.writeLong(lastMaintenance);
+ return true;
+ }
+ case TRANSACTION_runMaintenance: {
+ data.enforceInterface(DESCRIPTOR);
+ runMaintenance();
+ reply.writeNoException();
+ return true;
+ }
}
return super.onTransact(code, data, reply, flags);
}
@@ -1617,4 +1666,18 @@
public String getField(String field) throws RemoteException;
public int resizeSecureContainer(String id, int sizeMb, String key) throws RemoteException;
+
+ /**
+ * Report the time of the last maintenance operation such as fstrim.
+ * @return Timestamp of the last maintenance operation, in the
+ * System.currentTimeMillis() time base
+ * @throws RemoteException
+ */
+ public long lastMaintenance() throws RemoteException;
+
+ /**
+ * Kick off an immediate maintenance operation
+ * @throws RemoteException
+ */
+ public void runMaintenance() throws RemoteException;
}
diff --git a/core/java/android/preference/Preference.java b/core/java/android/preference/Preference.java
index 56d5617..0224c73 100644
--- a/core/java/android/preference/Preference.java
+++ b/core/java/android/preference/Preference.java
@@ -215,7 +215,7 @@
final TypedArray a = context.obtainStyledAttributes(
attrs, com.android.internal.R.styleable.Preference, defStyleAttr, defStyleRes);
- for (int i = a.getIndexCount(); i >= 0; i--) {
+ for (int i = a.getIndexCount() - 1; i >= 0; i--) {
int attr = a.getIndex(i);
switch (attr) {
case com.android.internal.R.styleable.Preference_icon:
diff --git a/core/java/android/preference/PreferenceManager.java b/core/java/android/preference/PreferenceManager.java
index ad940c6..0a0e625 100644
--- a/core/java/android/preference/PreferenceManager.java
+++ b/core/java/android/preference/PreferenceManager.java
@@ -156,7 +156,7 @@
* should be used ANY time a preference will be displayed, since some preference
* types need an Activity for managed queries.
*/
- private PreferenceManager(Context context) {
+ /*package*/ PreferenceManager(Context context) {
init(context);
}
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index 79e84d9..73c7cc3 100644
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -5544,6 +5544,13 @@
public static final String PACKAGE_VERIFIER_INCLUDE_ADB = "verifier_verify_adb_installs";
/**
+ * Time since last fstrim (milliseconds) after which we force one to happen
+ * during device startup. If unset, the default is 3 days.
+ * @hide
+ */
+ public static final String FSTRIM_MANDATORY_INTERVAL = "fstrim_mandatory_interval";
+
+ /**
* The interval in milliseconds at which to check packet counts on the
* mobile data interface when screen is on, to detect possible data
* connection problems.
diff --git a/core/java/android/widget/TimePickerSpinnerDelegate.java b/core/java/android/widget/TimePickerSpinnerDelegate.java
index 73e05e8..61d3d0f 100644
--- a/core/java/android/widget/TimePickerSpinnerDelegate.java
+++ b/core/java/android/widget/TimePickerSpinnerDelegate.java
@@ -61,6 +61,8 @@
// Also NOT a real index, just used for keyboard mode.
private static final int ENABLE_PICKER_INDEX = 3;
+ // LayoutLib relies on these constants. Change TimePickerSpinnerDelegate_Delegate if
+ // modifying these.
private static final int AM = 0;
private static final int PM = 1;
diff --git a/core/res/res/values-mcc204-mnc04/config.xml b/core/res/res/values-mcc204-mnc04/config.xml
index 3c03814..c9c96de 100644
--- a/core/res/res/values-mcc204-mnc04/config.xml
+++ b/core/res/res/values-mcc204-mnc04/config.xml
@@ -31,4 +31,9 @@
<item>"*611:+19085594899,BAE0000000000000"</item>
<item>"*86:+1MDN,BAE0000000000000"</item>
</string-array>
+
+ <string-array translatable="false" name="config_sms_convert_destination_number_support">
+ <item>true;BAE0000000000000</item>
+ <item>false</item>
+ </string-array>
</resources>
diff --git a/core/res/res/values-mcc310-mnc004/config.xml b/core/res/res/values-mcc310-mnc004/config.xml
index 423e250..6a34a3d 100644
--- a/core/res/res/values-mcc310-mnc004/config.xml
+++ b/core/res/res/values-mcc310-mnc004/config.xml
@@ -34,4 +34,9 @@
</string-array>
<bool name="config_auto_attach_data_on_creation">false</bool>
+
+ <string-array translatable="false" name="config_sms_convert_destination_number_support">
+ <item>true</item>
+ </string-array>
+
</resources>
diff --git a/core/res/res/values-mcc311-mnc480/config.xml b/core/res/res/values-mcc311-mnc480/config.xml
index c2be340..379e129 100644
--- a/core/res/res/values-mcc311-mnc480/config.xml
+++ b/core/res/res/values-mcc311-mnc480/config.xml
@@ -49,4 +49,9 @@
<item>"*611:+19085594899,"</item>
<item>"*86:+1MDN,"</item>
</string-array>
+
+ <string-array translatable="false" name="config_sms_convert_destination_number_support">
+ <item>true</item>
+ </string-array>
+
</resources>
diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml
index 1ea37f0..8006659 100644
--- a/core/res/res/values/config.xml
+++ b/core/res/res/values/config.xml
@@ -1853,4 +1853,20 @@
<bool name="config_switch_phone_on_voice_reg_state_change">true</bool>
<bool name="config_sms_force_7bit_encoding">false</bool>
+
+ <!-- This config is used to check if the carrier requires converting destination
+ number before sending out a SMS.
+ Formats for this configuration as below:
+ [true or false][;optional gid]
+ The logic to pick up the configuration:
+ (1) If the "config_sms_convert_destination_number_support" array has no gid
+ special item, the last one will be picked
+ (2) If the "config_sms_convert_destination_number_support" array has gid special
+ item and it matches the current sim's gid, it will be picked.
+ (3) If the "config_sms_convert_destination_number_support" array has gid special
+ item but it doesn't match the current sim's gid, the last one without gid
+ will be picked -->
+ <string-array translatable="false" name="config_sms_convert_destination_number_support">
+ <item>false</item>
+ </string-array>
</resources>
diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml
index c156887..ab97e17 100644
--- a/core/res/res/values/strings.xml
+++ b/core/res/res/values/strings.xml
@@ -3508,6 +3508,9 @@
<!-- [CHAR LIMIT=40] Title of dialog that is shown when performing a system upgrade. -->
<string name="android_upgrading_title">Android is upgrading\u2026</string>
+ <!-- [CHAR LIMIT=NONE] Message shown in upgrading dialog when doing an fstrim. -->
+ <string name="android_upgrading_fstrim">Optimizing storage.</string>
+
<!-- [CHAR LIMIT=NONE] Message shown in upgrading dialog for each .apk that is optimized. -->
<string name="android_upgrading_apk">Optimizing app
<xliff:g id="number" example="123">%1$d</xliff:g> of
diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml
index 2a9e1d1..3078722 100644
--- a/core/res/res/values/symbols.xml
+++ b/core/res/res/values/symbols.xml
@@ -1644,6 +1644,7 @@
<java-symbol type="string" name="aerr_application" />
<java-symbol type="string" name="aerr_process" />
<java-symbol type="string" name="aerr_title" />
+ <java-symbol type="string" name="android_upgrading_fstrim" />
<java-symbol type="string" name="android_upgrading_apk" />
<java-symbol type="string" name="android_upgrading_complete" />
<java-symbol type="string" name="android_upgrading_starting_apps" />
@@ -2072,4 +2073,5 @@
<java-symbol type="bool" name="config_switch_phone_on_voice_reg_state_change" />
<java-symbol type="string" name="whichHomeApplicationNamed" />
<java-symbol type="bool" name="config_sms_force_7bit_encoding" />
+ <java-symbol type="array" name="config_sms_convert_destination_number_support" />
</resources>
diff --git a/services/core/java/com/android/server/AlarmManagerService.java b/services/core/java/com/android/server/AlarmManagerService.java
index 8b524dd..554393af 100644
--- a/services/core/java/com/android/server/AlarmManagerService.java
+++ b/services/core/java/com/android/server/AlarmManagerService.java
@@ -787,7 +787,7 @@
}
setImpl(type, triggerAtTime, windowLength, interval, operation,
- false, workSource, alarmClock);
+ windowLength == AlarmManager.WINDOW_EXACT, workSource, alarmClock);
}
@Override
@@ -1425,12 +1425,9 @@
maxTriggerTime(nowELAPSED, nextElapsed, alarm.repeatInterval),
alarm.repeatInterval, alarm.operation, batch.standalone, true,
alarm.workSource, alarm.alarmClock, alarm.userId);
+ }
- // For now we count this as a wakeup alarm, meaning it needs to be
- // delivered immediately. In the future we should change this, but
- // that required delaying when we reschedule the repeat...!
- hasWakeup = false;
- } else if (alarm.wakeup) {
+ if (alarm.wakeup) {
hasWakeup = true;
}
diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java
index 6a6dcaf..b4a248f 100644
--- a/services/core/java/com/android/server/ConnectivityService.java
+++ b/services/core/java/com/android/server/ConnectivityService.java
@@ -2532,7 +2532,7 @@
String exclList = "";
String pacFileUrl = "";
if (proxyProperties != null && (!TextUtils.isEmpty(proxyProperties.getHost()) ||
- (proxyProperties.getPacFileUrl() != null))) {
+ !Uri.EMPTY.equals(proxyProperties.getPacFileUrl()))) {
if (!proxyProperties.isValid()) {
if (DBG)
log("Invalid proxy properties, ignoring: " + proxyProperties.toString());
@@ -2542,7 +2542,7 @@
host = mGlobalProxy.getHost();
port = mGlobalProxy.getPort();
exclList = mGlobalProxy.getExclusionListAsString();
- if (proxyProperties.getPacFileUrl() != null) {
+ if (!Uri.EMPTY.equals(proxyProperties.getPacFileUrl())) {
pacFileUrl = proxyProperties.getPacFileUrl().toString();
}
} else {
@@ -2604,7 +2604,7 @@
private void handleApplyDefaultProxy(ProxyInfo proxy) {
if (proxy != null && TextUtils.isEmpty(proxy.getHost())
- && (proxy.getPacFileUrl() == null)) {
+ && Uri.EMPTY.equals(proxy.getPacFileUrl())) {
proxy = null;
}
synchronized (mProxyLock) {
@@ -2620,7 +2620,8 @@
// global (to get the correct local port), and send a broadcast.
// TODO: Switch PacManager to have its own message to send back rather than
// reusing EVENT_HAS_CHANGED_PROXY and this call to handleApplyDefaultProxy.
- if ((mGlobalProxy != null) && (proxy != null) && (proxy.getPacFileUrl() != null)
+ if ((mGlobalProxy != null) && (proxy != null)
+ && (!Uri.EMPTY.equals(proxy.getPacFileUrl()))
&& proxy.getPacFileUrl().equals(mGlobalProxy.getPacFileUrl())) {
mGlobalProxy = proxy;
sendProxyBroadcast(mGlobalProxy);
diff --git a/services/core/java/com/android/server/MountService.java b/services/core/java/com/android/server/MountService.java
index e400fb6..6c981c0 100644
--- a/services/core/java/com/android/server/MountService.java
+++ b/services/core/java/com/android/server/MountService.java
@@ -83,6 +83,7 @@
import java.io.File;
import java.io.FileDescriptor;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
@@ -90,7 +91,9 @@
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
+import java.text.SimpleDateFormat;
import java.util.ArrayList;
+import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
@@ -359,6 +362,11 @@
// Used in the ObbActionHandler
private IMediaContainerService mContainerService = null;
+ // Last fstrim operation tracking
+ private static final String LAST_FSTRIM_FILE = "last-fstrim";
+ private final File mLastMaintenanceFile;
+ private long mLastMaintenance;
+
// Handler messages
private static final int H_UNMOUNT_PM_UPDATE = 1;
private static final int H_UNMOUNT_PM_DONE = 2;
@@ -536,6 +544,15 @@
case H_FSTRIM: {
waitForReady();
Slog.i(TAG, "Running fstrim idle maintenance");
+
+ // Remember when we kicked it off
+ try {
+ mLastMaintenance = System.currentTimeMillis();
+ mLastMaintenanceFile.setLastModified(mLastMaintenance);
+ } catch (Exception e) {
+ Slog.e(TAG, "Unable to record last fstrim!");
+ }
+
try {
// This method must be run on the main (handler) thread,
// so it is safe to directly call into vold.
@@ -544,6 +561,7 @@
} catch (NativeDaemonConnectorException ndce) {
Slog.e(TAG, "Failed to run fstrim!");
}
+
// invoke the completion callback, if any
Runnable callback = (Runnable) msg.obj;
if (callback != null) {
@@ -699,6 +717,18 @@
mHandler.sendMessage(mHandler.obtainMessage(H_FSTRIM, callback));
}
+ // Binder entry point for kicking off an immediate fstrim
+ @Override
+ public void runMaintenance() {
+ validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
+ runIdleMaintenance(null);
+ }
+
+ @Override
+ public long lastMaintenance() {
+ return mLastMaintenance;
+ }
+
private void doShareUnshareVolume(String path, String method, boolean enable) {
// TODO: Add support for multiple share methods
if (!method.equals("ums")) {
@@ -1477,6 +1507,22 @@
// Add OBB Action Handler to MountService thread.
mObbActionHandler = new ObbActionHandler(IoThread.get().getLooper());
+ // Initialize the last-fstrim tracking if necessary
+ File dataDir = Environment.getDataDirectory();
+ File systemDir = new File(dataDir, "system");
+ mLastMaintenanceFile = new File(systemDir, LAST_FSTRIM_FILE);
+ if (!mLastMaintenanceFile.exists()) {
+ // Not setting mLastMaintenance here means that we will force an
+ // fstrim during reboot following the OTA that installs this code.
+ try {
+ (new FileOutputStream(mLastMaintenanceFile)).close();
+ } catch (IOException e) {
+ Slog.e(TAG, "Unable to create fstrim record " + mLastMaintenanceFile.getPath());
+ }
+ } else {
+ mLastMaintenance = mLastMaintenanceFile.lastModified();
+ }
+
/*
* Create the connection to vold with a maximum queue of twice the
* amount of containers we'd ever expect to have. This keeps an
@@ -3075,6 +3121,12 @@
pw.increaseIndent();
mConnector.dump(fd, pw, args);
pw.decreaseIndent();
+
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+
+ pw.println();
+ pw.print("Last maintenance: ");
+ pw.println(sdf.format(new Date(mLastMaintenance)));
}
/** {@inheritDoc} */
diff --git a/services/core/java/com/android/server/net/IpConfigStore.java b/services/core/java/com/android/server/net/IpConfigStore.java
index 857b9e9..b5a450d 100644
--- a/services/core/java/com/android/server/net/IpConfigStore.java
+++ b/services/core/java/com/android/server/net/IpConfigStore.java
@@ -122,8 +122,10 @@
out.writeUTF(proxyProperties.getHost());
out.writeUTF(PROXY_PORT_KEY);
out.writeInt(proxyProperties.getPort());
- out.writeUTF(EXCLUSION_LIST_KEY);
- out.writeUTF(exclusionList);
+ if (exclusionList != null) {
+ out.writeUTF(EXCLUSION_LIST_KEY);
+ out.writeUTF(exclusionList);
+ }
written = true;
break;
case PAC:
diff --git a/services/core/java/com/android/server/pm/PackageManagerService.java b/services/core/java/com/android/server/pm/PackageManagerService.java
index 3e1647e..73ceea3 100644
--- a/services/core/java/com/android/server/pm/PackageManagerService.java
+++ b/services/core/java/com/android/server/pm/PackageManagerService.java
@@ -138,6 +138,7 @@
import android.os.Bundle;
import android.os.Environment;
import android.os.Environment.UserEnvironment;
+import android.os.storage.IMountService;
import android.os.storage.StorageManager;
import android.os.Debug;
import android.os.FileUtils;
@@ -161,6 +162,7 @@
import android.system.Os;
import android.system.StructStat;
import android.text.TextUtils;
+import android.text.format.DateUtils;
import android.util.ArraySet;
import android.util.AtomicFile;
import android.util.DisplayMetrics;
@@ -280,6 +282,18 @@
private static final long WATCHDOG_TIMEOUT = 1000*60*10; // ten minutes
/**
+ * Wall-clock timeout (in milliseconds) after which we *require* that an fstrim
+ * be run on this device. We use the value in the Settings.Global.MANDATORY_FSTRIM_INTERVAL
+ * settings entry if available, otherwise we use the hardcoded default. If it's been
+ * more than this long since the last fstrim, we force one during the boot sequence.
+ *
+ * This backstops other fstrim scheduling: if the device is alive at midnight+idle,
+ * one gets run at the next available charging+idle time. This final mandatory
+ * no-fstrim check kicks in only of the other scheduling criteria is never met.
+ */
+ private static final long DEFAULT_MANDATORY_FSTRIM_INTERVAL = 3 * DateUtils.DAY_IN_MILLIS;
+
+ /**
* Whether verification is enabled by default.
*/
private static final boolean DEFAULT_VERIFY_ENABLE = true;
@@ -4506,6 +4520,37 @@
public void performBootDexOpt() {
enforceSystemOrRoot("Only the system can request dexopt be performed");
+ // Before everything else, see whether we need to fstrim.
+ try {
+ IMountService ms = PackageHelper.getMountService();
+ if (ms != null) {
+ final long interval = android.provider.Settings.Global.getLong(
+ mContext.getContentResolver(),
+ android.provider.Settings.Global.FSTRIM_MANDATORY_INTERVAL,
+ DEFAULT_MANDATORY_FSTRIM_INTERVAL);
+ if (interval > 0) {
+ final long timeSinceLast = System.currentTimeMillis() - ms.lastMaintenance();
+ if (timeSinceLast > interval) {
+ Slog.w(TAG, "No disk maintenance in " + timeSinceLast
+ + "; running immediately");
+ if (!isFirstBoot()) {
+ try {
+ ActivityManagerNative.getDefault().showBootMessage(
+ mContext.getResources().getString(
+ R.string.android_upgrading_fstrim), true);
+ } catch (RemoteException e) {
+ }
+ }
+ ms.runMaintenance();
+ }
+ }
+ } else {
+ Slog.e(TAG, "Mount service unavailable!");
+ }
+ } catch (RemoteException e) {
+ // Can't happen; MountService is local
+ }
+
final HashSet<PackageParser.Package> pkgs;
synchronized (mPackages) {
pkgs = mDeferredDexOpt;
diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java
index 92ad1ad..22f6ca4 100644
--- a/services/java/com/android/server/SystemServer.java
+++ b/services/java/com/android/server/SystemServer.java
@@ -546,20 +546,6 @@
reportWtf("making display ready", e);
}
- try {
- mPackageManagerService.performBootDexOpt();
- } catch (Throwable e) {
- reportWtf("performing boot dexopt", e);
- }
-
- try {
- ActivityManagerNative.getDefault().showBootMessage(
- context.getResources().getText(
- com.android.internal.R.string.android_upgrading_starting_apps),
- false);
- } catch (RemoteException e) {
- }
-
if (mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
if (!disableStorage &&
!"0".equals(SystemProperties.get("system_init.startmountservice"))) {
@@ -575,7 +561,23 @@
reportWtf("starting Mount Service", e);
}
}
+ }
+ try {
+ mPackageManagerService.performBootDexOpt();
+ } catch (Throwable e) {
+ reportWtf("performing boot dexopt", e);
+ }
+
+ try {
+ ActivityManagerNative.getDefault().showBootMessage(
+ context.getResources().getText(
+ com.android.internal.R.string.android_upgrading_starting_apps),
+ false);
+ } catch (RemoteException e) {
+ }
+
+ if (mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
if (!disableNonCoreServices) {
try {
Slog.i(TAG, "LockSettingsService");
diff --git a/services/usb/java/com/android/server/usb/UsbDeviceManager.java b/services/usb/java/com/android/server/usb/UsbDeviceManager.java
index c63eb18..23ba3b6 100644
--- a/services/usb/java/com/android/server/usb/UsbDeviceManager.java
+++ b/services/usb/java/com/android/server/usb/UsbDeviceManager.java
@@ -209,8 +209,13 @@
mUseUsbNotification = !massStorageSupported;
// make sure the ADB_ENABLED setting value matches the current state
- Settings.Global.putInt(mContentResolver, Settings.Global.ADB_ENABLED, mAdbEnabled ? 1 : 0);
-
+ try {
+ Settings.Global.putInt(mContentResolver,
+ Settings.Global.ADB_ENABLED, mAdbEnabled ? 1 : 0);
+ } catch (SecurityException e) {
+ // If UserManager.DISALLOW_DEBUGGING_FEATURES is on, that this setting can't be changed.
+ Slog.d(TAG, "ADB_ENABLED is restricted.");
+ }
mHandler.sendEmptyMessage(MSG_SYSTEM_READY);
}
diff --git a/tools/layoutlib/.idea/codeStyleSettings.xml b/tools/layoutlib/.idea/codeStyleSettings.xml
index b324213..a04e440 100644
--- a/tools/layoutlib/.idea/codeStyleSettings.xml
+++ b/tools/layoutlib/.idea/codeStyleSettings.xml
@@ -67,9 +67,13 @@
</groups>
</arrangement>
</codeStyleSettings>
+ <codeStyleSettings language="XML">
+ <indentOptions>
+ <option name="CONTINUATION_INDENT_SIZE" value="4" />
+ </indentOptions>
+ </codeStyleSettings>
</value>
</option>
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
</component>
-</project>
-
+</project>
\ No newline at end of file
diff --git a/tools/layoutlib/bridge/src/android/content/res/BridgeAssetManager.java b/tools/layoutlib/bridge/src/android/content/res/BridgeAssetManager.java
index 93814b2..c41a4ee 100644
--- a/tools/layoutlib/bridge/src/android/content/res/BridgeAssetManager.java
+++ b/tools/layoutlib/bridge/src/android/content/res/BridgeAssetManager.java
@@ -23,7 +23,7 @@
public class BridgeAssetManager extends AssetManager {
/**
- * This initializes the static field {@link AssetManager#mSystem} which is used
+ * This initializes the static field {@link AssetManager#sSystem} which is used
* by methods who get a global asset manager using {@link AssetManager#getSystem()}.
* <p/>
* They will end up using our bridge asset manager.
diff --git a/tools/layoutlib/bridge/src/android/graphics/FontFamily_Delegate.java b/tools/layoutlib/bridge/src/android/graphics/FontFamily_Delegate.java
index 4993262..ab79664 100644
--- a/tools/layoutlib/bridge/src/android/graphics/FontFamily_Delegate.java
+++ b/tools/layoutlib/bridge/src/android/graphics/FontFamily_Delegate.java
@@ -282,7 +282,7 @@
@LayoutlibDelegate
/*package*/ static boolean nAddFontFromAsset(long nativeFamily, AssetManager mgr, String path) {
Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
- "FontFamily.addFontFromAsset is not supported.", null, null);
+ "Typeface.createFromAsset is not supported.", null, null);
return false;
}
diff --git a/tools/layoutlib/bridge/src/android/preference/BridgePreferenceInflater.java b/tools/layoutlib/bridge/src/android/preference/BridgePreferenceInflater.java
new file mode 100644
index 0000000..4f00b5d
--- /dev/null
+++ b/tools/layoutlib/bridge/src/android/preference/BridgePreferenceInflater.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.preference;
+
+import com.android.layoutlib.bridge.android.BridgeContext;
+import com.android.layoutlib.bridge.android.BridgeXmlBlockParser;
+
+import android.content.Context;
+import android.util.AttributeSet;
+
+public class BridgePreferenceInflater extends PreferenceInflater {
+
+ public BridgePreferenceInflater(Context context, PreferenceManager preferenceManager) {
+ super(context, preferenceManager);
+ }
+
+ @Override
+ protected Preference onCreateItem(String name, AttributeSet attrs)
+ throws ClassNotFoundException {
+ Object viewKey = null;
+ BridgeContext bc = null;
+
+ Context context = getContext();
+ if (context instanceof BridgeContext) {
+ bc = (BridgeContext) context;
+ }
+ if (attrs instanceof BridgeXmlBlockParser) {
+ viewKey = ((BridgeXmlBlockParser) attrs).getViewCookie();
+ }
+
+ Preference preference = super.onCreateItem(name, attrs);
+
+ if (viewKey != null && bc != null) {
+ bc.addCookie(preference, viewKey);
+ }
+ return preference;
+ }
+}
diff --git a/tools/layoutlib/bridge/src/android/preference/Preference_Delegate.java b/tools/layoutlib/bridge/src/android/preference/Preference_Delegate.java
new file mode 100644
index 0000000..49ee642
--- /dev/null
+++ b/tools/layoutlib/bridge/src/android/preference/Preference_Delegate.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.preference;
+
+import com.android.internal.R;
+import com.android.layoutlib.bridge.android.BridgeContext;
+import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
+
+import org.xmlpull.v1.XmlPullParser;
+
+import android.content.Context;
+import android.content.res.TypedArray;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ListView;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Delegate that provides implementation for native methods in {@link Preference}
+ * <p/>
+ * Through the layoutlib_create tool, selected methods of Preference have been replaced by calls to
+ * methods of the same name in this delegate class.
+ */
+public class Preference_Delegate {
+
+ @LayoutlibDelegate
+ /*package*/ static View getView(Preference pref, View convertView, ViewGroup parent) {
+ Context context = pref.getContext();
+ BridgeContext bc = context instanceof BridgeContext ? ((BridgeContext) context) : null;
+ convertView = pref.getView_Original(convertView, parent);
+ if (bc != null) {
+ Object cookie = bc.getCookie(pref);
+ if (cookie != null) {
+ bc.addViewKey(convertView, cookie);
+ }
+ }
+ return convertView;
+ }
+
+ /**
+ * Inflates the parser and returns the ListView containing the Preferences.
+ */
+ public static View inflatePreference(Context context, XmlPullParser parser, ViewGroup root) {
+ PreferenceManager pm = new PreferenceManager(context);
+ PreferenceScreen ps = pm.getPreferenceScreen();
+ PreferenceInflater inflater = new BridgePreferenceInflater(context, pm);
+ ps = (PreferenceScreen) inflater.inflate(parser, ps, true);
+ ListView preferenceView = createContainerView(context, root);
+ ps.bind(preferenceView);
+ return preferenceView;
+ }
+
+ private static ListView createContainerView(Context context, ViewGroup root) {
+ TypedArray a = context.obtainStyledAttributes(null, R.styleable.PreferenceFragment,
+ R.attr.preferenceFragmentStyle, 0);
+ int mLayoutResId = a.getResourceId(R.styleable.PreferenceFragment_layout,
+ R.layout.preference_list_fragment);
+ a.recycle();
+
+ LayoutInflater inflater =
+ (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+ inflater.inflate(mLayoutResId, root, true);
+
+ return (ListView) root.findViewById(android.R.id.list);
+ }
+}
diff --git a/tools/layoutlib/bridge/src/android/text/StaticLayout_Delegate.java b/tools/layoutlib/bridge/src/android/text/StaticLayout_Delegate.java
index 5a467b2..b0d79a8 100644
--- a/tools/layoutlib/bridge/src/android/text/StaticLayout_Delegate.java
+++ b/tools/layoutlib/bridge/src/android/text/StaticLayout_Delegate.java
@@ -13,8 +13,8 @@
/**
* Delegate that provides implementation for native methods in {@link android.text.StaticLayout}
- *
- * Through the layoutlib_create tool, selected methods of Handler have been replaced
+ * <p/>
+ * Through the layoutlib_create tool, selected methods of StaticLayout have been replaced
* by calls to methods of the same name in this delegate class.
*
*/
diff --git a/tools/layoutlib/bridge/src/android/text/format/Time_Delegate.java b/tools/layoutlib/bridge/src/android/text/format/Time_Delegate.java
deleted file mode 100644
index ed8498f..0000000
--- a/tools/layoutlib/bridge/src/android/text/format/Time_Delegate.java
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * Copyright (C) 2013 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package android.text.format;
-
-import java.util.Calendar;
-import java.util.TimeZone;
-import java.util.UnknownFormatConversionException;
-import java.util.regex.Pattern;
-
-import com.android.ide.common.rendering.api.LayoutLog;
-import com.android.layoutlib.bridge.Bridge;
-import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
-
-/**
- * Delegate used to provide new implementation for native methods of {@link Time}
- *
- * Through the layoutlib_create tool, some native methods of Time have been replaced by calls to
- * methods of the same name in this delegate class.
- */
-public class Time_Delegate {
-
- // Regex to match odd number of '%'.
- private static final Pattern p = Pattern.compile("(?<!%)(%%)*%(?!%)");
-
- // Format used by toString()
- private static final String FORMAT = "%1$tY%1$tm%1$tdT%1$tH%1$tM%1$tS<%1$tZ>";
-
- // ---- private helper methods ----
-
- private static Calendar timeToCalendar(Time time) {
- Calendar calendar = getCalendarInstance(time);
- calendar.set(time.year, time.month, time.monthDay, time.hour, time.minute, time.second);
- return calendar;
- }
-
- private static void calendarToTime(Calendar c, Time time) {
- time.timezone = c.getTimeZone().getID();
- time.set(c.get(Calendar.SECOND), c.get(Calendar.MINUTE), c.get(Calendar.HOUR_OF_DAY),
- c.get(Calendar.DATE), c.get(Calendar.MONTH), c.get(Calendar.YEAR));
- time.weekDay = c.get(Calendar.DAY_OF_WEEK);
- time.yearDay = c.get(Calendar.DAY_OF_YEAR);
- time.isDst = c.getTimeZone().inDaylightTime(c.getTime()) ? 1 : 0;
- // gmtoff is in seconds and TimeZone.getOffset() returns milliseconds.
- time.gmtoff = c.getTimeZone().getOffset(c.getTimeInMillis()) / DateUtils.SECOND_IN_MILLIS;
- }
-
- /**
- * Return a calendar instance with the correct timezone.
- *
- * @param time Time to obtain the timezone from.
- */
- private static Calendar getCalendarInstance(Time time) {
- // TODO: Check platform code to make sure the behavior is same for null/invalid timezone.
- if (time == null || time.timezone == null) {
- // Default to local timezone.
- return Calendar.getInstance();
- }
- // If timezone is invalid, use GMT.
- return Calendar.getInstance(TimeZone.getTimeZone(time.timezone));
- }
-}
diff --git a/tools/layoutlib/bridge/src/android/widget/TimePickerSpinnerDelegate_Delegate.java b/tools/layoutlib/bridge/src/android/widget/TimePickerSpinnerDelegate_Delegate.java
new file mode 100644
index 0000000..c9d35b9
--- /dev/null
+++ b/tools/layoutlib/bridge/src/android/widget/TimePickerSpinnerDelegate_Delegate.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.widget;
+
+import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
+
+import android.view.KeyEvent;
+
+/**
+ * Delegate used to provide new implementation of few methods in {@link TimePickerSpinnerDelegate}.
+ */
+public class TimePickerSpinnerDelegate_Delegate {
+
+ // Copied from TimePickerSpinnerDelegate.
+ private static final int AM = 0;
+ private static final int PM = 1;
+
+ @LayoutlibDelegate
+ static int getAmOrPmKeyCode(TimePickerSpinnerDelegate tpsd, int amOrPm) {
+ // We don't care about locales here.
+ if (amOrPm == AM) {
+ return KeyEvent.KEYCODE_A;
+ } else if (amOrPm == PM) {
+ return KeyEvent.KEYCODE_P;
+ } else {
+ assert false : "amOrPm value in TimePickerSpinnerDelegate can only be 0 or 1";
+ return -1;
+ }
+ }
+}
diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java
index ec78712..4d2c2fc 100644
--- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java
+++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java
@@ -22,6 +22,7 @@
import com.android.annotations.NonNull;
import com.android.ide.common.rendering.api.Capability;
import com.android.ide.common.rendering.api.DrawableParams;
+import com.android.ide.common.rendering.api.Features;
import com.android.ide.common.rendering.api.LayoutLog;
import com.android.ide.common.rendering.api.RenderSession;
import com.android.ide.common.rendering.api.Result;
@@ -36,6 +37,7 @@
import com.android.tools.layoutlib.create.OverrideMethod;
import com.android.util.Pair;
import com.ibm.icu.util.ULocale;
+import libcore.io.MemoryMappedFile_Delegate;
import android.content.res.BridgeAssetManager;
import android.graphics.Bitmap;
@@ -179,7 +181,7 @@
*/
private static LayoutLog sCurrentLog = sDefaultLog;
- private EnumSet<Capability> mCapabilities;
+ private static final int LAST_SUPPORTED_FEATURE = Features.PREFERENCES_RENDERING;
@Override
public int getApiLevel() {
@@ -187,8 +189,16 @@
}
@Override
+ @Deprecated
public EnumSet<Capability> getCapabilities() {
- return mCapabilities;
+ // The Capability class is deprecated and frozen. All Capabilities enumerated there are
+ // supported by this version of LayoutLibrary. So, it's safe to use EnumSet.allOf()
+ return EnumSet.allOf(Capability.class);
+ }
+
+ @Override
+ public boolean supports(int feature) {
+ return feature <= LAST_SUPPORTED_FEATURE;
}
@Override
@@ -199,26 +209,6 @@
sPlatformProperties = platformProperties;
sEnumValueMap = enumValueMap;
- // don't use EnumSet.allOf(), because the bridge doesn't come with its specific version
- // of layoutlib_api. It is provided by the client which could have a more recent version
- // with newer, unsupported capabilities.
- mCapabilities = EnumSet.of(
- Capability.UNBOUND_RENDERING,
- Capability.CUSTOM_BACKGROUND_COLOR,
- Capability.RENDER,
- Capability.LAYOUT_ONLY,
- Capability.EMBEDDED_LAYOUT,
- Capability.VIEW_MANIPULATION,
- Capability.PLAY_ANIMATION,
- Capability.ANIMATED_VIEW_MANIPULATION,
- Capability.ADAPTER_BINDING,
- Capability.EXTENDED_VIEWINFO,
- Capability.FIXED_SCALABLE_NINE_PATCH,
- Capability.RTL,
- Capability.ACTION_BAR,
- Capability.SIMULATE_PLATFORM);
-
-
BridgeAssetManager.initSystem();
// When DEBUG_LAYOUT is set and is not 0 or false, setup a default listener
@@ -252,6 +242,7 @@
// load the fonts.
FontFamily_Delegate.setFontLocation(fontLocation.getAbsolutePath());
+ MemoryMappedFile_Delegate.setDataDir(fontLocation.getAbsoluteFile().getParentFile());
// now parse com.android.internal.R (and only this one as android.R is a subset of
// the internal version), and put the content in the maps.
diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java
index aeb70e9..8523f1a 100644
--- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java
+++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java
@@ -73,6 +73,7 @@
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
+import android.view.accessibility.AccessibilityManager;
import android.view.textservice.TextServicesManager;
import java.io.File;
@@ -92,8 +93,15 @@
*/
public final class BridgeContext extends Context {
- private Resources mSystemResources;
+ /** The map adds cookies to each view so that IDE can link xml tags to views. */
private final HashMap<View, Object> mViewKeyMap = new HashMap<View, Object>();
+ /**
+ * In some cases, when inflating an xml, some objects are created. Then later, the objects are
+ * converted to views. This map stores the mapping from objects to cookies which can then be
+ * used to populate the mViewKeyMap.
+ */
+ private final HashMap<Object, Object> mViewKeyHelpMap = new HashMap<Object, Object>();
+ private Resources mSystemResources;
private final Object mProjectKey;
private final DisplayMetrics mMetrics;
private final RenderResources mRenderResources;
@@ -120,8 +128,9 @@
private BridgeContentResolver mContentResolver;
private final Stack<BridgeXmlBlockParser> mParserStack = new Stack<BridgeXmlBlockParser>();
+ private SharedPreferences mSharedPreferences;
- /**
+ /**
* @param projectKey An Object identifying the project. This is used for the cache mechanism.
* @param metrics the {@link DisplayMetrics}.
* @param renderResources the configured resources (both framework and projects) for this
@@ -189,6 +198,14 @@
return mViewKeyMap.get(view);
}
+ public void addCookie(Object o, Object cookie) {
+ mViewKeyHelpMap.put(o, cookie);
+ }
+
+ public Object getCookie(Object o) {
+ return mViewKeyHelpMap.get(o);
+ }
+
public Object getProjectKey() {
return mProjectKey;
}
@@ -460,6 +477,10 @@
return mDisplayManager;
}
+ if (ACCESSIBILITY_SERVICE.equals(service)) {
+ return AccessibilityManager.getInstance(this);
+ }
+
throw new UnsupportedOperationException("Unsupported Service: " + service);
}
@@ -475,28 +496,22 @@
@Override
public final BridgeTypedArray obtainStyledAttributes(int resid, int[] attrs)
throws Resources.NotFoundException {
+ StyleResourceValue style = null;
// get the StyleResourceValue based on the resId;
- StyleResourceValue style = getStyleByDynamicId(resid);
+ if (resid != 0) {
+ style = getStyleByDynamicId(resid);
- if (style == null) {
- // In some cases, style may not be a dynamic id, so we do a full search.
- ResourceReference ref = resolveId(resid);
- if (ref != null) {
- if (ref.isFramework()) {
- ref =
- getRenderResources().getFrameworkResource(ResourceType.STYLE, ref.getName());
- } else {
- ref =
- getRenderResources().getProjectResource(ResourceType.STYLE, ref.getName());
- }
- if (ref instanceof StyleResourceValue) {
- style = ((StyleResourceValue) ref);
+ if (style == null) {
+ // In some cases, style may not be a dynamic id, so we do a full search.
+ ResourceReference ref = resolveId(resid);
+ if (ref != null) {
+ style = mRenderResources.getStyle(ref.getName(), ref.isFramework());
}
}
- }
- if (style == null) {
- throw new Resources.NotFoundException();
+ if (style == null) {
+ throw new Resources.NotFoundException();
+ }
}
if (mTypedArrayCache == null) {
@@ -1172,8 +1187,10 @@
@Override
public SharedPreferences getSharedPreferences(String arg0, int arg1) {
- // pass
- return null;
+ if (mSharedPreferences == null) {
+ mSharedPreferences = new BridgeSharedPreferences();
+ }
+ return mSharedPreferences;
}
@Override
diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeSharedPreferences.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeSharedPreferences.java
new file mode 100644
index 0000000..132ff2f
--- /dev/null
+++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeSharedPreferences.java
@@ -0,0 +1,138 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.layoutlib.bridge.android;
+
+import android.content.SharedPreferences;
+
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * An empty shared preferences implementation which doesn't store anything. It always returns
+ * null, 0 or false.
+ */
+public class BridgeSharedPreferences implements SharedPreferences {
+ private Editor mEditor;
+
+ @Override
+ public Map<String, ?> getAll() {
+ return null;
+ }
+
+ @Override
+ public String getString(String key, String defValue) {
+ return null;
+ }
+
+ @Override
+ public Set<String> getStringSet(String key, Set<String> defValues) {
+ return null;
+ }
+
+ @Override
+ public int getInt(String key, int defValue) {
+ return 0;
+ }
+
+ @Override
+ public long getLong(String key, long defValue) {
+ return 0;
+ }
+
+ @Override
+ public float getFloat(String key, float defValue) {
+ return 0;
+ }
+
+ @Override
+ public boolean getBoolean(String key, boolean defValue) {
+ return false;
+ }
+
+ @Override
+ public boolean contains(String key) {
+ return false;
+ }
+
+ @Override
+ public Editor edit() {
+ if (mEditor != null) {
+ return mEditor;
+ }
+ mEditor = new Editor() {
+ @Override
+ public Editor putString(String key, String value) {
+ return null;
+ }
+
+ @Override
+ public Editor putStringSet(String key, Set<String> values) {
+ return null;
+ }
+
+ @Override
+ public Editor putInt(String key, int value) {
+ return null;
+ }
+
+ @Override
+ public Editor putLong(String key, long value) {
+ return null;
+ }
+
+ @Override
+ public Editor putFloat(String key, float value) {
+ return null;
+ }
+
+ @Override
+ public Editor putBoolean(String key, boolean value) {
+ return null;
+ }
+
+ @Override
+ public Editor remove(String key) {
+ return null;
+ }
+
+ @Override
+ public Editor clear() {
+ return null;
+ }
+
+ @Override
+ public boolean commit() {
+ return false;
+ }
+
+ @Override
+ public void apply() {
+ }
+ };
+ return mEditor;
+ }
+
+ @Override
+ public void registerOnSharedPreferenceChangeListener(
+ OnSharedPreferenceChangeListener listener) {
+ }
+
+ @Override
+ public void unregisterOnSharedPreferenceChangeListener(
+ OnSharedPreferenceChangeListener listener) {
+ }
+}
diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/SessionParamsFlags.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/SessionParamsFlags.java
new file mode 100644
index 0000000..e00ea6a
--- /dev/null
+++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/SessionParamsFlags.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.layoutlib.bridge.android;
+
+import com.android.ide.common.rendering.api.SessionParams;
+
+/**
+ * This contains all known keys for the {@link SessionParams#getFlag(SessionParams.Key)}.
+ * <p/>
+ * The IDE has its own copy of this class which may be newer or older than this one.
+ * <p/>
+ * Constants should never be modified or removed from this class.
+ */
+public final class SessionParamsFlags {
+
+ public static final SessionParams.Key<String> FLAG_KEY_ROOT_TAG =
+ new SessionParams.Key<String>("rootTag", String.class);
+
+ // Disallow instances.
+ private SessionParamsFlags() {}
+}
diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java
index a2eed9a..4637bfd 100644
--- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java
+++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java
@@ -36,6 +36,7 @@
import com.android.ide.common.rendering.api.Result.Status;
import com.android.ide.common.rendering.api.SessionParams;
import com.android.ide.common.rendering.api.SessionParams.RenderingMode;
+import com.android.ide.common.rendering.api.StyleResourceValue;
import com.android.ide.common.rendering.api.ViewInfo;
import com.android.ide.common.rendering.api.ViewType;
import com.android.internal.util.XmlUtils;
@@ -49,6 +50,7 @@
import com.android.layoutlib.bridge.android.BridgeContext;
import com.android.layoutlib.bridge.android.BridgeLayoutParamsMapAttributes;
import com.android.layoutlib.bridge.android.BridgeXmlBlockParser;
+import com.android.layoutlib.bridge.android.SessionParamsFlags;
import com.android.layoutlib.bridge.bars.Config;
import com.android.layoutlib.bridge.bars.NavigationBar;
import com.android.layoutlib.bridge.bars.StatusBar;
@@ -73,6 +75,7 @@
import android.graphics.Bitmap_Delegate;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
+import android.preference.Preference_Delegate;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.AttachInfo_Accessor;
@@ -87,7 +90,6 @@
import android.view.ViewGroup.MarginLayoutParams;
import android.view.ViewParent;
import android.view.WindowManagerGlobal_Delegate;
-import android.view.ViewParent;
import android.widget.AbsListView;
import android.widget.AbsSpinner;
import android.widget.ActionMenuView;
@@ -133,6 +135,7 @@
private int mMeasuredScreenHeight = -1;
private boolean mIsAlphaChannelImage;
private boolean mWindowIsFloating;
+ private Boolean mIsThemeAppCompat;
private int mStatusBarSize;
private int mNavigationBarSize;
@@ -193,11 +196,9 @@
DisplayMetrics metrics = getContext().getMetrics();
// use default of true in case it's not found to use alpha by default
- mIsAlphaChannelImage = getBooleanThemeValue(resources,
- "windowIsFloating", true /*defaultValue*/);
-
- mWindowIsFloating = getBooleanThemeValue(resources, "windowIsFloating",
- true /*defaultValue*/);
+ mIsAlphaChannelImage = getBooleanThemeValue(resources, "windowIsFloating", true, true);
+ // FIXME: Find out why both variables are taking the same value.
+ mWindowIsFloating = getBooleanThemeValue(resources, "windowIsFloating", true, true);
findBackground(resources);
findStatusBar(resources, metrics);
@@ -397,7 +398,15 @@
// it can instantiate the custom Fragment.
Fragment_Delegate.setProjectCallback(params.getProjectCallback());
- View view = mInflater.inflate(mBlockParser, mContentRoot);
+ String rootTag = params.getFlag(SessionParamsFlags.FLAG_KEY_ROOT_TAG);
+ boolean isPreference = "PreferenceScreen".equals(rootTag);
+ View view;
+ if (isPreference) {
+ view = Preference_Delegate.inflatePreference(getContext(), mBlockParser,
+ mContentRoot);
+ } else {
+ view = mInflater.inflate(mBlockParser, mContentRoot);
+ }
// done with the parser, pop it.
context.popParser();
@@ -408,7 +417,7 @@
AttachInfo_Accessor.setAttachInfo(mViewRoot);
// post-inflate process. For now this supports TabHost/TabWidget
- postInflateProcess(view, params.getProjectCallback());
+ postInflateProcess(view, params.getProjectCallback(), isPreference ? view : null);
// get the background drawable
if (mWindowBackground != null) {
@@ -1050,7 +1059,7 @@
private void findStatusBar(RenderResources resources, DisplayMetrics metrics) {
boolean windowFullscreen = getBooleanThemeValue(resources,
- "windowFullscreen", false /*defaultValue*/);
+ "windowFullscreen", false, !isThemeAppCompat(resources));
if (!windowFullscreen && !mWindowIsFloating) {
// default value
@@ -1077,7 +1086,7 @@
}
boolean windowActionBar = getBooleanThemeValue(resources,
- "windowActionBar", true /*defaultValue*/);
+ "windowActionBar", true, !isThemeAppCompat(resources));
// if there's a value and it's false (default is true)
if (windowActionBar) {
@@ -1104,7 +1113,7 @@
} else {
// action bar overrides title bar so only look for this one if action bar is hidden
boolean windowNoTitle = getBooleanThemeValue(resources,
- "windowNoTitle", false /*defaultValue*/);
+ "windowNoTitle", false, !isThemeAppCompat(resources));
if (!windowNoTitle) {
@@ -1176,20 +1185,30 @@
}
}
+ private boolean isThemeAppCompat(RenderResources resources) {
+ // Ideally, we should check if the corresponding activity extends
+ // android.support.v7.app.ActionBarActivity, and not care about the theme name at all.
+ if (mIsThemeAppCompat == null) {
+ StyleResourceValue defaultTheme = resources.getDefaultTheme();
+ StyleResourceValue val = resources.getStyle("Theme.AppCompat", false);
+ mIsThemeAppCompat = defaultTheme == val || resources.themeIsParentOf(val, defaultTheme);
+ }
+ return mIsThemeAppCompat;
+ }
+
/**
- * Looks for a attribute in the current theme. The attribute is in the android
- * namespace.
+ * Looks for an attribute in the current theme.
*
* @param resources the render resources
* @param name the name of the attribute
* @param defaultValue the default value.
+ * @param isFrameworkAttr if the attribute is in android namespace
* @return the value of the attribute or the default one if not found.
*/
private boolean getBooleanThemeValue(RenderResources resources,
- String name, boolean defaultValue) {
+ String name, boolean defaultValue, boolean isFrameworkAttr) {
- // get the title bar flag from the current theme.
- ResourceValue value = resources.findItemInTheme(name, true /*isFrameworkAttr*/);
+ ResourceValue value = resources.findItemInTheme(name, isFrameworkAttr);
// because it may reference something else, we resolve it.
value = resources.resolveResValue(value);
@@ -1210,12 +1229,16 @@
* based on the content of the {@link FrameLayout}.
* @param view the root view to process.
* @param projectCallback callback to the project.
+ * @param skip the view and it's children are not processed.
*/
@SuppressWarnings("deprecation") // For the use of Pair
- private void postInflateProcess(View view, IProjectCallback projectCallback)
+ private void postInflateProcess(View view, IProjectCallback projectCallback, View skip)
throws PostInflateException {
+ if (view == skip) {
+ return;
+ }
if (view instanceof TabHost) {
- setupTabHost((TabHost)view, projectCallback);
+ setupTabHost((TabHost) view, projectCallback);
} else if (view instanceof QuickContactBadge) {
QuickContactBadge badge = (QuickContactBadge) view;
badge.setImageToDefault();
@@ -1248,7 +1271,7 @@
boolean skipCallbackParser = false;
int count = binding.getHeaderCount();
- for (int i = 0 ; i < count ; i++) {
+ for (int i = 0; i < count; i++) {
Pair<View, Boolean> pair = context.inflateView(
binding.getHeaderAt(i),
list, false /*attachToRoot*/, skipCallbackParser);
@@ -1260,7 +1283,7 @@
}
count = binding.getFooterCount();
- for (int i = 0 ; i < count ; i++) {
+ for (int i = 0; i < count; i++) {
Pair<View, Boolean> pair = context.inflateView(
binding.getFooterAt(i),
list, false /*attachToRoot*/, skipCallbackParser);
@@ -1289,11 +1312,11 @@
}
}
} else if (view instanceof ViewGroup) {
- ViewGroup group = (ViewGroup)view;
+ ViewGroup group = (ViewGroup) view;
final int count = group.getChildCount();
- for (int c = 0 ; c < count ; c++) {
+ for (int c = 0; c < count; c++) {
View child = group.getChildAt(c);
- postInflateProcess(child, projectCallback);
+ postInflateProcess(child, projectCallback, skip);
}
}
}
@@ -1361,6 +1384,7 @@
for (int i = 0 ; i < count ; i++) {
View child = content.getChildAt(i);
String tabSpec = String.format("tab_spec%d", i+1);
+ @SuppressWarnings("ConstantConditions") // child cannot be null.
int id = child.getId();
@SuppressWarnings("deprecation")
Pair<ResourceType, String> resource = projectCallback.resolveResourceId(id);
diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/libcore/io/BridgeBufferIterator.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/libcore/io/BridgeBufferIterator.java
new file mode 100644
index 0000000..7e361a1
--- /dev/null
+++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/libcore/io/BridgeBufferIterator.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.layoutlib.bridge.libcore.io;
+
+import java.nio.ByteBuffer;
+
+import libcore.io.BufferIterator;
+
+/**
+ * Provides an implementation of {@link BufferIterator} over a {@link ByteBuffer}.
+ */
+public class BridgeBufferIterator extends BufferIterator {
+
+ private final long mSize;
+ private final ByteBuffer mByteBuffer;
+
+ public BridgeBufferIterator(long size, ByteBuffer buffer) {
+ mSize = size;
+ mByteBuffer = buffer;
+ }
+
+ @Override
+ public void seek(int offset) {
+ assert offset <= mSize;
+ mByteBuffer.position(offset);
+ }
+
+ @Override
+ public void skip(int byteCount) {
+ int newPosition = mByteBuffer.position() + byteCount;
+ assert newPosition <= mSize;
+ mByteBuffer.position(newPosition);
+ }
+
+ @Override
+ public void readByteArray(byte[] dst, int dstOffset, int byteCount) {
+ assert dst.length >= dstOffset + byteCount;
+ mByteBuffer.get(dst, dstOffset, byteCount);
+ }
+
+ @Override
+ public byte readByte() {
+ return mByteBuffer.get();
+ }
+
+ @Override
+ public int readInt() {
+ return mByteBuffer.getInt();
+ }
+
+ @Override
+ public void readIntArray(int[] dst, int dstOffset, int intCount) {
+ while (--intCount >= 0) {
+ dst[dstOffset++] = mByteBuffer.getInt();
+ }
+ }
+
+ @Override
+ public short readShort() {
+ return mByteBuffer.getShort();
+ }
+}
diff --git a/tools/layoutlib/bridge/src/libcore/io/MemoryMappedFile_Delegate.java b/tools/layoutlib/bridge/src/libcore/io/MemoryMappedFile_Delegate.java
new file mode 100644
index 0000000..723d5c4
--- /dev/null
+++ b/tools/layoutlib/bridge/src/libcore/io/MemoryMappedFile_Delegate.java
@@ -0,0 +1,118 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package libcore.io;
+
+import com.android.layoutlib.bridge.impl.DelegateManager;
+import com.android.layoutlib.bridge.libcore.io.BridgeBufferIterator;
+import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
+
+import android.system.ErrnoException;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.nio.ByteOrder;
+import java.nio.MappedByteBuffer;
+import java.nio.channels.FileChannel.MapMode;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Delegate used to provide alternate implementation of select methods of {@link MemoryMappedFile}.
+ */
+public class MemoryMappedFile_Delegate {
+
+ private static final DelegateManager<MemoryMappedFile_Delegate> sManager = new
+ DelegateManager<MemoryMappedFile_Delegate>(MemoryMappedFile_Delegate.class);
+
+ private static final Map<MemoryMappedFile, Long> sMemoryMappedFileMap =
+ new HashMap<MemoryMappedFile, Long>();
+
+ private final MappedByteBuffer mMappedByteBuffer;
+ private final long mSize;
+
+ /** Path on the target device where the data file is available. */
+ private static final String TARGET_PATH = System.getenv("ANDROID_ROOT") + "/usr/share/zoneinfo";
+ /** Path on the host (inside the SDK) where the data files are available. */
+ private static File sRootPath;
+
+ @LayoutlibDelegate
+ static MemoryMappedFile mmapRO(String path) throws ErrnoException {
+ if (!path.startsWith(TARGET_PATH)) {
+ throw new ErrnoException("Custom timezone data files are not supported.", 1);
+ }
+ if (sRootPath == null) {
+ throw new ErrnoException("Bridge has not been initialized properly.", 1);
+ }
+ path = path.substring(TARGET_PATH.length());
+ try {
+ File f = new File(sRootPath, path);
+ if (!f.exists()) {
+ throw new ErrnoException("File not found: " + f.getPath(), 1);
+ }
+ RandomAccessFile file = new RandomAccessFile(f, "r");
+ try {
+ long size = file.length();
+ MemoryMappedFile_Delegate newDelegate = new MemoryMappedFile_Delegate(file);
+ long filePointer = file.getFilePointer();
+ MemoryMappedFile mmFile = new MemoryMappedFile(filePointer, size);
+ long delegateIndex = sManager.addNewDelegate(newDelegate);
+ sMemoryMappedFileMap.put(mmFile, delegateIndex);
+ return mmFile;
+ } finally {
+ file.close();
+ }
+ } catch (IOException e) {
+ throw new ErrnoException("mmapRO", 1, e);
+ }
+ }
+
+ @LayoutlibDelegate
+ static void close(MemoryMappedFile thisFile) throws ErrnoException {
+ Long index = sMemoryMappedFileMap.get(thisFile);
+ if (index != null) {
+ sMemoryMappedFileMap.remove(thisFile);
+ sManager.removeJavaReferenceFor(index);
+ }
+ }
+
+ @LayoutlibDelegate
+ static BufferIterator bigEndianIterator(MemoryMappedFile file) {
+ MemoryMappedFile_Delegate delegate = getDelegate(file);
+ return new BridgeBufferIterator(delegate.mSize, delegate.mMappedByteBuffer.duplicate());
+ }
+
+ // TODO: implement littleEndianIterator()
+
+ public MemoryMappedFile_Delegate(RandomAccessFile file) throws IOException {
+ mSize = file.length();
+ // It's weird that map() takes size as long, but returns MappedByteBuffer which uses an int
+ // to store the marker to the position.
+ mMappedByteBuffer = file.getChannel().map(MapMode.READ_ONLY, 0, mSize);
+ assert mMappedByteBuffer.order() == ByteOrder.BIG_ENDIAN;
+ }
+
+ public static void setDataDir(File path) {
+ sRootPath = path;
+ }
+
+ private static MemoryMappedFile_Delegate getDelegate(MemoryMappedFile file) {
+ Long index = sMemoryMappedFileMap.get(file);
+ return index == null ? null : sManager.getDelegate(index);
+ }
+
+}
diff --git a/tools/layoutlib/bridge/src/libcore/util/ZoneInfo_WallTime_Delegate.java b/tools/layoutlib/bridge/src/libcore/util/ZoneInfo_WallTime_Delegate.java
new file mode 100644
index 0000000..f29c5c0
--- /dev/null
+++ b/tools/layoutlib/bridge/src/libcore/util/ZoneInfo_WallTime_Delegate.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package libcore.util;
+
+import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
+
+import java.util.GregorianCalendar;
+
+/**
+ * Delegate used to provide alternate implementation of select methods in {@link ZoneInfo.WallTime}
+ */
+public class ZoneInfo_WallTime_Delegate {
+
+ @LayoutlibDelegate
+ static GregorianCalendar createGregorianCalendar() {
+ return new GregorianCalendar();
+ }
+}
diff --git a/tools/layoutlib/bridge/tests/res/testApp/MyApplication/golden/activity.png b/tools/layoutlib/bridge/tests/res/testApp/MyApplication/golden/activity.png
new file mode 100644
index 0000000..943cdf1
--- /dev/null
+++ b/tools/layoutlib/bridge/tests/res/testApp/MyApplication/golden/activity.png
Binary files differ
diff --git a/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/layout/layout.xml b/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/layout/layout.xml
index 2704c07..b8ec5661 100644
--- a/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/layout/layout.xml
+++ b/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/layout/layout.xml
@@ -8,4 +8,10 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some text"/>
+ <DatePicker
+ android:layout_width="100dp"
+ android:layout_height="100dp"/>
+ <CalendarView
+ android:layout_width="100dp"
+ android:layout_height="100dp"/>
</LinearLayout>
\ No newline at end of file
diff --git a/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/ImageUtils.java b/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/ImageUtils.java
new file mode 100644
index 0000000..e13ad72
--- /dev/null
+++ b/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/ImageUtils.java
@@ -0,0 +1,336 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.layoutlib.bridge.intensive;
+
+import com.android.annotations.NonNull;
+
+import java.awt.AlphaComposite;
+import java.awt.Color;
+import java.awt.Graphics;
+import java.awt.Graphics2D;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.imageio.ImageIO;
+
+import static java.awt.RenderingHints.*;
+import static java.awt.image.BufferedImage.TYPE_INT_ARGB;
+import static java.io.File.separatorChar;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+
+// Adapted by taking the relevant pieces of code from the following classes:
+//
+// com.android.tools.idea.rendering.ImageUtils,
+// com.android.tools.idea.tests.gui.framework.fixture.layout.ImageFixture and
+// com.android.tools.idea.rendering.RenderTestBase
+/**
+ * Utilities related to image processing.
+ */
+public class ImageUtils {
+ /**
+ * Normally, this test will fail when there is a missing thumbnail. However, when
+ * you create creating a new test, it's useful to be able to turn this off such that
+ * you can generate all the missing thumbnails in one go, rather than having to run
+ * the test repeatedly to get to each new render assertion generating its thumbnail.
+ */
+ private static final boolean FAIL_ON_MISSING_THUMBNAIL = true;
+
+ private static final int THUMBNAIL_SIZE = 250;
+
+ private static final double MAX_PERCENT_DIFFERENCE = 0.1;
+
+ public static void requireSimilar(@NonNull String relativePath, @NonNull BufferedImage image)
+ throws IOException {
+ int maxDimension = Math.max(image.getWidth(), image.getHeight());
+ double scale = THUMBNAIL_SIZE / (double)maxDimension;
+ BufferedImage thumbnail = scale(image, scale, scale);
+
+ InputStream is = ImageUtils.class.getResourceAsStream(relativePath);
+ if (is == null) {
+ String message = "Unable to load golden thumbnail: " + relativePath + "\n";
+ message = saveImageAndAppendMessage(thumbnail, message, relativePath);
+ if (FAIL_ON_MISSING_THUMBNAIL) {
+ fail(message);
+ } else {
+ System.out.println(message);
+ }
+ }
+ else {
+ BufferedImage goldenImage = ImageIO.read(is);
+ assertImageSimilar(relativePath, goldenImage, thumbnail, MAX_PERCENT_DIFFERENCE);
+ }
+ }
+
+ public static void assertImageSimilar(String relativePath, BufferedImage goldenImage,
+ BufferedImage image, double maxPercentDifferent) throws IOException {
+ assertEquals("Only TYPE_INT_ARGB image types are supported", TYPE_INT_ARGB, image.getType());
+
+ if (goldenImage.getType() != TYPE_INT_ARGB) {
+ BufferedImage temp = new BufferedImage(goldenImage.getWidth(), goldenImage.getHeight(),
+ TYPE_INT_ARGB);
+ temp.getGraphics().drawImage(goldenImage, 0, 0, null);
+ goldenImage = temp;
+ }
+ assertEquals(TYPE_INT_ARGB, goldenImage.getType());
+
+ int imageWidth = Math.min(goldenImage.getWidth(), image.getWidth());
+ int imageHeight = Math.min(goldenImage.getHeight(), image.getHeight());
+
+ // Blur the images to account for the scenarios where there are pixel
+ // differences
+ // in where a sharp edge occurs
+ // goldenImage = blur(goldenImage, 6);
+ // image = blur(image, 6);
+
+ int width = 3 * imageWidth;
+ @SuppressWarnings("UnnecessaryLocalVariable")
+ int height = imageHeight; // makes code more readable
+ BufferedImage deltaImage = new BufferedImage(width, height, TYPE_INT_ARGB);
+ Graphics g = deltaImage.getGraphics();
+
+ // Compute delta map
+ long delta = 0;
+ for (int y = 0; y < imageHeight; y++) {
+ for (int x = 0; x < imageWidth; x++) {
+ int goldenRgb = goldenImage.getRGB(x, y);
+ int rgb = image.getRGB(x, y);
+ if (goldenRgb == rgb) {
+ deltaImage.setRGB(imageWidth + x, y, 0x00808080);
+ continue;
+ }
+
+ // If the pixels have no opacity, don't delta colors at all
+ if (((goldenRgb & 0xFF000000) == 0) && (rgb & 0xFF000000) == 0) {
+ deltaImage.setRGB(imageWidth + x, y, 0x00808080);
+ continue;
+ }
+
+ int deltaR = ((rgb & 0xFF0000) >>> 16) - ((goldenRgb & 0xFF0000) >>> 16);
+ int newR = 128 + deltaR & 0xFF;
+ int deltaG = ((rgb & 0x00FF00) >>> 8) - ((goldenRgb & 0x00FF00) >>> 8);
+ int newG = 128 + deltaG & 0xFF;
+ int deltaB = (rgb & 0x0000FF) - (goldenRgb & 0x0000FF);
+ int newB = 128 + deltaB & 0xFF;
+
+ int avgAlpha = ((((goldenRgb & 0xFF000000) >>> 24)
+ + ((rgb & 0xFF000000) >>> 24)) / 2) << 24;
+
+ int newRGB = avgAlpha | newR << 16 | newG << 8 | newB;
+ deltaImage.setRGB(imageWidth + x, y, newRGB);
+
+ delta += Math.abs(deltaR);
+ delta += Math.abs(deltaG);
+ delta += Math.abs(deltaB);
+ }
+ }
+
+ // 3 different colors, 256 color levels
+ long total = imageHeight * imageWidth * 3L * 256L;
+ float percentDifference = (float) (delta * 100 / (double) total);
+
+ String error = null;
+ String imageName = getName(relativePath);
+ if (percentDifference > maxPercentDifferent) {
+ error = String.format("Images differ (by %.1f%%)", percentDifference);
+ } else if (Math.abs(goldenImage.getWidth() - image.getWidth()) >= 2) {
+ error = "Widths differ too much for " + imageName + ": " +
+ goldenImage.getWidth() + "x" + goldenImage.getHeight() +
+ "vs" + image.getWidth() + "x" + image.getHeight();
+ } else if (Math.abs(goldenImage.getHeight() - image.getHeight()) >= 2) {
+ error = "Heights differ too much for " + imageName + ": " +
+ goldenImage.getWidth() + "x" + goldenImage.getHeight() +
+ "vs" + image.getWidth() + "x" + image.getHeight();
+ }
+
+ assertEquals(TYPE_INT_ARGB, image.getType());
+ if (error != null) {
+ // Expected on the left
+ // Golden on the right
+ g.drawImage(goldenImage, 0, 0, null);
+ g.drawImage(image, 2 * imageWidth, 0, null);
+
+ // Labels
+ if (imageWidth > 80) {
+ g.setColor(Color.RED);
+ g.drawString("Expected", 10, 20);
+ g.drawString("Actual", 2 * imageWidth + 10, 20);
+ }
+
+ File output = new File(getTempDir(), "delta-" + imageName);
+ if (output.exists()) {
+ boolean deleted = output.delete();
+ assertTrue(deleted);
+ }
+ ImageIO.write(deltaImage, "PNG", output);
+ error += " - see details in " + output.getPath() + "\n";
+ error = saveImageAndAppendMessage(image, error, relativePath);
+ System.out.println(error);
+ fail(error);
+ }
+
+ g.dispose();
+ }
+
+ /**
+ * Resize the given image
+ *
+ * @param source the image to be scaled
+ * @param xScale x scale
+ * @param yScale y scale
+ * @return the scaled image
+ */
+ @NonNull
+ public static BufferedImage scale(@NonNull BufferedImage source, double xScale, double yScale) {
+
+ int sourceWidth = source.getWidth();
+ int sourceHeight = source.getHeight();
+ int destWidth = Math.max(1, (int) (xScale * sourceWidth));
+ int destHeight = Math.max(1, (int) (yScale * sourceHeight));
+ int imageType = source.getType();
+ if (imageType == BufferedImage.TYPE_CUSTOM) {
+ imageType = BufferedImage.TYPE_INT_ARGB;
+ }
+ if (xScale > 0.5 && yScale > 0.5) {
+ BufferedImage scaled =
+ new BufferedImage(destWidth, destHeight, imageType);
+ Graphics2D g2 = scaled.createGraphics();
+ g2.setComposite(AlphaComposite.Src);
+ g2.setColor(new Color(0, true));
+ g2.fillRect(0, 0, destWidth, destHeight);
+ if (xScale == 1 && yScale == 1) {
+ g2.drawImage(source, 0, 0, null);
+ } else {
+ setRenderingHints(g2);
+ g2.drawImage(source, 0, 0, destWidth, destHeight, 0, 0, sourceWidth, sourceHeight,
+ null);
+ }
+ g2.dispose();
+ return scaled;
+ } else {
+ // When creating a thumbnail, using the above code doesn't work very well;
+ // you get some visible artifacts, especially for text. Instead use the
+ // technique of repeatedly scaling the image into half; this will cause
+ // proper averaging of neighboring pixels, and will typically (for the kinds
+ // of screen sizes used by this utility method in the layout editor) take
+ // about 3-4 iterations to get the result since we are logarithmically reducing
+ // the size. Besides, each successive pass in operating on much fewer pixels
+ // (a reduction of 4 in each pass).
+ //
+ // However, we may not be resizing to a size that can be reached exactly by
+ // successively diving in half. Therefore, once we're within a factor of 2 of
+ // the final size, we can do a resize to the exact target size.
+ // However, we can get even better results if we perform this final resize
+ // up front. Let's say we're going from width 1000 to a destination width of 85.
+ // The first approach would cause a resize from 1000 to 500 to 250 to 125, and
+ // then a resize from 125 to 85. That last resize can distort/blur a lot.
+ // Instead, we can start with the destination width, 85, and double it
+ // successfully until we're close to the initial size: 85, then 170,
+ // then 340, and finally 680. (The next one, 1360, is larger than 1000).
+ // So, now we *start* the thumbnail operation by resizing from width 1000 to
+ // width 680, which will preserve a lot of visual details such as text.
+ // Then we can successively resize the image in half, 680 to 340 to 170 to 85.
+ // We end up with the expected final size, but we've been doing an exact
+ // divide-in-half resizing operation at the end so there is less distortion.
+
+ int iterations = 0; // Number of halving operations to perform after the initial resize
+ int nearestWidth = destWidth; // Width closest to source width that = 2^x, x is integer
+ int nearestHeight = destHeight;
+ while (nearestWidth < sourceWidth / 2) {
+ nearestWidth *= 2;
+ nearestHeight *= 2;
+ iterations++;
+ }
+
+ BufferedImage scaled = new BufferedImage(nearestWidth, nearestHeight, imageType);
+
+ Graphics2D g2 = scaled.createGraphics();
+ setRenderingHints(g2);
+ g2.drawImage(source, 0, 0, nearestWidth, nearestHeight, 0, 0, sourceWidth, sourceHeight,
+ null);
+ g2.dispose();
+
+ sourceWidth = nearestWidth;
+ sourceHeight = nearestHeight;
+ source = scaled;
+
+ for (int iteration = iterations - 1; iteration >= 0; iteration--) {
+ int halfWidth = sourceWidth / 2;
+ int halfHeight = sourceHeight / 2;
+ scaled = new BufferedImage(halfWidth, halfHeight, imageType);
+ g2 = scaled.createGraphics();
+ setRenderingHints(g2);
+ g2.drawImage(source, 0, 0, halfWidth, halfHeight, 0, 0, sourceWidth, sourceHeight,
+ null);
+ g2.dispose();
+
+ sourceWidth = halfWidth;
+ sourceHeight = halfHeight;
+ source = scaled;
+ iterations--;
+ }
+ return scaled;
+ }
+ }
+
+ private static void setRenderingHints(@NonNull Graphics2D g2) {
+ g2.setRenderingHint(KEY_INTERPOLATION,VALUE_INTERPOLATION_BILINEAR);
+ g2.setRenderingHint(KEY_RENDERING, VALUE_RENDER_QUALITY);
+ g2.setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON);
+ }
+
+ /**
+ * Temp directory where to write the thumbnails and deltas.
+ */
+ @NonNull
+ private static File getTempDir() {
+ if (System.getProperty("os.name").equals("Mac OS X")) {
+ return new File("/tmp"); //$NON-NLS-1$
+ }
+
+ return new File(System.getProperty("java.io.tmpdir")); //$NON-NLS-1$
+ }
+
+ /**
+ * Saves the generated thumbnail image and appends the info message to an initial message
+ */
+ @NonNull
+ private static String saveImageAndAppendMessage(@NonNull BufferedImage image,
+ @NonNull String initialMessage, @NonNull String relativePath) throws IOException {
+ File output = new File(getTempDir(), getName(relativePath));
+ if (output.exists()) {
+ boolean deleted = output.delete();
+ assertTrue(deleted);
+ }
+ ImageIO.write(image, "PNG", output);
+ initialMessage += "Thumbnail for current rendering stored at " + output.getPath();
+// initialMessage += "\nRun the following command to accept the changes:\n";
+// initialMessage += String.format("mv %1$s %2$s", output.getPath(),
+// ImageUtils.class.getResource(relativePath).getPath());
+ // The above has been commented out, since the destination path returned is in out dir
+ // and it makes the tests pass without the code being actually checked in.
+ return initialMessage;
+ }
+
+ private static String getName(@NonNull String relativePath) {
+ return relativePath.substring(relativePath.lastIndexOf(separatorChar) + 1);
+ }
+}
diff --git a/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/Main.java b/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/Main.java
index a2588a6..96725af 100644
--- a/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/Main.java
+++ b/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/Main.java
@@ -39,6 +39,7 @@
import java.io.File;
import java.io.FileFilter;
+import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.Comparator;
@@ -75,7 +76,10 @@
private static final String PLATFORM_DIR;
private static final String TEST_RES_DIR;
- private static final String APP_TEST_RES = "/testApp/MyApplication/src/main/res";
+ /** Location of the app to test inside {@link #TEST_RES_DIR}*/
+ private static final String APP_TEST_DIR = "/testApp/MyApplication";
+ /** Location of the app's res dir inside {@link #TEST_RES_DIR}*/
+ private static final String APP_TEST_RES = APP_TEST_DIR + "/src/main/res";
private LayoutLog mLayoutLibLog;
private FrameworkResources mFrameworkRepo;
@@ -280,6 +284,12 @@
getLogger().error(session.getResult().getException(),
session.getResult().getErrorMessage());
}
+ try {
+ String goldenImagePath = APP_TEST_DIR + "/golden/activity.png";
+ ImageUtils.requireSimilar(goldenImagePath, session.getImage());
+ } catch (IOException e) {
+ getLogger().error(e, e.getMessage());
+ }
}
/**
diff --git a/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java b/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java
index 4e6f456..f9e6151 100644
--- a/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java
+++ b/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java
@@ -21,6 +21,7 @@
import com.android.tools.layoutlib.java.Charsets;
import com.android.tools.layoutlib.java.IntegralToString;
import com.android.tools.layoutlib.java.Objects;
+import com.android.tools.layoutlib.java.System_Delegate;
import com.android.tools.layoutlib.java.UnsafeByteSequence;
import java.util.Arrays;
@@ -131,6 +132,7 @@
IntegralToString.class,
UnsafeByteSequence.class,
Charsets.class,
+ System_Delegate.class,
};
/**
@@ -150,6 +152,7 @@
"android.graphics.Typeface#getSystemFontConfigLocation",
"android.os.Handler#sendMessageAtTime",
"android.os.HandlerThread#run",
+ "android.preference.Preference#getView",
"android.text.format.DateFormat#is24HourFormat",
"android.util.Xml#newPullParser",
"android.view.Choreographer#getRefreshRate",
@@ -167,10 +170,15 @@
"android.view.RenderNode#nSetElevation",
"android.view.RenderNode#nGetElevation",
"android.view.ViewGroup#drawChild",
+ "android.widget.TimePickerSpinnerDelegate#getAmOrPmKeyCode",
"com.android.internal.view.menu.MenuBuilder#createNewMenuItem",
"com.android.internal.util.XmlUtils#convertValueToInt",
"com.android.internal.textservice.ITextServicesManager$Stub#asInterface",
- "dalvik.system.VMRuntime#newUnpaddedArray"
+ "dalvik.system.VMRuntime#newUnpaddedArray",
+ "libcore.io.MemoryMappedFile#mmapRO",
+ "libcore.io.MemoryMappedFile#close",
+ "libcore.io.MemoryMappedFile#bigEndianIterator",
+ "libcore.util.ZoneInfo$WallTime#createGregorianCalendar",
};
/**
@@ -220,7 +228,6 @@
"android.os.SystemProperties",
"android.text.AndroidBidi",
"android.text.StaticLayout",
- "android.text.format.Time",
"android.util.FloatMath",
"android.view.Display",
"libcore.icu.DateIntervalFormat",
@@ -261,10 +268,12 @@
"java.nio.charset.Charsets", "com.android.tools.layoutlib.java.Charsets",
"java.lang.IntegralToString", "com.android.tools.layoutlib.java.IntegralToString",
"java.lang.UnsafeByteSequence", "com.android.tools.layoutlib.java.UnsafeByteSequence",
+ "java.nio.charset.StandardCharsets", "com.android.tools.layoutlib.java.Charsets",
};
private final static String[] EXCLUDED_CLASSES =
new String[] {
+ "android.preference.PreferenceActivity",
"org.kxml2.io.KXmlParser"
};
diff --git a/tools/layoutlib/create/src/com/android/tools/layoutlib/create/Main.java b/tools/layoutlib/create/src/com/android/tools/layoutlib/create/Main.java
index cd3c39e..fa570c8 100644
--- a/tools/layoutlib/create/src/com/android/tools/layoutlib/create/Main.java
+++ b/tools/layoutlib/create/src/com/android/tools/layoutlib/create/Main.java
@@ -108,6 +108,7 @@
"android.graphics.drawable.*",
"android.content.*",
"android.content.res.*",
+ "android.preference.*",
"org.apache.harmony.xml.*",
"com.android.internal.R**",
"android.pim.*", // for datepicker
diff --git a/tools/layoutlib/create/src/com/android/tools/layoutlib/create/ReplaceMethodCallsAdapter.java b/tools/layoutlib/create/src/com/android/tools/layoutlib/create/ReplaceMethodCallsAdapter.java
index 9c6fbac..1e2623f 100644
--- a/tools/layoutlib/create/src/com/android/tools/layoutlib/create/ReplaceMethodCallsAdapter.java
+++ b/tools/layoutlib/create/src/com/android/tools/layoutlib/create/ReplaceMethodCallsAdapter.java
@@ -16,6 +16,8 @@
package com.android.tools.layoutlib.create;
+import com.android.tools.layoutlib.java.System_Delegate;
+
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
@@ -47,24 +49,25 @@
private static final String ANDROID_LOCALE_CLASS =
"com/android/layoutlib/bridge/android/AndroidLocale";
- private static final String JAVA_LOCALE_CLASS = "java/util/Locale";
+ private static final String JAVA_LOCALE_CLASS = Type.getInternalName(java.util.Locale.class);
private static final Type STRING = Type.getType(String.class);
+ private static final String JAVA_LANG_SYSTEM = Type.getInternalName(System.class);
+
// Static initialization block to initialize METHOD_REPLACERS.
static {
// Case 1: java.lang.System.arraycopy()
METHOD_REPLACERS.add(new MethodReplacer() {
@Override
public boolean isNeeded(String owner, String name, String desc) {
- return "java/lang/System".equals(owner) && "arraycopy".equals(name) &&
+ return JAVA_LANG_SYSTEM.equals(owner) && "arraycopy".equals(name) &&
ARRAYCOPY_DESCRIPTORS.contains(desc);
}
@Override
- public void replace(int[] opcode, String[] methodInformation) {
- assert methodInformation.length == 3 && isNeeded(methodInformation[0], methodInformation[1], methodInformation[2])
- && opcode.length == 1;
- methodInformation[2] = "(Ljava/lang/Object;ILjava/lang/Object;II)V";
+ public void replace(MethodInformation mi) {
+ assert isNeeded(mi.owner, mi.name, mi.desc);
+ mi.desc = "(Ljava/lang/Object;ILjava/lang/Object;II)V";
}
});
@@ -80,12 +83,11 @@
}
@Override
- public void replace(int[] opcode, String[] methodInformation) {
- assert methodInformation.length == 3 && isNeeded(methodInformation[0], methodInformation[1], methodInformation[2])
- && opcode.length == 1;
- opcode[0] = Opcodes.INVOKESTATIC;
- methodInformation[0] = ANDROID_LOCALE_CLASS;
- methodInformation[2] = LOCALE_TO_STRING;
+ public void replace(MethodInformation mi) {
+ assert isNeeded(mi.owner, mi.name, mi.desc);
+ mi.opcode = Opcodes.INVOKESTATIC;
+ mi.owner = ANDROID_LOCALE_CLASS;
+ mi.desc = LOCALE_TO_STRING;
}
});
@@ -104,10 +106,27 @@
}
@Override
- public void replace(int[] opcode, String[] methodInformation) {
- assert methodInformation.length == 3 && isNeeded(methodInformation[0], methodInformation[1], methodInformation[2])
- && opcode.length == 1;
- methodInformation[0] = ANDROID_LOCALE_CLASS;
+ public void replace(MethodInformation mi) {
+ assert isNeeded(mi.owner, mi.name, mi.desc);
+ mi.owner = ANDROID_LOCALE_CLASS;
+ }
+ });
+
+ // Case 4: java.lang.System.log?()
+ METHOD_REPLACERS.add(new MethodReplacer() {
+ @Override
+ public boolean isNeeded(String owner, String name, String desc) {
+ return JAVA_LANG_SYSTEM.equals(owner) && name.length() == 4
+ && name.startsWith("log");
+ }
+
+ @Override
+ public void replace(MethodInformation mi) {
+ assert isNeeded(mi.owner, mi.name, mi.desc);
+ assert mi.desc.equals("(Ljava/lang/String;Ljava/lang/Throwable;)V")
+ || mi.desc.equals("(Ljava/lang/String;)V");
+ mi.name = "log";
+ mi.owner = Type.getInternalName(System_Delegate.class);
}
});
}
@@ -141,13 +160,12 @@
public void visitMethodInsn(int opcode, String owner, String name, String desc) {
for (MethodReplacer replacer : METHOD_REPLACERS) {
if (replacer.isNeeded(owner, name, desc)) {
- String[] methodInformation = {owner, name, desc};
- int[] opcodeOut = {opcode};
- replacer.replace(opcodeOut, methodInformation);
- opcode = opcodeOut[0];
- owner = methodInformation[0];
- name = methodInformation[1];
- desc = methodInformation[2];
+ MethodInformation mi = new MethodInformation(opcode, owner, name, desc);
+ replacer.replace(mi);
+ opcode = mi.opcode;
+ owner = mi.owner;
+ name = mi.name;
+ desc = mi.desc;
break;
}
}
@@ -155,19 +173,28 @@
}
}
+ private static class MethodInformation {
+ public int opcode;
+ public String owner;
+ public String name;
+ public String desc;
+
+ public MethodInformation(int opcode, String owner, String name, String desc) {
+ this.opcode = opcode;
+ this.owner = owner;
+ this.name = name;
+ this.desc = desc;
+ }
+ }
+
private interface MethodReplacer {
public boolean isNeeded(String owner, String name, String desc);
/**
- * This method must update the arrays with the new values of the method attributes -
+ * Updates the MethodInformation with the new values of the method attributes -
* opcode, owner, name and desc.
- * @param opcode This array should contain the original value of the opcode. The value is
- * modified by the method if needed. The size of the array must be 1.
*
- * @param methodInformation This array should contain the original values of the method
- * attributes - owner, name and desc in that order. The values
- * may be modified as needed. The size of the array must be 3.
*/
- public void replace(int[] opcode, String[] methodInformation);
+ public void replace(MethodInformation mi);
}
}
diff --git a/tools/layoutlib/create/src/com/android/tools/layoutlib/java/System_Delegate.java b/tools/layoutlib/create/src/com/android/tools/layoutlib/java/System_Delegate.java
new file mode 100644
index 0000000..613c8d9
--- /dev/null
+++ b/tools/layoutlib/create/src/com/android/tools/layoutlib/java/System_Delegate.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.tools.layoutlib.java;
+
+import com.android.tools.layoutlib.create.ReplaceMethodCallsAdapter;
+
+/**
+ * Provides dummy implementation of methods that don't exist on the host VM.
+ *
+ * @see ReplaceMethodCallsAdapter
+ */
+public class System_Delegate {
+ public static void log(String message) {
+ // ignore.
+ }
+
+ public static void log(String message, Throwable th) {
+ // ignore.
+ }
+}