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