DateIntervalFormat_Delegate.java revision 0d7ebc51ffb63f886af2196f05910925126726b9
1/* 2 * Copyright (C) 2013 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 17package libcore.icu; 18 19import java.text.FieldPosition; 20 21import com.android.ide.common.rendering.api.LayoutLog; 22import com.android.layoutlib.bridge.Bridge; 23import com.android.layoutlib.bridge.impl.DelegateManager; 24import com.android.tools.layoutlib.annotations.LayoutlibDelegate; 25import com.ibm.icu.text.DateIntervalFormat; 26import com.ibm.icu.util.DateInterval; 27import com.ibm.icu.util.TimeZone; 28import com.ibm.icu.util.ULocale; 29 30public class DateIntervalFormat_Delegate { 31 32 // ---- delegate manager ---- 33 private static final DelegateManager<DateIntervalFormat_Delegate> sManager = 34 new DelegateManager<DateIntervalFormat_Delegate>(DateIntervalFormat_Delegate.class); 35 36 // ---- delegate data ---- 37 private DateIntervalFormat mFormat; 38 39 40 // ---- native methods ---- 41 42 @LayoutlibDelegate 43 /*package*/static String formatDateInterval(long address, long fromDate, long toDate) { 44 DateIntervalFormat_Delegate delegate = sManager.getDelegate((int)address); 45 if (delegate == null) { 46 Bridge.getLog().error(LayoutLog.TAG_BROKEN, 47 "Unable for find native DateIntervalFormat", null); 48 return null; 49 } 50 DateInterval interval = new DateInterval(fromDate, toDate); 51 StringBuffer sb = new StringBuffer(); 52 FieldPosition pos = new FieldPosition(0); 53 delegate.mFormat.format(interval, sb, pos); 54 return sb.toString(); 55 } 56 57 @LayoutlibDelegate 58 /*package*/ static long createDateIntervalFormat(String skeleton, String localeName, 59 String tzName) { 60 TimeZone prevDefaultTz = TimeZone.getDefault(); 61 TimeZone.setDefault(TimeZone.getTimeZone(tzName)); 62 DateIntervalFormat_Delegate newDelegate = new DateIntervalFormat_Delegate(); 63 newDelegate.mFormat = 64 DateIntervalFormat.getInstance(skeleton, new ULocale(localeName)); 65 TimeZone.setDefault(prevDefaultTz); 66 return sManager.addNewDelegate(newDelegate); 67 } 68 69 @LayoutlibDelegate 70 /*package*/ static void destroyDateIntervalFormat(long address) { 71 sManager.removeJavaReferenceFor((int)address); 72 } 73 74} 75