summaryrefslogtreecommitdiffstats
path: root/tools/layoutlib
diff options
context:
space:
mode:
authorDeepanshu Gupta <deepanshu@google.com>2013-10-14 18:14:58 -0700
committerDeepanshu Gupta <deepanshu@google.com>2013-10-16 15:09:52 -0700
commit083e3caf66c21f7cc9511db479726c38d90e2d2f (patch)
tree3d7d94605482ef534aab18ff149c18b5e8eb9f7b /tools/layoutlib
parent81f74f4bc96ea4266cebe1b785d095558f540800 (diff)
downloadframeworks_base-083e3caf66c21f7cc9511db479726c38d90e2d2f.zip
frameworks_base-083e3caf66c21f7cc9511db479726c38d90e2d2f.tar.gz
frameworks_base-083e3caf66c21f7cc9511db479726c38d90e2d2f.tar.bz2
Fix CalendarView to show the right month and year label.
Change-Id: I95431f1054678d4192bd1621c1f69b29268f55e9
Diffstat (limited to 'tools/layoutlib')
-rw-r--r--tools/layoutlib/bridge/src/android/text/format/Time_Delegate.java61
-rw-r--r--tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java1
2 files changed, 62 insertions, 0 deletions
diff --git a/tools/layoutlib/bridge/src/android/text/format/Time_Delegate.java b/tools/layoutlib/bridge/src/android/text/format/Time_Delegate.java
new file mode 100644
index 0000000..15cd687
--- /dev/null
+++ b/tools/layoutlib/bridge/src/android/text/format/Time_Delegate.java
@@ -0,0 +1,61 @@
+/*
+ * 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.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("(?<!%)(%%)*%(?!%)");
+
+ @LayoutlibDelegate
+ /*package*/ static String format1(Time thisTime, String format) {
+
+ try {
+ // Change the format by adding changing '%' to "%1$t". This is required to tell the
+ // formatter which argument to use from the argument list. '%%' is left as is. In the
+ // replacement string, $0 refers to matched pattern. \\1 means '1', written this way to
+ // separate it from 0. \\$ means '$', written this way to suppress the special meaning
+ // of $.
+ return String.format(
+ p.matcher(format).replaceAll("$0\\1\\$t"),
+ timeToCalendar(thisTime, Calendar.getInstance()));
+ } catch (UnknownFormatConversionException e) {
+ Bridge.getLog().fidelityWarning(LayoutLog.TAG_STRFTIME, "Unrecognized format", e, format);
+ return format;
+ }
+ }
+
+ private static Calendar timeToCalendar(Time time, Calendar calendar) {
+ calendar.set(time.year, time.month, time.monthDay, time.hour, time.minute, time.second);
+ return calendar;
+ }
+
+}
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 4cada5d..7099a4b 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
@@ -129,6 +129,7 @@ public final class CreateInfo implements ICreateInfo {
"android.os.HandlerThread#run",
"android.os.Build#getString",
"android.text.format.DateFormat#is24HourFormat",
+ "android.text.format.Time#format1",
"android.view.Choreographer#getRefreshRate",
"android.view.Display#updateDisplayInfoLocked",
"android.view.LayoutInflater#rInflate",