diff options
127 files changed, 11137 insertions, 6065 deletions
diff --git a/api/current.xml b/api/current.xml index 99949cf..203ccaa 100644 --- a/api/current.xml +++ b/api/current.xml @@ -1780,6 +1780,17 @@ visibility="public" > </field> +<field name="adapter" + type="int" + transient="false" + volatile="false" + value="16843454" + static="true" + final="true" + deprecated="not deprecated" + visibility="public" +> +</field> <field name="addStatesFromChildren" type="int" transient="false" @@ -1989,6 +2000,17 @@ visibility="public" > </field> +<field name="as" + type="int" + transient="false" + volatile="false" + value="16843460" + static="true" + final="true" + deprecated="not deprecated" + visibility="public" +> +</field> <field name="author" type="int" transient="false" @@ -2649,6 +2671,17 @@ visibility="public" > </field> +<field name="column" + type="int" + transient="false" + volatile="false" + value="16843463" + static="true" + final="true" + deprecated="not deprecated" + visibility="public" +> +</field> <field name="columnDelay" type="int" transient="false" @@ -3815,6 +3848,17 @@ visibility="public" > </field> +<field name="from" + type="int" + transient="false" + volatile="false" + value="16843458" + static="true" + final="true" + deprecated="not deprecated" + visibility="public" +> +</field> <field name="fromAlpha" type="int" transient="false" @@ -3837,6 +3881,17 @@ visibility="public" > </field> +<field name="fromValue" + type="int" + transient="false" + volatile="false" + value="16843461" + static="true" + final="true" + deprecated="not deprecated" + visibility="public" +> +</field> <field name="fromXDelta" type="int" transient="false" @@ -7225,6 +7280,17 @@ visibility="public" > </field> +<field name="selection" + type="int" + transient="false" + volatile="false" + value="16843455" + static="true" + final="true" + deprecated="not deprecated" + visibility="public" +> +</field> <field name="settingsActivity" type="int" transient="false" @@ -7412,6 +7478,17 @@ visibility="public" > </field> +<field name="sortOrder" + type="int" + transient="false" + volatile="false" + value="16843456" + static="true" + final="true" + deprecated="not deprecated" + visibility="public" +> +</field> <field name="soundEffectsEnabled" type="int" transient="false" @@ -8600,6 +8677,17 @@ visibility="public" > </field> +<field name="to" + type="int" + transient="false" + volatile="false" + value="16843459" + static="true" + final="true" + deprecated="not deprecated" + visibility="public" +> +</field> <field name="toAlpha" type="int" transient="false" @@ -8622,6 +8710,17 @@ visibility="public" > </field> +<field name="toValue" + type="int" + transient="false" + volatile="false" + value="16843462" + static="true" + final="true" + deprecated="not deprecated" + visibility="public" +> +</field> <field name="toXDelta" type="int" transient="false" @@ -8798,6 +8897,17 @@ visibility="public" > </field> +<field name="uri" + type="int" + transient="false" + volatile="false" + value="16843457" + static="true" + final="true" + deprecated="not deprecated" + visibility="public" +> +</field> <field name="useLevel" type="int" transient="false" @@ -9348,6 +9458,28 @@ visibility="public" > </field> +<field name="withClass" + type="int" + transient="false" + volatile="false" + value="16843465" + static="true" + final="true" + deprecated="not deprecated" + visibility="public" +> +</field> +<field name="withExpression" + type="int" + transient="false" + volatile="false" + value="16843464" + static="true" + final="true" + deprecated="not deprecated" + visibility="public" +> +</field> <field name="writePermission" type="int" transient="false" @@ -34622,6 +34754,17 @@ visibility="public" > </field> +<field name="STORAGE_SERVICE" + type="java.lang.String" + transient="false" + volatile="false" + value=""storage"" + static="true" + final="true" + deprecated="not deprecated" + visibility="public" +> +</field> <field name="TELEPHONY_SERVICE" type="java.lang.String" transient="false" @@ -120568,6 +120711,240 @@ </method> </class> </package> +<package name="android.os.storage" +> +<class name="StorageEventListener" + extends="java.lang.Object" + abstract="true" + static="false" + final="false" + deprecated="not deprecated" + visibility="public" +> +<constructor name="StorageEventListener" + type="android.os.storage.StorageEventListener" + static="false" + final="false" + deprecated="not deprecated" + visibility="public" +> +</constructor> +<method name="onStorageStateChanged" + return="void" + abstract="false" + native="false" + synchronized="false" + static="false" + final="false" + deprecated="not deprecated" + visibility="public" +> +<parameter name="path" type="java.lang.String"> +</parameter> +<parameter name="oldState" type="java.lang.String"> +</parameter> +<parameter name="newState" type="java.lang.String"> +</parameter> +</method> +<method name="onUsbMassStorageConnectionChanged" + return="void" + abstract="false" + native="false" + synchronized="false" + static="false" + final="false" + deprecated="not deprecated" + visibility="public" +> +<parameter name="connected" type="boolean"> +</parameter> +</method> +</class> +<class name="StorageManager" + extends="java.lang.Object" + abstract="false" + static="false" + final="false" + deprecated="not deprecated" + visibility="public" +> +<method name="disableUsbMassStorage" + return="void" + abstract="false" + native="false" + synchronized="false" + static="false" + final="false" + deprecated="not deprecated" + visibility="public" +> +</method> +<method name="enableUsbMassStorage" + return="void" + abstract="false" + native="false" + synchronized="false" + static="false" + final="false" + deprecated="not deprecated" + visibility="public" +> +</method> +<method name="isUsbMassStorageConnected" + return="boolean" + abstract="false" + native="false" + synchronized="false" + static="false" + final="false" + deprecated="not deprecated" + visibility="public" +> +</method> +<method name="isUsbMassStorageEnabled" + return="boolean" + abstract="false" + native="false" + synchronized="false" + static="false" + final="false" + deprecated="not deprecated" + visibility="public" +> +</method> +<method name="registerListener" + return="void" + abstract="false" + native="false" + synchronized="false" + static="false" + final="false" + deprecated="not deprecated" + visibility="public" +> +<parameter name="listener" type="android.os.storage.StorageEventListener"> +</parameter> +</method> +<method name="unregisterListener" + return="void" + abstract="false" + native="false" + synchronized="false" + static="false" + final="false" + deprecated="not deprecated" + visibility="public" +> +<parameter name="listener" type="android.os.storage.StorageEventListener"> +</parameter> +</method> +</class> +<class name="StorageResultCode" + extends="java.lang.Object" + abstract="false" + static="false" + final="false" + deprecated="not deprecated" + visibility="public" +> +<constructor name="StorageResultCode" + type="android.os.storage.StorageResultCode" + static="false" + final="false" + deprecated="not deprecated" + visibility="public" +> +</constructor> +<field name="OperationFailedInternalError" + type="int" + transient="false" + volatile="false" + value="-1" + static="true" + final="true" + deprecated="not deprecated" + visibility="public" +> +</field> +<field name="OperationFailedMediaBlank" + type="int" + transient="false" + volatile="false" + value="-3" + static="true" + final="true" + deprecated="not deprecated" + visibility="public" +> +</field> +<field name="OperationFailedMediaCorrupt" + type="int" + transient="false" + volatile="false" + value="-4" + static="true" + final="true" + deprecated="not deprecated" + visibility="public" +> +</field> +<field name="OperationFailedNoMedia" + type="int" + transient="false" + volatile="false" + value="-2" + static="true" + final="true" + deprecated="not deprecated" + visibility="public" +> +</field> +<field name="OperationFailedStorageBusy" + type="int" + transient="false" + volatile="false" + value="-7" + static="true" + final="true" + deprecated="not deprecated" + visibility="public" +> +</field> +<field name="OperationFailedStorageMounted" + type="int" + transient="false" + volatile="false" + value="-6" + static="true" + final="true" + deprecated="not deprecated" + visibility="public" +> +</field> +<field name="OperationFailedStorageNotMounted" + type="int" + transient="false" + volatile="false" + value="-5" + static="true" + final="true" + deprecated="not deprecated" + visibility="public" +> +</field> +<field name="OperationSucceeded" + type="int" + transient="false" + volatile="false" + value="0" + static="true" + final="true" + deprecated="not deprecated" + visibility="public" +> +</field> +</class> +</package> <package name="android.preference" > <class name="CheckBoxPreference" @@ -197569,6 +197946,195 @@ </parameter> </method> </interface> +<class name="Adapters" + extends="java.lang.Object" + abstract="false" + static="false" + final="false" + deprecated="not deprecated" + visibility="public" +> +<constructor name="Adapters" + type="android.widget.Adapters" + static="false" + final="false" + deprecated="not deprecated" + visibility="public" +> +</constructor> +<method name="loadAdapter" + return="android.widget.BaseAdapter" + abstract="false" + native="false" + synchronized="false" + static="true" + final="false" + deprecated="not deprecated" + visibility="public" +> +<parameter name="context" type="android.content.Context"> +</parameter> +<parameter name="id" type="int"> +</parameter> +<parameter name="parameters" type="java.lang.Object..."> +</parameter> +</method> +<method name="loadCursorAdapter" + return="android.widget.CursorAdapter" + abstract="false" + native="false" + synchronized="false" + static="true" + final="false" + deprecated="not deprecated" + visibility="public" +> +<parameter name="context" type="android.content.Context"> +</parameter> +<parameter name="id" type="int"> +</parameter> +<parameter name="uri" type="java.lang.String"> +</parameter> +<parameter name="parameters" type="java.lang.Object..."> +</parameter> +</method> +<method name="loadCursorAdapter" + return="android.widget.CursorAdapter" + abstract="false" + native="false" + synchronized="false" + static="true" + final="false" + deprecated="not deprecated" + visibility="public" +> +<parameter name="context" type="android.content.Context"> +</parameter> +<parameter name="id" type="int"> +</parameter> +<parameter name="cursor" type="android.database.Cursor"> +</parameter> +<parameter name="parameters" type="java.lang.Object..."> +</parameter> +</method> +</class> +<class name="Adapters.CursorBinder" + extends="java.lang.Object" + abstract="true" + static="true" + final="false" + deprecated="not deprecated" + visibility="public" +> +<constructor name="Adapters.CursorBinder" + type="android.widget.Adapters.CursorBinder" + static="false" + final="false" + deprecated="not deprecated" + visibility="public" +> +<parameter name="context" type="android.content.Context"> +</parameter> +<parameter name="transformation" type="android.widget.Adapters.CursorTransformation"> +</parameter> +</constructor> +<method name="bind" + return="boolean" + abstract="true" + native="false" + synchronized="false" + static="false" + final="false" + deprecated="not deprecated" + visibility="public" +> +<parameter name="view" type="android.view.View"> +</parameter> +<parameter name="cursor" type="android.database.Cursor"> +</parameter> +<parameter name="columnIndex" type="int"> +</parameter> +</method> +<field name="mContext" + type="android.content.Context" + transient="false" + volatile="false" + static="false" + final="true" + deprecated="not deprecated" + visibility="protected" +> +</field> +<field name="mTransformation" + type="android.widget.Adapters.CursorTransformation" + transient="false" + volatile="false" + static="false" + final="true" + deprecated="not deprecated" + visibility="protected" +> +</field> +</class> +<class name="Adapters.CursorTransformation" + extends="java.lang.Object" + abstract="true" + static="true" + final="false" + deprecated="not deprecated" + visibility="public" +> +<constructor name="Adapters.CursorTransformation" + type="android.widget.Adapters.CursorTransformation" + static="false" + final="false" + deprecated="not deprecated" + visibility="public" +> +<parameter name="context" type="android.content.Context"> +</parameter> +</constructor> +<method name="transform" + return="java.lang.String" + abstract="true" + native="false" + synchronized="false" + static="false" + final="false" + deprecated="not deprecated" + visibility="public" +> +<parameter name="cursor" type="android.database.Cursor"> +</parameter> +<parameter name="columnIndex" type="int"> +</parameter> +</method> +<method name="transformToResource" + return="int" + abstract="false" + native="false" + synchronized="false" + static="false" + final="false" + deprecated="not deprecated" + visibility="public" +> +<parameter name="cursor" type="android.database.Cursor"> +</parameter> +<parameter name="columnIndex" type="int"> +</parameter> +</method> +<field name="mContext" + type="android.content.Context" + transient="false" + volatile="false" + static="false" + final="true" + deprecated="not deprecated" + visibility="protected" +> +</field> +</class> <class name="AlphabetIndexer" extends="android.database.DataSetObserver" abstract="false" @@ -199530,6 +200096,20 @@ <parameter name="autoRequery" type="boolean"> </parameter> </constructor> +<constructor name="CursorAdapter" + type="android.widget.CursorAdapter" + static="false" + final="false" + deprecated="not deprecated" + visibility="public" +> +<parameter name="context" type="android.content.Context"> +</parameter> +<parameter name="c" type="android.database.Cursor"> +</parameter> +<parameter name="flags" type="int"> +</parameter> +</constructor> <method name="bindView" return="void" abstract="true" @@ -199677,6 +200257,23 @@ <parameter name="autoRequery" type="boolean"> </parameter> </method> +<method name="init" + return="void" + abstract="false" + native="false" + synchronized="false" + static="false" + final="false" + deprecated="not deprecated" + visibility="protected" +> +<parameter name="context" type="android.content.Context"> +</parameter> +<parameter name="c" type="android.database.Cursor"> +</parameter> +<parameter name="flags" type="int"> +</parameter> +</method> <method name="newDropDownView" return="android.view.View" abstract="false" @@ -199748,6 +200345,28 @@ <parameter name="filterQueryProvider" type="android.widget.FilterQueryProvider"> </parameter> </method> +<field name="FLAG_AUTO_REQUERY" + type="int" + transient="false" + volatile="false" + value="1" + static="true" + final="true" + deprecated="not deprecated" + visibility="public" +> +</field> +<field name="FLAG_REGISTER_CONTENT_OBSERVER" + type="int" + transient="false" + volatile="false" + value="2" + static="true" + final="true" + deprecated="not deprecated" + visibility="public" +> +</field> </class> <class name="CursorTreeAdapter" extends="android.widget.BaseExpandableListAdapter" diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java index 30822d4..0afd6d2 100644 --- a/core/java/android/content/Context.java +++ b/core/java/android/content/Context.java @@ -1372,7 +1372,6 @@ public abstract class Context { public static final String SENSOR_SERVICE = "sensor"; /** - * @hide * Use with {@link #getSystemService} to retrieve a {@link * android.os.storage.StorageManager} for accesssing system storage * functions. diff --git a/core/java/android/content/SyncManager.java b/core/java/android/content/SyncManager.java index 455815f..18f69af 100644 --- a/core/java/android/content/SyncManager.java +++ b/core/java/android/content/SyncManager.java @@ -16,6 +16,8 @@ package android.content; +import com.google.android.collect.Maps; + import com.android.internal.R; import com.android.internal.util.ArrayUtils; @@ -55,6 +57,7 @@ import android.util.Pair; import java.io.FileDescriptor; import java.io.PrintWriter; import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Random; @@ -126,14 +129,13 @@ public class SyncManager implements OnAccountsUpdateListener { private static final int INITIALIZATION_UNBIND_DELAY_MS = 5000; - private static final String SYNC_WAKE_LOCK = "SyncManagerSyncWakeLock"; + private static final String SYNC_WAKE_LOCK_PREFIX = "SyncWakeLock"; private static final String HANDLE_SYNC_ALARM_WAKE_LOCK = "SyncManagerHandleSyncAlarmWakeLock"; private Context mContext; private volatile Account[] mAccounts = INITIAL_ACCOUNTS_ARRAY; - volatile private PowerManager.WakeLock mSyncWakeLock; volatile private PowerManager.WakeLock mHandleAlarmWakeLock; volatile private boolean mDataConnectionIsConnected = false; volatile private boolean mStorageIsLow = false; @@ -195,6 +197,8 @@ public class SyncManager implements OnAccountsUpdateListener { private static final Account[] INITIAL_ACCOUNTS_ARRAY = new Account[0]; + private final PowerManager mPowerManager; + public void onAccountsUpdated(Account[] accounts) { // remember if this was the first time this was called after an update final boolean justBootedUp = mAccounts == INITIAL_ACCOUNTS_ARRAY; @@ -356,15 +360,13 @@ public class SyncManager implements OnAccountsUpdateListener { } else { mNotificationMgr = null; } - PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); - mSyncWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, SYNC_WAKE_LOCK); - mSyncWakeLock.setReferenceCounted(false); + mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); // This WakeLock is used to ensure that we stay awake between the time that we receive // a sync alarm notification and when we finish processing it. We need to do this // because we don't do the work in the alarm handler, rather we do it in a message // handler. - mHandleAlarmWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, + mHandleAlarmWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, HANDLE_SYNC_ALARM_WAKE_LOCK); mHandleAlarmWakeLock.setReferenceCounted(false); @@ -1302,6 +1304,9 @@ public class SyncManager implements OnAccountsUpdateListener { public final SyncNotificationInfo mSyncNotificationInfo = new SyncNotificationInfo(); private Long mAlarmScheduleTime = null; public final SyncTimeTracker mSyncTimeTracker = new SyncTimeTracker(); + private PowerManager.WakeLock mSyncWakeLock; + private final HashMap<Pair<String, String>, PowerManager.WakeLock> mWakeLocks = + Maps.newHashMap(); // used to track if we have installed the error notification so that we don't reinstall // it if sync is still failing @@ -1315,6 +1320,18 @@ public class SyncManager implements OnAccountsUpdateListener { } } + private PowerManager.WakeLock getSyncWakeLock(String accountType, String authority) { + final Pair<String, String> wakeLockKey = Pair.create(accountType, authority); + PowerManager.WakeLock wakeLock = mWakeLocks.get(wakeLockKey); + if (wakeLock == null) { + final String name = SYNC_WAKE_LOCK_PREFIX + "_" + authority + "_" + accountType; + wakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, name); + wakeLock.setReferenceCounted(false); + mWakeLocks.put(wakeLockKey, wakeLock); + } + return wakeLock; + } + private void waitUntilReadyToRun() { CountDownLatch latch = mReadyToRunLatch; if (latch != null) { @@ -1477,8 +1494,9 @@ public class SyncManager implements OnAccountsUpdateListener { } } finally { final boolean isSyncInProgress = mActiveSyncContext != null; - if (!isSyncInProgress) { + if (!isSyncInProgress && mSyncWakeLock != null) { mSyncWakeLock.release(); + mSyncWakeLock = null; } manageSyncNotification(); manageErrorNotification(); @@ -1708,7 +1726,26 @@ public class SyncManager implements OnAccountsUpdateListener { return; } - mSyncWakeLock.acquire(); + // Find the wakelock for this account and authority and store it in mSyncWakeLock. + // Be sure to release the previous wakelock so that we don't end up with it being + // held until it is used again. + // There are a couple tricky things about this code: + // - make sure that we acquire the new wakelock before releasing the old one, + // otherwise the device might go to sleep as soon as we release it. + // - since we use non-reference counted wakelocks we have to be sure not to do + // the release if the wakelock didn't change. Othewise we would do an + // acquire followed by a release on the same lock, resulting in no lock + // being held. + PowerManager.WakeLock oldWakeLock = mSyncWakeLock; + try { + mSyncWakeLock = getSyncWakeLock(op.account.type, op.authority); + mSyncWakeLock.acquire(); + } finally { + if (oldWakeLock != null && oldWakeLock != mSyncWakeLock) { + oldWakeLock.release(); + } + } + // no need to schedule an alarm, as that will be done by our caller. // the next step will occur when we get either a timeout or a diff --git a/core/java/android/os/storage/StorageEventListener.java b/core/java/android/os/storage/StorageEventListener.java index 7b883a7..d3d39d6 100644 --- a/core/java/android/os/storage/StorageEventListener.java +++ b/core/java/android/os/storage/StorageEventListener.java @@ -18,7 +18,6 @@ package android.os.storage; /** * Used for receiving notifications from the StorageManager - * @hide */ public abstract class StorageEventListener { /** diff --git a/core/java/android/os/storage/StorageManager.java b/core/java/android/os/storage/StorageManager.java index a12603c..b49979c 100644 --- a/core/java/android/os/storage/StorageManager.java +++ b/core/java/android/os/storage/StorageManager.java @@ -45,8 +45,6 @@ import java.util.List; * {@link android.content.Context#getSystemService(java.lang.String)} with an argument * of {@link android.content.Context#STORAGE_SERVICE}. * - * @hide - * */ public class StorageManager diff --git a/core/java/android/os/storage/StorageResultCode.java b/core/java/android/os/storage/StorageResultCode.java index 075f47f..07d95df 100644 --- a/core/java/android/os/storage/StorageResultCode.java +++ b/core/java/android/os/storage/StorageResultCode.java @@ -19,8 +19,6 @@ package android.os.storage; /** * Class that provides access to constants returned from StorageManager * and lower level MountService APIs. - * - * @hide */ public class StorageResultCode { diff --git a/core/java/android/pim/vcard/JapaneseUtils.java b/core/java/android/pim/vcard/JapaneseUtils.java index 875c29e..b37b07d 100644 --- a/core/java/android/pim/vcard/JapaneseUtils.java +++ b/core/java/android/pim/vcard/JapaneseUtils.java @@ -366,11 +366,11 @@ import java.util.Map; } /** - * Return half-width version of that character if possible. Return null if not possible + * Returns half-width version of that character if possible. Returns null if not possible * @param ch input character * @return CharSequence object if the mapping for ch exists. Return null otherwise. */ - public static String tryGetHalfWidthText(char ch) { + public static String tryGetHalfWidthText(final char ch) { if (sHalfWidthMap.containsKey(ch)) { return sHalfWidthMap.get(ch); } else { diff --git a/core/java/android/pim/vcard/VCardBuilder.java b/core/java/android/pim/vcard/VCardBuilder.java index 0a6415d..9b8cfbb 100644 --- a/core/java/android/pim/vcard/VCardBuilder.java +++ b/core/java/android/pim/vcard/VCardBuilder.java @@ -47,7 +47,23 @@ import java.util.Map; import java.util.Set; /** - * The class which lets users create their own vCard String. + * <p> + * The class which lets users create their own vCard String. Typical usage is as follows: + * </p> + * <pre class="prettyprint">final VCardBuilder builder = new VCardBuilder(vcardType); + * builder.appendNameProperties(contentValuesListMap.get(StructuredName.CONTENT_ITEM_TYPE)) + * .appendNickNames(contentValuesListMap.get(Nickname.CONTENT_ITEM_TYPE)) + * .appendPhones(contentValuesListMap.get(Phone.CONTENT_ITEM_TYPE)) + * .appendEmails(contentValuesListMap.get(Email.CONTENT_ITEM_TYPE)) + * .appendPostals(contentValuesListMap.get(StructuredPostal.CONTENT_ITEM_TYPE)) + * .appendOrganizations(contentValuesListMap.get(Organization.CONTENT_ITEM_TYPE)) + * .appendWebsites(contentValuesListMap.get(Website.CONTENT_ITEM_TYPE)) + * .appendPhotos(contentValuesListMap.get(Photo.CONTENT_ITEM_TYPE)) + * .appendNotes(contentValuesListMap.get(Note.CONTENT_ITEM_TYPE)) + * .appendEvents(contentValuesListMap.get(Event.CONTENT_ITEM_TYPE)) + * .appendIms(contentValuesListMap.get(Im.CONTENT_ITEM_TYPE)) + * .appendRelation(contentValuesListMap.get(Relation.CONTENT_ITEM_TYPE)); + * return builder.toString();</pre> */ public class VCardBuilder { private static final String LOG_TAG = "VCardBuilder"; @@ -75,13 +91,14 @@ public class VCardBuilder { private static final String VCARD_WS = " "; private static final String VCARD_PARAM_EQUAL = "="; - private static final String VCARD_PARAM_ENCODING_QP = "ENCODING=QUOTED-PRINTABLE"; - - private static final String VCARD_PARAM_ENCODING_BASE64_V21 = "ENCODING=BASE64"; - private static final String VCARD_PARAM_ENCODING_BASE64_V30 = "ENCODING=b"; + private static final String VCARD_PARAM_ENCODING_QP = + "ENCODING=" + VCardConstants.PARAM_ENCODING_QP; + private static final String VCARD_PARAM_ENCODING_BASE64_V21 = + "ENCODING=" + VCardConstants.PARAM_ENCODING_BASE64; + private static final String VCARD_PARAM_ENCODING_BASE64_V30 = + "ENCODING=" + VCardConstants.PARAM_ENCODING_B; private static final String SHIFT_JIS = "SHIFT_JIS"; - private static final String UTF_8 = "UTF-8"; private final int mVCardType; @@ -92,21 +109,28 @@ public class VCardBuilder { private final boolean mShouldUseQuotedPrintable; private final boolean mUsesAndroidProperty; private final boolean mUsesDefactProperty; - private final boolean mUsesUtf8; - private final boolean mUsesShiftJis; private final boolean mAppendTypeParamName; private final boolean mRefrainsQPToNameProperties; private final boolean mNeedsToConvertPhoneticString; private final boolean mShouldAppendCharsetParam; - private final String mCharsetString; + private final String mCharset; private final String mVCardCharsetParameter; private StringBuilder mBuilder; private boolean mEndAppended; public VCardBuilder(final int vcardType) { + // Default charset should be used + this(vcardType, null); + } + + /** + * @param vcardType + * @param charset If null, we use default charset for export. + */ + public VCardBuilder(final int vcardType, String charset) { mVCardType = vcardType; mIsV30 = VCardConfig.isV30(vcardType); @@ -116,40 +140,77 @@ public class VCardBuilder { mOnlyOneNoteFieldIsAvailable = VCardConfig.onlyOneNoteFieldIsAvailable(vcardType); mUsesAndroidProperty = VCardConfig.usesAndroidSpecificProperty(vcardType); mUsesDefactProperty = VCardConfig.usesDefactProperty(vcardType); - mUsesUtf8 = VCardConfig.usesUtf8(vcardType); - mUsesShiftJis = VCardConfig.usesShiftJis(vcardType); mRefrainsQPToNameProperties = VCardConfig.shouldRefrainQPToNameProperties(vcardType); mAppendTypeParamName = VCardConfig.appendTypeParamName(vcardType); mNeedsToConvertPhoneticString = VCardConfig.needsToConvertPhoneticString(vcardType); - mShouldAppendCharsetParam = !(mIsV30 && mUsesUtf8); - - if (mIsDoCoMo) { - String charset; - try { - charset = CharsetUtils.charsetForVendor(SHIFT_JIS, "docomo").name(); - } catch (UnsupportedCharsetException e) { - Log.e(LOG_TAG, "DoCoMo-specific SHIFT_JIS was not found. Use SHIFT_JIS as is."); - charset = SHIFT_JIS; - } - mCharsetString = charset; - // Do not use mCharsetString bellow since it is different from "SHIFT_JIS" but - // may be "DOCOMO_SHIFT_JIS" or something like that (internal expression used in - // Android, not shown to the public). - mVCardCharsetParameter = "CHARSET=" + SHIFT_JIS; - } else if (mUsesShiftJis) { - String charset; - try { - charset = CharsetUtils.charsetForVendor(SHIFT_JIS).name(); - } catch (UnsupportedCharsetException e) { - Log.e(LOG_TAG, "Vendor-specific SHIFT_JIS was not found. Use SHIFT_JIS as is."); - charset = SHIFT_JIS; - } - mCharsetString = charset; + final boolean shouldUseUtf8 = VCardConfig.shouldUseUtf8ForExport(vcardType); + final boolean shouldUseShiftJis = VCardConfig.shouldUseShiftJisForExport(vcardType); + + // vCard 2.1 requires charset. + // vCard 3.0 does not allow it but we found some devices use it to determine + // the exact charset. + // We currently append it only when charset other than UTF_8 is used. + mShouldAppendCharsetParam = !(mIsV30 && shouldUseUtf8); + + if (VCardConfig.isDoCoMo(vcardType) || shouldUseShiftJis) { + if (!SHIFT_JIS.equalsIgnoreCase(charset)) { + Log.w(LOG_TAG, + "The charset \"" + charset + "\" is used while " + + SHIFT_JIS + " is needed to be used."); + if (TextUtils.isEmpty(charset)) { + mCharset = SHIFT_JIS; + } else { + try { + charset = CharsetUtils.charsetForVendor(charset).name(); + } catch (UnsupportedCharsetException e) { + Log.i(LOG_TAG, + "Career-specific \"" + charset + "\" was not found (as usual). " + + "Use it as is."); + } + mCharset = charset; + } + } else { + if (mIsDoCoMo) { + try { + charset = CharsetUtils.charsetForVendor(SHIFT_JIS, "docomo").name(); + } catch (UnsupportedCharsetException e) { + Log.e(LOG_TAG, + "DoCoMo-specific SHIFT_JIS was not found. " + + "Use SHIFT_JIS as is."); + charset = SHIFT_JIS; + } + } else { + try { + charset = CharsetUtils.charsetForVendor(SHIFT_JIS).name(); + } catch (UnsupportedCharsetException e) { + Log.e(LOG_TAG, + "Career-specific SHIFT_JIS was not found. " + + "Use SHIFT_JIS as is."); + charset = SHIFT_JIS; + } + } + mCharset = charset; + } mVCardCharsetParameter = "CHARSET=" + SHIFT_JIS; } else { - mCharsetString = UTF_8; - mVCardCharsetParameter = "CHARSET=" + UTF_8; + if (TextUtils.isEmpty(charset)) { + Log.i(LOG_TAG, + "Use the charset \"" + VCardConfig.DEFAULT_EXPORT_CHARSET + + "\" for export."); + mCharset = VCardConfig.DEFAULT_EXPORT_CHARSET; + mVCardCharsetParameter = "CHARSET=" + VCardConfig.DEFAULT_EXPORT_CHARSET; + } else { + try { + charset = CharsetUtils.charsetForVendor(charset).name(); + } catch (UnsupportedCharsetException e) { + Log.i(LOG_TAG, + "Career-specific \"" + charset + "\" was not found (as usual). " + + "Use it as is."); + } + mCharset = charset; + mVCardCharsetParameter = "CHARSET=" + charset; + } } clear(); } @@ -379,8 +440,8 @@ public class VCardBuilder { mBuilder.append(VCardConstants.PROPERTY_FN); // Note: "CHARSET" param is not allowed in vCard 3.0, but we may add it - // when it would be useful for external importers, assuming no external - // importer allows this vioration. + // when it would be useful or necessary for external importers, + // assuming the external importer allows this vioration of the spec. if (shouldAppendCharsetParam(displayName)) { mBuilder.append(VCARD_PARAM_SEPARATOR); mBuilder.append(mVCardCharsetParameter); @@ -454,18 +515,18 @@ public class VCardBuilder { mBuilder.append(VCARD_END_OF_LINE); } else if (mIsJapaneseMobilePhone) { // Note: There is no appropriate property for expressing - // phonetic name in vCard 2.1, while there is in + // phonetic name (Yomigana in Japanese) in vCard 2.1, while there is in // vCard 3.0 (SORT-STRING). - // We chose to use DoCoMo's way when the device is Japanese one - // since it is supported by - // a lot of Japanese mobile phones. This is "X-" property, so - // any parser hopefully would not get confused with this. + // We use DoCoMo's way when the device is Japanese one since it is already + // supported by a lot of Japanese mobile phones. + // This is "X-" property, so any parser hopefully would not get + // confused with this. // // Also, DoCoMo's specification requires vCard composer to use just the first // column. // i.e. - // o SOUND;X-IRMC-N:Miyakawa Daisuke;;;; - // x SOUND;X-IRMC-N:Miyakawa;Daisuke;;; + // good: SOUND;X-IRMC-N:Miyakawa Daisuke;;;; + // bad : SOUND;X-IRMC-N:Miyakawa;Daisuke;;; mBuilder.append(VCardConstants.PROPERTY_SOUND); mBuilder.append(VCARD_PARAM_SEPARATOR); mBuilder.append(VCardConstants.PARAM_TYPE_X_IRMC_N); @@ -519,10 +580,10 @@ public class VCardBuilder { mBuilder.append(encodedPhoneticGivenName); } } - mBuilder.append(VCARD_ITEM_SEPARATOR); - mBuilder.append(VCARD_ITEM_SEPARATOR); - mBuilder.append(VCARD_ITEM_SEPARATOR); - mBuilder.append(VCARD_ITEM_SEPARATOR); + mBuilder.append(VCARD_ITEM_SEPARATOR); // family;given + mBuilder.append(VCARD_ITEM_SEPARATOR); // given;middle + mBuilder.append(VCARD_ITEM_SEPARATOR); // middle;prefix + mBuilder.append(VCARD_ITEM_SEPARATOR); // prefix;suffix mBuilder.append(VCARD_END_OF_LINE); } @@ -549,7 +610,7 @@ public class VCardBuilder { mBuilder.append(VCARD_DATA_SEPARATOR); mBuilder.append(encodedPhoneticGivenName); mBuilder.append(VCARD_END_OF_LINE); - } + } // if (!TextUtils.isEmpty(phoneticGivenName)) if (!TextUtils.isEmpty(phoneticMiddleName)) { final boolean reallyUseQuotedPrintable = (mShouldUseQuotedPrintable && @@ -572,7 +633,7 @@ public class VCardBuilder { mBuilder.append(VCARD_DATA_SEPARATOR); mBuilder.append(encodedPhoneticMiddleName); mBuilder.append(VCARD_END_OF_LINE); - } + } // if (!TextUtils.isEmpty(phoneticGivenName)) if (!TextUtils.isEmpty(phoneticFamilyName)) { final boolean reallyUseQuotedPrintable = (mShouldUseQuotedPrintable && @@ -595,7 +656,7 @@ public class VCardBuilder { mBuilder.append(VCARD_DATA_SEPARATOR); mBuilder.append(encodedPhoneticFamilyName); mBuilder.append(VCARD_END_OF_LINE); - } + } // if (!TextUtils.isEmpty(phoneticFamilyName)) } } @@ -642,22 +703,18 @@ public class VCardBuilder { if (TextUtils.isEmpty(phoneNumber)) { continue; } - int type = (typeAsObject != null ? typeAsObject : DEFAULT_PHONE_TYPE); - if (type == Phone.TYPE_PAGER) { + + // PAGER number needs unformatted "phone number". + final int type = (typeAsObject != null ? typeAsObject : DEFAULT_PHONE_TYPE); + if (type == Phone.TYPE_PAGER || + VCardConfig.refrainPhoneNumberFormatting(mVCardType)) { phoneLineExists = true; if (!phoneSet.contains(phoneNumber)) { phoneSet.add(phoneNumber); appendTelLine(type, label, phoneNumber, isPrimary); } } else { - // The entry "may" have several phone numbers when the contact entry is - // corrupted because of its original source. - // - // e.g. I encountered the entry like the following. - // "111-222-3333 (Miami)\n444-555-6666 (Broward; 305-653-6796 (Miami); ..." - // This kind of entry is not able to be inserted via Android devices, but - // possible if the source of the data is already corrupted. - List<String> phoneNumberList = splitIfSeveralPhoneNumbersExist(phoneNumber); + final List<String> phoneNumberList = splitAndTrimPhoneNumbers(phoneNumber); if (phoneNumberList.isEmpty()) { continue; } @@ -670,7 +727,7 @@ public class VCardBuilder { phoneSet.add(actualPhoneNumber); appendTelLine(type, label, formattedPhoneNumber, isPrimary); } - } + } // for (String actualPhoneNumber : phoneNumberList) { } } } @@ -682,15 +739,38 @@ public class VCardBuilder { return this; } - private List<String> splitIfSeveralPhoneNumbersExist(final String phoneNumber) { - List<String> phoneList = new ArrayList<String>(); + /** + * <p> + * Splits a given string expressing phone numbers into several strings, and remove + * unnecessary characters inside them. The size of a returned list becomes 1 when + * no split is needed. + * </p> + * <p> + * The given number "may" have several phone numbers when the contact entry is corrupted + * because of its original source. + * e.g. "111-222-3333 (Miami)\n444-555-6666 (Broward; 305-653-6796 (Miami)" + * </p> + * <p> + * This kind of "phone numbers" will not be created with Android vCard implementation, + * but we may encounter them if the source of the input data has already corrupted + * implementation. + * </p> + * <p> + * To handle this case, this method first splits its input into multiple parts + * (e.g. "111-222-3333 (Miami)", "444-555-6666 (Broward", and 305653-6796 (Miami)") and + * removes unnecessary strings like "(Miami)". + * </p> + * <p> + * Do not call this method when trimming is inappropriate for its receivers. + * </p> + */ + private List<String> splitAndTrimPhoneNumbers(final String phoneNumber) { + final List<String> phoneList = new ArrayList<String>(); StringBuilder builder = new StringBuilder(); final int length = phoneNumber.length(); for (int i = 0; i < length; i++) { final char ch = phoneNumber.charAt(i); - // TODO: add a test case for string with '+', and care the other possible issues - // which may happen by ignoring non-digits other than '+'. if (Character.isDigit(ch) || ch == '+') { builder.append(ch); } else if ((ch == ';' || ch == '\n') && builder.length() > 0) { @@ -903,21 +983,21 @@ public class VCardBuilder { encodedCountry = escapeCharacters(rawCountry); encodedNeighborhood = escapeCharacters(rawNeighborhood); } - final StringBuffer addressBuffer = new StringBuffer(); - addressBuffer.append(encodedPoBox); - addressBuffer.append(VCARD_ITEM_SEPARATOR); - addressBuffer.append(VCARD_ITEM_SEPARATOR); - addressBuffer.append(encodedStreet); - addressBuffer.append(VCARD_ITEM_SEPARATOR); - addressBuffer.append(encodedLocality); - addressBuffer.append(VCARD_ITEM_SEPARATOR); - addressBuffer.append(encodedRegion); - addressBuffer.append(VCARD_ITEM_SEPARATOR); - addressBuffer.append(encodedPostalCode); - addressBuffer.append(VCARD_ITEM_SEPARATOR); - addressBuffer.append(encodedCountry); + final StringBuilder addressBuilder = new StringBuilder(); + addressBuilder.append(encodedPoBox); + addressBuilder.append(VCARD_ITEM_SEPARATOR); // PO BOX ; Extended Address + addressBuilder.append(VCARD_ITEM_SEPARATOR); // Extended Address : Street + addressBuilder.append(encodedStreet); + addressBuilder.append(VCARD_ITEM_SEPARATOR); // Street : Locality + addressBuilder.append(encodedLocality); + addressBuilder.append(VCARD_ITEM_SEPARATOR); // Locality : Region + addressBuilder.append(encodedRegion); + addressBuilder.append(VCARD_ITEM_SEPARATOR); // Region : Postal Code + addressBuilder.append(encodedPostalCode); + addressBuilder.append(VCARD_ITEM_SEPARATOR); // Postal Code : Country + addressBuilder.append(encodedCountry); return new PostalStruct( - reallyUseQuotedPrintable, appendCharset, addressBuffer.toString()); + reallyUseQuotedPrintable, appendCharset, addressBuilder.toString()); } else { // VCardUtils.areAllEmpty(rawAddressArray) == true // Try to use FORMATTED_ADDRESS instead. final String rawFormattedAddress = @@ -940,16 +1020,16 @@ public class VCardBuilder { // We use the second value ("Extended Address") just because Japanese mobile phones // do so. If the other importer expects the value be in the other field, some flag may // be needed. - final StringBuffer addressBuffer = new StringBuffer(); - addressBuffer.append(VCARD_ITEM_SEPARATOR); - addressBuffer.append(encodedFormattedAddress); - addressBuffer.append(VCARD_ITEM_SEPARATOR); - addressBuffer.append(VCARD_ITEM_SEPARATOR); - addressBuffer.append(VCARD_ITEM_SEPARATOR); - addressBuffer.append(VCARD_ITEM_SEPARATOR); - addressBuffer.append(VCARD_ITEM_SEPARATOR); + final StringBuilder addressBuilder = new StringBuilder(); + addressBuilder.append(VCARD_ITEM_SEPARATOR); // PO BOX ; Extended Address + addressBuilder.append(encodedFormattedAddress); + addressBuilder.append(VCARD_ITEM_SEPARATOR); // Extended Address : Street + addressBuilder.append(VCARD_ITEM_SEPARATOR); // Street : Locality + addressBuilder.append(VCARD_ITEM_SEPARATOR); // Locality : Region + addressBuilder.append(VCARD_ITEM_SEPARATOR); // Region : Postal Code + addressBuilder.append(VCARD_ITEM_SEPARATOR); // Postal Code : Country return new PostalStruct( - reallyUseQuotedPrintable, appendCharset, addressBuffer.toString()); + reallyUseQuotedPrintable, appendCharset, addressBuilder.toString()); } } @@ -1146,6 +1226,8 @@ public class VCardBuilder { } public VCardBuilder appendEvents(final List<ContentValues> contentValuesList) { + // There's possibility where a given object may have more than one birthday, which + // is inappropriate. We just build one birthday. if (contentValuesList != null) { String primaryBirthday = null; String secondaryBirthday = null; @@ -1213,16 +1295,19 @@ public class VCardBuilder { return this; } + /** + * @param emitEveryTime If true, builder builds the line even when there's no entry. + */ public void appendPostalLine(final int type, final String label, final ContentValues contentValues, - final boolean isPrimary, final boolean emitLineEveryTime) { + final boolean isPrimary, final boolean emitEveryTime) { final boolean reallyUseQuotedPrintable; final boolean appendCharset; final String addressValue; { PostalStruct postalStruct = tryConstructPostalStruct(contentValues); if (postalStruct == null) { - if (emitLineEveryTime) { + if (emitEveryTime) { reallyUseQuotedPrintable = false; appendCharset = false; addressValue = ""; @@ -1537,7 +1622,8 @@ public class VCardBuilder { mBuilder.append(VCARD_END_OF_LINE); } - public void appendAndroidSpecificProperty(final String mimeType, ContentValues contentValues) { + public void appendAndroidSpecificProperty( + final String mimeType, ContentValues contentValues) { if (!sAllowedAndroidPropertySet.contains(mimeType)) { return; } @@ -1659,7 +1745,7 @@ public class VCardBuilder { encodedValue = encodeQuotedPrintable(rawValue); } else { // TODO: one line may be too huge, which may be invalid in vCard spec, though - // several (even well-known) applications do not care this. + // several (even well-known) applications do not care that violation. encodedValue = escapeCharacters(rawValue); } @@ -1794,9 +1880,9 @@ public class VCardBuilder { byte[] strArray = null; try { - strArray = str.getBytes(mCharsetString); + strArray = str.getBytes(mCharset); } catch (UnsupportedEncodingException e) { - Log.e(LOG_TAG, "Charset " + mCharsetString + " cannot be used. " + Log.e(LOG_TAG, "Charset " + mCharset + " cannot be used. " + "Try default charset"); strArray = str.getBytes(); } diff --git a/core/java/android/pim/vcard/VCardComposer.java b/core/java/android/pim/vcard/VCardComposer.java index dc0d864..1bc7785 100644 --- a/core/java/android/pim/vcard/VCardComposer.java +++ b/core/java/android/pim/vcard/VCardComposer.java @@ -41,6 +41,7 @@ import android.provider.ContactsContract.CommonDataKinds.Relation; import android.provider.ContactsContract.CommonDataKinds.StructuredName; import android.provider.ContactsContract.CommonDataKinds.StructuredPostal; import android.provider.ContactsContract.CommonDataKinds.Website; +import android.text.TextUtils; import android.util.CharsetUtils; import android.util.Log; @@ -61,15 +62,11 @@ import java.util.Map; /** * <p> - * The class for composing VCard from Contacts information. Note that this is - * completely differnt implementation from - * android.syncml.pim.vcard.VCardComposer, which is not maintained anymore. + * The class for composing vCard from Contacts information. * </p> - * * <p> * Usually, this class should be used like this. * </p> - * * <pre class="prettyprint">VCardComposer composer = null; * try { * composer = new VCardComposer(context); @@ -94,14 +91,17 @@ import java.util.Map; * composer.terminate(); * } * } </pre> + * <P> + * Users have to manually take care of memory efficiency. Even one vCard may contain + * image of non-trivial size for mobile devices. + * </P> + * <P> + * In default, Default {@link VCardBuilder} class is used to build each vCard. + * </P> */ public class VCardComposer { private static final String LOG_TAG = "VCardComposer"; - public static final int DEFAULT_PHONE_TYPE = Phone.TYPE_HOME; - public static final int DEFAULT_POSTAL_TYPE = StructuredPostal.TYPE_HOME; - public static final int DEFAULT_EMAIL_TYPE = Email.TYPE_OTHER; - public static final String FAILURE_REASON_FAILED_TO_GET_DATABASE_INFO = "Failed to get database information"; @@ -119,6 +119,8 @@ public class VCardComposer { public static final String VCARD_TYPE_STRING_DOCOMO = "docomo"; + // Strictly speaking, "Shift_JIS" is the most appropriate, but we use upper version here, + // since usual vCard devices for Japanese devices already use it. private static final String SHIFT_JIS = "SHIFT_JIS"; private static final String UTF_8 = "UTF-8"; @@ -141,7 +143,7 @@ public class VCardComposer { sImMap.put(Im.PROTOCOL_ICQ, VCardConstants.PROPERTY_X_ICQ); sImMap.put(Im.PROTOCOL_JABBER, VCardConstants.PROPERTY_X_JABBER); sImMap.put(Im.PROTOCOL_SKYPE, VCardConstants.PROPERTY_X_SKYPE_USERNAME); - // Google talk is a special case. + // We don't add Google talk here since it has to be handled separately. } public static interface OneEntryHandler { @@ -152,37 +154,37 @@ public class VCardComposer { /** * <p> - * An useful example handler, which emits VCard String to outputstream one by one. + * An useful handler for emitting vCard String to an OutputStream object one by one. * </p> * <p> * The input OutputStream object is closed() on {@link #onTerminate()}. - * Must not close the stream outside. + * Must not close the stream outside this class. * </p> */ public class HandlerForOutputStream implements OneEntryHandler { @SuppressWarnings("hiding") private static final String LOG_TAG = "vcard.VCardComposer.HandlerForOutputStream"; - final private OutputStream mOutputStream; // mWriter will close this. - private Writer mWriter; - private boolean mOnTerminateIsCalled = false; + final private OutputStream mOutputStream; // mWriter will close this. + protected Writer mWriter; + /** * Input stream will be closed on the detruction of this object. */ - public HandlerForOutputStream(OutputStream outputStream) { + public HandlerForOutputStream(final OutputStream outputStream) { mOutputStream = outputStream; } - public boolean onInit(Context context) { + public final boolean onInit(final Context context) { try { mWriter = new BufferedWriter(new OutputStreamWriter( - mOutputStream, mCharsetString)); + mOutputStream, mCharset)); } catch (UnsupportedEncodingException e1) { - Log.e(LOG_TAG, "Unsupported charset: " + mCharsetString); + Log.e(LOG_TAG, "Unsupported charset: " + mCharset); mErrorReason = "Encoding is not supported (usually this does not happen!): " - + mCharsetString; + + mCharset; return false; } @@ -205,7 +207,7 @@ public class VCardComposer { return true; } - public boolean onEntryCreated(String vcard) { + public final boolean onEntryCreated(String vcard) { try { mWriter.write(vcard); } catch (IOException e) { @@ -218,7 +220,7 @@ public class VCardComposer { return true; } - public void onTerminate() { + public final void onTerminate() { mOnTerminateIsCalled = true; if (mWriter != null) { try { @@ -235,14 +237,21 @@ public class VCardComposer { "IOException during closing the output stream: " + e.getMessage()); } finally { - try { - mWriter.close(); - } catch (IOException e) { - } + closeOutputStream(); } } } + // Users can override this if they want to (e.g. if they don't want to close the stream). + // TODO: Should expose bare OutputStream instead? + public void closeOutputStream() { + try { + mWriter.close(); + } catch (IOException e) { + Log.w(LOG_TAG, "IOException is thrown during close(). Ignoring."); + } + } + @Override public void finalize() { if (!mOnTerminateIsCalled) { @@ -257,11 +266,10 @@ public class VCardComposer { private final ContentResolver mContentResolver; private final boolean mIsDoCoMo; - private final boolean mUsesShiftJis; private Cursor mCursor; private int mIdColumn; - private final String mCharsetString; + private final String mCharset; private boolean mTerminateIsCalled; private final List<OneEntryHandler> mHandlerList; @@ -272,11 +280,14 @@ public class VCardComposer { }; public VCardComposer(Context context) { - this(context, VCardConfig.VCARD_TYPE_DEFAULT, true); + this(context, VCardConfig.VCARD_TYPE_DEFAULT, null, true); } + /** + * The variant which sets charset to null and sets careHandlerErrors to true. + */ public VCardComposer(Context context, int vcardType) { - this(context, vcardType, true); + this(context, vcardType, null, true); } public VCardComposer(Context context, String vcardTypeStr, boolean careHandlerErrors) { @@ -284,9 +295,25 @@ public class VCardComposer { } /** + * The variant which sets charset to null. + */ + public VCardComposer(final Context context, final int vcardType, + final boolean careHandlerErrors) { + this(context, vcardType, null, careHandlerErrors); + } + + /** * Construct for supporting call log entry vCard composing. + * + * @param context Context to be used during the composition. + * @param vcardType The type of vCard, typically available via {@link VCardConfig}. + * @param charset The charset to be used. Use null when you don't need the charset. + * @param careHandlerErrors If true, This object returns false everytime + * a Handler object given via {{@link #addHandler(OneEntryHandler)} returns false. + * If false, this ignores those errors. */ public VCardComposer(final Context context, final int vcardType, + String charset, final boolean careHandlerErrors) { mContext = context; mVCardType = vcardType; @@ -294,30 +321,62 @@ public class VCardComposer { mContentResolver = context.getContentResolver(); mIsDoCoMo = VCardConfig.isDoCoMo(vcardType); - mUsesShiftJis = VCardConfig.usesShiftJis(vcardType); mHandlerList = new ArrayList<OneEntryHandler>(); - if (mIsDoCoMo) { - String charset; - try { - charset = CharsetUtils.charsetForVendor(SHIFT_JIS, "docomo").name(); - } catch (UnsupportedCharsetException e) { - Log.e(LOG_TAG, "DoCoMo-specific SHIFT_JIS was not found. Use SHIFT_JIS as is."); - charset = SHIFT_JIS; - } - mCharsetString = charset; - } else if (mUsesShiftJis) { - String charset; - try { - charset = CharsetUtils.charsetForVendor(SHIFT_JIS).name(); - } catch (UnsupportedCharsetException e) { - Log.e(LOG_TAG, "Vendor-specific SHIFT_JIS was not found. Use SHIFT_JIS as is."); - charset = SHIFT_JIS; + if (mIsDoCoMo || VCardConfig.shouldUseShiftJisForExport(vcardType)) { + if (!SHIFT_JIS.equalsIgnoreCase(charset)) { + Log.w(LOG_TAG, + "The charset \"" + charset + "\" is used while " + + SHIFT_JIS + " is needed to be used."); + if (TextUtils.isEmpty(charset)) { + mCharset = SHIFT_JIS; + } else { + try { + charset = CharsetUtils.charsetForVendor(charset).name(); + } catch (UnsupportedCharsetException e) { + Log.i(LOG_TAG, + "Career-specific \"" + charset + "\" was not found (as usual). " + + "Use it as is."); + } + mCharset = charset; + } + } else { + if (mIsDoCoMo) { + try { + charset = CharsetUtils.charsetForVendor(SHIFT_JIS, "docomo").name(); + } catch (UnsupportedCharsetException e) { + Log.e(LOG_TAG, + "DoCoMo-specific SHIFT_JIS was not found. " + + "Use SHIFT_JIS as is."); + charset = SHIFT_JIS; + } + } else { + try { + charset = CharsetUtils.charsetForVendor(SHIFT_JIS).name(); + } catch (UnsupportedCharsetException e) { + Log.e(LOG_TAG, + "Career-specific SHIFT_JIS was not found. " + + "Use SHIFT_JIS as is."); + charset = SHIFT_JIS; + } + } + mCharset = charset; } - mCharsetString = charset; } else { - mCharsetString = UTF_8; + if (TextUtils.isEmpty(charset)) { + mCharset = UTF_8; + } else { + try { + charset = CharsetUtils.charsetForVendor(charset).name(); + } catch (UnsupportedCharsetException e) { + Log.i(LOG_TAG, + "Career-specific \"" + charset + "\" was not found (as usual). " + + "Use it as is."); + } + mCharset = charset; + } } + Log.d(LOG_TAG, "use the charset \"" + mCharset + "\""); } /** @@ -351,7 +410,7 @@ public class VCardComposer { } if (mCareHandlerErrors) { - List<OneEntryHandler> finishedList = new ArrayList<OneEntryHandler>( + final List<OneEntryHandler> finishedList = new ArrayList<OneEntryHandler>( mHandlerList.size()); for (OneEntryHandler handler : mHandlerList) { if (!handler.onInit(mContext)) { @@ -414,7 +473,7 @@ public class VCardComposer { mErrorReason = FAILURE_REASON_NOT_INITIALIZED; return false; } - String vcard; + final String vcard; try { if (mIdColumn >= 0) { vcard = createOneEntryInternal(mCursor.getString(mIdColumn), @@ -437,8 +496,7 @@ public class VCardComposer { mCursor.moveToNext(); } - // This function does not care the OutOfMemoryError on the handler side - // :-P + // This function does not care the OutOfMemoryError on the handler side :-P if (mCareHandlerErrors) { List<OneEntryHandler> finishedList = new ArrayList<OneEntryHandler>( mHandlerList.size()); @@ -457,7 +515,7 @@ public class VCardComposer { } private String createOneEntryInternal(final String contactId, - Method getEntityIteratorMethod) throws VCardException { + final Method getEntityIteratorMethod) throws VCardException { final Map<String, List<ContentValues>> contentValuesListMap = new HashMap<String, List<ContentValues>>(); // The resolver may return the entity iterator with no data. It is possible. @@ -527,20 +585,34 @@ public class VCardComposer { } } - final VCardBuilder builder = new VCardBuilder(mVCardType); - builder.appendNameProperties(contentValuesListMap.get(StructuredName.CONTENT_ITEM_TYPE)) - .appendNickNames(contentValuesListMap.get(Nickname.CONTENT_ITEM_TYPE)) - .appendPhones(contentValuesListMap.get(Phone.CONTENT_ITEM_TYPE)) - .appendEmails(contentValuesListMap.get(Email.CONTENT_ITEM_TYPE)) - .appendPostals(contentValuesListMap.get(StructuredPostal.CONTENT_ITEM_TYPE)) - .appendOrganizations(contentValuesListMap.get(Organization.CONTENT_ITEM_TYPE)) - .appendWebsites(contentValuesListMap.get(Website.CONTENT_ITEM_TYPE)) - .appendPhotos(contentValuesListMap.get(Photo.CONTENT_ITEM_TYPE)) - .appendNotes(contentValuesListMap.get(Note.CONTENT_ITEM_TYPE)) - .appendEvents(contentValuesListMap.get(Event.CONTENT_ITEM_TYPE)) - .appendIms(contentValuesListMap.get(Im.CONTENT_ITEM_TYPE)) - .appendRelation(contentValuesListMap.get(Relation.CONTENT_ITEM_TYPE)); - return builder.toString(); + return buildVCard(contentValuesListMap); + } + + /** + * Builds and returns vCard using given map, whose key is CONTENT_ITEM_TYPE defined in + * {ContactsContract}. Developers can override this method to customize the output. + */ + public String buildVCard(final Map<String, List<ContentValues>> contentValuesListMap) { + if (contentValuesListMap == null) { + Log.e(LOG_TAG, "The given map is null. Ignore and return empty String"); + return ""; + } else { + final VCardBuilder builder = new VCardBuilder(mVCardType, mCharset); + // TODO: Android-specific X attributes? + builder.appendNameProperties(contentValuesListMap.get(StructuredName.CONTENT_ITEM_TYPE)) + .appendNickNames(contentValuesListMap.get(Nickname.CONTENT_ITEM_TYPE)) + .appendPhones(contentValuesListMap.get(Phone.CONTENT_ITEM_TYPE)) + .appendEmails(contentValuesListMap.get(Email.CONTENT_ITEM_TYPE)) + .appendPostals(contentValuesListMap.get(StructuredPostal.CONTENT_ITEM_TYPE)) + .appendOrganizations(contentValuesListMap.get(Organization.CONTENT_ITEM_TYPE)) + .appendWebsites(contentValuesListMap.get(Website.CONTENT_ITEM_TYPE)) + .appendPhotos(contentValuesListMap.get(Photo.CONTENT_ITEM_TYPE)) + .appendNotes(contentValuesListMap.get(Note.CONTENT_ITEM_TYPE)) + .appendEvents(contentValuesListMap.get(Event.CONTENT_ITEM_TYPE)) + .appendIms(contentValuesListMap.get(Im.CONTENT_ITEM_TYPE)) + .appendRelation(contentValuesListMap.get(Relation.CONTENT_ITEM_TYPE)); + return builder.toString(); + } } public void terminate() { @@ -563,26 +635,38 @@ public class VCardComposer { @Override public void finalize() { if (!mTerminateIsCalled) { + Log.w(LOG_TAG, "terminate() is not called yet. We call it in finalize() step."); terminate(); } } + /** + * @return returns the number of available entities. The return value is undefined + * when this object is not ready yet (typically when {{@link #init()} is not called + * or when {@link #terminate()} is already called). + */ public int getCount() { if (mCursor == null) { + Log.w(LOG_TAG, "This object is not ready yet."); return 0; } return mCursor.getCount(); } + /** + * @return true when there's no entity to be built. The return value is undefined + * when this object is not ready yet. + */ public boolean isAfterLast() { if (mCursor == null) { + Log.w(LOG_TAG, "This object is not ready yet."); return false; } return mCursor.isAfterLast(); } /** - * @return Return the error reason if possible. + * @return Returns the error reason. */ public String getErrorReason() { return mErrorReason; diff --git a/core/java/android/pim/vcard/VCardConfig.java b/core/java/android/pim/vcard/VCardConfig.java index 3442ae7..01edf93 100644 --- a/core/java/android/pim/vcard/VCardConfig.java +++ b/core/java/android/pim/vcard/VCardConfig.java @@ -15,6 +15,7 @@ */ package android.pim.vcard; +import android.telephony.PhoneNumberUtils; import android.util.Log; import java.util.HashMap; @@ -43,10 +44,28 @@ public class VCardConfig { /* package */ static final int PARSE_TYPE_FOMA = 3; // For Japanese FOMA mobile phones. /* package */ static final int PARSE_TYPE_WINDOWS_MOBILE_JP = 4; - // Assumes that "iso-8859-1" is able to map "all" 8bit characters to some unicode and - // decode the unicode to the original charset. If not, this setting will cause some bug. - public static final String DEFAULT_CHARSET = "iso-8859-1"; - + /** + * <P> + * The charset used during import. + * </P> + * <P> + * We cannot determine which charset should be used to interpret a given vCard file, + * while we have to decode sime encoded data (e.g. BASE64) to binary. + * In order to avoid "misinterpretation" of charset as much as possible, + * "ISO-8859-1" (a.k.a Latin-1) is first used for reading a stream. + * When charset is specified in a property (with "CHARSET=..." parameter), + * the string is decoded to raw bytes and encoded into the specific charset, + * assuming "ISO-8859-1" is able to map "all" 8bit characters to some unicode, + * and it has 1 to 1 mapping in all 8bit characters. + * If the assumption is not correct, this setting will cause some bug. + * </P> + */ + /* package */ static final String DEFAULT_TEMPORARY_CHARSET = "ISO-8859-1"; + + // TODO: still intermediate procedures uses this charset. Fix it. + public static final String DEFAULT_IMPORT_CHARSET = "ISO-8859-1"; + public static final String DEFAULT_EXPORT_CHARSET = "UTF-8"; + public static final int FLAG_V21 = 0; public static final int FLAG_V30 = 1; @@ -58,10 +77,13 @@ public class VCardConfig { private static final int NAME_ORDER_MASK = 0xC; // 0x10 is reserved for safety - - private static final int FLAG_CHARSET_UTF8 = 0; - private static final int FLAG_CHARSET_SHIFT_JIS = 0x100; - private static final int FLAG_CHARSET_MASK = 0xF00; + + /* + * These flags are ignored when charset is explicitly given by a caller. + */ + private static final int FLAG_USE_UTF8_FOR_EXPORT = 0; + private static final int FLAG_USE_SHIFT_JIS_FOR_EXPORT = 0x100; + private static final int FLAG_CHARSET_MASK_FOR_EKPORT = 0xF00; /** * The flag indicating the vCard composer will add some "X-" properties used only in Android @@ -182,6 +204,30 @@ public class VCardConfig { */ public static final int FLAG_APPEND_TYPE_PARAM = 0x04000000; + /** + * <P> + * The flag indicating the vCard composer does touch nothing toward phone number Strings + * but leave it as is. + * </P> + * <P> + * The vCard specifications mention nothing toward phone numbers, while some devices + * do (wrongly, but with innevitable reasons). + * For example, there's a possibility Japanese mobile phones are expected to have + * just numbers, hypens, plus, etc. but not usual alphabets, while US mobile phones + * should get such characters. To make exported vCard simple for external parsers, + * we have used {@link PhoneNumberUtils#formatNumber(String)} during export, and + * removed unnecessary characters inside the number (e.g. "111-222-3333 (Miami)" + * becomes "111-222-3333"). + * Unfortunate side effect of that use was some control characters used in the other + * areas may be badly affected by the formatting. + * </P> + * <P> + * This flag disables that formatting, affecting both importer and exporter. + * If the user is aware of some side effects due to the implicit formatting, use this flag. + * </P> + */ + public static final int FLAG_REFRAIN_PHONE_NUMBER_FORMATTING = 0x02000000; + //// The followings are VCard types available from importer/exporter. //// /** @@ -196,7 +242,7 @@ public class VCardConfig { * </P> */ public static final int VCARD_TYPE_V21_GENERIC_UTF8 = - (FLAG_V21 | NAME_ORDER_DEFAULT | FLAG_CHARSET_UTF8 | + (FLAG_V21 | NAME_ORDER_DEFAULT | FLAG_USE_UTF8_FOR_EXPORT | FLAG_USE_DEFACT_PROPERTY | FLAG_USE_ANDROID_PROPERTY); /* package */ static String VCARD_TYPE_V21_GENERIC_UTF8_STR = "v21_generic"; @@ -210,7 +256,7 @@ public class VCardConfig { * </P> */ public static final int VCARD_TYPE_V30_GENERIC_UTF8 = - (FLAG_V30 | NAME_ORDER_DEFAULT | FLAG_CHARSET_UTF8 | + (FLAG_V30 | NAME_ORDER_DEFAULT | FLAG_USE_UTF8_FOR_EXPORT | FLAG_USE_DEFACT_PROPERTY | FLAG_USE_ANDROID_PROPERTY); /* package */ static final String VCARD_TYPE_V30_GENERIC_UTF8_STR = "v30_generic"; @@ -222,7 +268,7 @@ public class VCardConfig { * </P> */ public static final int VCARD_TYPE_V21_EUROPE_UTF8 = - (FLAG_V21 | NAME_ORDER_EUROPE | FLAG_CHARSET_UTF8 | + (FLAG_V21 | NAME_ORDER_EUROPE | FLAG_USE_UTF8_FOR_EXPORT | FLAG_USE_DEFACT_PROPERTY | FLAG_USE_ANDROID_PROPERTY); /* package */ static final String VCARD_TYPE_V21_EUROPE_UTF8_STR = "v21_europe"; @@ -236,7 +282,7 @@ public class VCardConfig { * </P> */ public static final int VCARD_TYPE_V30_EUROPE_UTF8 = - (FLAG_V30 | NAME_ORDER_EUROPE | FLAG_CHARSET_UTF8 | + (FLAG_V30 | NAME_ORDER_EUROPE | FLAG_USE_UTF8_FOR_EXPORT | FLAG_USE_DEFACT_PROPERTY | FLAG_USE_ANDROID_PROPERTY); /* package */ static final String VCARD_TYPE_V30_EUROPE_STR = "v30_europe"; @@ -250,7 +296,7 @@ public class VCardConfig { * </P> */ public static final int VCARD_TYPE_V21_JAPANESE_UTF8 = - (FLAG_V21 | NAME_ORDER_JAPANESE | FLAG_CHARSET_UTF8 | + (FLAG_V21 | NAME_ORDER_JAPANESE | FLAG_USE_UTF8_FOR_EXPORT | FLAG_USE_DEFACT_PROPERTY | FLAG_USE_ANDROID_PROPERTY); /* package */ static final String VCARD_TYPE_V21_JAPANESE_UTF8_STR = "v21_japanese_utf8"; @@ -265,7 +311,7 @@ public class VCardConfig { * </P> */ public static final int VCARD_TYPE_V21_JAPANESE_SJIS = - (FLAG_V21 | NAME_ORDER_JAPANESE | FLAG_CHARSET_SHIFT_JIS | + (FLAG_V21 | NAME_ORDER_JAPANESE | FLAG_USE_SHIFT_JIS_FOR_EXPORT | FLAG_USE_DEFACT_PROPERTY | FLAG_USE_ANDROID_PROPERTY); /* package */ static final String VCARD_TYPE_V21_JAPANESE_SJIS_STR = "v21_japanese_sjis"; @@ -280,7 +326,7 @@ public class VCardConfig { * </P> */ public static final int VCARD_TYPE_V30_JAPANESE_SJIS = - (FLAG_V30 | NAME_ORDER_JAPANESE | FLAG_CHARSET_SHIFT_JIS | + (FLAG_V30 | NAME_ORDER_JAPANESE | FLAG_USE_SHIFT_JIS_FOR_EXPORT | FLAG_USE_DEFACT_PROPERTY | FLAG_USE_ANDROID_PROPERTY); /* package */ static final String VCARD_TYPE_V30_JAPANESE_SJIS_STR = "v30_japanese_sjis"; @@ -294,7 +340,7 @@ public class VCardConfig { * </P> */ public static final int VCARD_TYPE_V30_JAPANESE_UTF8 = - (FLAG_V30 | NAME_ORDER_JAPANESE | FLAG_CHARSET_UTF8 | + (FLAG_V30 | NAME_ORDER_JAPANESE | FLAG_USE_UTF8_FOR_EXPORT | FLAG_USE_DEFACT_PROPERTY | FLAG_USE_ANDROID_PROPERTY); /* package */ static final String VCARD_TYPE_V30_JAPANESE_UTF8_STR = "v30_japanese_utf8"; @@ -310,7 +356,7 @@ public class VCardConfig { * </P> */ public static final int VCARD_TYPE_V21_JAPANESE_MOBILE = - (FLAG_V21 | NAME_ORDER_JAPANESE | FLAG_CHARSET_SHIFT_JIS | + (FLAG_V21 | NAME_ORDER_JAPANESE | FLAG_USE_SHIFT_JIS_FOR_EXPORT | FLAG_CONVERT_PHONETIC_NAME_STRINGS | FLAG_REFRAIN_QP_TO_NAME_PROPERTIES); @@ -379,12 +425,17 @@ public class VCardConfig { return !isV30(vcardType); } - public static boolean usesUtf8(final int vcardType) { - return ((vcardType & FLAG_CHARSET_MASK) == FLAG_CHARSET_UTF8); + /* package */ static boolean shouldUseUtf8ForExport(final int vcardType) { + return ((vcardType & FLAG_CHARSET_MASK_FOR_EKPORT) == FLAG_USE_UTF8_FOR_EXPORT); } - public static boolean usesShiftJis(final int vcardType) { - return ((vcardType & FLAG_CHARSET_MASK) == FLAG_CHARSET_SHIFT_JIS); + /** + * Shift_JIS (a charset for Japanese text files) needs special handling to select + * carrer specific variants. + * @hide just for test + */ + public static boolean shouldUseShiftJisForExport(final int vcardType) { + return ((vcardType & FLAG_CHARSET_MASK_FOR_EKPORT) == FLAG_USE_SHIFT_JIS_FOR_EXPORT); } public static int getNameOrderType(final int vcardType) { @@ -423,6 +474,10 @@ public class VCardConfig { return sJapaneseMobileTypeSet.contains(vcardType); } + /* package */ static boolean refrainPhoneNumberFormatting(final int vcardType) { + return ((vcardType & FLAG_REFRAIN_PHONE_NUMBER_FORMATTING) != 0); + } + public static boolean needsToConvertPhoneticString(final int vcardType) { return ((vcardType & FLAG_CONVERT_PHONETIC_NAME_STRINGS) != 0); } diff --git a/core/java/android/pim/vcard/VCardConstants.java b/core/java/android/pim/vcard/VCardConstants.java index 8c07126..e11b1fd 100644 --- a/core/java/android/pim/vcard/VCardConstants.java +++ b/core/java/android/pim/vcard/VCardConstants.java @@ -109,6 +109,12 @@ public class VCardConstants { public static final String PARAM_TYPE_BBS = "BBS"; public static final String PARAM_TYPE_VIDEO = "VIDEO"; + public static final String PARAM_ENCODING_7BIT = "7BIT"; + public static final String PARAM_ENCODING_8BIT = "8BIT"; + public static final String PARAM_ENCODING_QP = "QUOTED-PRINTABLE"; + public static final String PARAM_ENCODING_BASE64 = "BASE64"; // Available in vCard 2.1 + public static final String PARAM_ENCODING_B = "B"; // Available in vCard 3.0 + // TYPE parameters for Phones, which are not formally valid in vCard (at least 2.1). // These types are basically encoded to "X-" parameters when composing vCard. // Parser passes these when "X-" is added to the parameter or not. @@ -130,10 +136,6 @@ public class VCardConstants { // Do not use in composer side. public static final String PARAM_EXTRA_TYPE_COMPANY = "COMPANY"; - // DoCoMo specific type parameter. Used with "SOUND" property, which is alternate of SORT-STRING in - // vCard 3.0. - public static final String PARAM_TYPE_X_IRMC_N = "X-IRMC-N"; - public interface ImportOnly { public static final String PROPERTY_X_NICKNAME = "X-NICKNAME"; // Some device emits this "X-" parameter for expressing Google Talk, @@ -142,6 +144,12 @@ public class VCardConstants { public static final String PROPERTY_X_GOOGLE_TALK_WITH_SPACE = "X-GOOGLE TALK"; } + //// Mainly for package constants. + + // DoCoMo specific type parameter. Used with "SOUND" property, which is alternate of + // SORT-STRING invCard 3.0. + /* package */ static final String PARAM_TYPE_X_IRMC_N = "X-IRMC-N"; + /* package */ static final int MAX_DATA_COLUMN = 15; /* package */ static final int MAX_CHARACTER_NUMS_QP = 76; diff --git a/core/java/android/pim/vcard/VCardEntry.java b/core/java/android/pim/vcard/VCardEntry.java index 1327770..7c7e9b8 100644 --- a/core/java/android/pim/vcard/VCardEntry.java +++ b/core/java/android/pim/vcard/VCardEntry.java @@ -488,7 +488,7 @@ public class VCardEntry { final StringBuilder builder = new StringBuilder(); final String trimed = data.trim(); final String formattedNumber; - if (type == Phone.TYPE_PAGER) { + if (type == Phone.TYPE_PAGER || VCardConfig.refrainPhoneNumberFormatting(mVCardType)) { formattedNumber = trimed; } else { final int length = trimed.length(); @@ -500,8 +500,7 @@ public class VCardEntry { } // Use NANP in default when there's no information about locale. - final int formattingType = (VCardConfig.isJapaneseDevice(mVCardType) ? - PhoneNumberUtils.FORMAT_JAPAN : PhoneNumberUtils.FORMAT_NANP); + final int formattingType = VCardUtils.getPhoneNumberFormat(mVCardType); formattedNumber = PhoneNumberUtils.formatNumber(builder.toString(), formattingType); } PhoneData phoneData = new PhoneData(type, formattedNumber, label, isPrimary); diff --git a/core/java/android/pim/vcard/VCardEntryConstructor.java b/core/java/android/pim/vcard/VCardEntryConstructor.java index 290ca2b..4efb105 100644 --- a/core/java/android/pim/vcard/VCardEntryConstructor.java +++ b/core/java/android/pim/vcard/VCardEntryConstructor.java @@ -80,7 +80,7 @@ public class VCardEntryConstructor implements VCardInterpreter { if (inputCharset != null) { mInputCharset = inputCharset; } else { - mInputCharset = VCardConfig.DEFAULT_CHARSET; + mInputCharset = VCardConfig.DEFAULT_TEMPORARY_CHARSET; } if (charsetForDetodedBytes != null) { mCharsetForDecodedBytes = charsetForDetodedBytes; diff --git a/core/java/android/pim/vcard/VCardParser.java b/core/java/android/pim/vcard/VCardParser.java index 57c52a6..71ab5c4 100644 --- a/core/java/android/pim/vcard/VCardParser.java +++ b/core/java/android/pim/vcard/VCardParser.java @@ -20,82 +20,60 @@ import android.pim.vcard.exception.VCardException; import java.io.IOException; import java.io.InputStream; -public abstract class VCardParser { - protected final int mParseType; - protected boolean mCanceled; - - public VCardParser() { - this(VCardConfig.PARSE_TYPE_UNKNOWN); - } - - public VCardParser(int parseType) { - mParseType = parseType; - } - +public interface VCardParser { /** - * <P> - * Parses the given stream and send the VCard data into VCardBuilderBase object. - * </P. - * <P> + * <p> + * Parses the given stream and send the vCard data into VCardBuilderBase object. + * </p>. + * <p> * Note that vCard 2.1 specification allows "CHARSET" parameter, and some career sets * local encoding to it. For example, Japanese phone career uses Shift_JIS, which is - * formally allowed in VCard 2.1, but not recommended in VCard 3.0. In VCard 2.1, - * In some exreme case, some VCard may have different charsets in one VCard (though - * we do not see any device which emits such kind of malicious data) - * </P> - * <P> - * In order to avoid "misunderstanding" charset as much as possible, this method - * use "ISO-8859-1" for reading the stream. When charset is specified in some property - * (with "CHARSET=..." parameter), the string is decoded to raw bytes and encoded to - * the charset. This method assumes that "ISO-8859-1" has 1 to 1 mapping in all 8bit - * characters, which is not completely sure. In some cases, this "decoding-encoding" - * scheme may fail. To avoid the case, - * </P> - * <P> - * We recommend you to use {@link VCardSourceDetector} and detect which kind of source the - * VCard comes from and explicitly specify a charset using the result. - * </P> + * formally allowed in vCard 2.1, but not allowed in vCard 3.0. In vCard 2.1, + * In some exreme case, it is allowed for vCard to have different charsets in one vCard. + * </p> + * <p> + * We recommend you use {@link VCardSourceDetector} and detect which kind of source the + * vCard comes from and explicitly specify a charset using the result. + * </p> * * @param is The source to parse. * @param interepreter A {@link VCardInterpreter} object which used to construct data. * @return Returns true for success. Otherwise returns false. * @throws IOException, VCardException */ - public abstract boolean parse(InputStream is, VCardInterpreter interepreter) + public boolean parse(InputStream is, VCardInterpreter interepreter) throws IOException, VCardException; - + /** - * <P> + * <p> * The method variants which accept charset. - * </P> - * <P> - * RFC 2426 "recommends" (not forces) to use UTF-8, so it may be OK to use - * UTF-8 as an encoding when parsing vCard 3.0. But note that some Japanese - * phone uses Shift_JIS as a charset (e.g. W61SH), and another uses - * "CHARSET=SHIFT_JIS", which is explicitly prohibited in vCard 3.0 specification (e.g. W53K). - * </P> + * </p> * * @param is The source to parse. * @param charset Charset to be used. - * @param builder The VCardBuilderBase object. + * @param interpreter The VCardBuilderBase object. * @return Returns true when successful. Otherwise returns false. * @throws IOException, VCardException */ - public abstract boolean parse(InputStream is, String charset, VCardInterpreter builder) + public boolean parse(InputStream is, String charset, VCardInterpreter interpreter) throws IOException, VCardException; /** * The method variants which tells this object the operation is already canceled. + * @hide */ - public abstract void parse(InputStream is, String charset, + // TODO: remove this if possible. + public boolean parse(InputStream is, String charset, VCardInterpreter builder, boolean canceled) throws IOException, VCardException; - + /** - * Cancel parsing. - * Actual cancel is done after the end of the current one vcard entry parsing. + * <p> + * Cancel parsing vCard. Useful when you want to stop the parse in the other threads. + * </p> + * <p> + * Actual cancel is done after parsing the current vcard. + * </p> */ - public void cancel() { - mCanceled = true; - } + public abstract void cancel(); } diff --git a/core/java/android/pim/vcard/VCardParserImpl_V21.java b/core/java/android/pim/vcard/VCardParserImpl_V21.java new file mode 100644 index 0000000..22fdb75 --- /dev/null +++ b/core/java/android/pim/vcard/VCardParserImpl_V21.java @@ -0,0 +1,991 @@ +/* + * Copyright (C) 2010 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.pim.vcard; + +import android.pim.vcard.exception.VCardAgentNotSupportedException; +import android.pim.vcard.exception.VCardException; +import android.pim.vcard.exception.VCardInvalidCommentLineException; +import android.pim.vcard.exception.VCardInvalidLineException; +import android.pim.vcard.exception.VCardNestedException; +import android.pim.vcard.exception.VCardVersionException; +import android.util.Log; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; + +/** + * <p> + * Basic implementation achieving vCard parsing. Based on vCard 2.1, + * </p> + */ +/* package */ class VCardParserImpl_V21 { + private static final String LOG_TAG = "VCardParserImpl_V21"; + + private static final class CustomBufferedReader extends BufferedReader { + private long mTime; + + public CustomBufferedReader(Reader in) { + super(in); + } + + @Override + public String readLine() throws IOException { + long start = System.currentTimeMillis(); + String ret = super.readLine(); + long end = System.currentTimeMillis(); + mTime += end - start; + return ret; + } + + public long getTotalmillisecond() { + return mTime; + } + } + + private static final String sDefaultEncoding = "8BIT"; + + protected boolean mCanceled; + protected VCardInterpreter mInterpreter; + + /** + * <p> + * The encoding type for deconding byte streams. This member variable is + * reset to a default encoding every time when a new item comes. + * </p> + * <p> + * "Encoding" in vCard is different from "Charset". It is mainly used for + * addresses, notes, images. "7BIT", "8BIT", "BASE64", and + * "QUOTED-PRINTABLE" are known examples. + * </p> + */ + protected String mCurrentEncoding; + + /** + * <p> + * The reader object to be used internally. + * </p> + * <p> + * Developers should not directly read a line from this object. Use + * getLine() unless there some reason. + * </p> + */ + protected BufferedReader mReader; + + /** + * <p> + * Set for storing unkonwn TYPE attributes, which is not acceptable in vCard + * specification, but happens to be seen in real world vCard. + * </p> + */ + protected final Set<String> mUnknownTypeSet = new HashSet<String>(); + + /** + * <p> + * Set for storing unkonwn VALUE attributes, which is not acceptable in + * vCard specification, but happens to be seen in real world vCard. + * </p> + */ + protected final Set<String> mUnknownValueSet = new HashSet<String>(); + + + // In some cases, vCard is nested. Currently, we only consider the most + // interior vCard data. + // See v21_foma_1.vcf in test directory for more information. + private int mNestCount; + + // Used only for parsing END:VCARD. + private String mPreviousLine; + + // For measuring performance. + private long mTimeTotal; + private long mTimeReadStartRecord; + private long mTimeReadEndRecord; + private long mTimeStartProperty; + private long mTimeEndProperty; + private long mTimeParseItems; + private long mTimeParseLineAndHandleGroup; + private long mTimeParsePropertyValues; + private long mTimeParseAdrOrgN; + private long mTimeHandleMiscPropertyValue; + private long mTimeHandleQuotedPrintable; + private long mTimeHandleBase64; + + /** + * <p> + * Same as {{@link #VCardParserImpl_V21(VCardSourceDetector)} with + * {@link VCardConfig#PARSE_TYPE_UNKNOWN}. + * </p> + */ + public VCardParserImpl_V21() { + this(VCardConfig.PARSE_TYPE_UNKNOWN); + } + + /** + * <p> + * The constructor which uses the estimated type available from a given detector. + * </p> + */ + public VCardParserImpl_V21(VCardSourceDetector detector) { + this(detector != null ? detector.getEstimatedType() : VCardConfig.PARSE_TYPE_UNKNOWN); + } + + /** + * <p> + * The constructor which uses a given parse type like + * {@link VCardConfig#PARSE_TYPE_UNKNOWN}. + * </p> + * <p> + * This should be used only when you already know the exact type to be used. + * </p> + */ + public VCardParserImpl_V21(int parseType) { + if (parseType == VCardConfig.PARSE_TYPE_FOMA) { + mNestCount = 1; + } + } + + /** + * <p> + * Parses the file at the given position. + * </p> + */ + // <pre class="prettyprint">vcard_file = [wsls] vcard [wsls]</pre> + protected void parseVCardFile() throws IOException, VCardException { + boolean firstRead = true; + while (true) { + if (mCanceled) { + break; + } + if (!parseOneVCard(firstRead)) { + break; + } + firstRead = false; + } + + if (mNestCount > 0) { + boolean useCache = true; + for (int i = 0; i < mNestCount; i++) { + readEndVCard(useCache, true); + useCache = false; + } + } + } + + /** + * @return true when a given property name is a valid property name. + */ + protected boolean isValidPropertyName(final String propertyName) { + if (!(getKnownPropertyNameSet().contains(propertyName.toUpperCase()) || + propertyName.startsWith("X-")) + && !mUnknownTypeSet.contains(propertyName)) { + mUnknownTypeSet.add(propertyName); + Log.w(LOG_TAG, "Property name unsupported by vCard 2.1: " + propertyName); + } + return true; + } + + /** + * @return String. It may be null, or its length may be 0 + * @throws IOException + */ + protected String getLine() throws IOException { + return mReader.readLine(); + } + + /** + * @return String with it's length > 0 + * @throws IOException + * @throws VCardException when the stream reached end of line + */ + protected String getNonEmptyLine() throws IOException, VCardException { + String line; + while (true) { + line = getLine(); + if (line == null) { + throw new VCardException("Reached end of buffer."); + } else if (line.trim().length() > 0) { + return line; + } + } + } + + /* + * vcard = "BEGIN" [ws] ":" [ws] "VCARD" [ws] 1*CRLF + * items *CRLF + * "END" [ws] ":" [ws] "VCARD" + */ + private boolean parseOneVCard(boolean firstRead) throws IOException, VCardException { + boolean allowGarbage = false; + if (firstRead) { + if (mNestCount > 0) { + for (int i = 0; i < mNestCount; i++) { + if (!readBeginVCard(allowGarbage)) { + return false; + } + allowGarbage = true; + } + } + } + + if (!readBeginVCard(allowGarbage)) { + return false; + } + long start; + if (mInterpreter != null) { + start = System.currentTimeMillis(); + mInterpreter.startEntry(); + mTimeReadStartRecord += System.currentTimeMillis() - start; + } + start = System.currentTimeMillis(); + parseItems(); + mTimeParseItems += System.currentTimeMillis() - start; + readEndVCard(true, false); + if (mInterpreter != null) { + start = System.currentTimeMillis(); + mInterpreter.endEntry(); + mTimeReadEndRecord += System.currentTimeMillis() - start; + } + return true; + } + + /** + * @return True when successful. False when reaching the end of line + * @throws IOException + * @throws VCardException + */ + protected boolean readBeginVCard(boolean allowGarbage) throws IOException, VCardException { + String line; + do { + while (true) { + line = getLine(); + if (line == null) { + return false; + } else if (line.trim().length() > 0) { + break; + } + } + String[] strArray = line.split(":", 2); + int length = strArray.length; + + // Though vCard 2.1/3.0 specification does not allow lower cases, + // vCard file emitted by some external vCard expoter have such + // invalid Strings. + // So we allow it. + // e.g. BEGIN:vCard + if (length == 2 && strArray[0].trim().equalsIgnoreCase("BEGIN") + && strArray[1].trim().equalsIgnoreCase("VCARD")) { + return true; + } else if (!allowGarbage) { + if (mNestCount > 0) { + mPreviousLine = line; + return false; + } else { + throw new VCardException("Expected String \"BEGIN:VCARD\" did not come " + + "(Instead, \"" + line + "\" came)"); + } + } + } while (allowGarbage); + + throw new VCardException("Reached where must not be reached."); + } + + /** + * <p> + * The arguments useCache and allowGarbase are usually true and false + * accordingly when this function is called outside this function itself. + * </p> + * + * @param useCache When true, line is obtained from mPreviousline. + * Otherwise, getLine() is used. + * @param allowGarbage When true, ignore non "END:VCARD" line. + * @throws IOException + * @throws VCardException + */ + protected void readEndVCard(boolean useCache, boolean allowGarbage) throws IOException, + VCardException { + String line; + do { + if (useCache) { + // Though vCard specification does not allow lower cases, + // some data may have them, so we allow it. + line = mPreviousLine; + } else { + while (true) { + line = getLine(); + if (line == null) { + throw new VCardException("Expected END:VCARD was not found."); + } else if (line.trim().length() > 0) { + break; + } + } + } + + String[] strArray = line.split(":", 2); + if (strArray.length == 2 && strArray[0].trim().equalsIgnoreCase("END") + && strArray[1].trim().equalsIgnoreCase("VCARD")) { + return; + } else if (!allowGarbage) { + throw new VCardException("END:VCARD != \"" + mPreviousLine + "\""); + } + useCache = false; + } while (allowGarbage); + } + + /* + * items = *CRLF item / item + */ + protected void parseItems() throws IOException, VCardException { + boolean ended = false; + + if (mInterpreter != null) { + long start = System.currentTimeMillis(); + mInterpreter.startProperty(); + mTimeStartProperty += System.currentTimeMillis() - start; + } + ended = parseItem(); + if (mInterpreter != null && !ended) { + long start = System.currentTimeMillis(); + mInterpreter.endProperty(); + mTimeEndProperty += System.currentTimeMillis() - start; + } + + while (!ended) { + // follow VCARD ,it wont reach endProperty + if (mInterpreter != null) { + long start = System.currentTimeMillis(); + mInterpreter.startProperty(); + mTimeStartProperty += System.currentTimeMillis() - start; + } + try { + ended = parseItem(); + } catch (VCardInvalidCommentLineException e) { + Log.e(LOG_TAG, "Invalid line which looks like some comment was found. Ignored."); + ended = false; + } + if (mInterpreter != null && !ended) { + long start = System.currentTimeMillis(); + mInterpreter.endProperty(); + mTimeEndProperty += System.currentTimeMillis() - start; + } + } + } + + /* + * item = [groups "."] name [params] ":" value CRLF / [groups "."] "ADR" + * [params] ":" addressparts CRLF / [groups "."] "ORG" [params] ":" orgparts + * CRLF / [groups "."] "N" [params] ":" nameparts CRLF / [groups "."] + * "AGENT" [params] ":" vcard CRLF + */ + protected boolean parseItem() throws IOException, VCardException { + mCurrentEncoding = sDefaultEncoding; + + final String line = getNonEmptyLine(); + long start = System.currentTimeMillis(); + + String[] propertyNameAndValue = separateLineAndHandleGroup(line); + if (propertyNameAndValue == null) { + return true; + } + if (propertyNameAndValue.length != 2) { + throw new VCardInvalidLineException("Invalid line \"" + line + "\""); + } + String propertyName = propertyNameAndValue[0].toUpperCase(); + String propertyValue = propertyNameAndValue[1]; + + mTimeParseLineAndHandleGroup += System.currentTimeMillis() - start; + + if (propertyName.equals("ADR") || propertyName.equals("ORG") || propertyName.equals("N")) { + start = System.currentTimeMillis(); + handleMultiplePropertyValue(propertyName, propertyValue); + mTimeParseAdrOrgN += System.currentTimeMillis() - start; + return false; + } else if (propertyName.equals("AGENT")) { + handleAgent(propertyValue); + return false; + } else if (isValidPropertyName(propertyName)) { + if (propertyName.equals("BEGIN")) { + if (propertyValue.equals("VCARD")) { + throw new VCardNestedException("This vCard has nested vCard data in it."); + } else { + throw new VCardException("Unknown BEGIN type: " + propertyValue); + } + } else if (propertyName.equals("VERSION") && !propertyValue.equals(getVersionString())) { + throw new VCardVersionException("Incompatible version: " + propertyValue + " != " + + getVersionString()); + } + start = System.currentTimeMillis(); + handlePropertyValue(propertyName, propertyValue); + mTimeParsePropertyValues += System.currentTimeMillis() - start; + return false; + } + + throw new VCardException("Unknown property name: \"" + propertyName + "\""); + } + + // For performance reason, the states for group and property name are merged into one. + static private final int STATE_GROUP_OR_PROPERTY_NAME = 0; + static private final int STATE_PARAMS = 1; + // vCard 3.0 specification allows double-quoted parameters, while vCard 2.1 does not. + static private final int STATE_PARAMS_IN_DQUOTE = 2; + + protected String[] separateLineAndHandleGroup(String line) throws VCardException { + final String[] propertyNameAndValue = new String[2]; + final int length = line.length(); + if (length > 0 && line.charAt(0) == '#') { + throw new VCardInvalidCommentLineException(); + } + + int state = STATE_GROUP_OR_PROPERTY_NAME; + int nameIndex = 0; + + // This loop is developed so that we don't have to take care of bottle neck here. + // Refactor carefully when you need to do so. + for (int i = 0; i < length; i++) { + final char ch = line.charAt(i); + switch (state) { + case STATE_GROUP_OR_PROPERTY_NAME: { + if (ch == ':') { // End of a property name. + final String propertyName = line.substring(nameIndex, i); + if (propertyName.equalsIgnoreCase("END")) { + mPreviousLine = line; + return null; + } + if (mInterpreter != null) { + mInterpreter.propertyName(propertyName); + } + propertyNameAndValue[0] = propertyName; + if (i < length - 1) { + propertyNameAndValue[1] = line.substring(i + 1); + } else { + propertyNameAndValue[1] = ""; + } + return propertyNameAndValue; + } else if (ch == '.') { // Each group is followed by the dot. + final String groupName = line.substring(nameIndex, i); + if (groupName.length() == 0) { + Log.w(LOG_TAG, "Empty group found. Ignoring."); + } else if (mInterpreter != null) { + mInterpreter.propertyGroup(groupName); + } + nameIndex = i + 1; // Next should be another group or a property name. + } else if (ch == ';') { // End of property name and beginneng of parameters. + final String propertyName = line.substring(nameIndex, i); + if (propertyName.equalsIgnoreCase("END")) { + mPreviousLine = line; + return null; + } + if (mInterpreter != null) { + mInterpreter.propertyName(propertyName); + } + propertyNameAndValue[0] = propertyName; + nameIndex = i + 1; + state = STATE_PARAMS; // Start parameter parsing. + } + break; + } + case STATE_PARAMS: { + if (ch == '"') { + if (VCardConstants.VERSION_V21.equalsIgnoreCase(getVersionString())) { + Log.w(LOG_TAG, "Double-quoted params found in vCard 2.1. " + + "Silently allow it"); + } + state = STATE_PARAMS_IN_DQUOTE; + } else if (ch == ';') { // Starts another param. + handleParams(line.substring(nameIndex, i)); + nameIndex = i + 1; + } else if (ch == ':') { // End of param and beginenning of values. + handleParams(line.substring(nameIndex, i)); + if (i < length - 1) { + propertyNameAndValue[1] = line.substring(i + 1); + } else { + propertyNameAndValue[1] = ""; + } + return propertyNameAndValue; + } + break; + } + case STATE_PARAMS_IN_DQUOTE: { + if (ch == '"') { + if (VCardConstants.VERSION_V21.equalsIgnoreCase(getVersionString())) { + Log.w(LOG_TAG, "Double-quoted params found in vCard 2.1. " + + "Silently allow it"); + } + state = STATE_PARAMS; + } + break; + } + } + } + + throw new VCardInvalidLineException("Invalid line: \"" + line + "\""); + } + + /* + * params = ";" [ws] paramlist paramlist = paramlist [ws] ";" [ws] param / + * param param = "TYPE" [ws] "=" [ws] ptypeval / "VALUE" [ws] "=" [ws] + * pvalueval / "ENCODING" [ws] "=" [ws] pencodingval / "CHARSET" [ws] "=" + * [ws] charsetval / "LANGUAGE" [ws] "=" [ws] langval / "X-" word [ws] "=" + * [ws] word / knowntype + */ + protected void handleParams(String params) throws VCardException { + final String[] strArray = params.split("=", 2); + if (strArray.length == 2) { + final String paramName = strArray[0].trim().toUpperCase(); + String paramValue = strArray[1].trim(); + if (paramName.equals("TYPE")) { + handleType(paramValue); + } else if (paramName.equals("VALUE")) { + handleValue(paramValue); + } else if (paramName.equals("ENCODING")) { + handleEncoding(paramValue); + } else if (paramName.equals("CHARSET")) { + handleCharset(paramValue); + } else if (paramName.equals("LANGUAGE")) { + handleLanguage(paramValue); + } else if (paramName.startsWith("X-")) { + handleAnyParam(paramName, paramValue); + } else { + throw new VCardException("Unknown type \"" + paramName + "\""); + } + } else { + handleParamWithoutName(strArray[0]); + } + } + + /** + * vCard 3.0 parser implementation may throw VCardException. + */ + @SuppressWarnings("unused") + protected void handleParamWithoutName(final String paramValue) throws VCardException { + handleType(paramValue); + } + + /* + * ptypeval = knowntype / "X-" word + */ + protected void handleType(final String ptypeval) { + if (!(getKnownTypeSet().contains(ptypeval.toUpperCase()) + || ptypeval.startsWith("X-")) + && !mUnknownTypeSet.contains(ptypeval)) { + mUnknownTypeSet.add(ptypeval); + Log.w(LOG_TAG, String.format("TYPE unsupported by %s: ", getVersion(), ptypeval)); + } + if (mInterpreter != null) { + mInterpreter.propertyParamType("TYPE"); + mInterpreter.propertyParamValue(ptypeval); + } + } + + /* + * pvalueval = "INLINE" / "URL" / "CONTENT-ID" / "CID" / "X-" word + */ + protected void handleValue(final String pvalueval) { + if (!(getKnownValueSet().contains(pvalueval.toUpperCase()) + || pvalueval.startsWith("X-") + || mUnknownValueSet.contains(pvalueval))) { + mUnknownValueSet.add(pvalueval); + Log.w(LOG_TAG, String.format( + "The value unsupported by TYPE of %s: ", getVersion(), pvalueval)); + } + if (mInterpreter != null) { + mInterpreter.propertyParamType("VALUE"); + mInterpreter.propertyParamValue(pvalueval); + } + } + + /* + * pencodingval = "7BIT" / "8BIT" / "QUOTED-PRINTABLE" / "BASE64" / "X-" word + */ + protected void handleEncoding(String pencodingval) throws VCardException { + if (getAvailableEncodingSet().contains(pencodingval) || + pencodingval.startsWith("X-")) { + if (mInterpreter != null) { + mInterpreter.propertyParamType("ENCODING"); + mInterpreter.propertyParamValue(pencodingval); + } + mCurrentEncoding = pencodingval; + } else { + throw new VCardException("Unknown encoding \"" + pencodingval + "\""); + } + } + + /** + * <p> + * vCard 2.1 specification only allows us-ascii and iso-8859-xxx (See RFC 1521), + * but recent vCard files often contain other charset like UTF-8, SHIFT_JIS, etc. + * We allow any charset. + * </p> + */ + protected void handleCharset(String charsetval) { + if (mInterpreter != null) { + mInterpreter.propertyParamType("CHARSET"); + mInterpreter.propertyParamValue(charsetval); + } + } + + /** + * See also Section 7.1 of RFC 1521 + */ + protected void handleLanguage(String langval) throws VCardException { + String[] strArray = langval.split("-"); + if (strArray.length != 2) { + throw new VCardException("Invalid Language: \"" + langval + "\""); + } + String tmp = strArray[0]; + int length = tmp.length(); + for (int i = 0; i < length; i++) { + if (!isAsciiLetter(tmp.charAt(i))) { + throw new VCardException("Invalid Language: \"" + langval + "\""); + } + } + tmp = strArray[1]; + length = tmp.length(); + for (int i = 0; i < length; i++) { + if (!isAsciiLetter(tmp.charAt(i))) { + throw new VCardException("Invalid Language: \"" + langval + "\""); + } + } + if (mInterpreter != null) { + mInterpreter.propertyParamType("LANGUAGE"); + mInterpreter.propertyParamValue(langval); + } + } + + private boolean isAsciiLetter(char ch) { + if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { + return true; + } + return false; + } + + /** + * Mainly for "X-" type. This accepts any kind of type without check. + */ + protected void handleAnyParam(String paramName, String paramValue) { + if (mInterpreter != null) { + mInterpreter.propertyParamType(paramName); + mInterpreter.propertyParamValue(paramValue); + } + } + + protected void handlePropertyValue(String propertyName, String propertyValue) + throws IOException, VCardException { + final String upperEncoding = mCurrentEncoding.toUpperCase(); + if (upperEncoding.equals(VCardConstants.PARAM_ENCODING_QP)) { + final long start = System.currentTimeMillis(); + final String result = getQuotedPrintable(propertyValue); + if (mInterpreter != null) { + ArrayList<String> v = new ArrayList<String>(); + v.add(result); + mInterpreter.propertyValues(v); + } + mTimeHandleQuotedPrintable += System.currentTimeMillis() - start; + } else if (upperEncoding.equals(VCardConstants.PARAM_ENCODING_BASE64) + || upperEncoding.equals(VCardConstants.PARAM_ENCODING_B)) { + final long start = System.currentTimeMillis(); + // It is very rare, but some BASE64 data may be so big that + // OutOfMemoryError occurs. To ignore such cases, use try-catch. + try { + final String result = getBase64(propertyValue); + if (mInterpreter != null) { + ArrayList<String> v = new ArrayList<String>(); + v.add(result); + mInterpreter.propertyValues(v); + } + } catch (OutOfMemoryError error) { + Log.e(LOG_TAG, "OutOfMemoryError happened during parsing BASE64 data!"); + if (mInterpreter != null) { + mInterpreter.propertyValues(null); + } + } + mTimeHandleBase64 += System.currentTimeMillis() - start; + } else { + if (!(upperEncoding.equals("7BIT") || upperEncoding.equals("8BIT") || + upperEncoding.startsWith("X-"))) { + Log.w(LOG_TAG, + String.format("The encoding \"%s\" is unsupported by vCard %s", + mCurrentEncoding, getVersionString())); + } + + final long start = System.currentTimeMillis(); + if (mInterpreter != null) { + ArrayList<String> v = new ArrayList<String>(); + v.add(maybeUnescapeText(propertyValue)); + mInterpreter.propertyValues(v); + } + mTimeHandleMiscPropertyValue += System.currentTimeMillis() - start; + } + } + + /** + * <p> + * Parses and returns Quoted-Printable. + * </p> + * + * @param firstString The string following a parameter name and attributes. + * Example: "string" in + * "ADR:ENCODING=QUOTED-PRINTABLE:string\n\r". + * @return whole Quoted-Printable string, including a given argument and + * following lines. Excludes the last empty line following to Quoted + * Printable lines. + * @throws IOException + * @throws VCardException + */ + private String getQuotedPrintable(String firstString) throws IOException, VCardException { + // Specifically, there may be some padding between = and CRLF. + // See the following: + // + // qp-line := *(qp-segment transport-padding CRLF) + // qp-part transport-padding + // qp-segment := qp-section *(SPACE / TAB) "=" + // ; Maximum length of 76 characters + // + // e.g. (from RFC 2045) + // Now's the time = + // for all folk to come= + // to the aid of their country. + if (firstString.trim().endsWith("=")) { + // remove "transport-padding" + int pos = firstString.length() - 1; + while (firstString.charAt(pos) != '=') { + } + StringBuilder builder = new StringBuilder(); + builder.append(firstString.substring(0, pos + 1)); + builder.append("\r\n"); + String line; + while (true) { + line = getLine(); + if (line == null) { + throw new VCardException("File ended during parsing a Quoted-Printable String"); + } + if (line.trim().endsWith("=")) { + // remove "transport-padding" + pos = line.length() - 1; + while (line.charAt(pos) != '=') { + } + builder.append(line.substring(0, pos + 1)); + builder.append("\r\n"); + } else { + builder.append(line); + break; + } + } + return builder.toString(); + } else { + return firstString; + } + } + + protected String getBase64(String firstString) throws IOException, VCardException { + StringBuilder builder = new StringBuilder(); + builder.append(firstString); + + while (true) { + String line = getLine(); + if (line == null) { + throw new VCardException("File ended during parsing BASE64 binary"); + } + if (line.length() == 0) { + break; + } + builder.append(line); + } + + return builder.toString(); + } + + /** + * <p> + * Mainly for "ADR", "ORG", and "N" + * </p> + */ + /* + * addressparts = 0*6(strnosemi ";") strnosemi ; PO Box, Extended Addr, + * Street, Locality, Region, Postal Code, Country Name orgparts = + * *(strnosemi ";") strnosemi ; First is Organization Name, remainder are + * Organization Units. nameparts = 0*4(strnosemi ";") strnosemi ; Family, + * Given, Middle, Prefix, Suffix. ; Example:Public;John;Q.;Reverend Dr.;III, + * Esq. strnosemi = *(*nonsemi ("\;" / "\" CRLF)) *nonsemi ; To include a + * semicolon in this string, it must be escaped ; with a "\" character. We + * do not care the number of "strnosemi" here. We are not sure whether we + * should add "\" CRLF to each value. We exclude them for now. + */ + protected void handleMultiplePropertyValue(String propertyName, String propertyValue) + throws IOException, VCardException { + // vCard 2.1 does not allow QUOTED-PRINTABLE here, but some + // softwares/devices + // emit such data. + if (mCurrentEncoding.equalsIgnoreCase("QUOTED-PRINTABLE")) { + propertyValue = getQuotedPrintable(propertyValue); + } + + if (mInterpreter != null) { + mInterpreter.propertyValues(VCardUtils.constructListFromValue(propertyValue, + (getVersion() == VCardConfig.FLAG_V30))); + } + } + + /* + * vCard 2.1 specifies AGENT allows one vcard entry. Currently we emit an + * error toward the AGENT property. + * // TODO: Support AGENT property. + * item = + * ... / [groups "."] "AGENT" [params] ":" vcard CRLF vcard = "BEGIN" [ws] + * ":" [ws] "VCARD" [ws] 1*CRLF items *CRLF "END" [ws] ":" [ws] "VCARD" + */ + protected void handleAgent(final String propertyValue) throws VCardException { + if (!propertyValue.toUpperCase().contains("BEGIN:VCARD")) { + // Apparently invalid line seen in Windows Mobile 6.5. Ignore them. + return; + } else { + throw new VCardAgentNotSupportedException("AGENT Property is not supported now."); + } + } + + /** + * For vCard 3.0. + */ + protected String maybeUnescapeText(final String text) { + return text; + } + + /** + * Returns unescaped String if the character should be unescaped. Return + * null otherwise. e.g. In vCard 2.1, "\;" should be unescaped into ";" + * while "\x" should not be. + */ + protected String maybeUnescapeCharacter(final char ch) { + return unescapeCharacter(ch); + } + + /* package */ static String unescapeCharacter(final char ch) { + // Original vCard 2.1 specification does not allow transformation + // "\:" -> ":", "\," -> ",", and "\\" -> "\", but previous + // implementation of + // this class allowed them, so keep it as is. + if (ch == '\\' || ch == ';' || ch == ':' || ch == ',') { + return String.valueOf(ch); + } else { + return null; + } + } + + private void showPerformanceInfo() { + Log.d(LOG_TAG, "Total parsing time: " + mTimeTotal + " ms"); + if (mReader instanceof CustomBufferedReader) { + Log.d(LOG_TAG, "Total readLine time: " + + ((CustomBufferedReader) mReader).getTotalmillisecond() + " ms"); + } + Log.d(LOG_TAG, "Time for handling the beggining of the record: " + mTimeReadStartRecord + + " ms"); + Log.d(LOG_TAG, "Time for handling the end of the record: " + mTimeReadEndRecord + " ms"); + Log.d(LOG_TAG, "Time for parsing line, and handling group: " + mTimeParseLineAndHandleGroup + + " ms"); + Log.d(LOG_TAG, "Time for parsing ADR, ORG, and N fields:" + mTimeParseAdrOrgN + " ms"); + Log.d(LOG_TAG, "Time for parsing property values: " + mTimeParsePropertyValues + " ms"); + Log.d(LOG_TAG, "Time for handling normal property values: " + mTimeHandleMiscPropertyValue + + " ms"); + Log.d(LOG_TAG, "Time for handling Quoted-Printable: " + mTimeHandleQuotedPrintable + " ms"); + Log.d(LOG_TAG, "Time for handling Base64: " + mTimeHandleBase64 + " ms"); + } + + /** + * @return {@link VCardConfig#FLAG_V21} + */ + protected int getVersion() { + return VCardConfig.FLAG_V21; + } + + /** + * @return {@link VCardConfig#FLAG_V30} + */ + protected String getVersionString() { + return VCardConstants.VERSION_V21; + } + + protected Set<String> getKnownPropertyNameSet() { + return VCardParser_V21.sKnownPropertyNameSet; + } + + protected Set<String> getKnownTypeSet() { + return VCardParser_V21.sKnownTypeSet; + } + + protected Set<String> getKnownValueSet() { + return VCardParser_V21.sKnownValueSet; + } + + protected Set<String> getAvailableEncodingSet() { + return VCardParser_V21.sAvailableEncoding; + } + + protected String getDefaultEncoding() { + return sDefaultEncoding; + } + + + public boolean parse(InputStream is, String inputCharset, VCardInterpreter interpreter) + throws IOException, VCardException { + if (is == null) { + throw new NullPointerException("InputStream must not be null."); + } + if (inputCharset == null) { + inputCharset = VCardConfig.DEFAULT_TEMPORARY_CHARSET; + } + + final InputStreamReader tmpReader = new InputStreamReader(is, inputCharset); + if (VCardConfig.showPerformanceLog()) { + mReader = new CustomBufferedReader(tmpReader); + } else { + mReader = new BufferedReader(tmpReader); + } + + mInterpreter = interpreter; + + long start = System.currentTimeMillis(); + if (mInterpreter != null) { + mInterpreter.start(); + } + parseVCardFile(); + if (mInterpreter != null) { + mInterpreter.end(); + } + mTimeTotal += System.currentTimeMillis() - start; + + if (VCardConfig.showPerformanceLog()) { + showPerformanceInfo(); + } + + return true; + } + + public boolean parse(InputStream is, String charset, + VCardInterpreter builder, boolean canceled) + throws IOException, VCardException { + mCanceled = canceled; + return parse(is, charset, builder); + } + + public final void cancel() { + mCanceled = true; + } +} diff --git a/core/java/android/pim/vcard/VCardParserImpl_V30.java b/core/java/android/pim/vcard/VCardParserImpl_V30.java new file mode 100644 index 0000000..fb6ee4f --- /dev/null +++ b/core/java/android/pim/vcard/VCardParserImpl_V30.java @@ -0,0 +1,315 @@ +/* + * Copyright (C) 2010 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.pim.vcard; + +import java.io.IOException; +import java.util.Set; + +import android.pim.vcard.exception.VCardException; +import android.util.Log; + +/** + * <p> + * Basic implementation achieving vCard 3.0 parsing. + * </p> + * <p> + * This class inherits vCard 2.1 implementation since technically they are similar, + * while specifically there's logical no relevance between them. + * So that developers are not confused with the inheritance, + * {@link VCardParser_V30} does not inherit {@link VCardParser_V21}, while + * {@link VCardParserImpl_V30} inherits {@link VCardParserImpl_V21}. + * </p> + */ +/* package */ class VCardParserImpl_V30 extends VCardParserImpl_V21 { + private static final String LOG_TAG = "VCardParserImpl_V30"; + + private String mPreviousLine; + private boolean mEmittedAgentWarning = false; + + public VCardParserImpl_V30() { + super(); + } + + public VCardParserImpl_V30(VCardSourceDetector detector) { + this(detector != null ? detector.getEstimatedType() : VCardConfig.PARSE_TYPE_UNKNOWN); + } + + public VCardParserImpl_V30(int parseMode) { + super(parseMode); + } + + @Override + protected int getVersion() { + return VCardConfig.FLAG_V30; + } + + @Override + protected String getVersionString() { + return VCardConstants.VERSION_V30; + } + + @Override + protected String getLine() throws IOException { + if (mPreviousLine != null) { + String ret = mPreviousLine; + mPreviousLine = null; + return ret; + } else { + return mReader.readLine(); + } + } + + /** + * vCard 3.0 requires that the line with space at the beginning of the line + * must be combined with previous line. + */ + @Override + protected String getNonEmptyLine() throws IOException, VCardException { + String line; + StringBuilder builder = null; + while (true) { + line = mReader.readLine(); + if (line == null) { + if (builder != null) { + return builder.toString(); + } else if (mPreviousLine != null) { + String ret = mPreviousLine; + mPreviousLine = null; + return ret; + } + throw new VCardException("Reached end of buffer."); + } else if (line.length() == 0) { + if (builder != null) { + return builder.toString(); + } else if (mPreviousLine != null) { + String ret = mPreviousLine; + mPreviousLine = null; + return ret; + } + } else if (line.charAt(0) == ' ' || line.charAt(0) == '\t') { + if (builder != null) { + // See Section 5.8.1 of RFC 2425 (MIME-DIR document). + // Following is the excerpts from it. + // + // DESCRIPTION:This is a long description that exists on a long line. + // + // Can be represented as: + // + // DESCRIPTION:This is a long description + // that exists on a long line. + // + // It could also be represented as: + // + // DESCRIPTION:This is a long descrip + // tion that exists o + // n a long line. + builder.append(line.substring(1)); + } else if (mPreviousLine != null) { + builder = new StringBuilder(); + builder.append(mPreviousLine); + mPreviousLine = null; + builder.append(line.substring(1)); + } else { + throw new VCardException("Space exists at the beginning of the line"); + } + } else { + if (mPreviousLine == null) { + mPreviousLine = line; + if (builder != null) { + return builder.toString(); + } + } else { + String ret = mPreviousLine; + mPreviousLine = line; + return ret; + } + } + } + } + + /* + * vcard = [group "."] "BEGIN" ":" "VCARD" 1 * CRLF + * 1 * (contentline) + * ;A vCard object MUST include the VERSION, FN and N types. + * [group "."] "END" ":" "VCARD" 1 * CRLF + */ + @Override + protected boolean readBeginVCard(boolean allowGarbage) throws IOException, VCardException { + // TODO: vCard 3.0 supports group. + return super.readBeginVCard(allowGarbage); + } + + @Override + protected void readEndVCard(boolean useCache, boolean allowGarbage) + throws IOException, VCardException { + // TODO: vCard 3.0 supports group. + super.readEndVCard(useCache, allowGarbage); + } + + /** + * vCard 3.0 allows iana-token as paramType, while vCard 2.1 does not. + */ + @Override + protected void handleParams(final String params) throws VCardException { + try { + super.handleParams(params); + } catch (VCardException e) { + // maybe IANA type + String[] strArray = params.split("=", 2); + if (strArray.length == 2) { + handleAnyParam(strArray[0], strArray[1]); + } else { + // Must not come here in the current implementation. + throw new VCardException( + "Unknown params value: " + params); + } + } + } + + @Override + protected void handleAnyParam(final String paramName, final String paramValue) { + super.handleAnyParam(paramName, paramValue); + } + + @Override + protected void handleParamWithoutName(final String paramValue) throws VCardException { + super.handleParamWithoutName(paramValue); + } + + /* + * vCard 3.0 defines + * + * param = param-name "=" param-value *("," param-value) + * param-name = iana-token / x-name + * param-value = ptext / quoted-string + * quoted-string = DQUOTE QSAFE-CHAR DQUOTE + */ + @Override + protected void handleType(final String ptypevalues) { + String[] ptypeArray = ptypevalues.split(","); + mInterpreter.propertyParamType("TYPE"); + for (String value : ptypeArray) { + int length = value.length(); + if (length >= 2 && value.startsWith("\"") && value.endsWith("\"")) { + mInterpreter.propertyParamValue(value.substring(1, value.length() - 1)); + } else { + mInterpreter.propertyParamValue(value); + } + } + } + + @Override + protected void handleAgent(final String propertyValue) { + // The way how vCard 3.0 supports "AGENT" is completely different from vCard 2.1. + // + // e.g. + // AGENT:BEGIN:VCARD\nFN:Joe Friday\nTEL:+1-919-555-7878\n + // TITLE:Area Administrator\, Assistant\n EMAIL\;TYPE=INTERN\n + // ET:jfriday@host.com\nEND:VCARD\n + // + // TODO: fix this. + // + // issue: + // vCard 3.0 also allows this as an example. + // + // AGENT;VALUE=uri: + // CID:JQPUBLIC.part3.960129T083020.xyzMail@host3.com + // + // This is not vCard. Should we support this? + // + // Just ignore the line for now, since we cannot know how to handle it... + if (!mEmittedAgentWarning) { + Log.w(LOG_TAG, "AGENT in vCard 3.0 is not supported yet. Ignore it"); + mEmittedAgentWarning = true; + } + } + + /** + * vCard 3.0 does not require two CRLF at the last of BASE64 data. + * It only requires that data should be MIME-encoded. + */ + @Override + protected String getBase64(final String firstString) + throws IOException, VCardException { + final StringBuilder builder = new StringBuilder(); + builder.append(firstString); + + while (true) { + final String line = getLine(); + if (line == null) { + throw new VCardException("File ended during parsing BASE64 binary"); + } + if (line.length() == 0) { + break; + } else if (!line.startsWith(" ") && !line.startsWith("\t")) { + mPreviousLine = line; + break; + } + builder.append(line); + } + + return builder.toString(); + } + + /** + * ESCAPED-CHAR = "\\" / "\;" / "\," / "\n" / "\N") + * ; \\ encodes \, \n or \N encodes newline + * ; \; encodes ;, \, encodes , + * + * Note: Apple escapes ':' into '\:' while does not escape '\' + */ + @Override + protected String maybeUnescapeText(final String text) { + return unescapeText(text); + } + + public static String unescapeText(final String text) { + StringBuilder builder = new StringBuilder(); + final int length = text.length(); + for (int i = 0; i < length; i++) { + char ch = text.charAt(i); + if (ch == '\\' && i < length - 1) { + final char next_ch = text.charAt(++i); + if (next_ch == 'n' || next_ch == 'N') { + builder.append("\n"); + } else { + builder.append(next_ch); + } + } else { + builder.append(ch); + } + } + return builder.toString(); + } + + @Override + protected String maybeUnescapeCharacter(final char ch) { + return unescapeCharacter(ch); + } + + public static String unescapeCharacter(final char ch) { + if (ch == 'n' || ch == 'N') { + return "\n"; + } else { + return String.valueOf(ch); + } + } + + @Override + protected Set<String> getKnownPropertyNameSet() { + return VCardParser_V30.sKnownPropertyNameSet; + } +} diff --git a/core/java/android/pim/vcard/VCardParser_V21.java b/core/java/android/pim/vcard/VCardParser_V21.java index fe8cfb0..f7e496a 100644 --- a/core/java/android/pim/vcard/VCardParser_V21.java +++ b/core/java/android/pim/vcard/VCardParser_V21.java @@ -15,922 +15,112 @@ */ package android.pim.vcard; -import android.pim.vcard.exception.VCardAgentNotSupportedException; import android.pim.vcard.exception.VCardException; -import android.pim.vcard.exception.VCardInvalidCommentLineException; -import android.pim.vcard.exception.VCardInvalidLineException; -import android.pim.vcard.exception.VCardNestedException; -import android.pim.vcard.exception.VCardVersionException; -import android.util.Log; -import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; import java.util.Set; /** - * This class is used to parse vCard. Please refer to vCard Specification 2.1 for more detail. + * </p> + * vCard parser for vCard 2.1. See the specification for more detail about the spec itself. + * </p> + * <p> + * The spec is written in 1996, and currently various types of "vCard 2.1" exist. + * To handle real the world vCard formats appropriately and effectively, this class does not + * obey with strict vCard 2.1. + * In stead, not only vCard spec but also real world vCard is considered. + * </p> + * e.g. A lot of devices and softwares let vCard importer/exporter to use + * the PNG format to determine the type of image, while it is not allowed in + * the original specification. As of 2010, we can see even the FLV format + * (possible in Japanese mobile phones). + * </p> */ -public class VCardParser_V21 extends VCardParser { - private static final String LOG_TAG = "VCardParser_V21"; - - /** Store the known-type */ - private static final HashSet<String> sKnownTypeSet = new HashSet<String>( - Arrays.asList("DOM", "INTL", "POSTAL", "PARCEL", "HOME", "WORK", - "PREF", "VOICE", "FAX", "MSG", "CELL", "PAGER", "BBS", - "MODEM", "CAR", "ISDN", "VIDEO", "AOL", "APPLELINK", - "ATTMAIL", "CIS", "EWORLD", "INTERNET", "IBMMAIL", - "MCIMAIL", "POWERSHARE", "PRODIGY", "TLX", "X400", "GIF", - "CGM", "WMF", "BMP", "MET", "PMB", "DIB", "PICT", "TIFF", - "PDF", "PS", "JPEG", "QTIME", "MPEG", "MPEG2", "AVI", - "WAVE", "AIFF", "PCM", "X509", "PGP")); - - /** Store the known-value */ - private static final HashSet<String> sKnownValueSet = new HashSet<String>( - Arrays.asList("INLINE", "URL", "CONTENT-ID", "CID")); - - /** Store the property names available in vCard 2.1 */ - private static final HashSet<String> sAvailablePropertyNameSetV21 = - new HashSet<String>(Arrays.asList( - "BEGIN", "LOGO", "PHOTO", "LABEL", "FN", "TITLE", "SOUND", - "VERSION", "TEL", "EMAIL", "TZ", "GEO", "NOTE", "URL", - "BDAY", "ROLE", "REV", "UID", "KEY", "MAILER")); - - /** - * Though vCard 2.1 specification does not allow "B" encoding, some data may have it. - * We allow it for safety... - */ - private static final HashSet<String> sAvailableEncodingV21 = - new HashSet<String>(Arrays.asList( - "7BIT", "8BIT", "QUOTED-PRINTABLE", "BASE64", "B")); - - // Used only for parsing END:VCARD. - private String mPreviousLine; - - /** The builder to build parsed data */ - protected VCardInterpreter mBuilder = null; - - /** - * The encoding type. "Encoding" in vCard is different from "Charset". - * e.g. 7BIT, 8BIT, QUOTED-PRINTABLE. - */ - protected String mEncoding = null; - - protected final String sDefaultEncoding = "8BIT"; - - // Should not directly read a line from this object. Use getLine() instead. - protected BufferedReader mReader; - - // In some cases, vCard is nested. Currently, we only consider the most interior vCard data. - // See v21_foma_1.vcf in test directory for more information. - private int mNestCount; - - // In order to reduce warning message as much as possible, we hold the value which made Logger - // emit a warning message. - protected Set<String> mUnknownTypeMap = new HashSet<String>(); - protected Set<String> mUnknownValueMap = new HashSet<String>(); - - // For measuring performance. - private long mTimeTotal; - private long mTimeReadStartRecord; - private long mTimeReadEndRecord; - private long mTimeStartProperty; - private long mTimeEndProperty; - private long mTimeParseItems; - private long mTimeParseLineAndHandleGroup; - private long mTimeParsePropertyValues; - private long mTimeParseAdrOrgN; - private long mTimeHandleMiscPropertyValue; - private long mTimeHandleQuotedPrintable; - private long mTimeHandleBase64; - - public VCardParser_V21() { - this(null); - } - - public VCardParser_V21(VCardSourceDetector detector) { - this(detector != null ? detector.getEstimatedType() : VCardConfig.PARSE_TYPE_UNKNOWN); - } - - public VCardParser_V21(int parseType) { - super(parseType); - if (parseType == VCardConfig.PARSE_TYPE_FOMA) { - mNestCount = 1; - } - } - - /** - * Parses the file at the given position. - * - * vcard_file = [wsls] vcard [wsls] - */ - protected void parseVCardFile() throws IOException, VCardException { - boolean firstReading = true; - while (true) { - if (mCanceled) { - break; - } - if (!parseOneVCard(firstReading)) { - break; - } - firstReading = false; - } - - if (mNestCount > 0) { - boolean useCache = true; - for (int i = 0; i < mNestCount; i++) { - readEndVCard(useCache, true); - useCache = false; - } - } - } - - protected int getVersion() { - return VCardConfig.FLAG_V21; - } - - protected String getVersionString() { - return VCardConstants.VERSION_V21; - } - +public final class VCardParser_V21 implements VCardParser { /** - * @return true when the propertyName is a valid property name. + * A unmodifiable Set storing the property names available in the vCard 2.1 specification. */ - protected boolean isValidPropertyName(String propertyName) { - if (!(sAvailablePropertyNameSetV21.contains(propertyName.toUpperCase()) || - propertyName.startsWith("X-")) && - !mUnknownTypeMap.contains(propertyName)) { - mUnknownTypeMap.add(propertyName); - Log.w(LOG_TAG, "Property name unsupported by vCard 2.1: " + propertyName); - } - return true; - } + /* package */ static final Set<String> sKnownPropertyNameSet = + Collections.unmodifiableSet(new HashSet<String>( + Arrays.asList("BEGIN", "LOGO", "PHOTO", "LABEL", "FN", "TITLE", "SOUND", + "VERSION", "TEL", "EMAIL", "TZ", "GEO", "NOTE", "URL", + "BDAY", "ROLE", "REV", "UID", "KEY", "MAILER"))); /** - * @return true when the encoding is a valid encoding. - */ - protected boolean isValidEncoding(String encoding) { - return sAvailableEncodingV21.contains(encoding.toUpperCase()); - } - - /** - * @return String. It may be null, or its length may be 0 - * @throws IOException + * A unmodifiable Set storing the types known in vCard 2.1. */ - protected String getLine() throws IOException { - return mReader.readLine(); - } - - /** - * @return String with it's length > 0 - * @throws IOException - * @throws VCardException when the stream reached end of line - */ - protected String getNonEmptyLine() throws IOException, VCardException { - String line; - while (true) { - line = getLine(); - if (line == null) { - throw new VCardException("Reached end of buffer."); - } else if (line.trim().length() > 0) { - return line; - } - } - } + /* package */ static final Set<String> sKnownTypeSet = + Collections.unmodifiableSet(new HashSet<String>( + Arrays.asList("DOM", "INTL", "POSTAL", "PARCEL", "HOME", "WORK", + "PREF", "VOICE", "FAX", "MSG", "CELL", "PAGER", "BBS", + "MODEM", "CAR", "ISDN", "VIDEO", "AOL", "APPLELINK", + "ATTMAIL", "CIS", "EWORLD", "INTERNET", "IBMMAIL", + "MCIMAIL", "POWERSHARE", "PRODIGY", "TLX", "X400", "GIF", + "CGM", "WMF", "BMP", "MET", "PMB", "DIB", "PICT", "TIFF", + "PDF", "PS", "JPEG", "QTIME", "MPEG", "MPEG2", "AVI", + "WAVE", "AIFF", "PCM", "X509", "PGP"))); /** - * vcard = "BEGIN" [ws] ":" [ws] "VCARD" [ws] 1*CRLF - * items *CRLF - * "END" [ws] ":" [ws] "VCARD" + * A unmodifiable Set storing the values for the type "VALUE", available in the vCard 2.1. */ - private boolean parseOneVCard(boolean firstReading) throws IOException, VCardException { - boolean allowGarbage = false; - if (firstReading) { - if (mNestCount > 0) { - for (int i = 0; i < mNestCount; i++) { - if (!readBeginVCard(allowGarbage)) { - return false; - } - allowGarbage = true; - } - } - } - - if (!readBeginVCard(allowGarbage)) { - return false; - } - long start; - if (mBuilder != null) { - start = System.currentTimeMillis(); - mBuilder.startEntry(); - mTimeReadStartRecord += System.currentTimeMillis() - start; - } - start = System.currentTimeMillis(); - parseItems(); - mTimeParseItems += System.currentTimeMillis() - start; - readEndVCard(true, false); - if (mBuilder != null) { - start = System.currentTimeMillis(); - mBuilder.endEntry(); - mTimeReadEndRecord += System.currentTimeMillis() - start; - } - return true; - } - - /** - * @return True when successful. False when reaching the end of line - * @throws IOException - * @throws VCardException - */ - protected boolean readBeginVCard(boolean allowGarbage) throws IOException, VCardException { - String line; - do { - while (true) { - line = getLine(); - if (line == null) { - return false; - } else if (line.trim().length() > 0) { - break; - } - } - String[] strArray = line.split(":", 2); - int length = strArray.length; - - // Though vCard 2.1/3.0 specification does not allow lower cases, - // vCard file emitted by some external vCard expoter have such invalid Strings. - // So we allow it. - // e.g. BEGIN:vCard - if (length == 2 && - strArray[0].trim().equalsIgnoreCase("BEGIN") && - strArray[1].trim().equalsIgnoreCase("VCARD")) { - return true; - } else if (!allowGarbage) { - if (mNestCount > 0) { - mPreviousLine = line; - return false; - } else { - throw new VCardException( - "Expected String \"BEGIN:VCARD\" did not come " - + "(Instead, \"" + line + "\" came)"); - } - } - } while(allowGarbage); - - throw new VCardException("Reached where must not be reached."); - } + /* package */ static final Set<String> sKnownValueSet = + Collections.unmodifiableSet(new HashSet<String>( + Arrays.asList("INLINE", "URL", "CONTENT-ID", "CID"))); /** - * The arguments useCache and allowGarbase are usually true and false accordingly when - * this function is called outside this function itself. - * - * @param useCache When true, line is obtained from mPreviousline. Otherwise, getLine() - * is used. - * @param allowGarbage When true, ignore non "END:VCARD" line. - * @throws IOException - * @throws VCardException - */ - protected void readEndVCard(boolean useCache, boolean allowGarbage) - throws IOException, VCardException { - String line; - do { - if (useCache) { - // Though vCard specification does not allow lower cases, - // some data may have them, so we allow it. - line = mPreviousLine; - } else { - while (true) { - line = getLine(); - if (line == null) { - throw new VCardException("Expected END:VCARD was not found."); - } else if (line.trim().length() > 0) { - break; - } - } - } - - String[] strArray = line.split(":", 2); - if (strArray.length == 2 && - strArray[0].trim().equalsIgnoreCase("END") && - strArray[1].trim().equalsIgnoreCase("VCARD")) { - return; - } else if (!allowGarbage) { - throw new VCardException("END:VCARD != \"" + mPreviousLine + "\""); - } - useCache = false; - } while (allowGarbage); - } - - /** - * items = *CRLF item - * / item - */ - protected void parseItems() throws IOException, VCardException { - boolean ended = false; - - if (mBuilder != null) { - long start = System.currentTimeMillis(); - mBuilder.startProperty(); - mTimeStartProperty += System.currentTimeMillis() - start; - } - ended = parseItem(); - if (mBuilder != null && !ended) { - long start = System.currentTimeMillis(); - mBuilder.endProperty(); - mTimeEndProperty += System.currentTimeMillis() - start; - } - - while (!ended) { - // follow VCARD ,it wont reach endProperty - if (mBuilder != null) { - long start = System.currentTimeMillis(); - mBuilder.startProperty(); - mTimeStartProperty += System.currentTimeMillis() - start; - } - try { - ended = parseItem(); - } catch (VCardInvalidCommentLineException e) { - Log.e(LOG_TAG, "Invalid line which looks like some comment was found. Ignored."); - ended = false; - } - if (mBuilder != null && !ended) { - long start = System.currentTimeMillis(); - mBuilder.endProperty(); - mTimeEndProperty += System.currentTimeMillis() - start; - } - } - } - - /** - * item = [groups "."] name [params] ":" value CRLF - * / [groups "."] "ADR" [params] ":" addressparts CRLF - * / [groups "."] "ORG" [params] ":" orgparts CRLF - * / [groups "."] "N" [params] ":" nameparts CRLF - * / [groups "."] "AGENT" [params] ":" vcard CRLF + * <p> + * A unmodifiable Set storing the values for the type "ENCODING", available in the vCard 2.1. + * </p> + * <p> + * Though vCard 2.1 specification does not allow "B" encoding, some data may have it. + * We allow it for safety. + * </p> */ - protected boolean parseItem() throws IOException, VCardException { - mEncoding = sDefaultEncoding; - - final String line = getNonEmptyLine(); - long start = System.currentTimeMillis(); - - String[] propertyNameAndValue = separateLineAndHandleGroup(line); - if (propertyNameAndValue == null) { - return true; - } - if (propertyNameAndValue.length != 2) { - throw new VCardInvalidLineException("Invalid line \"" + line + "\""); - } - String propertyName = propertyNameAndValue[0].toUpperCase(); - String propertyValue = propertyNameAndValue[1]; - - mTimeParseLineAndHandleGroup += System.currentTimeMillis() - start; - - if (propertyName.equals("ADR") || propertyName.equals("ORG") || - propertyName.equals("N")) { - start = System.currentTimeMillis(); - handleMultiplePropertyValue(propertyName, propertyValue); - mTimeParseAdrOrgN += System.currentTimeMillis() - start; - return false; - } else if (propertyName.equals("AGENT")) { - handleAgent(propertyValue); - return false; - } else if (isValidPropertyName(propertyName)) { - if (propertyName.equals("BEGIN")) { - if (propertyValue.equals("VCARD")) { - throw new VCardNestedException("This vCard has nested vCard data in it."); - } else { - throw new VCardException("Unknown BEGIN type: " + propertyValue); - } - } else if (propertyName.equals("VERSION") && - !propertyValue.equals(getVersionString())) { - throw new VCardVersionException("Incompatible version: " + - propertyValue + " != " + getVersionString()); - } - start = System.currentTimeMillis(); - handlePropertyValue(propertyName, propertyValue); - mTimeParsePropertyValues += System.currentTimeMillis() - start; - return false; - } - - throw new VCardException("Unknown property name: \"" + propertyName + "\""); - } - - static private final int STATE_GROUP_OR_PROPNAME = 0; - static private final int STATE_PARAMS = 1; - // vCard 3.0 specification allows double-quoted param-value, while vCard 2.1 does not. - // This is just for safety. - static private final int STATE_PARAMS_IN_DQUOTE = 2; - - protected String[] separateLineAndHandleGroup(String line) throws VCardException { - int state = STATE_GROUP_OR_PROPNAME; - int nameIndex = 0; - - final String[] propertyNameAndValue = new String[2]; + /* package */ static final Set<String> sAvailableEncoding = + Collections.unmodifiableSet(new HashSet<String>( + Arrays.asList(VCardConstants.PARAM_ENCODING_7BIT, + VCardConstants.PARAM_ENCODING_8BIT, + VCardConstants.PARAM_ENCODING_QP, + VCardConstants.PARAM_ENCODING_BASE64, + VCardConstants.PARAM_ENCODING_B))); - final int length = line.length(); - if (length > 0 && line.charAt(0) == '#') { - throw new VCardInvalidCommentLineException(); - } + private final VCardParserImpl_V21 mVCardParserImpl; - for (int i = 0; i < length; i++) { - char ch = line.charAt(i); - switch (state) { - case STATE_GROUP_OR_PROPNAME: { - if (ch == ':') { - final String propertyName = line.substring(nameIndex, i); - if (propertyName.equalsIgnoreCase("END")) { - mPreviousLine = line; - return null; - } - if (mBuilder != null) { - mBuilder.propertyName(propertyName); - } - propertyNameAndValue[0] = propertyName; - if (i < length - 1) { - propertyNameAndValue[1] = line.substring(i + 1); - } else { - propertyNameAndValue[1] = ""; - } - return propertyNameAndValue; - } else if (ch == '.') { - String groupName = line.substring(nameIndex, i); - if (mBuilder != null) { - mBuilder.propertyGroup(groupName); - } - nameIndex = i + 1; - } else if (ch == ';') { - String propertyName = line.substring(nameIndex, i); - if (propertyName.equalsIgnoreCase("END")) { - mPreviousLine = line; - return null; - } - if (mBuilder != null) { - mBuilder.propertyName(propertyName); - } - propertyNameAndValue[0] = propertyName; - nameIndex = i + 1; - state = STATE_PARAMS; - } - break; - } - case STATE_PARAMS: { - if (ch == '"') { - state = STATE_PARAMS_IN_DQUOTE; - } else if (ch == ';') { - handleParams(line.substring(nameIndex, i)); - nameIndex = i + 1; - } else if (ch == ':') { - handleParams(line.substring(nameIndex, i)); - if (i < length - 1) { - propertyNameAndValue[1] = line.substring(i + 1); - } else { - propertyNameAndValue[1] = ""; - } - return propertyNameAndValue; - } - break; - } - case STATE_PARAMS_IN_DQUOTE: { - if (ch == '"') { - state = STATE_PARAMS; - } - break; - } - } - } - - throw new VCardInvalidLineException("Invalid line: \"" + line + "\""); - } - - /** - * params = ";" [ws] paramlist - * paramlist = paramlist [ws] ";" [ws] param - * / param - * param = "TYPE" [ws] "=" [ws] ptypeval - * / "VALUE" [ws] "=" [ws] pvalueval - * / "ENCODING" [ws] "=" [ws] pencodingval - * / "CHARSET" [ws] "=" [ws] charsetval - * / "LANGUAGE" [ws] "=" [ws] langval - * / "X-" word [ws] "=" [ws] word - * / knowntype - */ - protected void handleParams(String params) throws VCardException { - String[] strArray = params.split("=", 2); - if (strArray.length == 2) { - final String paramName = strArray[0].trim().toUpperCase(); - String paramValue = strArray[1].trim(); - if (paramName.equals("TYPE")) { - handleType(paramValue); - } else if (paramName.equals("VALUE")) { - handleValue(paramValue); - } else if (paramName.equals("ENCODING")) { - handleEncoding(paramValue); - } else if (paramName.equals("CHARSET")) { - handleCharset(paramValue); - } else if (paramName.equals("LANGUAGE")) { - handleLanguage(paramValue); - } else if (paramName.startsWith("X-")) { - handleAnyParam(paramName, paramValue); - } else { - throw new VCardException("Unknown type \"" + paramName + "\""); - } - } else { - handleParamWithoutName(strArray[0]); - } - } - - /** - * vCard 3.0 parser may throw VCardException. - */ - @SuppressWarnings("unused") - protected void handleParamWithoutName(final String paramValue) throws VCardException { - handleType(paramValue); - } - - /** - * ptypeval = knowntype / "X-" word - */ - protected void handleType(final String ptypeval) { - String upperTypeValue = ptypeval; - if (!(sKnownTypeSet.contains(upperTypeValue) || upperTypeValue.startsWith("X-")) && - !mUnknownTypeMap.contains(ptypeval)) { - mUnknownTypeMap.add(ptypeval); - Log.w(LOG_TAG, "TYPE unsupported by vCard 2.1: " + ptypeval); - } - if (mBuilder != null) { - mBuilder.propertyParamType("TYPE"); - mBuilder.propertyParamValue(upperTypeValue); - } - } - - /** - * pvalueval = "INLINE" / "URL" / "CONTENT-ID" / "CID" / "X-" word - */ - protected void handleValue(final String pvalueval) { - if (!sKnownValueSet.contains(pvalueval.toUpperCase()) && - pvalueval.startsWith("X-") && - !mUnknownValueMap.contains(pvalueval)) { - mUnknownValueMap.add(pvalueval); - Log.w(LOG_TAG, "VALUE unsupported by vCard 2.1: " + pvalueval); - } - if (mBuilder != null) { - mBuilder.propertyParamType("VALUE"); - mBuilder.propertyParamValue(pvalueval); - } - } - - /** - * pencodingval = "7BIT" / "8BIT" / "QUOTED-PRINTABLE" / "BASE64" / "X-" word - */ - protected void handleEncoding(String pencodingval) throws VCardException { - if (isValidEncoding(pencodingval) || - pencodingval.startsWith("X-")) { - if (mBuilder != null) { - mBuilder.propertyParamType("ENCODING"); - mBuilder.propertyParamValue(pencodingval); - } - mEncoding = pencodingval; - } else { - throw new VCardException("Unknown encoding \"" + pencodingval + "\""); - } - } - - /** - * vCard 2.1 specification only allows us-ascii and iso-8859-xxx (See RFC 1521), - * but today's vCard often contains other charset, so we allow them. - */ - protected void handleCharset(String charsetval) { - if (mBuilder != null) { - mBuilder.propertyParamType("CHARSET"); - mBuilder.propertyParamValue(charsetval); - } + public VCardParser_V21() { + mVCardParserImpl = new VCardParserImpl_V21(); } - /** - * See also Section 7.1 of RFC 1521 - */ - protected void handleLanguage(String langval) throws VCardException { - String[] strArray = langval.split("-"); - if (strArray.length != 2) { - throw new VCardException("Invalid Language: \"" + langval + "\""); - } - String tmp = strArray[0]; - int length = tmp.length(); - for (int i = 0; i < length; i++) { - if (!isLetter(tmp.charAt(i))) { - throw new VCardException("Invalid Language: \"" + langval + "\""); - } - } - tmp = strArray[1]; - length = tmp.length(); - for (int i = 0; i < length; i++) { - if (!isLetter(tmp.charAt(i))) { - throw new VCardException("Invalid Language: \"" + langval + "\""); - } - } - if (mBuilder != null) { - mBuilder.propertyParamType("LANGUAGE"); - mBuilder.propertyParamValue(langval); - } + public VCardParser_V21(VCardSourceDetector detector) { + mVCardParserImpl = new VCardParserImpl_V21(detector); } - /** - * Mainly for "X-" type. This accepts any kind of type without check. - */ - protected void handleAnyParam(String paramName, String paramValue) { - if (mBuilder != null) { - mBuilder.propertyParamType(paramName); - mBuilder.propertyParamValue(paramValue); - } + public VCardParser_V21(int parseType) { + mVCardParserImpl = new VCardParserImpl_V21(parseType); } - protected void handlePropertyValue(String propertyName, String propertyValue) - throws IOException, VCardException { - if (mEncoding.equalsIgnoreCase("QUOTED-PRINTABLE")) { - final long start = System.currentTimeMillis(); - final String result = getQuotedPrintable(propertyValue); - if (mBuilder != null) { - ArrayList<String> v = new ArrayList<String>(); - v.add(result); - mBuilder.propertyValues(v); - } - mTimeHandleQuotedPrintable += System.currentTimeMillis() - start; - } else if (mEncoding.equalsIgnoreCase("BASE64") || - mEncoding.equalsIgnoreCase("B")) { - final long start = System.currentTimeMillis(); - // It is very rare, but some BASE64 data may be so big that - // OutOfMemoryError occurs. To ignore such cases, use try-catch. - try { - final String result = getBase64(propertyValue); - if (mBuilder != null) { - ArrayList<String> v = new ArrayList<String>(); - v.add(result); - mBuilder.propertyValues(v); - } - } catch (OutOfMemoryError error) { - Log.e(LOG_TAG, "OutOfMemoryError happened during parsing BASE64 data!"); - if (mBuilder != null) { - mBuilder.propertyValues(null); - } - } - mTimeHandleBase64 += System.currentTimeMillis() - start; - } else { - if (!(mEncoding == null || mEncoding.equalsIgnoreCase("7BIT") - || mEncoding.equalsIgnoreCase("8BIT") - || mEncoding.toUpperCase().startsWith("X-"))) { - Log.w(LOG_TAG, "The encoding unsupported by vCard spec: \"" + mEncoding + "\"."); - } + //// Implemented methods - final long start = System.currentTimeMillis(); - if (mBuilder != null) { - ArrayList<String> v = new ArrayList<String>(); - v.add(maybeUnescapeText(propertyValue)); - mBuilder.propertyValues(v); - } - mTimeHandleMiscPropertyValue += System.currentTimeMillis() - start; - } - } - - protected String getQuotedPrintable(String firstString) throws IOException, VCardException { - // Specifically, there may be some padding between = and CRLF. - // See the following: - // - // qp-line := *(qp-segment transport-padding CRLF) - // qp-part transport-padding - // qp-segment := qp-section *(SPACE / TAB) "=" - // ; Maximum length of 76 characters - // - // e.g. (from RFC 2045) - // Now's the time = - // for all folk to come= - // to the aid of their country. - if (firstString.trim().endsWith("=")) { - // remove "transport-padding" - int pos = firstString.length() - 1; - while(firstString.charAt(pos) != '=') { - } - StringBuilder builder = new StringBuilder(); - builder.append(firstString.substring(0, pos + 1)); - builder.append("\r\n"); - String line; - while (true) { - line = getLine(); - if (line == null) { - throw new VCardException( - "File ended during parsing quoted-printable String"); - } - if (line.trim().endsWith("=")) { - // remove "transport-padding" - pos = line.length() - 1; - while(line.charAt(pos) != '=') { - } - builder.append(line.substring(0, pos + 1)); - builder.append("\r\n"); - } else { - builder.append(line); - break; - } - } - return builder.toString(); - } else { - return firstString; - } - } - - protected String getBase64(String firstString) throws IOException, VCardException { - StringBuilder builder = new StringBuilder(); - builder.append(firstString); - - while (true) { - String line = getLine(); - if (line == null) { - throw new VCardException( - "File ended during parsing BASE64 binary"); - } - if (line.length() == 0) { - break; - } - builder.append(line); - } - - return builder.toString(); - } - - /** - * Mainly for "ADR", "ORG", and "N" - * We do not care the number of strnosemi here. - * - * addressparts = 0*6(strnosemi ";") strnosemi - * ; PO Box, Extended Addr, Street, Locality, Region, - * Postal Code, Country Name - * orgparts = *(strnosemi ";") strnosemi - * ; First is Organization Name, - * remainder are Organization Units. - * nameparts = 0*4(strnosemi ";") strnosemi - * ; Family, Given, Middle, Prefix, Suffix. - * ; Example:Public;John;Q.;Reverend Dr.;III, Esq. - * strnosemi = *(*nonsemi ("\;" / "\" CRLF)) *nonsemi - * ; To include a semicolon in this string, it must be escaped - * ; with a "\" character. - * - * We are not sure whether we should add "\" CRLF to each value. - * For now, we exclude them. - */ - protected void handleMultiplePropertyValue(String propertyName, String propertyValue) + public boolean parse(InputStream is, VCardInterpreter interepreter) throws IOException, VCardException { - // vCard 2.1 does not allow QUOTED-PRINTABLE here, - // but some softwares/devices emit such data. - if (mEncoding.equalsIgnoreCase("QUOTED-PRINTABLE")) { - propertyValue = getQuotedPrintable(propertyValue); - } - - if (mBuilder != null) { - mBuilder.propertyValues(VCardUtils.constructListFromValue( - propertyValue, (getVersion() == VCardConfig.FLAG_V30))); - } + return mVCardParserImpl.parse(is, VCardConfig.DEFAULT_TEMPORARY_CHARSET, interepreter); } - /** - * vCard 2.1 specifies AGENT allows one vcard entry. It is not encoded at all. - * - * item = ... - * / [groups "."] "AGENT" - * [params] ":" vcard CRLF - * vcard = "BEGIN" [ws] ":" [ws] "VCARD" [ws] 1*CRLF - * items *CRLF "END" [ws] ":" [ws] "VCARD" - */ - protected void handleAgent(final String propertyValue) throws VCardException { - if (!propertyValue.toUpperCase().contains("BEGIN:VCARD")) { - // Apparently invalid line seen in Windows Mobile 6.5. Ignore them. - return; - } else { - throw new VCardAgentNotSupportedException("AGENT Property is not supported now."); - } - // TODO: Support AGENT property. - } - - /** - * For vCard 3.0. - */ - protected String maybeUnescapeText(final String text) { - return text; - } - - /** - * Returns unescaped String if the character should be unescaped. Return null otherwise. - * e.g. In vCard 2.1, "\;" should be unescaped into ";" while "\x" should not be. - */ - protected String maybeUnescapeCharacter(final char ch) { - return unescapeCharacter(ch); - } - - public static String unescapeCharacter(final char ch) { - // Original vCard 2.1 specification does not allow transformation - // "\:" -> ":", "\," -> ",", and "\\" -> "\", but previous implementation of - // this class allowed them, so keep it as is. - if (ch == '\\' || ch == ';' || ch == ':' || ch == ',') { - return String.valueOf(ch); - } else { - return null; - } - } - - @Override - public boolean parse(final InputStream is, final VCardInterpreter builder) + public boolean parse(InputStream is, String charset, VCardInterpreter interpreter) throws IOException, VCardException { - return parse(is, VCardConfig.DEFAULT_CHARSET, builder); + return mVCardParserImpl.parse(is, charset, interpreter); } - - @Override - public boolean parse(InputStream is, String charset, VCardInterpreter builder) - throws IOException, VCardException { - if (charset == null) { - charset = VCardConfig.DEFAULT_CHARSET; - } - final InputStreamReader tmpReader = new InputStreamReader(is, charset); - if (VCardConfig.showPerformanceLog()) { - mReader = new CustomBufferedReader(tmpReader); - } else { - mReader = new BufferedReader(tmpReader); - } - - mBuilder = builder; - long start = System.currentTimeMillis(); - if (mBuilder != null) { - mBuilder.start(); - } - parseVCardFile(); - if (mBuilder != null) { - mBuilder.end(); - } - mTimeTotal += System.currentTimeMillis() - start; - - if (VCardConfig.showPerformanceLog()) { - showPerformanceInfo(); - } - - return true; - } - - @Override - public void parse(InputStream is, String charset, VCardInterpreter builder, boolean canceled) + public boolean parse(InputStream is, String charset, + VCardInterpreter interpreter, boolean canceled) throws IOException, VCardException { - mCanceled = canceled; - parse(is, charset, builder); - } - - private void showPerformanceInfo() { - Log.d(LOG_TAG, "Total parsing time: " + mTimeTotal + " ms"); - if (mReader instanceof CustomBufferedReader) { - Log.d(LOG_TAG, "Total readLine time: " + - ((CustomBufferedReader)mReader).getTotalmillisecond() + " ms"); - } - Log.d(LOG_TAG, "Time for handling the beggining of the record: " + - mTimeReadStartRecord + " ms"); - Log.d(LOG_TAG, "Time for handling the end of the record: " + - mTimeReadEndRecord + " ms"); - Log.d(LOG_TAG, "Time for parsing line, and handling group: " + - mTimeParseLineAndHandleGroup + " ms"); - Log.d(LOG_TAG, "Time for parsing ADR, ORG, and N fields:" + mTimeParseAdrOrgN + " ms"); - Log.d(LOG_TAG, "Time for parsing property values: " + mTimeParsePropertyValues + " ms"); - Log.d(LOG_TAG, "Time for handling normal property values: " + - mTimeHandleMiscPropertyValue + " ms"); - Log.d(LOG_TAG, "Time for handling Quoted-Printable: " + - mTimeHandleQuotedPrintable + " ms"); - Log.d(LOG_TAG, "Time for handling Base64: " + mTimeHandleBase64 + " ms"); + return mVCardParserImpl.parse(is, charset, interpreter, canceled); } - private boolean isLetter(char ch) { - if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { - return true; - } - return false; - } -} - -class CustomBufferedReader extends BufferedReader { - private long mTime; - - public CustomBufferedReader(Reader in) { - super(in); - } - - @Override - public String readLine() throws IOException { - long start = System.currentTimeMillis(); - String ret = super.readLine(); - long end = System.currentTimeMillis(); - mTime += end - start; - return ret; - } - - public long getTotalmillisecond() { - return mTime; + public void cancel() { + mVCardParserImpl.cancel(); } } diff --git a/core/java/android/pim/vcard/VCardParser_V30.java b/core/java/android/pim/vcard/VCardParser_V30.java index 4ecfe97..13b4196 100644 --- a/core/java/android/pim/vcard/VCardParser_V30.java +++ b/core/java/android/pim/vcard/VCardParser_V30.java @@ -16,343 +16,89 @@ package android.pim.vcard; import android.pim.vcard.exception.VCardException; -import android.util.Log; import java.io.IOException; +import java.io.InputStream; import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; +import java.util.Set; /** - * The class used to parse vCard 3.0. - * Please refer to vCard Specification 3.0 (http://tools.ietf.org/html/rfc2426). + * <p> + * vCard parser for vCard 3.0. See RFC 2426 for more detail. + * </p> + * <p> + * This parser allows vCard format which is not allowed in the RFC, since + * we have seen several vCard 3.0 files which don't comply with it. + * </p> + * <p> + * e.g. vCard 3.0 does not allow "CHARSET" attribute, but some actual files + * have it and they uses non UTF-8 charsets. UTF-8 is recommended in RFC 2426, + * but it is not a must. We silently allow "CHARSET". + * </p> */ -public class VCardParser_V30 extends VCardParser_V21 { - private static final String LOG_TAG = "VCardParser_V30"; - - private static final HashSet<String> sAcceptablePropsWithParam = new HashSet<String>( - Arrays.asList( +public class VCardParser_V30 implements VCardParser { + /* package */ static final Set<String> sKnownPropertyNameSet = + Collections.unmodifiableSet(new HashSet<String>(Arrays.asList( "BEGIN", "LOGO", "PHOTO", "LABEL", "FN", "TITLE", "SOUND", "VERSION", "TEL", "EMAIL", "TZ", "GEO", "NOTE", "URL", "BDAY", "ROLE", "REV", "UID", "KEY", "MAILER", // 2.1 "NAME", "PROFILE", "SOURCE", "NICKNAME", "CLASS", - "SORT-STRING", "CATEGORIES", "PRODID")); // 3.0 - - // Although "7bit" and "BASE64" is not allowed in vCard 3.0, we allow it for safety. - private static final HashSet<String> sAcceptableEncodingV30 = new HashSet<String>( - Arrays.asList("7BIT", "8BIT", "BASE64", "B")); - - // Although RFC 2426 specifies some property must not have parameters, we allow it, - // since there may be some careers which violates the RFC... - private static final HashSet<String> acceptablePropsWithoutParam = new HashSet<String>(); - - private String mPreviousLine; - - private boolean mEmittedAgentWarning = false; + "SORT-STRING", "CATEGORIES", "PRODID"))); // 3.0 /** - * True when the caller wants the parser to be strict about the input. - * Currently this is only for testing. + * <p> + * A unmodifiable Set storing the values for the type "ENCODING", available in the vCard 3.0. + * </p> + * <p> + * Though vCard 2.1 specification does not allow "7BIT" or "BASE64", we allow them for safety. + * </p> + * <p> + * "QUOTED-PRINTABLE" is not allowed in vCard 3.0 and not in this parser either, + * because the encoding ambiguates how the vCard file to be parsed. + * </p> */ - private final boolean mStrictParsing; - - public VCardParser_V30() { - super(); - mStrictParsing = false; - } - - /** - * @param strictParsing when true, this object throws VCardException when the vcard is not - * valid from the view of vCard 3.0 specification (defined in RFC 2426). Note that this class - * is not fully yet for being used with this flag and may not notice invalid line(s). - * - * @hide currently only for testing! - */ - public VCardParser_V30(boolean strictParsing) { - super(); - mStrictParsing = strictParsing; - } - - public VCardParser_V30(int parseMode) { - super(parseMode); - mStrictParsing = false; - } + /* package */ static final Set<String> sAcceptableEncoding = + Collections.unmodifiableSet(new HashSet<String>(Arrays.asList( + VCardConstants.PARAM_ENCODING_7BIT, + VCardConstants.PARAM_ENCODING_8BIT, + VCardConstants.PARAM_ENCODING_BASE64, + VCardConstants.PARAM_ENCODING_B))); - @Override - protected int getVersion() { - return VCardConfig.FLAG_V30; - } + private final VCardParserImpl_V30 mVCardParserImpl; - @Override - protected String getVersionString() { - return VCardConstants.VERSION_V30; + public VCardParser_V30() { + mVCardParserImpl = new VCardParserImpl_V30(); } - @Override - protected boolean isValidPropertyName(String propertyName) { - if (!(sAcceptablePropsWithParam.contains(propertyName) || - acceptablePropsWithoutParam.contains(propertyName) || - propertyName.startsWith("X-")) && - !mUnknownTypeMap.contains(propertyName)) { - mUnknownTypeMap.add(propertyName); - Log.w(LOG_TAG, "Property name unsupported by vCard 3.0: " + propertyName); - } - return true; + public VCardParser_V30(VCardSourceDetector detector) { + mVCardParserImpl = new VCardParserImpl_V30(detector); } - @Override - protected boolean isValidEncoding(String encoding) { - return sAcceptableEncodingV30.contains(encoding.toUpperCase()); + public VCardParser_V30(int parseType) { + mVCardParserImpl = new VCardParserImpl_V30(parseType); } - @Override - protected String getLine() throws IOException { - if (mPreviousLine != null) { - String ret = mPreviousLine; - mPreviousLine = null; - return ret; - } else { - return mReader.readLine(); - } - } + //// Implemented methods - /** - * vCard 3.0 requires that the line with space at the beginning of the line - * must be combined with previous line. - */ - @Override - protected String getNonEmptyLine() throws IOException, VCardException { - String line; - StringBuilder builder = null; - while (true) { - line = mReader.readLine(); - if (line == null) { - if (builder != null) { - return builder.toString(); - } else if (mPreviousLine != null) { - String ret = mPreviousLine; - mPreviousLine = null; - return ret; - } - throw new VCardException("Reached end of buffer."); - } else if (line.length() == 0) { - if (builder != null) { - return builder.toString(); - } else if (mPreviousLine != null) { - String ret = mPreviousLine; - mPreviousLine = null; - return ret; - } - } else if (line.charAt(0) == ' ' || line.charAt(0) == '\t') { - if (builder != null) { - // See Section 5.8.1 of RFC 2425 (MIME-DIR document). - // Following is the excerpts from it. - // - // DESCRIPTION:This is a long description that exists on a long line. - // - // Can be represented as: - // - // DESCRIPTION:This is a long description - // that exists on a long line. - // - // It could also be represented as: - // - // DESCRIPTION:This is a long descrip - // tion that exists o - // n a long line. - builder.append(line.substring(1)); - } else if (mPreviousLine != null) { - builder = new StringBuilder(); - builder.append(mPreviousLine); - mPreviousLine = null; - builder.append(line.substring(1)); - } else { - throw new VCardException("Space exists at the beginning of the line"); - } - } else { - if (mPreviousLine == null) { - mPreviousLine = line; - if (builder != null) { - return builder.toString(); - } - } else { - String ret = mPreviousLine; - mPreviousLine = line; - return ret; - } - } - } - } - - - /** - * vcard = [group "."] "BEGIN" ":" "VCARD" 1 * CRLF - * 1 * (contentline) - * ;A vCard object MUST include the VERSION, FN and N types. - * [group "."] "END" ":" "VCARD" 1 * CRLF - */ - @Override - protected boolean readBeginVCard(boolean allowGarbage) throws IOException, VCardException { - // TODO: vCard 3.0 supports group. - return super.readBeginVCard(allowGarbage); - } - - @Override - protected void readEndVCard(boolean useCache, boolean allowGarbage) + public boolean parse(InputStream is, VCardInterpreter interepreter) throws IOException, VCardException { - // TODO: vCard 3.0 supports group. - super.readEndVCard(useCache, allowGarbage); + return mVCardParserImpl.parse(is, VCardConfig.DEFAULT_TEMPORARY_CHARSET, interepreter); } - /** - * vCard 3.0 allows iana-token as paramType, while vCard 2.1 does not. - */ - @Override - protected void handleParams(String params) throws VCardException { - try { - super.handleParams(params); - } catch (VCardException e) { - // maybe IANA type - String[] strArray = params.split("=", 2); - if (strArray.length == 2) { - handleAnyParam(strArray[0], strArray[1]); - } else { - // Must not come here in the current implementation. - throw new VCardException( - "Unknown params value: " + params); - } - } - } - - @Override - protected void handleAnyParam(String paramName, String paramValue) { - super.handleAnyParam(paramName, paramValue); - } - - @Override - protected void handleParamWithoutName(final String paramValue) throws VCardException { - if (mStrictParsing) { - throw new VCardException("Parameter without name is not acceptable in vCard 3.0"); - } else { - super.handleParamWithoutName(paramValue); - } - } - - /** - * vCard 3.0 defines - * - * param = param-name "=" param-value *("," param-value) - * param-name = iana-token / x-name - * param-value = ptext / quoted-string - * quoted-string = DQUOTE QSAFE-CHAR DQUOTE - */ - @Override - protected void handleType(String ptypevalues) { - String[] ptypeArray = ptypevalues.split(","); - mBuilder.propertyParamType("TYPE"); - for (String value : ptypeArray) { - int length = value.length(); - if (length >= 2 && value.startsWith("\"") && value.endsWith("\"")) { - mBuilder.propertyParamValue(value.substring(1, value.length() - 1)); - } else { - mBuilder.propertyParamValue(value); - } - } - } - - @Override - protected void handleAgent(String propertyValue) { - // The way how vCard 3.0 supports "AGENT" is completely different from vCard 2.1. - // - // e.g. - // AGENT:BEGIN:VCARD\nFN:Joe Friday\nTEL:+1-919-555-7878\n - // TITLE:Area Administrator\, Assistant\n EMAIL\;TYPE=INTERN\n - // ET:jfriday@host.com\nEND:VCARD\n - // - // TODO: fix this. - // - // issue: - // vCard 3.0 also allows this as an example. - // - // AGENT;VALUE=uri: - // CID:JQPUBLIC.part3.960129T083020.xyzMail@host3.com - // - // This is not vCard. Should we support this? - // - // Just ignore the line for now, since we cannot know how to handle it... - if (!mEmittedAgentWarning) { - Log.w(LOG_TAG, "AGENT in vCard 3.0 is not supported yet. Ignore it"); - mEmittedAgentWarning = true; - } - } - - /** - * vCard 3.0 does not require two CRLF at the last of BASE64 data. - * It only requires that data should be MIME-encoded. - */ - @Override - protected String getBase64(String firstString) throws IOException, VCardException { - StringBuilder builder = new StringBuilder(); - builder.append(firstString); - - while (true) { - String line = getLine(); - if (line == null) { - throw new VCardException( - "File ended during parsing BASE64 binary"); - } - if (line.length() == 0) { - break; - } else if (!line.startsWith(" ") && !line.startsWith("\t")) { - mPreviousLine = line; - break; - } - builder.append(line); - } - - return builder.toString(); - } - - /** - * ESCAPED-CHAR = "\\" / "\;" / "\," / "\n" / "\N") - * ; \\ encodes \, \n or \N encodes newline - * ; \; encodes ;, \, encodes , - * - * Note: Apple escapes ':' into '\:' while does not escape '\' - */ - @Override - protected String maybeUnescapeText(String text) { - return unescapeText(text); - } - - public static String unescapeText(String text) { - StringBuilder builder = new StringBuilder(); - int length = text.length(); - for (int i = 0; i < length; i++) { - char ch = text.charAt(i); - if (ch == '\\' && i < length - 1) { - char next_ch = text.charAt(++i); - if (next_ch == 'n' || next_ch == 'N') { - builder.append("\n"); - } else { - builder.append(next_ch); - } - } else { - builder.append(ch); - } - } - return builder.toString(); + public boolean parse(InputStream is, String charset, VCardInterpreter interpreter) + throws IOException, VCardException { + return mVCardParserImpl.parse(is, charset, interpreter); } - @Override - protected String maybeUnescapeCharacter(char ch) { - return unescapeCharacter(ch); + public boolean parse(InputStream is, String charset, + VCardInterpreter interpreter, boolean canceled) + throws IOException, VCardException { + return mVCardParserImpl.parse(is, charset, interpreter, canceled); } - public static String unescapeCharacter(char ch) { - if (ch == 'n' || ch == 'N') { - return "\n"; - } else { - return String.valueOf(ch); - } + public void cancel() { + mVCardParserImpl.cancel(); } } diff --git a/core/java/android/pim/vcard/VCardUtils.java b/core/java/android/pim/vcard/VCardUtils.java index 11b112b..26ebed7 100644 --- a/core/java/android/pim/vcard/VCardUtils.java +++ b/core/java/android/pim/vcard/VCardUtils.java @@ -329,8 +329,8 @@ public class VCardUtils { if (ch == '\\' && i < length - 1) { char nextCh = value.charAt(i + 1); final String unescapedString = - (isV30 ? VCardParser_V30.unescapeCharacter(nextCh) : - VCardParser_V21.unescapeCharacter(nextCh)); + (isV30 ? VCardParserImpl_V30.unescapeCharacter(nextCh) : + VCardParserImpl_V21.unescapeCharacter(nextCh)); if (unescapedString != null) { builder.append(unescapedString); i++; diff --git a/core/java/android/preference/PreferenceActivity.java b/core/java/android/preference/PreferenceActivity.java index 726793d..4686978 100644 --- a/core/java/android/preference/PreferenceActivity.java +++ b/core/java/android/preference/PreferenceActivity.java @@ -23,7 +23,10 @@ import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.os.Message; +import android.text.TextUtils; import android.view.View; +import android.view.View.OnClickListener; +import android.widget.Button; /** * Shows a hierarchy of {@link Preference} objects as @@ -69,30 +72,43 @@ import android.view.View; * As a convenience, this activity implements a click listener for any * preference in the current hierarchy, see * {@link #onPreferenceTreeClick(PreferenceScreen, Preference)}. - * + * * @see Preference * @see PreferenceScreen */ public abstract class PreferenceActivity extends ListActivity implements PreferenceManager.OnPreferenceTreeClickListener { - + private static final String PREFERENCES_TAG = "android:preferences"; - + + // extras that allow any preference activity to be launched as part of a wizard + + // show Back and Next buttons? takes boolean parameter + // Back will then return RESULT_CANCELED and Next RESULT_OK + private static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar"; + + // specify custom text for the Back or Next buttons, or cause a button to not appear + // at all by setting it to null + private static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text"; + private static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text"; + + private Button mNextButton; + private PreferenceManager mPreferenceManager; - + private Bundle mSavedInstanceState; /** * The starting request code given out to preference framework. */ private static final int FIRST_REQUEST_CODE = 100; - + private static final int MSG_BIND_PREFERENCES = 0; private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { - + case MSG_BIND_PREFERENCES: bindPreferences(); break; @@ -105,7 +121,49 @@ public abstract class PreferenceActivity extends ListActivity implements super.onCreate(savedInstanceState); setContentView(com.android.internal.R.layout.preference_list_content); - + + // see if we should show Back/Next buttons + Intent intent = getIntent(); + if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) { + + findViewById(com.android.internal.R.id.button_bar).setVisibility(View.VISIBLE); + + Button backButton = (Button)findViewById(com.android.internal.R.id.back_button); + backButton.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + setResult(RESULT_CANCELED); + finish(); + } + }); + mNextButton = (Button)findViewById(com.android.internal.R.id.next_button); + mNextButton.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + setResult(RESULT_OK); + finish(); + } + }); + + // set our various button parameters + if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) { + String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT); + if (TextUtils.isEmpty(buttonText)) { + mNextButton.setVisibility(View.GONE); + } + else { + mNextButton.setText(buttonText); + } + } + if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) { + String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT); + if (TextUtils.isEmpty(buttonText)) { + backButton.setVisibility(View.GONE); + } + else { + backButton.setText(buttonText); + } + } + } + mPreferenceManager = onCreatePreferenceManager(); getListView().setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); } @@ -113,14 +171,13 @@ public abstract class PreferenceActivity extends ListActivity implements @Override protected void onStop() { super.onStop(); - + mPreferenceManager.dispatchActivityStop(); } @Override protected void onDestroy() { super.onDestroy(); - mPreferenceManager.dispatchActivityDestroy(); } @@ -156,7 +213,7 @@ public abstract class PreferenceActivity extends ListActivity implements @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); - + mPreferenceManager.dispatchActivityResult(requestCode, resultCode, data); } @@ -176,7 +233,7 @@ public abstract class PreferenceActivity extends ListActivity implements if (mHandler.hasMessages(MSG_BIND_PREFERENCES)) return; mHandler.obtainMessage(MSG_BIND_PREFERENCES).sendToTarget(); } - + private void bindPreferences() { final PreferenceScreen preferenceScreen = getPreferenceScreen(); if (preferenceScreen != null) { @@ -187,10 +244,10 @@ public abstract class PreferenceActivity extends ListActivity implements } } } - + /** * Creates the {@link PreferenceManager}. - * + * * @return The {@link PreferenceManager} used by this activity. */ private PreferenceManager onCreatePreferenceManager() { @@ -198,7 +255,7 @@ public abstract class PreferenceActivity extends ListActivity implements preferenceManager.setOnPreferenceTreeClickListener(this); return preferenceManager; } - + /** * Returns the {@link PreferenceManager} used by this activity. * @return The {@link PreferenceManager}. @@ -206,7 +263,7 @@ public abstract class PreferenceActivity extends ListActivity implements public PreferenceManager getPreferenceManager() { return mPreferenceManager; } - + private void requirePreferenceManager() { if (mPreferenceManager == null) { throw new RuntimeException("This should be called after super.onCreate."); @@ -215,7 +272,7 @@ public abstract class PreferenceActivity extends ListActivity implements /** * Sets the root of the preference hierarchy that this activity is showing. - * + * * @param preferenceScreen The root {@link PreferenceScreen} of the preference hierarchy. */ public void setPreferenceScreen(PreferenceScreen preferenceScreen) { @@ -228,37 +285,37 @@ public abstract class PreferenceActivity extends ListActivity implements } } } - + /** * Gets the root of the preference hierarchy that this activity is showing. - * + * * @return The {@link PreferenceScreen} that is the root of the preference * hierarchy. */ public PreferenceScreen getPreferenceScreen() { return mPreferenceManager.getPreferenceScreen(); } - + /** * Adds preferences from activities that match the given {@link Intent}. - * + * * @param intent The {@link Intent} to query activities. */ public void addPreferencesFromIntent(Intent intent) { requirePreferenceManager(); - + setPreferenceScreen(mPreferenceManager.inflateFromIntent(intent, getPreferenceScreen())); } - + /** * Inflates the given XML resource and adds the preference hierarchy to the current * preference hierarchy. - * + * * @param preferencesResId The XML resource ID to inflate. */ public void addPreferencesFromResource(int preferencesResId) { requirePreferenceManager(); - + setPreferenceScreen(mPreferenceManager.inflateFromResource(this, preferencesResId, getPreferenceScreen())); } @@ -269,20 +326,20 @@ public abstract class PreferenceActivity extends ListActivity implements public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { return false; } - + /** * Finds a {@link Preference} based on its key. - * + * * @param key The key of the preference to retrieve. * @return The {@link Preference} with the key, or null. * @see PreferenceGroup#findPreference(CharSequence) */ public Preference findPreference(CharSequence key) { - + if (mPreferenceManager == null) { return null; } - + return mPreferenceManager.findPreference(key); } @@ -292,5 +349,14 @@ public abstract class PreferenceActivity extends ListActivity implements mPreferenceManager.dispatchNewIntent(intent); } } - + + // give subclasses access to the Next button + /** @hide */ + protected boolean hasNextButton() { + return mNextButton != null; + } + /** @hide */ + protected Button getNextButton() { + return mNextButton; + } } diff --git a/core/java/android/provider/ContactsContract.java b/core/java/android/provider/ContactsContract.java index 40a408a..1531392 100644 --- a/core/java/android/provider/ContactsContract.java +++ b/core/java/android/provider/ContactsContract.java @@ -5558,6 +5558,28 @@ public final class ContactsContract { "com.android.contacts.action.SHOW_OR_CREATE_CONTACT"; /** + * Starts an Activity that lets the user select the multiple phones from a + * list of phone numbers which come from the contacts or + * {@link #EXTRA_PHONE_URIS}. + * <p> + * The phone numbers being passed in through {@link #EXTRA_PHONE_URIS} + * could belong to the contacts or not, and will be selected by default. + * <p> + * The user's selection will be returned from + * {@link android.app.Activity#onActivityResult(int, int, android.content.Intent)} + * if the resultCode is + * {@link android.app.Activity#RESULT_OK}, the array of picked phone + * numbers are in the Intent's + * {@link #EXTRA_PHONE_URIS}; otherwise, the + * {@link android.app.Activity#RESULT_CANCELED} is returned if the user + * left the Activity without changing the selection. + * + * @hide + */ + public static final String ACTION_GET_MULTIPLE_PHONES = + "com.android.contacts.action.GET_MULTIPLE_PHONES"; + + /** * Used with {@link #SHOW_OR_CREATE_CONTACT} to force creating a new * contact if no matching contact found. Otherwise, default behavior is * to prompt user with dialog before creating. @@ -5578,6 +5600,23 @@ public final class ContactsContract { "com.android.contacts.action.CREATE_DESCRIPTION"; /** + * Used with {@link #ACTION_GET_MULTIPLE_PHONES} as the input or output value. + * <p> + * The phone numbers want to be picked by default should be passed in as + * input value. These phone numbers could belong to the contacts or not. + * <p> + * The phone numbers which were picked by the user are returned as output + * value. + * <p> + * Type: array of URIs, the tel URI is used for the phone numbers which don't + * belong to any contact, the content URI is used for phone id in contacts. + * + * @hide + */ + public static final String EXTRA_PHONE_URIS = + "com.android.contacts.extra.PHONE_URIS"; + + /** * Optional extra used with {@link #SHOW_OR_CREATE_CONTACT} to specify a * dialog location using screen coordinates. When not specified, the * dialog will be centered. diff --git a/core/java/android/text/DynamicLayout.java b/core/java/android/text/DynamicLayout.java index 14e5655..b6aa03a 100644 --- a/core/java/android/text/DynamicLayout.java +++ b/core/java/android/text/DynamicLayout.java @@ -310,7 +310,6 @@ extends Layout Directions[] objects = new Directions[1]; - for (int i = 0; i < n; i++) { ints[START] = reflowed.getLineStart(i) | (reflowed.getParagraphDirection(i) << DIR_SHIFT) | diff --git a/core/java/android/text/Layout.java b/core/java/android/text/Layout.java index 38ac9b7..ff1f2a60 100644 --- a/core/java/android/text/Layout.java +++ b/core/java/android/text/Layout.java @@ -16,25 +16,31 @@ package android.text; +import com.android.internal.util.ArrayUtils; + import android.emoji.EmojiFactory; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; +import android.graphics.Path; import android.graphics.Rect; import android.graphics.RectF; -import android.graphics.Path; -import com.android.internal.util.ArrayUtils; - -import junit.framework.Assert; -import android.text.style.*; import android.text.method.TextKeyListener; +import android.text.style.AlignmentSpan; +import android.text.style.LeadingMarginSpan; +import android.text.style.LineBackgroundSpan; +import android.text.style.ParagraphStyle; +import android.text.style.ReplacementSpan; +import android.text.style.TabStopSpan; import android.view.KeyEvent; +import junit.framework.Assert; + /** - * A base class that manages text layout in visual elements on - * the screen. - * <p>For text that will be edited, use a {@link DynamicLayout}, - * which will be updated as the text changes. + * A base class that manages text layout in visual elements on + * the screen. + * <p>For text that will be edited, use a {@link DynamicLayout}, + * which will be updated as the text changes. * For text that will not change, use a {@link StaticLayout}. */ public abstract class Layout { @@ -66,7 +72,7 @@ public abstract class Layout { TextPaint paint) { return getDesiredWidth(source, 0, source.length(), paint); } - + /** * Return how wide a layout must be in order to display the * specified text slice with one line per paragraph. @@ -185,13 +191,13 @@ public abstract class Layout { if (dbottom < bottom) { bottom = dbottom; } - - int first = getLineForVertical(top); + + int first = getLineForVertical(top); int last = getLineForVertical(bottom); - + int previousLineBottom = getLineTop(first); int previousLineEnd = getLineStart(first); - + TextPaint paint = mPaint; CharSequence buf = mText; int width = mWidth; @@ -238,7 +244,7 @@ public abstract class Layout { previousLineBottom = getLineTop(first); previousLineEnd = getLineStart(first); spans = NO_PARA_SPANS; - } + } // There can be a highlight even without spans if we are drawing // a non-spanned transformation of a spanned editing buffer. @@ -255,7 +261,7 @@ public abstract class Layout { } Alignment align = mAlignment; - + // Next draw the lines, one at a time. // the baseline is the top of the following line minus the current // line's descent. @@ -271,7 +277,7 @@ public abstract class Layout { int lbaseline = lbottom - getLineDescent(i); boolean isFirstParaLine = false; - if (spannedText) { + if (spannedText) { if (start == 0 || buf.charAt(start - 1) == '\n') { isFirstParaLine = true; } @@ -282,7 +288,7 @@ public abstract class Layout { spanend = sp.nextSpanTransition(start, textLength, ParagraphStyle.class); spans = sp.getSpans(start, spanend, ParagraphStyle.class); - + align = mAlignment; for (int n = spans.length-1; n >= 0; n--) { if (spans[n] instanceof AlignmentSpan) { @@ -292,7 +298,7 @@ public abstract class Layout { } } } - + int dir = getParagraphDirection(i); int left = 0; int right = mWidth; @@ -309,7 +315,7 @@ public abstract class Layout { margin.drawLeadingMargin(c, paint, right, dir, ltop, lbaseline, lbottom, buf, start, end, isFirstParaLine, this); - + right -= margin.getLeadingMargin(isFirstParaLine); } else { margin.drawLeadingMargin(c, paint, left, dir, ltop, @@ -417,7 +423,7 @@ public abstract class Layout { mWidth = wid; } - + /** * Return the total height of this layout. */ @@ -450,7 +456,7 @@ public abstract class Layout { * Return the number of lines of text in this layout. */ public abstract int getLineCount(); - + /** * Return the baseline for the specified line (0…getLineCount() - 1) * If bounds is not null, return the top, left, right, bottom extents @@ -524,13 +530,95 @@ public abstract class Layout { */ public abstract int getBottomPadding(); + + /** + * Returns true if the character at offset and the preceding character + * are at different run levels (and thus there's a split caret). + * @param offset the offset + * @return true if at a level boundary + */ + private boolean isLevelBoundary(int offset) { + int line = getLineForOffset(offset); + Directions dirs = getLineDirections(line); + if (dirs == DIRS_ALL_LEFT_TO_RIGHT || dirs == DIRS_ALL_RIGHT_TO_LEFT) { + return false; + } + + int[] runs = dirs.mDirections; + int lineStart = getLineStart(line); + int lineEnd = getLineEnd(line); + if (offset == lineStart || offset == lineEnd) { + int paraLevel = getParagraphDirection(line) == 1 ? 0 : 1; + int runIndex = offset == lineStart ? 0 : runs.length - 2; + return ((runs[runIndex + 1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK) != paraLevel; + } + + offset -= lineStart; + for (int i = 0; i < runs.length; i += 2) { + if (offset == runs[i]) { + return true; + } + } + return false; + } + + private boolean primaryIsTrailingPrevious(int offset) { + int line = getLineForOffset(offset); + int lineStart = getLineStart(line); + int lineEnd = getLineEnd(line); + int[] runs = getLineDirections(line).mDirections; + + int levelAt = -1; + for (int i = 0; i < runs.length; i += 2) { + int start = lineStart + runs[i]; + int limit = start + (runs[i+1] & RUN_LENGTH_MASK); + if (limit > lineEnd) { + limit = lineEnd; + } + if (offset >= start && offset < limit) { + if (offset > start) { + // Previous character is at same level, so don't use trailing. + return false; + } + levelAt = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK; + break; + } + } + if (levelAt == -1) { + // Offset was limit of line. + levelAt = getParagraphDirection(line) == 1 ? 0 : 1; + } + + // At level boundary, check previous level. + int levelBefore = -1; + if (offset == lineStart) { + levelBefore = getParagraphDirection(line) == 1 ? 0 : 1; + } else { + offset -= 1; + for (int i = 0; i < runs.length; i += 2) { + int start = lineStart + runs[i]; + int limit = start + (runs[i+1] & RUN_LENGTH_MASK); + if (limit > lineEnd) { + limit = lineEnd; + } + if (offset >= start && offset < limit) { + levelBefore = (runs[i+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK; + break; + } + } + } + + return levelBefore < levelAt; + } + /** * Get the primary horizontal position for the specified text offset. * This is the location where a new character would be inserted in * the paragraph's primary direction. */ public float getPrimaryHorizontal(int offset) { - return getHorizontal(offset, false, true); + boolean trailing = primaryIsTrailingPrevious(offset); + return getHorizontal(offset, trailing); } /** @@ -539,17 +627,17 @@ public abstract class Layout { * the direction other than the paragraph's primary direction. */ public float getSecondaryHorizontal(int offset) { - return getHorizontal(offset, true, true); + boolean trailing = primaryIsTrailingPrevious(offset); + return getHorizontal(offset, !trailing); } - private float getHorizontal(int offset, boolean trailing, boolean alt) { + private float getHorizontal(int offset, boolean trailing) { int line = getLineForOffset(offset); - return getHorizontal(offset, trailing, alt, line); + return getHorizontal(offset, trailing, line); } - private float getHorizontal(int offset, boolean trailing, boolean alt, - int line) { + private float getHorizontal(int offset, boolean trailing, int line) { int start = getLineStart(line); int end = getLineVisibleEnd(line); int dir = getParagraphDirection(line); @@ -562,7 +650,7 @@ public abstract class Layout { } float wid = measureText(mPaint, mWorkPaint, mText, start, offset, end, - dir, directions, trailing, alt, tab, tabs); + dir, directions, trailing, tab, tabs); if (offset > end) { if (dir == DIR_RIGHT_TO_LEFT) @@ -679,7 +767,7 @@ public abstract class Layout { end = getLineEnd(line); } else { end = getLineVisibleEnd(line); - } + } boolean tab = getLineContainsTab(line); if (tabs == null && tab && mText instanceof Spanned) { @@ -738,7 +826,7 @@ public abstract class Layout { } /** - * Get the character offset on the specfied line whose position is + * Get the character offset on the specified line whose position is * closest to the specified horizontal position. */ public int getOffsetForHorizontal(int line, float horiz) { @@ -752,14 +840,13 @@ public abstract class Layout { int best = min; float bestdist = Math.abs(getPrimaryHorizontal(best) - horiz); - int here = min; - for (int i = 0; i < dirs.mDirections.length; i++) { - int there = here + dirs.mDirections[i]; - int swap = ((i & 1) == 0) ? 1 : -1; + for (int i = 0; i < dirs.mDirections.length; i += 2) { + int here = min + dirs.mDirections[i]; + int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK); + int swap = (dirs.mDirections[i+1] & RUN_RTL_FLAG) != 0 ? -1 : 1; if (there > max) there = max; - int high = there - 1 + 1, low = here + 1 - 1, guess; while (high - low > 1) { @@ -792,7 +879,7 @@ public abstract class Layout { if (dist < bestdist) { bestdist = dist; - best = low; + best = low; } } @@ -802,8 +889,6 @@ public abstract class Layout { bestdist = dist; best = here; } - - here = there; } float dist = Math.abs(getPrimaryHorizontal(max) - horiz); @@ -823,14 +908,14 @@ public abstract class Layout { return getLineStart(line + 1); } - /** + /** * Return the text offset after the last visible character (so whitespace * is not counted) on the specified line. */ public int getLineVisibleEnd(int line) { return getLineVisibleEnd(line, getLineStart(line), getLineStart(line+1)); } - + private int getLineVisibleEnd(int line, int start, int end) { if (DEBUG) { Assert.assertTrue(getLineStart(line) == start && getLineStart(line+1) == end); @@ -882,207 +967,178 @@ public abstract class Layout { return getLineTop(line) - (getLineTop(line+1) - getLineDescent(line)); } - /** - * Return the text offset that would be reached by moving left - * (possibly onto another line) from the specified offset. - */ public int getOffsetToLeftOf(int offset) { - int line = getLineForOffset(offset); - int start = getLineStart(line); - int end = getLineEnd(line); - Directions dirs = getLineDirections(line); - - if (line != getLineCount() - 1) - end--; - - float horiz = getPrimaryHorizontal(offset); - - int best = offset; - float besth = Integer.MIN_VALUE; - int candidate; - - candidate = TextUtils.getOffsetBefore(mText, offset); - if (candidate >= start && candidate <= end) { - float h = getPrimaryHorizontal(candidate); - - if (h < horiz && h > besth) { - best = candidate; - besth = h; - } - } - - candidate = TextUtils.getOffsetAfter(mText, offset); - if (candidate >= start && candidate <= end) { - float h = getPrimaryHorizontal(candidate); - - if (h < horiz && h > besth) { - best = candidate; - besth = h; - } - } - - int here = start; - for (int i = 0; i < dirs.mDirections.length; i++) { - int there = here + dirs.mDirections[i]; - if (there > end) - there = end; - - float h = getPrimaryHorizontal(here); - - if (h < horiz && h > besth) { - best = here; - besth = h; - } - - candidate = TextUtils.getOffsetAfter(mText, here); - if (candidate >= start && candidate <= end) { - h = getPrimaryHorizontal(candidate); - - if (h < horiz && h > besth) { - best = candidate; - besth = h; - } - } - - candidate = TextUtils.getOffsetBefore(mText, there); - if (candidate >= start && candidate <= end) { - h = getPrimaryHorizontal(candidate); - - if (h < horiz && h > besth) { - best = candidate; - besth = h; - } - } - - here = there; - } - - float h = getPrimaryHorizontal(end); - - if (h < horiz && h > besth) { - best = end; - besth = h; - } - - if (best != offset) - return best; - - int dir = getParagraphDirection(line); - - if (dir > 0) { - if (line == 0) - return best; - else - return getOffsetForHorizontal(line - 1, 10000); - } else { - if (line == getLineCount() - 1) - return best; - else - return getOffsetForHorizontal(line + 1, 10000); - } + return getOffsetToLeftRightOf(offset, true); } - /** - * Return the text offset that would be reached by moving right - * (possibly onto another line) from the specified offset. - */ public int getOffsetToRightOf(int offset) { - int line = getLineForOffset(offset); - int start = getLineStart(line); - int end = getLineEnd(line); - Directions dirs = getLineDirections(line); - - if (line != getLineCount() - 1) - end--; - - float horiz = getPrimaryHorizontal(offset); - - int best = offset; - float besth = Integer.MAX_VALUE; - int candidate; - - candidate = TextUtils.getOffsetBefore(mText, offset); - if (candidate >= start && candidate <= end) { - float h = getPrimaryHorizontal(candidate); - - if (h > horiz && h < besth) { - best = candidate; - besth = h; - } - } - - candidate = TextUtils.getOffsetAfter(mText, offset); - if (candidate >= start && candidate <= end) { - float h = getPrimaryHorizontal(candidate); + return getOffsetToLeftRightOf(offset, false); + } - if (h > horiz && h < besth) { - best = candidate; - besth = h; + // 1) The caret marks the leading edge of a character. The character + // logically before it might be on a different level, and the active caret + // position is on the character at the lower level. If that character + // was the previous character, the caret is on its trailing edge. + // 2) Take this character/edge and move it in the indicated direction. + // This gives you a new character and a new edge. + // 3) This position is between two visually adjacent characters. One of + // these might be at a lower level. The active position is on the + // character at the lower level. + // 4) If the active position is on the trailing edge of the character, + // the new caret position is the following logical character, else it + // is the character. + private int getOffsetToLeftRightOf(int caret, boolean toLeft) { + int line = getLineForOffset(caret); + int lineStart = getLineStart(line); + int lineEnd = getLineEnd(line); + + boolean paraIsRtl = getParagraphDirection(line) == -1; + int[] runs = getLineDirections(line).mDirections; + + int runIndex, runLevel = 0, runStart = lineStart, runLimit = lineEnd, newCaret = -1; + boolean trailing = false; + + if (caret == lineStart) { + runIndex = -2; + } else if (caret == lineEnd) { + runIndex = runs.length; + } else { + // First, get information about the run containing the character with + // the active caret. + for (runIndex = 0; runIndex < runs.length; runIndex += 2) { + runStart = lineStart + runs[runIndex]; + if (caret >= runStart) { + runLimit = runStart + (runs[runIndex+1] & RUN_LENGTH_MASK); + if (runLimit > lineEnd) { + runLimit = lineEnd; + } + if (caret < runLimit) { + runLevel = (runs[runIndex+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK; + if (caret == runStart) { + // The caret is on a run boundary, see if we should + // use the position on the trailing edge of the previous + // logical character instead. + int prevRunIndex, prevRunLevel, prevRunStart, prevRunLimit; + int pos = caret - 1; + for (prevRunIndex = 0; prevRunIndex < runs.length; prevRunIndex += 2) { + prevRunStart = lineStart + runs[prevRunIndex]; + if (pos >= prevRunStart) { + prevRunLimit = prevRunStart + (runs[prevRunIndex+1] & RUN_LENGTH_MASK); + if (prevRunLimit > lineEnd) { + prevRunLimit = lineEnd; + } + if (pos < prevRunLimit) { + prevRunLevel = (runs[prevRunIndex+1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK; + if (prevRunLevel < runLevel) { + // Start from logically previous character. + runIndex = prevRunIndex; + runLevel = prevRunLevel; + runStart = prevRunStart; + runLimit = prevRunLimit; + trailing = true; + break; + } + } + } + } + } + break; + } } - } - - int here = start; - for (int i = 0; i < dirs.mDirections.length; i++) { - int there = here + dirs.mDirections[i]; - if (there > end) - there = end; - - float h = getPrimaryHorizontal(here); - - if (h > horiz && h < besth) { - best = here; - besth = h; + } + + // caret might be = lineEnd. This is generally a space or paragraph + // separator and has an associated run, but might be the end of + // text, in which case it doesn't. If that happens, we ran off the + // end of the run list, and runIndex == runs.length. In this case, + // we are at a run boundary so we skip the below test. + if (runIndex != runs.length) { + boolean rtlRun = (runLevel & 0x1) != 0; + boolean advance = toLeft == rtlRun; + if (caret != (advance ? runLimit : runStart) || advance != trailing) { + // Moving within or into the run, so we can move logically. + newCaret = getOffsetBeforeAfter(caret, advance); + // If the new position is internal to the run, we're at the strong + // position already so we're finished. + if (newCaret != (advance ? runLimit : runStart)) { + return newCaret; + } + } + } + } + + // If newCaret is -1, we're starting at a run boundary and crossing + // into another run. Otherwise we've arrived at a run boundary, and + // need to figure out which character to attach to. Note we might + // need to run this twice, if we cross a run boundary and end up at + // another run boundary. + while (true) { + boolean advance = toLeft == paraIsRtl; + int otherRunIndex = runIndex + (advance ? 2 : -2); + if (otherRunIndex >= 0 && otherRunIndex < runs.length) { + int otherRunStart = lineStart + runs[otherRunIndex]; + int otherRunLimit = otherRunStart + (runs[otherRunIndex+1] & RUN_LENGTH_MASK); + if (otherRunLimit > lineEnd) { + otherRunLimit = lineEnd; } - - candidate = TextUtils.getOffsetAfter(mText, here); - if (candidate >= start && candidate <= end) { - h = getPrimaryHorizontal(candidate); - - if (h > horiz && h < besth) { - best = candidate; - besth = h; - } + int otherRunLevel = runs[otherRunIndex+1] >>> RUN_LEVEL_SHIFT & RUN_LEVEL_MASK; + boolean otherRunIsRtl = (otherRunLevel & 1) != 0; + + advance = toLeft == otherRunIsRtl; + if (newCaret == -1) { + newCaret = getOffsetBeforeAfter(advance ? otherRunStart : otherRunLimit, advance); + if (newCaret == (advance ? otherRunLimit : otherRunStart)) { + // Crossed and ended up at a new boundary, repeat a second and final time. + runIndex = otherRunIndex; + runLevel = otherRunLevel; + continue; + } + break; } - candidate = TextUtils.getOffsetBefore(mText, there); - if (candidate >= start && candidate <= end) { - h = getPrimaryHorizontal(candidate); - - if (h > horiz && h < besth) { - best = candidate; - besth = h; - } + // The new caret is at a boundary. + if (otherRunLevel < runLevel) { + // The strong character is in the other run. + newCaret = advance ? otherRunStart : otherRunLimit; } + break; + } + + if (newCaret == -1) { + // We're walking off the end of the line. The paragraph + // level is always equal to or lower than any internal level, so + // the boundaries get the strong caret. + newCaret = getOffsetBeforeAfter(caret, advance); + break; + } + // Else we've arrived at the end of the line. That's a strong position. + // We might have arrived here by crossing over a run with no internal + // breaks and dropping out of the above loop before advancing one final + // time, so reset the caret. + // Note, we use '<=' below to handle a situation where the only run + // on the line is a counter-directional run. If we're not advancing, + // we can end up at the 'lineEnd' position but the caret we want is at + // the lineStart. + if (newCaret <= lineEnd) { + newCaret = advance ? lineEnd : lineStart; + } + break; + } + + return newCaret; + } - here = there; - } - - float h = getPrimaryHorizontal(end); - - if (h > horiz && h < besth) { - best = end; - besth = h; - } - - if (best != offset) - return best; - - int dir = getParagraphDirection(line); - - if (dir > 0) { - if (line == getLineCount() - 1) - return best; - else - return getOffsetForHorizontal(line + 1, -10000); - } else { - if (line == 0) - return best; - else - return getOffsetForHorizontal(line - 1, -10000); + // utility, maybe just roll into the above. + private int getOffsetBeforeAfter(int offset, boolean after) { + if (after) { + return TextUtils.getOffsetAfter(mText, offset); } + return TextUtils.getOffsetBefore(mText, offset); } private int getOffsetAtStartOf(int offset) { + // XXX this probably should skip local reorderings and + // zero-width characters, look at callers if (offset == 0) return 0; @@ -1115,7 +1171,7 @@ public abstract class Layout { /** * Fills in the specified Path with a representation of a cursor * at the specified offset. This will often be a vertical line - * but can be multiple discontinous lines in text with multiple + * but can be multiple discontinuous lines in text with multiple * directionalities. */ public void getCursorPath(int point, Path dest, @@ -1127,7 +1183,8 @@ public abstract class Layout { int bottom = getLineTop(line+1); float h1 = getPrimaryHorizontal(point) - 0.5f; - float h2 = getSecondaryHorizontal(point) - 0.5f; + float h2 = isLevelBoundary(point) ? + getSecondaryHorizontal(point) - 0.5f : h1; int caps = TextKeyListener.getMetaState(editingBuffer, KeyEvent.META_SHIFT_ON) | @@ -1204,9 +1261,10 @@ public abstract class Layout { if (lineend > linestart && mText.charAt(lineend - 1) == '\n') lineend--; - int here = linestart; - for (int i = 0; i < dirs.mDirections.length; i++) { - int there = here + dirs.mDirections[i]; + for (int i = 0; i < dirs.mDirections.length; i += 2) { + int here = linestart + dirs.mDirections[i]; + int there = here + (dirs.mDirections[i+1] & RUN_LENGTH_MASK); + if (there > lineend) there = lineend; @@ -1215,14 +1273,12 @@ public abstract class Layout { int en = Math.min(end, there); if (st != en) { - float h1 = getHorizontal(st, false, false, line); - float h2 = getHorizontal(en, true, false, line); + float h1 = getHorizontal(st, false, line); + float h2 = getHorizontal(en, true, line); dest.addRect(h1, top, h2, bottom, Path.Direction.CW); } } - - here = there; } } @@ -1257,7 +1313,7 @@ public abstract class Layout { addSelection(startline, start, getLineEnd(startline), top, getLineBottom(startline), dest); - + if (getParagraphDirection(startline) == DIR_RIGHT_TO_LEFT) dest.addRect(getLineLeft(startline), top, 0, getLineBottom(startline), Path.Direction.CW); @@ -1395,27 +1451,31 @@ public abstract class Layout { float h = 0; - int here = 0; - for (int i = 0; i < directions.mDirections.length; i++) { - int there = here + directions.mDirections[i]; - if (there > end - start) - there = end - start; + int lastRunIndex = directions.mDirections.length - 2; + for (int i = 0; i < directions.mDirections.length; i += 2) { + int here = start + directions.mDirections[i]; + int there = here + (directions.mDirections[i+1] & RUN_LENGTH_MASK); + boolean runIsRtl = (directions.mDirections[i+1] & RUN_RTL_FLAG) != 0; + + if (there > end) + there = end; int segstart = here; for (int j = hasTabs ? here : there; j <= there; j++) { - if (j == there || buf[j] == '\t') { + int pj = j - start; + if (j == there || buf[pj] == '\t') { h += Styled.drawText(canvas, text, - start + segstart, start + j, - dir, (i & 1) != 0, x + h, + segstart, j, + dir, runIsRtl, x + h, top, y, bottom, paint, workPaint, - start + j != end); + i != lastRunIndex || j != end); - if (j != there && buf[j] == '\t') + if (j != there) h = dir * nextTab(text, start, end, h * dir, parspans); segstart = j + 1; - } else if (hasTabs && buf[j] >= 0xD800 && buf[j] <= 0xDFFF && j + 1 < there) { - int emoji = Character.codePointAt(buf, j); + } else if (hasTabs && buf[pj] >= 0xD800 && buf[pj] <= 0xDFFF && j + 1 < there) { + int emoji = Character.codePointAt(buf, pj); if (emoji >= MIN_EMOJI && emoji <= MAX_EMOJI) { Bitmap bm = EMOJI_FACTORY. @@ -1423,10 +1483,10 @@ public abstract class Layout { if (bm != null) { h += Styled.drawText(canvas, text, - start + segstart, start + j, - dir, (i & 1) != 0, x + h, + segstart, j, + dir, runIsRtl, x + h, top, y, bottom, paint, workPaint, - start + j != end); + i != lastRunIndex || j != end); if (mEmojiRect == null) { mEmojiRect = new RectF(); @@ -1434,9 +1494,9 @@ public abstract class Layout { workPaint.set(paint); Styled.measureText(paint, workPaint, text, - start + j, start + j + 1, + j, j + 1, null); - + float bitmapHeight = bm.getHeight(); float textHeight = -workPaint.ascent(); float scale = textHeight / bitmapHeight; @@ -1454,21 +1514,24 @@ public abstract class Layout { } } } - - here = there; } if (hasTabs) TextUtils.recycle(buf); } - private static float measureText(TextPaint paint, + /** + * Get the distance from the margin to the requested edge of the character + * at offset on the line from start to end. Trailing indicates the edge + * should be the trailing edge of the character at offset-1. + */ + /* package */ static float measureText(TextPaint paint, TextPaint workPaint, CharSequence text, int start, int offset, int end, int dir, Directions directions, - boolean trailing, boolean alt, - boolean hasTabs, Object[] tabs) { + boolean trailing, boolean hasTabs, + Object[] tabs) { char[] buf = null; if (hasTabs) { @@ -1478,19 +1541,19 @@ public abstract class Layout { float h = 0; - if (alt) { - if (dir == DIR_RIGHT_TO_LEFT) - trailing = !trailing; + int target = trailing ? offset - 1 : offset; + if (target < start) { + return 0; } - int here = 0; - for (int i = 0; i < directions.mDirections.length; i++) { - if (alt) - trailing = !trailing; - - int there = here + directions.mDirections[i]; - if (there > end - start) - there = end - start; + int[] runs = directions.mDirections; + for (int i = 0; i < runs.length; i += 2) { + int here = start + runs[i]; + int there = here + (runs[i+1] & RUN_LENGTH_MASK); + if (there > end) { + there = end; + } + boolean runIsRtl = (runs[i+1] & RUN_RTL_FLAG) != 0; int segstart = here; for (int j = hasTabs ? here : there; j <= there; j++) { @@ -1498,11 +1561,11 @@ public abstract class Layout { Bitmap bm = null; if (hasTabs && j < there) { - codept = buf[j]; + codept = buf[j - start]; } if (codept >= 0xD800 && codept <= 0xDFFF && j + 1 < there) { - codept = Character.codePointAt(buf, j); + codept = Character.codePointAt(buf, j - start); if (codept >= MIN_EMOJI && codept <= MAX_EMOJI) { bm = EMOJI_FACTORY.getBitmapFromAndroidPua(codept); @@ -1512,33 +1575,34 @@ public abstract class Layout { if (j == there || codept == '\t' || bm != null) { float segw; - if (offset < start + j || - (trailing && offset <= start + j)) { - if (dir == DIR_LEFT_TO_RIGHT && (i & 1) == 0) { + boolean inSegment = target >= segstart && target < j; + + if (inSegment) { + if (dir == DIR_LEFT_TO_RIGHT && !runIsRtl) { h += Styled.measureText(paint, workPaint, text, - start + segstart, offset, + segstart, offset, null); return h; } - if (dir == DIR_RIGHT_TO_LEFT && (i & 1) != 0) { + if (dir == DIR_RIGHT_TO_LEFT && runIsRtl) { h -= Styled.measureText(paint, workPaint, text, - start + segstart, offset, + segstart, offset, null); return h; } } + // XXX Style.measureText assumes LTR? segw = Styled.measureText(paint, workPaint, text, - start + segstart, start + j, + segstart, j, null); - if (offset < start + j || - (trailing && offset <= start + j)) { + if (inSegment) { if (dir == DIR_LEFT_TO_RIGHT) { h += segw - Styled.measureText(paint, workPaint, text, - start + segstart, + segstart, offset, null); return h; } @@ -1546,7 +1610,7 @@ public abstract class Layout { if (dir == DIR_RIGHT_TO_LEFT) { h -= segw - Styled.measureText(paint, workPaint, text, - start + segstart, + segstart, offset, null); return h; } @@ -1557,11 +1621,15 @@ public abstract class Layout { else h += segw; - if (j != there && buf[j] == '\t') { - if (offset == start + j) + if (j != there && buf[j - start] == '\t') { + if (offset == j) return h; h = dir * nextTab(text, start, end, h * dir, tabs); + + if (target == j) { + return h; + } } if (bm != null) { @@ -1569,7 +1637,7 @@ public abstract class Layout { Styled.measureText(paint, workPaint, text, j, j + 2, null); - float wid = (float) bm.getWidth() * + float wid = bm.getWidth() * -workPaint.ascent() / bm.getHeight(); if (dir == DIR_RIGHT_TO_LEFT) { @@ -1584,8 +1652,6 @@ public abstract class Layout { segstart = j + 1; } } - - here = there; } if (hasTabs) @@ -1616,7 +1682,7 @@ public abstract class Layout { Paint.FontMetricsInt fm, boolean hasTabs, Object[] tabs) { char[] buf = null; - + if (hasTabs) { buf = TextUtils.obtain(end - start); TextUtils.getChars(text, start, end, buf, 0); @@ -1652,6 +1718,8 @@ public abstract class Layout { if (pos == len || codept == '\t' || bm != null) { workPaint.baselineShift = 0; + // XXX Styled.measureText assumes the run direction is LTR, + // but it might not be. Check this. width += Styled.measureText(paint, workPaint, text, start + lastPos, start + pos, fm); @@ -1683,7 +1751,7 @@ public abstract class Layout { Styled.measureText(paint, workPaint, text, start + pos, start + pos + 1, null); - width += (float) bm.getWidth() * + width += bm.getWidth() * -workPaint.ascent() / bm.getHeight(); // Since we had an emoji, we bump past the second half @@ -1804,23 +1872,22 @@ public abstract class Layout { /** * Stores information about bidirectional (left-to-right or right-to-left) - * text within the layout of a line. TODO: This work is not complete - * or correct and will be fleshed out in a later revision. + * text within the layout of a line. */ public static class Directions { - private short[] mDirections; - - // The values in mDirections are the offsets from the first character - // in the line to the next flip in direction. Runs at even indices - // are left-to-right, the others are right-to-left. So, for example, - // a line that starts with a right-to-left run has 0 at mDirections[0], - // since the 'first' (ltr) run is zero length. - // - // The code currently assumes that each run is adjacent to the previous - // one, progressing in the base line direction. This isn't sufficient - // to handle nested runs, for example numeric text in an rtl context - // in an ltr paragraph. - /* package */ Directions(short[] dirs) { + // Directions represents directional runs within a line of text. + // Runs are pairs of ints listed in visual order, starting from the + // leading margin. The first int of each pair is the offset from + // the first character of the line to the start of the run. The + // second int represents both the length and level of the run. + // The length is in the lower bits, accessed by masking with + // DIR_LENGTH_MASK. The level is in the higher bits, accessed + // by shifting by DIR_LEVEL_SHIFT and masking by DIR_LEVEL_MASK. + // To simply test for an RTL direction, test the bit using + // DIR_RTL_FLAG, if set then the direction is rtl. + + /* package */ int[] mDirections; + /* package */ Directions(int[] dirs) { mDirections = dirs; } } @@ -1870,7 +1937,7 @@ public abstract class Layout { public int length() { return mText.length(); } - + public CharSequence subSequence(int start, int end) { char[] s = new char[end - start]; getChars(start, end, s, 0); @@ -1936,12 +2003,17 @@ public abstract class Layout { public static final int DIR_LEFT_TO_RIGHT = 1; public static final int DIR_RIGHT_TO_LEFT = -1; - + /* package */ static final int DIR_REQUEST_LTR = 1; /* package */ static final int DIR_REQUEST_RTL = -1; /* package */ static final int DIR_REQUEST_DEFAULT_LTR = 2; /* package */ static final int DIR_REQUEST_DEFAULT_RTL = -2; + /* package */ static final int RUN_LENGTH_MASK = 0x03ffffff; + /* package */ static final int RUN_LEVEL_SHIFT = 26; + /* package */ static final int RUN_LEVEL_MASK = 0x3f; + /* package */ static final int RUN_RTL_FLAG = 1 << RUN_LEVEL_SHIFT; + public enum Alignment { ALIGN_NORMAL, ALIGN_OPPOSITE, @@ -1953,9 +2025,8 @@ public abstract class Layout { private static final int TAB_INCREMENT = 20; /* package */ static final Directions DIRS_ALL_LEFT_TO_RIGHT = - new Directions(new short[] { 32767 }); + new Directions(new int[] { 0, RUN_LENGTH_MASK }); /* package */ static final Directions DIRS_ALL_RIGHT_TO_LEFT = - new Directions(new short[] { 0, 32767 }); - + new Directions(new int[] { 0, RUN_LENGTH_MASK | RUN_RTL_FLAG }); } diff --git a/core/java/android/text/StaticLayout.java b/core/java/android/text/StaticLayout.java index f02ad2a..bfa0ab6 100644 --- a/core/java/android/text/StaticLayout.java +++ b/core/java/android/text/StaticLayout.java @@ -16,14 +16,15 @@ package android.text; +import com.android.internal.util.ArrayUtils; + import android.graphics.Bitmap; import android.graphics.Paint; -import com.android.internal.util.ArrayUtils; -import android.util.Log; import android.text.style.LeadingMarginSpan; import android.text.style.LineHeightSpan; import android.text.style.MetricAffectingSpan; import android.text.style.ReplacementSpan; +import android.util.Log; /** * StaticLayout is a Layout for text that will not be edited after it @@ -31,8 +32,9 @@ import android.text.style.ReplacementSpan; * <p>This is used by widgets to control text layout. You should not need * to use this class directly unless you are implementing your own widget * or custom display object, or would be tempted to call - * {@link android.graphics.Canvas#drawText(java.lang.CharSequence, int, int, float, float, android.graphics.Paint) - * Canvas.drawText()} directly.</p> + * {@link android.graphics.Canvas#drawText(java.lang.CharSequence, int, int, + * float, float, android.graphics.Paint) + * Canvas.drawText()} directly.</p> */ public class StaticLayout @@ -62,7 +64,7 @@ extends Layout boolean includepad, TextUtils.TruncateAt ellipsize, int ellipsizedWidth) { super((ellipsize == null) - ? source + ? source : (source instanceof Spanned) ? new SpannedEllipsizer(source) : new Ellipsizer(source), @@ -72,7 +74,7 @@ extends Layout * This is annoying, but we can't refer to the layout until * superclass construction is finished, and the superclass * constructor wants the reference to the display text. - * + * * This will break if the superclass constructor ever actually * cares about the content instead of just holding the reference. */ @@ -196,7 +198,7 @@ extends Layout // starts in this layout, before the // current paragraph - choosehtv[i] = getLineTop(getLineForOffset(o)); + choosehtv[i] = getLineTop(getLineForOffset(o)); } else { // starts in this paragraph @@ -224,7 +226,7 @@ extends Layout boolean easy = true; boolean altered = false; - int dir = DEFAULT_DIR; // XXX + int dir = DEFAULT_DIR; // XXX pass value in for (int i = 0; i < n; i++) { if (chs[i] >= FIRST_RIGHT_TO_LEFT) { @@ -253,7 +255,8 @@ extends Layout if (!easy) { // XXX put override flags, etc. into chdirs - dir = bidi(dir, chs, chdirs, n, false); + // XXX supply dir rather than force + dir = AndroidBidi.bidi(DIR_REQUEST_DEFAULT_LTR, chs, chdirs, n, false); // Do mirroring for right-to-left segments @@ -316,7 +319,7 @@ extends Layout paint.getTextWidths(sub, i, next, widths); System.arraycopy(widths, 0, widths, end - start + (i - start), next - i); - + paint.getFontMetricsInt(fm); } else { mWorkPaint.baselineShift = 0; @@ -376,7 +379,7 @@ extends Layout whichPaint = mWorkPaint; } - float wid = (float) bm.getWidth() * + float wid = bm.getWidth() * -whichPaint.ascent() / bm.getHeight(); @@ -411,7 +414,7 @@ extends Layout /* * From the Unicode Line Breaking Algorithm: * (at least approximately) - * + * * .,:; are class IS: breakpoints * except when adjacent to digits * / is class SY: a breakpoint @@ -606,239 +609,6 @@ extends Layout } } - /** - * Runs the unicode bidi algorithm on the first n chars in chs, returning - * the char dirs in chInfo and the base line direction of the first - * paragraph. - * - * XXX change result from dirs to levels - * - * @param dir the direction flag, either DIR_REQUEST_LTR, - * DIR_REQUEST_RTL, DIR_REQUEST_DEFAULT_LTR, or DIR_REQUEST_DEFAULT_RTL. - * @param chs the text to examine - * @param chInfo on input, if hasInfo is true, override and other flags - * representing out-of-band embedding information. On output, the generated - * dirs of the text. - * @param n the length of the text/information in chs and chInfo - * @param hasInfo true if chInfo has input information, otherwise the - * input data in chInfo is ignored. - * @return the resolved direction level of the first paragraph, either - * DIR_LEFT_TO_RIGHT or DIR_RIGHT_TO_LEFT. - */ - /* package */ static int bidi(int dir, char[] chs, byte[] chInfo, int n, - boolean hasInfo) { - - AndroidCharacter.getDirectionalities(chs, chInfo, n); - - /* - * Determine primary paragraph direction if not specified - */ - if (dir != DIR_REQUEST_LTR && dir != DIR_REQUEST_RTL) { - // set up default - dir = dir >= 0 ? DIR_LEFT_TO_RIGHT : DIR_RIGHT_TO_LEFT; - for (int j = 0; j < n; j++) { - int d = chInfo[j]; - - if (d == Character.DIRECTIONALITY_LEFT_TO_RIGHT) { - dir = DIR_LEFT_TO_RIGHT; - break; - } - if (d == Character.DIRECTIONALITY_RIGHT_TO_LEFT) { - dir = DIR_RIGHT_TO_LEFT; - break; - } - } - } - - final byte SOR = dir == DIR_LEFT_TO_RIGHT ? - Character.DIRECTIONALITY_LEFT_TO_RIGHT : - Character.DIRECTIONALITY_RIGHT_TO_LEFT; - - /* - * XXX Explicit overrides should go here - */ - - /* - * Weak type resolution - */ - - // dump(chdirs, n, "initial"); - - // W1 non spacing marks - for (int j = 0; j < n; j++) { - if (chInfo[j] == Character.NON_SPACING_MARK) { - if (j == 0) - chInfo[j] = SOR; - else - chInfo[j] = chInfo[j - 1]; - } - } - - // dump(chdirs, n, "W1"); - - // W2 european numbers - byte cur = SOR; - for (int j = 0; j < n; j++) { - byte d = chInfo[j]; - - if (d == Character.DIRECTIONALITY_LEFT_TO_RIGHT || - d == Character.DIRECTIONALITY_RIGHT_TO_LEFT || - d == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC) - cur = d; - else if (d == Character.DIRECTIONALITY_EUROPEAN_NUMBER) { - if (cur == - Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC) - chInfo[j] = Character.DIRECTIONALITY_ARABIC_NUMBER; - } - } - - // dump(chdirs, n, "W2"); - - // W3 arabic letters - for (int j = 0; j < n; j++) { - if (chInfo[j] == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC) - chInfo[j] = Character.DIRECTIONALITY_RIGHT_TO_LEFT; - } - - // dump(chdirs, n, "W3"); - - // W4 single separator between numbers - for (int j = 1; j < n - 1; j++) { - byte d = chInfo[j]; - byte prev = chInfo[j - 1]; - byte next = chInfo[j + 1]; - - if (d == Character.DIRECTIONALITY_EUROPEAN_NUMBER_SEPARATOR) { - if (prev == Character.DIRECTIONALITY_EUROPEAN_NUMBER && - next == Character.DIRECTIONALITY_EUROPEAN_NUMBER) - chInfo[j] = Character.DIRECTIONALITY_EUROPEAN_NUMBER; - } else if (d == Character.DIRECTIONALITY_COMMON_NUMBER_SEPARATOR) { - if (prev == Character.DIRECTIONALITY_EUROPEAN_NUMBER && - next == Character.DIRECTIONALITY_EUROPEAN_NUMBER) - chInfo[j] = Character.DIRECTIONALITY_EUROPEAN_NUMBER; - if (prev == Character.DIRECTIONALITY_ARABIC_NUMBER && - next == Character.DIRECTIONALITY_ARABIC_NUMBER) - chInfo[j] = Character.DIRECTIONALITY_ARABIC_NUMBER; - } - } - - // dump(chdirs, n, "W4"); - - // W5 european number terminators - boolean adjacent = false; - for (int j = 0; j < n; j++) { - byte d = chInfo[j]; - - if (d == Character.DIRECTIONALITY_EUROPEAN_NUMBER) - adjacent = true; - else if (d == Character.DIRECTIONALITY_EUROPEAN_NUMBER_TERMINATOR && adjacent) - chInfo[j] = Character.DIRECTIONALITY_EUROPEAN_NUMBER; - else - adjacent = false; - } - - //dump(chdirs, n, "W5"); - - // W5 european number terminators part 2, - // W6 separators and terminators - adjacent = false; - for (int j = n - 1; j >= 0; j--) { - byte d = chInfo[j]; - - if (d == Character.DIRECTIONALITY_EUROPEAN_NUMBER) - adjacent = true; - else if (d == Character.DIRECTIONALITY_EUROPEAN_NUMBER_TERMINATOR) { - if (adjacent) - chInfo[j] = Character.DIRECTIONALITY_EUROPEAN_NUMBER; - else - chInfo[j] = Character.DIRECTIONALITY_OTHER_NEUTRALS; - } - else { - adjacent = false; - - if (d == Character.DIRECTIONALITY_EUROPEAN_NUMBER_SEPARATOR || - d == Character.DIRECTIONALITY_COMMON_NUMBER_SEPARATOR || - d == Character.DIRECTIONALITY_PARAGRAPH_SEPARATOR || - d == Character.DIRECTIONALITY_SEGMENT_SEPARATOR) - chInfo[j] = Character.DIRECTIONALITY_OTHER_NEUTRALS; - } - } - - // dump(chdirs, n, "W6"); - - // W7 strong direction of european numbers - cur = SOR; - for (int j = 0; j < n; j++) { - byte d = chInfo[j]; - - if (d == SOR || - d == Character.DIRECTIONALITY_LEFT_TO_RIGHT || - d == Character.DIRECTIONALITY_RIGHT_TO_LEFT) - cur = d; - - if (d == Character.DIRECTIONALITY_EUROPEAN_NUMBER) - chInfo[j] = cur; - } - - // dump(chdirs, n, "W7"); - - // N1, N2 neutrals - cur = SOR; - for (int j = 0; j < n; j++) { - byte d = chInfo[j]; - - if (d == Character.DIRECTIONALITY_LEFT_TO_RIGHT || - d == Character.DIRECTIONALITY_RIGHT_TO_LEFT) { - cur = d; - } else if (d == Character.DIRECTIONALITY_EUROPEAN_NUMBER || - d == Character.DIRECTIONALITY_ARABIC_NUMBER) { - cur = Character.DIRECTIONALITY_RIGHT_TO_LEFT; - } else { - byte dd = SOR; - int k; - - for (k = j + 1; k < n; k++) { - dd = chInfo[k]; - - if (dd == Character.DIRECTIONALITY_LEFT_TO_RIGHT || - dd == Character.DIRECTIONALITY_RIGHT_TO_LEFT) { - break; - } - if (dd == Character.DIRECTIONALITY_EUROPEAN_NUMBER || - dd == Character.DIRECTIONALITY_ARABIC_NUMBER) { - dd = Character.DIRECTIONALITY_RIGHT_TO_LEFT; - break; - } - } - - for (int y = j; y < k; y++) { - if (dd == cur) - chInfo[y] = cur; - else - chInfo[y] = SOR; - } - - j = k - 1; - } - } - - // dump(chdirs, n, "final"); - - // extra: enforce that all tabs and surrogate characters go the - // primary direction - // TODO: actually do directions right for surrogates - - for (int j = 0; j < n; j++) { - char c = chs[j]; - - if (c == '\t' || (c >= 0xD800 && c <= 0xDFFF)) { - chInfo[j] = SOR; - } - } - - return dir; - } - private static final char FIRST_CJK = '\u2E80'; /** * Returns true if the specified character is one of those specified @@ -1062,49 +832,123 @@ extends Layout if (tab) lines[off + TAB] |= TAB_MASK; - { - lines[off + DIR] |= dir << DIR_SHIFT; + lines[off + DIR] |= dir << DIR_SHIFT; + Directions linedirs = DIRS_ALL_LEFT_TO_RIGHT; + // easy means all chars < the first RTL, so no emoji, no nothing + // XXX a run with no text or all spaces is easy but might be an empty + // RTL paragraph. Make sure easy is false if this is the case. + if (easy) { + mLineDirections[j] = linedirs; + } else { + int startOff = start - pstart; + int baseLevel = dir == DIR_LEFT_TO_RIGHT ? 0 : 1; + int curLevel = chdirs[startOff]; + int minLevel = curLevel; + int runCount = 1; + for (int i = start + 1; i < end; ++i) { + int level = chdirs[i - pstart]; + if (level != curLevel) { + curLevel = level; + ++runCount; + } + } - int cur = Character.DIRECTIONALITY_LEFT_TO_RIGHT; - int count = 0; + // add final run for trailing counter-directional whitespace + int visEnd = end; + if ((curLevel & 1) != (baseLevel & 1)) { + // look for visible end + while (--visEnd >= start) { + char ch = text.charAt(visEnd); - if (!easy) { - for (int k = start; k < end; k++) { - if (chdirs[k - pstart] != cur) { - count++; - cur = chdirs[k - pstart]; + if (ch == '\n') { + --visEnd; + break; + } + + if (ch != ' ' && ch != '\t') { + break; } } + ++visEnd; + if (visEnd != end) { + ++runCount; + } } - Directions linedirs; - - if (count == 0) { - linedirs = DIRS_ALL_LEFT_TO_RIGHT; + if (runCount == 1 && minLevel == baseLevel) { + if ((minLevel & 1) != 0) { + linedirs = DIRS_ALL_RIGHT_TO_LEFT; + } + // we're done, only one run on this line } else { - short[] ld = new short[count + 1]; - - cur = Character.DIRECTIONALITY_LEFT_TO_RIGHT; - count = 0; - int here = start; - - for (int k = start; k < end; k++) { - if (chdirs[k - pstart] != cur) { - // XXX check to make sure we don't - // overflow short - ld[count++] = (short) (k - here); - cur = chdirs[k - pstart]; - here = k; + int[] ld = new int[runCount * 2]; + int maxLevel = minLevel; + int levelBits = minLevel << RUN_LEVEL_SHIFT; + { + // Start of first pair is always 0, we write + // length then start at each new run, and the + // last run length after we're done. + int n = 1; + int prev = start; + curLevel = minLevel; + for (int i = start; i < visEnd; ++i) { + int level = chdirs[i - pstart]; + if (level != curLevel) { + curLevel = level; + if (level > maxLevel) { + maxLevel = level; + } else if (level < minLevel) { + minLevel = level; + } + // XXX ignore run length limit of 2^RUN_LEVEL_SHIFT + ld[n++] = (i - prev) | levelBits; + ld[n++] = i - start; + levelBits = curLevel << RUN_LEVEL_SHIFT; + prev = i; + } + } + ld[n] = (visEnd - prev) | levelBits; + if (visEnd < end) { + ld[++n] = visEnd - start; + ld[++n] = (end - visEnd) | (baseLevel << RUN_LEVEL_SHIFT); } } - ld[count] = (short) (end - here); - - if (count == 1 && ld[0] == 0) { - linedirs = DIRS_ALL_RIGHT_TO_LEFT; + // See if we need to swap any runs. + // If the min level run direction doesn't match the base + // direction, we always need to swap (at this point + // we have more than one run). + // Otherwise, we don't need to swap the lowest level. + // Since there are no logically adjacent runs at the same + // level, if the max level is the same as the (new) min + // level, we have a series of alternating levels that + // is already in order, so there's no more to do. + // + boolean swap; + if ((minLevel & 1) == baseLevel) { + minLevel += 1; + swap = maxLevel > minLevel; } else { - linedirs = new Directions(ld); + swap = runCount > 1; + } + if (swap) { + for (int level = maxLevel - 1; level >= minLevel; --level) { + for (int i = 0; i < ld.length; i += 2) { + if (chdirs[startOff + ld[i]] >= level) { + int e = i + 2; + while (e < ld.length && chdirs[startOff + ld[e]] >= level) { + e += 2; + } + for (int low = i, hi = e - 2; low < hi; low += 2, hi -= 2) { + int x = ld[low]; ld[low] = ld[hi]; ld[hi] = x; + x = ld[low+1]; ld[low+1] = ld[hi+1]; ld[hi+1] = x; + } + i = e + 2; + } + } + } } + linedirs = new Directions(ld); } mLineDirections[j] = linedirs; @@ -1232,11 +1076,11 @@ extends Layout } public int getLineTop(int line) { - return mLines[mColumns * line + TOP]; + return mLines[mColumns * line + TOP]; } public int getLineDescent(int line) { - return mLines[mColumns * line + DESCENT]; + return mLines[mColumns * line + DESCENT]; } public int getLineStart(int line) { diff --git a/core/java/android/widget/AbsListView.java b/core/java/android/widget/AbsListView.java index 48e7f79..0a39ab6 100644 --- a/core/java/android/widget/AbsListView.java +++ b/core/java/android/widget/AbsListView.java @@ -559,6 +559,16 @@ public abstract class AbsListView extends AdapterView<ListAdapter> implements Te boolean smoothScrollbar = a.getBoolean(R.styleable.AbsListView_smoothScrollbar, true); setSmoothScrollbarEnabled(smoothScrollbar); + + final int adapterId = a.getResourceId(R.styleable.AbsListView_adapter, 0); + if (adapterId != 0) { + final Context c = context; + post(new Runnable() { + public void run() { + setAdapter(Adapters.loadAdapter(c, adapterId)); + } + }); + } a.recycle(); } diff --git a/core/java/android/widget/Adapters.java b/core/java/android/widget/Adapters.java new file mode 100644 index 0000000..05e501a --- /dev/null +++ b/core/java/android/widget/Adapters.java @@ -0,0 +1,1191 @@ +/* + * Copyright (C) 2010 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 android.app.Activity; +import android.content.Context; +import android.content.res.Resources; +import android.content.res.TypedArray; +import android.content.res.XmlResourceParser; +import android.database.Cursor; +import android.graphics.BitmapFactory; +import android.net.Uri; +import android.os.AsyncTask; +import android.util.AttributeSet; +import android.util.Xml; +import android.view.View; +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; + +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.HashMap; + +import static com.android.internal.R.*; + +/** + * <p>This class can be used to load {@link android.widget.Adapter adapters} defined in + * XML resources. XML-defined adapters can be used to easily create adapters in your + * own application or to pass adapters to other processes.</p> + * + * <h2>Types of adapters</h2> + * <p>Adapters defined using XML resources can only be one of the following supported + * types. Arbitrary adapters are not supported to guarantee the safety of the loaded + * code when adapters are loaded across packages.</p> + * <ul> + * <li><a href="#xml-cursor-adapter">Cursor adapter</a>: a cursor adapter can be used + * to display the content of a cursor, most often coming from a content provider</li> + * </ul> + * <p>The complete XML format definition of each adapter type is available below.</p> + * + * <a name="xml-cursor-adapter" /> + * <h2>Cursor adapter</h2> + * <p>A cursor adapter XML definition starts with the + * <a href="#xml-cursor-adapter-tag"><code><cursor-adapter /></code></a> + * tag and may contain one or more instances of the following tags:</p> + * <ul> + * <li><a href="#xml-cursor-adapter-select-tag"><code><select /></code></a></li> + * <li><a href="#xml-cursor-adapter-bind-tag"><code><bind /></code></a></li> + * </ul> + * + * <a name="xml-cursor-adapter-tag" /> + * <h3><cursor-adapter /></h3> + * <p>The <code><cursor-adapter /></code> element defines the beginning of the + * document and supports the following attributes:</p> + * <ul> + * <li><code>android:layout</code>: Reference to the XML layout to be inflated for + * each item of the adapter. This attribute is mandatory.</li> + * <li><code>android:selection</code>: Selection expression, used when the + * <code>android:uri</code> attribute is defined or when the adapter is loaded with + * {@link android.widget.Adapters#loadCursorAdapter(android.content.Context, int, String, Object[])}. + * This attribute is optional.</li> + * <li><code>android:sortOrder</code>: Sort expression, used when the + * <code>android:uri</code> attribute is defined or when the adapter is loaded with + * {@link android.widget.Adapters#loadCursorAdapter(android.content.Context, int, String, Object[])}. + * This attribute is optional.</li> + * <li><code>android:uri</code>: URI of the content provider to query to retrieve a cursor. + * Specifying this attribute is equivalent to calling {@link android.widget.Adapters#loadCursorAdapter(android.content.Context, int, String, Object[])}. + * If you call this method, the value of the XML attribute is ignored. This attribute is + * optional.</li> + * </ul> + * <p>In addition, you can specify one or more instances of + * <a href="#xml-cursor-adapter-select-tag"><code><select /></code></a> and + * <a href="#xml-cursor-adapter-bind-tag"><code><bind /></code></a> tags as children + * of <code><cursor-adapter /></code>.</p> + * + * <a name="xml-cursor-adapter-select-tag" /> + * <h3><select /></h3> + * <p>The <code><select /></code> tag is used to select columns from the cursor + * when doing the query. This can be very useful when using transformations in the + * <code><bind /></code> elements. It can also be very useful if you are providing + * your own <a href="#xml-cursor-adapter-bind-data-types">binder</a> or + * <a href="#xml-cursor-adapter-bind-data-types">transformation</a> classes. + * <code><select /></code> elements are ignored if you supply the cursor yourself.</p> + * <p>The <code><select /></code> supports the following attributes:</p> + * <ul> + * <li><code>android:column</code>: Name of the column to select in the cursor during the + * query operation</li> + * </ul> + * <p><strong>Note:</strong> The column named <code>_id</code> is always implicitely + * selected.</p> + * + * <a name="xml-cursor-adapter-bind-tag" /> + * <h3><bind /></h3> + * <p>The <code><bind /></code> tag is used to bind a column from the cursor to + * a {@link android.view.View}. A column bound using this tag is automatically selected + * during the query and a matching + * <a href="#xml-cursor-adapter-select-tag"><code><select /></code> tag is therefore + * not required.</p> + * + * <p>Each binding is declared as a one to one matching but + * custom binder classes or special + * <a href="#xml-cursor-adapter-bind-data-transformation">data transformations</a> can + * allow you to bind several columns to a single view. In this case you must use the + * <a href="#xml-cursor-adapter-select-tag"><code><select /></code> tag to make + * sure any required column is part of the query.</p> + * + * <p>The <code><bind /></code> tag supports the following attributes:</p> + * <ul> + * <li><code>android:from</code>: The name of the column to bind from. + * This attribute is mandatory.</li> + * <li><code>android:to</code>: The id of the view to bind to. This attribute is mandatory.</li> + * <li><code>android:as</code>: The <a href="#xml-cursor-adapter-bind-data-types">data type</a> + * of the binding. This attribute is mandatory.</li> + * </ul> + * + * <p>In addition, a <code><bind /></code> can contain zero or more instances of + * <a href="#xml-cursor-adapter-bind-data-transformation">data transformations</a> chilren + * tags.</p> + * + * <a name="xml-cursor-adapter-bind-data-types" /> + * <h4>Binding data types</h4> + * <p>For a binding to occur the data type of the bound column/view pair must be specified. + * The following data types are currently supported:</p> + * <ul> + * <li><code>string</code>: The content of the column is interpreted as a string and must be + * bound to a {@link android.widget.TextView}</li> + * <li><code>image</code>: The content of the column is interpreted as a blob describing an + * image and must be bound to an {@link android.widget.ImageView}</li> + * <li><code>image-uri</code>: The content of the column is interpreted as a URI to an image + * and must be bound to an {@link android.widget.ImageView}</li> + * <li><code>drawable</code>: The content of the column is interpreted as a resource id to a + * drawable and must be bound to an {@link android.widget.ImageView}</li> + * <li>A fully qualified class name: The name of a class corresponding to an implementation of + * {@link android.widget.Adapters.CursorBinder}. Cursor binders can be used to provide + * bindings not supported by default. Custom binders cannot be used with + * {@link android.content.Context#isRestricted() restricted contexts}, for instance in an + * app widget</li> + * </ul> + * + * <a name="xml-cursor-adapter-bind-transformation" /> + * <h4>Binding transformations</h4> + * <p>When defining a data binding you can specify an optional transformation by using one + * of the following tags as a child of a <code><bind /></code> elements:</p> + * <ul> + * <li><code><map /></code>: Maps a constant string to a string or a resource. Use + * one instance of this tag per value you want to map</li> + * <li><code><transform /></code>: Transforms a column's value using an expression + * or an instance of {@link android.widget.Adapters.CursorTransformation}</li> + * </ul> + * <p>While several <code><map /></code> tags can be used at the same time, you cannot + * mix <code><map /></code> and <code><transform /></code> tags. If several + * <code><transform /></code> tags are specified, only the last one is retained.</p> + * + * <a name="xml-cursor-adapter-bind-transformation-map" /> + * <p><strong><map /></strong></p> + * <p>A map element simply specifies a value to match from and a value to match to. When + * a column's value equals the value to match from, it is replaced with the value to match + * to. The following attributes are supported:</p> + * <ul> + * <li><code>android:fromValue</code>: The value to match from. This attribute is mandatory</li> + * <li><code>android:toValue</code>: The value to match to. This value can be either a string + * or a resource identifier. This value is interpreted as a resource identifier when the + * data binding is of type <code>drawable</code>. This attribute is mandatory</li> + * </ul> + * + * <a name="xml-cursor-adapter-bind-transformation-transform" /> + * <p><strong><transform /></strong></p> + * <p>A simple transform that occurs either by calling a specified class or by performing + * simple text substitution. The following attributes are supported:</p> + * <ul> + * <li><code>android:withExpression</code>: The transformation expression. The expression is + * a string containing column names surrounded with curly braces { and }. During the + * transformation each column name is replaced by its value. All columns must have been + * selected in the query. An example of expression is <code>"First name: {first_name}, + * last name: {last_name}"</code>. This attribute is mandatory + * if <code>android:withClass</code> is not specified and ignored if <code>android:withClass</code> + * is specified</li> + * <li><code>android:withClass</code>: A fully qualified class name corresponding to an + * implementation of {@link android.widget.Adapters.CursorTransformation}. Custom + * transformationscannot be used with + * {@link android.content.Context#isRestricted() restricted contexts}, for instance in + * an app widget This attribute is mandatory if <code>android:withExpression</code> is + * not specified</li> + * </ul> + * + * <h3>Example</h3> + * <p>The following example defines a cursor adapter that queries all the contacts with + * a phone number using the contacts content provider. Each contact is displayed with + * its display name, its favorite status and its photo. To display photos, a custom data + * binder is declared:</p> + * + * <pre class="prettyprint"> + * <cursor-adapter xmlns:android="http://schemas.android.com/apk/res/android" + * android:uri="content://com.android.contacts/contacts" + * android:selection="has_phone_number=1" + * android:layout="@layout/contact_item"> + * + * <bind android:from="display_name" android:to="@id/name" android:as="string" /> + * <bind android:from="starred" android:to="@id/star" android:as="drawable"> + * <map android:fromValue="0" android:toValue="@android:drawable/star_big_off" /> + * <map android:fromValue="1" android:toValue="@android:drawable/star_big_on" /> + * </bind> + * <bind android:from="_id" android:to="@id/name" + * android:as="com.google.android.test.adapters.ContactPhotoBinder" /> + * + * </cursor-adapter> + * </pre> + * + * <h3>Related APIs</h3> + * <ul> + * <li>{@link android.widget.Adapters#loadAdapter(android.content.Context, int, Object[])}</li> + * <li>{@link android.widget.Adapters#loadCursorAdapter(android.content.Context, int, android.database.Cursor, Object[])}</li> + * <li>{@link android.widget.Adapters#loadCursorAdapter(android.content.Context, int, String, Object[])}</li> + * <li>{@link android.widget.Adapters.CursorBinder}</li> + * <li>{@link android.widget.Adapters.CursorTransformation}</li> + * <li>{@link android.widget.CursorAdapter}</li> + * </ul> + * + * @see android.widget.Adapter + * @see android.content.ContentProvider + * + * @attr ref android.R.styleable#CursorAdapter_layout + * @attr ref android.R.styleable#CursorAdapter_selection + * @attr ref android.R.styleable#CursorAdapter_sortOrder + * @attr ref android.R.styleable#CursorAdapter_uri + * @attr ref android.R.styleable#CursorAdapter_BindItem_as + * @attr ref android.R.styleable#CursorAdapter_BindItem_from + * @attr ref android.R.styleable#CursorAdapter_BindItem_to + * @attr ref android.R.styleable#CursorAdapter_MapItem_fromValue + * @attr ref android.R.styleable#CursorAdapter_MapItem_toValue + * @attr ref android.R.styleable#CursorAdapter_SelectItem_column + * @attr ref android.R.styleable#CursorAdapter_TransformItem_withClass + * @attr ref android.R.styleable#CursorAdapter_TransformItem_withExpression + */ +@SuppressWarnings({"JavadocReference"}) +public class Adapters { + private static final String ADAPTER_CURSOR = "cursor-adapter"; + + /** + * <p>Interface used to bind a {@link android.database.Cursor} column to a View. This + * interface can be used to provide bindings for data types not supported by the + * standard implementation of {@link android.widget.Adapters}.</p> + * + * <p>A binder is provided with a cursor transformation which may or may not be used + * to transform the value retrieved from the cursor. The transformation is guaranteed + * to never be null so it's always safe to apply the transformation.</p> + * + * <p>The binder is associated with a Context but can be re-used with multiple cursors. + * As such, the implementation should make no assumption about the Cursor in use.</p> + * + * @see android.view.View + * @see android.database.Cursor + * @see android.widget.Adapters.CursorTransformation + */ + public static abstract class CursorBinder { + /** + * <p>The context associated with this binder.</p> + */ + protected final Context mContext; + + /** + * <p>The transformation associated with this binder. This transformation is never + * null and may or may not be applied to the Cursor data during the + * {@link #bind(android.view.View, android.database.Cursor, int)} operation.</p> + * + * @see #bind(android.view.View, android.database.Cursor, int) + */ + protected final CursorTransformation mTransformation; + + /** + * <p>Creates a new Cursor binder.</p> + * + * @param context The context associated with this binder. + * @param transformation The transformation associated with this binder. This + * transformation may or may not be applied by the binder and is guaranteed + * to not be null. + */ + public CursorBinder(Context context, CursorTransformation transformation) { + mContext = context; + mTransformation = transformation; + } + + /** + * <p>Binds the specified Cursor column to the supplied View. The binding operation + * can query other Cursor columns as needed. During the binding operation, values + * retrieved from the Cursor may or may not be transformed using this binder's + * cursor transformation.</p> + * + * @param view The view to bind data to. + * @param cursor The cursor to bind data from. + * @param columnIndex The column index in the cursor where the data to bind resides. + * + * @see #mTransformation + * + * @return True if the column was successfully bound to the View, false otherwise. + */ + public abstract boolean bind(View view, Cursor cursor, int columnIndex); + } + + /** + * <p>Interface used to transform data coming out of a {@link android.database.Cursor} + * before it is bound to a {@link android.view.View}.</p> + * + * <p>Transformations are used to transform text-based data (in the form of a String), + * or to transform data into a resource identifier. A default implementation is provided + * to generate resource identifiers.</p> + * + * @see android.database.Cursor + * @see android.widget.Adapters.CursorBinder + */ + public static abstract class CursorTransformation { + /** + * <p>The context associated with this transformation.</p> + */ + protected final Context mContext; + + /** + * <p>Creates a new Cursor transformation.</p> + * + * @param context The context associated with this transformation. + */ + public CursorTransformation(Context context) { + mContext = context; + } + + /** + * <p>Transforms the specified Cursor column into a String. The transformation + * can simply return the content of the column as a String (this is known + * as the identity transformation) or manipulate the content. For instance, + * a transformation can perform text substitutions or concatenate other + * columns with the specified column.</p> + * + * @param cursor The cursor that contains the data to transform. + * @param columnIndex The index of the column to transform. + * + * @return A String containing the transformed value of the column. + */ + public abstract String transform(Cursor cursor, int columnIndex); + + /** + * <p>Transforms the specified Cursor column into a resource identifier. + * The default implementation simply interprets the content of the column + * as an integer.</p> + * + * @param cursor The cursor that contains the data to transform. + * @param columnIndex The index of the column to transform. + * + * @return A resource identifier. + */ + public int transformToResource(Cursor cursor, int columnIndex) { + return cursor.getInt(columnIndex); + } + } + + /** + * <p>Loads the {@link android.widget.CursorAdapter} defined in the specified + * XML resource. The content of the adapter is loaded from the content provider + * identified by the supplied URI.</p> + * + * <p><strong>Note:</strong> If the supplied {@link android.content.Context} is + * an {@link android.app.Activity}, the cursor returned by the content provider + * will be automatically managed. Otherwise, you are responsible for managing the + * cursor yourself.</p> + * + * <p>The format of the XML definition of the cursor adapter is documented at + * the top of this page.</p> + * + * @param context The context to load the XML resource from. + * @param id The identifier of the XML resource declaring the adapter. + * @param uri The URI of the content provider. + * @param parameters Optional parameters to pass to the CursorAdapter, used + * to substitute values in the selection expression. + * + * @return A {@link android.widget.CursorAdapter} + * + * @throws IllegalArgumentException If the XML resource does not contain + * a valid <cursor-adapter /> definition. + * + * @see android.content.ContentProvider + * @see android.widget.CursorAdapter + * @see #loadAdapter(android.content.Context, int, Object[]) + */ + public static CursorAdapter loadCursorAdapter(Context context, int id, String uri, + Object... parameters) { + + XmlCursorAdapter adapter = (XmlCursorAdapter) loadAdapter(context, id, ADAPTER_CURSOR, + parameters); + + if (uri != null) { + adapter.setUri(uri); + } + adapter.load(); + + return adapter; + } + + /** + * <p>Loads the {@link android.widget.CursorAdapter} defined in the specified + * XML resource. The content of the adapter is loaded from the specified cursor. + * You are responsible for managing the supplied cursor.</p> + * + * <p>The format of the XML definition of the cursor adapter is documented at + * the top of this page.</p> + * + * @param context The context to load the XML resource from. + * @param id The identifier of the XML resource declaring the adapter. + * @param cursor The cursor containing the data for the adapter. + * @param parameters Optional parameters to pass to the CursorAdapter, used + * to substitute values in the selection expression. + * + * @return A {@link android.widget.CursorAdapter} + * + * @throws IllegalArgumentException If the XML resource does not contain + * a valid <cursor-adapter /> definition. + * + * @see android.content.ContentProvider + * @see android.widget.CursorAdapter + * @see android.database.Cursor + * @see #loadAdapter(android.content.Context, int, Object[]) + */ + public static CursorAdapter loadCursorAdapter(Context context, int id, Cursor cursor, + Object... parameters) { + + XmlCursorAdapter adapter = (XmlCursorAdapter) loadAdapter(context, id, ADAPTER_CURSOR, + parameters); + + if (cursor != null) { + adapter.changeCursor(cursor); + } + + return adapter; + } + + /** + * <p>Loads the adapter defined in the specified XML resource. The XML definition of + * the adapter must follow the format definition of one of the supported adapter + * types described at the top of this page.</p> + * + * <p><strong>Note:</strong> If the loaded adapter is a {@link android.widget.CursorAdapter} + * and the supplied {@link android.content.Context} is an {@link android.app.Activity}, + * the cursor returned by the content provider will be automatically managed. Otherwise, + * you are responsible for managing the cursor yourself.</p> + * + * @param context The context to load the XML resource from. + * @param id The identifier of the XML resource declaring the adapter. + * @param parameters Optional parameters to pass to the adapter. + * + * @return An adapter instance. + * + * @see #loadCursorAdapter(android.content.Context, int, android.database.Cursor, Object[]) + * @see #loadCursorAdapter(android.content.Context, int, String, Object[]) + */ + public static BaseAdapter loadAdapter(Context context, int id, Object... parameters) { + final BaseAdapter adapter = loadAdapter(context, id, null, parameters); + if (adapter instanceof ManagedAdapter) { + ((ManagedAdapter) adapter).load(); + } + return adapter; + } + + /** + * Loads an adapter from the specified XML resource. The optional assertName can + * be used to exit early if the adapter defined in the XML resource is not of the + * expected type. + * + * @param context The context to associate with the adapter. + * @param id The resource id of the XML document defining the adapter. + * @param assertName The mandatory name of the adapter in the XML document. + * Ignored if null. + * @param parameters Optional parameters passed to the adapter. + * + * @return An instance of {@link android.widget.BaseAdapter}. + */ + private static BaseAdapter loadAdapter(Context context, int id, String assertName, + Object... parameters) { + + XmlResourceParser parser = null; + try { + parser = context.getResources().getXml(id); + return createAdapterFromXml(context, parser, Xml.asAttributeSet(parser), + id, parameters, assertName); + } catch (XmlPullParserException ex) { + Resources.NotFoundException rnf = new Resources.NotFoundException( + "Can't load adapter resource ID " + + context.getResources().getResourceEntryName(id)); + rnf.initCause(ex); + throw rnf; + } catch (IOException ex) { + Resources.NotFoundException rnf = new Resources.NotFoundException( + "Can't load adapter resource ID " + + context.getResources().getResourceEntryName(id)); + rnf.initCause(ex); + throw rnf; + } finally { + if (parser != null) parser.close(); + } + } + + /** + * Generates an adapter using the specified XML parser. This method is responsible + * for choosing the type of the adapter to create based on the content of the + * XML parser. + * + * This method will generate an {@link IllegalArgumentException} if + * <code>assertName</code> is not null and does not match the root tag of the XML + * document. + */ + private static BaseAdapter createAdapterFromXml(Context c, + XmlPullParser parser, AttributeSet attrs, int id, Object[] parameters, + String assertName) throws XmlPullParserException, IOException { + + BaseAdapter adapter = null; + + // Make sure we are on a start tag. + int type; + int depth = parser.getDepth(); + + while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) && + type != XmlPullParser.END_DOCUMENT) { + + if (type != XmlPullParser.START_TAG) { + continue; + } + + String name = parser.getName(); + if (assertName != null && !assertName.equals(name)) { + throw new IllegalArgumentException("The adapter defined in " + + c.getResources().getResourceEntryName(id) + " must be a <" + name + " />"); + } + + if (ADAPTER_CURSOR.equals(name)) { + adapter = createCursorAdapter(c, parser, attrs, id, parameters); + } else { + throw new IllegalArgumentException("Unknown adapter name " + parser.getName() + + " in " + c.getResources().getResourceEntryName(id)); + } + } + + return adapter; + + } + + /** + * Creates an XmlCursorAdapter using an XmlCursorAdapterParser. + */ + private static XmlCursorAdapter createCursorAdapter(Context c, XmlPullParser parser, + AttributeSet attrs, int id, Object[] parameters) + throws IOException, XmlPullParserException { + + return new XmlCursorAdapterParser(c, parser, attrs, id).parse(parameters); + } + + /** + * Parser that can generate XmlCursorAdapter instances. This parser is responsible for + * handling all the attributes and child nodes for a <cursor-adapter />. + */ + private static class XmlCursorAdapterParser { + private static final String ADAPTER_CURSOR_BIND = "bind"; + private static final String ADAPTER_CURSOR_SELECT = "select"; + private static final String ADAPTER_CURSOR_AS_STRING = "string"; + private static final String ADAPTER_CURSOR_AS_IMAGE = "image"; + private static final String ADAPTER_CURSOR_AS_IMAGE_URI = "image-uri"; + private static final String ADAPTER_CURSOR_AS_DRAWABLE = "drawable"; + private static final String ADAPTER_CURSOR_MAP = "map"; + private static final String ADAPTER_CURSOR_TRANSFORM = "transform"; + + private final Context mContext; + private final XmlPullParser mParser; + private final AttributeSet mAttrs; + private final int mId; + + private final HashMap<String, CursorBinder> mBinders; + private final ArrayList<String> mFrom; + private final ArrayList<Integer> mTo; + private final CursorTransformation mIdentity; + private final Resources mResources; + + public XmlCursorAdapterParser(Context c, XmlPullParser parser, AttributeSet attrs, int id) { + mContext = c; + mParser = parser; + mAttrs = attrs; + mId = id; + + mResources = mContext.getResources(); + mBinders = new HashMap<String, CursorBinder>(); + mFrom = new ArrayList<String>(); + mTo = new ArrayList<Integer>(); + mIdentity = new IdentityTransformation(mContext); + } + + public XmlCursorAdapter parse(Object[] parameters) + throws IOException, XmlPullParserException { + + Resources resources = mResources; + TypedArray a = resources.obtainAttributes(mAttrs, styleable.CursorAdapter); + + String uri = a.getString(styleable.CursorAdapter_uri); + String selection = a.getString(styleable.CursorAdapter_selection); + String sortOrder = a.getString(styleable.CursorAdapter_sortOrder); + int layout = a.getResourceId(styleable.CursorAdapter_layout, 0); + if (layout == 0) { + throw new IllegalArgumentException("The layout specified in " + + resources.getResourceEntryName(mId) + " does not exist"); + } + + a.recycle(); + + XmlPullParser parser = mParser; + int type; + int depth = parser.getDepth(); + + while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) && + type != XmlPullParser.END_DOCUMENT) { + + if (type != XmlPullParser.START_TAG) { + continue; + } + + String name = parser.getName(); + + if (ADAPTER_CURSOR_BIND.equals(name)) { + parseBindTag(); + } else if (ADAPTER_CURSOR_SELECT.equals(name)) { + parseSelectTag(); + } else { + throw new RuntimeException("Unknown tag name " + parser.getName() + " in " + + resources.getResourceEntryName(mId)); + } + } + + String[] fromArray = mFrom.toArray(new String[mFrom.size()]); + int[] toArray = new int[mTo.size()]; + for (int i = 0; i < toArray.length; i++) { + toArray[i] = mTo.get(i); + } + + String[] selectionArgs = null; + if (parameters != null) { + selectionArgs = new String[parameters.length]; + for (int i = 0; i < selectionArgs.length; i++) { + selectionArgs[i] = (String) parameters[i]; + } + } + + return new XmlCursorAdapter(mContext, layout, uri, fromArray, toArray, selection, + selectionArgs, sortOrder, mBinders); + } + + private void parseSelectTag() { + TypedArray a = mResources.obtainAttributes(mAttrs, styleable.CursorAdapter_SelectItem); + + String fromName = a.getString(styleable.CursorAdapter_SelectItem_column); + if (fromName == null) { + throw new IllegalArgumentException("A select item in " + + mResources.getResourceEntryName(mId) + " does not have a 'column' attribute"); + } + + a.recycle(); + + mFrom.add(fromName); + mTo.add(View.NO_ID); + } + + private void parseBindTag() throws IOException, XmlPullParserException { + Resources resources = mResources; + TypedArray a = resources.obtainAttributes(mAttrs, styleable.CursorAdapter_BindItem); + + String fromName = a.getString(styleable.CursorAdapter_BindItem_from); + if (fromName == null) { + throw new IllegalArgumentException("A bind item in " + + resources.getResourceEntryName(mId) + " does not have a 'from' attribute"); + } + + int toName = a.getResourceId(styleable.CursorAdapter_BindItem_to, 0); + if (toName == 0) { + throw new IllegalArgumentException("A bind item in " + + resources.getResourceEntryName(mId) + " does not have a 'to' attribute"); + } + + String asType = a.getString(styleable.CursorAdapter_BindItem_as); + if (asType == null) { + throw new IllegalArgumentException("A bind item in " + + resources.getResourceEntryName(mId) + " does not have an 'as' attribute"); + } + + mFrom.add(fromName); + mTo.add(toName); + mBinders.put(fromName, findBinder(asType)); + + a.recycle(); + } + + private CursorBinder findBinder(String type) throws IOException, XmlPullParserException { + final XmlPullParser parser = mParser; + final Context context = mContext; + CursorTransformation transformation = mIdentity; + + int tagType; + int depth = parser.getDepth(); + + final boolean isDrawable = ADAPTER_CURSOR_AS_DRAWABLE.equals(type); + + while (((tagType = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) + && tagType != XmlPullParser.END_DOCUMENT) { + + if (tagType != XmlPullParser.START_TAG) { + continue; + } + + String name = parser.getName(); + + if (ADAPTER_CURSOR_TRANSFORM.equals(name)) { + transformation = findTransformation(); + } else if (ADAPTER_CURSOR_MAP.equals(name)) { + if (!(transformation instanceof MapTransformation)) { + transformation = new MapTransformation(context); + } + findMap(((MapTransformation) transformation), isDrawable); + } else { + throw new RuntimeException("Unknown tag name " + parser.getName() + " in " + + context.getResources().getResourceEntryName(mId)); + } + } + + if (ADAPTER_CURSOR_AS_STRING.equals(type)) { + return new StringBinder(context, transformation); + } else if (ADAPTER_CURSOR_AS_IMAGE.equals(type)) { + return new ImageBinder(context, transformation); + } else if (ADAPTER_CURSOR_AS_IMAGE_URI.equals(type)) { + return new ImageUriBinder(context, transformation); + } else if (isDrawable) { + return new DrawableBinder(context, transformation); + } else { + return createBinder(type, transformation); + } + } + + private CursorBinder createBinder(String type, CursorTransformation transformation) { + if (mContext.isRestricted()) return null; + + try { + final Class<?> klass = Class.forName(type, true, mContext.getClassLoader()); + if (CursorBinder.class.isAssignableFrom(klass)) { + final Constructor<?> c = klass.getDeclaredConstructor( + Context.class, CursorTransformation.class); + return (CursorBinder) c.newInstance(mContext, transformation); + } + } catch (ClassNotFoundException e) { + throw new IllegalArgumentException("Cannot instanciate binder type in " + + mContext.getResources().getResourceEntryName(mId) + ": " + type, e); + } catch (NoSuchMethodException e) { + throw new IllegalArgumentException("Cannot instanciate binder type in " + + mContext.getResources().getResourceEntryName(mId) + ": " + type, e); + } catch (InvocationTargetException e) { + throw new IllegalArgumentException("Cannot instanciate binder type in " + + mContext.getResources().getResourceEntryName(mId) + ": " + type, e); + } catch (InstantiationException e) { + throw new IllegalArgumentException("Cannot instanciate binder type in " + + mContext.getResources().getResourceEntryName(mId) + ": " + type, e); + } catch (IllegalAccessException e) { + throw new IllegalArgumentException("Cannot instanciate binder type in " + + mContext.getResources().getResourceEntryName(mId) + ": " + type, e); + } + + return null; + } + + private void findMap(MapTransformation transformation, boolean drawable) { + Resources resources = mResources; + + TypedArray a = resources.obtainAttributes(mAttrs, styleable.CursorAdapter_MapItem); + + String from = a.getString(styleable.CursorAdapter_MapItem_fromValue); + if (from == null) { + throw new IllegalArgumentException("A map item in " + + resources.getResourceEntryName(mId) + " does not have a 'fromValue' attribute"); + } + + if (!drawable) { + String to = a.getString(styleable.CursorAdapter_MapItem_toValue); + if (to == null) { + throw new IllegalArgumentException("A map item in " + + resources.getResourceEntryName(mId) + " does not have a 'toValue' attribute"); + } + transformation.addStringMapping(from, to); + } else { + int to = a.getResourceId(styleable.CursorAdapter_MapItem_toValue, 0); + if (to == 0) { + throw new IllegalArgumentException("A map item in " + + resources.getResourceEntryName(mId) + " does not have a 'toValue' attribute"); + } + transformation.addResourceMapping(from, to); + } + + a.recycle(); + } + + private CursorTransformation findTransformation() { + Resources resources = mResources; + CursorTransformation transformation = null; + TypedArray a = resources.obtainAttributes(mAttrs, styleable.CursorAdapter_TransformItem); + + String className = a.getString(styleable.CursorAdapter_TransformItem_withClass); + if (className == null) { + String expression = a.getString( + styleable.CursorAdapter_TransformItem_withExpression); + transformation = createExpressionTransformation(expression); + } else if (!mContext.isRestricted()) { + try { + final Class<?> klass = Class.forName(className, true, mContext.getClassLoader()); + if (CursorTransformation.class.isAssignableFrom(klass)) { + final Constructor<?> c = klass.getDeclaredConstructor(Context.class); + transformation = (CursorTransformation) c.newInstance(mContext); + } + } catch (ClassNotFoundException e) { + throw new IllegalArgumentException("Cannot instanciate transform type in " + + mContext.getResources().getResourceEntryName(mId) + ": " + className, e); + } catch (NoSuchMethodException e) { + throw new IllegalArgumentException("Cannot instanciate transform type in " + + mContext.getResources().getResourceEntryName(mId) + ": " + className, e); + } catch (InvocationTargetException e) { + throw new IllegalArgumentException("Cannot instanciate transform type in " + + mContext.getResources().getResourceEntryName(mId) + ": " + className, e); + } catch (InstantiationException e) { + throw new IllegalArgumentException("Cannot instanciate transform type in " + + mContext.getResources().getResourceEntryName(mId) + ": " + className, e); + } catch (IllegalAccessException e) { + throw new IllegalArgumentException("Cannot instanciate transform type in " + + mContext.getResources().getResourceEntryName(mId) + ": " + className, e); + } + } + + a.recycle(); + + if (transformation == null) { + throw new IllegalArgumentException("A transform item in " + + resources.getResourceEntryName(mId) + " must have a 'withClass' or " + + "'withExpression' attribute"); + } + + return transformation; + } + + private CursorTransformation createExpressionTransformation(String expression) { + return new ExpressionTransformation(mContext, expression); + } + } + + /** + * Interface used by adapters that require to be loaded after creation. + */ + private static interface ManagedAdapter { + /** + * Loads the content of the adapter, asynchronously. + */ + void load(); + } + + /** + * Implementation of a Cursor adapter defined in XML. This class is a thin wrapper + * of a SimpleCursorAdapter. The main difference is the ability to handle CursorBinders. + */ + private static class XmlCursorAdapter extends SimpleCursorAdapter implements ManagedAdapter { + private final Context mContext; + private String mUri; + private final String mSelection; + private final String[] mSelectionArgs; + private final String mSortOrder; + private final String[] mColumns; + private final CursorBinder[] mBinders; + private AsyncTask<Void,Void,Cursor> mLoadTask; + + XmlCursorAdapter(Context context, int layout, String uri, String[] from, int[] to, + String selection, String[] selectionArgs, String sortOrder, + HashMap<String, CursorBinder> binders) { + + super(context, layout, null, from, to); + mContext = context; + mUri = uri; + mSelection = selection; + mSelectionArgs = selectionArgs; + mSortOrder = sortOrder; + mColumns = new String[from.length + 1]; + // This is mandatory in CursorAdapter + mColumns[0] = "_id"; + System.arraycopy(from, 0, mColumns, 1, from.length); + + CursorBinder basic = new StringBinder(context, new IdentityTransformation(context)); + final int count = from.length; + mBinders = new CursorBinder[count]; + + for (int i = 0; i < count; i++) { + CursorBinder binder = binders.get(from[i]); + if (binder == null) binder = basic; + mBinders[i] = binder; + } + } + + @Override + public void bindView(View view, Context context, Cursor cursor) { + final int count = mTo.length; + final int[] from = mFrom; + final int[] to = mTo; + final CursorBinder[] binders = mBinders; + + for (int i = 0; i < count; i++) { + final View v = view.findViewById(to[i]); + if (v != null) { + binders[i].bind(v, cursor, from[i]); + } + } + } + + public void load() { + if (mUri != null) { + mLoadTask = new QueryTask().execute(); + } + } + + void setUri(String uri) { + mUri = uri; + } + + @Override + public void changeCursor(Cursor c) { + if (mLoadTask != null && mLoadTask.getStatus() != QueryTask.Status.FINISHED) { + mLoadTask.cancel(true); + mLoadTask = null; + } + super.changeCursor(c); + } + + class QueryTask extends AsyncTask<Void, Void, Cursor> { + @Override + protected Cursor doInBackground(Void... params) { + if (mContext instanceof Activity) { + return ((Activity) mContext).managedQuery( + Uri.parse(mUri), mColumns, mSelection, mSelectionArgs, mSortOrder); + } else { + return mContext.getContentResolver().query( + Uri.parse(mUri), mColumns, mSelection, mSelectionArgs, mSortOrder); + } + } + + @Override + protected void onPostExecute(Cursor cursor) { + if (!isCancelled()) { + XmlCursorAdapter.super.changeCursor(cursor); + } + } + } + } + + /** + * Identity transformation, returns the content of the specified column as a String, + * without performing any manipulation. This is used when no transformation is specified. + */ + private static class IdentityTransformation extends CursorTransformation { + public IdentityTransformation(Context context) { + super(context); + } + + @Override + public String transform(Cursor cursor, int columnIndex) { + return cursor.getString(columnIndex); + } + } + + /** + * An expression transformation is a simple template based replacement utility. + * In an expression, each segment of the form <code>{(^[}]+)}</code> is replaced + * with the value of the column of name $1. + */ + private static class ExpressionTransformation extends CursorTransformation { + private final ExpressionNode mFirstNode = new ConstantExpressionNode(""); + private final StringBuilder mBuilder = new StringBuilder(); + + public ExpressionTransformation(Context context, String expression) { + super(context); + + parse(expression); + } + + private void parse(String expression) { + ExpressionNode node = mFirstNode; + int segmentStart; + int count = expression.length(); + + for (int i = 0; i < count; i++) { + char c = expression.charAt(i); + // Start a column name segment + segmentStart = i; + if (c == '{') { + while (i < count && (c = expression.charAt(i)) != '}') { + i++; + } + // We've reached the end, but the expression didn't close + if (c != '}') { + throw new IllegalStateException("The transform expression contains a " + + "non-closed column name: " + + expression.substring(segmentStart + 1, i)); + } + node.next = new ColumnExpressionNode(expression.substring(segmentStart + 1, i)); + } else { + while (i < count && (c = expression.charAt(i)) != '{') { + i++; + } + node.next = new ConstantExpressionNode(expression.substring(segmentStart, i)); + // Rewind if we've reached a column expression + if (c == '{') i--; + } + node = node.next; + } + } + + @Override + public String transform(Cursor cursor, int columnIndex) { + final StringBuilder builder = mBuilder; + builder.delete(0, builder.length()); + + ExpressionNode node = mFirstNode; + // Skip the first node + while ((node = node.next) != null) { + builder.append(node.asString(cursor)); + } + + return builder.toString(); + } + + static abstract class ExpressionNode { + public ExpressionNode next; + + public abstract String asString(Cursor cursor); + } + + static class ConstantExpressionNode extends ExpressionNode { + private final String mConstant; + + ConstantExpressionNode(String constant) { + mConstant = constant; + } + + @Override + public String asString(Cursor cursor) { + return mConstant; + } + } + + static class ColumnExpressionNode extends ExpressionNode { + private final String mColumnName; + private Cursor mSignature; + private int mColumnIndex = -1; + + ColumnExpressionNode(String columnName) { + mColumnName = columnName; + } + + @Override + public String asString(Cursor cursor) { + if (cursor != mSignature || mColumnIndex == -1) { + mColumnIndex = cursor.getColumnIndex(mColumnName); + mSignature = cursor; + } + + return cursor.getString(mColumnIndex); + } + } + } + + /** + * A map transformation offers a simple mapping between specified String values + * to Strings or integers. + */ + private static class MapTransformation extends CursorTransformation { + private final HashMap<String, String> mStringMappings; + private final HashMap<String, Integer> mResourceMappings; + + public MapTransformation(Context context) { + super(context); + mStringMappings = new HashMap<String, String>(); + mResourceMappings = new HashMap<String, Integer>(); + } + + void addStringMapping(String from, String to) { + mStringMappings.put(from, to); + } + + void addResourceMapping(String from, int to) { + mResourceMappings.put(from, to); + } + + @Override + public String transform(Cursor cursor, int columnIndex) { + final String value = cursor.getString(columnIndex); + final String transformed = mStringMappings.get(value); + return transformed == null ? value : transformed; + } + + @Override + public int transformToResource(Cursor cursor, int columnIndex) { + final String value = cursor.getString(columnIndex); + final Integer transformed = mResourceMappings.get(value); + try { + return transformed == null ? Integer.parseInt(value) : transformed; + } catch (NumberFormatException e) { + return 0; + } + } + } + + /** + * Binds a String to a TextView. + */ + private static class StringBinder extends CursorBinder { + public StringBinder(Context context, CursorTransformation transformation) { + super(context, transformation); + } + + @Override + public boolean bind(View view, Cursor cursor, int columnIndex) { + ((TextView) view).setText(mTransformation.transform(cursor, columnIndex)); + return true; + } + } + + /** + * Binds an image blob to an ImageView. + */ + private static class ImageBinder extends CursorBinder { + public ImageBinder(Context context, CursorTransformation transformation) { + super(context, transformation); + } + + @Override + public boolean bind(View view, Cursor cursor, int columnIndex) { + final byte[] data = cursor.getBlob(columnIndex); + ((ImageView) view).setImageBitmap(BitmapFactory.decodeByteArray(data, 0, data.length)); + return true; + } + } + + /** + * Binds an image URI to an ImageView. + */ + private static class ImageUriBinder extends CursorBinder { + public ImageUriBinder(Context context, CursorTransformation transformation) { + super(context, transformation); + } + + @Override + public boolean bind(View view, Cursor cursor, int columnIndex) { + ((ImageView) view).setImageURI(Uri.parse( + mTransformation.transform(cursor, columnIndex))); + return true; + } + } + + /** + * Binds a drawable resource identifier to an ImageView. + */ + private static class DrawableBinder extends CursorBinder { + public DrawableBinder(Context context, CursorTransformation transformation) { + super(context, transformation); + } + + @Override + public boolean bind(View view, Cursor cursor, int columnIndex) { + final int resource = mTransformation.transformToResource(cursor, columnIndex); + if (resource == 0) return false; + + ((ImageView) view).setImageResource(resource); + return true; + } + } +} diff --git a/core/java/android/widget/CursorAdapter.java b/core/java/android/widget/CursorAdapter.java index baa6833..6a891fc 100644 --- a/core/java/android/widget/CursorAdapter.java +++ b/core/java/android/widget/CursorAdapter.java @@ -80,6 +80,18 @@ public abstract class CursorAdapter extends BaseAdapter implements Filterable, protected FilterQueryProvider mFilterQueryProvider; /** + * If set the adapter will call requery() on the cursor whenever a content change + * notification is delivered. Implies {@link #FLAG_REGISTER_CONTENT_OBSERVER} + */ + public static final int FLAG_AUTO_REQUERY = 0x01; + + /** + * If set the adapter will register a content observer on the cursor and will call + * {@link #onContentChanged()} when a notification comes in. + */ + public static final int FLAG_REGISTER_CONTENT_OBSERVER = 0x02; + + /** * Constructor. The adapter will call requery() on the cursor whenever * it changes so that the most recent data is always displayed. * @@ -87,7 +99,7 @@ public abstract class CursorAdapter extends BaseAdapter implements Filterable, * @param context The context */ public CursorAdapter(Context context, Cursor c) { - init(context, c, true); + init(context, c, FLAG_AUTO_REQUERY); } /** @@ -99,19 +111,43 @@ public abstract class CursorAdapter extends BaseAdapter implements Filterable, * data is always displayed. */ public CursorAdapter(Context context, Cursor c, boolean autoRequery) { - init(context, c, autoRequery); + init(context, c, autoRequery ? FLAG_AUTO_REQUERY : 0); + } + + /** + * Constructor + * @param c The cursor from which to get the data. + * @param context The context + * @param flags flags used to determine the behavior of the adapter + */ + public CursorAdapter(Context context, Cursor c, int flags) { + init(context, c, flags); } protected void init(Context context, Cursor c, boolean autoRequery) { + init(context, c, autoRequery ? FLAG_AUTO_REQUERY : 0); + } + + protected void init(Context context, Cursor c, int flags) { + if ((flags & FLAG_AUTO_REQUERY) == FLAG_AUTO_REQUERY) { + flags |= FLAG_REGISTER_CONTENT_OBSERVER; + mAutoRequery = true; + } else { + mAutoRequery = false; + } boolean cursorPresent = c != null; - mAutoRequery = autoRequery; mCursor = c; mDataValid = cursorPresent; mContext = context; mRowIDColumn = cursorPresent ? c.getColumnIndexOrThrow("_id") : -1; - mChangeObserver = new ChangeObserver(); + if ((flags & FLAG_REGISTER_CONTENT_OBSERVER) == FLAG_REGISTER_CONTENT_OBSERVER) { + mChangeObserver = new ChangeObserver(); + } else { + mChangeObserver = null; + } + if (cursorPresent) { - c.registerContentObserver(mChangeObserver); + if (mChangeObserver != null) c.registerContentObserver(mChangeObserver); c.registerDataSetObserver(mDataSetObserver); } } @@ -246,13 +282,13 @@ public abstract class CursorAdapter extends BaseAdapter implements Filterable, return; } if (mCursor != null) { - mCursor.unregisterContentObserver(mChangeObserver); + if (mChangeObserver != null) mCursor.unregisterContentObserver(mChangeObserver); mCursor.unregisterDataSetObserver(mDataSetObserver); mCursor.close(); } mCursor = cursor; if (cursor != null) { - cursor.registerContentObserver(mChangeObserver); + if (mChangeObserver != null) cursor.registerContentObserver(mChangeObserver); cursor.registerDataSetObserver(mDataSetObserver); mRowIDColumn = cursor.getColumnIndexOrThrow("_id"); mDataValid = true; diff --git a/core/java/android/widget/SimpleCursorAdapter.java b/core/java/android/widget/SimpleCursorAdapter.java index 7d3459e..d1c2270 100644 --- a/core/java/android/widget/SimpleCursorAdapter.java +++ b/core/java/android/widget/SimpleCursorAdapter.java @@ -62,7 +62,8 @@ public class SimpleCursorAdapter extends ResourceCursorAdapter { private int mStringConversionColumn = -1; private CursorToStringConverter mCursorToStringConverter; private ViewBinder mViewBinder; - private String[] mOriginalFrom; + + String[] mOriginalFrom; /** * Constructor. diff --git a/core/java/com/android/internal/widget/DigitalClock.java b/core/java/com/android/internal/widget/DigitalClock.java index fa47ff6..23e2277 100644 --- a/core/java/com/android/internal/widget/DigitalClock.java +++ b/core/java/com/android/internal/widget/DigitalClock.java @@ -30,7 +30,7 @@ import android.provider.Settings; import android.text.format.DateFormat; import android.util.AttributeSet; import android.view.View; -import android.widget.LinearLayout; +import android.widget.RelativeLayout; import android.widget.TextView; import java.text.DateFormatSymbols; @@ -39,7 +39,7 @@ import java.util.Calendar; /** * Displays the time */ -public class DigitalClock extends LinearLayout { +public class DigitalClock extends RelativeLayout { private final static String M12 = "h:mm"; private final static String M24 = "kk:mm"; diff --git a/core/res/res/drawable/ic_btn_back.png b/core/res/res/drawable/ic_btn_back.png Binary files differnew file mode 100644 index 0000000..c9bff4c --- /dev/null +++ b/core/res/res/drawable/ic_btn_back.png diff --git a/core/res/res/drawable/ic_btn_next.png b/core/res/res/drawable/ic_btn_next.png Binary files differnew file mode 100755 index 0000000..c6cf436 --- /dev/null +++ b/core/res/res/drawable/ic_btn_next.png diff --git a/core/res/res/layout/keyguard_screen_tab_unlock.xml b/core/res/res/layout/keyguard_screen_tab_unlock.xml index 04bc693..945d283 100644 --- a/core/res/res/layout/keyguard_screen_tab_unlock.xml +++ b/core/res/res/layout/keyguard_screen_tab_unlock.xml @@ -41,20 +41,21 @@ android:ellipsize="marquee" android:gravity="right|bottom" android:textAppearance="?android:attr/textAppearanceMedium" + android:textSize="22sp" /> - <!-- emergency call button shown when sim is missing or PUKd --> - <Button - android:id="@+id/emergencyCallButton" + <!-- "emergency calls only" shown when sim is missing or PUKd --> + <TextView + android:id="@+id/emergencyCallText" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_alignParentTop="true" + android:layout_below="@id/carrier" android:layout_alignParentRight="true" - android:layout_marginTop="10dip" + android:layout_marginTop="0dip" android:layout_marginRight="8dip" - android:drawableLeft="@drawable/ic_emergency" - style="@style/Widget.Button.Transparent" - android:drawablePadding="8dip" + android:text="@string/emergency_calls_only" + android:textAppearance="?android:attr/textAppearanceSmall" + android:textColor="@color/white" /> <!-- time and date --> @@ -64,6 +65,7 @@ android:layout_below="@id/carrier" android:layout_marginTop="52dip" android:layout_marginLeft="20dip" + android:layout_marginBottom="8dip" > <TextView android:id="@+id/timeDisplay" @@ -71,7 +73,6 @@ android:layout_height="wrap_content" android:singleLine="true" android:ellipsize="none" - android:gravity="bottom" android:textSize="72sp" android:textAppearance="?android:attr/textAppearanceMedium" android:shadowColor="#C0000000" @@ -84,8 +85,9 @@ <TextView android:id="@+id/am_pm" android:layout_width="wrap_content" - android:layout_height="match_parent" - android:gravity="bottom" + android:layout_height="wrap_content" + android:layout_toRightOf="@id/timeDisplay" + android:layout_alignBaseline="@id/timeDisplay" android:singleLine="true" android:ellipsize="none" android:textSize="22sp" diff --git a/core/res/res/layout/keyguard_screen_tab_unlock_land.xml b/core/res/res/layout/keyguard_screen_tab_unlock_land.xml index 4774dfc..6b76004 100644 --- a/core/res/res/layout/keyguard_screen_tab_unlock_land.xml +++ b/core/res/res/layout/keyguard_screen_tab_unlock_land.xml @@ -46,18 +46,19 @@ android:ellipsize="marquee" android:gravity="right|bottom" android:textAppearance="?android:attr/textAppearanceMedium" + android:textSize="22sp" /> - <!-- emergency call button shown when sim is missing or PUKd --> - <Button - android:id="@+id/emergencyCallButton" + <!-- "emergency calls only" shown when sim is missing or PUKd --> + <TextView + android:id="@+id/emergencyCallText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginTop="20dip" - android:drawableLeft="@drawable/ic_emergency" - style="@style/Widget.Button.Transparent" - android:drawablePadding="8dip" + android:text="@string/emergency_calls_only" + android:textAppearance="?android:attr/textAppearanceSmall" + android:textColor="@color/white" /> <com.android.internal.widget.DigitalClock android:id="@+id/time" @@ -65,12 +66,12 @@ android:layout_height="wrap_content" android:layout_below="@id/carrier" android:layout_marginTop="56dip" + android:layout_marginBottom="8dip" > <TextView android:id="@+id/timeDisplay" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:gravity="bottom" android:singleLine="true" android:ellipsize="none" android:textSize="72sp" @@ -85,8 +86,9 @@ <TextView android:id="@+id/am_pm" android:layout_width="wrap_content" - android:layout_height="match_parent" - android:gravity="bottom" + android:layout_height="wrap_content" + android:layout_toRightOf="@id/timeDisplay" + android:layout_alignBaseline="@id/timeDisplay" android:singleLine="true" android:ellipsize="none" android:textSize="22sp" diff --git a/core/res/res/layout/keyguard_screen_unlock_landscape.xml b/core/res/res/layout/keyguard_screen_unlock_landscape.xml index c1b406f..83381a1 100644 --- a/core/res/res/layout/keyguard_screen_unlock_landscape.xml +++ b/core/res/res/layout/keyguard_screen_unlock_landscape.xml @@ -58,18 +58,19 @@ android:ellipsize="marquee" android:gravity="right|bottom" /> + <com.android.internal.widget.DigitalClock android:id="@+id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_marginTop="8dip" + android:layout_marginBottom="8dip" > <TextView android:id="@+id/timeDisplay" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:gravity="bottom" android:singleLine="true" android:ellipsize="none" android:textSize="72sp" @@ -84,8 +85,9 @@ <TextView android:id="@+id/am_pm" android:layout_width="wrap_content" - android:layout_height="match_parent" - android:gravity="bottom" + android:layout_height="wrap_content" + android:layout_toRightOf="@id/timeDisplay" + android:layout_alignBaseline="@id/timeDisplay" android:singleLine="true" android:ellipsize="none" android:textSize="22sp" diff --git a/core/res/res/layout/keyguard_screen_unlock_portrait.xml b/core/res/res/layout/keyguard_screen_unlock_portrait.xml index 74a0eee..8dacfaf 100644 --- a/core/res/res/layout/keyguard_screen_unlock_portrait.xml +++ b/core/res/res/layout/keyguard_screen_unlock_portrait.xml @@ -55,12 +55,12 @@ android:layout_alignParentTop="true" android:layout_marginTop="15dip" android:layout_marginLeft="20dip" + android:layout_marginBottom="8dip" > <TextView android:id="@+id/timeDisplay" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:gravity="bottom" android:singleLine="true" android:ellipsize="none" android:textSize="56sp" @@ -74,8 +74,9 @@ <TextView android:id="@+id/am_pm" android:layout_width="wrap_content" - android:layout_height="match_parent" - android:gravity="bottom" + android:layout_height="wrap_content" + android:layout_toRightOf="@id/timeDisplay" + android:layout_alignBaseline="@id/timeDisplay" android:singleLine="true" android:ellipsize="none" android:textSize="18sp" diff --git a/core/res/res/layout/preference_list_content.xml b/core/res/res/layout/preference_list_content.xml index 8f86981..844d338 100644 --- a/core/res/res/layout/preference_list_content.xml +++ b/core/res/res/layout/preference_list_content.xml @@ -4,22 +4,58 @@ ** ** Copyright 2006, 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 +** 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 +** 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 +** 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. */ --> -<ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/list" - android:layout_width="match_parent" + +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:orientation="vertical" android:layout_height="match_parent" - android:drawSelectorOnTop="false" - android:scrollbarAlwaysDrawVerticalTrack="true" + android:layout_width="match_parent"> + + <ListView android:id="@android:id/list" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + android:layout_weight="1" + android:drawSelectorOnTop="false" + android:scrollbarAlwaysDrawVerticalTrack="true" /> + + <RelativeLayout android:id="@+id/button_bar" + android:layout_height="wrap_content" + android:layout_width="fill_parent" + android:layout_weight="0" + android:background="@android:drawable/bottom_bar" + android:visibility="gone"> + + <Button android:id="@+id/back_button" + android:layout_width="150dip" + android:layout_height="wrap_content" + android:layout_margin="5dip" + android:layout_alignParentLeft="true" + android:drawableLeft="@drawable/ic_btn_back" + android:drawablePadding="3dip" + android:text="@string/back_button_label" + /> + + <Button android:id="@+id/next_button" + android:layout_width="150dip" + android:layout_height="wrap_content" + android:layout_margin="5dip" + android:layout_alignParentRight="true" + android:drawableRight="@drawable/ic_btn_next" + android:drawablePadding="3dip" + android:text="@string/next_button_label" + /> + </RelativeLayout> +</LinearLayout> diff --git a/core/res/res/values/attrs.xml b/core/res/res/values/attrs.xml index a3ccaf7..8004328 100644 --- a/core/res/res/values/attrs.xml +++ b/core/res/res/values/attrs.xml @@ -1518,6 +1518,8 @@ will use only the number of items in the adapter and the number of items visible on screen to determine the scrollbar's properties. --> <attr name="smoothScrollbar" format="boolean" /> + <!-- A reference to an XML description of the adapter to attach to the list. --> + <attr name="adapter" format="reference" /> </declare-styleable> <declare-styleable name="AbsSpinner"> <!-- Reference to an array resource that will populate the Spinner. For static content, @@ -3644,5 +3646,68 @@ <declare-styleable name="RecognitionService"> <attr name="settingsActivity" /> </declare-styleable> + + <!-- =============================== --> + <!-- Adapters attributes --> + <!-- =============================== --> + <eat-comment /> + + <!-- Adapter used to bind cursors. --> + <declare-styleable name="CursorAdapter"> + <!-- URI to get the cursor from. Optional. --> + <attr name="uri" format="string" /> + <!-- Selection statement for the query. Optional. --> + <attr name="selection" format="string" /> + <!-- Sort order statement for the query. Optional. --> + <attr name="sortOrder" format="string" /> + <!-- Layout resource used to display each row from the cursor. Mandatory. --> + <attr name="layout" /> + </declare-styleable> + + <!-- Attributes used in bind items for XML cursor adapters. --> + <declare-styleable name="CursorAdapter_BindItem"> + <!-- The name of the column to bind from. Mandatory. --> + <attr name="from" format="string" /> + <!-- The resource id of the view to bind to. Mandatory. --> + <attr name="to" format="reference" /> + <!-- The type of binding. If this value is not specified, the type will be + inferred from the type of the "to" target view. Mandatory. + + The type can be one of: + <ul> + <li>string, The content of the column is interpreted as a string.</li> + <li>image, The content of the column is interpreted as a blob describing an image.</li> + <li>image-uri, The content of the column is interpreted as a URI to an image.</li> + <li>drawable, The content of the column is interpreted as a resource id to a drawable.</li> + <li>A fully qualified class name, corresponding to an implementation of + android.widget.Adapters.CursorBinder.</li> + </ul> + --> + <attr name="as" format="string" /> + </declare-styleable> + + <!-- Attributes used in select items for XML cursor adapters. --> + <declare-styleable name="CursorAdapter_SelectItem"> + <!-- The name of the column to select. Mandatory. --> + <attr name="column" format="string" /> + </declare-styleable> + + <!-- Attributes used to map values to new values in XML cursor adapters' bind items. --> + <declare-styleable name="CursorAdapter_MapItem"> + <!-- The original value from the column. Mandatory. --> + <attr name="fromValue" format="string" /> + <!-- The new value from the column. Mandatory. --> + <attr name="toValue" format="string" /> + </declare-styleable> + + <!-- Attributes used to map values to new values in XML cursor adapters' bind items. --> + <declare-styleable name="CursorAdapter_TransformItem"> + <!-- The transformation expression. Mandatory if "withClass" is not specified. --> + <attr name="withExpression" format="string" /> + <!-- The transformation class, an implementation of + android.widget.Adapters.CursorTransformation. Mandatory if "withExpression" + is not specified. --> + <attr name="withClass" format="string" /> + </declare-styleable> </resources> diff --git a/core/res/res/values/public.xml b/core/res/res/values/public.xml index 98c3a0a..5678b1b 100644 --- a/core/res/res/values/public.xml +++ b/core/res/res/values/public.xml @@ -1238,5 +1238,22 @@ <public type="id" name="custom" id="0x0102002b" /> <public type="anim" name="cycle_interpolator" id="0x010a000c" /> + +<!-- =============================================================== + Resources proposed for Gingerbread. + =============================================================== --> + <eat-comment /> + <public type="attr" name="adapter" id="0x010102be" /> + <public type="attr" name="selection" id="0x010102bf" /> + <public type="attr" name="sortOrder" id="0x010102c0" /> + <public type="attr" name="uri" id="0x010102c1" /> + <public type="attr" name="from" id="0x010102c2" /> + <public type="attr" name="to" id="0x010102c3" /> + <public type="attr" name="as" id="0x010102c4" /> + <public type="attr" name="fromValue" id="0x010102c5" /> + <public type="attr" name="toValue" id="0x010102c6" /> + <public type="attr" name="column" id="0x010102c7" /> + <public type="attr" name="withExpression" id="0x010102c8" /> + <public type="attr" name="withClass" id="0x010102c9" /> </resources> diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml index 45d7aea..ee4d59d 100644 --- a/core/res/res/values/strings.xml +++ b/core/res/res/values/strings.xml @@ -2258,6 +2258,10 @@ <string name="tethered_notification_title">Tethering active</string> <string name="tethered_notification_message">Touch to configure</string> + <!-- Strings for possible PreferenceActivity Back/Next buttons --> + <string name="back_button_label">Back</string> + <string name="next_button_label">Next</string> + <!-- Strings for throttling notification --> <!-- Shown when the user is in danger of being throttled --> <string name="throttle_warning_notification_title">High mobile data use</string> diff --git a/core/tests/coretests/src/android/pim/vcard/ContentValuesVerifier.java b/core/tests/coretests/src/android/pim/vcard/ContentValuesVerifier.java index b9e9875..5262f29 100644 --- a/core/tests/coretests/src/android/pim/vcard/ContentValuesVerifier.java +++ b/core/tests/coretests/src/android/pim/vcard/ContentValuesVerifier.java @@ -56,7 +56,7 @@ import java.util.List; public void verify(InputStream is, int vCardType) throws IOException, VCardException { final VCardParser vCardParser; if (VCardConfig.isV30(vCardType)) { - vCardParser = new VCardParser_V30(true); // use StrictParsing + vCardParser = new VCardParser_V30(); } else { vCardParser = new VCardParser_V21(); } diff --git a/core/tests/coretests/src/android/pim/vcard/ContentValuesVerifierElem.java b/core/tests/coretests/src/android/pim/vcard/ContentValuesVerifierElem.java index 2edbb36..d50c95a 100644 --- a/core/tests/coretests/src/android/pim/vcard/ContentValuesVerifierElem.java +++ b/core/tests/coretests/src/android/pim/vcard/ContentValuesVerifierElem.java @@ -57,7 +57,7 @@ import java.io.InputStream; public void verify(InputStream is, int vCardType) throws IOException, VCardException { final VCardParser vCardParser; if (VCardConfig.isV30(vCardType)) { - vCardParser = new VCardParser_V30(true); // use StrictParsing + vCardParser = new VCardParser_V30(); } else { vCardParser = new VCardParser_V21(); } diff --git a/core/tests/coretests/src/android/pim/vcard/PropertyNodesVerifier.java b/core/tests/coretests/src/android/pim/vcard/PropertyNodesVerifier.java index cfdd074..4cb3c1f 100644 --- a/core/tests/coretests/src/android/pim/vcard/PropertyNodesVerifier.java +++ b/core/tests/coretests/src/android/pim/vcard/PropertyNodesVerifier.java @@ -63,7 +63,7 @@ import java.util.List; public void verify(InputStream is, int vCardType) throws IOException, VCardException { final VCardParser vCardParser; if (VCardConfig.isV30(vCardType)) { - vCardParser = new VCardParser_V30(true); // Use StrictParsing. + vCardParser = new VCardParser_V30(); } else { vCardParser = new VCardParser_V21(); } diff --git a/core/tests/coretests/src/android/pim/vcard/VCardExporterTests.java b/core/tests/coretests/src/android/pim/vcard/VCardExporterTests.java index 004a197..2de0464 100644 --- a/core/tests/coretests/src/android/pim/vcard/VCardExporterTests.java +++ b/core/tests/coretests/src/android/pim/vcard/VCardExporterTests.java @@ -275,6 +275,16 @@ public class VCardExporterTests extends VCardTestsBase { testPhoneBasicCommon(V30); } + public void testPhoneRefrainFormatting() { + mVerifier.initForExportTest(V21 | VCardConfig.FLAG_REFRAIN_PHONE_NUMBER_FORMATTING); + mVerifier.addInputEntry().addContentValues(Phone.CONTENT_ITEM_TYPE) + .put(Phone.NUMBER, "1234567890(abcdefghijklmnopqrstuvwxyz)") + .put(Phone.TYPE, Phone.TYPE_HOME); + mVerifier.addPropertyNodesVerifierElemWithEmptyName() + .addExpectedNode("TEL", "1234567890(abcdefghijklmnopqrstuvwxyz)", + new TypeSet("HOME")); + } + /** * Tests that vCard composer emits corresponding type param which we expect. */ diff --git a/core/tests/coretests/src/android/pim/vcard/VCardJapanizationTests.java b/core/tests/coretests/src/android/pim/vcard/VCardJapanizationTests.java index 5b60342..c9e85dc 100644 --- a/core/tests/coretests/src/android/pim/vcard/VCardJapanizationTests.java +++ b/core/tests/coretests/src/android/pim/vcard/VCardJapanizationTests.java @@ -17,15 +17,13 @@ package android.pim.vcard; import android.content.ContentValues; -import android.pim.vcard.VCardConfig; +import android.pim.vcard.PropertyNodesVerifierElem.TypeSet; import android.provider.ContactsContract.CommonDataKinds.Nickname; import android.provider.ContactsContract.CommonDataKinds.Note; import android.provider.ContactsContract.CommonDataKinds.Phone; import android.provider.ContactsContract.CommonDataKinds.StructuredName; import android.provider.ContactsContract.CommonDataKinds.StructuredPostal; -import android.pim.vcard.PropertyNodesVerifierElem.TypeSet; - import java.util.Arrays; public class VCardJapanizationTests extends VCardTestsBase { @@ -114,7 +112,7 @@ public class VCardJapanizationTests extends VCardTestsBase { .put(StructuredName.PHONETIC_GIVEN_NAME, "\u305F\u308D\u3046"); final ContentValues contentValues = - (VCardConfig.usesShiftJis(vcardType) ? + (VCardConfig.shouldUseShiftJisForExport(vcardType) ? (VCardConfig.isV30(vcardType) ? mContentValuesForSJis : mContentValuesForQPAndSJis) : (VCardConfig.isV30(vcardType) ? null : mContentValuesForQPAndUtf8)); @@ -214,7 +212,7 @@ public class VCardJapanizationTests extends VCardTestsBase { .put(StructuredPostal.TYPE, StructuredPostal.TYPE_CUSTOM) .put(StructuredPostal.LABEL, "\u304A\u3082\u3061\u304B\u3048\u308A"); - ContentValues contentValues = (VCardConfig.usesShiftJis(vcardType) ? + ContentValues contentValues = (VCardConfig.shouldUseShiftJisForExport(vcardType) ? (VCardConfig.isV30(vcardType) ? mContentValuesForSJis : mContentValuesForQPAndSJis) : (VCardConfig.isV30(vcardType) ? mContentValuesForUtf8 : diff --git a/core/tests/coretests/src/android/pim/vcard/VCardVerifier.java b/core/tests/coretests/src/android/pim/vcard/VCardVerifier.java index bfc3158..4377f0c 100644 --- a/core/tests/coretests/src/android/pim/vcard/VCardVerifier.java +++ b/core/tests/coretests/src/android/pim/vcard/VCardVerifier.java @@ -209,12 +209,11 @@ import java.util.Arrays; } } - final VCardParser parser = - (mIsV30 ? new VCardParser_V30(true) : new VCardParser_V21()); + final VCardParser parser = (mIsV30 ? new VCardParser_V30() : new VCardParser_V21()); InputStream is = null; try { String charset = - (VCardConfig.usesShiftJis(mVCardType) ? "SHIFT_JIS" : "UTF-8"); + (VCardConfig.shouldUseShiftJisForExport(mVCardType) ? "SHIFT_JIS" : "UTF-8"); is = new ByteArrayInputStream(vcard.getBytes(charset)); mTestCase.assertEquals(true, parser.parse(is, null, builder)); } catch (IOException e) { @@ -292,8 +291,7 @@ import java.util.Arrays; while (!composer.isAfterLast()) { try { final Method mockGetEntityIteratorMethod = getMockGetEntityIteratorMethod(); - mTestCase.assertTrue( - composer.createOneEntry(getMockGetEntityIteratorMethod())); + mTestCase.assertTrue(composer.createOneEntry(getMockGetEntityIteratorMethod())); } catch (Exception e) { e.printStackTrace(); mTestCase.fail(); diff --git a/core/tests/coretests/src/android/pim/vcard/VNodeBuilder.java b/core/tests/coretests/src/android/pim/vcard/VNodeBuilder.java index 0e6c325..1e607dd 100644 --- a/core/tests/coretests/src/android/pim/vcard/VNodeBuilder.java +++ b/core/tests/coretests/src/android/pim/vcard/VNodeBuilder.java @@ -68,7 +68,7 @@ public class VNodeBuilder implements VCardInterpreter { private boolean mStrictLineBreakParsing; public VNodeBuilder() { - this(VCardConfig.DEFAULT_CHARSET, TARGET_CHARSET, false); + this(VCardConfig.DEFAULT_IMPORT_CHARSET, TARGET_CHARSET, false); } public VNodeBuilder(String charset, boolean strictLineBreakParsing) { @@ -83,7 +83,7 @@ public class VNodeBuilder implements VCardInterpreter { if (sourceCharset != null) { mSourceCharset = sourceCharset; } else { - mSourceCharset = VCardConfig.DEFAULT_CHARSET; + mSourceCharset = VCardConfig.DEFAULT_IMPORT_CHARSET; } if (targetCharset != null) { mTargetCharset = targetCharset; diff --git a/core/tests/coretests/src/android/text/StaticLayoutBidiTest.java b/core/tests/coretests/src/android/text/StaticLayoutBidiTest.java index 8e7e63e..fb0f0c1 100644 --- a/core/tests/coretests/src/android/text/StaticLayoutBidiTest.java +++ b/core/tests/coretests/src/android/text/StaticLayoutBidiTest.java @@ -22,7 +22,7 @@ import android.util.Log; import junit.framework.TestCase; /** - * Tests StaticLayout bidi implementation. + * Quick check of native bidi implementation. */ public class StaticLayoutBidiTest extends TestCase { @@ -41,73 +41,47 @@ public class StaticLayoutBidiTest extends TestCase { //@SmallTest public void testAllLtr() { - expectBidi(REQ_DL, "a test", "000000", L); + expectNativeBidi(REQ_DL, "a test", "000000", L); } //@SmallTest public void testLtrRtl() { - expectBidi(REQ_DL, "abc " + ALEF + BET + GIMEL, "0000111", L); + expectNativeBidi(REQ_DL, "abc " + ALEF + BET + GIMEL, "0000111", L); } //@SmallTest public void testAllRtl() { - expectBidi(REQ_DL, ALEF + SP + ALEF + BET + GIMEL + DALET, "111111", R); + expectNativeBidi(REQ_DL, ALEF + SP + ALEF + BET + GIMEL + DALET, "111111", R); } //@SmallTest public void testRtlLtr() { - expectBidi(REQ_DL, ALEF + BET + GIMEL + " abc", "1111000", R); + expectNativeBidi(REQ_DL, ALEF + BET + GIMEL + " abc", "1111222", R); } //@SmallTest public void testRAllLtr() { - expectBidi(REQ_R, "a test", "000000", R); + expectNativeBidi(REQ_R, "a test", "222222", R); } //@SmallTest public void testRLtrRtl() { - expectBidi(REQ_R, "abc " + ALEF + BET + GIMEL, "0001111", R); + expectNativeBidi(REQ_R, "abc " + ALEF + BET + GIMEL, "2221111", R); } //@SmallTest public void testLAllRtl() { - expectBidi(REQ_L, ALEF + SP + ALEF + BET + GIMEL + DALET, "111111", L); + expectNativeBidi(REQ_L, ALEF + SP + ALEF + BET + GIMEL + DALET, "111111", L); } //@SmallTest public void testLRtlLtr() { - expectBidi(REQ_L, ALEF + BET + GIMEL + " abc", "1110000", L); - } - - private void expectBidi(int dir, String text, - String expectedLevels, int expectedDir) { - char[] chs = text.toCharArray(); - int n = chs.length; - byte[] chInfo = new byte[n]; - - int resultDir = StaticLayout.bidi(dir, chs, chInfo, n, false); - - { - StringBuilder sb = new StringBuilder("info:"); - for (int i = 0; i < n; ++i) { - sb.append(" ").append(String.valueOf(chInfo[i])); - } - Log.i("BIDI", sb.toString()); - } - - char[] resultLevelChars = new char[n]; - for (int i = 0; i < n; ++i) { - resultLevelChars[i] = (char)('0' + chInfo[i]); - } - String resultLevels = new String(resultLevelChars); - assertEquals("direction", expectedDir, resultDir); - assertEquals("levels", expectedLevels, resultLevels); + expectNativeBidi(REQ_DL, ALEF + BET + GIMEL + " abc", "1111222", R); } //@SmallTest public void testNativeBidi() { - // native bidi returns levels, not simply directions - expectNativeBidi(REQ_DL, ALEF + BET + GIMEL + " abc", "1111222", R); + expectNativeBidi(REQ_L, ALEF + BET + GIMEL + " abc", "1110000", L); } private void expectNativeBidi(int dir, String text, diff --git a/core/tests/coretests/src/android/text/StaticLayoutDirectionsTest.java b/core/tests/coretests/src/android/text/StaticLayoutDirectionsTest.java new file mode 100644 index 0000000..4fde849 --- /dev/null +++ b/core/tests/coretests/src/android/text/StaticLayoutDirectionsTest.java @@ -0,0 +1,243 @@ +/* + * Copyright (C) 2010 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; + +import android.text.Layout.Directions; +import android.text.StaticLayoutTest.LayoutBuilder; + +import java.util.Arrays; +import java.util.Formatter; + +import junit.framework.TestCase; + +public class StaticLayoutDirectionsTest extends TestCase { + private static final char ALEF = '\u05d0'; + + private static Directions dirs(int ... dirs) { + return new Directions(dirs); + } + + // constants from Layout that are package-protected + private static final int RUN_LENGTH_MASK = 0x03ffffff; + private static final int RUN_LEVEL_SHIFT = 26; + private static final int RUN_LEVEL_MASK = 0x3f; + private static final int RUN_RTL_FLAG = 1 << RUN_LEVEL_SHIFT; + + private static final Directions DIRS_ALL_LEFT_TO_RIGHT = + new Directions(new int[] { 0, RUN_LENGTH_MASK }); + private static final Directions DIRS_ALL_RIGHT_TO_LEFT = + new Directions(new int[] { 0, RUN_LENGTH_MASK | RUN_RTL_FLAG }); + + private static final int LVL1_1 = 1 | (1 << RUN_LEVEL_SHIFT); + private static final int LVL2_1 = 1 | (2 << RUN_LEVEL_SHIFT); + private static final int LVL2_2 = 2 | (2 << RUN_LEVEL_SHIFT); + + private static String[] texts = { + "", + " ", + "a", + "a1", + "aA", + "a1b", + "a1A", + "aA1", + "aAb", + "aA1B", + "aA1B2", + + // rtl + "A", + "A1", + "Aa", + "A1B", + "A1a", + "Aa1", + "AaB" + }; + + // Expected directions are an array of start/length+level pairs, + // in visual order from the leading margin. + private static Directions[] expected = { + DIRS_ALL_LEFT_TO_RIGHT, + DIRS_ALL_LEFT_TO_RIGHT, + DIRS_ALL_LEFT_TO_RIGHT, + DIRS_ALL_LEFT_TO_RIGHT, + dirs(0, 1, 1, LVL1_1), + DIRS_ALL_LEFT_TO_RIGHT, + dirs(0, 2, 2, LVL1_1), + dirs(0, 1, 2, LVL2_1, 1, LVL1_1), + dirs(0, 1, 1, LVL1_1, 2, 1), + dirs(0, 1, 3, LVL1_1, 2, LVL2_1, 1, LVL1_1), + dirs(0, 1, 4, LVL2_1, 3, LVL1_1, 2, LVL2_1, 1, LVL1_1), + + // rtl + DIRS_ALL_RIGHT_TO_LEFT, + dirs(0, LVL1_1, 1, LVL2_1), + dirs(0, LVL1_1, 1, LVL2_1), + dirs(0, LVL1_1, 1, LVL2_1, 2, LVL1_1), + dirs(0, LVL1_1, 1, LVL2_2), + dirs(0, LVL1_1, 1, LVL2_2), + dirs(0, LVL1_1, 1, LVL2_1, 2, LVL1_1), + }; + + private static String pseudoBidiToReal(String src) { + char[] chars = src.toCharArray(); + for (int j = 0; j < chars.length; ++j) { + char c = chars[j]; + if (c >= 'A' && c <= 'D') { + chars[j] = (char)(ALEF + c - 'A'); + } + } + + return new String(chars, 0, chars.length); + } + + // @SmallTest + public void testDirections() { + StringBuilder buf = new StringBuilder("\n"); + Formatter f = new Formatter(buf); + + LayoutBuilder b = StaticLayoutTest.builder(); + for (int i = 0; i < texts.length; ++i) { + b.setText(pseudoBidiToReal(texts[i])); + checkDirections(b.build(), i, b.text, expected, f); + } + if (buf.length() > 1) { + fail(buf.toString()); + } + } + + // @SmallTest + public void testTrailingWhitespace() { + LayoutBuilder b = StaticLayoutTest.builder(); + b.setText(pseudoBidiToReal("Ab c")); + float width = b.paint.measureText(b.text, 0, 5); // exclude 'c' + b.setWidth(Math.round(width)); + Layout l = b.build(); + if (l.getLineCount() != 2) { + throw new RuntimeException("expected 2 lines, got: " + l.getLineCount()); + } + Directions result = l.getLineDirections(0); + Directions expected = dirs(0, LVL1_1, 1, LVL2_1, 2, 3 | (1 << Layout.RUN_LEVEL_SHIFT)); + expectDirections("split line", expected, result); + } + + public void testNextToRightOf() { + LayoutBuilder b = StaticLayoutTest.builder(); + b.setText(pseudoBidiToReal("aA1B2")); + // visual a2B1A positions 04321 + // 0: |a2B1A, strong is sol, after -> 0 + // 1: a|2B1A, strong is a, after ->, 1 + // 2: a2|B1A, strong is B, after -> 4 + // 3: a2B|1A, strong is B, before -> 3 + // 4: a2B1|A, strong is A, after -> 2 + // 5: a2B1A|, strong is eol, before -> 5 + int[] expected = { 0, 1, 4, 3, 2, 5 }; + Layout l = b.build(); + int n = 0; + for (int i = 1; i < expected.length; ++i) { + int t = l.getOffsetToRightOf(n); + if (t != expected[i]) { + fail("offset[" + i + "] to right of: " + n + " expected: " + + expected[i] + " got: " + t); + } + n = t; + } + } + + public void testNextToLeftOf() { + LayoutBuilder b = StaticLayoutTest.builder(); + b.setText(pseudoBidiToReal("aA1B2")); + int[] expected = { 0, 1, 4, 3, 2, 5 }; + Layout l = b.build(); + int n = 5; + for (int i = expected.length - 1; --i >= 0;) { + int t = l.getOffsetToLeftOf(n); + if (t != expected[i]) { + fail("offset[" + i + "] to left of: " + n + " expected: " + + expected[i] + " got: " + t); + } + n = t; + } + } + + // utility, not really a test + /* + public void testMeasureText1() { + LayoutBuilder b = StaticLayoutTest.builder(); + String text = "ABC"; // "abAB" + b.setText(pseudoBidiToReal(text)); + Layout l = b.build(); + Directions directions = l.getLineDirections(0); + + TextPaint workPaint = new TextPaint(); + + int dir = -1; // LEFT_TO_RIGHT + boolean trailing = true; + boolean alt = true; + do { + dir = -dir; + do { + trailing = !trailing; + for (int offset = 0, end = b.text.length(); offset <= end; ++offset) { + float width = Layout.measureText(b.paint, + workPaint, + b.text, + 0, offset, end, + dir, directions, + trailing, false, + null); + Log.i("BIDI", "dir: " + dir + " trail: " + trailing + + " offset: " + offset + " width: " + width); + } + } while (!trailing); + } while (dir > 0); + } + */ + + // utility for displaying arrays in hex + private static String hexArray(int[] array) { + StringBuilder sb = new StringBuilder(); + sb.append('{'); + for (int i : array) { + if (sb.length() > 1) { + sb.append(", "); + } + sb.append(Integer.toHexString(i)); + } + sb.append('}'); + return sb.toString(); + } + + private void checkDirections(Layout l, int i, String text, + Directions[] expectedDirs, Formatter f) { + Directions expected = expectedDirs[i]; + Directions result = l.getLineDirections(0); + if (!Arrays.equals(expected.mDirections, result.mDirections)) { + f.format("%n[%2d] '%s', %s != %s", i, text, + hexArray(expected.mDirections), + hexArray(result.mDirections)); + } + } + + private void expectDirections(String msg, Directions expected, Directions result) { + if (!Arrays.equals(expected.mDirections, result.mDirections)) { + fail("expected: " + hexArray(expected.mDirections) + + " got: " + hexArray(result.mDirections)); + } + } +} diff --git a/core/tests/coretests/src/android/text/StaticLayoutTest.java b/core/tests/coretests/src/android/text/StaticLayoutTest.java index 1f58a2c..d554a50 100644 --- a/core/tests/coretests/src/android/text/StaticLayoutTest.java +++ b/core/tests/coretests/src/android/text/StaticLayoutTest.java @@ -228,11 +228,11 @@ public class StaticLayoutTest extends TestCase { } } - private static LayoutBuilder builder() { + /* package */ static LayoutBuilder builder() { return new LayoutBuilder(); } - private static class LayoutBuilder { + /* package */ static class LayoutBuilder { String text = "This is a test"; TextPaint paint = new TextPaint(); // default int width = 100; diff --git a/core/tests/coretests/src/android/util/ExpandableListScenario.java b/core/tests/coretests/src/android/util/ExpandableListScenario.java deleted file mode 100644 index 4a12b0d..0000000 --- a/core/tests/coretests/src/android/util/ExpandableListScenario.java +++ /dev/null @@ -1,386 +0,0 @@ -/* - * Copyright (C) 2007 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.util; - -import java.util.ArrayList; -import java.util.List; - -import android.view.Gravity; -import android.view.View; -import android.view.ViewGroup; -import android.widget.AbsListView; -import android.widget.BaseExpandableListAdapter; -import android.widget.ExpandableListAdapter; -import android.widget.ExpandableListView; -import android.widget.ListView; -import android.widget.TextView; - -/** - * Utility base class for creating various Expandable List scenarios. - * <p> - * WARNING: A lot of the features are mixed between ListView's expected position - * (flat list position) and an ExpandableListView's expected position. You must add/change - * features as you need them. - * - * @see ListScenario - */ -public abstract class ExpandableListScenario extends ListScenario { - protected ExpandableListAdapter mAdapter; - protected List<MyGroup> mGroups; - - @Override - protected ListView createListView() { - return new ExpandableListView(this); - } - - @Override - protected Params createParams() { - return new ExpandableParams(); - } - - @Override - protected void setAdapter(ListView listView) { - ((ExpandableListView) listView).setAdapter(mAdapter = createAdapter()); - } - - protected ExpandableListAdapter createAdapter() { - return new MyAdapter(); - } - - @Override - protected void readAndValidateParams(Params params) { - ExpandableParams expandableParams = (ExpandableParams) params; - - int[] numChildren = expandableParams.mNumChildren; - - mGroups = new ArrayList<MyGroup>(numChildren.length); - for (int i = 0; i < numChildren.length; i++) { - mGroups.add(new MyGroup(numChildren[i])); - } - - expandableParams.superSetNumItems(); - - super.readAndValidateParams(params); - } - - /** - * Get the ExpandableListView widget. - * @return The main widget. - */ - public ExpandableListView getExpandableListView() { - return (ExpandableListView) super.getListView(); - } - - public static class ExpandableParams extends Params { - private int[] mNumChildren; - - /** - * Sets the number of children per group. - * - * @param numChildrenPerGroup The number of children per group. - */ - public ExpandableParams setNumChildren(int[] numChildren) { - mNumChildren = numChildren; - return this; - } - - /** - * Sets the number of items on the superclass based on the number of - * groups and children per group. - */ - private ExpandableParams superSetNumItems() { - int numItems = 0; - - if (mNumChildren != null) { - for (int i = mNumChildren.length - 1; i >= 0; i--) { - numItems += mNumChildren[i]; - } - } - - super.setNumItems(numItems); - - return this; - } - - @Override - public Params setNumItems(int numItems) { - throw new IllegalStateException("Use setNumGroups and setNumChildren instead."); - } - - @Override - public ExpandableParams setFadingEdgeScreenSizeFactor(double fadingEdgeScreenSizeFactor) { - return (ExpandableParams) super.setFadingEdgeScreenSizeFactor(fadingEdgeScreenSizeFactor); - } - - @Override - public ExpandableParams setItemScreenSizeFactor(double itemScreenSizeFactor) { - return (ExpandableParams) super.setItemScreenSizeFactor(itemScreenSizeFactor); - } - - @Override - public ExpandableParams setItemsFocusable(boolean itemsFocusable) { - return (ExpandableParams) super.setItemsFocusable(itemsFocusable); - } - - @Override - public ExpandableParams setMustFillScreen(boolean fillScreen) { - return (ExpandableParams) super.setMustFillScreen(fillScreen); - } - - @Override - public ExpandableParams setPositionScreenSizeFactorOverride(int position, double itemScreenSizeFactor) { - return (ExpandableParams) super.setPositionScreenSizeFactorOverride(position, itemScreenSizeFactor); - } - - @Override - public ExpandableParams setPositionUnselectable(int position) { - return (ExpandableParams) super.setPositionUnselectable(position); - } - - @Override - public ExpandableParams setStackFromBottom(boolean stackFromBottom) { - return (ExpandableParams) super.setStackFromBottom(stackFromBottom); - } - - @Override - public ExpandableParams setStartingSelectionPosition(int startingSelectionPosition) { - return (ExpandableParams) super.setStartingSelectionPosition(startingSelectionPosition); - } - - @Override - public ExpandableParams setConnectAdapter(boolean connectAdapter) { - return (ExpandableParams) super.setConnectAdapter(connectAdapter); - } - } - - /** - * Gets a string for the value of some item. - * @param packedPosition The position of the item. - * @return The string. - */ - public final String getValueAtPosition(long packedPosition) { - final int type = ExpandableListView.getPackedPositionType(packedPosition); - - if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { - return mGroups.get(ExpandableListView.getPackedPositionGroup(packedPosition)) - .children.get(ExpandableListView.getPackedPositionChild(packedPosition)) - .name; - } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) { - return mGroups.get(ExpandableListView.getPackedPositionGroup(packedPosition)) - .name; - } else { - throw new IllegalStateException("packedPosition is not a valid position."); - } - } - - /** - * Whether a particular position is out of bounds. - * - * @param packedPosition The packed position. - * @return Whether it's out of bounds. - */ - private boolean isOutOfBounds(long packedPosition) { - final int type = ExpandableListView.getPackedPositionType(packedPosition); - - if (type == ExpandableListView.PACKED_POSITION_TYPE_NULL) { - throw new IllegalStateException("packedPosition is not a valid position."); - } - - final int group = ExpandableListView.getPackedPositionGroup(packedPosition); - if (group >= mGroups.size() || group < 0) { - return true; - } - - if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { - final int child = ExpandableListView.getPackedPositionChild(packedPosition); - if (child >= mGroups.get(group).children.size() || child < 0) { - return true; - } - } - - return false; - } - - /** - * Gets a view for the packed position, possibly reusing the convertView. - * - * @param packedPosition The position to get a view for. - * @param convertView Optional view to convert. - * @param parent The future parent. - * @return A view. - */ - private View getView(long packedPosition, View convertView, ViewGroup parent) { - if (isOutOfBounds(packedPosition)) { - throw new IllegalStateException("position out of range for adapter!"); - } - - final ExpandableListView elv = getExpandableListView(); - final int flPos = elv.getFlatListPosition(packedPosition); - - if (convertView != null) { - ((TextView) convertView).setText(getValueAtPosition(packedPosition)); - convertView.setId(flPos); - return convertView; - } - - int desiredHeight = getHeightForPosition(flPos); - return createView(packedPosition, flPos, parent, desiredHeight); - } - - /** - * Create a view for a group or child position. - * - * @param packedPosition The packed position (has type, group pos, and optionally child pos). - * @param flPos The flat list position (the position that the ListView goes by). - * @param parent The parent view. - * @param desiredHeight The desired height. - * @return A view. - */ - protected View createView(long packedPosition, int flPos, ViewGroup parent, int desiredHeight) { - TextView result = new TextView(parent.getContext()); - result.setHeight(desiredHeight); - result.setText(getValueAtPosition(packedPosition)); - final ViewGroup.LayoutParams lp = new AbsListView.LayoutParams( - ViewGroup.LayoutParams.MATCH_PARENT, - ViewGroup.LayoutParams.WRAP_CONTENT); - result.setLayoutParams(lp); - result.setGravity(Gravity.CENTER_VERTICAL); - result.setPadding(36, 0, 0, 0); - result.setId(flPos); - return result; - } - - /** - * Returns a group index containing either the number of children or at - * least one child. - * - * @param numChildren The group must have this amount, or -1 if using - * atLeastOneChild. - * @param atLeastOneChild The group must have at least one child, or false - * if using numChildren. - * @return A group index with the requirements. - */ - public int findGroupWithNumChildren(int numChildren, boolean atLeastOneChild) { - final ExpandableListAdapter adapter = mAdapter; - - for (int i = adapter.getGroupCount() - 1; i >= 0; i--) { - final int curNumChildren = adapter.getChildrenCount(i); - - if (numChildren == curNumChildren || atLeastOneChild && curNumChildren > 0) { - return i; - } - } - - return -1; - } - - public List<MyGroup> getGroups() { - return mGroups; - } - - public ExpandableListAdapter getAdapter() { - return mAdapter; - } - - /** - * Simple expandable list adapter. - */ - protected class MyAdapter extends BaseExpandableListAdapter { - public Object getChild(int groupPosition, int childPosition) { - return getValueAtPosition(ExpandableListView.getPackedPositionForChild(groupPosition, - childPosition)); - } - - public long getChildId(int groupPosition, int childPosition) { - return mGroups.get(groupPosition).children.get(childPosition).id; - } - - public int getChildrenCount(int groupPosition) { - return mGroups.get(groupPosition).children.size(); - } - - public View getChildView(int groupPosition, int childPosition, boolean isLastChild, - View convertView, ViewGroup parent) { - return getView(ExpandableListView.getPackedPositionForChild(groupPosition, - childPosition), convertView, parent); - } - - public Object getGroup(int groupPosition) { - return getValueAtPosition(ExpandableListView.getPackedPositionForGroup(groupPosition)); - } - - public int getGroupCount() { - return mGroups.size(); - } - - public long getGroupId(int groupPosition) { - return mGroups.get(groupPosition).id; - } - - public View getGroupView(int groupPosition, boolean isExpanded, View convertView, - ViewGroup parent) { - return getView(ExpandableListView.getPackedPositionForGroup(groupPosition), - convertView, parent); - } - - public boolean isChildSelectable(int groupPosition, int childPosition) { - return true; - } - - public boolean hasStableIds() { - return true; - } - - } - - public static class MyGroup { - private static long mNextId = 1000; - - String name; - long id = mNextId++; - List<MyChild> children; - - public MyGroup(int numChildren) { - name = "Group " + id; - children = new ArrayList<MyChild>(numChildren); - for (int i = 0; i < numChildren; i++) { - children.add(new MyChild()); - } - } - } - - public static class MyChild { - private static long mNextId = 2000; - - String name; - long id = mNextId++; - - public MyChild() { - name = "Child " + id; - } - } - - @Override - protected final void init(Params params) { - init((ExpandableParams) params); - } - - /** - * @see ListScenario#init - */ - protected abstract void init(ExpandableParams params); -} diff --git a/core/tests/coretests/src/android/widget/expandablelistview/ExpandableListBasicTest.java b/core/tests/coretests/src/android/widget/expandablelistview/ExpandableListBasicTest.java deleted file mode 100644 index e23b516..0000000 --- a/core/tests/coretests/src/android/widget/expandablelistview/ExpandableListBasicTest.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Copyright (C) 2007 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.expandablelistview; - -import android.test.ActivityInstrumentationTestCase2; -import android.test.suitebuilder.annotation.MediumTest; -import android.util.ExpandableListScenario; -import android.util.ListUtil; -import android.util.ExpandableListScenario.MyGroup; -import android.view.KeyEvent; -import android.widget.BaseExpandableListAdapter; -import android.widget.ExpandableListAdapter; -import android.widget.ExpandableListView; - -import java.util.List; - -public class ExpandableListBasicTest extends ActivityInstrumentationTestCase2<ExpandableListSimple> { - private ExpandableListScenario mActivity; - private ExpandableListView mExpandableListView; - private ExpandableListAdapter mAdapter; - private ListUtil mListUtil; - - public ExpandableListBasicTest() { - super(ExpandableListSimple.class); - } - - @Override - protected void setUp() throws Exception { - super.setUp(); - - mActivity = getActivity(); - mExpandableListView = mActivity.getExpandableListView(); - mAdapter = mExpandableListView.getExpandableListAdapter(); - mListUtil = new ListUtil(mExpandableListView, getInstrumentation()); - } - - @MediumTest - public void testPreconditions() { - assertNotNull(mActivity); - assertNotNull(mExpandableListView); - } - - private int expandGroup(int numChildren, boolean atLeastOneChild) { - final int groupPos = mActivity.findGroupWithNumChildren(numChildren, atLeastOneChild); - assertTrue("Could not find group to expand", groupPos >= 0); - - assertFalse("Group is already expanded", mExpandableListView.isGroupExpanded(groupPos)); - mListUtil.arrowScrollToSelectedPosition(groupPos); - getInstrumentation().waitForIdleSync(); - sendKeys(KeyEvent.KEYCODE_DPAD_CENTER); - getInstrumentation().waitForIdleSync(); - assertTrue("Group did not expand", mExpandableListView.isGroupExpanded(groupPos)); - - return groupPos; - } - - @MediumTest - public void testExpandGroup() { - expandGroup(-1, true); - } - - @MediumTest - public void testCollapseGroup() { - final int groupPos = expandGroup(-1, true); - - sendKeys(KeyEvent.KEYCODE_DPAD_CENTER); - getInstrumentation().waitForIdleSync(); - assertFalse("Group did not collapse", mExpandableListView.isGroupExpanded(groupPos)); - } - - @MediumTest - public void testExpandedGroupMovement() { - // Expand the first group - mListUtil.arrowScrollToSelectedPosition(0); - sendKeys(KeyEvent.KEYCODE_DPAD_CENTER); - getInstrumentation().waitForIdleSync(); - - // Ensure it expanded - assertTrue("Group did not expand", mExpandableListView.isGroupExpanded(0)); - - // Wait until that's all good - getInstrumentation().waitForIdleSync(); - - // Make sure it expanded - assertTrue("Group did not expand", mExpandableListView.isGroupExpanded(0)); - - // Insert a collapsed group in front of the one just expanded - List<MyGroup> groups = mActivity.getGroups(); - MyGroup insertedGroup = new MyGroup(1); - groups.add(0, insertedGroup); - - // Notify data change - assertTrue("Adapter is not an instance of the base adapter", - mAdapter instanceof BaseExpandableListAdapter); - final BaseExpandableListAdapter adapter = (BaseExpandableListAdapter) mAdapter; - - mActivity.runOnUiThread(new Runnable() { - public void run() { - adapter.notifyDataSetChanged(); - } - }); - getInstrumentation().waitForIdleSync(); - - // Make sure the right group is expanded - assertTrue("The expanded state didn't stay with the proper group", - mExpandableListView.isGroupExpanded(1)); - assertFalse("The expanded state was given to the inserted group", - mExpandableListView.isGroupExpanded(0)); - } - - @MediumTest - public void testContextMenus() { - ExpandableListTester tester = new ExpandableListTester(mExpandableListView, this); - tester.testContextMenus(); - } - - @MediumTest - public void testConvertionBetweenFlatAndPacked() { - ExpandableListTester tester = new ExpandableListTester(mExpandableListView, this); - tester.testConvertionBetweenFlatAndPackedOnGroups(); - tester.testConvertionBetweenFlatAndPackedOnChildren(); - } - - @MediumTest - public void testSelectedPosition() { - ExpandableListTester tester = new ExpandableListTester(mExpandableListView, this); - tester.testSelectedPositionOnGroups(); - tester.testSelectedPositionOnChildren(); - } -} diff --git a/core/tests/coretests/src/android/widget/expandablelistview/ExpandableListSimple.java b/core/tests/coretests/src/android/widget/expandablelistview/ExpandableListSimple.java deleted file mode 100644 index 78db28c..0000000 --- a/core/tests/coretests/src/android/widget/expandablelistview/ExpandableListSimple.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (C) 2007 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.expandablelistview; - -import android.view.Menu; -import android.view.MenuItem; -import android.view.MenuItem.OnMenuItemClickListener; -import android.widget.BaseExpandableListAdapter; - -import android.util.ExpandableListScenario; - -public class ExpandableListSimple extends ExpandableListScenario { - private static final int[] NUM_CHILDREN = {4, 3, 2, 1, 0}; - - @Override - protected void init(ExpandableParams params) { - params.setNumChildren(NUM_CHILDREN) - .setItemScreenSizeFactor(0.14); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - - menu.add("Add item").setOnMenuItemClickListener(new OnMenuItemClickListener() { - public boolean onMenuItemClick(MenuItem item) { - mGroups.add(0, new MyGroup(2)); - ((BaseExpandableListAdapter) mAdapter).notifyDataSetChanged(); - return true; - } - }); - - return true; - } - -} diff --git a/core/tests/coretests/src/android/widget/expandablelistview/ExpandableListTester.java b/core/tests/coretests/src/android/widget/expandablelistview/ExpandableListTester.java deleted file mode 100644 index dfb10fb..0000000 --- a/core/tests/coretests/src/android/widget/expandablelistview/ExpandableListTester.java +++ /dev/null @@ -1,241 +0,0 @@ -/* - * Copyright (C) 2007 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.expandablelistview; - -import android.app.Instrumentation; -import android.test.ActivityInstrumentationTestCase2; -import android.util.ExpandableListScenario; -import android.util.ListUtil; -import android.view.KeyEvent; -import android.view.View; -import android.widget.ExpandableListAdapter; -import android.widget.ExpandableListView; - -import junit.framework.Assert; - -public class ExpandableListTester { - private final ExpandableListView mExpandableListView; - private final ExpandableListAdapter mAdapter; - private final ListUtil mListUtil; - - private final ActivityInstrumentationTestCase2<? extends ExpandableListScenario> - mActivityInstrumentation; - - Instrumentation mInstrumentation; - - public ExpandableListTester( - ExpandableListView expandableListView, - ActivityInstrumentationTestCase2<? extends ExpandableListScenario> - activityInstrumentation) { - mExpandableListView = expandableListView; - Instrumentation instrumentation = activityInstrumentation.getInstrumentation(); - mListUtil = new ListUtil(mExpandableListView, instrumentation); - mAdapter = mExpandableListView.getExpandableListAdapter(); - mActivityInstrumentation = activityInstrumentation; - mInstrumentation = mActivityInstrumentation.getInstrumentation(); - } - - private void expandGroup(final int groupIndex, int flatPosition) { - Assert.assertFalse("Group is already expanded", mExpandableListView - .isGroupExpanded(groupIndex)); - mListUtil.arrowScrollToSelectedPosition(flatPosition); - mInstrumentation.waitForIdleSync(); - mActivityInstrumentation.sendKeys(KeyEvent.KEYCODE_DPAD_CENTER); - mActivityInstrumentation.getInstrumentation().waitForIdleSync(); - Assert.assertTrue("Group did not expand " + groupIndex, - mExpandableListView.isGroupExpanded(groupIndex)); - } - - void testContextMenus() { - // Add a position tester ContextMenu listener to the ExpandableListView - PositionTesterContextMenuListener menuListener = new PositionTesterContextMenuListener(); - mExpandableListView.setOnCreateContextMenuListener(menuListener); - - int index = 0; - - // Scrolling on header elements should trigger an AdapterContextMenu - for (int i=0; i<mExpandableListView.getHeaderViewsCount(); i++) { - // Check group index in context menu - menuListener.expectAdapterContextMenu(i); - // Make sure the group is visible so that getChild finds it - mListUtil.arrowScrollToSelectedPosition(index); - View headerChild = mExpandableListView.getChildAt(index - - mExpandableListView.getFirstVisiblePosition()); - mExpandableListView.showContextMenuForChild(headerChild); - mInstrumentation.waitForIdleSync(); - Assert.assertNull(menuListener.getErrorMessage(), menuListener.getErrorMessage()); - index++; - } - - int groupCount = mAdapter.getGroupCount(); - for (int groupIndex = 0; groupIndex < groupCount; groupIndex++) { - - // Expand group - expandGroup(groupIndex, index); - - // Check group index in context menu - menuListener.expectGroupContextMenu(groupIndex); - // Make sure the group is visible so that getChild finds it - mListUtil.arrowScrollToSelectedPosition(index); - View groupChild = mExpandableListView.getChildAt(index - - mExpandableListView.getFirstVisiblePosition()); - mExpandableListView.showContextMenuForChild(groupChild); - mInstrumentation.waitForIdleSync(); - Assert.assertNull(menuListener.getErrorMessage(), menuListener.getErrorMessage()); - index++; - - final int childrenCount = mAdapter.getChildrenCount(groupIndex); - for (int childIndex = 0; childIndex < childrenCount; childIndex++) { - // Check child index in context menu - mListUtil.arrowScrollToSelectedPosition(index); - menuListener.expectChildContextMenu(groupIndex, childIndex); - View child = mExpandableListView.getChildAt(index - - mExpandableListView.getFirstVisiblePosition()); - mExpandableListView.showContextMenuForChild(child); - mInstrumentation.waitForIdleSync(); - Assert.assertNull(menuListener.getErrorMessage(), menuListener.getErrorMessage()); - index++; - } - } - - // Scrolling on footer elements should trigger an AdapterContextMenu - for (int i=0; i<mExpandableListView.getFooterViewsCount(); i++) { - // Check group index in context menu - menuListener.expectAdapterContextMenu(index); - // Make sure the group is visible so that getChild finds it - mListUtil.arrowScrollToSelectedPosition(index); - View footerChild = mExpandableListView.getChildAt(index - - mExpandableListView.getFirstVisiblePosition()); - mExpandableListView.showContextMenuForChild(footerChild); - mInstrumentation.waitForIdleSync(); - Assert.assertNull(menuListener.getErrorMessage(), menuListener.getErrorMessage()); - index++; - } - - // Cleanup: remove the listener we added. - mExpandableListView.setOnCreateContextMenuListener(null); - } - - private int expandAGroup() { - final int groupIndex = 2; - final int headerCount = mExpandableListView.getHeaderViewsCount(); - Assert.assertTrue("Not enough groups", groupIndex < mAdapter.getGroupCount()); - expandGroup(groupIndex, groupIndex + headerCount); - return groupIndex; - } - - // This method assumes that NO group is expanded when called - void testConvertionBetweenFlatAndPackedOnGroups() { - final int headerCount = mExpandableListView.getHeaderViewsCount(); - - for (int i=0; i<headerCount; i++) { - Assert.assertEquals("Non NULL position for header item", - ExpandableListView.PACKED_POSITION_VALUE_NULL, - mExpandableListView.getExpandableListPosition(i)); - } - - // Test all (non expanded) groups - final int groupCount = mAdapter.getGroupCount(); - for (int groupIndex = 0; groupIndex < groupCount; groupIndex++) { - int expectedFlatPosition = headerCount + groupIndex; - long packedPositionForGroup = ExpandableListView.getPackedPositionForGroup(groupIndex); - Assert.assertEquals("Group not found at flat position " + expectedFlatPosition, - packedPositionForGroup, - mExpandableListView.getExpandableListPosition(expectedFlatPosition)); - - Assert.assertEquals("Wrong flat position for group " + groupIndex, - expectedFlatPosition, - mExpandableListView.getFlatListPosition(packedPositionForGroup)); - } - - for (int i=0; i<mExpandableListView.getFooterViewsCount(); i++) { - Assert.assertEquals("Non NULL position for header item", - ExpandableListView.PACKED_POSITION_VALUE_NULL, - mExpandableListView.getExpandableListPosition(headerCount + groupCount + i)); - } - } - - // This method assumes that NO group is expanded when called - void testConvertionBetweenFlatAndPackedOnChildren() { - // Test with an expanded group - final int headerCount = mExpandableListView.getHeaderViewsCount(); - final int groupIndex = expandAGroup(); - - final int childrenCount = mAdapter.getChildrenCount(groupIndex); - for (int childIndex = 0; childIndex < childrenCount; childIndex++) { - int expectedFlatPosition = headerCount + groupIndex + 1 + childIndex; - long childPos = ExpandableListView.getPackedPositionForChild(groupIndex, childIndex); - - Assert.assertEquals("Wrong flat position for child ", - childPos, - mExpandableListView.getExpandableListPosition(expectedFlatPosition)); - - Assert.assertEquals("Wrong flat position for child ", - expectedFlatPosition, - mExpandableListView.getFlatListPosition(childPos)); - } - } - - // This method assumes that NO group is expanded when called - void testSelectedPositionOnGroups() { - int index = 0; - - // Scrolling on header elements should not give a valid selected position. - for (int i=0; i<mExpandableListView.getHeaderViewsCount(); i++) { - mListUtil.arrowScrollToSelectedPosition(index); - Assert.assertEquals("Header item is selected", - ExpandableListView.PACKED_POSITION_VALUE_NULL, - mExpandableListView.getSelectedPosition()); - index++; - } - - // Check selection on group items - final int groupCount = mAdapter.getGroupCount(); - for (int groupIndex = 0; groupIndex < groupCount; groupIndex++) { - mListUtil.arrowScrollToSelectedPosition(index); - Assert.assertEquals("Group item is not selected", - ExpandableListView.getPackedPositionForGroup(groupIndex), - mExpandableListView.getSelectedPosition()); - index++; - } - - // Scrolling on footer elements should not give a valid selected position. - for (int i=0; i<mExpandableListView.getFooterViewsCount(); i++) { - mListUtil.arrowScrollToSelectedPosition(index); - Assert.assertEquals("Footer item is selected", - ExpandableListView.PACKED_POSITION_VALUE_NULL, - mExpandableListView.getSelectedPosition()); - index++; - } - } - - // This method assumes that NO group is expanded when called - void testSelectedPositionOnChildren() { - // Test with an expanded group - final int headerCount = mExpandableListView.getHeaderViewsCount(); - final int groupIndex = expandAGroup(); - - final int childrenCount = mAdapter.getChildrenCount(groupIndex); - for (int childIndex = 0; childIndex < childrenCount; childIndex++) { - int childFlatPosition = headerCount + groupIndex + 1 + childIndex; - mListUtil.arrowScrollToSelectedPosition(childFlatPosition); - Assert.assertEquals("Group item is not selected", - ExpandableListView.getPackedPositionForChild(groupIndex, childIndex), - mExpandableListView.getSelectedPosition()); - } - } -} diff --git a/core/tests/coretests/src/android/widget/expandablelistview/ExpandableListWithHeaders.java b/core/tests/coretests/src/android/widget/expandablelistview/ExpandableListWithHeaders.java deleted file mode 100644 index 2251c1d..0000000 --- a/core/tests/coretests/src/android/widget/expandablelistview/ExpandableListWithHeaders.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (C) 2007 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.expandablelistview; - -import android.os.Bundle; -import android.util.ExpandableListScenario; -import android.widget.Button; -import android.widget.ExpandableListView; - -public class ExpandableListWithHeaders extends ExpandableListScenario { - private static final int[] sNumChildren = {1, 4, 3, 2, 6}; - private static final int sNumOfHeadersAndFooters = 12; - - @Override - protected void init(ExpandableParams params) { - params.setStackFromBottom(false) - .setStartingSelectionPosition(-1) - .setNumChildren(sNumChildren) - .setItemScreenSizeFactor(0.14) - .setConnectAdapter(false); - } - - @Override - protected void onCreate(Bundle icicle) { - super.onCreate(icicle); - - final ExpandableListView expandableListView = getExpandableListView(); - expandableListView.setItemsCanFocus(true); - - for (int i = 0; i < sNumOfHeadersAndFooters; i++) { - Button header = new Button(this); - header.setText("Header View " + i); - expandableListView.addHeaderView(header); - } - - for (int i = 0; i < sNumOfHeadersAndFooters; i++) { - Button footer = new Button(this); - footer.setText("Footer View " + i); - expandableListView.addFooterView(footer); - } - - // Set adapter here AFTER we set header and footer views - setAdapter(expandableListView); - } - - /** - * @return The number of headers (and the same number of footers) - */ - public int getNumOfHeadersAndFooters() { - return sNumOfHeadersAndFooters; - } - -} diff --git a/core/tests/coretests/src/android/widget/expandablelistview/ExpandableListWithHeadersTest.java b/core/tests/coretests/src/android/widget/expandablelistview/ExpandableListWithHeadersTest.java deleted file mode 100644 index 64a0fff..0000000 --- a/core/tests/coretests/src/android/widget/expandablelistview/ExpandableListWithHeadersTest.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright (C) 2007 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.expandablelistview; - -import android.test.ActivityInstrumentationTestCase2; -import android.test.suitebuilder.annotation.LargeTest; -import android.test.suitebuilder.annotation.MediumTest; -import android.util.ListUtil; -import android.view.KeyEvent; -import android.widget.ExpandableListView; - -public class ExpandableListWithHeadersTest extends - ActivityInstrumentationTestCase2<ExpandableListWithHeaders> { - private ExpandableListView mExpandableListView; - private ListUtil mListUtil; - - public ExpandableListWithHeadersTest() { - super(ExpandableListWithHeaders.class); - } - - @Override - protected void setUp() throws Exception { - super.setUp(); - - mExpandableListView = getActivity().getExpandableListView(); - mListUtil = new ListUtil(mExpandableListView, getInstrumentation()); - } - - @MediumTest - public void testPreconditions() { - assertNotNull(mExpandableListView); - } - - @MediumTest - public void testExpandOnFirstPosition() { - // Should be a header, and hence the first group should NOT have expanded - mListUtil.arrowScrollToSelectedPosition(0); - sendKeys(KeyEvent.KEYCODE_DPAD_CENTER); - getInstrumentation().waitForIdleSync(); - assertFalse(mExpandableListView.isGroupExpanded(0)); - } - - @LargeTest - public void testExpandOnFirstGroup() { - mListUtil.arrowScrollToSelectedPosition(getActivity().getNumOfHeadersAndFooters()); - sendKeys(KeyEvent.KEYCODE_DPAD_CENTER); - getInstrumentation().waitForIdleSync(); - assertTrue(mExpandableListView.isGroupExpanded(0)); - } - - @MediumTest - public void testContextMenus() { - ExpandableListTester tester = new ExpandableListTester(mExpandableListView, this); - tester.testContextMenus(); - } - - @MediumTest - public void testConvertionBetweenFlatAndPacked() { - ExpandableListTester tester = new ExpandableListTester(mExpandableListView, this); - tester.testConvertionBetweenFlatAndPackedOnGroups(); - tester.testConvertionBetweenFlatAndPackedOnChildren(); - } - - @MediumTest - public void testSelectedPosition() { - ExpandableListTester tester = new ExpandableListTester(mExpandableListView, this); - tester.testSelectedPositionOnGroups(); - tester.testSelectedPositionOnChildren(); - } -} diff --git a/core/tests/coretests/src/android/widget/expandablelistview/InflatedExpandableListView.java b/core/tests/coretests/src/android/widget/expandablelistview/InflatedExpandableListView.java deleted file mode 100644 index f4c9d56..0000000 --- a/core/tests/coretests/src/android/widget/expandablelistview/InflatedExpandableListView.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright (C) 2007 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.expandablelistview; - -import com.android.frameworks.coretests.R; - -import android.app.Activity; -import android.os.Bundle; -import android.view.Gravity; -import android.view.View; -import android.view.ViewGroup; -import android.widget.AbsListView; -import android.widget.BaseExpandableListAdapter; -import android.widget.ExpandableListView; -import android.widget.TextView; - -public class InflatedExpandableListView extends Activity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - setContentView(R.layout.inflated_expandablelistview); - - ExpandableListView elv = (ExpandableListView) findViewById(R.id.elv); - elv.setAdapter(new MyExpandableListAdapter()); - } - - public class MyExpandableListAdapter extends BaseExpandableListAdapter { - // Sample data set. children[i] contains the children (String[]) for groups[i]. - private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" }; - private String[][] children = { - { "Arnold", "Barry", "Chuck", "David" }, - { "Ace", "Bandit", "Cha-Cha", "Deuce" }, - { "Fluffy", "Snuggles" }, - { "Goldy", "Bubbles" } - }; - - public Object getChild(int groupPosition, int childPosition) { - return children[groupPosition][childPosition]; - } - - public long getChildId(int groupPosition, int childPosition) { - return childPosition; - } - - public int getChildrenCount(int groupPosition) { - return children[groupPosition].length; - } - - public TextView getGenericView() { - // Layout parameters for the ExpandableListView - AbsListView.LayoutParams lp = new AbsListView.LayoutParams( - ViewGroup.LayoutParams.MATCH_PARENT, 64); - - TextView textView = new TextView(InflatedExpandableListView.this); - textView.setLayoutParams(lp); - // Center the text vertically - textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); - // Set the text starting position - textView.setPadding(36, 0, 0, 0); - return textView; - } - - public View getChildView(int groupPosition, int childPosition, boolean isLastChild, - View convertView, ViewGroup parent) { - TextView textView = getGenericView(); - textView.setText(getChild(groupPosition, childPosition).toString()); - return textView; - } - - public Object getGroup(int groupPosition) { - return groups[groupPosition]; - } - - public int getGroupCount() { - return groups.length; - } - - public long getGroupId(int groupPosition) { - return groupPosition; - } - - public View getGroupView(int groupPosition, boolean isExpanded, View convertView, - ViewGroup parent) { - TextView textView = getGenericView(); - textView.setText(getGroup(groupPosition).toString()); - return textView; - } - - public boolean isChildSelectable(int groupPosition, int childPosition) { - return true; - } - - public boolean hasStableIds() { - return true; - } - - } - -} diff --git a/core/tests/coretests/src/android/widget/expandablelistview/PositionTesterContextMenuListener.java b/core/tests/coretests/src/android/widget/expandablelistview/PositionTesterContextMenuListener.java deleted file mode 100644 index 2dbdff8..0000000 --- a/core/tests/coretests/src/android/widget/expandablelistview/PositionTesterContextMenuListener.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright (C) 2010 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.expandablelistview; - -import android.view.ContextMenu; -import android.view.View; -import android.view.ContextMenu.ContextMenuInfo; -import android.view.View.OnCreateContextMenuListener; -import android.widget.ExpandableListView; -import android.widget.AdapterView.AdapterContextMenuInfo; - -public class PositionTesterContextMenuListener implements OnCreateContextMenuListener { - - private int groupPosition, childPosition; - - // Fake constant to store in testType a test type specific to headers and footers - private static final int ADAPTER_TYPE = -1; - private int testType; // as returned by getPackedPositionType - - // Will be set to null by each call to onCreateContextMenu, unless an error occurred. - private String errorMessage; - - public void expectGroupContextMenu(int groupPosition) { - this.groupPosition = groupPosition; - testType = ExpandableListView.PACKED_POSITION_TYPE_GROUP; - } - - public void expectChildContextMenu(int groupPosition, int childPosition) { - this.groupPosition = groupPosition; - this.childPosition = childPosition; - testType = ExpandableListView.PACKED_POSITION_TYPE_CHILD; - } - - public void expectAdapterContextMenu(int flatPosition) { - this.groupPosition = flatPosition; - testType = ADAPTER_TYPE; - } - - public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { - errorMessage = null; - if (testType == ADAPTER_TYPE) { - if (!isTrue("MenuInfo is not an AdapterContextMenuInfo", - menuInfo instanceof AdapterContextMenuInfo)) { - return; - } - AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo; - if (!areEqual("Wrong flat position", groupPosition, adapterContextMenuInfo.position)) { - return; - } - } else { - if (!isTrue("MenuInfo is not an ExpandableListContextMenuInfo", - menuInfo instanceof ExpandableListView.ExpandableListContextMenuInfo)) { - return; - } - ExpandableListView.ExpandableListContextMenuInfo elvMenuInfo = - (ExpandableListView.ExpandableListContextMenuInfo) menuInfo; - long packedPosition = elvMenuInfo.packedPosition; - - int packedPositionType = ExpandableListView.getPackedPositionType(packedPosition); - if (!areEqual("Wrong packed position type", testType, packedPositionType)) { - return; - } - - int packedPositionGroup = ExpandableListView.getPackedPositionGroup(packedPosition); - if (!areEqual("Wrong group position", groupPosition, packedPositionGroup)) { - return; - } - - if (testType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { - int packedPositionChild = ExpandableListView.getPackedPositionChild(packedPosition); - if (!areEqual("Wrong child position", childPosition, packedPositionChild)) { - return; - } - } - } - } - - private boolean areEqual(String message, int expected, int actual) { - if (expected != actual) { - errorMessage = String.format(message + " (%d vs %d", expected, actual); - return false; - } - return true; - } - - private boolean isTrue(String message, boolean value) { - if (!value) { - errorMessage = message; - return false; - } - return true; - } - - public String getErrorMessage() { - return errorMessage; - } -} diff --git a/docs/html/guide/guide_toc.cs b/docs/html/guide/guide_toc.cs index dce78c4..fd163f6 100644 --- a/docs/html/guide/guide_toc.cs +++ b/docs/html/guide/guide_toc.cs @@ -99,18 +99,36 @@ </li> <li class="toggle-list"> <div><a href="<?cs var:toroot ?>guide/topics/resources/index.html"> - <span class="en">Resources and Assets</span> + <span class="en">Application Resources</span> </a></div> <ul> - <li><a href="<?cs var:toroot ?>guide/topics/resources/resources-i18n.html"> - <span class="en">Resources and I18n</span> + <li><a href="<?cs var:toroot ?>guide/topics/resources/providing-resources.html"> + <span class="en">Providing Resources</span> </a></li> - <li><a href="<?cs var:toroot ?>guide/topics/resources/available-resources.html"> - <span class="en">Available Resource Types</span> + <li><a href="<?cs var:toroot ?>guide/topics/resources/accessing-resources.html"> + <span class="en">Accessing Resources</span> + </a></li> + <li><a href="<?cs var:toroot ?>guide/topics/resources/runtime-changes.html"> + <span class="en">Handling Runtime Changes</span> </a></li> <li><a href="<?cs var:toroot ?>guide/topics/resources/localization.html"> <span class="en">Localization</span> </a></li> + <li class="toggle-list"> + <div><a href="<?cs var:toroot ?>guide/topics/resources/available-resources.html"> + <span class="en">Resource Types</span> + </a></div> + <ul> + <li><a href="<?cs var:toroot ?>guide/topics/resources/animation-resource.html">Animation</a></li> + <li><a href="<?cs var:toroot ?>guide/topics/resources/color-list-resource.html">Color State List</a></li> + <li><a href="<?cs var:toroot ?>guide/topics/resources/drawable-resource.html">Drawable</a></li> + <li><a href="<?cs var:toroot ?>guide/topics/resources/layout-resource.html">Layout</a></li> + <li><a href="<?cs var:toroot ?>guide/topics/resources/menu-resource.html">Menu</a></li> + <li><a href="<?cs var:toroot ?>guide/topics/resources/string-resource.html">String</a></li> + <li><a href="<?cs var:toroot ?>guide/topics/resources/style-resource.html">Style</a></li> + <li><a href="<?cs var:toroot ?>guide/topics/resources/more-resources.html">More Types</a></li> + </ul> + </li> </ul> </li> <li><a href="<?cs var:toroot ?>guide/topics/intents/intents-filters.html"> diff --git a/docs/html/guide/topics/graphics/2d-graphics.jd b/docs/html/guide/topics/graphics/2d-graphics.jd index 051427b..e46dbb4 100644 --- a/docs/html/guide/topics/graphics/2d-graphics.jd +++ b/docs/html/guide/topics/graphics/2d-graphics.jd @@ -442,7 +442,7 @@ class is the basis for frame animations.</p> <p>While you can define the frames of an animation in your code, using the {@link android.graphics.drawable.AnimationDrawable} class API, it's more simply accomplished with a single XML file that lists the frames that compose the animation. Like the tween animation above, the XML file for this kind -of animation belongs in the <code>res/anim/</code> directory of your Android project. In this case, +of animation belongs in the <code>res/drawable/</code> directory of your Android project. In this case, the instructions are the order and duration for each frame of the animation.</p> <p>The XML file consists of an <code><animation-list></code> element as the root node and a series @@ -459,7 +459,7 @@ Here's an example XML file for a frame-by-frame animation:</p> <p>This animation runs for just three frames. By setting the <code>android:oneshot</code> attribute of the list to <var>true</var>, it will cycle just once then stop and hold on the last frame. If it is set <var>false</var> then -the animation will loop. With this XML saved as <code>rocket_thrust.xml</code> in the <code>res/anim/</code> directory +the animation will loop. With this XML saved as <code>rocket_thrust.xml</code> in the <code>res/drawable/</code> directory of the project, it can be added as the background image to a View and then called to play. Here's an example Activity, in which the animation is added to an {@link android.widget.ImageView} and then animated when the screen is touched:</p> <pre> @@ -470,7 +470,7 @@ public void onCreate(Bundle savedInstanceState) { setContentView(R.layout.main); ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image); - rocketImage.setBackgroundResource(R.anim.rocket_thrust); + rocketImage.setBackgroundResource(R.drawable.rocket_thrust); rocketAnimation = (AnimationDrawable) rocketImage.getBackground(); } diff --git a/docs/html/guide/topics/resources/accessing-resources.jd b/docs/html/guide/topics/resources/accessing-resources.jd new file mode 100644 index 0000000..e3e4055 --- /dev/null +++ b/docs/html/guide/topics/resources/accessing-resources.jd @@ -0,0 +1,284 @@ +page.title=Accessing Resources +parent.title=Application Resources +parent.link=index.html +@jd:body + +<div id="qv-wrapper"> +<div id="qv"> + <h2>Quickview</h2> + <ul> + <li>Resources can be referenced from code using integers from {@code R.java}, such as +{@code R.drawable.myimage}</li> + <li>Resources can be referenced from resources using a special XML syntax, such as {@code +@drawable/myimage}</li> + <li>You can also access your app resources with methods in +{@link android.content.res.Resources}</li> + </ul> + + <h2>Key classes</h2> + <ol> + <li>{@link android.content.res.Resources}</li> + </ol> + + <h2>In this document</h2> + <ol> + <li><a href="#ResourcesInCode">Accessing Resources in Code</a></li> + <li><a href="#ReferencesToResources">Accessing Resources in Other XML Resources</a> + <ol> + <li><a href="#ReferencesToThemeAttributes">Referencing style attributes</a></li> + </ol> + </li> + </ol> + + <h2>See also</h2> + <ol> + <li><a href="providing-resources.html">Providing Resources</a></li> + <li><a href="available-resources.html">Resource Types</a></li> + </ol> +</div> +</div> + + +<p>There are two ways you can reference your resources for use in your application:</p> +<ul> + <li><strong>From your code:</strong> Using an integer from a sub-class in your {@code R} class, +such as: + <p>{@code R.string.hello}</p> + <p>You will see Android APIs that accept this kind of resource identifier as a method parameter +(usually defined as the {@code id} parameter).</p> + </li> + <li><strong>From another resource:</strong> Using a special XML syntax that corresponds to the +{@code R} sub-class, such as: + <p>{@code @string/hello}</p> + <p>You can use this syntax in an XML resource any place where a value is expected that is +matched by the existing resource. For example, if you need to provide a string in either an XML +attribute or element value, you can reference a resource instead of providing a hard-coded +string.</p> + </li> +</ul> + + + +<h2 id="ResourcesInCode">Accessing Resources in Code </h2> + +<p>When your application is compiled, Android generates the {@code R.java} file (inside +the {@code gen/} directory), which contains resource +identifiers to all the resources in your {@code res/} directory. For each type of resource, a +specific subclass is added to the {@code R} class (for example, +{@code R.drawable}) and for each resource of that type, a static +integer is added to the subclass (for example, +{@code R.drawable.icon}). This integer is the resource ID and you can use it to retrieve +your resource from your application code.</p> + + +<div class="sidebox-wrapper"> +<div class="sidebox"> +<h2>Access to Original Files</h2> + +<p>While uncommon, you might need access your original files and directories. If you do, then +saving your files in {@code res/} won't work for you. Instead, you can save your resources in the +{@code assets/} directory.</p> +<p>Files saved in the {@code assets/} directory will <em>not</em> be given a resource +ID, so you can't reference them through the {@code R} class or from XML resources. Instead, you can +query files in the {@code assets/} directory like a normal file system and read raw data using +{@link android.content.res.AssetManager}.</p> +<p>However, if all you require is the ability to read raw data (such as a video or audio file), +then save the file in the {@code res/raw/} directory and read a stream of bytes using {@link +android.content.res.Resources#openRawResource(int)}.</p> + +</div> +</div> + + +<p class="caution"><strong>Caution:</strong> You should never modify the {@code +R.java} file by hand—it is generated by the {@code aapt} tool when your project is +compiled. Any changes will be overridden next time you compile.</p> + +<p>Here is the syntax to reference a resource in code:</p> +<p> +<code>[<em><package_name></em>.]R.<em><resource_type></em>.<em><resource_name></em></code> +</p> + +<ul> + <li><em>{@code <package_name>}</em> is the name of the package in which the resource is located (not +required when referencing resources from your own package).</li> + <li><em>{@code <resource_type>}</em> is the {@code R} subclass for the resource type.</li> + <li><em>{@code <resource_name>}</em> is either the {@code +android:name} attribute value (for some resources defined in XML files) or the resource filename +without the extension.</li> +</ul> +<p>See <a href="resource-types.html">Resource Types</a> for +more information about each resource type and how to reference them.</p> + +<p>In many cases, you can supply an API method with the resource ID. For example, to set the image +for an {@link android.widget.ImageView}:</p> +<pre> +ImageView iv = (ImageView) findViewById(R.id.myimageview); +iv.setImageResource(R.drawable.myimage); +</pre> + +<p>You can also retrieve your +resource objects using methods in {@link android.content.res.Resources}, which you can create an +instance of with {@link android.content.Context#getResources Context.getResources()}. For example, +to get a string:</p> +<pre> +Resources res = this.getResources(); +String string = res.getString(R.string.mystring); +</pre> + +<p>You can also access resources from the platform by prefixing the {@code android} +namespace. Android contains a number of standard resources, such as styles and themes for your +layout, button backgrounds, and layouts. To refer to these in code, qualify your reference with the +<code>android</code> package name. For example, +<code>android.R.layout.simple_gallery_item</code>.</p> + +<p>Here are some examples of using resources in code:</p> + +<pre> +// Load a background for the current screen from a drawable resource +{@link android.app.Activity#getWindow()}.{@link +android.view.Window#setBackgroundDrawableResource(int) +setBackgroundDrawableResource}(R.drawable.my_background_image) ; + +// Set the Activity title by getting a string from the Resources object, because +// this method requires a CharSequence rather than a resource ID +{@link android.app.Activity#getWindow()}.{@link android.view.Window#setTitle(CharSequence) +setTitle}(getResources().{@link android.content.res.Resources#getText(int) +getText}(R.string.main_title)); + +// Load a custom layout for the current screen +{@link android.app.Activity#setContentView(int) +setContentView}(R.layout.main_screen); + +// Set a slide in animation by getting an Animation from the Resources object +mFlipper.{@link android.widget.ViewAnimator#setInAnimation(Animation) +setInAnimation}(AnimationUtils.loadAnimation(this, + R.anim.hyperspace_in)); + +// Set the text on a TextView object using a resource ID +TextView msgTextView = (TextView) findViewById(R.id.msg); +msgTextView.{@link android.widget.TextView#setText(int) setText}(R.string.hello_message); +</pre> + + + + + + +<h2 id="ReferencesToResources">Accessing Resources in other XML Resources</h2> + +<p>When creating an XML resource, some values for attributes and elements can be a reference to +an existing resource. This is often used in layout files to supply strings and images.</p> + +<p>Here is the syntax to reference a resource in an XML resource:</p> +<p><code>@[<em><package_name></em>:]<em><resource_type></em>/<em><resource_name></em></code></p> + +<ul> + <li>{@code <package_name>} is the name of the package in which the resource is located (not +required when referencing resources from the same package)</li> + <li>{@code <resource_type>} is the +{@code R} subclass for the resource type</li> + <li>{@code <resource_name>} is either the {@code +android:name} attribute value (for some resources defined in XML files) or the resource filename +without the extension</li> +</ul> + +<p>See <a href="resource-types.html">Resource Types</a> for +more information about each resource type and how to reference them.</p> + +<p>For example, if you have the following resource file that includes a <a +href="more-resources.html#Color">color resource</a> and a <a +href="string-resource.html">string resource</a>:</p> + +<pre> +<?xml version="1.0" encoding="utf-8"?> +<resources> + <color name="opaque_red">#f00</color> + <string name="hello">Hello!</string> +</resources> +</pre> + +<p>You can use these resources in the following layout file to set the text color and +text string:</p> + +<pre> +<?xml version="1.0" encoding="utf-8"?> +<EditText xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + <strong>android:textColor="@color/opaque_red" + android:text="@string/hello"</strong> /> +</pre> + +<p>In this case you don't need to specify the package name in the resource reference because the +resources are from your own package. To +reference a system resource, you would need to include the package name. For example:</p> + +<pre> +<?xml version="1.0" encoding="utf-8"?> +<EditText xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + <strong>android:textColor="@android:color/secondary_text_dark"</strong> + android:text="@string/hello" /> +</pre> + +<p class="note"><strong>Note:</strong> You should always use a string resource when supplying +strings in a layout file, as demonstrated above, so that the strings can be localized. For +information about creating alternative resources (such as localized strings), see <a +href="providing-resources.html#AlternativeResources">Providing Alternative +Resources</a>.</p> + +<p>This facility for referencing resources between resources can also be used to create +alias resources. For example, you can create new drawable resources that is an alias for an existing +image:</p> + +<pre> +<?xml version="1.0" encoding="utf-8"?> +<bitmap xmlns:android="http://schemas.android.com/apk/res/android" + android:src="@drawable/other_drawable" /> +</pre> + +<p>This is discussed further in <a href="providing-resources.html#AliasResources">Creating +alias resources</a>.</p> + + + + +<h3 id="ReferencesToThemeAttributes">Referencing style attributes</h3> + +<p>A style attribute resource is another type of resource that allows you to reference the value +of an attribute in the currently-applied theme. Referencing a style attribute allows you to +customize the look of UI elements by styling them to match standard variations supplied by the +current theme, instead of supplying a hard-coded value. Referencing a style attribute +essentially says, "use the style that is defined by this attribute, in the current theme."</p> + +<p>To reference a style attribute, the name syntax is almost identical to the normal resource +format, but instead of the at-symbol ({@code @}), use a question-mark ({@code ?}), and the +resource type portion is optional. For instance:</p> + +<p> +<code> +?[<em><package_name></em>:][<em><resource_type></em>/]<em><resource_name></em> +</code> +</p> + +<p>For example, here's how you might reference an attribute in a layout, +to set the text color to match the "primary" text color of the system theme:</p> + +<pre> +<EditText id="text" + android:layout_width="fill_parent" + android:layout_height="wrap_content" + <strong>android:textColor="?android:textColorSecondary"</strong> + android:text="@string/hello_world" /> +</pre> + +<p>Using this markup, you are +supplying the name of an attribute resource that will be looked up in the theme. +Because the system resource tool knows that an attribute resource is expected, +you do not need to explicitly state the type (which would be +<code>?android:attr/textColorSecondary</code>), so you can exclude the {@code attr} type.</p> + + + diff --git a/docs/html/guide/topics/resources/animation-resource.jd b/docs/html/guide/topics/resources/animation-resource.jd new file mode 100644 index 0000000..b2fab04 --- /dev/null +++ b/docs/html/guide/topics/resources/animation-resource.jd @@ -0,0 +1,564 @@ +page.title=Animation Resources +parent.title=Resource Types +parent.link=available-resources.html +@jd:body + +<div id="qv-wrapper"> + <div id="qv"> + <h2>See also</h2> + <ol> + <li><a href="{@docRoot}guide/topics/graphics/2d-graphics.html#tween-animation">2D +Graphics</a></li> + </ol> + </div> +</div> + + +<p>An animation resource can define one of two types of animations:</p> +<dl> + <dt><a href="#Tween">Tween Animation</a></dt> + <dd>Creates an animation by performing a series of transformations on a single image. + An {@link android.view.animation.Animation}.</dd> + <dt><a href="#Frame">Frame Animation</a></dt> + <dd>Creates an animation by showing a sequence of images in order. + An {@link android.graphics.drawable.AnimationDrawable}.</dd> +</dl> + + + +<h2 id="Tween">Tween Animation</h2> + +<p>An animation defined in XML that performs transitions such as rotating, +fading, moving, and stretching on a graphic. +</p> + +<dl class="xml"> + +<dt>file location:</dt> +<dd><code>res/anim/<em>filename</em>.xml</code><br/> +The filename will be used as the resource ID.</dd> + +<dt>compiled resource datatype:</dt> +<dd>Resource pointer to an {@link android.view.animation.Animation}.</dd> + +<dt>resource reference:</dt> +<dd> +In Java: <code>R.anim.<em>filename</em></code><br/> +In XML: <code>@[<em>package</em>:]anim/<em>filename</em></code> +</dd> + +<dt>syntax:</dt> +<dd> +<pre class="stx"> +<?xml version="1.0" encoding="utf-8"?> +<<a href="#set-element">set</a> xmlns:android="http://schemas.android.com/apk/res/android" + android:interpolator="@[package:]anim/<em>interpolator_resource</em>" + android:shareInterpolator=["true" | "false"] > + <<a href="#alpha-element">alpha</a> + android:fromAlpha="<em>float</em>" + android:toAlpha="<em>float</em>" /> + <<a href="#scale-element">scale</a> + android:fromXScale="<em>float</em>" + android:toXScale="<em>float</em>" + android:fromYScale="<em>float</em>" + android:toYScale="<em>float</em>" + android:pivotX="<em>string</em>" + android:pivotY="<em>string</em>" /> + <<a href="#translate-element">translate</a> + android:fromX="<em>string</em>" + android:toX="<em>string</em>" + android:fromY="<em>string</em>" + android:toY="<em>string</em>" /> + <<a href="#rotate-element">rotate</a> + android:fromDegrees="<em>float</em>" + android:toDegrees="<em>float</em>" + android:pivotX="<em>string</em>" + android:pivotY="<em>string</em>" /> + <<a href="#set-element">set</a>> + ... + </set> +</set> +</pre> + +<p>The file must have a single root element: either an +<code><alpha></code>, <code><scale></code>, <code><translate></code>, +<code><rotate></code>, or <code><set></code> element that holds +a group (or groups) of other animation elements (even nested <code><set></code> elements). +</p> +</dd> + +<dt>elements:</dt> +<dd> +<dl class="tag-list"> + <dt id="set-element"><code><set></code></dt> + <dd>A container that holds other animation elements +(<code><alpha></code>, <code><scale></code>, <code><translate></code>, +<code><rotate></code>) or other <code><set></code> elements. Represents an {@link +android.view.animation.AnimationSet}. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>android:interpolator</code></dt> + <dd><em>Interpolator resource</em>. + An {@link android.view.animation.Interpolator} to apply on the animation. + The value must be a reference to a resource that specifies an interpolator + (not an interpolator class name). There are default interpolator + resources available from the platform or you can create your own interpolator resource. + See the discussion below for more about <a href="#Interpolators">Interpolators</a>.</dd> + <dt><code>android:shareInterpolator</code></dt> + <dd><em>Boolean</em>. "true" if you want to share the same interpolator among all child +elements.</dd> + </dl> + </dd> + <dt id="alpha-element"><code><alpha></code></dt> + <dd>A fade-in or fade-out animation. Represents an {@link +android.view.animation.AlphaAnimation}. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>android:fromAlpha</code></dt> + <dd><em>Float</em>. Starting opacity offset, where 0.0 is transparent and 1.0 +is opaque.</dd> + <dt><code>android:toAlpha</code></dt> + <dd><em>Float</em>. Ending opacity offset, where 0.0 is transparent and 1.0 +is opaque.</dd> + </dl> + <p>For more attributes supported by <code><alpha></code>, see the +{@link android.view.animation.Animation} class reference (of which, all XML attributes are +inherrited by this element).</p> + </dd> + <dt id="scale-element"><code><scale></code></dt> + <dd>A resizing animation. You can specify the center point of the image from which it grows +outward (or inward) by specifying {@code pivotX} and {@code pivotY}. For example, if these values +are 0, 0 (top-left corner), all growth will be down and to the right. Represents a {@link +android.view.animation.ScaleAnimation}. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>android:fromXScale</code></dt> + <dd><em>Float</em>. Starting X size offset, where 1.0 is no change.</dd> + <dt><code>android:toXScale</code></dt> + <dd><em>Float</em>. Ending X size offset, where 1.0 is no change.</dd> + <dt><code>android:fromYScale</code></dt> + <dd><em>Float</em>. Starting Y size offset, where 1.0 is no change.</dd> + <dt><code>android:toYScale</code></dt> + <dd><em>Float</em>. Ending Y size offset, where 1.0 is no change.</dd> + <dt><code>android:pivotX</code></dt> + <dd><em>Float</em>. The X coordinate to remain fixed when the object is scaled.</dd> + <dt><code>android:pivotY</code></dt> + <dd><em>Float</em>. The Y coordinate to remain fixed when the object is scaled.</dd> + </dl> + <p>For more attributes supported by <code><scale></code>, see the +{@link android.view.animation.Animation} class reference (of which, all XML attributes are +inherrited by this element).</p> + </dd> + <dt id="translate-element"><code><translate></code></dt> + <dd>A vertical and/or horizontal motion. Supports the following attributes in any of +the following three formats: values from -100 to 100 ending with "%", indicating a percentage +relative to itself; values from -100 to 100 ending in "%p", indicating a percentage relative to its +parent; a float value with no suffix, indicating an absolute value. Represents a {@link +android.view.animation.TranslateAnimation}. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>android:fromXDelta</code></dt> + <dd><em>Float or percentage</em>. Starting X offset. Either in: pixels relative to the +normal position, in percentage relative to the element width (%), or relative to the parent width +(%p).</dd> + <dt><code>android:toXDelta</code></dt> + <dd><em>Float or percentage</em>. Ending X offset. Either in: pixels relative to the +normal position, in percentage relative to the element width (%), or relative to the parent width +(%p).</dd> + <dt><code>android:fromYDelta</code></dt> + <dd><em>Float or percentage</em>. Starting Y offset. Either in: pixels relative to the +normal position, in percentage relative to the element height (%), or relative to the parent height +(%p).</dd> + <dt><code>android:toYDelta</code></dt> + <dd><em>Float or percentage</em>. Ending Y offset. Either in: pixels relative to the +normal position, in percentage relative to the element height (%), or relative to the parent height +(%p).</dd> + </dl> + <p>For more attributes supported by <code><translate></code>, see the +{@link android.view.animation.Animation} class reference (of which, all XML attributes are +inherrited by this element).</p> + </dd> + <dt id="rotate-element"><code><rotate></code></dt> + <dd>A rotation animation. Represents a {@link android.view.animation.RotateAnimation}. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>android:fromDegrees</code></dt> + <dd><em>Integer</em>. Starting angular position, in degrees.</dd> + <dt><code>android:toDegrees</code></dt> + <dd><em>Integer</em>. Ending angular position, in degrees.</dd> + <dt><code>android:pivotX</code></dt> + <dd><em>Integer or percentage</em>. The X coordinate of the center of rotation, in total +pixels (where 0 is the left edge) or percentage of the screen width.</dd> + <dt><code>android:pivotY</code></dt> + <dd><em>Integer or percentage</em>. The Y coordinate of the center of rotation, in total +pixels (where 0 is the top edge) or percentage of the screen height.</dd> + </dl> + <p>For more attributes supported by <code><rotate></code>, see the +{@link android.view.animation.Animation} class reference (of which, all XML attributes are +inherrited by this element).</p> + </dd> +</dl> +</dd> <!-- end elements and attributes --> + +<dt>example:</dt> +<dd> + <pp>XML file saved at <code>res/anim/hyperspace_jump.xml</code>:</p> +<pre> +<set xmlns:android="http://schemas.android.com/apk/res/android" + android:shareInterpolator="false"> + <scale + android:interpolator="@android:anim/accelerate_decelerate_interpolator" + android:fromXScale="1.0" + android:toXScale="1.4" + android:fromYScale="1.0" + android:toYScale="0.6" + android:pivotX="50%" + android:pivotY="50%" + android:fillAfter="false" + android:duration="700" /> + <set + android:interpolator="@android:anim/accelerate_interpolator" + android:startOffset="700"> + <scale + android:fromXScale="1.4" + android:toXScale="0.0" + android:fromYScale="0.6" + android:toYScale="0.0" + android:pivotX="50%" + android:pivotY="50%" + android:duration="400" /> + <rotate + android:fromDegrees="0" + android:toDegrees="-45" + android:toYScale="0.0" + android:pivotX="50%" + android:pivotY="50%" + android:duration="400" /> + </set> +</set> +</pre> + <p>This application code will apply the animation to an {@link android.widget.ImageView} and +start the animation:</p> +<pre> +ImageView image = (ImageView) findViewById(R.id.image); +Animation hyperspaceJump = AnimationUtils.{@link android.view.animation.AnimationUtils#loadAnimation(Context,int) loadAnimation}(this, R.anim.hyperspace_jump); +image.{@link android.view.View#startAnimation(Animation) startAnimation}(hyperspaceJump); +</pre> +</dd> <!-- end example --> + + +<dt>see also:</dt> +<dd> +<ul> + <li><a href="{@docRoot}guide/topics/graphics/2d-graphics.html#tween-animation">2D +Graphics: Tween Animation</a></li> +</ul> +</dd> + +</dl> + + + + + +<h3 id="Interpolators">Interpolators</h3> + +<p>An interpolator is an animation modifier defined in XML that affects the rate of change in an +animation. This allows your existing animation effects to be accelerated, decelerated, repeated, +bounced, etc.</p> + +<p>An interpolator is applied to an animation element with the {@code android:interpolator} +attribute, the value of which is a reference to an interpolator resource.</p> + +<p>All interpolators available in Android are subclasses of the {@link +android.view.animation.Interpolator} class. For each interpolator class, Android +includes a public resource you can reference in order to apply the interpolator to an animation +using the the {@code android:interpolator} attribute. +The following table specifies the resource to use for each interpolator:</p> + +<table> + <tr><th>Interpolator class</th><th>Resource ID</th></tr> + <tr> + <td>{@link android.view.animation.AccelerateDecelerateInterpolator}</td> + <td>{@code @android:anim/accelerate_decelerate_interpolator}</td> + </tr> + <tr> + <td>{@link android.view.animation.AccelerateInterpolator}</td> + <td>{@code @android:anim/accelerate_interpolator}</td> + </tr> + <tr> + <td>{@link android.view.animation.AnticipateInterpolator}</td> + <td>{@code @android:anim/anticipate_interpolator}</td> + </tr> + <tr> + <td>{@link android.view.animation.AnticipateOvershootInterpolator}</td> + <td>{@code @android:anim/anticipate_overshoot_interpolator}</td> + </tr> + <tr> + <td>{@link android.view.animation.BounceInterpolator}</td> + <td>{@code @android:anim/bounce_interpolator}</td> + </tr> + <tr> + <td>{@link android.view.animation.CycleInterpolator}</td> + <td>{@code @android:anim/cycle_interpolator}</td> + </tr> + <tr> + <td>{@link android.view.animation.DecelerateInterpolator}</td> + <td>{@code @android:anim/decelerate_interpolator}</td> + </tr> + <tr> + <td>{@link android.view.animation.LinearInterpolator}</td> + <td>{@code @android:anim/linear_interpolator}</td> + </tr> + <tr> + <td>{@link android.view.animation.OvershootInterpolator}</td> + <td>{@code @android:anim/overshoot_interpolator}</td> + </tr> +</table> + +<p>Here's how you can apply one of these with the {@code android:interpolator} attribute:</p> +<pre> +<set android:interpolator="@android:anim/accelerate_interpolator"> + ... +</set> +</pre> + + +<h4>Custom interpolators</h4> + +<p>If you're not satisfied with the interpolators provided by the platform (listed in the +table above), you can create a custom interpolator resource with modified attributes. +For example, you can adjust the rate of +acceleration for the {@link android.view.animation.AnticipateInterpolator}, or adjust the number of +cycles for the {@link android.view.animation.CycleInterpolator}. In order to do so, you need to +create your own interpolator resource in an XML file. +</p> + +<dl class="xml"> + +<dt>file location:</dt> +<dd><code>res/anim/<em>filename</em>.xml</code><br/> +The filename will be used as the resource ID.</dd> + +<dt>compiled resource datatype:</dt> +<dd>Resource pointer to the corresponding interpolator object.</dd> + +<dt>resource reference:</dt> +<dd> +In XML: <code>@[<em>package</em>:]anim/<em>filename</em></code> +</dd> + +<dt>syntax:</dt> +<dd> +<pre class="stx"> +<?xml version="1.0" encoding="utf-8"?> +<<em>InterpolatorName</em> xmlns:android="http://schemas.android.com/apk/res/android" + android:<em>attribute_name</em>="<em>value</em>" + /> +</pre> +<p>If you don't apply any attributes, then your interpolator will function exactly the same as +those provided by the platform (listed in the table above).</p> +</dd> + +<dt>elements:</dt> +<dd>Notice that each {@link android.view.animation.Interpolator} implementation, when +defined in XML, begins its name in lowercase.</p> + +<dl class="tag-list"> + <dt><code><accelerateDecelerateInterpolator></code></dt> + <dd>The rate of change starts and ends slowly but accelerates through the +middle. <p>No attributes.</p></dd> + <dt><code><accelerateInterpolator></code></dt> + <dd>The rate of change starts out slowly, then accelerates. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>android:factor</code></dt> + <dd><em>Float</em>. The acceleration rate (default is 1).</dd> + </dl> + </dd> + <dt><code><anticipateInterpolator></code></dt> + <dd>The change starts backward then flings forward. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>android:tension</code></dt> + <dd><em>Float</em>. The amount of tension to apply (default is 2).</dd> + </dl> + </dd> + <dt><code><anticipateOvershootInterpolator></code></dt> + <dd>The change starts backward, flings forward and overshoots the target value, then +settles at the final value. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>android:tension</code></dt> + <dd><em>Float</em>. The amount of tension to apply (default is 2).</dd> + <dt><code>android:extraTension</code></dt> + <dd><em>Float</em>. The amount by which to multiply the tension (default is + 1.5).</dd> + </dl> + </dd> + <dt><code><bounceInterpolator></code></dt> + <dd>The change bounces at the end. <p>No attributes</p></dd> + <dt><code><cycleInterpolator></code></dt> + <dd>Repeats the animation for a specified number of cycles. The rate of change follows a +sinusoidal pattern. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>android:cycles</code></dt> + <dd><em>Integer</em>. The number of cycles (default is 1).</dd> + </dl> + </dd> + <dt><code><decelerateInterpolator></code></dt> + <dd>The rate of change starts out quickly, then decelerates. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>android:factor</code></dt> + <dd><em>Float</em>. The deceleration rate (default is 1).</dd> + </dl> + </dd> + <dt><code><linearInterpolator></code></dt> + <dd>The rate of change is constant. <p>No attributes.</p></dd> + <dt><code><overshootInterpolator></code></dt> + <dd>The change flings forward and overshoots the last value, then comes back. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>android:tension</code></dt> + <dd><em>Float</em>. The amount of tension to apply (default is 2).</dd> + </dl> + </dd> +</dl> + +<dt>example:</dt> +<dd> + <p>XML file saved at <code>res/anim/my_overshoot_interpolator.xml</code>:</p> +<pre> +<?xml version="1.0" encoding="utf-8"?> +<overshootInterpolator xmlns:android="http://schemas.android.com/apk/res/android" + android:tension="7.0" + /> +</pre> + <p>This animation XML will apply the interpolator:</p> +<pre> +<scale xmlns:android="http://schemas.android.com/apk/res/android" + android:interpolator="@anim/my_overshoot_interpolator" + android:fromXScale="1.0" + android:toXScale="3.0" + android:fromYScale="1.0" + android:toYScale="3.0" + android:pivotX="50%" + android:pivotY="50%" + android:duration="700" /> +</pre> +</dd> +</dl> + + + + + + + + + + + + + + + + + +<h2 id="Frame">Frame Animation</h2> + +<p>An animation defined in XML that shows a sequence of images in order (like a film). +</p> + + +<dl class="xml"> + +<dt>file location:</dt> +<dd><code>res/drawable/<em>filename</em>.xml</code><br/> +The filename will be used as the resource ID.</dd> + +<dt>compiled resource datatype:</dt> +<dd>Resource pointer to an {@link android.graphics.drawable.AnimationDrawable}.</dd> + +<dt>resource reference:</dt> +<dd> +In Java: <code>R.drawable.<em>filename</em></code><br/> +In XML: <code>@[<em>package</em>:]drawable.<em>filename</em></code> +</dd> + +<dt>syntax:</dt> +<dd> +<pre class="stx"> +<?xml version="1.0" encoding="utf-8"?> +<<a href="#animation-list-element">animation-list</a> xmlns:android="http://schemas.android.com/apk/res/android" + android:oneshot=["true" | "false"] > + <<a href="#item-element">item</a> + android:drawable="@[package:]drawable/<em>drawable_resource_name</em>" + android:duration="<em>integer</em>" /> +</animation-list> +</pre> +</dd> + +<dt>elements:</dt> +<dd> +<dl class="tag-list"> +<dt id="animation-list-element"><code><animation-list></code></dt> + <dd><strong>Required</strong>. This must be the root element. Contains one or more +<code><item></code> elements. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>android:oneshot</code></dt> + <dd><em>Boolean</em>. "true" if you want to perform the animation once; "false" to loop the +animation.</dd> + </dl> + </dd> +<dt id="item-element"><code><item></code></dt> + <dd>A single frame of animation. Must be a child of a <code><animation-list></code> element. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>android:drawable</code></dt> + <dd><em>Drawable resource</em>. The drawable to use for this frame.</dd> + <dt><code>android:duration</code></dt> + <dd><em>Integer</em>. The duration to show this frame, in milliseconds.</dd> + </dl> + </dd> +</dl> +</dd> <!-- end elements and attributes --> + +<dt>example:</dt> +<dd> + <dl> + <dt>XML file saved at <code>res/anim/rocket.xml</code>:</dt> + <dd> +<pre> +<?xml version="1.0" encoding="utf-8"?> +<animation-list xmlns:android="http://schemas.android.com/apk/res/android" + android:oneshot="false"> + <item android:drawable="@drawable/rocket_thrust1" android:duration="200" /> + <item android:drawable="@drawable/rocket_thrust2" android:duration="200" /> + <item android:drawable="@drawable/rocket_thrust3" android:duration="200" /> +</animation-list> +</pre> + </dd> + + <dt>This application code will set the animation as the background for a View, + then play the animation:</dt> + <dd> +<pre> +ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image); +rocketImage.{@link android.view.View#setBackgroundResource(int) setBackgroundResource}(R.drawable.rocket_thrust); + +rocketAnimation = (AnimationDrawable) rocketImage.{@link android.view.View#getBackground()}; +rocketAnimation.{@link android.graphics.drawable.AnimationDrawable#start()}; +</pre> + </dd> + + </dl> +</dd> <!-- end example --> + +</dl> + + + diff --git a/docs/html/guide/topics/resources/available-resources.jd b/docs/html/guide/topics/resources/available-resources.jd index 0e003a0..19babee 100644 --- a/docs/html/guide/topics/resources/available-resources.jd +++ b/docs/html/guide/topics/resources/available-resources.jd @@ -1,1451 +1,59 @@ -page.title=Available Resource Types -parent.title=Resources and Assets +page.title=Resource Types +parent.title=Application Resources parent.link=index.html @jd:body <div id="qv-wrapper"> <div id="qv"> - - <h2>Key classes</h2> - <ol> - <li>{@link android.content.res.Resources}</li> - <li>{@link android.content.res.AssetManager}</li> - </ol> - - <h2>In this document</h2> + <h2>See also</h2> <ol> - <li><a href="#simplevalues">Simple Values</a> - <ol> - <li><a href="#colorvals">Color Values</a></li> - <li><a href="#stringresources">Strings and Styled Text</a></li> - <li><a href="#dimension">Dimension Values</a></li> - </ol> - </li> - <li><a href="#drawables">Drawables</a> - <ol> - <li><a href="#imagefileresources">Bitmap Files</a></li> - <li><a href="#colordrawableresources">Color Drawables</a></li> - <li><a href="#ninepatch">Nine-Patch (Stretchable) Images</a></li> - </ol> - </li> - <li><a href="#animation">Animation</a></li> - <li><a href="#menus">Menus</a></li> - <li><a href="#layoutresources">Layout</a> - <ol> - <li><a href="#customresources">Custom Layout Resources</a> - </ol> - </li> - <li><a href="#stylesandthemes">Styles and Themes</a></li> - <li><a href="#Searchable">Searchable</a></li> + <li><a href="providing-resources.html">Providing Resources</a></li> + <li><a href="accessing-resources.html">Accessing Resources</a></li> </ol> - </div> </div> -<p>This page describes the different types of resources that you can -externalize from your code and package with your application. </p> - - -<p>For more details on how to use resources in your application, please see the - <a href="resources-i18n.html">Resources and Internationalization</a> - documentation.</p> - - -<h2 id="simplevalues">Simple Values</h2> - -<p>All simple resource values can be expressed as a string, using various -formats to unambiguously indicate the type of resource being created. For -this reason, these values can be defined both as standard resources -(under res/values/), as well as direct values supplied for -mappings in <a href="#stylesandthemes">styles and themes</a>, and attributes in -XML files such as <a href="#layoutresources">layouts</a>.</p> - - - -<h3 id="colorvals">Color Values</h3> -<p> - A color value specifies an RGB value with an alpha channel, which can - be used in various places such as specifying a solid color for a {@link android.graphics.drawable.Drawable} - or the color to use for text. A color value always begins with - a pound (#) character and then followed by the Alpha-Red-Green-Blue information - in one of the following formats: -</p> -<ul> -<li> #RGB -<li> #ARGB -<li> #RRGGBB -<li> #AARRGGBB -</ul> -<p> - If you want to retrieve the color represented by a resource ID, you can call - the {@link android.content.res.Resources#getColor(int) Resources.getColor()} method. -</p> -<p> - <strong>Source file format:</strong> XML file requiring a - <code><?xml version="1.0" encoding="utf-8"?></code> declaration, and - a root <code><resources></code> element containing one or more - <code><color></code> tags. -</p> -<p> - <strong>Resource source file location</strong>: {@code res/values/<em>colors</em>.xml} (File name is arbitrary.) -</p> -<p> - <strong>Compiled resource datatype:</strong> Resource pointer to a Java int. -</p> -<p> - <strong>Resource reference name:</strong> -</p> -<ul> - <li> - <strong>Java:</strong> <code>R.color.<em>some_name</em></code> - </li> - <li> - <strong>XML:</strong> <code>@[<em>package</em>:]color/some_name</code> (where <em>some_name</em> is the <em>name</em> of a specific color) - </li> -</ul> -<p> - <strong>Syntax</strong> -</p> -<pre> -<color name=<em>color_name</em>><em>#color_value</em></color> -</pre> -<dl> - <dt> - <color> - </dt> - <dd> - Value is a color, using web-style syntax, as describe above. Has only one attribute: - <ul> - <li> - <em>name</em> - The name used in referring to this color. - </li> - </ul> - </dd> -</dl> -<p> - <strong>Example XML Declaration</strong> -</p> -<p> - The following code declares two colors, the first fully opaque, and the - second translucent. -</p> -<pre> -<resources> - <color name="opaque_red">#f00</color> - <color name="translucent_red">#80ff0000</color> -</resources> -</pre> -<p> - <strong>Example Code Use</strong> -</p> -<p> - Example Java code -</p> -<pre> -// Retrieve a color value. -int color = getResources.getColor(R.color.opaque_red); -</pre> -<p> - Example XML code -</p> -<pre> -<TextView android:layout_width="fill_parent" - android:layout_height="wrap_content" - android:textAlign="center" - android:textColor="@color/translucent_red" - android:text="Some Text"/> -</pre> - - - -<h3 id="stringresources">Strings and Styled Text</h3> -<p> - Strings, with optional <a href="#styledtext">simple formatting</a>, can be -stored and retrieved as resources. You can add formatting to your string by -using three standard HTML tags: <b>, <i>, and <u>. To -guarantee getting an unstyled string only (the raw text) call the -<code>toString()</code> method of the retrieved CharSequence object. -Methods that accept string resources should be able to process these styling -tags. -</p> -<p> - If you want to retrieve the String represented by a resource ID, you can call the {@link android.content.Context#getString(int) Context.getString()} method. -</p> -<p> - <strong>Note:</strong> If you use an apostrophe or a quote in your string, you must either escape it or enclose the whole string in the other kind of enclosing quotes: -</p> -<pre> -<string name="good_example">"This'll work"</string> -<string name="good_example_2">This\'ll also work</string> -<string name="bad_example">This won't work!</string> -<string name="bad_example_2">XML encodings won&apos;t work either!</string> -</pre> -<p> - <strong>Source file format:</strong> XML file requiring a <code><?xml version="1.0" encoding="utf-8"?></code> declaration, and a root <code><resources></code> element containing one or more <code><string></code> tags. -</p> -<p> - <strong>Resource source file location</strong>: {@code res/values/<em>strings</em>.xml} (File name is arbitrary.) -</p> -<p> - <strong>Compiled resource datatype:</strong> Resource pointer to a Java CharSequence. -</p> -<p> - <strong>Resource reference name:</strong> -</p> -<ul> - <li> - <strong>Java:</strong> <code>R.string.<em>some_name</em></code> - </li> - <li> - <strong>XML:</strong> <code>@[<em>package</em>:]string/some_name</code> (where <em>some_name</em> is the <em>name</em> of a specific string) - </li> -</ul> -<p> - <strong>Syntax</strong> -</p> -<pre> -<string name=<em>string_name</em>><em>string_value</em></string> -</pre> -<dl> - <dt> - <string> - </dt> - <dd> - Value is a string, with optional styling tags. Has only one attribute: - <ul> - <li> - <em>name</em> - The name used in referring to this string. - </li> - </ul> - </dd> -</dl> -<p> - <strong>Example XML Declaration</strong> -</p> -<p> - The following declares two strings: the first — simple text with no - formatting (resulting in a CharSequence that is simply a String object) — the second includes formatting information in the string (resulting - in a CharSequence that is a complex data structure). If you are using the custom editor for string files in Eclipse, the HTML formatting tags will automatically be escaped and you will need to use {@link android.content.Context#getString(int) Context.getString()} and {@link android.text.Html#fromHtml} to retrieve the resource and then convert it to formatted text. -</p> -<pre> -<resources> - <string name="simple_welcome_message">Welcome!</string> - <string name="styled_welcome_message">We are <b><i>so</i></b> glad to see you.</string> -</resources> -</pre> -<p> - <strong>Example Code Use</strong> -</p> -<p> - Example Java code -</p> -<pre> -// Assign a styled string resource to a TextView -// on the current screen. -CharSequence str = getString(R.string.styled_welcome_message); -TextView tv = (TextView)findViewByID(R.id.text); -tv.setText(str); -</pre> -<p> - Example XML code -</p> -<pre> -<TextView android:layout_width="fill_parent" - android:layout_height="wrap_content" - android:textAlign="center" - android:text="@string/simple_welcome_message"/> -</pre> - - -<h4 id="styledtext">Using Styled Text as a Format String</h4> -<p> -Sometimes you may want to create a styled text resource that is also used as a -format string. This cannot be done directly because there is no way of passing -the styled text as the format string argument of String.format() -without stripping out the style information. The workaround is to store the -style tags as escaped HTML tags, and then convert the escaped HTML string into -a styled text after formatting has taken place. -</p> -<p> -To use styled text as a format string, do the following. -</p> -<ol> - <li>Store your styled text resource as an escaped string, so that the HTML tags in your text resource are not interpreted as if they were XML tags: -<pre> -<resources> - <string name="search_results_resultsTextFormat">%1$d results for &lt;b>&amp;quot;%2$s&amp;quot;&lt;/b></string> -</resources> -</pre> -<p> -In this example the format string has two arguments: <code>%1$d</code> is a decimal number, <code>%2$s</code> is a string. -</p> -</li> -<li> - Make sure any String arguments are properly escaped if they might contain '<' or '&' characters. -The {@link android.text.TextUtils#htmlEncode} method will do this: -<pre> -String escapedTitle = TextUtil.htmlEncode(title); -</pre> -</li> -<li> - Use String.format() to format the HTML text, then use {@link android.text.Html#fromHtml} to convert the HTML text into styled text: -<pre> -String resultsTextFormat = getContext().getResources().getString(R.string.search_results_resultsTextFormat); -String resultsText = String.format(resultsTextFormat, count, escapedTitle); -CharSequence styledResults = Html.fromHtml(resultsText); -</pre> -</li> -</ol> - - -<h3 id="dimension"Dimension Values</h3> -<p>You can create common dimensions to use for various screen elements by -defining dimension values in XML. A dimension resource is a number followed by -a unit of measurement. For example: 10px, 2in, 5sp. Here are the units of -measurement supported by Android:</p> -<dl> - <dt>px</dt> - <dd>Pixels - corresponds to actual pixels on the screen.</dd> - - <dt>in</dt> - <dd>Inches - based on the physical size of the screen.</dd> - - <dt>mm</dt> - <dd>Millimeters - based on the physical size of the screen.</dd> +<p>Each of the documents in this section describe the usage, format and syntax for a certain type +of application resource that you can provide in your resources directory ({@code res/}).</p> - <dt>pt</dt> - <dd>Points - 1/72 of an inch based on the physical size of the screen.</dd> +<p>Here's a brief summary of each resource type:</p> - <dt>dp</dt> - <dd>Density-independent Pixels - an abstract unit that is based on the - physical density of the screen. These units are relative to a 160 dpi - screen, so one dp is one pixel on a 160 dpi screen. The ratio of - dp-to-pixel will change with the screen density, but not necessarily - in direct proportion. <strong>Note:</strong> The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".</dd> - - <dt>sp</dt> - <dd>Scale-independent Pixels - this is like the dp unit, but it is also - scaled by the user's font size preference. It is recommend you use this - unit when specifying font sizes, so they will be adjusted for both the - screen density and user's preference.</dd> -</dl> - -<p>Dimension values are not normally used as raw resources, but rather as -attribute values in XML files. You can, however, create plain resources -containing this data type.</p> - -<p><strong>Source file format:</strong> XML file requiring a <code><?xml -version="1.0" encoding="utf-8"?></code> declaration, and a root -<code><resources></code> element containing one or more -<code><dimen></code> tags.</p> - -<p><strong>Resource source file location</strong>: {@code res/values/dimens.xml} (File -name is arbitrary, but standard practice is to put all dimensions in one file -devoted to dimensions.)</p> -<p><strong>Compiled resource datatype:</strong> Resource pointer to a -dimension.</p> -<p> - <strong>Resource reference name:</strong> -</p> -<ul> - <li> - <strong>Java:</strong> <code>R.dimen.<em>some_name</em></code> - </li> - <li> - <strong>XML:</strong> <code>@[<em>package</em>:]dimen/<em>some_name</em></code> (where <em>some_name</em> is the <em>name</em> of a specific <code><dimen></code> element) - </li> -</ul> -<p> - <strong>Syntax</strong> -</p> -<pre> -<dimen name=<em>dimen_name</em>><em>dimen_value</em></dimen> -</pre> <dl> - <dt> - <dimen> - </dt> - <dd> - A valid dimension value. - <ul> - <li> - <em>name</em> - The name used in referring to this dimension. - </li> - </ul> - </dd> + <dt><a href="{@docRoot}guide/topics/resources/animation-resource.html">Animation Resources</a></dt> + <dd>Define pre-determined animations.<br/> +Tween animations are saved in {@code res/anim/} and accessed from the {@code R.anim} class.<br/> +Frame animations are saved in {@code res/drawable/} and accessed from the {@code R.drawable} class.</dd> + <dt><a href="{@docRoot}guide/topics/resources/color-list-resource.html">Color State List Resource</a></dt> + <dd>Define a color resources that changes based on the View state.<br/> +Saved in {@code res/color/} and accessed from the {@code R.color} class.</dd> + <dt><a href="{@docRoot}guide/topics/resources/drawable-resource.html">Drawable Resources</a></dt> + <dd>Define various graphics with bitmaps or XML.<br/> +Saved in {@code res/drawable/} and accessed from the {@code R.drawable} class.</dd> + <dt><a href="{@docRoot}guide/topics/resources/layout-resource.html">Layout Resource</a></dt> + <dd>Define the layout for your application UI.<br/> +Saved in {@code res/layout/} and accessed from the {@code R.layout} class.</dd> + <dt><a href="{@docRoot}guide/topics/resources/menu-resource.html">Menu Resource</a></dt> + <dd>Define the contents of your application menus.<br/> +Saved in {@code res/menu/} and accessed from the {@code R.menu} class.</dd> + <dt><a href="{@docRoot}guide/topics/resources/string-resource.html">String Resources</a></dt> + <dd>Define strings, string arrays, and plurals (and include string formatting and styling).<br/> +Saved in {@code res/values/} and accessed from the {@code R.string}, {@code R.array}, +and {@code R.plurals} classes.</dd> + <dt><a href="{@docRoot}guide/topics/resources/style-resource.html">Style Resource</a></dt> + <dd>Define the look and format for UI elements.<br/> +Saved in {@code res/values/} and accessed from the {@code R.style} class.</dd> + <dt><a href="{@docRoot}guide/topics/resources/more-resources.html">More Resource Types</a></dt> + <dd>Define values such as booleans, integers, dimensions, colors, and other arrays.<br/> +Saved in {@code res/values/} but each accessed from unique {@code R} sub-classes (such as {@code +R.bool}, {@code R.integer}, {@code R.dimen}, etc.).</dd> </dl> -<p> - <strong>Example XML Declaration</strong> -</p> -<p> - The following code declares several dimension values. -</p> -<pre> -<resources> - <dimen name="one_pixel">1px</dimen> - <dimen name="double_density">2dp</dimen> - <dimen name="sixteen_sp">16sp</dimen> -</resources> -</pre> -<p> - <strong>Example Code Use</strong> -</p> -<p> - Example Java code: -</p> -<pre> -float dimen = Resources.getDimen(R.dimen.one_pixel); -</pre> -<p> - Example XML code: -</p> -<pre> -<TextView android:layout_width="fill_parent" - android:layout_height="wrap_content" - android:textSize="@dimen/sixteen_sp"/> -</pre> - - -<h2 id="drawables">Drawables</h2> -<p>A {@link android.graphics.drawable.Drawable} is a type of resource that -you retrieve with {@link android.content.res.Resources#getDrawable -Resources.getDrawable()} and use to draw to the screen. There are a -number of drawable resources that can be created.</p> -<h3 id="imagefileresources">Bitmap Files</h3> -<p>Android supports bitmap resource files in a few different formats: png -(preferred), jpg (acceptable), gif (discouraged). The bitmap file itself is -compiled and referenced by the file name without the extension (so -res/drawable/my_picture.png would be referenced as R.drawable.my_picture).</p> - -<p> - <strong>Source file formats:</strong> png (preferred), jpg (acceptable), gif (discouraged). One resource per file. -</p> -<p> - <strong>Resource file location</strong>: {@code res/drawable/<em>some_file</em>.png} -</p> -<p> - <strong>Compiled resource datatype:</strong> Resource pointer to a {@link android.graphics.drawable.BitmapDrawable BitmapDrawable}. -</p> -<p> - <strong>Resource reference name:</strong> -</p> -<ul> - <li> - <strong>Java:</strong> <code>R.drawable.<em>some_file</em></code> - </li> - <li> - <strong>XML:</strong> <code>@[<em>package</em>:]drawable/<em>some_file</em></code> - </li> -</ul> - -<p>For more discussion and examples using drawable resources, see the discussion in <a href="{@docRoot}guide/topics/graphics/2d-graphics.html#drawable-resource#drawables">2D Graphics</a>.</p> - - -<h3 id="colordrawableresources">Color Drawables</h3> -<p>You can create a {@link android.graphics.drawable.PaintDrawable} object that is a rectangle of color, -with optionally rounded corners. This element can be defined in any of the -files inside res/values/.</p> -<p><strong>Source file format:</strong> XML file requiring a <code><?xml -version="1.0" encoding="utf-8"?></code> declaration, and a root -<code><resources></code> element containing one or more -<code><drawable></code> tags.</p> -<p> - <strong>Resource source file location</strong>: {@code res/values/colors.xml} (File name is arbitrary, but standard practice is to put the PaintDrawable - items in the file along with the <a href="resources-i18n.html#numericcolorresources">numeric color values</a>.) -</p> -<p> - <strong>Compiled resource datatype:</strong> Resource pointer to a {@link android.graphics.drawable.PaintDrawable}. -</p> -<p> - <strong>Resource reference name:</strong> -</p> -<ul> - <li> - <strong>Java:</strong> <code>R.drawable.<em>some_name</em></code> - </li> - <li> - <strong>XML:</strong> <code>@[<em>package</em>:]drawable/<em>some_name</em></code> (where <em>some_name</em> is the name of a specific resource) - </li> -</ul> -<p> - <strong>Syntax</strong> -</p> -<pre> -<drawable name=<em>color_name</em>><em>color_value</em></drawable> -</pre> -<dl> - <dt> - <drawable> - </dt> - <dd> - A valid <a href="#colorvals">color value</a>. - <ul> - <li> - <em>name</em> - The name used in referring to this drawable. - </li> - </ul> - </dd> -</dl> -<p> - <strong>Example XML Declaration</strong> -</p> -<p> - The following code declares several color drawables. -</p> -<pre> -<resources> - <drawable name="solid_red">#f00</drawable> - <drawable name="solid_blue">#0000ff</drawable> - <drawable name="solid_green">#f0f0</drawable> -</resources> -</pre> -<p> - <strong>Example Code Use</strong> -</p> -<p> - Example Java code -</p> -<pre> -// Assign a PaintDrawable as the background to -// a TextView on the current screen. -Drawable redDrawable = Resources.getDrawable(R.drawable.solid_red); -TextView tv = (TextView)findViewByID(R.id.text); -tv.setBackground(redDrawable); -</pre> -<p> - Example XML code -</p> -<pre> -<TextView android:layout_width="fill_parent" - android:layout_height="wrap_content" - android:textAlign="center" - android:background="@drawable/solid_red"/> -</pre> - - -<h3 id="ninepatch">Nine-Patch (stretchable) Images</h3> -<p> - Android supports a stretchable bitmap image, called a - {@link android.graphics.NinePatch} graphic. This is a PNG image in which - you define stretchable sections that Android will resize to fit the object - at display time to accommodate variable sized sections, such as text strings. - You typically assign this resource to the View's background. An example use - of a stretchable image is the button backgrounds that Android uses; buttons - must stretch to accommodate strings of various lengths. -</p> - -<p> - <strong>Source file format:</strong> PNG — one resource per file -</p> -<p> - <strong>Resource source file location</strong>: {@code res/drawable/<em>some_name</em>.9.png} (Filename must end in {@code .9.png}) -</p> -<p> - <strong>Compiled resource datatype:</strong> Resource pointer to a {@link android.graphics.drawable.NinePatchDrawable NinePatchDrawable}. -</p> -<p> - <strong>Resource reference name:</strong> -</p> -<ul> - <li> - <strong>Java:</strong> <code>R.drawable.<em>some_file</em></code> - </li> - <li> - <strong>XML:</strong> <code>@[<em>package</em>:]drawable.<em>some_file</em></code> - </li> -</ul> - - -<p>For more information and examples using NinePatch drawables, see the discussion -in <a href="{@docRoot}guide/topics/graphics/2d-graphics.html#nine-patch">2D Graphics</a>.</p> - - - -<h2 id="animation">Animation</h2> - -<h3 id="tweenedanimation">Tweened Animation</h3> -<p> - Android can perform simple animation on a graphic, or a series of graphics. These include rotations, fading, moving, and stretching. -</p> -<p> - <strong>Source file format:</strong> XML file, one resource per file, one root tag with no <code><?xml></code> declaration -</p> -<p> - <strong>Resource file location</strong>: {@code res/anim/<em>some_file</em>.xml} -</p> -<p> - <strong>Compiled resource datatype:</strong> Resource pointer to an {@link android.view.animation.Animation}. -</p> -<p> - <strong>Resource reference name:</strong> -</p> -<ul> - <li> - <strong>Java:</strong> <code>R.anim.<em>some_file</em></code> - </li> - <li> - <strong>XML:</strong> <code>@[<em>package</em>:]anim/<em>some_file</em></code> - </li> -</ul> -<p> - <strong>Syntax</strong> -</p> -<p> - The file must have a single root element: this will be either a single <code><alpha></code>, <code><scale></code>, <code><translate></code>, <code><rotate></code>, interpolator element, or <code><set></code> element that holds groups of these elements (which may include another <code><set></code>). By default, all elements are applied simultaneously. To have them occur sequentially, you must specify the <code>startOffset</code> attribute. -</p> -<pre> -<set android:shareInterpolator=boolean> // Only required if multiple tags are used. - <alpha android:fromAlpha=float - android:toAlpha=float > | - <scale android:fromXScale=float - android:toXScale=float - android:fromYScale=float - android:toYScale=float - android:pivotX=string - android:pivotY=string > | - <translate android:fromX=string - android:toX=string - android:fromY=string - android:toY=string > | - <rotate android:fromDegrees=float - android:toDegrees=float - android:pivotX=string - android:pivotY=string > | - <<em>interpolator tag</em>> - <set> -</set> -</pre> -<p> - <strong>Elements and Attributes</strong> -</p> -<dl> - <dt> - <set> - </dt> - <dd> - A container that can recursively hold itself or other animations. - Represents an {@link android.view.animation.AnimationSet}. - You can include as many child elements of the same or different types as you like. - Supports the following attribute: - <ul> - <li> - <em>shareInterpolator</em> - Whether to share the same Interpolator among all immediate child elements. - </li> - </ul> - </dd> - <dt> - <alpha> - </dt> - <dd> - A fading animation. Represents an {@link android.view.animation.AlphaAnimation}. - Supports the following attributes: - <ul> - <li> - <em>fromAlpha</em> - 0.0 to 1.0, where 0.0 is transparent. - </li> - <li> - <em>toAlpha</em> - 0.0 to 1.0, where 0.0 is transparent. - </li> - </ul> - </dd> - <dt> - <scale> - </dt> - <dd> - A resizing animation. Represents a {@link android.view.animation.ScaleAnimation}. - You can specify what is the center point of the image (the pinned center), from which it grows outward (or inward), by specifying pivotX and pivotY. So, for example, if these were 0, 0 (top left corner), all growth would be down and to the right. <code>scale</code> supports the following attributes: - <ul> - <li> - <em>fromXScale</em> - Starting X size, where 1.0 is no change. - </li> - <li> - <em>toXScale</em> - Ending X size, where 1.0 is no change. - </li> - <li> - <em>fromYScale</em> - Starting Y size, where 1.0 is no change. - </li> - <li> - <em>toYScale</em> - Ending Y size, where 1.0 is no change. - </li> - <li> - <em>pivotX</em> - The X coordinate of the pinned center. - </li> - <li> - <em>pivotY</em> - The Y coordinate of the pinned center. - </li> - </ul> - </dd> - <dt> - <translate> - </dt> - <dd> - A vertical/horizontal motion animation. - Represents a {@link android.view.animation.TranslateAnimation}. -Supports the following attributes in any of the following three formats: values from -100 to 100, ending with "%", indicating a percentage relative to itself; values from -100 to 100, ending in "%p", indicating a percentage relative to its parent; a float with no suffix, indicating an absolute value. - <ul> - <li> - <em>fromXDelta</em> - Starting X location. - </li> - <li> - <em>toXDelta</em> - Ending X location. - </li> - <li> - <em>fromYDelta</em> - Starting Y location. - </li> - <li> - <em>toYDelta</em> - Ending Y location. - </li> - </ul> - </dd> - <dt> - <rotate> - </dt> - <dd> - A rotation animation. Represents a {@link android.view.animation.RotateAnimation}. - Supports the following attributes: - <ul> - <li> - <em>fromDegrees</em> - Starting rotation, in degrees. - </li> - <li> - <em>toDegrees</em> - Ending rotation, in degrees. - </li> - <li> - <em>pivotX</em> - The X coordinate of the center of rotation, in pixels, where (0,0) is the top left corner. - </li> - <li> - <em>pivotY</em> - The Y coordinate of the center of rotation, in pixels, where (0,0) is the top left corner. - </li> - </ul> - </dd> - <dt> - <em><interpolator tag></em> - </dt> - <dd> - You can also use any of the interpolator subclass elements defined in {@link android.R.styleable}. Examples include <CycleInterpolator>, <EaseInInterpolator>, and <EaseOutInterpolator>. These objects define a velocity curve that describes how quickly a visual action takes place on a timeline (fast at first and slow later, slow at first and gradually faster, and so on). - </dd> -</dl> -<p> -In addition to the attributes defined for each element above, the elements -<code><alpha></code>, <code><scale></code>, <code><translate></code>, -<code><rotate></code>, and <code><set></code> all support the following attributes (inherited -from the {@link android.view.animation.Animation} class): -</p> -<dl> - <dt><em>{@link android.R.attr#duration duration}</em></dt> - <dd> - Duration, in milliseconds, for this effect. - </dd> - <dt><em>{@link android.R.attr#startOffset startOffset}</em></dt> - <dd> - Offset start time for this effect, in milliseconds. - </dd> - <dt><em>{@link android.R.attr#fillBefore fillBefore}</em></dt> - <dd> - When set true, the animation transformation is applied before the animation begins. - </dd> - <dt><em>{@link android.R.attr#fillAfter fillAfter}</em></dt> - <dd> - When set true, the animation transformation is applied after the animation ends. - </dd> - <dt><em>{@link android.R.attr#repeatCount repeatCount}</em></dt> - <dd> - Defines the number of times the animation should repeat. - </dd> - <dt><em>{@link android.R.attr#repeatMode repeatMode}</em></dt> - <dd> - Defines the animation behavior when it reaches the end and the repeat count is greater than 0. - Options are to either restart or reverse the animation. - </dd> - <dt><em>{@link android.R.attr#zAdjustment zAdjustment}</em></dt> - <dd> - Defines the z-axis ordering mode to use when running the animation (normal, top, or bottom). - </dd> - <dt><em>{@link android.R.attr#interpolator interpolator}</em></dt> - <dd> - You can optionally set an interpolator for each element to determine how quickly or slowly it performs its effect over time. For example, slow at the beginning and faster at the end for EaseInInterpolator, and the reverse for EaseOutInterpolator. A list of interpolators is given in {@link android.R.anim}. To specify these, use the syntax @android:anim/<em>interpolatorName</em>. - </dd> -</dl> - -<p>For more discussion and animation code samples, see the discussion in the -<a href="{@docRoot}guide/topics/graphics/2d-graphics.html#tween-animation">2D Graphics</a> document.</p> -<h2 id="menus">Menus</h2> -<p>Application menus (Options Menu, Context Menu, or Sub Menu) can be defined as -XML resources and inflated by your application using {@link android.view.MenuInflater}.</p> - -<p><strong>Source file format:</strong> XML file, one resource per file, one root tag, -<code><?xml></code> declaration not required.</p> -<p><strong>Resource file location:</strong> res/menu/<em>some_file</em>.xml</p> -<p><strong>Compiled resource datatype:</strong> Resource pointer to a {@link android.view.Menu} (or subclass) resource.</p> -<p><strong>Resource reference name:</strong> </p> -<ul><li><strong>Java:</strong> <code>R.menu.<em>some_file</em></code></li></ul> - -<h3>Syntax</h3> -<p>The file must have a single root element: a <code><menu></code> element. In all, -there are three valid elements: <code><menu></code>, <code><group></code> and <code><item></code>. The -<code><item></code> and <code><group></code> elements must be the children of a <code><menu></code>, but <code><item></code> -elements can also be the children of a <code><group></code>, and another <code><menu></code> element may be the child -of an <code><item></code> (to create a Sub Menu).</p> -<pre> -<menu xmlns:android="http://schemas.android.com/apk/res/android"> - - <item android:id="@+id/<em>example_item</em> - android:title="<em>Example Item</em>" - android:icon="<em>@drawable/example_item_icon</em>" /> - - <group android:id="@+id/<em>example_group</em>"> - <item android:id="@+id/<em>example_item2</em> - android:title="<em>Example Item 2</em>" - android:icon="<em>@drawable/example_item2_icon</em>" /> - </group> - - <item android:id="@+id/<em>example_submenu</em> - android:title="<em>Example Sub Menu</em>" > - <menu> - <item android:id="@+id/<em>example_submenu_item</em> - android:title="<em>Example Sub Menu Item</em>" /> - </menu> - </item> - -</menu> -</pre> - -<h3>Elements and Attributes</h3> -<p>All attributes must be defined with the <em>android</em> namespace (e.g., <em>android:icon="@drawable/icon"</em>).</p> -<dl> - <dt><menu></dt> - <dd>The root of a menu. Contains <code><item></code> and <code><group></code> nodes. No attributes.</dd> - <dt><group></dt> - <dd>A menu group. Contains <code><item></code> elements. Valid attributes: - <ul> - <li><em>id</em> - A unique integer ID for the group.</li> - <li><em>menuCategory</em> - Value corresponding to Menu CATEGORY_* constants — defines the priority of the group. Valid values: - <em>container</em>, <em>system</em>, <em>secondary</em>, and <em>alternative</em>.</li> - <li><em>orderInCategory</em> - An integer that defines the default order of the items within the category.</li> - <li><em>checkableBehavior</em> - Whether the items are checkable. Valid values: - <em>none</em>, <em>all</em> (exclusive / radio buttons), <em>single</em> (non-exclusive / checkboxes)</li> - <li><em>visible</em> - Whether the group is visible. <em>true</em> or <em>false</em>.</li> - <li><em>enabled</em> - Whether the group is enabled. <em>true</em> or <em>false</em>.</li> - </ul> - </dd> - <dt><item></dt> - <dd>A menu item. May contain a <code><menu></code> element (for a Sub Menu). Valid attributes: - <ul> - <li><em>id</em> - A unique resource ID for the item.</li> - <li><em>menuCategory</em> - Used to define the menu category.</li> - <li><em>orderInCategory</em> - Used to define the order of the item, within a group.</li> - <li><em>title</em> - A string for the menu title.</li> - <li><em>titleCondensed</em> - A condensed string title, for situations in which the normal title is too long.</li> - <li><em>icon</em> - A resource identifier for a drawable icon.</li> - <li><em>alphabeticShortcut</em> - A character for the alphabetic shortcut key.</li> - <li><em>numericShortcut</em> - A number for the numeric shortcut key.</li> - <li><em>checkable</em> - Whether the item is checkable. <em>true</em> or <em>false</em>.</li> - <li><em>checked</em> - Whether the item is checked by default. <em>true</em> or <em>false</em>.</li> - <li><em>visible</em> - Whether the item is visible by default. <em>true</em> or <em>false</em>.</li> - <li><em>enabled</em> - Whether the item is enabled by default. <em>true</em> or <em>false</em>.</li> - </ul> - </dd> -</dl> - -<p>For more discussion on how to create menus in XML and inflate them in your application, -read <a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a>.</p> - - - -<h2 id="layoutresources">Layout</h2> -<p>Android lets you specify screen layouts using XML elements inside an XML -file, similar to designing screen layout for a webpage in an HTML file. Each -file contains a whole screen or a part of a screen, and is compiled into a -View resource that can be passed in to -{@link android.app.Activity#setContentView(int) Activity.setContentView} or used as a -reference by other layout resource elements. Files are saved in the -<code>res/layout/</code> folder of your project, and compiled by the Android resource -compiler, aapt. </p> - -<p> Every layout XML file must evaluate to a single root -element. First we'll describe how to use the standard XML tags understood by -Android as it is shipped, and then we'll give a little information on how you -can define your own custom XML elements for custom View objects. - -<p> The root element must have -the Android namespace "http://schemas.android.com/apk/res/android" defined in -the root element.</p> - -<p>For a complete discussion on creating layouts, see the -<a href="{@docRoot}guide/topics/ui/index.html">User Interface</a> topic.</p> - -<p> <strong>Source file format:</strong> XML file -requiring a <code><?xml version="1.0" encoding="utf-8"?></code> -declaration, and a root element of one of the supported XML layout elements. -</p> - - -<p><strong>Resource file location</strong>: -res/layout/<em>some_file</em>.xml.</p> -<p> - <strong>Compiled resource datatype:</strong> Resource pointer to a {@link android.view.View} (or subclass) resource. -</p> -<p> - <strong>Resource reference name:</strong> -</p> -<ul> - <li> - <strong>Java:</strong> <code>R.layout.<em>some_file</em></code> - </li> - <li> - <strong>XML:</strong> <code>@[<em>package</em>:]layout/<em>some_file</em></code> - </li> -</ul> -<p> - <strong>Syntax</strong> -</p> -<pre> -<<em>ViewGroupClass</em> xmlns:android="http://schemas.android.com/apk/res/android" - android:id="@+id/<em>string_name</em>" (attributes)> - <<em>widget</em> or other nested <em>ViewGroupClass</em>>+ - <requestFocus/>(0 or 1 per layout file, assigned to any element) -</<em>ViewGroupClass</em>> -</pre> -<dl> - <dt> - <<em>ViewGroupClass</em>> - </dt> - <dd> - <p>The file must have a single root element. This can be a ViewGroup class that contains other elements, or a widget (or custom item) if it's only one object. By default, you can use any (case-sensitive) Android {@link android.widget widget} or {@link android.view.ViewGroup ViewGroup} class name as an element. These elements support attributes that apply to the underlying class, but the naming is not as clear. How to discover what attributes are supported for what tags is discussed below. You should not assume that any nesting is valid (for example you cannot enclose <code><TextView></code> elements inside a <code><ListLayout></code>).</p> - <p>If a class derives from another class, the XML element inherits all the attributes from the element that it "derives" from. So, for example, <code><EditText></code> is the corresponding XML element for the EditText class. It exposes its own unique attributes (<code>EditText_numeric</code>), as well as all attributes supported by <code><TextView></code> and <code><View></code>. For the <em>id</em> attribute of a tag in XML, you should use a special syntax: "@+id/<em>somestringvalue</em>". The "@+" syntax creates a resource number in the R.id class, if one doesn't exist, or uses it, if it does exist. When declaring an ID value for an XML tag, use this syntax. Example: <code><TextView android:id="@+id/nameTextbox"/></code>, and refer to it this way in Java: <code>findViewById(R.id.nameTextbox)</code>. All elements support the following values:</p> - <ul> - <li> - <em>id</em> - An ID value used to access this element in Java. Typically you will use the syntax @+id/<em>string_name</em> to generate an ID for you in the id.xml file if you haven't created one yourself. - </li> - <li> - <code>xmlns:android="http://schemas.android.com/apk/res/android"</code> - <em><strong>Required for the root element only.</strong></em> - </li> - </ul> - </dd> - <dt> - <requestFocus> - </dt> - <dd> - Any element representing a View object can include this empty element, which gives it's parent tag initial focus on the screen. You can have only one of these elements per file. - </dd> -</dl> -<p> - <strong>What Attributes Are Supported for What Elements?</strong> -</p> -<p> - Android uses the {@link android.view.LayoutInflater} class at run time to load an XML layout resource and translate it into visual elements. By default, all widget class names are supported directly as tags, but a full list of supported tags and attributes is listed in the {@link android.R.styleable} reference page. However, the attribute names are somewhat obscure. If an underscore appears in the name, this indicates that it is an attribute — typically of the element before the underscore. So, for example, <code>EditText_autoText</code> means that the <code><EditText></code> tag supports an attribute <em>autoText</em>. When you actually use the attribute in that element, use only the portion after the last underscore, and prefix the attribute with the prefix "<code>android:</code>". So, for example, if {@link android.R.styleable} lists the following values: -</p> -<ul> - <li> - <code>TextView</code> - </li> - <li> - <code>TextView_lines</code> - </li> - <li> - <code>TextView_maxlines</code> - </li> -</ul> -<p> - You could create an element like this: -</p> -<pre> -<TextView android:lines="10" android:maxlines="20"/> -</pre> -<p> - This would create a {@link android.widget.TextView} object and set its lines and maxlines properties. -</p> -<p> - Attributes come from three sources: -</p> -<ul> - <li> - <strong>Attributes exposed directly by the element.</strong> For example, <code>TextView</code> supports <code>TextView_text</code>, as discussed above. - </li> - <li> - <strong>Attributes exposed by all the superclasses of that element.</strong> For example, the TextView class extends the View class, so the <code><TextView></code> element supports all the attributes that the <code><View></code> element exposes — a long list, including <code>View_paddingBottom</code> and <code>View_scrollbars</code>. These too are used without the class name: <code><TextView android:paddingBottom="20" android:scrollbars="horizontal" /></code>. - </li> - <li> - <strong>Attributes of the object's {@link android.view.ViewGroup.LayoutParams} subclass.</strong> All View objects support a LayoutParams member (see <a href="{@docRoot}guide/topics/ui/declaring-layout.html#layout-params">Declaring Layout</a>). To set properties on an element's LayoutParams member, the attribute to use is "android:layout_<em>layoutParamsProperty</em>". For example: <code>android:layout_gravity</code> for an object wrapped by a <code><LinearLayout></code> element. Remember that each LayoutParams subclass also supports inherited attributes. Attributes exposed by each subclass are given in the format <em>someLayoutParamsSubclass</em>_Layout_layout_<em>someproperty</em>. This defines an attribute "android:layout_<em>someproperty</em>". Here is an example of how Android documentation lists the properties of the {@link android.widget.LinearLayout.LayoutParams LinearLayout.LayoutParams} class: - </li> -</ul> -<ul> - <li>LinearLayout_Layout // The actual object — not used. - </li> - <li>LinearLayout_Layout_layout_gravity // Exposes a <code>gravity</code> attribute - </li> - <li>LinearLayout_Layout_layout_height // Exposes a <code>height</code> attribute - </li> - <li>LinearLayout_Layout_layout_weight // Exposes a <code>weight</code> attribute - </li> - <li>LinearLayout_Layout_layout_width // Exposes a <code>width</code> attribute - </li> -</ul> -<p> - Here is an example that sets some of these values on a few objects, including direct attributes, inherited attributes, and LayoutParams attributes: -</p> -<pre> -<?xml version="1.0" encoding="utf-8"?> -<!-- res/main_screen.xml --> -<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" - android:orientation="vertical" // The object's own orientation property - android:padding="4" // Inherited View property - android:gravity="center" // The object's own property - android:layout_width="fill_parent" // Parent object's LinearLayout.LayoutParams.width - android:layout_height="fill_parent"> // Parent object's LinearLayout.LayoutParams.height - - <TextView android:layout_width="fill_parent" // TextView.LayoutParams.width - android:layout_height="wrap_content" // TextView.LayoutParams.height - android:layout_weight="0" // TextView.LayoutParams.weight - android:paddingBottom="4" // TextView.paddingBottom - android:text="@string/redirect_getter"/> // TextView.text - - <EditText android:id="@+id/text" - android:layout_width="fill_parent" // EditText.LayoutParams.width - android:layout_height="wrap_content" // EditText.LayoutParams.height - android:layout_weight="0" // EditText.LinearLayoutParams.weight - android:paddingBottom="4"> // EditText.paddingBottom - <requestFocus /> - </EditText> - - <Button android:id="@+id/apply" - android:layout_width="wrap_content" // Button.LayoutParams.width - android:layout_height="wrap_content" // Button.LayoutParams.height - android:text="@string/apply" /> // TextView.text -</LinearLayout> -</pre> -<p> - <strong>Example Code Use</strong> -</p> -<p> - The most common use is to load the XML file (located at <em>res/main_screen.xml</em>) and use it as the current screen, as shown here with the preceding file: -</p> -<pre> -setContentView(R.layout.main_screen); -</pre> -<p> - However, layout elements can also represent repeating elements used as templates. -</p> - -<p>Also see <a href="{@docRoot}guide/topics/ui/index.html">User Interface</a> for more information on layouts.</p> - - - -<h3 id="customresources">Custom Layout Resources</h3> -<p> - You can define custom elements to use in layout resources. These custom elements can then be used the same as any Android layout elements: that is, you can use them and specify their attributes in other resources. The ApiDemos sample application has an example of creating a custom layout XML tag, LabelView. To create a custom element, you will need the following files: -</p> -<ul> - <li> - <strong>Java implementation file</strong> - The implementation file. The class must extend {@link android.view.View View} or a subclass. See LabelView.java in ApiDemos. - </li> - <li> - <strong>res/values/attrs.xml</strong> - Defines the XML element, and the attributes that it supports, for clients to use to instantiate your object in their layout XML file. Define your element in a <code><declare-styleable name=<em>your_java_class_name</em>></code>. See res/values/attrs.xml in ApiDemos. - </li> - <li> - <strong>res/layout/<em>your_class</em>.xml</strong> [<em>optional</em>] - An optional XML file to describe the layout of your object. This could also be done in Java. See custom_view_1.xml in ApiDemos. - </li> -</ul> -<p> - <strong>Source file format:</strong> XML file without an <code><?xml></code> declaration, and a <code><resources></code> root element containing one or more custom element tags. -</p> -<p> - <strong>Resource file location</strong>: {@code res/values/<em>attrs</em>.xml} (File name is arbitrary.) -</p> -<p> - <strong>Compiled resource datatype:</strong> Resource pointer to a {@link android.view.View} (or subclass) resource. -</p> -<p> - <strong>Resource reference name:</strong> R.styleable.<em>some_file</em> (Java). -</p> - - - -<h2 id="stylesandthemes">Styles and Themes</h2> -<p> - A <em>style</em> is one or more attributes applied to a single element (for example, 10 point red Arial font, applied to a TextView). A style is applied as an attribute to an element in a layout XML file. -</p> -<p> - A <em>theme</em> is one or more attributes applied to a whole screen — for example, you might apply the stock Android Theme.dialog theme to an activity designed to be a floating dialog box. A theme is assigned as an attribute to an Activity in the manifest file. -</p> -<p> - Both styles and themes are defined in a <code><style></code> block containing one or more string or numerical values (typically color values), or references to other resources (drawables and so on). These elements support inheritance, so you could have MyBaseTheme, MyBaseTheme.Fancy, MyBaseTheme.Small, and so on. -</p> - -<p>For a complete discussion on styles and themes, read -<a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>.</p> - -<p> - <strong>Source file format:</strong> XML file requiring a <code><?xml version="1.0" encoding="utf-8"?></code> declaration, and a root <code><resources></code> element containing one or more <code><style></code> tags. -</p> -<p> - <strong>Resource source file location</strong>: {@code res/values/styles.xml} (File name is arbitrary, but standard practice is to put all styles into a file named styles.xml.) -</p> -<p> - <strong>Compiled resource datatype:</strong> Resource pointer to a Java CharSequence. -</p> -<p> - <strong>Resource reference name:</strong> -</p> -<ul> - <li> - <strong>Java:</strong> <code>R.style.<em>styleID</em></code> for the whole style, <code>R.style.<em>styleID</em>.<em>itemID</em></code> for an individual setting - </li> - <li> - <strong>XML:</strong> <code>@[<em>package</em>:]style/<em>styleID</em></code> for a whole style, <code>@[<em>package</em>:]style/<em>styleID</em>/<em>itemID</em></code> for an individual item. <strong>Note</strong>: to refer to a value in the <em>currently</em> applied theme, use "?" instead of "@" as described below (XML). - </li> -</ul> -<p> - <strong>Syntax</strong> -</p> -<pre> -<style name=<em>string</em> [parent=<em>string</em>] > - <item name=<em>string</em>><em>Hex value | string value | reference</em></item>+<em> -</em></style> -</pre> -<dl> - <dt> - <style> - </dt> - <dd> - Holds one or more <item> elements, each describing one value. This style, which is a bundle of values, can be referred to as a <em>theme</em>. - <ul> - <li> - <em>name</em> - The name used in referring to this theme. - </li> - <li> - <em>parent</em> - An optional parent theme. All values from the specified theme will be inherited into this theme. Any values with identical names that you specify will override inherited values. The name must be qualified by the package, but you don't need the /style directive (for example, <code>android:Theme</code> for the base Android theme, or <code>MyTheme</code> for a theme defined in your package). - </li> - </ul> - </dd> - <dt> - <item> - </dt> - <dd> - A value to use in this theme. It can be a standard string, a hex color value, or a reference to any other resource type. - </dd> -</dl> - -<p>For examples of how to declare and apply styles and themes, read -<a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>.</p> - - - -<h2 id="Searchable">Searchable</h2> - -<p>To make search appear to the user as a seamless system-wide feature, the Android framework -offers APIs that let applications control how they are searched. -Applications can customize how search is invoked, how the search dialog looks, and what type of -search results are available, including suggestions that are shown as the user types.</p> - -<p>In order to utilize the Android search framework, an application must provide a search configuration -in the form of an XML resource. -This section describes the search configuration XML in terms of its syntax and usage. For a more -complete discussion about how to implement search features for your application, see -<!-- <a href="{@docRoot}guide/topics/search/index.html">Search</a> --> -{@link android.app.SearchManager}.</p> - -<p> - <strong>Source file format:</strong> - XML file requiring a <code><?xml version="1.0" encoding="utf-8"?></code> - declaration, and a root <code><searchable></code> element. -</p> - -<p> - <strong>Resource source file location</strong>: {@code res/xml/searchable.xml} - (The file name is arbitrary, but standard practice is to use searchable.xml.) -</p> - -<p> - <strong>Compiled resource datatype:</strong> - Resource pointer to an xml object. -</p> - -<p> - <strong>Resource reference name:</strong> -</p> - -<ul> - <li> - <strong>Java:</strong> - <code>R.xml.<em>filename</em></code>. - </li> - <li> - <strong>XML:</strong> - <code>@[<em>package</em>:]xml/<em>filename</em></code> (e.g., <code>@xml/searchable</code>). - </li> -</ul> - -<p> - <strong>Syntax</strong> -</p> - -<pre> -<searchable xmlns:android="http://schemas.android.com/apk/res/android - android:label="@string/search_label" - ... > - <em><actionkey - android:keycode="KEYCODE_CALL" - ... ></em> -</searchable> -</pre> - -<dl> - <dt> - <searchable> - </dt> - <dd> - Defines all application search configurations, including settings for text and voice searches - performed within the application. It accepts the following attributes: - <ul> - <li> - <em>label</em> - <strong>Required</strong>. This is the name for your application, as it - will appear to the user. This will be visible only if <em>searchMode</em> is set to - "showSearchLabelAsBadge" (see below). - </li> - <li> - <em>hint</em> - This is the text to display in the search text field when no text has - been entered. This is recommended in order to provide context to the search about to be - performed (e.g., "Search the Dictionary"). - </li> - <li> - <em>searchMode</em> - If provided and non-zero, this sets additional modes for control - of the search presentation. The following mode values are accepted: - <ul> - <li> - <em>showSearchLabelAsBadge</em> - If set, this enables the display of the - search target (label) within the search bar. - </li> - <li> - <em>queryRewriteFromData</em> - If set, this causes the suggestion column - SUGGEST_COLUMN_INTENT_DATA to be considered as the text for suggestion query - rewriting. This should only be used when the values in - SUGGEST_COLUMN_INTENT_DATA are suitable for user inspection and editing - - typically, HTTP/HTTPS Uri's. - </li> - <li> - <em>queryRewriteFromText</em> - If set, this causes the suggestion - column SUGGEST_COLUMN_TEXT_1 to be considered as the text for suggestion query - rewriting. This should be used for suggestions in which no query - text is provided and the SUGGEST_COLUMN_INTENT_DATA values are not suitable - for user inspection and editing. - </li> - </ul> - <p>More than one of the above values for <em>searchMode</em> can be used at once. For - example, you can declare two modes at once, like this: - <code>searchMode="queryRewriteFromData|queryRewriteFromText"</code> - </li> - <li> - <em>inputType</em> - If provided, supplies a hint about the type of search text - the user will be entering. For most searches, in which free form text is expected, - this attribute is not needed. See - {@link android.R.attr#inputType} for a list of suitable values for this attribute. - </li> - <li> - <em>imeOptions</em> - If provided, supplies additional options for the input method. - For most searches, in which free form text is expected, this attribute is not needed, - and will default to "actionSearch". See - {@link android.R.attr#imeOptions} for a list of suitable values for this attribute. - </li> - </ul> - - <p>If you have defined a content provider to generate search suggestions, you need to - provide some more searchable metadata in order to configure communications with the content - provider. The following are additional {@code <searchable>} attributes for use when - providing search suggestions:</p> - - <ul> - <li> - <em>searchSuggestAuthority</em> - <strong>Required to provide search suggestions</strong>. - This value must match the authority string provided in the provider section of your manifest. - </li> - <li> - <em>searchSuggestPath</em> - If provided, this path will be inserted in the suggestions - query Uri, after the authority you have provide but before the standard suggestions path. - This is only required if you have a single content provider issuing different types - of suggestions (e.g. for different data types) and you need - a way to disambiguate the suggestions queries when they are received. - </li> - <li> - <em>searchSuggestSelection</em> - If provided, this value will be passed into your - query function as the selection parameter. Typically this will be a WHERE clause for - your database, and will contain a single question mark, which represents the actual query - string that has been typed by the user. However, you can also use any non-null value to simply - trigger the delivery of the query text (via selection arguments), and then use the query - text in any way appropriate for your provider (ignoring the actual text of the selection parameter.) - </li> - <li> - <em>searchSuggestIntentAction</em> - The default Intent action to be used when a user - clicks on a search suggestion. - If provided, and not overridden by the selected suggestion, this value will - be placed in the action field of the {@link android.content.Intent} when the - user clicks a suggestion. - </li> - <li> - <em>searchSuggestIntentData</em> - The default Intent data to be used when a user - clicks on a search suggestion. - If provided, and not overridden by the selected suggestion, this value will be - placed in the data field of the {@link android.content.Intent} when the user clicks - a suggestion. - </li> - </ul> - - <p>Beyond providing search suggestions while using your application's local search, you - can also configure your search suggestions to be made available to Quick Search Box, - which will allow users so receive search suggestions from your application content from outside - your application. The following are additional {@code <searchable>} attributes for use when - providing search suggestions to Quick Search Box:</p> - - <ul> - <li> - <em>includeInGlobalSearch</em> - <strong>Required to provide search suggestions in - Quick Search Box</strong>. If true, this indicates the search suggestions provided by your - application should be included in the globally accessible Quick Search Box. The user must - still enable your application as a searchable item in the system search settings in order - for your suggestions to appear in Quick Search Box. - </li> - <li> - <em>searchSettingsDescription</em> - If provided, this provides a brief description - of the search suggestions that you provide to Quick Search Box, - and will be displayed in the search settings entry for your application. - </li> - <li> - <em>queryAfterZeroResults</em> - Indicates whether a source should be invoked for - supersets of queries it has returned zero results for in the past. For example, if a - source returned zero results for "bo", it would be ignored for "bob". If set to false, - this source will only be ignored for a single session; the next time the search dialog - is invoked, all sources will be queried. The default value is false. - </li> - <li> - <em>searchSuggestThreshold</em> - Indicates the minimum number of characters needed to - trigger a source lookup from Quick Search Box. Only guarantees that a source will not be - queried for anything shorter than the threshold. The default value is 0. - </li> - </ul> - - <p>To enable voice search for your Activity, you can add fields to the searchable metadata - that enable and configure voice search. The following are additional {@code <searchable>} - attributes for use when implementing voice search:</p> - - <ul> - <li> - <em>voiceSearchMode</em> - <strong>Required to provide voice search - capabilities</strong>. - If provided and non-zero, enables voice search. - (Voice search may not be provided by the device, in which case these flags will - have no effect.) The following mode values are accepted: - <ul> - <li> - <em>showVoiceSearchButton</em> - If set, display a voice search button. This only - takes effect if voice search is available on the device. If set, then "launchWebSearch" - or "launchRecognizer" must also be set. - </li> - <li> - <em>launchWebSearch</em> - If set, the voice search button will take the user directly - to a built-in voice web search activity. Most applications will not use this flag, as - it will take the user away from the activity in which search was invoked. - </li> - <li> - <em>launchRecognizer</em> - If set, the voice search button will take - the user directly to a built-in voice recording activity. This activity - will prompt the user to speak, transcribe the spoken text, and forward the resulting - query text to the searchable activity, just as if the user had typed it into the - search UI and clicked the search button. - </li> - </ul> - </li> - <li> - <em>voiceLanguageModel</em> - A string constant from - {@link android.speech.RecognizerIntent}. - If provided, this specifies the language model that - should be used by the voice recognition system. See - {@link android.speech.RecognizerIntent#EXTRA_LANGUAGE_MODEL } for more - information. If not provided, the default value - {@link android.speech.RecognizerIntent#LANGUAGE_MODEL_FREE_FORM } will be used. - </li> - <li> - <em>voicePromptText</em> - A string. If provided, this specifies a prompt - that will be displayed during voice input. If not provided, a default prompt - will be displayed. - </li> - <li> - <em>voiceLanguage</em> - A string constant from {@link java.util.Locale}. - If provided, this specifies the spoken language to be expected. - This is only needed if it is different from the current value of - {@link java.util.Locale#getDefault()}. - </li> - <li> - <em>voiceMaxResults</em> - If provided, enforces the maximum number of results to return, - including the "best" result which will always be provided as the SEARCH intent's primary - query. Must be one or greater. Use EXTRA_RESULTS to get the results from the intent. - If not provided, the recognizer will choose how many results to return. - </li> - </ul> - </dd> - - <dt> - <actionkey> - </dt> - <dd> - Defines a shortcut key for a search action. - <ul> - <li> - <em>keycode</em> - <strong>Required</strong>. This attribute denotes the action key - you wish to respond to. Note that not all action keys are actually supported using - this mechanism, as many of them are used for typing, - navigation, or system functions. This will be added to the - {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH} Intent that is passed to your - searchable Activity. To examine the key code, use - {@link android.content.Intent#getIntExtra getIntExtra(SearchManager.ACTION_KEY)}. - Note that, in addition to the keycode, you must also provide one or more of - the action specifier attributes below. - </li> - <li> - <em>queryActionMsg</em> - If you wish to handle an action key during normal - search query entry, you must define an action string here. This will be added to the - {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH} Intent that is - passed to your searchable Activity. To examine the string, use - {@link android.content.Intent#getStringExtra - getStringExtra(SearchManager.ACTION_MSG)}. - </li> - <li> - <em>suggestActionMsg</em> - If you wish to handle an action key while a - suggestion is being displayed and selected, there are two ways to handle this. - If all of your suggestions can handle the action key, you can simply define - the action message using this attribute. This will be added to the - {@link android.content.Intent#ACTION_SEARCH} Intent that is passed to your - searchable Activity. To examine the string, - use {@link android.content.Intent#getStringExtra - getStringExtra(SearchManager.ACTION_MSG)}. - </li> - <li> - <em>suggestActionMsgColumn</em> - If you wish to handle an action key while - a suggestion is being displayed and selected, but you do not wish to enable - this action key for every suggestion, then you can use this attribute to control - it on a suggestion-by-suggestion basis. First, you must define a column - (and name it here) where your suggestions will include the action string. Then, - in your content provider, you must provide this column, and when desired, - provide data in this column. The search manager will look at your suggestion cursor, - using the string provided here in order to select a column, and will use - that to select a string from the cursor. That string will be added to the - {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH} - Intent that is passed to your searchable Activity. To examine - the string, use {@link android.content.Intent#getStringExtra - getStringExtra(SearchManager.ACTION_MSG)}. If the data does not exist for the - selection suggestion, the action key will be ignored. - </li> - </ul> - </dd> -</dl> - - diff --git a/docs/html/guide/topics/resources/drawable-resource.jd b/docs/html/guide/topics/resources/drawable-resource.jd new file mode 100644 index 0000000..ec964ed --- /dev/null +++ b/docs/html/guide/topics/resources/drawable-resource.jd @@ -0,0 +1,711 @@ +page.title=Drawable Resources +parent.title=Resource Types +parent.link=available-resources.html +@jd:body + +<div id="qv-wrapper"> + <div id="qv"> + <h2>See also</h2> + <ol> + <li><a href="{@docRoot}guide/topics/graphics/2d-graphics.html">2D Graphics</a></li> + </ol> + </div> +</div> + +<p>A drawable resource is a general concept for a graphic that you +can retrieve with {@link android.content.res.Resources#getDrawable(int)} +and draw on the screen. There are several different types of drawables:</p> +<dl> + <dt><a href="#Bitmap">Bitmap File</a><dt> + <dd>A bitmap graphic file ({@code .png}, {@code .jpg}, or {@code .gif}). + A {@link android.graphics.drawable.BitmapDrawable}.</dd> + <dt><a href="#NinePatch">Nine-Patch File</a></dt> + <dd>A PNG file with stretchable regions to allow image resizing based on content ({@code +.9.png}). A {@link android.graphics.drawable.NinePatchDrawable}.</dd> +<!-- <dt><a href="#BitmapAlias">Bitmap Alias</a><dt> + <dd>An alias for a drawable.</dd> --> + <dt><a href="#StateList">State List</a></dt> + <dd>An XML file that references different bitmap graphics + for different states (for example, to use a different image when a button is pressed). + A {@link android.graphics.drawable.StateListDrawable}.</dd> + <dt><a href="#Color">Color</a></dt> + <dd>A resource defined in XML that specifies a rectangle of color, with + optionally rounded corners. A {@link android.graphics.drawable.PaintDrawable}.</dd> + <dt><a href="#Shape">Shape</a></dt> + <dd>An XML file that defines a geometric shape, including colors and gradients. + A {@link android.graphics.drawable.ShapeDrawable}.</dd> +</dl> + +<p>Documentation for the {@link android.graphics.drawable.AnimationDrawable} resource +is in the <a href="animation-resource.html">Animation Resource</a> document.</p> + +<h2 id="Bitmap">Bitmap File</h2> + +<p>A basic bitmap image. Android supports basic bitmap files in a few different formats: +{@code .png} (preferred), {@code .jpg} (acceptable), {@code .gif} (discouraged).</p> + +<p class="note"><strong>Note:</strong> Bitmap files may be automatically optimized with lossless +image compression by the <a href="{@docRoot}guide/developing/tools/aapt.html">aapt</a> tool. For +example, a true-color PNG that does not require more than 256 colors may be converted to an 8-bit +PNG with a color palette. This will result in an image of equal quality but which requires less +memory. So be aware that the image binaries placed in this directory can change during the build. If +you plan on reading an image as a bit stream in order to convert it to a bitmap, put your images in +the <code>res/raw/</code> folder instead, where they will not be optimized.</p> + +<dl class="xml"> + +<dt>file location:</dt> +<dd><code>res/drawable/<em>filename</em>.png</code> ({@code .png}, {@code .jpg}, or {@code .gif})<br/> +The filename will be used as the resource ID.</dd> + +<dt>compiled resource datatype:</dt> +<dd>Resource pointer to a {@link android.graphics.drawable.BitmapDrawable}.</dd> + +<dt>resource reference:</dt> +<dd> +In Java: <code>R.drawable.<em>filename</em></code></li><br/> +In XML: <code>@[<em>package</em>:]drawable/<em>filename</em></code> +</dd> + +<dt>example:</dt> +<dd>With an image saved at <code>res/drawable/myimage.png</code>, this layout XML will apply +the image to a View: +<pre> +<ImageView + android:layout_height="wrap_content" + android:layout_width="wrap_content" + <strong>android:src="@drawable/myimage"</strong> /> +</pre> +<p>This application code will retrieve the image as a {@link +android.graphics.drawable.Drawable}:</p> +<pre> +Resources res = {@link android.content.Context#getResources()}; +Drawable drawable = res.{@link android.content.res.Resources#getDrawable(int) getDrawable}(R.drawable.myimage); +</pre> +</dd> + +<dt>see also:</dt> +<dd> +<ul> + <li><a href="{@docRoot}guide/topics/graphics/2d-graphics.html">2D Graphics</a></li> + <li>{@link android.graphics.drawable.BitmapDrawable}</li> +</ul> +</dd> + +</dl> + + + + + + + +<h2 id="NinePatch">Nine-Patch File</h2> + +<p>A {@link android.graphics.NinePatch} is a PNG image in which you can define stretchable regions +that Android will scale when content within the View exceeds the normal image bounds. You will +typically assign this type of image as the background of a View that has at least one dimension set +to {@code "wrap_content"}, and when the View grows to accomodate the content, the Nine-Patch image +will also be scaled to match the size of the View. An example use of a Nine-Patch image is the +background used by Android's standard {@link android.widget.Button} widget, which must stretch to +accommodate the text (or image) inside the button.</p> + +<p>For a complete discussion about how to define a Nine-Patch file with stretchable regions, +see the <a href="{@docRoot}guide/topics/graphics/2d-graphics.html#nine-patch">2D Graphics</a> +document.</p> + +<dl class="xml"> + +<dt>file location:</dt> +<dd><code>res/drawable/<em>filename</em>.9.png</code><br/> +The filename will be used as the resource ID.</dd> + +<dt>compiled resource datatype:</dt> +<dd>Resource pointer to a {@link android.graphics.drawable.NinePatchDrawable}.</dd> + +<dt>resource reference:</dt> +<dd> +In Java: <code>R.drawable.<em>filename</em></code><br/> +In XML: <code>@[<em>package</em>:]drawable/<em>filename</em></code> +</dd> + +<dt>example:</dt> +<dd>With an image saved at <code>res/drawable/myninepatch.9.png</code>, this layout XML will +apply the Nine-Patch to a View: +<pre> +<Button + android:layout_height="wrap_content" + android:layout_width="wrap_content" + <strong>android:background="@drawable/myninepatch"</strong> /> +</pre> +</dd> + +<dt>see also:</dt> +<dd> +<ul> + <li><a href="{@docRoot}guide/topics/graphics/2d-graphics.html#nine-patch">2D Graphics</a></li> + <li>{@link android.graphics.drawable.NinePatchDrawable}</li> +</ul> +</dd> + +</dl> + + + + + + + + +<h2 id="StateList">State List</h2> + +<p>A {@link android.graphics.drawable.StateListDrawable} is a drawable object defined in XML +that uses a several different images to represent the same graphic, depending on the state of +the object. For example, a {@link +android.widget.Button} widget can exist in one of several different states (pressed, focused, +or niether) and, using a state list drawable, you can provide a different button image for each +state.</p> + +<p>You can describe the state list in an XML file. Each graphic is represented by an {@code +<item>} element inside a single {@code <selector>} element. Each {@code <item>} +uses various attributes to describe the state in which it should be used as the graphic for the +drawable.</p> +<p>During each state change, the state list is traversed top to bottom and the first item that +matches the current state will be used—the selection is <em>not</em> based on the "best +match," but simply the first item that meets the minimum criteria of the state.</p> + +<dl class="xml"> + +<dt>file location:</dt> +<dd><code>res/drawable/<em>filename</em>.xml</code><br/> +The filename will be used as the resource ID.</dd> + +<dt>compiled resource datatype:</dt> +<dd>Resource pointer to a {@link android.graphics.drawable.StateListDrawable}.</dd> + +<dt>resource reference:</dt> +<dd> +In Java: <code>R.drawable.<em>filename</em></code><br/> +In XML: <code>@[<em>package</em>:]drawable/<em>filename</em></code> +</dd> + +<dt>syntax:</dt> +<dd> +<pre class="stx"> +<?xml version="1.0" encoding="utf-8"?> +<<a href="#selector-element">selector</a> xmlns:android="http://schemas.android.com/apk/res/android" + android:constantSize=["true" | "false"] + android:dither=["true" | "false"] + android:variablePadding=["true" | "false"] > + <<a href="#item-element">item</a> + android:drawable="@[package:]drawable/<em>drawable_resource</em>" + android:state_pressed=["true" | "false"] + android:state_focused=["true" | "false"] + android:state_selected=["true" | "false"] + android:state_active=["true" | "false"] + android:state_checkable=["true" | "false"] + android:state_checked=["true" | "false"] + android:state_enabled=["true" | "false"] + android:state_window_focused=["true" | "false"] /> +</selector> +</pre> +</dd> + +<dt>elements:</dt> +<dd> +<dl class="tag-list"> + + <dt id="selector-element"><code><selector></code></dt> + <dd><strong>Required.</strong> This must be the root element. Contains one or more {@code +<item>} elements. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>xmlns:android</code></dt> + <dd><em>String</em>. <strong>Required.</strong> Defines the XML namespace, which must be + <code>"http://schemas.android.com/apk/res/android"</code>. + <dt><code>android:constantSize</code></dt> + <dd><em>Boolean</em>. "true" if the drawable's reported internal size will remain constant as the state +changes (the size will be the maximum of all of the states); "false" if the size will vary based on +the current state. Default is false.</dd> + <dt><code>android:dither</code></dt> + <dd><em>Boolean</em>. "true" to enable dithering of the bitmap if the bitmap does not have the same pixel +configuration as the screen (for instance, an ARGB 8888 bitmap with an RGB 565 screen); "false" to +disable dithering. Default is true.</dd> + <dt><code>android:variablePadding</code></dt> + <dd><em>Boolean</em>. "true" if the drawable's padding should change based on the current +state that is selected; "false" if the padding should stay the same (based on the maximum +padding of all the states). Enabling this feature requires that you deal with +performing layout when the state changes, which is often not supported. Default is false.</dd> + </dl> + </dd> + <dt id="item-element"><code><item></code></dt> + <dd>Defines a drawable to use during certain states, as described by its attributes. Must be a +child of a <code><selector></code> element. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>android:drawable</code></dt> + <dd><em>Drawable resource</em>. <strong>Required</strong>. Reference to a drawable resource.</dd> + <dt><code>android:state_pressed</code></dt> + <dd><em>Boolean</em>. "true" if this item should be used when the object is pressed (such as when a button +is touched/clicked); "false" if this item should be used in the default, non-pressed state.</dd> + <dt><code>android:state_focused</code></dt> + <dd><em>Boolean</em>. "true" if this item should be used when the object is focused (such as when a button +is highlighted using the trackball/d-pad); "false" if this item should be used in the default, +non-focused state.</dd> + <dt><code>android:state_selected</code></dt> + <dd><em>Boolean</em>. "true" if this item should be used when the object is selected (such as when a +tab is opened); "false" if this item should be used when the object is not selected.</dd> + <dt><code>android:state_checkable</code></dt> + <dd><em>Boolean</em>. "true" if this item should be used when the object is checkable; "false" if this +item should be used when the object is not checkable. (Only useful if the object can +transition between a checkable and non-checkable widget.)</dd> + <dt><code>android:state_checked</code></dt> + <dd><em>Boolean</em>. "true" if this item should be used when the object is checked; "false" if it +should be used when the object is un-checked.</dd> + <dt><code>android:state_enabled</code></dt> + <dd><em>Boolean</em>. "true" if this item should be used when the object is enabled (capable of +receiving touch/click events); "false" if it should be used when the object is disabled.</dd> + <dt><code>android:state_window_focused</code></dt> + <dd><em>Boolean</em>. "true" if this item should be used when the application window has focus (the +application is in the foreground), "false" if this item should be used when the application +window does not have focus (for example, if the notification shade is pulled down or a dialog appears).</dd> + </dl> + <p class="note"><strong>Note:</strong>Remember that the first item in the state list that +matches the current state of the object will be applied. So if the first item in the list contains +none of the state attributes above, then it will be applied every time, which is why your +default value should always be last (as demonstrated in the following example).</p> + </dd> + +</dl> +</dd> <!-- end elements and attributes --> + +<dt>example:</dt> +<dd>XML file saved at <code>res/drawable/button.xml</code>: +<pre> +<?xml version="1.0" encoding="utf-8"?> +<selector xmlns:android="http://schemas.android.com/apk/res/android"> + <item android:state_pressed="true" + android:drawable="@drawable/button_pressed" /> <!-- pressed --> + <item android:state_focused="true" + android:drawable="@drawable/button_focused" /> <!-- focused --> + <item android:drawable="@drawable/button_normal" /> <!-- default --> +</selector> +</pre> + +<p>This layout XML will apply the drawable to a View:</p> +<pre> +<ImageView + android:layout_height="wrap_content" + android:layout_width="wrap_content" + <strong>android:src="@drawable/button"</strong> /> +</pre> +</dd> <!-- end example --> + +<dt>see also:</dt> +<dd> +<ul> + <li>{@link android.graphics.drawable.StateListDrawable}</li> +</ul> +</dd> + +</dl> + + + + + + + + + + + + + + +<h2 id="Color">Color</h2> + +<p>This is a color defined in XML that's used as a drawable to fill a rectangular space, +with optionally rounded corners. This kind of drawable behaves like a color fill.</p> + +<p class="note"><strong>Note:</strong> A color drawable is a simple resource that is referenced +using the value provided in the {@code name} attribute (not the name of the XML file). As +such, you can combine a color drawable resources with other simple resources in the one XML file, +under one {@code <resources>} element.</p> + + +<dl class="xml"> + +<dt>file location:</dt> +<dd><code>res/drawable/<em>filename</em>.png</code><br/> +The filename is arbitrary. The element's {@code name} will be used as the resource ID.</dd> + +<dt>compiled resource datatype:</dt> +<dd>Resource pointer to a {@link android.graphics.drawable.PaintDrawable}.</dd> + +<dt>resource reference:</dt> +<dd> +In Java: <code>R.drawable.<em>color_name</em></code><br/> +In XML: <code>@[<em>package</em>:]drawable/<em>color_name</em></code> +</dd> + +<dt>syntax:</dt> +<dd> +<pre class="stx"> +<?xml version="1.0" encoding="utf-8"?> +<<a href="#color-resources-element">resources</a>> + <<a href="#drawable-element">drawable</a> + name="<em>color_name</em>" + ><em>color</em></drawable> +</resources> +</pre> +</dd> + +<dt>elements:</dt> +<dd> +<dl class="tag-list"> + + <dt id="color-resources-element"><code><resources></code></dt> + <dd><strong>Required.</strong> This must be the root node. + <p>No attributes.</p> + </dd> + <dt id="drawable-element"><code><drawable></code></dt> + <dd>A color to use as a drawable rectangle. The value can be + any valid hexadecimal color value or a <a href="more-resources.html#Color">color + resource</a>. A color value always begins with a pound (#) character, followed + by the Alpha-Red-Green-Blue information in one of the following formats: + #<em>RGB</em>, #<em>RRGGBB</em>, #<em>ARGB</em>, or #<em>AARRGGBB</em>. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>name</code></dt> + <dd><em>String</em>. <strong>Required</strong>. + A name for the color. This name will be used as the resource ID.</dd> + </dl> + + </dd> + +</dl> +</dd> <!-- end elements and attributes --> + +<dt>example:</dt> +<dd>XML file saved at <code>res/drawable/colors.xml</code>: +<pre> +<?xml version="1.0" encoding="utf-8"?> +<resources> + <drawable name="solid_red">#f00</drawable> + <drawable name="solid_blue">#0000ff</drawable> +</resources> +</pre> + <p>This layout XML will apply a color drawable to a View:</p> +<pre> +<TextView + android:layout_width="fill_parent" + android:layout_height="wrap_content" + <strong>android:background="@drawable/solid_blue"</strong> /> +</pre> + <p>This application code will get a color drawable and apply it to a View:</p> +<pre> +Resources res = {@link android.content.Context#getResources()}; +Drawable redDrawable = res.{@link android.content.res.Resources#getDrawable(int) getDrawable}(R.drawable.solid_red); + +TextView tv = (TextView) findViewByID(R.id.text); +tv.setBackground(redDrawable); +</pre> +</dd> <!-- end example --> + +<dt>see also:</dt> +<dd> +<ul> + <li>{@link android.graphics.drawable.PaintDrawable}</li> +</ul> +</dd> + +</dl> + + + + + + + + + + + + +<h2 id="Shape">Shape</h2> + +<p>This is a generic shape defined in XML.</p> + +<dl class="xml"> + +<dt>file location:</dt> +<dd><code>res/drawable/<em>filename</em>.xml</code><br/> +The filename will be used as the resource ID.</dd> + +<dt>compiled resource datatype:</dt> +<dd>Resource pointer to a {@link android.graphics.drawable.ShapeDrawable}.</dd> + +<dt>resource reference:</dt> +<dd> +In Java: <code>R.drawable.<em>filename</em></code><br/> +In XML: <code>@[<em>package</em>:]drawable/<em>filename</em></code> +</dd> + +<dt>syntax:</dt> +<dd> +<pre class="stx"> +<?xml version="1.0" encoding="utf-8"?> +<<a href="#shape-element">shape</a> xmlns:android="http://schemas.android.com/apk/res/android" + android:shape=["rectangle" | "oval" | "line" | "ring"] > + <<a href="#gradient-element">gradient</a> + android:angle="<em>integer</em>" + android:centerX="<em>integer</em>" + android:centerY="<em>integer</em>" + android:centerColor="<em>integer</em>" + android:gradientRadius="<em>integer</em>" + android:type="" + android:usesLevel="" + android:startColor="<em>color</em>" + android:endColor="<em>color</em>" /> + <<a href="#solid-element">solid</a> + android:color="<em>color</em>" /> + <<a href="#stroke-element">stroke</a> + android:width="<em>integer</em>" + android:color="<em>color</em>" + android:dashWidth="<em>integer</em>" + android:dashGap="<em>integer</em>" /> + <<a href="#padding-element">padding</a> + android:left="<em>integer</em>" + android:top="<em>integer</em>" + android:right="<em>integer</em>" + android:bottom="<em>integer</em>" /> + <<a href="#corners-element">corners</a> + android:radius="<em>integer</em>" + android:topLeftRadius="<em>integer</em>" + android:topRightRadius="<em>integer</em>" + android:bottomLeftRadius="<em>integer</em>" + android:bottomRightRadius="<em>integer</em>" /> +</shape> +</pre> +</dd> + +<dt>elements:</dt> +<dd> +<dl class="tag-list"> + + <dt id="shape-element"><code><shape></code></dt> + <dd><strong>Required.</strong> This must be the root element. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>android:shape</code></dt> + <dd><em>Keyword</em>. Defines the type of shape. Valid values are: + <table> + <tr><th>Value</th><th>Desciption</th></tr> + <tr><td>{@code "rectangle"}</td> + <td>A rectangle that fills the containing View. This is the default shape.</td></tr> + <tr><td>{@code "oval"}</td> + <td>An oval shape that fits the dimensions of the containing View.</td></tr> + <tr><td>{@code "line"}</td> + <td>A horizontal line that spans the width of the containing View. This + shape requires the {@code <stroke>} element to define the width of the + line.</td></tr> + <tr><td>{@code "ring"}</td> + <td>A ring shape.</td></tr> + </table> + </dd> + </dl> + <p>The following attributes are used only when {@code android:shape="ring"}:</p> + <dl class="atn-list"> + <dt><code>android:innerRadius</code></dt> + <dd><em>Dimension</em>. The radius for the +inner part of the ring (the hole in the middle), as a dimension value or <a +href="more-resources.html#Dimension">dimension resource</a>.</dd> + <dt><code>android:innerRadiusRatio</code></dt> + <dd><em>Float</em>. The radius for the inner +part of the ring, expressed as a ratio of the ring's width. For instance, if {@code +android:innerRadiusRatio="5"}, then the inner radius equals the ring's width divided by 5. This +value will be overridden by {@code android:innerRadius}. Default value is 9.</dd> + <dt><code>android:thickness</code></dt> + <dd><em>Dimension</em>. The thickness of the +ring, as a dimension value or <a +href="more-resources.html#Dimension">dimension resource</a>.</dd> + <dt><code>android:thicknessRatio</code></dt> + <dd><em>Float</em>. The thickness of the ring, +expressed as a ratio of the ring's width. For instance, if {@code android:thicknessRatio="2"}, then +the thickness equals the ring's width divided by 2. This value will be overridden by {@code +android:innerRadius}. Default value is 3.</dd> + <dt><code>android:useLevel</code></dt> + <dd><em>Boolean</em>. "true" if this is used as +a {@link android.graphics.drawable.LevelListDrawable}. This should normally be "false" + or your shape may not appear.</dd> + </dl> + <dt id="gradient-element"><code><gradient></code></dt> + <dd>Specifies a gradient color for the shape. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>android:type</code></dt> + <dd><em>Keyword</em>. The type of gradient pattern to apply. Valid values are: + <table> + <tr><th>Value</th><th>Description</th></tr> + <tr><td>{@code "linear"}</td> + <td>A linear gradient. This is the default.</td></tr> + <tr><td>{@code "radial"}</td> + <td>A radial gradient. The start color is the center color.</td></tr> + <tr><td>{@code "sweep"}</td> + <td>A sweeping line gradient. </td></tr> + </table> + </dd> + <dt><code>android:angle</code></dt> + <dd><em>Integer</em>. The angle for the gradient, in degrees. 0 is left to right, 90 is +bottom to top. It must be a multiple of 45. Default is 0.</dd> + <dt><code>android:centerX</code></dt> + <dd><em>Float</em>. The relative X-position for the center of the gradient (0 - 1.0). +Does not apply when {@code android:type="linear"}.</dd> + <dt><code>android:centerY</code></dt> + <dd><em>Float</em>. The relative Y-position for the center of the gradient (0 - 1.0). +Does not apply when {@code android:type="linear"}.</dd> + <dt><code>android:centerColor</code></dt> + <dd><em>Color</em>. Optional color that comes between the start and end colors, as a +hexadecimal value or <a href="more-resources.html#Color">color resource</a>.</dd> + <dt><code>android:gradientRadius</code></dt> + <dd><em>Float</em>. The radius for the gradient. Only applied when {@code +android:type="radial"}.</dd> + <dt><code>android:useLevel</code></dt> + <dd><em>Boolean</em>. "true" if this is used as a {@link +android.graphics.drawable.LevelListDrawable}.</dd> + <dt><code>android:startColor</code></dt> + <dd><em>Color</em>. The starting color, as a hexadecimal +value or <a href="more-resources.html#Color">color resource</a>.</dd> + <dt><code>android:endColor</code></dt> + <dd><em>Color</em>. The ending color, as a hexadecimal +value or <a href="more-resources.html#Color">color resource</a>.</dd> + </dl> + </dd> + <dt id="solid-element"><code><solid></code></dt> + <dd>A solid color to fill the shape. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>android:color</code></dt> + <dd><em>Color</em>. The color to apply to the shape, as a hexadecimal +value or <a href="more-resources.html#Color">color resource</a>.</dd> + </dl> + </dd> + <dt id="stroke-element"><code><stroke></code></dt> + <dd>A stroke line for the shape. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>android:width</code></dt> + <dd><em>Dimension</em>. The thickness of the line, as a dimension value or <a +href="more-resources.html#Dimension">dimension resource</a>.</dd> + <dt><code>android:color</code></dt> + <dd><em>Color</em>. The color of the line, as a +hexadecimal value or <a href="more-resources.html#Color">color resource</a>.</dd> + <dt><code>android:dashGap</code></dt> + <dd><em>Dimension</em>. The distance between line dashes, as a dimension value or <a +href="more-resources.html#Dimension">dimension resource</a>. Only valid if {@code +android:dashWidth} is set.</dd> + <dt><code>android:dashWidth</code></dt> + <dd><em>Dimension</em>. The size of each dash line, as a dimension value or <a +href="more-resources.html#Dimension">dimension resource</a>. Only valid if {@code +android:dashGap} is set.</dd> + </dl> + </dd> + <dt id="padding-element"><code><padding></code></dt> + <dd>Padding to apply to the containing View element (this pads the position of the View +content, not the shape). + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>android:left</code></dt> + <dd><em>Dimension</em>. Left padding, as a dimension value or <a +href="more-resources.html#Dimension">dimension resource</a>.</dd> + <dt><code>android:top</code></dt> + <dd><em>Dimension</em>. Top padding, as a dimension value or <a +href="more-resources.html#Dimension">dimension resource</a>.</dd> + <dt><code>android:right</code></dt> + <dd><em>Dimension</em>. Right padding, as a dimension value or <a +href="more-resources.html#Dimension">dimension resource</a>.</dd> + <dt><code>android:bottom</code></dt> + <dd><em>Dimension</em>. Bottom padding, as a dimension value or <a +href="more-resources.html#Dimension">dimension resource</a>.</dd> + </dl> + </dd> + <dt id="corners-element"><code><corners></code></dt> + <dd>Creates rounded corners for the shape. Applies only when the shape is a rectangle. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>android:radius</code></dt> + <dd><em>Dimension</em>. The radius for all corners, as a dimension value or <a +href="more-resources.html#Dimension">dimension resource</a>. This will be overridden for each +corner by the following attributes.</dd> + <dt><code>android:topLeftRadius</code></dt> + <dd><em>Dimension</em>. The radius for the top-left corner, as a dimension value or <a +href="more-resources.html#Dimension">dimension resource</a>.</dd> + <dt><code>android:topRightRadius</code></dt> + <dd><em>Dimension</em>. The radius for the top-right corner, as a dimension value or <a +href="more-resources.html#Dimension">dimension resource</a>.</dd> + <dt><code>android:bottomLeftRadius</code></dt> + <dd><em>Dimension</em>. The radius for the bottom-left corner, as a dimension value or <a +href="more-resources.html#Dimension">dimension resource</a>.</dd> + <dt><code>android:bottomRightRadius</code></dt> + <dd><em>Dimension</em>. The radius for the bottom-right corner, as a dimension value or <a +href="more-resources.html#Dimension">dimension resource</a>.</dd> + </dl> + <p class="note"><strong>Note:</strong> Every corner must (initially) be provided a corner +radius greater than zero, or else no corners will be rounded. If you want specific corners +to <em>not</em> be rounded, a work-around is to use {@code android:radius} to set a default corner +radius greater than zero, but then override each and every corner with the values you really +want, providing zero ("0dp") where you don't want rounded corners.</p> + </dd> + +</dl> +</dd> <!-- end elements and attributes --> + +<dt>example:</dt> +<dd>XML file saved at <code>res/drawable/gradient_box.xml</code>: +<pre> +<?xml version="1.0" encoding="utf-8"?> +<shape xmlns:android="http://schemas.android.com/apk/res/android" + android:shape="rectangle"> + <gradient + android:startColor="#FFFF0000" + android:endColor="#80FF00FF" + android:angle="45"/> + <padding android:left="7dp" + android:top="7dp" + android:right="7dp" + android:bottom="7dp" /> + <corners android:radius="8dp" /> +</shape> +</pre> + <p>This layout XML will apply the shape drawable to a View:</p> +<pre> +<TextView + android:background="@drawable/gradient_box" + android:layout_height="wrap_content" + android:layout_width="wrap_content" /> +</pre> + <p>This application code will get the shape drawable and apply it to a View:</p> +<pre> +Resources res = {@link android.content.Context#getResources()}; +Drawable shape = res. {@link android.content.res.Resources#getDrawable(int) getDrawable}(R.drawable.gradient_box); + +TextView tv = (TextView)findViewByID(R.id.textview); +tv.setBackground(shape); +</pre> +</dd> <!-- end example --> + +</dl> + + + + + + + + + + + + + diff --git a/docs/html/guide/topics/resources/index.jd b/docs/html/guide/topics/resources/index.jd index 1425cfa..f602a04 100644 --- a/docs/html/guide/topics/resources/index.jd +++ b/docs/html/guide/topics/resources/index.jd @@ -1,40 +1,104 @@ -page.title=Resources and Assets +page.title=Application Resources @jd:body <div id="qv-wrapper"> <div id="qv"> + <h2>Topics</h2> + <ol> + <li><a href="providing-resources.html">Providing Resources</a></li> + <li><a href="accessing-resources.html">Accessing Resources</a></li> + <li><a href="runtime-changes.html">Handling Runtime Changes</a></li> + <li><a href="localization.html">Localization</a></li> + </ol> - <h2>Key classes</h2> + <h2>Reference</h2> <ol> - <li>{@link android.content.res.Resources}</li> - <li>{@link android.content.res.AssetManager}</li> + <li><a href="available-resources.html">Resource Types</a></li> </ol> +</div> +</div> + +<p>You should always externalize resources such as images and strings from your application +code, so that you can maintain them independently. Externalizing your +resources also allows you to provide alternative resources that support specific device +configurations such as different languages or screen sizes, which becomes increasingly +important as more Android-powered devices become available with different configurations. In order +to provide this functionality, you must organize resources in your project's {@code res/} +directory, using various sub-directories that group resources by type and configuration.</p> + +<div class="figure" style="width:441px"> +<img src="{@docRoot}images/resources/resource_devices_diagram1.png" height="137" alt="" /> +<p class="img-caption"> +<strong>Figure 1.</strong> Two device configurations, both using default +resources.</p> </div> + +<div class="figure" style="width:441px"> +<img src="{@docRoot}images/resources/resource_devices_diagram2.png" height="137" alt="" /> +<p class="img-caption"> +<strong>Figure 2.</strong> Two device configurations, one using alternative +resources.</p> </div> -<p>Resources are an integral part of an Android application. -In general, these are external elements that you want to include and reference within your application, -like images, audio, video, text strings, layouts, themes, etc. Every Android application contains -a directory for resources (<code>res/</code>) and a directory for assets (<code>assets/</code>). -Assets are used less often, because their applications are far fewer. You only need to save data -as an asset when you need to read the raw bytes. The directories for resources and assets both -reside at the top of an Android project tree, at the same level as your source code directory -(<code>src/</code>).</p> - -<p>The difference between "resources" and "assets" isn't much on the surface, but in general, -you'll use resources to store your external content much more often than you'll use assets. -The real difference is that anything -placed in the resources directory will be easily accessible from your application from the -<code>R</code> class, which is compiled by Android. Whereas, anything placed in the assets -directory will maintain its raw file format and, in order to read it, you must use the -{@link android.content.res.AssetManager} to read the file as a stream of bytes. So keeping -files and data in resources (<code>res/</code>) makes them easily accessible.</p> - -<p>Within the documents of this topic, you'll find information on the kinds of standard resources -that are typically used in an Android application and how to reference them from you code. -<a href="{@docRoot}guide/topics/resources/resources-i18n.html">Resources and Internationalization</a> -is where you should start, to learn more about how Android utilizes project resources. Then, the -<a href="{@docRoot}guide/topics/resources/available-resources.html">Available Resource Types</a> -document offers a summary of various resource types and a reference to their specifications. -</p> +<p>For any type of resource, you can specify <em>default</em> and multiple +<em>alternative</em> resources for your application:</p> +<ul> + <li>Default resources are those that should be used regardless of +the device configuration or when there are no alternative resources that match the current +configuration.</li> + <li>Alternative resources are those that you've designed for use with a specific +configuration. To specify that a group of resources are for a specific configuration, +append an appropriate configuration qualifier to the directory name.</li> +</ul> + +<p>For example, while your default UI +layout is saved in the {@code res/layout/} directory, you might specify a different UI layout to +be used when the screen is in landscape orientation, by saving it in the {@code res/layout-land/} +directory. The Android system will automatically apply the appropriate resources by matching the +device's current configuration to your resource directory names.</p> + +<p>Figure 1 demonstrates how a collection of default resources from an application will be applied +to two different devices when there are no alternative resources available. Figure 2 shows +the same application with a set of alternative resources that qualify for one of the device +configurations, thus, the two devices uses different resources.</p> + +<p>The information above is just an introduction to how application resources work on Android. +The following documents provide a complete guide to how you can organize your application resources, +specify alternative resources, access them in your application, and more:</p> + +<dl> + <dt><strong><a href="providing-resources.html">Providing Resources</a></strong></dt> + <dd>What kinds of resources you can provide in your app, where to save them, and how to create +alternative resources for specific device configurations.</dd> + <dt><strong><a href="accessing-resources.html">Accessing Resources</a></strong></dt> + <dd>How to use the resources you've provided, either by referencing them from your application +code or from other XML resources.</dd> + <dt><strong><a href="runtime-changes.html">Handling Runtime Changes</a></strong></dt> + <dd>How to manage configuration changes that occur while your Activity is running.</dd> + <dt><strong><a href="localization.html">Localization</a></strong></dt> + <dd>A bottom-up guide to localizing your application using alternative resources. While this is +just one specific use of alternative resources, it is very important in order to reach more +users.</dd> + <dt><strong><a href="available-resources.html">Resource Types</a></strong></dt> + <dd>A reference of various resource types you can provide, describing their XML elements, +attributes, and syntax. For example, this reference shows you how to create a resource for +application menus, drawables, animations, and more.</dd> +</dl> + +<!-- +<h2>Raw Assets</h2> + +<p>An alternative to saving files in {@code res/} is to save files in the {@code +assets/} directory. This should only be necessary if you need direct access to original files and +directories by name. Files saved in the {@code assets/} directory will not be given a resource +ID, so you can't reference them through the {@code R} class or from XML resources. Instead, you can +query data in the {@code assets/} directory like an ordinary file system, search through the +directory and +read raw data using {@link android.content.res.AssetManager}. For example, this can be more useful +when dealing with textures for a game. However, if you only need to read raw data from a file +(such as a video or audio file), then you should save files into the {@code res/raw/} directory and +then read a stream of bytes using {@link android.content.res.Resources#openRawResource(int)}. This +is uncommon, but if you need direct access to original files in {@code assets/}, refer to the {@link +android.content.res.AssetManager} documentation.</p> +--> diff --git a/docs/html/guide/topics/resources/layout-resource.jd b/docs/html/guide/topics/resources/layout-resource.jd new file mode 100644 index 0000000..a6b177f --- /dev/null +++ b/docs/html/guide/topics/resources/layout-resource.jd @@ -0,0 +1,224 @@ +page.title=Layout Resource +parent.title=Resource Types +parent.link=available-resources.html +@jd:body + +<div id="qv-wrapper"> + <div id="qv"> + <h2>See also</h2> + <ol> + <li><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a></li> + </ol> + </div> +</div> + +<p>A layout resource defines the architecture for the UI in an Activity or a component of a UI.</p> + + +<dl class="xml"> + +<dt>file location:</dt> +<dd><code>res/layout/<em>filename</em>.xml</code><br/> +The filename will be used as the resource ID.</dd> + +<dt>compiled resource datatype:</dt> +<dd>Resource pointer to a {@link android.view.View} (or subclass) resource.</dd> + +<dt>resource reference:</dt> +<dd> +In Java: <code>R.layout.<em>filename</em></code><br/> +In XML: <code>@[<em>package</em>:]layout/<em>filename</em></code> +</dd> + +<dt>syntax:</dt> +<dd> +<pre class="stx"> +<?xml version="1.0" encoding="utf-8"?> +<<a href="#viewgroup-element"><em>ViewGroup</em></a> xmlns:android="http://schemas.android.com/apk/res/android" + android:id="@+id/<em>name</em>" + android:layout_height="@+id/<em>string_name</em>" + android:layout_width="@+id/<em>string_name</em>" + [<em>other attributes</em>] > + <<a href="#view-element"><em>View</em></a> + android:id="@+id/<em>name</em>" + android:layout_height="@+id/<em>string_name</em>" + android:layout_width="@+id/<em>string_name</em>" + [<em>other attributes</em>] > + <<a href="#requestfocus-element">requestFocus</a>/> + </<em>View</em>> + <<a href="#viewgroup-element"><em>ViewGroup</em></a> > + <<a href="#view-element"><em>View</em></a> /> + </<em>ViewGroup</em>> +</<em>ViewGroup</em>> +</pre> +<p class="note"><strong>Note:</strong> The root element can be either a +{@link android.view.ViewGroup} or a {@link android.view.View}, but there must be only +one root element and it must contain the {@code xmlns:android} attribute with the {@code android} +namespace as shown.</p> +</dd> + +<dt>elements:</dt> +<dd> + <dl class="tag-list"> + + <dt id="viewgroup-element"><code><ViewGroup></code></dt> + <dd>A container for other {@link android.view.View} elements. There are many + different kinds of {@link android.view.ViewGroup} objects and each one lets you + specify the layout of the child elements in different ways. Different kinds of + {@link android.view.ViewGroup} objects include {@link android.widget.LinearLayout}, + {@link android.widget.RelativeLayout}, and {@link android.widget.FrameLayout}. + <p>You should not assume that any derivation of {@link android.view.ViewGroup} + will accept nested {@link android.view.View}s. Some {@link android.view.ViewGroup}s + are implementations of the {@link android.widget.AdapterView} class, which determines + its children only from an {@link android.widget.Adapter}.</p> + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>android:id</code></dt> + <dd><em>Resource name</em>. A unique resource name for the element, which you can +use to obtain a reference to the {@link android.view.ViewGroup} from your application. + The value takes the form: <code>"@+id/<em>name</em>"</code>. See more about the + <a href="#idvalue">value for {@code android:id}</a> below. + </dd> + <dt><code>android:layout_height</code></dt> + <dd><em>Dimension or keyword</em>. <strong>Required</strong>. The height for the group, as a +dimension value (or <a +href="more-resources.html#Dimension">dimension resource</a>) or a keyword ({@code "fill_parent"} +or {@code "wrap_content"}). See the <a href="#layoutvalues">valid values</a> below. + </dd> + <dt><code>android:layout_width</code></dt> + <dd><em>Dimension or keyword</em>. <strong>Required</strong>. The width for the group, as a +dimension value (or <a +href="more-resources.html#Dimension">dimension resource</a>) or a keyword ({@code "fill_parent"} +or {@code "wrap_content"}). See the <a href="#layoutvalues">valid values</a> below. + </dd> + </dl> + <p>More attributes are supported by the {@link android.view.ViewGroup} + base class, and many more are supported by each implementation of + {@link android.view.ViewGroup}. For a reference of all available attributes, + see the corresponding reference documentation for the {@link android.view.ViewGroup} class +(for example, the <a + href="{@docRoot}reference/android/widget/LinearLayout#lattrs">LinearLayout XML attributes</a>).</p> + </dd> + <dt id="view-element"><code><View></code></dt> + <dd>An individual UI component, generally referred to as a "widget". Different + kinds of {@link android.view.View} objects include {@link android.widget.TextView}, + {@link android.widget.Button}, and {@link android.widget.CheckBox}. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>android:id</code></dt> + <dd><em>Resource name</em>. A unique resource name for the element, which you can use to + obtain a reference to the {@link android.view.View} from your application. + The value takes the form: <code>"@+id/<em>name</em>"</code>. See more about the + <a href="#idvalue">value for {@code android:id}</a> below. + </dd> + <dt><code>android:layout_height</code></dt> + <dd><em>Dimension or keyword</em>. <strong>Required</strong>. The height for the element, as +a dimension value (or <a +href="more-resources.html#Dimension">dimension resource</a>) or a keyword ({@code "fill_parent"} +or {@code "wrap_content"}). See the <a href="#layoutvalues">valid values</a> below. + </dd> + <dt><code>android:layout_width</code></dt> + <dd><em>Dimension or keyword</em>. <strong>Required</strong>. The width for the element, as +a dimension value (or <a +href="more-resources.html#Dimension">dimension resource</a>) or a keyword ({@code "fill_parent"} +or {@code "wrap_content"}). See the <a href="#layoutvalues">valid values</a> below. + </dd> + </dl> + <p>More attributes are supported by the {@link android.view.View} + base class, and many more are supported by each implementation of + {@link android.view.View}. Read <a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring + Layout</a> for more information. For a reference of all available attributes, + see the corresponding reference documentation (for example, the <a + href="{@docRoot}reference/android/widget/TextView.html#lattrs">TextView XML attributes</a>).</p> + </dd> + <dt id="requestfocus-element"><code><requestFocus></code></dt> + <dd>Any element representing a {@link android.view.View} object can include this empty element, + which gives it's parent initial focus on the screen. You can have only one of these + elements per file.</dd> + + </dl> + +<h4 id="idvalue">Value for <code>android:id</code></h4> + +<p>For the ID value, you should use this syntax form: <code>"@+id/<em>name</em>"</code>. The plus symbol, +{@code +}, indicates that this is a new resource ID and the aapt tool will create +a new resource number to the {@code R.java} class, if it doesn't already exist. For example:</p> +<pre> +<TextView android:id="@+id/nameTextbox"/> +</pre> +<p>You can then refer to it this way in Java:</p> +<pre> +findViewById(R.id.nameTextbox); +</pre> + +<h4 id="layoutvalues">Value for <code>android:layout_height</code> and +<code>android:layout_width</code>:</h4> + + <p>The height and width value can be expressed using any of the + <a href="more-resources.html#Dimension">dimension + units</a> supported by Android (px, dp, sp, pt, in, mm) or with the following keywords:</p> + <table><tr><th>Value</th><th>Description</th></tr> + <tr> + <td><code>match_parent</code></td> + <td>Sets the dimension to match that of the parent element. Added in API Level 8 to +deprecate <code>fill_parent</code>.</td> + </tr> + <tr> + <td><code>fill_parent</code></td> + <td>Sets the dimension to match that of the parent element.</td> + </tr><tr> + <td><code>wrap_content</code></td> + <td>Sets the dimension only to the size required to fit the content of this element.</td> + </tr> + </table> + +<h4>Custom View elements</h4> + +<p>You can create your own custom {@link android.view.View} and {@link android.view.ViewGroup} +elements and apply them to your layout the same as a standard layout +element. You can also specify the attributes supported in the XML element. To learn more, +read <a href="{@docRoot}guide/topics/ui/custom-components.html">Building Custom Components</a>. +</p> + +</dd> <!-- end elements and attributes --> + +<dt>example:</dt> +<dd>XML file saved at <code>res/layout/main_activity.xml</code>: +<pre> +<?xml version="1.0" encoding="utf-8"?> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + android:orientation="vertical" > + <TextView android:id="@+id/text" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="Hello, I am a TextView" /> + <Button android:id="@+id/button" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="Hello, I am a Button" /> +</LinearLayout> +</pre> + <p>This application code will load the layout for an {@link android.app.Activity}, in the + {@link android.app.Activity#onCreate(Bundle) onCreate()} method:</dt> + <dd> +<pre> +public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView.(R.layout.main_activity); +} +</pre> +</dd> <!-- end example --> + + +<dt>see also:</dt> +<dd> +<ul> + <li><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a></li> + <li>{@link android.view.View}</li> + <li>{@link android.view.ViewGroup}</li> +</ul> +</dd> + +</dl>
\ No newline at end of file diff --git a/docs/html/guide/topics/resources/localization.jd b/docs/html/guide/topics/resources/localization.jd index 192695b..e76a92c 100755 --- a/docs/html/guide/topics/resources/localization.jd +++ b/docs/html/guide/topics/resources/localization.jd @@ -1,5 +1,5 @@ page.title=Localization
-parent.title=Resources and Assets
+parent.title=Application Resources
parent.link=index.html
@jd:body
@@ -25,12 +25,11 @@ defaults.</li> <ol><li><a href="#defaults-r-important">Why Default Resources Are Important</a></li></ol>
<li><a href="#using-framework">Using Resources for Localization</a>
<ol>
- <li><a href="#creating-defaults">How to Create Default Resources</a></li><li>
- <a href="#creating-alternates">How to Create Alternate Resources</a></li>
+ <li><a href="#creating-defaults">How to Create Default Resources</a></li>
+ <li><a href="#creating-alternatives">How to Create Alternative Resources</a></li>
<li><a href="#resource-precedence">Which Resources Take Precedence?</a></li>
<li><a href="#referring-to-resources">Referring to Resources in Java</a></li>
-
- </ol>
+ </ol>
</li>
<li><a href="#strategies">Localization Strategies</a></li>
<li><a href="#testing">Testing Localized Applications</a></li>
@@ -52,7 +51,7 @@ defaults.</li> <ol>
<li><a
href="{@docRoot}resources/tutorials/localization/index.html">Hello, L10N Tutorial</a></li>
- <li><a href="resources-i18n.html">Resources</a></li>
+ <li><a href="{@docRoot}guide/topics/resources/providing-resources.html">Providing Resources</a></li>
<li><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a></li>
<li><a href="{@docRoot}reference/android/app/Activity.html#ActivityLifecycle">Activity Lifecycle</a></li>
</ol>
@@ -80,7 +79,7 @@ functionality:</p> <ul>
<li>You can put most or all of the <em>contents</em> of your application's
user interface into resource files, as described in this document and in <a
-href="index.html">Resources</a>. </li>
+href="{@docRoot}guide/topics/resources/providing-resources.html">Providing Resources</a>.</li>
<li>The <em>behavior</em> of the user interface, on the other hand, is driven
by your Java code.
For example, if users input data that needs to be formatted or sorted
@@ -107,14 +106,15 @@ resources that best match the device.</p> <p>(This document focuses on localization and locale. For a complete description
of resource-switching and all the types of configurations that you can
specify — screen orientation, touchscreen type, and so on — see <a
-href="resources-i18n.html#AlternateResources">Alternate Resources</a>.)</p>
+href="{@docRoot}guide/topics/resources/providing-resources.html#AlternativeResources">Providing
+Alternative Resources</a>.)</p>
<table border="0" cellspacing="0" cellpadding="0">
<tr border="0">
<td width="180" style="border: 0pt none ;"><p class="special-note">
<strong>When you write your application:</strong>
<br><br>
- You create a set of default resources, plus alternates to be used in
+ You create a set of default resources, plus alternatives to be used in
different locales.</p></td>
<td style="border: 0pt none; padding:0">
<p style="border:0; padding:0"><img src="../../../images/resources/right-arrow.png" alt="right-arrow"
@@ -126,7 +126,7 @@ href="resources-i18n.html#AlternateResources">Alternate Resources</a>.)</p> </tr>
</table>
-<p>When you write your application, you create default and alternate resources
+<p>When you write your application, you create default and alternative resources
for your application to use. To create resources, you place files within
specially named subdirectories of the project's <code>res/</code> directory.
</p>
@@ -204,18 +204,19 @@ speak. </p> <em>default</em> string file must contain them all.
</p>
-<h3 id="creating-alternates">How to Create Alternate Resources</h3>
+<h3 id="creating-alternatives">How to Create Alternative Resources</h3>
-<p>A large part of localizing an application is providing alternate text for
-different languages. In some cases you will also provide alternate graphics,
+<p>A large part of localizing an application is providing alternative text for
+different languages. In some cases you will also provide alternative graphics,
sounds, layouts, and other locale-specific resources. </p>
<p>An application can specify many <code>res/<em><qualifiers></em>/</code>
-directories, each with different qualifiers. To create an alternate resource for
+directories, each with different qualifiers. To create an alternative resource for
a different locale, you use a qualifier that specifies a language or a
language-region combination. (The name of a resource directory must conform
to the naming scheme described in
-<a href="resources-i18n.html#AlternateResources">Alternate Resources</a>,
+<a href="{@docRoot}guide/topics/resources/providing-resources.html#AlternativeResources">Providing
+Alternative Resources</a>,
or else it will not compile.)</p>
<p><em>Example:</em></p>
@@ -223,7 +224,7 @@ or else it will not compile.)</p> <p>Suppose that your application's default language is English. Suppose also
that you want to localize all the text in your application to French, and most
of the text in your application (everything except the application's title) to
-Japanese. In this case, you could create three alternate <code>strings.xml</code>
+Japanese. In this case, you could create three alternative <code>strings.xml</code>
files, each stored in a locale-specific resource directory:</p>
<ol>
@@ -315,19 +316,21 @@ configured for Hindi. That is because in the resource-selection process, Android will prefer an MCC match over a language match. </p>
<p>The selection process is not always as straightforward as these examples
-suggest. Please read <a href="resources-i18n.html#best-match">How Android finds
-the best matching directory</a> for a more nuanced description of the
+suggest. Please read <a
+href="{@docRoot}guide/topics/resources/creating-resources.html#BestMatch">How Android Finds
+the Best-matching Resource</a> for a more nuanced description of the
process. All the qualifiers are described and listed in order of
-precedence in <a href="resources-i18n.html#table2">Table 2 in the Resources
-document</a>.</p>
+precedence in <a
+href="{@docRoot}guide/topics/resources/creating-resources.html#table2">Table 2 of Providing
+Alternative Resources</a>.</p>
<h3 id="referring-to-resources">Referring to Resources in Java</h3>
<p>In your application's Java code, you refer to resources using the syntax
<code>R.<em>resource_type</em>.<em>resource_name</em></code> or
<code>android.R.<em>resource_type</em>.<em>resource_name</em></code><em>.</em>
-For more about this, see <a href="resources-i18n.html#ResourcesInCode">Using
-Resources in Code</a>.</p>
+For more about this, see <a
+href="{@docRoot}guide/topics/resources/using-resources.html">Accessing Resources</a>.</p>
<h2 id="strategies">Localization Strategies</h2>
@@ -359,7 +362,7 @@ that your application will need. </p> <h4>Design a flexible layout</h4>
<p> If you need to rearrange your layout to fit a certain language (for example
-German with its long words), you can create an alternate layout for that
+German with its long words), you can create an alternative layout for that
language (for example <code>res/layout-de/main.xml</code>). However, doing this
can make your application harder to maintain. It is better to create a single
layout that is more flexible.</p>
@@ -383,10 +386,10 @@ languages.</li> <p>You probably do not need to create a locale-specific
alternative for every resource in your application. For example, the layout
defined in the <code>res/layout/main.xml</code> file might work in any locale,
-in which case there would be no need to create any alternate layout files.
+in which case there would be no need to create any alternative layout files.
</p>
-<p>Also, you might not need to create alternate text for every
+<p>Also, you might not need to create alternative text for every
string. For example, assume the following:</p>
<ul>
@@ -395,7 +398,7 @@ English. Every string that the application uses is defined, using American English spellings, in <code>res/values/strings.xml</code>. </li>
<li>For a few important phrases, you want to provide
-British English spelling. You want these alternate strings to be used when your
+British English spelling. You want these alternative strings to be used when your
application runs on a device in the United Kingdom. </li>
</ul>
diff --git a/docs/html/guide/topics/resources/menu-resource.jd b/docs/html/guide/topics/resources/menu-resource.jd new file mode 100644 index 0000000..c9f27fa --- /dev/null +++ b/docs/html/guide/topics/resources/menu-resource.jd @@ -0,0 +1,207 @@ +page.title=Menu Resource +parent.title=Resource Types +parent.link=available-resources.html +@jd:body + +<div id="qv-wrapper"> + <div id="qv"> + <h2>See also</h2> + <ol> + <li><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a></li> + </ol> + </div> +</div> + +<p>A menu resource defines an application menu (Options Menu, Context Menu, or Sub Menu) that +can be inflated with {@link android.view.MenuInflater}.</p> + +<dl class="xml"> + +<dt>file location:</dt> +<dd><code>res/menu/<em>filename</em>.xml</code><br/> +The filename will be used as the resource ID.</dd> + +<dt>compiled resource datatype:</dt> +<dd>Resource pointer to a {@link android.view.Menu} (or subclass) resource.</dd> + +<dt>resource reference:</dt> +<dd> +In Java: <code>R.menu.<em>filename</em></code><br/> +In XML: <code>@[<em>package</em>:]menu.<em>filename</em></code> +</dd> + +<dt>syntax:</dt> +<dd> +<pre> +<?xml version="1.0" encoding="utf-8"?> +<<a href="#menu-element">menu</a> xmlns:android="http://schemas.android.com/apk/res/android"> + <<a href="#item-element">item</a> android:id="<em>resource ID</em>" + android:menuCategory=["container" | "system" | "secondary" | "alternative"] + android:orderInCategory="<em>integer</em>" + android:title="<em>string</em>" + android:titleCondensed="<em>string</em>" + android:icon="<em>drawable resource</em>" + android:alphabeticShortcut="<em>string</em>" + android:numericShortcut="<em>string</em>" + android:checkable=["true" | "false"] + android:visible=["visible" | "invisible" | "gone"] + android:enabled=["enabled" | "disabled"] /> + <<a href="#group-element">group</a> android:id="<em>resource ID</em>" + android:menuCategory=["container" | "system" | "secondary" | "alternative"] + android:orderInCategory="<em>integer</em>" + android:checkableBehavior=["none" | "all" | "single"] + android:visible=["visible" | "invisible" | "gone"] + android:enabled=["enabled" | "disabled"] > + <<a href="#item-element">item</a> /> + </group> + <<a href="#item-element">item</a> > + <<a href="#menu-element">menu</a>> + <<a href="#item-element">item</a> /> + </menu> + </item> +</menu> +</pre> +</dd> + +<dt>elements:</dt> +<dd> +<dl class="tag-list"> + + <dt id="menu-element"><code><menu></code></dt> + <dd><strong>Required.</strong> This must be the root node. Contains <code><item></code> and/or + <code><group></code> elements. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>xmlns:android</code></dt> + <dd><em>String</em>. <strong>Required.</strong> Defines the XML namespace, which must be + <code>"http://schemas.android.com/apk/res/android"</code>. + </dl> + </dd> + <dt id="group-element"><code><group></code></dt> + <dd>A menu group (to create a collection of items that share traits, such as whether they are +visible, enabled, or checkable). Contains one or more <code><item></code> elements. Must be a +child of a <code><menu></code> element. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>android:id</code></dt> + <dd><em>Resource name</em>. A unique resource name. The value takes the form: +<code>"@+id/<em>name</em>"</code>.</dd> + <dt><code>android:menuCategory</code></dt> + <dd><em>Keyword</em>. Value corresponding to {@link android.view.Menu} {@code CATEGORY_*} + constants, which define the group's priority. Valid values: + <table> + <tr><th>Value</th><th>Description</th></tr> + <tr><td><code>container</code></td><td>For groups that are part of a +container.</td></tr> + <tr><td><code>system</code></td><td>For groups that are provided by the +system.</td></tr> + <tr><td><code>secondary</code></td><td>For groups that are user-supplied secondary +(infrequently used) options.</td></tr> + <tr><td><code>alternative</code></td><td>For groups that are alternative actions +on the data that is currently displayed.</td></tr> + </table> + </dd> + <dt><code>android:orderInCategory</code></dt> + <dd><em>Integer</em>. The default order of the items within the category.</dd> + <dt><code>android:checkableBehavior</code></dt> + <dd><em>Keyword</em>. The type of checkable behavior for the group. Valid values: + <table> + <tr><th>Value</th><th>Description</th></tr> + <tr><td><code>none</code></td><td>Not checkable</td></tr> + <tr><td><code>all</code></td><td>All items can be checked (use checkboxes)</td></tr> + <tr><td><code>single</code></td><td>Only one item can be checked (use radio buttons)</td></tr> + </table> + </dd> + <dt><code>android:visible</code></dt> + <dd><em>Boolean</em>. "true" if the group is visible.</dd> + <dt><code>android:enabled</code></dt> + <dd><em>Boolean</em>. "true" if the group is enabled.</dd> + </dl> + </dd> + <dt id="item-element"><code><item></code></dt> + <dd>A menu item. May contain a <code><menu></code> element (for a Sub + Menu). Must be a child of a <code><menu></code> or <code><group></code> element. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>android:id</code></dt> + <dd><em>Resource name</em>. A unique resource name. The value takes the form: +<code>"@+id/<em>name</em>"</code>.</dd> + <dt><code>android:menuCategory</code></dt> + <dd><em>Keyword</em>. Value corresponding to {@link android.view.Menu} {@code CATEGORY_*} + constants, which define the item's priority. Valid values: + <table> + <tr><th>Value</th><th>Description</th></tr> + <tr><td><code>container</code></td><td>For items that are part of a +container.</td></tr> + <tr><td><code>system</code></td><td>For items that are provided by the +system.</td></tr> + <tr><td><code>secondary</code></td><td>For items that are user-supplied secondary +(infrequently used) options.</td></tr> + <tr><td><code>alternative</code></td><td>For items that are alternative actions +on the data that is currently displayed.</td></tr> + </table> + </dd> + <dt><code>android:orderInCategory</code></dt> + <dd><em>Integer</em>. The order of "importance" of the item, within a group.</dd> + <dt><code>android:title</code></dt> + <dd><em>String</em>. The menu title.</dd> + <dt><code>android:titleCondensed</code></dt> + <dd><em>String</em>. A condensed title, for situations in which the normal title is +too long.</dd> + <dt><code>android:icon</code></dt> + <dd><em>Drawable resource</em>. An image to be used as the menu item icon.</dd> + <dt><code>android:alphabeticShortcut</code></dt> + <dd><em>Char</em>. A character for the alphabetic shortcut key.</dd> + <dt><code>android:numericShortcut</code></dt> + <dd><em>Integer</em>. A number for the numeric shortcut key.</dd> + <dt><code>android:checkable</code></dt> + <dd><em>Boolean</em>. "true" if the item is checkable.</dd> + <dt><code>android:checked</code></dt> + <dd><em>Boolean</em>. "true" if the item is checked by default.</dd> + <dt><code>android:visible</code></dt> + <dd><em>Boolean</em>. "true" if the item is visible by default.</dd> + <dt><code>android:enabled</code></dt> + <dd><em>Boolean</em>. "true" if the item is enabled by default.</dd> + </dl> + </dd> +</dl> + +</dd> + +<dt>example:</dt> +<dd>XML file saved at <code>res/menu/example_menu.xml</code>: +<pre> +<menu xmlns:android="http://schemas.android.com/apk/res/android"> + <item android:id="@+id/example_item" + android:title="Example Item" + android:icon="@drawable/example_item_icon" /> + <group android:id="@+id/example_group"> + <item android:id="@+id/group_item1" + android:title="Group Item 1" + android:icon="@drawable/example_item1_icon" /> + <item android:id="@+id/group_item2" + android:title="Group Item 2" + android:icon="@drawable/example_item2_icon" /> + </group> + <item android:id="@+id/submenu" + android:title="Sub Menu" > + <menu> + <item android:id="@+id/submenu_item" + android:title="Sub Menu Item" /> + </menu> + </item> +</menu> +</pre> + <p>This application code will inflate the menu from the {@link +android.app.Activity#onCreateOptionsMenu(Menu)} callback:</p> +<pre> +public boolean onCreateOptionsMenu(Menu menu) { + MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.example_menu, menu); + return true; +} +</pre> +</dd> <!-- end example --> + + +</dl>
\ No newline at end of file diff --git a/docs/html/guide/topics/resources/more-resources.jd b/docs/html/guide/topics/resources/more-resources.jd new file mode 100644 index 0000000..0e2b30b --- /dev/null +++ b/docs/html/guide/topics/resources/more-resources.jd @@ -0,0 +1,684 @@ +page.title=More Resource Types +parent.title=Resource Types +parent.link=available-resources.html +@jd:body + +<p>This page defines more types of resources you can externalize, including:</p> + +<dl> + <dt><a href="#Bool">Bool</a></dt> + <dd>XML resource that carries a boolean value.</dd> + <dt><a href="#Color">Color</a></dt> + <dd>XML resource that carries a color value (a hexadecimal color).</dd> + <dt><a href="#Dimension">Dimension</a></dt> + <dd>XML resource that carries a dimension value (with a unit of measure).</dd> + <dt><a href="#Integer">Integer</a></dt> + <dd>XML resource that carries an integer value.</dd> + <dt><a href="#IntegerArray">Integer Array</a></dt> + <dd>XML resource that provides an array of integers.</dd> + <dt><a href="#TypedArray">Typed Array</a></dt> + <dd>XML resource that provides a {@link android.content.res.TypedArray} (which you can use +for an array of drawables).</dd> +</dl> + + + + +<h2 id="Bool">Bool</h2> + +<p>A boolean value defined in XML.</p> + +<p class="note"><strong>Note:</strong> A bool is a simple resource that is referenced +using the value provided in the {@code name} attribute (not the name of the XML file). As +such, you can combine bool resources with other simple resources in the one XML file, +under one {@code <resources>} element.</p> + +<dl class="xml"> + +<dt>file location:</dt> +<dd><code>res/values/<em>filename</em>.xml</code><br/> +The filename is arbitrary. The {@code <bool>} element's {@code name} will be used as the resource +ID.</dd> + +<dt>resource reference:</dt> +<dd> +In Java: <code>R.bool.<em>bool_name</em></code><br/> +In XML: <code>@[<em>package</em>:]bool/<em>bool_name</em></code> +</dd> + +<dt>syntax:</dt> +<dd> +<pre class="stx"> +<?xml version="1.0" encoding="utf-8"?> +<<a href="#bool-resources-element">resources</a>> + <<a href="#bool-element">bool</a> + name="<em>bool_name</em>" + >[true | false]</bool> +</resources> +</pre> +</dd> + +<dt>elements:</dt> +<dd> +<dl class="tag-list"> + + <dt id="bool-resources-element"><code><resources></code></dt> + <dd><strong>Required.</strong> This must be the root node. + <p>No attributes.</p> + </dd> + <dt id="bool-element"><code><bool></code></dt> + <dd>A boolean value: {@code true} or {@code false}. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>name</code></dt> + <dd><em>String</em>. A name for the bool value. This will be used as the resource ID.</dd> + </dl> + </dd> + +</dl> +</dd> <!-- end elements and attributes --> + +<dt>example:</dt> +<dd>XML file saved at <code>res/values-small/bools.xml</code>: +<pre> +<?xml version="1.0" encoding="utf-8"?> +<resources> + <bool name="screen_small">true</bool> + <bool name="adjust_view_bounds">true</bool> +</resources> +</pre> + + <p>This application code retrieves the boolean:</p> +<pre> +Resources res = {@link android.content.Context#getResources()}; +boolean screenIsSmall = res.{@link android.content.res.Resources#getBoolean(int) getBoolean}(R.bool.screen_small); +</pre> + <p>This layout XML uses the boolean for an attribute:</p> +<pre> +<ImageView + android:layout_height="fill_parent" + android:layout_width="fill_parent" + android:src="@drawable/logo" + android:adjustViewBounds="@bool/adjust_view_bounds" /> +</pre> +</dd> <!-- end example --> + +</dl> + + + + +<h2 id="Color">Color</h2> + +<p>A color value defined in XML. +The color is specified with an RGB value and alpha channel. A color resource can be used +any place that expects a hexadecimal color value.</p> + +<p>The value always begins with a pound (#) character and then followed by the +Alpha-Red-Green-Blue information in one of the following formats:</p> +<ul> + <li>#<em>RGB</em></li> + <li>#<em>ARGB</em></li> + <li>#<em>RRGGBB</em></li> + <li>#<em>AARRGGBB</em></li> +</ul> + +<p class="note"><strong>Note:</strong> A color is a simple resource that is referenced +using the value provided in the {@code name} attribute (not the name of the XML file). As +such, you can combine color resources with other simple resources in the one XML file, +under one {@code <resources>} element.</p> + +<dl class="xml"> + +<dt>file location:</dt> +<dd><code>res/values/colors.xml</code><br/> +The filename is arbitrary. The {@code <color>} element's {@code name} will be used as the +resource ID.</dd> + +<dt>resource reference:</dt> +<dd> +In Java: <code>R.color.<em>color_name</em></code><br/> +In XML: <code>@[<em>package</em>:]color/<em>color_name</em></code> +</dd> + +<dt>syntax:</dt> +<dd> +<pre class="stx"> +<?xml version="1.0" encoding="utf-8"?> +<<a href="#color-resources-element">resources</a>> + <<a href="#color-element">color</a> + name="<em>color_name</em>" + ><em>hex_color</em></color> +</resources> +</pre> +</dd> + +<dt>elements:</dt> +<dd> +<dl class="tag-list"> + + <dt id="color-resources-element"><code><resources></code></dt> + <dd><strong>Required.</strong> This must be the root node. + <p>No attributes.</p> + </dd> + <dt id="color-element"><code><color></code></dt> + <dd>A color expressed in hexadecimal, as described above. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>name</code></dt> + <dd><em>String</em>. A name for the color. This will be used as the resource ID. + </dd> + </dl> + </dd> + +</dl> +</dd> <!-- end elements and attributes --> + +<dt>example:</dt> +<dd>XML file saved at <code>res/values/colors.xml</code>: +<pre> +<?xml version="1.0" encoding="utf-8"?> +<resources> + <color name="opaque_red">#f00</color> + <color name="translucent_red">#80ff0000</color> +</resources> +</pre> + + <p>This application code retrieves the color resource:</p> +<pre> +Resources res = {@link android.content.Context#getResources()}; +int color = res.{@link android.content.res.Resources#getColor(int) getColor}(R.color.opaque_red); +</pre> + <p>This layout XML applies the color to an attribute:</p> +<pre> +<TextView + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:textColor="@color/translucent_red" + android:text="Hello"/> +</pre> +</dd> <!-- end example --> + +</dl> + + + + + +<h2 id="Dimension">Dimension</h2> + +<p>A dimension value defined in XML. A dimension +is specified with a number followed by a unit of measure. +For example: 10px, 2in, 5sp. The following units of measure are supported by Android:</p> +<dl> + <dt>{@code dp}</dt> + <dd>Density-independent Pixels - an abstract unit that is based on the physical density of the screen. + These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of + dp-to-pixel will change with the screen density, but not necessarily in direct proportion. The + compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".</dd> + <dt>{@code sp}</dt> + <dd>Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font + size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted + for both the screen density and the user's preference.</dd> + <dt>{@code pt}</dt> + <dd>Points - 1/72 of an inch based on the physical size of the screen.</dd> + <dt>{@code px}</dt> + <dd>Pixels - corresponds to actual pixels on the screen. This unit of measure is not recommended because + the actual representation can vary across devices; each devices may have a different number of pixels + per inch and may have more or fewer total pixels available on the screen.</dd> + <dt>{@code mm}</dt> + <dd>Millimeters - based on the physical size of the screen.</dd> + <dt>{@code in}</dt> + <dd>Inches - based on the physical size of the screen.</dd> +</dl> + +<p class="note"><strong>Note:</strong> A dimension is a simple resource that is referenced +using the value provided in the {@code name} attribute (not the name of the XML file). As +such, you can combine dimension resources with other simple resources in the one XML file, +under one {@code <resources>} element.</p> + +<dl class="xml"> + +<dt>file location:</dt> +<dd><code>res/values/<em>filename</em>.xml</code><br/> +The filename is arbitrary. The {@code <dimen>} element's {@code name} will be used as the +resource ID.</dd> + +<dt>resource reference:</dt> +<dd> +In Java: <code>R.dimen.<em>dimension_name</em></code><br/> +In XML: <code>@[<em>package</em>:]dimen/<em>dimension_name</em></code> +</dd> + +<dt>syntax:</dt> +<dd> +<pre class="stx"> +<?xml version="1.0" encoding="utf-8"?> +<<a href="#dimen-resources-element">resources</a>> + <<a href="#dimen-element">dimen</a> + name="<em>dimension_name</em>" + ><em>dimension</em></dimen> +</resources> +</pre> +</dd> + +<dt>elements:</dt> +<dd> +<dl class="tag-list"> + + <dt id="dimen-resources-element"><code><resources></code></dt> + <dd><strong>Required.</strong> This must be the root node. + <p>No attributes.</p> + </dd> + <dt id="dimen-element"><code><dimen></code></dt> + <dd>A dimension, represented by a float, followed by a unit of measurement (dp, sp, pt, px, mm, in), + as described above. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>name</code></dt> + <dd><em>String</em>. A name for the dimension. This will be used as the resource ID. + </dd> + </dl> + </dd> + +</dl> +</dd> <!-- end elements and attributes --> + +<dt>example:</dt> +<dd>XML file saved at <code>res/values/dimens.xml</code>: +<pre> +<?xml version="1.0" encoding="utf-8"?> +<resources> + <dimen name="textview_height">25dp</dimen> + <dimen name="textview_width">150dp</dimen> + <dimen name="ball_radius">30dp</dimen> + <dimen name="font_size">16sp</dimen> +</resources> +</pre> + + <p>This application code retrieves a dimension:</p> +<pre> +Resources res = {@link android.content.Context#getResources()}; +float fontSize = res.{@link android.content.res.Resources#getDimension(int) getDimension}(R.dimen.font_size); +</pre> + <p>This layout XML applies dimensions to attributes:</p> +<pre> +<TextView + android:layout_height="@dimen/textview_height" + android:layout_width="@dimen/textview_width" + android:textSize="@dimen/sixteen_sp"/> +</pre> + </dl> +</dd> <!-- end example --> + +</dl> + + + + + + +<h2 id="Integer">Integer</h2> + +<p>An integer defined in XML.</p> + +<p class="note"><strong>Note:</strong> An integer is a simple resource that is referenced +using the value provided in the {@code name} attribute (not the name of the XML file). As +such, you can combine integer resources with other simple resources in the one XML file, +under one {@code <resources>} element.</p> + +<dl class="xml"> + +<dt>file location:</dt> +<dd><code>res/values/<em>filename.xml</em></code><br/> +The filename is arbitrary. The {@code <integer>} element's {@code name} will be used as the +resource ID.</dd> + +<dt>resource reference:</dt> +<dd> +In Java: <code>R.integer.<em>integer_name</em></code><br/> +In XML: <code>@[<em>package</em>:]integer/<em>integer_name</em></code> +</dd> + +<dt>syntax:</dt> +<dd> +<pre class="stx"> +<?xml version="1.0" encoding="utf-8"?> +<<a href="#integer-resources-element">resources</a>> + <<a href="#integer-element">integer</a> + name="<em>integer_name</em>" + ><em>integer</em></dimen> +</resources> +</pre> +</dd> + +<dt>elements:</dt> +<dd> +<dl class="tag-list"> + + <dt id="integer-resources-element"><code><resources></code></dt> + <dd><strong>Required.</strong> This must be the root node. + <p>No attributes.</p> + </dd> + <dt id="integer-element"><code><integer></code></dt> + <dd>An integer. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>name</code></dt> + <dd><em>String</em>. A name for the integer. This will be used as the resource ID. + </dd> + </dl> + </dd> + +</dl> +</dd> <!-- end elements and attributes --> + +<dt>example:</dt> +<dd> + <p>XML file saved at <code>res/values/integers.xml</code>:</p> +<pre> +<?xml version="1.0" encoding="utf-8"?> +<resources> + <integer name="max_speed">75</dimen> + <integer name="min_speed">5</dimen> +</resources> +</pre> + <p>This application code retrieves an integer:</p> +<pre> +Resources res = {@link android.content.Context#getResources()}; +int maxSpeed = res.{@link android.content.res.Resources#getInteger(int) getInteger}(R.integer.max_speed); +</pre> +</dd> <!-- end example --> + + +</dl> + + + + + +<h2 id="IntegerArray">Integer Array</h2> + +<p>An array of integers defined in XML.</p> + +<p class="note"><strong>Note:</strong> An integer array is a simple resource that is referenced +using the value provided in the {@code name} attribute (not the name of the XML file). As +such, you can combine integer array resources with other simple resources in the one XML file, +under one {@code <resources>} element.</p> + + +<dl class="xml"> + +<dt>file location:</dt> +<dd><code>res/values/<em>filename</em>.xml</code><br/> +The filename is arbitrary. The {@code <integer-array>} element's {@code name} will be used as the +resource ID.</dd> + +<dt>compiled resource datatype:</dt> +<dd>Resource pointer to an array of integers.</dd> + +<dt>resource reference:</dt> +<dd> +In Java: <code>R.array.<em>string_array_name</em></code><br/> +In XML: <code>@[<em>package</em>:]array.<em>integer_array_name</em></code> +</dd> + +<dt>syntax:</dt> +<dd> +<pre class="stx"> +<?xml version="1.0" encoding="utf-8"?> +<<a href="#integer-array-resources-element">resources</a>> + <<a href="#integer-array-element">integer-array</a> + name="<em>integer_array_name</em>"> + <<a href="#integer-array-item-element">item</a> + ><em>integer</em></item> + </integer-array> +</resources> +</pre> +</dd> + +<dt>elements:</dt> +<dd> +<dl class="tag-list"> + <dt id="integer-array-resources-element"><code><resources></code></dt> + <dd><strong>Required.</strong> This must be the root node. + <p>No attributes.</p> + </dd> + <dt id="integer-array-element"><code><string-array></code></dt> + <dd>Defines an array of integers. Contains one or more child {@code <item>} elements. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>android:name</code></dt> + <dd><em>String</em>. A name for the array. This name will be used as the resource +ID to reference the array.</dd> + </dl> + </dd> + <dt id="integer-array-item-element"><code><item></code></dt> + <dd>An integer. The value can be a referenced to another +integer resource. Must be a child of a {@code <integer-array>} element. + <p>No attributes.</p> + </dd> +</dl> +</dd> <!-- end elements --> + +<dt>example:</dt> +<dd>XML file saved at <code>res/values/integers.xml</code>: +<pre> +<?xml version="1.0" encoding="utf-8"?> +<resources> + <integer-array name="bits"> + <item>4</item> + <item>8</item> + <item>16</item> + <item>32</item> + </integer-array> +</resources> +</pre> + + <p>This application code retrieves the integer array:</p> +<pre> +Resources res = {@link android.content.Context#getResources()}; +int[] bits = res.{@link android.content.res.Resources#getIntArray(int) getIntArray}(R.array.bits); +</pre> +</dd> <!-- end example --> + +</dl> + + + + + +<h2 id="TypedArray">Typed Array</h2> + +<p>A {@link android.content.res.TypedArray} defined in XML. You can use +this to create an array of other resources, such as drawables. Note that the array +is not required to be homogeneous, so you can create an array of mixed resource types, but +you must be aware of what and where the data types are in the array so that you can properly obtain +each item with the {@link android.content.res.TypedArray}'s {@code get...()} methods.</p> + +<p class="note"><strong>Note:</strong> A typed array is a simple resource that is referenced +using the value provided in the {@code name} attribute (not the name of the XML file). As +such, you can combine typed array resources with other simple resources in the one XML file, +under one {@code <resources>} element.</p> + + +<dl class="xml"> + +<dt>file location:</dt> +<dd><code>res/values/<em>filename</em>.xml</code><br/> +The filename is arbitrary. The {@code <array>} element's {@code name} will be used as the +resource ID.</dd> + +<dt>compiled resource datatype:</dt> +<dd>Resource pointer to a {@link android.content.res.TypedArray}.</dd> + +<dt>resource reference:</dt> +<dd> +In Java: <code>R.array.<em>array_name</em></code><br/> +In XML: <code>@[<em>package</em>:]array.<em>array_name</em></code> +</dd> + +<dt>syntax:</dt> +<dd> +<pre class="stx"> +<?xml version="1.0" encoding="utf-8"?> +<<a href="#array-resources-element">resources</a>> + <<a href="#array-element">array</a> + name="<em>integer_array_name</em>"> + <<a href="#array-item-element">item</a>><em>resource</em></item> + </array> +</resources> +</pre> +</dd> + +<dt>elements:</dt> +<dd> +<dl class="tag-list"> + <dt id="array-resources-element"><code><resources></code></dt> + <dd><strong>Required.</strong> This must be the root node. + <p>No attributes.</p> + </dd> + <dt id="array-element"><code><array></code></dt> + <dd>Defines an array. Contains one or more child {@code <item>} elements. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>android:name</code></dt> + <dd><em>String</em>. A name for the array. This name will be used as the resource +ID to reference the array.</dd> + </dl> + </dd> + <dt id="array-item-element"><code><item></code></dt> + <dd>A generic resource. The value can be a reference to a resource or a simple data type. +Must be a child of an {@code <array>} element. + <p>No attributes.</p> + </dd> +</dl> +</dd> <!-- end elements --> + +<dt>example:</dt> +<dd>XML file saved at <code>res/values/arrays.xml</code>: +<pre> +<?xml version="1.0" encoding="utf-8"?> +<resources> + <array name="icons"> + <item>@drawable/home</item> + <item>@drawable/settings</item> + <item>@drawable/logout</item> + </array> + <array name="colors"> + <item>#FFFF0000</item> + <item>#FF00FF00</item> + <item>#FF0000FF</item> + </array> +</resources> +</pre> + + <p>This application code retrieves each array and then obtains the first entry in each array:</p> +<pre> +Resources res = {@link android.content.Context#getResources()}; +TypedArray icons = res.{@link android.content.res.Resources#obtainTypedArray(int) obtainTypedArray}(R.array.icons); +Drawable drawable = icons.{@link android.content.res.TypedArray#getDrawable(int) getDrawable}(0); + +TypedArray colors = res.{@link android.content.res.Resources#obtainTypedArray(int) obtainTypedArray}(R.array.icons); +int color = colors.{@link android.content.res.TypedArray#getColor(int,int) getColor}(0,0); +</pre> +</dd> <!-- end example --> + +</dl> + + + + + + + + + + +<!-- TODO + + +<h2>Styleable Attribute</h2> + + +<dl class="xml"> + +<dt>syntax:</dt> +<dd> +<pre class="stx"> +</pre> +</dd> + +<dt>file location:</dt> +<dd><code>res/</code></dd> + +<dt>compiled resource datatype:</dt> +<dd>Resource pointer to a {@link android.view.Menu} (or subclass) resource.</dd> + +<dt>resource reference:</dt> +<dd>Java: <code>R.</code><br/> + XML: +</dd> + +<dt>elements and attributes:</dt> +<dd> +<dl class="attr"> + + <dt><code></code></dt> + <dd></dd> + <dt><code></code></dt> + <dd>Valid attributes: + <dl> + <dt><code></code></dt> + <dd> + </dd> + <dt><code></code></dt> + <dd> + </dd> + </dl> + </dd> + +</dl> +</dd> + +<dt>example:</dt> +<dd> + <dl> + + <dt>XML file saved at <code>res/</code>:</dt> + <dd> +<pre> + +</pre> + </dd> + + <dt>Java code :</dt> + <dd> +<pre> + +</pre> + </dd> + + </dl> +</dd> + + +<dt>see also:</dt> +<dd> +<ul> + <li></li> +</ul> +</dd> + +</dl> + + + + + + +--> + + + + diff --git a/docs/html/guide/topics/resources/providing-resources.jd b/docs/html/guide/topics/resources/providing-resources.jd new file mode 100644 index 0000000..b495eb8 --- /dev/null +++ b/docs/html/guide/topics/resources/providing-resources.jd @@ -0,0 +1,723 @@ +page.title=Providing Resources +parent.title=Application Resources +parent.link=index.html +@jd:body + +<div id="qv-wrapper"> +<div id="qv"> + <h2>Quickview</h2> + <ul> + <li>Different types of resources belong in different sub-directories of {@code res/}</li> + <li>Alternative resources provide configuration-specific resource files</li> + </ul> + <h2>In this document</h2> + <ol> + <li><a href="#AlternativeResources">Providing Alternative Resources</a> + <ol> + <li><a href="#AliasResources">Creating alias resources</a></li> + </ol> + </li> + <li><a href="#BestMatch">How Android Finds the Best-matching Resource</a></li> + </ol> + + <h2>See also</h2> + <ol> + <li><a href="accessing-resources.html">Accessing Resources</a></li> + <li><a href="available-resources.html">Resource Types</a></li> + </ol> +</div> +</div> + +<p>There are several types of resource files you can +include in your application and each type belongs in a specific sub-directory of your project's +{@code res/} directory. Absolutely no files should be saved directly inside {@code res/}.</p> + +<p>For example, here's the file hierarchy for a simple project:</p> + +<pre class="no-pretty-print"> +MyProject/ + src/ <span style="color:black"> + MyActivity.java </span> + res/ + drawable/ <span style="color:black"> + icon.png </span> + layout/ <span style="color:black"> + main_layout.xml </span> + values/ <span style="color:black"> + strings.xml </span> +</pre> + +<p>This project includes an image resource, a layout resource, and string resource file.</p> + +<p>Table 1 lists the different {@code res/} sub-directories supported +and describes the types of resource files that belong in each one.</p> + +<p class="table-caption" id="table1"><strong>Table 1.</strong> Resource directories. Each directory +belongs inside the project {@code res/} directory.</p> + +<table> + <tr> + <th scope="col">Directory</th> + <th scope="col">Resource Types</th> + </tr> + + <tr> + <td><code>anim/</code></td> + <td>XML files that define tween animations. See <a +href="animation-resource.html">Animation Resources</a>.</td> + </tr> + + <tr> + <td><code>color/</code></td> + <td>XML files that define a state list of colors. See <a href="color-list-resource.html">Color +State List Resources</a></td> + </tr> + + <tr> + <td><code>drawable/</code></td> + <td><p>Bitmap files ({@code .png}, {@code .9.png}, {@code .jpg}, {@code .gif}) or XML files that +are compiled into the following Drawable resource subtypes:</p> + <ul> + <li>Bitmap files</li> + <li>Nine-Patches (re-sizable bitmaps)</li> + <li>State lists</li> + <li>Color drawables</li> + <li>Shapes</li> + <li>Animation drawables</li> + </ul> + <p>See <a href="drawable-resource.html">Drawable Resources</a>.</p> + </td> + </tr> + + <tr> + <td><code>layout/</code></td> + <td>XML files that define a user interface layout. + See <a href="layout-resource.html">Layout Resource</a>.</td> + </tr> + + <tr> + <td><code>menu/</code></td> + <td>XML files that define application menus, such as an Options Menu, Context Menu, or Sub +Menu. See <a href="menu-resource.html">Menu Resource</a>.</td> + </tr> + + <tr> + <td><code>raw/</code></td> + <td><p>Arbitrary files to save in their raw form. Files in here are not compressed by the +system. To open these resources with a raw {@link java.io.InputStream}, call {@link +android.content.res.Resources#openRawResource(int) +Resources.openRawResource()} with the resource ID, which is {@code R.raw.<em>filename</em>}.</p> + <p>However, if you require direct access to original file names and file hierarchy, instead of +using a resource ID to access your files, you might consider saving some resources in the {@code +assets/} directory, instead of {@code res/raw/}. You can query data in the {@code assets/} directory +like an ordinary file system, search through the directory and read raw data using {@link +android.content.res.AssetManager}.</p></td> + </tr> + + <tr> + <td><code>values/</code></td> + <td><p>XML files that contain simple values, such as strings, integers, and colors.</p> + <p>Unlike the other {@code res/} subdirectories, this one + can hold files that contain descriptions of more than one resource, rather than +just one resource for the file. The XML element types of an XML file in {@code values/} control +how these resources are defined in the {@code R} class. For example, a {@code <string>} +element will create an +{@code R.string} resource, and a {@code <color>} element will create an {@code R.color} +resource.</p> + <p>While the name of a file in this directory is arbitrary and not related to the name given +to a resource produced, you might want to separate different types of resources into different +files for easier maintenance. Here are some filename conventions for some of the different types +of resources you can save here:</p> + <ul> + <li>arrays.xml to define resource arrays (<a +href="more-resources.html#TypedArray">typed arrays</a>).</li> + <li>colors.xml to define <a +href="more-resources.html#Color">color values</a></li> + <li>dimens.xml to define <a +href="more-resources.html#Dimension">dimension values</a>.</li> + <li>strings.xml to define <a href="string-resource.html">string +values</a>.</li> + <li>styles.xml to define <a href="style-resource.html">styles</a>.</li> + </ul> + <p>See <a href="string-resource.html">String Resources</a>, + <a href="style-resource.html">Style Resource</a>, and + <a href="more-resources.html">More Resource Types</a>.</p> + </td> + </tr> + + <tr> + <td><code>xml/</code></td> + <td>Arbitrary XML files that are compiled and can be read at runtime by calling {@link +android.content.res.Resources#getXml(int) Resources.getXML()}. Various XML configuration files +must also be saved here, such as a <a +href="{@docRoot}guide/topics/search/searchable-config.html">searchable configuration</a>. +<!-- or preferences configuration. --></td> + </tr> +</table> + +<p>For more information about certain types of resources, see the <a +href="available-resources.html">Resource Types</a> documentation.</p> + + + + + + +<h2 id="AlternativeResources">Providing Alternative Resources</h2> + + +<div class="figure" style="width:441px"> +<img src="{@docRoot}images/resources/resource_devices_diagram2.png" height="137" alt="" /> +<p class="img-caption"> +<strong>Figure 1.</strong> Two device configurations, one using alternative resources.</p> +</div> + +<p>Almost every application should provide alternative resources to support specific device +configurations. For instance, you should include different drawable resources for different +screen densities and different string resources for different languages. At runtime, Android +will automatically detect the current device configuration and then load the appropriate +resources.</p> + +<p>For each set of resources for which you want to provide configuration-specific alternatives:</p> +<ol> + <li>Create a new directory in {@code res/} named in the form {@code +<em><resources_name></em>-<em><config_qualifier></em>}. + <ul> + <li><em>{@code <resources_name>}</em> is the directory name of the corresponding default +resources.</li> + <li><em>{@code <config_qualifier>}</em> is a name that specifies a configuration +for which these resources are to be used.</li> + </ul> + <p>You can append more than one <em>{@code <config_qualifier>}</em>. Separate each +one with a dash.</p> + </li> + <li>Save your alternative resources in this directory, named exactly the same as the default +resource files.</li> +</ol> + +<p>For example, here are some default and alternative resources:</p> + +<pre class="no-pretty-print"> +res/ + drawable/ <span style="color:black"> + icon.png + background.png </span> + drawable-hdpi/ <span style="color:black"> + icon.png + background.png </span> +</pre> + +<p>The {@code hdpi} qualifier indicates that the resources are for devices with a high-density +screen. While the images in each directory are different, the filenames are +identical. This way, the resource ID that you use to reference the {@code icon.png} image is +always the same. When you request the {@code icon} drawable, Android will select the +version of that drawable that best matches the current device configuration.</p> + +<p>Android supports several configuration qualifiers and you can +add multiple qualifiers to one directory name in order to further specify the configuration, by +separating the qualifiers with dashes. Table 2 lists the valid configuration qualifiers, in order +of precedence—they must be specified in the directory name in the order that they are listed +in the table.</p> + + +<p class="table-caption" id="table2"><strong>Table 2.</strong> Alternative resource qualifier +names.</p> +<table border="1"> + <tr> + <th>Qualifier</th> + <th>Values</th> + <th>Description</th> + </tr> + <tr> + <td>MCC and MNC</td> + <td>Examples:<br/> + <code>mcc310</code><br/> + <code><nobr>mcc310-mnc004</nobr></code><br/> + <code>mcc208-mnc00</code><br/> + etc. + </td> + <td> + <p>Specifies resources based on the mobile country code (MCC), optionally followed by mobile +network code (MNC) + from the SIM in the device. For example, <code>mcc310</code> is U.S. on any carrier, + <code>mcc310-mnc004</code> is U.S. on Verizon, and <code>mcc208-mnc00</code> is France on + Orange.</p> + <p>If the device uses a radio connection (GSM phone), the MCC will come + from the SIM, and the MNC will come from the network to which the + device is attached.</p> + <p>You might sometimes use the MCC alone, for example to include country-specific legal +resources in your application, but if you only need to specify based on language, then use the +language and region qualifier below. If you decide to use the MCC and MNC qualifier, you +should do so with great care and completely test that it works as expected.</p></td> + </tr> + <tr> + <td>Language and region</td> + <td>Examples:<br/> + <code>en</code><br/> + <code>fr</code><br/> + <code>en-rUS</code><br/> + <code>fr-rFR</code><br/> + <code>fr-rCA</code><br/> + etc. + </td> + <td><p>This is defined by a two-letter <a +href="http://www.loc.gov/standards/iso639-2/php/code_list.php">ISO + 639-1</a> language code, optionally followed by a two letter + <a +href="http://www.iso.org/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html">ISO + 3166-1-alpha-2</a> region code (preceded by lowercase "r"). + </p><p> + The codes are <em>not</em> case-sensitive; the {@code r} prefix is used to + distinguish the region portion. + You cannot specify a region alone.</p> + <p>This can change during the life +of your application if the user changes their language in the system settings. See <a +href="runtime-changes.html">Handling Runtime Changes</a> for information about +how this can affect your application during runtime.</p> + <p>See <a href="localization.html">Localization</a> for a complete guide to localizing +your application for other langauges.</p> + </td> + </tr> + <tr> + <td>Screen size</td> + <td> + <code>small</code><br/> + <code>normal</code><br/> + <code>large</code> + </td> + <td> + <ul> + <li> <b>Small screens</b> are based on the space available on a + QVGA low density screen. Considering a portrait HVGA display, this has + the same available width but less height -- it is 3:4 vs. HVGA's + 2:3 aspect ratio. Examples are QVGA low density and VGA high + density.</li> + <li> <b>Normal screens</b> are based on the traditional Android HVGA + medium density screen. A screen is considered to be normal if it is + at least this size (independent of density) and not larger. Examples + of such screens a WQVGA low density, HVGA medium density, WVGA + high density.</li> + <li> <b>Large screens</b> are based on the space available on a + VGA medium density screen. Such a screen has significantly more + available space in both width and height than an HVGA display. + Examples are VGA and WVGA medium density screens.</li> + </ul> + <p>See <a href="{@docRoot}guide/practices/screens_support.html">Supporting Multiple +Screens</a> for more information.</p> + </td> + </tr> + <tr> + <td>Wider/taller screens</td> + <td> + <code>long</code><br/> + <code>notlong</code> + </td> + <td> + <p>This is based purely on the aspect ratio of the screen (a "long" screen is wider): + <ul> + <li><strong>Long</strong>: WQVGA, WVGA, FWVGA</li> + <li><strong>Not long</strong>: QVGA, HVGA, and VGA</li> + </ul> + <p>This is not related to the screen orientation.</p> + </td> + </tr> + <tr> + <td>Screen orientation</td> + <td> + <code>port</code><br/> + <code>land</code> <!-- <br/> + <code>square</code> --> + </td> + <td> + <p>Portrait orientation ({@code port}) is vertical and landscape orientation +({@code land}) is horizontal. <!-- Square mode is currently not used. --> </p> + <p>This can change during the life of your application if the user rotates the +screen. See <a href="runtime-changes.html">Handling Runtime Changes</a> for information about +how this affects your application during runtime.</p> + </td> + </tr> + <tr> + <td>Dock mode</td> + <td> + <code>car</code><br/> + <code>desk</code> + </td> + <td> + <p>These configurations can be initiated when the device is placed in a dock.</p> + <p><em>Added in API Level 8.</em></p> + <p>This can change during the life of your application if the user places the device in a +dock. See <a href="runtime-changes.html">Handling Runtime Changes</a> for +information about how this affects your application during runtime. Also see {@link +android.app.UiModeManager}.</p> + </td> + </tr> + <tr> + <td>Night mode</td> + <td> + <code>night</code><br/> + <code>notnight</code> + </td> + <td> + <p> + These configurations can be initiated by the device light sensor (if available).</p> + <p><em>Added in API Level 8.</em></p> + <p>This can change during the life of your application if the device determines that the +user environment is a "night" (dark) setting. See <a href="runtime-changes.html">Handling Runtime +Changes</a> for information about how this affects your application during runtime. You can +also explicitly set this mode using {@link android.app.UiModeManager}.</p> + </td> + </tr> + <tr> + <td>Screen pixel density (dpi)</td> + <td> + <code>ldpi</code><br/> + <code>mdpi</code><br/> + <code>hdpi</code><br/> + <code>nodpi</code> + </td> + <td> + <p>The medium + density of traditional HVGA screens (mdpi) is defined to be approximately + 160dpi; low density (ldpi) is 120, and high density (hdpi) is 240. There + is thus a 4:3 scaling factor between each density, so a 9x9 bitmap + in ldpi would be 12x12 in mdpi and 16x16 in hdpi. The special + <code>nodpi</code> density can be used with bitmap resources to prevent + them from being scaled at load time to match the device density. + </p><p> + When Android selects which resource files to use, + it handles screen density differently than the other qualifiers. + In step 1 of <a href="#BestMatch">How Android finds the best + matching directory</a> (below), screen density is always considered to + be a match. In step 4, if the qualifier being considered is screen + density, Android will select the best final match at that point, + without any need to move on to step 5. + </p> + <p>See <a href="{@docRoot}guide/practices/screens_support.html">Supporting Multiple +Screens</a> for more information.</p> + </td> + </tr> + <tr> + <td>Touchscreen type</td> + <td> + <code>notouch</code><br/> + <code>stylus</code><br/> + <code>finger</code> + </td> + <td><p>If the device has a resistive touch screen that's suited for use with a stylus, +then it may use the {@code stylus} resources.</p> + </td> + </tr> + <tr> + <td>Keyboard availability</td> + <td> + <code>keysexposed</code><br/> + <code>keyshidden</code><br/> + <code>keyssoft</code> + </td> + <td> + <p>If your application has specific resources that should only be used with a soft keyboard, +use the <code>keyssoft</code> value. If you do not provide <code>keyssoft</code> resources, but do +provide <code>keysexposed</code> and <code>keyshidden</code>, and the device shows a soft keyboard, +the system will use <code>keysexposed</code> resources.</p> + <p>This can change during the life of your application if the user opens a keyboard. See <a +href="runtime-changes.html">Handling Runtime Changes</a> for information about how this affects your +application during runtime.</p> + </td> + </tr> + <tr> + <td>Primary text input method</td> + <td> + <code>nokeys</code><br/> + <code>qwerty</code><br/> + <code>12key</code> + </td> + <td><p>If the device has no hardware keys for text input, then it may use the {@code +nokeys} resources. Even if the device has a QWERTY keyboard but it is currently hidden, it may use +the {@code qwerty} resources.</td> + </tr> + <tr> + <td>Navigation key availability</td> + <td> + <code>navexposed</code><br/> + <code>navhidden</code> + </td> + <td> + <p> + If the device's navigation keys are currently available to + the user, it may use the {@code navexposed} resources; if they are not + available (such as behind a closed lid), it may use the {@code navhidden} resources.</p> + <p>This can change during the life of your application if the user reveals the navigation +keys. See <a href="runtime-changes.html">Handling Runtime Changes</a> for +information about how this affects your application during runtime.</p> + </td> + </tr> + <tr> + <td>Primary non-touch navigation method</td> + <td> + <code>nonav</code><br/> + <code>dpad</code><br/> + <code>trackball</code><br/> + <code>wheel</code> + </td> + <td><p>If the device has no navigation facility other than using the touchscreen, then it +may use the {@code nonav} resources.</p> + </td> + </tr> +<!-- DEPRECATED + <tr> + <td>Screen dimensions</td> + <td>Examples:<br/> + <code>320x240</code><br/> + <code>640x480</code><br/> + etc. + </td> + <td> + <p>The larger dimension must be specified first. <strong>This configuration is deprecated +and should not be used</strong>. Instead use "screen size," "wider/taller screens," and "screen +orientation" described above.</p> + </td> + </tr> +--> + <tr> + <td>API Level</td> + <td>Examples:<br/> + <code>v4</code><br/> + <code>v5</code><br/> + <code>v6</code><br/> + <code>v7</code><br/> + etc.</td> + <td> + <p>The API Level supported by the device, for example <code>v1</code> for API Level 1 +(Android 1.0) or <code>v5</code> for API Level 5 (Android 2.0). See the <a +href="{@docRoot}guide/appendix/api-levels.html">Android API Levels</a> document for more information +about these values.</p> + </td> + </tr> +</table> + +<p>Here are some important rules about using resource qualifier names:</p> + +<ul> + <li>You can specify multiple qualifiers for a single set of resources, separated by dashes. For +example, <code>drawable-en-rUS-land</code> applies to US-English devices in landscape +orientation.</li> + <li>The qualifiers must be in the order listed in <a href="#table2">Table 2</a> above. For +example: + <ul> + <li>Wrong: <code>drawable-hdpi-port/</code></li> + <li>Correct: <code>drawable-port-hdpi/</code></li> + </ul> + </li> + <li>Qualified directories cannot be nested. For example, you cannot have +<code>res/drawable/drawable-en/</code>.</li> + <li>Values are case-insensitive. The resource compiler converts directory names + to lower case before processing to avoid problems on case-insensitive + file systems. Any capitalization in the names is only to benefit readability.</li> + <li>Only one value for each qualifier type is supported. For example, if you want to use +the same drawable files for Spain and France, you <em>cannot</em> have a directory named +<code>drawable-rES-rFR/</code>. Instead you need two resource directories, such as +<code>drawable-rES/</code> and <code>drawable-rFR/</code>, which contain the appropriate files. +However, you are not required to actually duplicate the same files in both locations (which +could multiply the size of your package if the files are large). Instead, you can create a +reference to one instance of the resources. See <a href="#AliasResources">Creating +alias resources</a>, below.</li> +</ul> + + + +<h3 id="AliasResources">Creating alias resources</h3> + +<p>When you have a resource that you'd like to use for more than one device +configuration (but not for all configurations), you <em>don't</em> have to put the same resource in +each of the alternative resource directories. Instead, you can (in some cases) create an alternative +resource that acts as an alias for a resource saved in your default resource directory.</p> + +<p>For example, imagine you have an image, {@code icon.png}, and you have different versions of it +for different locales, but two locales, English-Canadian and French-Canadian, need to +use the same version. You might assume that you need to copy the Canadian version of the +icon into the alternative resource directory for both English-Canadian and French-Canadian, but it's +not true. What you can do instead is save the Canadian version as {@code icon_ca.png} (any name +other than {@code icon.png}) and put +it in the default {@code res/drawable/} directory. Then create an {@code icon.xml} file in {@code +res/drawable-en-rCA/} and {@code res/drawable-fr-rCA/} that refers to the {@code icon_ca.png} +resource using the {@code <bitmap>} element. This allows you to store just one version of the +PNG file and two small XML files that point to it. (An example XML file is shown below.)</p> + +<p class="note"><strong>Note:</strong> Not all resources offer a mechanism by which you can +create an alias to another resource. In particular, animation, menu, raw, and other unspecified +resources in the {@code xml/} directory don't provide this kind of feature.</p> + + +<h4>Drawable</h4> + +<p>To create an alias to an existing drawable, use the {@code <bitmap>} element. +For example:</p> + +<pre> +<?xml version="1.0" encoding="utf-8"?> +<bitmap xmlns:android="http://schemas.android.com/apk/res/android" + android:src="@drawable/icon_ca" /> +</pre> + +<p>If you save this file as {@code icon.xml}, it will be compiled into a resource that you +can reference as {@code R.drawable.icon}, but is actually an alias for the {@code +R.drawable.icon_ca} resource.</p> + + +<h4>Layout</h4> + +<p>To create an alias to an existing layout, use the {@code <include>} +element, wrapped in a {@code <merge>}. For example:</p> + +<pre> +<?xml version="1.0" encoding="utf-8"?> +<merge> + <include layout="@layout/main_ltr"/> +</merge> +</pre> + +<p>If you save this file as {@code main.xml}, it will be compiled into a resource you can reference +as {@code R.layout.main}, but is actually an alias for the {@code R.layout.main_ltr} +resource.</p> + + +<h4>Strings and other simple values</h4> + +<p>To create an alias to an existing string, simply use the resource ID of the desired +string as the value for the new string. For example:</p> + +<pre> +<?xml version="1.0" encoding="utf-8"?> +<resources> + <string name="hello">Hello</string> + <string name="hi">@string/hello</string> +</resources> +</pre> + +<p>The {@code R.string.hi} resource is now an alias for the {@code R.string.hello}.</p> + +<p> <a href="{@docRoot}guide/topics/resources/more-resources.html">Other simple values</a> work the +same way. For example, a color:</p> + +<pre> +<?xml version="1.0" encoding="utf-8"?> +<resources> + <color name="yellow">#f00</color> + <color name="highlight">@color/red</color> +</resources> +</pre> + + + + + + +<h2 id="BestMatch">How Android Finds the Best-matching Resource</h2> + +<p>Once you have saved alternative resources for your application, Android will pick which of the +various underlying resource files should be used at runtime for each resource +requested, depending on the current device configuration. To demonstrate how Android will select the +resource to use, assume the following drawables are available:</p> + +<pre class="no-pretty-print"> +res/drawable/ +res/drawable-en/ +res/drawable-fr-rCA/ +res/drawable-en-port/ +res/drawable-en-notouch-12key/ +res/drawable-port-ldpi/ +res/drawable-port-notouch-12key +</pre> + +<p>And assume the following is the device configuration:</p> + +<p style="margin-left:2em;"> +Locale = <code>en-GB</code> <br/> +Screen orientation = <code>port</code> <br/> +Screen pixel density = <code>hdpi</code> <br/> +Touchscreen type = <code>notouch</code> <br/> +Primary text input method = <code>12key</code> +</p> + + +<p>Here is how Android makes the drawable selection and how a drawable will be selected from the +configuration above: </p> + +<ol> + <li>Eliminate resource files that contradict the device configuration. + <p>The <code>drawable-fr-rCA/</code> directory will be eliminated, because it +contradicts the locale of the device.</p> +<pre class="no-pretty-print"> +drawable/ +drawable-en/ +<strike>drawable-fr-rCA/</strike> +drawable-en-port/ +drawable-en-notouch-12key/ +drawable-port-ldpi/ +drawable-port-notouch-12key +</pre> +<p class="note"><strong>Exception: </strong>Screen pixel density is the one qualifier that is not +used to eliminate files. Even though the screen density of the device is mdpi, +<code>drawable-port-ldpi/</code> is not eliminated because every screen density is +considered to be a match at this point.</p></li> + + <li>From <a href="#table2">Table 2</a>, pick the (next) highest-precedence qualifier in +the list. (Start with MCC, then move down through the list.) </li> + <li>Do any of the available resource directories include this qualifier? </li> + <ul> + <li>If No, return to step 2 and look at the next qualifier. In the example, + the answer is "no" until the language qualifier is reached.</li> + <li>If Yes, move on to step 4.</li> + </ul> + </li> + + <li>Eliminate resource directories that do not include this qualifier. In the example, the system +eliminates all the directories that do not include a language qualifier:</li> +<pre class="no-pretty-print"> +<strike>drawable/</strike> +drawable-en/ +drawable-en-port/ +drawable-en-notouch-12key/ +<strike>drawable-port-ldpi/</strike> +<strike>drawable-port-notouch-12key</strike> +</pre> +<p class="note"><strong>Exception:</strong> If the qualifier in question is screen pixel density, +Android will +select the option that most closely matches the device, and the selection process will be complete. +In general, Android will prefer scaling down a larger original image to scaling up a smaller +original image.</p> + </li> + + <li>Go back and repeat steps 2, 3, and 4 until only one choice remains. In the example, screen +orientation is the next qualifier for which there are any matches. +So, resources that do not specify a screen orientation are eliminated: +<pre class="no-pretty-print"> +<strike>drawable-en/</strike> +drawable-en-port/ +<strike>drawable-en-notouch-12key/</strike> +</pre> +<p>Only one choice remains, so the drawable will be taken from the {@code drawable-en-port} +directory.</p> + </li> +</ol> + +<p>Though this procedure is executed for each resource requested, the system will further optimize +some aspects. One such optimization is that once the device configuration is known, it might +completely eliminate alternative resources that can never match. For example, if the configuration +language is English ("en"), then any resource directory that has a language qualifier set to +something other than English will never be included in the pool of resources checked (though a +resource directory <em>without</em> the language qualifier is still included).</p> + +<p class="note"><strong>Note:</strong> The <em>precedence</em> of the qualifier (in <a +href="#table2">Table 2</a>) is more important +than the number of qualifiers that exactly match the device. For example, in step 4 above, the last +choice on the list includes three qualifiers that exactly match the device (orientation, touchscreen +type, and input method), while <code>drawable-en</code> has only one parameter that matches +(language). However, language has a higher precedence than these other qualifiers, so +<code>drawable-port-notouch-12key</code> +is out.</p> + +<p>The following flowchart summarizes how Android selects the resource directory to use.</p> +<p><img src="{@docRoot}images/resources/res-selection-flowchart.png" alt="" +height="471" /></p> + diff --git a/docs/html/guide/topics/resources/resources-i18n.jd b/docs/html/guide/topics/resources/resources-i18n.jd index fcf2af9..e1c96fb 100755 --- a/docs/html/guide/topics/resources/resources-i18n.jd +++ b/docs/html/guide/topics/resources/resources-i18n.jd @@ -1,808 +1,8 @@ -page.title=Resources and Internationalization -parent.title=Resources and Assets -parent.link=index.html +page.title=Application Resources @jd:body -<div id="qv-wrapper"> -<div id="qv"> +<script type="text/javascript"> + window.location = toRoot + "guide/topics/resources/index.html"; +</script> - <h2>Key classes</h2> - <ol> - <li>{@link android.content.res.Resources}</li> - </ol> - - <h2>In this document</h2> - <ol> - <li><a href="#intro">Introduction</a></li> - <li><a href="#CreatingResources">Creating Resources</a></li> - <li><a href="#UsingResources">Using Resources</a> - <ol> - <li><a href="#ResourcesInCode">Using Resources in Code</a></li> - <li><a href="#ReferencesToResources">References to Resources</a></li> - <li><a href="#ReferencesToThemeAttributes">References to Theme Attributes</a></li> - <li><a href="#UsingSystemResources">Using System Resources</a></li> - </ol> - </li> - <li><a href="#AlternateResources">Alternate Resources</a></li> - <li><a href="#ResourcesTerminology">Terminology</a></li> - <li><a href="#i18n">Internationalization (I18N)</a></li> - </ol> -</div> -</div> - -<p>Resources are external files (that is, non-code files) that are used by -your code and compiled into your application at build time. Android -supports a number of different kinds of resource files, including XML, -PNG, and JPEG files. The XML files have very different formats depending -on what they describe. This document describes what kinds of files are -supported, and the syntax or format of each.</p> -<p>Resources are externalized from source code, and XML files are compiled into -a binary, fast loading format for efficiency reasons. Strings, likewise, are compressed -into a more efficient storage form. It is for these reasons that we have these -different resource types in the Android platform.</p> - -<p>This is a fairly technically dense document, and together with the -<a href="available-resources.html">Available Resources</a> -document, they cover a lot of information about resources. It is not necessary -to know this document by heart to use Android, but rather to know that the -information is here when you need it.</p> - -<a name="intro"></a> -<h2>Introduction</h2> - -<p>This topic includes a terminology list associated with resources, and a series - of examples of using resources in code. For a complete guide to the supported - Android resource types, see - <a href="available-resources.html">Available Resources</a>. - </p> -<p>The Android resource system keeps track of all non-code - assets associated with an application. You use the - {@link android.content.res.Resources Resources} class to access your - application's resources; the Resources instance associated with your - application can generally be found through - {@link android.content.Context#getResources Context.getResources()}.</p> -<p>An application's resources are compiled into the application -binary at build time for you by the build system. To use a resource, -you must install it correctly in the source tree and build your -application. As part of the build process, symbols for each -of the resources are generated that you can use in your source -code -- this allows the compiler to verify that your application code matches -up with the resources you defined.</p> - -<p>The rest of this section is organized as a tutorial on how to -use resources in an application.</p> - -<a name="CreatingResources" id="CreatingResources"></a> -<h2>Creating Resources</h2> - -<p>Android supports string, bitmap, and many other types of resource. The syntax and format -of each, and where they're stored, depends upon the type of object. In -general, though, you create resources from three types of files: XML files -(everything but bitmaps and raw), bitmap files(for images) and Raw files (anything -else, for example sound files, etc.). In fact, there are two different types of -XML file as well, those that get compiled as-is into the package, and those that -are used to generate resources by aapt. Here is a list of each -resource type, the format of the file, a description of the file, and details -of any XML files. </p> - -<p>You will create and store your resource files under the appropriate -subdirectory under the <code>res/</code> directory in your project. Android -has a resource compiler (aapt) that compiles resources according to which -subfolder they are in, and the format of the file. Table 1 shows a list of the file -types for each resource. See the -<a href="available-resources.html">Available Resources</a> for -descriptions of each type of object, the syntax, and the format or syntax of -the containing file.</p> -<p class="caption">Table 1</p> -<table width="100%" border="1"> - <tr> - <th scope="col">Directory</th> - <th scope="col">Resource Types </th> - </tr> - <tr> - <td><code>res/anim/</code></td> - <td>XML files that are compiled into - <a href="available-resources.html#animationdrawable">frame by - frame animation</a> or - <a href="available-resources.html#tweenedanimation">tweened - animation</a> objects </td> - </tr> - <tr> - <td><code>res/drawable/</code></td> - <td><p>.png, .9.png, .jpg files that are compiled into the following - Drawable resource subtypes:</p> - <ul class="nolist"> - <li><a href="available-resources.html#imagefileresources">bitmap files</a></li> - <li><a href="available-resources.html#ninepatch">9-patches (resizable bitmaps)</a></li> - </ul> - <p>To get a resource of this type, use <code>mContext.getResources().getDrawable(R.drawable.<em>imageId</em>)</code></p> - <p class="note"><strong>Note:</strong> Image resources placed in here may - be automatically optimized with lossless image compression by the - <a href="{@docRoot}guide/developing/tools/aapt.html">aapt</a> tool. For example, a true-color PNG - that does not require more than 256 colors may be converted to an 8-bit PNG with a color palette. - This will result in an image of equal quality but which requires less memory. So be aware that the - image binaries placed in this directory can change during the build. If you plan on reading - an image as a bit stream in order to convert it to a bitmap, put your images in the - <code>res/raw/</code> folder instead, where they will not be optimized.</p> - </td> - </tr> - <tr> - <td><code>res/layout/</code></td> - <td>XML files that are compiled into screen layouts (or part of a screen). - See <a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a>.</td> - </tr> - <tr> - <td><code>res/values/</code></td> - <td><p>XML files that can be compiled into many kinds of resource.</p> - <p class="note"><strong>Note:</strong> Unlike the other res/ folders, this one - can hold any number of files that hold descriptions of resources to create - rather than the resources themselves. The XML element types control - where these resources are placed under the R class.</p> - <p>While the files can be named anything, these are - the typical files in this folder (the convention is to name - the file after the type of elements defined within):</p> - <ul> - <li><strong>arrays.xml</strong> to define arrays </li> - <!-- TODO: add section on arrays --> - <li><strong>colors.xml</strong> to define <a href="available-resources.html#colordrawableresources">color - drawables</a> and <a href="#colorvals">color string values</a>. - Use <code>Resources.getDrawable()</code> and - <code>Resources.getColor(), respectively,</code> - to get these resources.</li> - <li><strong>dimens.xml</strong> to define <a href="available-resources.html#dimension">dimension value</a>. Use <code>Resources.getDimension()</code> to get - these resources.</li> - <li><strong>strings.xml</strong> to define <a href="available-resources.html#stringresources">string</a> values (use either - <code>Resources.getString</code> or preferably <code>Resources.getText()</code> - to get - these resources. <code>getText()</code> will retain any rich text styling - which is usually desirable for UI strings.</li> - <li><strong>styles.xml</strong> to define <a href="available-resources.html#stylesandthemes">style</a> objects.</li> - </ul></td> - </tr> - <tr> - <td><code>res/xml/</code></td> - <td>Arbitrary XML files that are compiled and can be read at run time by - calling {@link android.content.res.Resources#getXml(int) Resources.getXML()}.</td> - </tr> - <tr> - <td><code>res/raw/</code></td> - <td>Arbitrary files to copy directly to the device. They are added uncompiled - to the compressed file that your application build produces. To use these - resources in your application, call {@link android.content.res.Resources#openRawResource(int) - Resources.openRawResource()} with the resource ID, which is R.raw.<em>somefilename</em>.</td> - </tr> -</table> -<p>Resources are compiled into the final APK file. Android creates a wrapper class, - called R, that you can use to refer to these resources in your code. R contains subclasses - named according to the path and file name of the source file</p> -<a name="colorvals" id="colorvals"></a> -<h3>Global Resource Notes</h3> -<ul> - <li>Several resources allow you to define colors. Android accepts color values - written in various web-style formats -- a hexadecimal constant in any of the - following forms: #RGB, #ARGB, #RRGGBB, #AARRGGBB. </li> - <li>All color values support setting an alpha channel value, where the first - two hexadecimal numbers specify the transparency. Zero in the alpha channel - means transparent. The default value is opaque. </li> -</ul> -<a name="UsingResources" id="UsingResources"></a> -<h2>Using Resources </h2> -<p>This section describes how to use the resources you've created. It includes the - following topics:</p> -<ul> - <li><a href="#ResourcesInCode">Using resources in code</a> - How to call - resources in your code to instantiate them. </li> - <li><a href="#ReferencesToResources">Referring to resources from other resources</a> - - You can reference resources from other resources. This lets you reuse common - resource values inside resources. </li> - <li><a href="#AlternateResources">Supporting Alternate Resources for Alternate - Configurations</a> - You can specify different resources - to load, depending on the language or display configuration of the host - hardware. </li> -</ul> -<p>At compile time, Android generates a class named R that contains resource identifiers - to all the resources in your program. This class contains several subclasses, - one for each type of resource supported by Android, and for which you provided - a resource file. Each class contains one or more identifiers for the compiled resources, - that you use in your code to load the resource. Here is a small resource file - that contains string, layout (screens or parts of screens), and image resources.</p> -<p class="note"><strong>Note:</strong> the R class is an auto-generated file and is not -designed to be edited by hand. It will be automatically re-created as needed when -the resources are updated.</p> -<pre class="prettyprint">package com.android.samples; -public final class R { - public static final class string { - public static final int greeting=0x0204000e; - public static final int start_button_text=0x02040001; - public static final int submit_button_text=0x02040008; - public static final int main_screen_title=0x0204000a; - }; - public static final class layout { - public static final int start_screen=0x02070000; - public static final int new_user_pane=0x02070001; - public static final int select_user_list=0x02070002; - - }; - public static final class drawable { - public static final int company_logo=0x02020005; - public static final int smiling_cat=0x02020006; - public static final int yellow_fade_background=0x02020007; - public static final int stretch_button_1=0x02020008; - - }; -}; -</pre> -<a name="ResourcesInCode" id="ResourcesInCode"></a> -<h3>Using Resources in Code </h3> - -<p>Using resources in code is just a matter of knowing the full resource ID -and what type of object your resource has been compiled into. Here is the -syntax for referring to a resource:</p> -<p><code>R.<em>resource_type</em>.<em>resource_name</em></code></p> -<p>or</p> -<p><code>android.R.<em>resource_type</em>.<em>resource_name</em></code></p> - -<p>Where <code>resource_type</code> is the R subclass that holds a specific type -of resource. <code>resource_name</code> is the <em>name</em> attribute for resources -defined in XML files, or the file name (without the extension) for resources -defined by other file types. Each type of resource will be added to a specific -R subclass, depending on the type of resource it is; to learn which R subclass -hosts your compiled resource type, consult the -<a href="available-resources.html">Available Resources</a> document. Resources compiled by your own application can -be referred to without a package name (simply as -<code>R.<em>resource_type</em>.<em>resource_name</em></code>). Android contains -a number of standard resources, such as screen styles and button backgrounds. To -refer to these in code, you must qualify them with <code>android</code>, as in -<code>android.R.drawable.button_background</code>.</p> - -<p>Here are some good and bad examples of using compiled resources in code:</p> - -<pre class="prettyprint">// Load a background for the current screen from a drawable resource. -this.getWindow().setBackgroundDrawableResource(R.drawable.my_background_image); - -// WRONG Sending a string resource reference into a -// method that expects a string. -this.getWindow().setTitle(R.string.main_title); - -// RIGHT Need to get the title from the Resources wrapper. -this.getWindow().setTitle(Resources.getText(R.string.main_title)); - -// Load a custom layout for the current screen. -setContentView(R.layout.main_screen); - -// Set a slide in animation for a ViewFlipper object. -mFlipper.setInAnimation(AnimationUtils.loadAnimation(this, - R.anim.hyperspace_in)); - -// Set the text on a TextView object. -TextView msgTextView = (TextView)findViewByID(R.id.msg); -msgTextView.setText(R.string.hello_message); </pre> - -<a name="ReferencesToResources" id="ReferencesToResources"></a> -<h3>References to Resources</h3> - -<p>A value supplied in an attribute (or resource) can also be a reference to -a resource. This is often used in layout files to supply strings (so they -can be localized) and images (which exist in another file), though a reference -can be any resource type including colors and integers.</p> - -<p>For example, if we have -<a href="available-resources.html#colordrawableresources">color -resources</a>, we can write a layout file that sets the text color size to be -the value contained in one of those resources:</p> - -<pre> -<?xml version="1.0" encoding="utf-8"?> -<EditText id="text" - xmlns:android="http://schemas.android.com/apk/res/android" - android:layout_width="fill_parent" android:layout_height="fill_parent" - <strong>android:textColor="@color/opaque_red"</strong> - android:text="Hello, World!" /> -</pre> - -<p>Note here the use of the '@' prefix to introduce a resource reference -- the -text following that is the name of a resource in the form -of <code>@[package:]type/name</code>. In this case we didn't need to specify -the package because we are referencing a resource in our own package. To -reference a system resource, you would need to write:</p> - -<pre> -<?xml version="1.0" encoding="utf-8"?> -<EditText id="text" - xmlns:android="http://schemas.android.com/apk/res/android" - android:layout_width="fill_parent" android:layout_height="fill_parent" - android:textColor="@<strong>android:</strong>color/opaque_red" - android:text="Hello, World!" /> -</pre> - -<p>As another example, you should always use resource references when supplying -strings in a layout file so that they can be localized:</p> - -<pre> -<?xml version="1.0" encoding="utf-8"?> -<EditText id="text" - xmlns:android="http://schemas.android.com/apk/res/android" - android:layout_width="fill_parent" android:layout_height="fill_parent" - android:textColor="@android:color/opaque_red" - android:text="@string/hello_world" /> -</pre> - -<p>This facility can also be used to create references between resources. -For example, we can create new drawable resources that are aliases for -existing images:</p> - -<pre> -<?xml version="1.0" encoding="utf-8"?> -<resources> - <drawable id="my_background">@android:drawable/theme2_background</drawable> -</resources> -</pre> - -<a name="ReferencesToThemeAttributes"></a> -<h3>References to Theme Attributes</h3> - -<p>Another kind of resource value allows you to reference the value of an -attribute in the current theme. This attribute reference can <em>only</em> -be used in style resources and XML attributes; it allows you to customize the -look of UI elements by changing them to standard variations supplied by the -current theme, instead of supplying more concrete values.</p> - -<p>As an example, we can use this in our layout to set the text color to -one of the standard colors defined in the base system theme:</p> - -<pre> -<?xml version="1.0" encoding="utf-8"?> -<EditText id="text" - xmlns:android="http://schemas.android.com/apk/res/android" - android:layout_width="fill_parent" android:layout_height="fill_parent" - <strong>android:textColor="?android:textDisabledColor"</strong> - android:text="@string/hello_world" /> -</pre> - -<p>Note that this is very similar to a resource reference, except we are using -an '?' prefix instead of '@'. When you use this markup, you are supplying -the name of an attribute resource that will be looked up in the theme -- -because the resource tool knows that an attribute resource is expected, -you do not need to explicitly state the type (which would be -<code>?android:attr/android:textDisabledColor</code>).</p> - -<p>Other than using this resource identifier to find the value in the -theme instead of raw resources, the name syntax is identical to the '@' format: -<code>?[namespace:]type/name</code> with the type here being optional.</p> - -<a name="UsingSystemResources"></a> -<h3>Using System Resources</h3> - -<p>Many resources included with the system are available to applications. -All such resources are defined under the class "android.R". For example, -you can display the standard application icon in a screen with the following -code:</p> - -<pre class="prettyprint"> -public class MyActivity extends Activity -{ - public void onStart() - { - requestScreenFeatures(FEATURE_BADGE_IMAGE); - - super.onStart(); - - setBadgeResource(android.R.drawable.sym_def_app_icon); - } -} -</pre> - -<p>In a similar way, this code will apply to your screen the standard -"green background" visual treatment defined by the system:</p> - -<pre class="prettyprint"> -public class MyActivity extends Activity -{ - public void onStart() - { - super.onStart(); - - setTheme(android.R.style.Theme_Black); - } -} -</pre> - -<a name="AlternateResources" id="AlternateResources"></a> -<h2>Alternate Resources (for alternate languages and configurations)</h2> - -<p>You can supply different resources for your application to use depending on the UI -language or hardware configuration on the device. Note that although you can -include different string, layout, and other resources, the SDK does not expose -methods to let you specify which alternate resource set to load. Android -detects the proper set for the hardware and location, and loads them as -appropriate. Users can select alternate language settings using the settings -panel on the device. </p> -<p>To include alternate resources, create parallel resource folders with -qualifiers appended to the folder names, indicating the configuration it -applies to (language, screen orientation, and so on). For example, here is a -project that holds one string resource file for English, and another for -French:</p> - -<pre> -MyApp/ - res/ - values-en/ - strings.xml - values-fr/ - strings.xml -</pre> - -<p>Android supports several types of qualifiers, with various values for each. -Append these to the end of the resource folder name, separated by dashes. You -can add multiple qualifiers to each folder name, but they must appear in the -order they are listed here. For example, a folder containing drawable -resources for a fully specified configuration would look like this:</p> - -<pre> -MyApp/ - res/ - drawable-en-rUS-large-long-port-mdpi-finger-keysexposed-qwerty-navexposed-dpad-480x320/ -</pre> - -<p>More typically, you will only specify a few specific configuration options. You may drop any of the values from the -complete list, as long as the remaining values are still in the same -order:</p> - -<pre> -MyApp/ - res/ - drawable-en-rUS-finger/ - drawable-port/ - drawable-port-mdpi/ - drawable-qwerty/ -</pre> -<p>Table 2 lists the valid folder-name qualifiers, in order of precedence. Qualifiers that are listed higher in the table take precedence over those listed lower, as described in <a href="#best-match">How Android finds the best matching directory</a>. </p> -<p class="caption" id="table2">Table 2</p> -<table border="1"> - <tr> - <th> Qualifier </th> - <th> Values </th> - </tr> - <tr> - <td>MCC and MNC</td> - <td><p>The mobile country code optionally followed by mobile network code - from the SIM in the device. For example - <code>mcc310</code> (U.S. on any carrier); - <code>mcc310-mnc004</code> (U.S., Verizon brand); - <code>mcc208-mnc00</code> (France, Orange brand); - <code>mcc234-mnc00</code> (U.K., BT brand). - </p><p> - If the device uses a radio connection (GSM phone), the MCC will come - from the SIM, and the MNC will come from the network to which the - device is attached. You might sometimes use the MCC alone, for example - to include country-specific legal resources in your application. If - your application specifies resources for a MCC/MNC combination, those - resources can only be used if both the MCC and the MNC match. </p></td> - </tr> - <tr> - <td>Language and region</td> - <td><p>The two letter <a href="http://www.loc.gov/standards/iso639-2/php/code_list.php">ISO - 639-1</a> language code optionally followed by a two letter - <a href="http://www.iso.org/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html">ISO - 3166-1-alpha-2</a> region code (preceded by lowercase "r"). For example - <code>fr</code>, <code>en-rUS</code>, <code>fr-rFR</code>, <code>es-rES</code>. - </p><p> - The codes are <em>not</em> case-sensitive; the r prefix is used to - distinguish the region portion. - You cannot specify a region alone, but you can specify a language alone, - for example <code>en</code>, <code>fr</code>, <code>es</code>.</p> </td> - </tr> - <tr> - <td>Screen dimensions</td> - <td><p><code>small</code>, <code>normal</code>, <code>large</code> - </p><p> - Specify that the resource is for a particular class of screen. - The meanings of these are:</p> - <ul> - <li> <b>Normal screens</b> are based on the traditional Android HVGA - medium density screen. A screen is considered to be normal if it is - at least this size (independent of density) and not large. Examples - of such screens a WQVGA low density, HVGA medium density, WVGA - high density. - <li> <b>Small screens</b> are based on the space available on a - QVGA low density screen. Considering a portrait HVGA display, this has - the same available width but less height -- it is 3:4 vs. HVGA's - 2:3 aspect ratio. Examples are QVGA low density and VGA high - density. - <li> <b>Large screens</b> are based on the space available on a - VGA medium density screen. Such a screen has significantly more - available space in both width and height than an HVGA display. - Examples are VGA and WVGA medium density screens. - </td> - </tr> - <tr> - <td>Wider/taller screens</td> - <td><p><code>long</code>, <code>notlong</code> - </p><p> - Specify that the resource is for a taller/wider than traditional - screen. This is based purely on the aspect ration of the screen: - QVGA, HVGA, and VGA are notlong; WQVGA, WVGA, FWVGA are long. Note - that long may mean either wide or tall, depending on the current - orientation.</p> - </td> - </tr> - <tr> - <td>Screen orientation</td> - <td><p><code>port</code>, <code>land</code>, <code>square</code> - </p><p> - Specifies that the resource is for a screen that is tall (port) - or wide (land); square is not currently used.</p> - </td> - </tr> - <tr> - <td>Screen pixel density</td> - <td><p><code>ldpi</code>, <code>mdpi</code>, <code>hdpi</code>, <code>nodpi</code> - </p><p> - Specifies the screen density the resource is defined for. The medium - density of traditional HVGA screens (mdpi) is defined to be approximately - 160dpi; low density (ldpi) is 120, and high density (hdpi) is 240. There - is thus a 4:3 scaling factor between each density, so a 9x9 bitmap - in ldpi would be 12x12 is mdpi and 16x16 in hdpi. The special - <code>nodpi</code> density can be used with bitmap resources to prevent - them from being scaled at load time to match the device density. - </p><p> - When Android selects which resource files to use, - it handles screen density differently than the other qualifiers. - In step 1 of <a href="#best-match">How Android finds the best - matching directory</a> (below), screen density is always considered to - be a match. In step 4, if the qualifier being considered is screen - density, Android will select the best final match at that point, - without any need to move on to step 5. - </p><p> - You can also specify explicit densities like <code>92dpi</code> - or <code>108dpi</code>, but these are not fully supported by the - system so should not be used. - </p> - </td> - </tr> - <tr> - <td>Touchscreen type</td> - <td><code>notouch</code>, <code>stylus</code>, <code>finger</code></td> - </tr> - <tr> - <td>Whether the keyboard is available to the user</td> - <td><p><code>keysexposed</code>, <code>keyshidden</code>, <code>keyssoft</code> - </p><p> - If your application has specific resources that should only be used with a soft keyboard, use the <code>keyssoft</code> value. If no <code>keyssoft</code> resources are available (only <code>keysexposed</code> and <code>keyshidden</code>) and the device shows a soft keyboard, the system will use <code>keysexposed</code> resources.</p> </td> - </tr> - <tr> - <td>Primary text input method</td> - <td><code>nokeys</code>, <code>qwerty</code>, <code>12key</code> </td> - </tr> - <tr> - <td>Whether the navigation keys are available to the user</td> - <td><p><code>navexposed</code>, <code>navhidden</code> - </p><p> - If the hardware's navigation keys are currently available to - the user, the navexposed resources will be used; if they are not - available (such as behind a closed lid), navhidden will be used.</p></td> - </tr> - <tr> - <td>Primary non-touchscreen<br /> - navigation method</td> - <td><code>nonav</code>, <code>dpad</code>, <code>trackball</code>, <code>wheel</code> </td> - </tr> - <tr> - <td>Screen dimensions</td> - <td><code>320x240</code>, <code>640x480</code>, etc. The larger dimension - must be specified first. This configuration is deprecated and - should not be used; use instead screen dimension, wider/taller - screens, and screen orientation described above.</td> - </tr> - <tr> - <td>SDK version</td> - <td>The SDK version supported by the device, for example <code>v3</code>. The Android 1.0 SDK is <code>v1, </code> the 1.1 SDK is <code>v2</code>, and the 1.5 SDK is <code>v3</code>.</td> - </tr> - <tr> - <td>(Minor version)</td> - <td>(You cannot currently specify minor version. It is always set to 0.)</td> - </tr> -</table> - -<p>This list does not include device-specific parameters such as carrier, -branding, device/hardware, or manufacturer. Everything that an application -needs to know about the device that it is running on is encoded via the -resource qualifiers in the table above.</p> - -<p>All resource directories, qualified and unqualified, live under the <code>res/</code> folder. Here are some guidelines on qualified resource directory names:</p> - -<ul> - <li>You can specify multiple qualifiers, separated by dashes. For example, <code>drawable-en-rUS-land</code> will apply to US-English - devices in landscape orientation. </li> - <li>The qualifiers must be in the order listed in <a href="#table2">Table 2</a> above. For example: - <ul> - <li>Correct: <code>values-mcc460-nokeys/</code></li> - <li>Incorrect: <code>values-nokeys-mcc460/</code></li> - </ul> - </li> - <li>Values are case-insensitive. The resource compiler converts folder names - to lower case before processing to avoid problems in case-insensitive - file systems. On case-sensitive file systems, you should keep all names - lower-case or at least use a consistent case to protect your future - sanity when trying to find a resource file.</li> - <li>Only one value for each qualifier type is supported. For example, if you want to use exactly the same drawable files for Spain and France, you will need two resource directories, such as <code>drawable-rES/</code> and <code>drawable-rFR/</code>, containing identical files. You cannot - have a directory named <code>drawable-rES-rFR/</code>. </li> - <li>Qualified directories cannot be nested. For example, you cannot have <code>res/drawable/drawable-en</code>. </li> -</ul> - -<h3>How resources are referenced in code</h3> -<p>All resources will be referenced in code or resource reference syntax by - their simple, undecorated names. So if a resource were named this:<br /> - <code>MyApp/res/drawable-port-mdpi/myimage.png</code><br /> - It would be referenced as this:<br /> - <code>R.drawable.myimage</code> (code)<br /> - <code>@drawable/myimage</code> (XML)</p> -<p>If several drawable directories are available, Android will select one of them (as described below) and load <code>myimage.png</code> from it.</p> -<h3 id="best-match">How Android finds the best matching directory </h3> - -<p>Android will pick which of the various underlying resource files should be -used at runtime, depending on the current configuration of the device. -The example used here assumes the following device configuration:</p> - - <p style="margin-left:2em">Locale = <code>en-GB</code><br> - Screen orientation = <code>port</code><br> - Screen pixel density = <code>mdpi</code><br> - Touchscreen type = <code>notouch</code><br> - Primary text input method = <code>12key</code><br> - </p> - -<p>Here is how Android makes the selection: </p> -<ol> - <li> - Eliminate resource files that contradict the - device configuration. For example, assume that the following resource directories are available for drawables. The <code>drawable-fr-rCA/</code> directory will be eliminated, because it contradicts the locale of the device.<br> -<pre>MyApp/res/drawable/ -MyApp/res/drawable-en/ -<strike>MyApp/res/drawable-fr-rCA/</strike> -MyApp/res/drawable-en-port/ -MyApp/res/drawable-en-notouch-12key/ -MyApp/res/drawable-port-ldpi/ -MyApp/res/drawable-port-notouch-12key</pre> - <strong>Exception: </strong>Screen pixel density is the one qualifier that is not used to eliminate files. Even though the screen density of the device is medium dpi, <code>drawable-port-ldpi/</code> is not eliminated from the list, because every screen density is considered to be a - match at this point.</li> - <li>From <a href="#table2">Table 2</a>, pick the highest-precedence qualifier that remains in the list. (Start with MCC, then move down through the list.) </li> - <li>Do any of the available resource directories include this qualifier? </li> - <ul> - <li>If No, return to step 2 and look at the next qualifier listed in Table 2. In our example, the answer is "no" until we reach Language.</li> - <li>If Yes, move on to step 4.</li> - </ul> - <li>Eliminate resource directories that do not include this qualifier. In our example, we eliminate all the directories that do not include a language qualifier. </li> - <pre><strike>MyApp/res/drawable/</strike> -MyApp/res/drawable-en/ -MyApp/res/drawable-en-port/ -MyApp/res/drawable-en-notouch-12key/ -<strike>MyApp/res/drawable-port-ldpi/</strike> -<strike>MyApp/res/drawable-port-notouch-12key</strike></pre> - <strong>Exception:</strong> If the qualifier in question is screen pixel density, Android will select the option that most closely matches the device, and the selection process will be complete. In general, Android will prefer scaling down a larger original image to scaling up a smaller original image.<br><br></li> - -<li>Go back and repeat steps 2, 3, and 4 until only one choice remains. In the example, screen orientation is the next qualifier in the table for which we have any matches. - Eliminate resources that do not specify a screen orientation. </p> - <pre><strike>MyApp/res/drawable-en/</strike> -MyApp/res/drawable-en-port/ -<strike>MyApp/res/drawable-en-notouch-12key/</strike></pre> - Only one choice remains, so that's it. When drawables are called for in this - example application, the Android system will load resources from the - <code>MyApp/res/drawable-en-port/</code> directory. In addition, if the - resource being loaded is a bitmap, it will be scaled up so that its supplied - low density matches the device's medium density. -</ol> -<p class="note"><strong>Tip:</strong> The <em>precedence</em> of the qualifiers is more important than the number of qualifiers that exactly match the device. For example, in step 4 above, the last choice on the list includes three qualifiers that exactly match the device (orientation, touchscreen type, and input method), while <code>drawable-en</code> has only one parameter that matches (language). However, language has a higher precedence, so <code>drawable-port-notouch-12key</code> is out.</p> -<p>This flowchart summarizes how Android selects resource directories to load.</p> -<p><img src="../../../images/resources/res-selection-flowchart.png" alt="resource-selection" width="461" height="471" style="margin:15px"></p> -<h3>Terminology</h3> -<p>The resource system brings a number of different pieces together to -form the final complete resource functionality. To help understand the -overall system, here are some brief definitions of the core concepts and -components you will encounter in using it:</p> - -<p><strong>Asset</strong>: A single blob of data associated with an application. This -includes object files compiled from the Java source code, graphics (such as PNG -images), XML files, etc. These files are organized in a directory hierarchy -that, during final packaging of the application, is bundled together into a -single ZIP file.</p> - -<p><strong>aapt</strong>: Android Asset Packaging Tool. The tool that generates the -final ZIP file of application assets. In addition to collecting raw assets -together, it also parses resource definitions into binary asset data.</p> - -<p><strong>Resource Table</strong>: A special asset that aapt generates for you, -describing all of the resources contained in an application/package. -This file is accessed for you by the Resources class; it is not touched -directly by applications.</p> - -<p><strong>Resource</strong>: An entry in the Resource Table describing a single -named value. Broadly, there are two types of resources: primitives and -bags.</p> - -<p><strong>Resource Identifier</strong>: In the Resource Table all resources are -identified by a unique integer number. In source code (resource descriptions, -XML files, Java source code) you can use symbolic names that stand as constants for -the actual resource identifier integer.</p> - -<p><strong>Primitive Resource</strong>: All primitive resources can be written as a -simple string, using formatting to describe a variety of primitive types -included in the resource system: integers, colors, strings, references to -other resources, etc. Complex resources, such as bitmaps and XML -describes, are stored as a primitive string resource whose value is the path -of the underlying Asset holding its actual data.</p> - -<p><strong>Bag Resource</strong>: A special kind of resource entry that, instead of a -simple string, holds an arbitrary list of name/value pairs. Each name is -itself a resource identifier, and each value can hold -the same kinds of string formatted data as a normal resource. Bags also -support inheritance: a bag can inherit the values from another bag, selectively -replacing or extending them to generate its own contents.</p> - -<p><strong>Kind</strong>: The resource kind is a way to organize resource identifiers -for various purposes. For example, drawable resources are used to -instantiate Drawable objects, so their data is a primitive resource containing -either a color constant or string path to a bitmap or XML asset. Other -common resource kinds are string (localized string primitives), color -(color primitives), layout (a string path to an XML asset describing a view -layout), and style (a bag resource describing user interface attributes). -There is also a standard "attr" resource kind, which defines the resource -identifiers to be used for naming bag items and XML attributes</p> - -<p><strong>Style</strong>: The name of the resource kind containing bags that are used -to supply a set of user interface attributes. For example, a TextView class may -be given a style resource that defines its text size, color, and alignment. -In a layout XML file, you associate a style with a bag using the "style" -attribute, whose value is the name of the style resource.</p> - -<p><strong>Style Class</strong>: Specifies a related set of attribute resources. -This data is not placed in the resource table itself, but used to generate -constants in the source code that make it easier for you to retrieve values out of -a style resource and/or XML tag's attributes. For example, the -Android platform defines a "View" style class that -contains all of the standard view attributes: padding, visibility, -background, etc.; when View is inflated it uses this style class to -retrieve those values from the XML file (at which point style and theme -information is applied as appropriate) and load them into its instance.</p> - -<p><strong>Configuration</strong>: For any particular resource identifier, there may be -multiple different available values depending on the current configuration. -The configuration includes the locale (language and country), screen -orientation, etc. The current configuration is used to -select which resource values are in effect when the resource table is -loaded.</p> - -<p><strong>Theme</strong>: A standard style resource that supplies global -attribute values for a particular context. For example, when writing an -Activity the application developer can select a standard theme to use, such -as the Theme.White or Theme.Black styles; this style supplies information -such as the screen background image/color, default text color, button style, -text editor style, text size, etc. When inflating a layout resource, most -values for widgets (the text color, selector, background) if not explicitly -set will come from the current theme; style and attribute -values supplied in the layout can also assign their value from explicitly -named values in the theme attributes if desired.</p> - -<p><strong>Overlay</strong>: A resource table that does not define a new set of resources, -but instead replaces the values of resources that are in another resource table. -Like a configuration, this is applied at load time -to the resource data; it can add new configuration values (for example -strings in a new locale), replace existing values (for example change -the standard white background image to a "Hello Kitty" background image), -and modify resource bags (for example change the font size of the Theme.White -style to have an 18 pt font size). This is the facility that allows the -user to select between different global appearances of their device, or -download files with new appearances.</p> - -<h2>Resource Reference</h2> -<p>The <a href="available-resources.html">Available Resources</a> -document provides a detailed list of the various types of resource and how to use them -from within the Java source code, or from other references.</p> - -<a name="i18n" id="i18n"></a> -<h2>Internationalization and Localization</h2> -<p class="note"><strong>Coming Soon:</strong> Internationalization and Localization are -critical, but are also not quite ready yet in the current SDK. As the -SDK matures, this section will contain information on the Internationalization -and Localization features of the Android platform. In the meantime, it is a good -idea to start by externalizing all strings, and practicing good structure in -creating and using resources.</p> +<p><strong>This document has moved. Please see <a href="index.html">Application Resources</a>.</strong></p>
\ No newline at end of file diff --git a/docs/html/guide/topics/resources/string-resource.jd b/docs/html/guide/topics/resources/string-resource.jd new file mode 100644 index 0000000..81c5d55 --- /dev/null +++ b/docs/html/guide/topics/resources/string-resource.jd @@ -0,0 +1,444 @@ +page.title=String Resources +parent.title=Resource Types +parent.link=available-resources.html +@jd:body + +<p>A string resource provides text strings for your application +with optional text styling and formatting. There are three types of resources that can provide +your application with strings:</p> + +<dl> + <dt><a href="#String">String</a></dt> + <dd>XML resource that provides a single string.</dd> + <dt><a href="#StringArray">String Array</a></dt> + <dd>XML resource that provides an array of strings.</dd> + <dt><a href="#Plurals">Plurals</a></dt> + <dd>XML resource that carries different strings for different pluralizations + of the same word or phrase.</dd> +</dl> + +<p>All strings are capable of applying some styling markup and formatting arguments. For +information about styling and formatting strings, see the section about <a +href="#FormattingAndStyling">Formatting and Styling</a>.</p> + + + + +<h2 id="String">String</h2> + +<p>A single string that can be referenced from the application or from other resource files (such +as an XML layout).</p> + +<p class="note"><strong>Note:</strong> A string is a simple resource that is referenced +using the value provided in the {@code name} attribute (not the name of the XML file). So, you can +combine string resources with other simple resources in the one XML file, +under one {@code <resources>} element.</p> + +<dl class="xml"> + +<dt>file location:</dt> +<dd><code>res/values/<em>filename</em>.xml</code><br/> +The filename is arbitrary. The {@code <string>} element's {@code name} will be used as the +resource ID.</dd> + +<dt>compiled resource datatype:</dt> +<dd>Resource pointer to a {@link java.lang.String}.</dd> + +<dt>resource reference:</dt> +<dd> +In Java: <code>R.string.<em>string_name</em></code><br/> +In XML:<code>@string/<em>string_name</em></code> +</dd> + +<dt>syntax:</dt> +<dd> +<pre class="stx"> +<?xml version="1.0" encoding="utf-8"?> +<<a href="#string-resources-element">resources</a>> + <<a href="#string-element">string</a> + name="<em>string_name</em>" + ><em>text_string</em></string> +</resources> +</pre> +</dd> + +<dt>elements:</dt> +<dd> +<dl class="tag-list"> + + <dt id="string-resources-element"><code><resources></code></dt> + <dd><strong>Required.</strong> This must be the root node. + <p>No attributes.</p> + </dd> + <dt id="string-element"><code><string></code></dt> + <dd>A string, which can include styling tags. Beware that you must escape apostrophes and +quotation marks. For more information about how to properly style and format your strings see <a +href="#FormattingAndStyling">Formatting and Styling</a>, below. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>name</code></dt> + <dd><em>String</em>. A name for the string. This name will be used as the resource +ID.</dd> + </dl> + </dd> + +</dl> +</dd> <!-- end elements and attributes --> + +<dt>example:</dt> +<dd>XML file saved at <code>res/values/strings.xml</code>: +<pre> +<?xml version="1.0" encoding="utf-8"?> +<resources> + <string name="hello">Hello!</string> +</resources> +</pre> + + <p>This layout XML applies a string to a View:</p> +<pre> +<TextView + android:layout_width="fill_parent" + android:layout_height="wrap_content" + <strong>android:text="@string/hello"</strong> /> +</pre> + + <p>This application code retrieves a string:</p> +<pre> +String string = {@link android.content.Context#getString(int) getString}(R.string.hello); +</pre> +<p>You can use either {@link android.content.Context#getString(int)} or +{@link android.content.Context#getText(int)} to retieve a string. {@link +android.content.Context#getText(int)} will retain any rich text styling applied to the string.</p> + +</dd> <!-- end example --> + +</dl> + + + + + + + + + +<h2 id="StringArray">String Array</h2> + +<p>An array of strings that can be referenced from the application.</p> + +<p class="note"><strong>Note:</strong> A string array is a simple resource that is referenced +using the value provided in the {@code name} attribute (not the name of the XML file). As +such, you can combine string array resources with other simple resources in the one XML file, +under one {@code <resources>} element.</p> + +<dl class="xml"> + +<dt>file location:</dt> +<dd><code>res/values/<em>filename</em>.xml</code><br/> +The filename is arbitrary. The {@code <string-array>} element's {@code name} will be used as the +resource ID.</dd> + +<dt>compiled resource datatype:</dt> +<dd>Resource pointer to an array of {@link java.lang.String}s.</dd> + +<dt>resource reference:</dt> +<dd> +In Java: <code>R.array.<em>string_array_name</em></code> +</dd> + +<dt>syntax:</dt> +<dd> +<pre class="stx"> +<?xml version="1.0" encoding="utf-8"?> +<<a href="#string-array-resources-element">resources</a>> + <<a href="#string-array-element">string-array</a> + name="<em>string_array_name</em>"> + <<a href="#string-array-item-element">item</a> + ><em>text_string</em></item> + </string-array> +</resources> +</pre> +</dd> + +<dt>elements:</dt> +<dd> +<dl class="tag-list"> + <dt id="string-array-resources-element"><code><resources></code></dt> + <dd><strong>Required.</strong> This must be the root node. + <p>No attributes.</p> + </dd> + <dt id="string-array-element"><code><string-array></code></dt> + <dd>Defines an array of strings. Contains one or more {@code <item>} elements. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>name</code></dt> + <dd><em>String</em>. A name for the array. This name will be used as the resource +ID to reference the array.</dd> + </dl> + + </dd> + <dt id="string-array-item-element"><code><item></code></dt> + <dd>A string, which can include styling tags. The value can be a referenced to another +string resource. Must be a child of a {@code <string-array>} element. Beware that you +must escape apostrophes and +quotation marks. See <a href="#FormattingAndStyling">Formatting and Styling</a>, below, for +information about to properly style and format your strings. + <p>No attributes.</p> + </dd> +</dl> +</dd> <!-- end elements --> + +<dt>example:</dt> +<dd>XML file saved at <code>res/values/strings.xml</code>: +<pre> +<?xml version="1.0" encoding="utf-8"?> +<resources> + <string-array name="planets_array"> + <item>Mercury</item> + <item>Venus</item> + <item>Earth</item> + <item>Mars</item> + </string-array> +</resources> +</pre> + + <p>This application code retrieves a string array:</p> +<pre> +Resources res = {@link android.content.Context#getResources()}; +String[] planets = res.{@link android.content.res.Resources#getStringArray(int) +getStringArray}(R.array.planets_array); +</pre> +</dd> <!-- end example --> + +</dl> + + + + + + + +<h2 id="Plurals">Plurals</h2> + +<p>A pair of strings that each provide a different plural form of the same word or phrase, +which you can collectively reference from the application. When you request the plurals +resource using a method such as {@link android.content.res.Resources#getQuantityString(int,int) +getQuantityString()}, you must pass a "count", which will determine the plural form you +require and return that string to you.</p> + +<p class="note"><strong>Note:</strong> A plurals collection is a simple resource that is +referenced using the value provided in the {@code name} attribute (not the name of the XML +file). As such, you can combine plurals resources with other simple resources in the one +XML file, under one {@code <resources>} element.</p> + +<dl class="xml"> + +<dt>file location:</dt> +<dd><code>res/values/<em>filename</em>.xml</code><br/> +The filename is arbitrary. The {@code <plurals>} element's {@code name} will be used as the +resource ID.</dd> + +<dt>resource reference:</dt> +<dd> +In Java: <code>R.plurals.<em>plural_name</em></code> +</dd> + +<dt>syntax:</dt> +<dd> +<pre class="stx"> +<?xml version="1.0" encoding="utf-8"?> +<<a href="#plurals-resources-element">resources</a>> + <<a href="#plurals-element">plurals</a> + name="<em>plural_name</em>"> + <<a href="#plurals-item-element">item</a> + quantity=["one" | "other"] + ><em>text_string</em></item> + </plurals> +</resources> +</pre> +</dd> + +<dt>elements:</dt> +<dd> +<dl class="tag-list"> + + <dt id="plurals-resources-element"><code><resources></code></dt> + <dd><strong>Required.</strong> This must be the root node. + <p>No attributes.</p> + </dd> + <dt id="plurals-element"><code><plurals></code></dt> + <dd>A collection of strings, of which, one string is provided depending on the amount of +something. Contains one or more {@code <item>} elements. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>name</code></dt> + <dd><em>String</em>. A name for the pair of strings. This name will be used as the +resource ID.</dd> + </dl> + + </dd> + <dt id="plurals-item-element"><code><item></code></dt> + <dd>A plural or singular string. The value can be a referenced to another +string resource. Must be a child of a {@code <plurals>} element. Beware that you must +escape apostrophes and quotation marks. See <a href="#FormattingAndStyling">Formatting and +Styling</a>, below, for information about to properly style and format your strings. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>quantity</code></dt> + <dd><em>Keyword</em>. A value indicating the case in which this string should be used. Valid +values: + <table> + <tr><th>Value</th><th>Description</th></tr> + <tr> + <td>{@code one}</td><td>When there is one (a singular string).</td> + </tr> + <tr> + <td>{@code other}</td><td>When the quantity is anything other than one (a plural +string, but also used when the count is zero).</td> + </tr> + </table> + </dd> + </dl> + </dd> + +</dl> +</dd> <!-- end elements --> + +<dt>example:</dt> +<dd>XML file saved at {@code res/values/strings.xml}:</p> +<pre> +<?xml version="1.0" encoding="utf-8"?> +<resources> + <plurals name="numberOfSongsAvailable"> + <item quantity="one">One song found.</item> + <item quantity="other">%d songs found.</item> + </plurals> +</resources> +</pre> + <p>Java code:</p> +<pre> +int count = getNumberOfsongsAvailable(); +Resources res = {@link android.content.Context#getResources()}; +String songsFound = res.{@link android.content.res.Resources#getQuantityString(int,int) +getQuantityString}(R.plurals.numberOfSongsAvailable, count); +</pre> +</dd> <!-- end example --> + +</dl> + + + + + + + + +<h2 id="FormattingAndStyling">Formatting and Styling</h2> + +<p>Here are a few important things you should know about how to properly +format and style your string resources.</p> + + +<h3>Escaping apostrophes and quotes</h3> + +<p>If you have an apostrophe or a quote in your string, you must either escape it or enclose the +whole string in the other type of enclosing quotes. For example, here are some stings that +do and don't work:</p> + +<pre> +<string name="good_example">"This'll work"</string> +<string name="good_example_2">This\'ll also work</string> +<string name="bad_example">This doesn't work</string> +<string name="bad_example_2">XML encodings don&apos;t work</string> +</pre> + + +<h3>Formatting strings</h3> + +<p>If you need to format your strings using <a +href="{@docRoot}reference/java/lang/String.html#format(java.lang.String, +java.lang.Object...)">{@code String.format(String, Object...)}</a>, +then you can do so by putting +your format arguments in the string resource. For example, with the following resource:</p> + +<pre> +<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string> +</pre> + +<p>In this example, the format string has two arguments: {@code %1$s} is a string and {@code %2$d} +is a decimal number. You can format the string with arguements from your application like this:</p> + +<pre> +Resources res = {@link android.content.Context#getResources()}; +String text = String.<a href="{@docRoot}reference/java/lang/String.html#format(java.lang.String, +java.lang.Object...)">format</a>(res.getString(R.string.welcome_messages), username, mailCount); +</pre> + + + +<h3>Styling with HTML markup</h3> + +<p>You can add styling to your strings with HTML markup. For example:</p> +<pre> +<?xml version="1.0" encoding="utf-8"?> +<resources> + <string name="welcome">Welcome to <b>Android</b>!</string> +</resources> +</pre> +<p>Supported HTML elements include:</p> +<ul> + <li>{@code <b>} for <b>bold</b> text.</li> + <li>{@code <i>} for <i>italic</i> text.</li> + <li>{@code <u>} for <u>underline</u> text.</li> +</ul> + +<p>Sometimes you may want to create a styled text resource that is also used as a format +string. Normally, this won't work because the <a +href="{@docRoot}reference/java/lang/String.html#format(java.lang.String, +java.lang.Object...)">{@code String.format(String, Object...)}</a> +method will strip all the style +information from the string. The work-around to this is to write the HTML tags with escaped +entities, which are then recovered with {@link android.text.Html#fromHtml(String)}, +after the formatting takes place. For example:</p> + +<ol> + <li>Store your styled text resource as an HTML-escaped string: +<pre> +<resources> + <string name="welcome_messages">Hello, %1$s! You have &lt;b>%2$d new messages&lt;/b>.</string> +</resources> +</pre> +<p>In this formatted string, a {@code <b>} element is added. Notice that the opening bracket is +HTML-escaped, using the {@code &lt;} notation.</p> + </li> + <li>Then format the string as usual, but also call {@link android.text.Html#fromHtml} to +convert the HTML text into styled text: +<pre> +Resources res = {@link android.content.Context#getResources()}; +String text = String.<a +href="{@docRoot}reference/java/lang/String.html#format(java.lang.String, +java.lang.Object...)">format</a>(res.getString(R.string.welcome_messages), username, mailCount); +CharSequence styledText = Html.fromHtml(text); +</pre> + </li> +</ol> + +<p>Because the {@link android.text.Html#fromHtml} method will format all HTML entities, be sure to +escape any possible HTML characters in the strings you use with the formatted text, using +{@link android.text.TextUtils#htmlEncode}. For instance, if you'll be passing a string argument to +<a href="{@docRoot}reference/java/lang/String.html#format(java.lang.String, +java.lang.Object...)">{@code String.format()}</a> that may contain characters such as +"<" or "&", then they must be escaped before formatting, so that when the formatted string +is passed through {@link android.text.Html#fromHtml}, the characters come out the way they were +originally written. For example:</p> +<pre> +String escapedUsername = TextUtil.{@link android.text.TextUtils#htmlEncode htmlEncode}(username); + +Resources res = {@link android.content.Context#getResources()}; +String text = String.<a href="{@docRoot}reference/java/lang/String.html#format(java.lang.String, +java.lang.Object...)">format</a>(res.getString(R.string.welcome_messages), escapedUsername, mailCount); +CharSequence styledText = Html.fromHtml(text); +</pre> + + + diff --git a/docs/html/guide/topics/resources/style-resource.jd b/docs/html/guide/topics/resources/style-resource.jd new file mode 100644 index 0000000..def727c --- /dev/null +++ b/docs/html/guide/topics/resources/style-resource.jd @@ -0,0 +1,128 @@ +page.title=Style Resource +parent.title=Resource Types +parent.link=available-resources.html +@jd:body + +<div id="qv-wrapper"> + <div id="qv"> + <h2>See also</h2> + <ol> + <li><a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a></li> + </ol> + </div> +</div> + + +<p>A style resource defines the format and look for a UI. +A style can be applied to an individual {@link android.view.View} (from within a layout file) or to +an entire {@link android.app.Activity} or application (from within the manifest file).</p> + +<p>For more information about creating and applying styles, please read +<a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>.</p> + +<p class="note"><strong>Note:</strong> A style is a simple resource that is referenced +using the value provided in the {@code name} attribute (not the name of the XML file). As +such, you can combine style resources with other simple resources in the one XML file, +under one {@code <resources>} element.</p> + +<dl class="xml"> + +<dt>file location:</dt> +<dd><code>res/values/<em>filename</em>.xml</code><br/> +The filename is arbitrary. The element's {@code name} will be used as the resource ID.</dd> + +<dt>resource reference:</dt> +<dd> +In XML: <code>@[package:]style/<em>style_name</em></code> +</dd> + +<dt>syntax:</dt> +<dd> +<pre class="stx"> +<?xml version="1.0" encoding="utf-8"?> +<<a href="#resources-element">resources</a>> + <<a href="#style-element">style</a> + name="<em>style_name</em>" + parent="@[package:]style/<em>style_to_inherit</em>"> + <<a href="#item-element">item</a> + name="<em>[package:]style_property_name</em>" + ><em>style_value</em></item> + </style> +</resources> +</pre> +</dd> + +<dt>elements:</dt> +<dd> +<dl class="tag-list"> + + <dt id="resources-element"><code><resources></code></dt> + <dd><strong>Required.</strong> This must be the root node. + <p>No attributes.</p> + </dd> + <dt id="style-element"><code><style></code></dt> + <dd>Defines a single style. Contains {@code <item>} elements. + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>name</code></dt> + <dd><em>String</em>. <strong>Required</strong>. A name for the style, which is used as the +resource ID to apply the style to a View, Activity, or application. + </dd> + <dt><code>parent</code></dt> + <dd><em>Style resource</em>. Reference to a style from which this +style should inherit style properties. + </dd> + </dl> + + </dd> + <dt id="item-element"><code><item></code></dt> + <dd>Defines a single property for the style. Must be a child of a + <code><style></code> element.</p> + <p class="caps">attributes:</p> + <dl class="atn-list"> + <dt><code>name</code></dt> + <dd><em>Attribute resource</em>. <strong>Required</strong>. The name of the style property +to be defined, with a package prefix if necessary (for example {@code android:textColor}). + </dd> + </dl> + </dd> + +</dl> +</dd> <!-- end elements and attributes --> + +<dt>example:</dt> +<dd> + <dl> + + <dt>XML file for the style (saved in <code>res/values/</code>):</dt> + <dd> +<pre> +<?xml version="1.0" encoding="utf-8"?> +<resources> + <style name="CustomText" parent="@style/Text"> + <item name="android:textSize">20sp</item> + <item name="android:textColor">#008</item> + </style> +</resources> +</pre> + </dd> + + <dt>XML file that applies the style to a {@link android.widget.TextView} + (saved in <code>res/layout/</code>):</dt> + <dd> +<pre> +<?xml version="1.0" encoding="utf-8"?> +<EditText + style="@style/CustomText" + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:text="Hello, World!" /> +</pre> + </dd> + + </dl> +</dd> <!-- end example --> + +</dl> + + diff --git a/docs/html/images/resources/resource_devices_diagram1.png b/docs/html/images/resources/resource_devices_diagram1.png Binary files differnew file mode 100644 index 0000000..adbdab6 --- /dev/null +++ b/docs/html/images/resources/resource_devices_diagram1.png diff --git a/docs/html/images/resources/resource_devices_diagram2.png b/docs/html/images/resources/resource_devices_diagram2.png Binary files differnew file mode 100644 index 0000000..403dd4f --- /dev/null +++ b/docs/html/images/resources/resource_devices_diagram2.png diff --git a/graphics/java/android/graphics/PorterDuff.java b/graphics/java/android/graphics/PorterDuff.java index 3904234..0e405c2 100644 --- a/graphics/java/android/graphics/PorterDuff.java +++ b/graphics/java/android/graphics/PorterDuff.java @@ -53,7 +53,10 @@ public class PorterDuff { /** [Sa * Da, Sc * Dc] */ MULTIPLY (14), /** [Sa + Da - Sa * Da, Sc + Dc - Sc * Dc] */ - SCREEN (15); + SCREEN (15), + /** Saturate(S + D) */ + ADD (16), + OVERLAY (17); Mode(int nativeInt) { this.nativeInt = nativeInt; diff --git a/graphics/java/android/renderscript/Allocation.java b/graphics/java/android/renderscript/Allocation.java index 17c0778..d32a0b5 100644 --- a/graphics/java/android/renderscript/Allocation.java +++ b/graphics/java/android/renderscript/Allocation.java @@ -76,10 +76,30 @@ public class Allocation extends BaseObj { subData1D(0, mType.getElementCount(), d); } + public void subData(int off, FieldPacker fp) { + int eSize = mType.mElement.getSizeBytes(); + final byte[] data = fp.getData(); + + int count = data.length / eSize; + if ((eSize * count) != data.length) { + throw new IllegalArgumentException("Field packer length " + data.length + + " not divisible by element size " + eSize + "."); + } + data1DChecks(off, count, data.length, data.length); + mRS.nAllocationSubData1D(mID, off, count, data, data.length); + } + private void data1DChecks(int off, int count, int len, int dataSize) { mRS.validate(); - if((off < 0) || (count < 1) || ((off + count) > mType.getElementCount())) { - throw new IllegalArgumentException("Offset or Count out of bounds."); + if(off < 0) { + throw new IllegalArgumentException("Offset must be >= 0."); + } + if(count < 1) { + throw new IllegalArgumentException("Count must be >= 1."); + } + if((off + count) > mType.getElementCount()) { + throw new IllegalArgumentException("Overflow, Available count " + mType.getElementCount() + + ", got " + count + " at offset " + off + "."); } if((len) < dataSize) { throw new IllegalArgumentException("Array too small for allocation type."); diff --git a/graphics/java/android/renderscript/Byte2.java b/graphics/java/android/renderscript/Byte2.java new file mode 100644 index 0000000..95cf88c --- /dev/null +++ b/graphics/java/android/renderscript/Byte2.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2009 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.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Byte2 { + public Byte2() { + } + + public byte x; + public byte y; +} + + + + diff --git a/graphics/java/android/renderscript/Byte3.java b/graphics/java/android/renderscript/Byte3.java new file mode 100644 index 0000000..a6c0ca9 --- /dev/null +++ b/graphics/java/android/renderscript/Byte3.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2009 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.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Byte3 { + public Byte3() { + } + + public byte x; + public byte y; + public byte z; +} + + + + diff --git a/graphics/java/android/renderscript/Byte4.java b/graphics/java/android/renderscript/Byte4.java new file mode 100644 index 0000000..a5bfc61 --- /dev/null +++ b/graphics/java/android/renderscript/Byte4.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2009 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.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Byte4 { + public Byte4() { + } + + public byte x; + public byte y; + public byte z; + public byte w; +} + + + diff --git a/graphics/java/android/renderscript/Element.java b/graphics/java/android/renderscript/Element.java index 10ef05a..7b155fe 100644 --- a/graphics/java/android/renderscript/Element.java +++ b/graphics/java/android/renderscript/Element.java @@ -126,6 +126,77 @@ public class Element extends BaseObj { return rs.mElement_USER_F32; } + public static Element USER_ELEMENT(RenderScript rs) { + if(rs.mElement_USER_ELEMENT == null) { + rs.mElement_USER_ELEMENT = createUser(rs, DataType.RS_ELEMENT); + } + return rs.mElement_USER_ELEMENT; + } + + public static Element USER_TYPE(RenderScript rs) { + if(rs.mElement_USER_TYPE == null) { + rs.mElement_USER_TYPE = createUser(rs, DataType.RS_TYPE); + } + return rs.mElement_USER_TYPE; + } + + public static Element USER_ALLOCATION(RenderScript rs) { + if(rs.mElement_USER_ALLOCATION == null) { + rs.mElement_USER_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION); + } + return rs.mElement_USER_ALLOCATION; + } + + public static Element USER_SAMPLER(RenderScript rs) { + if(rs.mElement_USER_SAMPLER == null) { + rs.mElement_USER_SAMPLER = createUser(rs, DataType.RS_SAMPLER); + } + return rs.mElement_USER_SAMPLER; + } + + public static Element USER_SCRIPT(RenderScript rs) { + if(rs.mElement_USER_SCRIPT == null) { + rs.mElement_USER_SCRIPT = createUser(rs, DataType.RS_SCRIPT); + } + return rs.mElement_USER_SCRIPT; + } + + public static Element USER_MESH(RenderScript rs) { + if(rs.mElement_USER_MESH == null) { + rs.mElement_USER_MESH = createUser(rs, DataType.RS_MESH); + } + return rs.mElement_USER_MESH; + } + + public static Element USER_PROGRAM_FRAGMENT(RenderScript rs) { + if(rs.mElement_USER_PROGRAM_FRAGMENT == null) { + rs.mElement_USER_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT); + } + return rs.mElement_USER_PROGRAM_FRAGMENT; + } + + public static Element USER_PROGRAM_VERTEX(RenderScript rs) { + if(rs.mElement_USER_PROGRAM_VERTEX == null) { + rs.mElement_USER_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX); + } + return rs.mElement_USER_PROGRAM_VERTEX; + } + + public static Element USER_PROGRAM_RASTER(RenderScript rs) { + if(rs.mElement_USER_PROGRAM_RASTER == null) { + rs.mElement_USER_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER); + } + return rs.mElement_USER_PROGRAM_RASTER; + } + + public static Element USER_PROGRAM_STORE(RenderScript rs) { + if(rs.mElement_USER_PROGRAM_STORE == null) { + rs.mElement_USER_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE); + } + return rs.mElement_USER_PROGRAM_STORE; + } + + public static Element A_8(RenderScript rs) { if(rs.mElement_A_8 == null) { rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A); diff --git a/graphics/java/android/renderscript/FieldPacker.java b/graphics/java/android/renderscript/FieldPacker.java index b26e47d..6d55c7e 100644 --- a/graphics/java/android/renderscript/FieldPacker.java +++ b/graphics/java/android/renderscript/FieldPacker.java @@ -33,21 +33,24 @@ public class FieldPacker { } } - void reset() { + public void reset() { mPos = 0; } + public void reset(int i) { + mPos = i; + } - void addI8(byte v) { + public void addI8(byte v) { mData[mPos++] = v; } - void addI16(short v) { + public void addI16(short v) { align(2); mData[mPos++] = (byte)(v & 0xff); mData[mPos++] = (byte)(v >> 8); } - void addI32(int v) { + public void addI32(int v) { align(4); mData[mPos++] = (byte)(v & 0xff); mData[mPos++] = (byte)((v >> 8) & 0xff); @@ -55,7 +58,7 @@ public class FieldPacker { mData[mPos++] = (byte)((v >> 24) & 0xff); } - void addI64(long v) { + public void addI64(long v) { align(8); mData[mPos++] = (byte)(v & 0xff); mData[mPos++] = (byte)((v >> 8) & 0xff); @@ -67,14 +70,14 @@ public class FieldPacker { mData[mPos++] = (byte)((v >> 56) & 0xff); } - void addU8(short v) { + public void addU8(short v) { if ((v < 0) || (v > 0xff)) { throw new IllegalArgumentException("Saving value out of range for type"); } mData[mPos++] = (byte)v; } - void addU16(int v) { + public void addU16(int v) { if ((v < 0) || (v > 0xffff)) { throw new IllegalArgumentException("Saving value out of range for type"); } @@ -83,7 +86,7 @@ public class FieldPacker { mData[mPos++] = (byte)(v >> 8); } - void addU32(long v) { + public void addU32(long v) { if ((v < 0) || (v > 0xffffffff)) { throw new IllegalArgumentException("Saving value out of range for type"); } @@ -94,7 +97,7 @@ public class FieldPacker { mData[mPos++] = (byte)((v >> 24) & 0xff); } - void addU64(long v) { + public void addU64(long v) { if (v < 0) { throw new IllegalArgumentException("Saving value out of range for type"); } @@ -109,15 +112,135 @@ public class FieldPacker { mData[mPos++] = (byte)((v >> 56) & 0xff); } - void addF32(float v) { + public void addF32(float v) { addI32(Float.floatToRawIntBits(v)); } - void addF64(float v) { + public void addF64(float v) { addI64(Double.doubleToRawLongBits(v)); } - final byte[] getData() { + public void addObj(BaseObj obj) { + if (obj != null) { + addI32(obj.getID()); + } else { + addI32(0); + } + } + + public void addF32(Float2 v) { + addF32(v.x); + addF32(v.y); + } + public void addF32(Float3 v) { + addF32(v.x); + addF32(v.y); + addF32(v.z); + } + public void addF32(Float4 v) { + addF32(v.x); + addF32(v.y); + addF32(v.z); + addF32(v.w); + } + + public void addI8(Byte2 v) { + addI8(v.x); + addI8(v.y); + } + public void addI8(Byte3 v) { + addI8(v.x); + addI8(v.y); + addI8(v.z); + } + public void addI8(Byte4 v) { + addI8(v.x); + addI8(v.y); + addI8(v.z); + addI8(v.w); + } + + public void addU8(Short2 v) { + addU8(v.x); + addU8(v.y); + } + public void addU8(Short3 v) { + addU8(v.x); + addU8(v.y); + addU8(v.z); + } + public void addU8(Short4 v) { + addU8(v.x); + addU8(v.y); + addU8(v.z); + addU8(v.w); + } + + public void addI16(Short2 v) { + addI16(v.x); + addI16(v.y); + } + public void addI16(Short3 v) { + addI16(v.x); + addI16(v.y); + addI16(v.z); + } + public void addI16(Short4 v) { + addI16(v.x); + addI16(v.y); + addI16(v.z); + addI16(v.w); + } + + public void addU16(Int2 v) { + addU16(v.x); + addU16(v.y); + } + public void addU16(Int3 v) { + addU16(v.x); + addU16(v.y); + addU16(v.z); + } + public void addU16(Int4 v) { + addU16(v.x); + addU16(v.y); + addU16(v.z); + addU16(v.w); + } + + public void addI32(Int2 v) { + addI32(v.x); + addI32(v.y); + } + public void addI32(Int3 v) { + addI32(v.x); + addI32(v.y); + addI32(v.z); + } + public void addI32(Int4 v) { + addI32(v.x); + addI32(v.y); + addI32(v.z); + addI32(v.w); + } + + public void addU32(Int2 v) { + addU32(v.x); + addU32(v.y); + } + public void addU32(Int3 v) { + addU32(v.x); + addU32(v.y); + addU32(v.z); + } + public void addU32(Int4 v) { + addU32(v.x); + addU32(v.y); + addU32(v.z); + addU32(v.w); + } + + public final byte[] getData() { return mData; } diff --git a/graphics/java/android/renderscript/Vector2f.java b/graphics/java/android/renderscript/Float2.java index 567d57f..8fea91f 100644 --- a/graphics/java/android/renderscript/Vector2f.java +++ b/graphics/java/android/renderscript/Float2.java @@ -24,8 +24,8 @@ import android.util.Log; * @hide * **/ -public class Vector2f { - public Vector2f() { +public class Float2 { + public Float2() { } public float x; diff --git a/graphics/java/android/renderscript/Vector3f.java b/graphics/java/android/renderscript/Float3.java index f2842f3..9d9e406 100644 --- a/graphics/java/android/renderscript/Vector3f.java +++ b/graphics/java/android/renderscript/Float3.java @@ -24,8 +24,8 @@ import android.util.Log; * @hide * **/ -public class Vector3f { - public Vector3f() { +public class Float3 { + public Float3() { } public float x; diff --git a/graphics/java/android/renderscript/Vector4f.java b/graphics/java/android/renderscript/Float4.java index fabd959..a703e80 100644 --- a/graphics/java/android/renderscript/Vector4f.java +++ b/graphics/java/android/renderscript/Float4.java @@ -24,8 +24,8 @@ import android.util.Log; * @hide * **/ -public class Vector4f { - public Vector4f() { +public class Float4 { + public Float4() { } public float x; diff --git a/graphics/java/android/renderscript/Int2.java b/graphics/java/android/renderscript/Int2.java new file mode 100644 index 0000000..56e2fe9 --- /dev/null +++ b/graphics/java/android/renderscript/Int2.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2009 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.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Int2 { + public Int2() { + } + + public int x; + public int y; +} + + + + diff --git a/graphics/java/android/renderscript/Int3.java b/graphics/java/android/renderscript/Int3.java new file mode 100644 index 0000000..1b27509 --- /dev/null +++ b/graphics/java/android/renderscript/Int3.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2009 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.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Int3 { + public Int3() { + } + + public int x; + public int y; + public int z; +} + + + + diff --git a/graphics/java/android/renderscript/Int4.java b/graphics/java/android/renderscript/Int4.java new file mode 100644 index 0000000..3d6f3f5 --- /dev/null +++ b/graphics/java/android/renderscript/Int4.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2009 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.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Int4 { + public Int4() { + } + + public int x; + public int y; + public int z; + public int w; +} + + + diff --git a/graphics/java/android/renderscript/Long2.java b/graphics/java/android/renderscript/Long2.java new file mode 100644 index 0000000..11ead2f --- /dev/null +++ b/graphics/java/android/renderscript/Long2.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2009 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.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Long2 { + public Long2() { + } + + public long x; + public long y; +} + + + + diff --git a/graphics/java/android/renderscript/Long3.java b/graphics/java/android/renderscript/Long3.java new file mode 100644 index 0000000..1604532 --- /dev/null +++ b/graphics/java/android/renderscript/Long3.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2009 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.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Long3 { + public Long3() { + } + + public long x; + public long y; + public long z; +} + + + + diff --git a/graphics/java/android/renderscript/Long4.java b/graphics/java/android/renderscript/Long4.java new file mode 100644 index 0000000..2fd2747 --- /dev/null +++ b/graphics/java/android/renderscript/Long4.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2009 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.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Long4 { + public Long4() { + } + + public long x; + public long y; + public long z; + public long w; +} + + + diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java index a935243..db2a3fd 100644 --- a/graphics/java/android/renderscript/RenderScript.java +++ b/graphics/java/android/renderscript/RenderScript.java @@ -203,6 +203,17 @@ public class RenderScript { Element mElement_USER_I32; Element mElement_USER_F32; + Element mElement_USER_ELEMENT; + Element mElement_USER_TYPE; + Element mElement_USER_ALLOCATION; + Element mElement_USER_SAMPLER; + Element mElement_USER_SCRIPT; + Element mElement_USER_MESH; + Element mElement_USER_PROGRAM_FRAGMENT; + Element mElement_USER_PROGRAM_VERTEX; + Element mElement_USER_PROGRAM_RASTER; + Element mElement_USER_PROGRAM_STORE; + Element mElement_A_8; Element mElement_RGB_565; Element mElement_RGB_888; diff --git a/graphics/java/android/renderscript/Script.java b/graphics/java/android/renderscript/Script.java index 57ccfa3..0d21368 100644 --- a/graphics/java/android/renderscript/Script.java +++ b/graphics/java/android/renderscript/Script.java @@ -42,6 +42,10 @@ public class Script extends BaseObj { } } + protected void invoke(int slot) { + mRS.nScriptInvoke(mID, slot); + } + Script(int id, RenderScript rs) { super(rs); mID = id; @@ -145,5 +149,48 @@ public class Script extends BaseObj { } + + public static class FieldBase { + protected Element mElement; + protected Type mType; + protected Allocation mAllocation; + + protected void init(RenderScript rs, int dimx) { + mAllocation = Allocation.createSized(rs, mElement, dimx); + mType = mAllocation.getType(); + } + + protected FieldBase() { + } + + public Element getElement() { + return mElement; + } + + public Type getType() { + return mType; + } + + public Allocation getAllocation() { + return mAllocation; + } + + //@Override + public void updateAllocation() { + } + + + // + /* + public class ScriptField_UserField + extends android.renderscript.Script.FieldBase { + + protected + + } + + */ + + } } diff --git a/graphics/java/android/renderscript/ScriptC.java b/graphics/java/android/renderscript/ScriptC.java index bb99e23..f5d5b2f 100644 --- a/graphics/java/android/renderscript/ScriptC.java +++ b/graphics/java/android/renderscript/ScriptC.java @@ -37,6 +37,47 @@ public class ScriptC extends Script { super(id, rs); } + protected ScriptC(RenderScript rs, Resources resources, int resourceID, boolean isRoot) { + super(0, rs); + mID = internalCreate(rs, resources, resourceID, isRoot); + } + + + private static synchronized int internalCreate(RenderScript rs, Resources resources, int resourceID, boolean isRoot) { + byte[] pgm; + int pgmLength; + InputStream is = resources.openRawResource(resourceID); + try { + try { + pgm = new byte[1024]; + pgmLength = 0; + while(true) { + int bytesLeft = pgm.length - pgmLength; + if (bytesLeft == 0) { + byte[] buf2 = new byte[pgm.length * 2]; + System.arraycopy(pgm, 0, buf2, 0, pgm.length); + pgm = buf2; + bytesLeft = pgm.length - pgmLength; + } + int bytesRead = is.read(pgm, pgmLength, bytesLeft); + if (bytesRead <= 0) { + break; + } + pgmLength += bytesRead; + } + } finally { + is.close(); + } + } catch(IOException e) { + throw new Resources.NotFoundException(); + } + + rs.nScriptCBegin(); + rs.nScriptCSetScript(pgm, 0, pgmLength); + rs.nScriptSetRoot(isRoot); + return rs.nScriptCCreate(); + } + public static class Builder extends Script.Builder { byte[] mProgram; int mProgramLength; diff --git a/graphics/java/android/renderscript/Short2.java b/graphics/java/android/renderscript/Short2.java new file mode 100644 index 0000000..426801f --- /dev/null +++ b/graphics/java/android/renderscript/Short2.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2009 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.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Short2 { + public Short2() { + } + + public short x; + public short y; +} + + + + diff --git a/graphics/java/android/renderscript/Short3.java b/graphics/java/android/renderscript/Short3.java new file mode 100644 index 0000000..7b9c305 --- /dev/null +++ b/graphics/java/android/renderscript/Short3.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2009 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.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Short3 { + public Short3() { + } + + public short x; + public short y; + public short z; +} + + + + diff --git a/graphics/java/android/renderscript/Short4.java b/graphics/java/android/renderscript/Short4.java new file mode 100644 index 0000000..9a474e2 --- /dev/null +++ b/graphics/java/android/renderscript/Short4.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2009 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.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Short4 { + public Short4() { + } + + public short x; + public short y; + public short z; + public short w; +} + + + diff --git a/libs/rs/Android.mk b/libs/rs/Android.mk index 98464a0..0b06022 100644 --- a/libs/rs/Android.mk +++ b/libs/rs/Android.mk @@ -76,16 +76,18 @@ ifneq ($(TARGET_SIMULATOR),true) LOCAL_SRC_FILES:= \ rsAdapter.cpp \ rsAllocation.cpp \ + rsAnimation.cpp \ rsComponent.cpp \ rsContext.cpp \ rsDevice.cpp \ rsElement.cpp \ - rsFileA3D.cpp \ + rsFileA3D.cpp \ rsLight.cpp \ rsLocklessFifo.cpp \ rsObjectBase.cpp \ rsMatrix.cpp \ - rsMesh.cpp \ + rsMesh.cpp \ + rsMutex.cpp \ rsNoise.cpp \ rsProgram.cpp \ rsProgramFragment.cpp \ @@ -96,7 +98,8 @@ LOCAL_SRC_FILES:= \ rsScript.cpp \ rsScriptC.cpp \ rsScriptC_Lib.cpp \ - rsShaderCache.cpp \ + rsShaderCache.cpp \ + rsSignal.cpp \ rsSimpleMesh.cpp \ rsThreadIO.cpp \ rsType.cpp \ diff --git a/libs/rs/RenderScript.h b/libs/rs/RenderScript.h index d280f50..7415ba9 100644 --- a/libs/rs/RenderScript.h +++ b/libs/rs/RenderScript.h @@ -30,6 +30,7 @@ extern "C" { typedef void * RsAdapter1D; typedef void * RsAdapter2D; typedef void * RsAllocation; +typedef void * RsAnimation; typedef void * RsContext; typedef void * RsDevice; typedef void * RsElement; @@ -205,7 +206,27 @@ enum RsPrimitive { enum RsError { RS_ERROR_NONE, RS_ERROR_BAD_SHADER, - RS_ERROR_BAD_SCRIPT + RS_ERROR_BAD_SCRIPT, + RS_ERROR_BAD_VALUE, + RS_ERROR_OUT_OF_MEMORY +}; + +enum RsAnimationInterpolation { + RS_ANIMATION_INTERPOLATION_STEP, + RS_ANIMATION_INTERPOLATION_LINEAR, + RS_ANIMATION_INTERPOLATION_BEZIER, + RS_ANIMATION_INTERPOLATION_CARDINAL, + RS_ANIMATION_INTERPOLATION_HERMITE, + RS_ANIMATION_INTERPOLATION_BSPLINE +}; + +enum RsAnimationEdge { + RS_ANIMATION_EDGE_UNDEFINED, + RS_ANIMATION_EDGE_CONSTANT, + RS_ANIMATION_EDGE_GRADIENT, + RS_ANIMATION_EDGE_CYCLE, + RS_ANIMATION_EDGE_OSCILLATE, + RS_ANIMATION_EDGE_CYLE_RELATIVE }; #ifndef NO_RS_FUNCS diff --git a/libs/rs/java/Fountain/res/raw/fountain2.rs b/libs/rs/java/Fountain/res/raw/fountain2.rs index 3301140..5d36e35 100644 --- a/libs/rs/java/Fountain/res/raw/fountain2.rs +++ b/libs/rs/java/Fountain/res/raw/fountain2.rs @@ -1,9 +1,9 @@ // Fountain test script #pragma version(1) -#include "rs_types.rsh" -#include "rs_math.rsh" -#include "rs_graphics.rsh" +#include "../../../../scriptc/rs_types.rsh" +#include "../../../../scriptc/rs_math.rsh" +#include "../../../../scriptc/rs_graphics.rsh" static int newPart = 0; @@ -12,15 +12,15 @@ typedef struct Control_s { int rate; int count; float r, g, b; - rs_allocation partBuffer; rs_mesh partMesh; + rs_allocation partBuffer; } Control_t; Control_t *Control; typedef struct Point_s{ float2 delta; - float2 position; - unsigned int color; + rs_position2 pos; + rs_color4u color; } Point_t; Point_t *point; @@ -33,8 +33,6 @@ int main(int launchID) { if (rate) { float rMax = ((float)rate) * 0.005f; - int x = Control->x; - int y = Control->y; int color = ((int)(Control->r * 255.f)) | ((int)(Control->g * 255.f)) << 8 | ((int)(Control->b * 255.f)) << 16 | @@ -42,9 +40,11 @@ int main(int launchID) { Point_t * np = &p[newPart]; while (rate--) { - np->delta = vec2Rand(rMax); - np->position.x = x; - np->position.y = y; + np->delta.x = rand(rMax); + np->delta.y = rand(rMax); + //np->delta = vec2Rand(rMax); + np->pos.x = Control->x; + np->pos.y = Control->y; np->color = color; newPart++; np++; @@ -57,13 +57,13 @@ int main(int launchID) { for (ct=0; ct < count; ct++) { float dy = p->delta.y + 0.15f; - float posy = p->position.y + dy; + float posy = p->pos.y + dy; if ((posy > height) && (dy > 0)) { dy *= -0.3f; } p->delta.y = dy; - p->position.x += p->delta.x; - p->position.y = posy; + p->pos.x += p->delta.x; + p->pos.y = posy; p++; } diff --git a/libs/rs/java/ImageProcessing/res/raw/threshold2.rs b/libs/rs/java/ImageProcessing/res/raw/threshold2.rs new file mode 100644 index 0000000..9f687b5 --- /dev/null +++ b/libs/rs/java/ImageProcessing/res/raw/threshold2.rs @@ -0,0 +1,49 @@ +#pragma version(1) + +#include "../../../../scriptc/rs_types.rsh" +#include "../../../../scriptc/rs_math.rsh" +#include "../../../../scriptc/rs_graphics.rsh" + +typedef struct Params_s{ + int inHeight; + int inWidth; + int outHeight; + int outWidth; + float threshold; +} Params_t; + +Params_t * Params; +rs_color4u * InPixel; +rs_color4u * OutPixel; + + +int main() { + int t = uptimeMillis(); + + rs_color4u *in = InPixel; + rs_color4u *out = OutPixel; + + int count = Params->inWidth * Params->inHeight; + int i; + float threshold = Params->threshold * 255.f; + + for (i = 0; i < count; i++) { + float luminance = 0.2125f * in->x + + 0.7154f * in->y + + 0.0721f * in->z; + if (luminance > threshold) { + *out = *in; + } else { + *((int *)out) = *((int *)in) & 0xff000000; + } + + in++; + out++; + } + + t= uptimeMillis() - t; + debugI32("Filter time", t); + + sendToClient(&count, 1, 4, 0); + return 0; +} diff --git a/libs/rs/rs.spec b/libs/rs/rs.spec index cb9937c..08aa369 100644 --- a/libs/rs/rs.spec +++ b/libs/rs/rs.spec @@ -480,3 +480,13 @@ SimpleMeshBindVertex { param uint32_t slot } +AnimationCreate { + param const float *inValues + param const float *outValues + param uint32_t valueCount + param RsAnimationInterpolation interp + param RsAnimationEdge pre + param RsAnimationEdge post + ret RsAnimation + } + diff --git a/libs/rs/rsAllocation.cpp b/libs/rs/rsAllocation.cpp index 4e8278d..e5ff1d7 100644 --- a/libs/rs/rsAllocation.cpp +++ b/libs/rs/rsAllocation.cpp @@ -170,6 +170,7 @@ void Allocation::uploadToTexture(const Context *rsc) glGenerateMipmap(GL_TEXTURE_2D); } + rsc->checkError("Allocation::uploadToTexture"); } void Allocation::deferedUploadToBufferObject(const Context *rsc) @@ -201,6 +202,7 @@ void Allocation::uploadToBufferObject(const Context *rsc) glBindBuffer(GL_ARRAY_BUFFER, mBufferID); glBufferData(GL_ARRAY_BUFFER, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); + rsc->checkError("Allocation::uploadToBufferObject"); } void Allocation::uploadCheck(const Context *rsc) diff --git a/libs/rs/rsAnimation.cpp b/libs/rs/rsAnimation.cpp new file mode 100644 index 0000000..48c9334 --- /dev/null +++ b/libs/rs/rsAnimation.cpp @@ -0,0 +1,134 @@ +/* + * Copyright (C) 2009 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. + */ + +#include "rsContext.h" +#include "rsAnimation.h" + + +using namespace android; +using namespace android::renderscript; + +/* +Animation::Animation(Context *rsc) : ObjectBase(rsc) +{ + mAllocFile = __FILE__; + mAllocLine = __LINE__; + + mValuesInput = NULL; + mValuesOutput = NULL; + mValueCount = 0; + mInterpolation = RS_ANIMATION_INTERPOLATION_STEP; + mEdgePre = RS_ANIMATION_EDGE_UNDEFINED; + mEdgePost = RS_ANIMATION_EDGE_UNDEFINED; + mInputMin = 0; + mInputMax = 0; +} + +Animation * Animation::create(Context *rsc, + const float *inValues, const float *outValues, + uint32_t valueCount, RsAnimationInterpolation interp, + RsAnimationEdge pre, RsAnimationEdge post) +{ + if (valueCount < 2) { + rsc->setError(RS_ERROR_BAD_VALUE, "Animations require more than 2 values."); + return NULL; + } + Animation *a = new Animation(rsc); + if (!a) { + rsc->setError(RS_ERROR_OUT_OF_MEMORY); + return NULL; + } + + float *vin = (float *)malloc(valueCount * sizeof(float)); + float *vout = (float *)malloc(valueCount * sizeof(float)); + a->mValuesInput = vin; + a->mValuesOutput = vout; + if (a->mValuesInput == NULL || a->mValuesOutput == NULL) { + delete a; + rsc->setError(RS_ERROR_OUT_OF_MEMORY); + return NULL; + } + + a->mEdgePre = pre; + a->mEdgePost = post; + a->mInterpolation = interp; + a->mValueCount = valueCount; + + memcpy(vin, inValues, valueCount * sizeof(float)); + memcpy(vout, outValues, valueCount * sizeof(float)); + a->mInputMin = inValues[0]; + a->mInputMax = inValues[0]; + + bool needSort = false; + for (uint32_t ct=1; ct < valueCount; ct++) { + if (a->mInputMin > vin[ct]) { + needSort = true; + a->mInputMin = vin[ct]; + } + if (a->mInputMax < vin[ct]) { + a->mInputMax = vin[ct]; + } else { + needSort = true; + } + } + + while (1) { + bool changed = false; + for (uint32_t ct=1; ct < valueCount; ct++) { + if (vin[ct-1] > vin[ct]) { + float t = vin[ct-1]; + vin[ct-1] = vin[ct]; + vin[ct] = t; + t = vout[ct-1]; + vout[ct-1] = vout[ct]; + vout[ct] = t; + changed = true; + } + } + if (!changed) break; + } + + return a; +} +*/ + + +///////////////////////////////////////// +// + +namespace android { +namespace renderscript { + +RsAnimation rsi_AnimationCreate(Context *rsc, + const float *inValues, + const float *outValues, + uint32_t valueCount, + RsAnimationInterpolation interp, + RsAnimationEdge pre, + RsAnimationEdge post) +{ + //LOGE("rsi_ElementCreate %i %i %i %i", dt, dk, norm, vecSize); + Animation *a = NULL;//Animation::create(rsc, inValues, outValues, valueCount, interp, pre, post); + if (a != NULL) { + a->incUserRef(); + } + return (RsAnimation)a; +} + + +} +} + diff --git a/libs/rs/rsAnimation.h b/libs/rs/rsAnimation.h new file mode 100644 index 0000000..b8db661 --- /dev/null +++ b/libs/rs/rsAnimation.h @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2009 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. + */ + +#ifndef ANDROID_RS_ANIMATION_H +#define ANDROID_RS_ANIMATION_H + +#include "rsUtils.h" +#include "rsObjectBase.h" + +// --------------------------------------------------------------------------- +namespace android { +namespace renderscript { + + +class Animation : public ObjectBase +{ +public: + ~Animation(); + + static Animation * create(Context *rsc, + const float *inValues, const float *outValues, + uint32_t valueCount, RsAnimationInterpolation, + RsAnimationEdge pre, RsAnimationEdge post); + + float eval(float) const; + + +protected: + Animation(Context *rsc); + + + + float evalInRange(float) const; + + + + const float *mValuesInput; + const float *mValuesOutput; + uint32_t mValueCount; + RsAnimationInterpolation mInterpolation; + RsAnimationEdge mEdgePre; + RsAnimationEdge mEdgePost; + + // derived + float mInputMin; + float mInputMax; +}; + + + + +} +} +#endif //ANDROID_STRUCTURED_ELEMENT_H + diff --git a/libs/rs/rsContext.cpp b/libs/rs/rsContext.cpp index d8a9a99..4107229 100644 --- a/libs/rs/rsContext.cpp +++ b/libs/rs/rsContext.cpp @@ -664,8 +664,7 @@ void Context::appendNameDefines(String8 *str) const bool Context::objDestroyOOBInit() { - int status = pthread_mutex_init(&mObjDestroy.mMutex, NULL); - if (status) { + if (!mObjDestroy.mMutex.init()) { LOGE("Context::ObjDestroyOOBInit mutex init failure"); return false; } @@ -675,9 +674,8 @@ bool Context::objDestroyOOBInit() void Context::objDestroyOOBRun() { if (mObjDestroy.mNeedToEmpty) { - int status = pthread_mutex_lock(&mObjDestroy.mMutex); - if (status) { - LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status); + if (!mObjDestroy.mMutex.lock()) { + LOGE("Context::ObjDestroyOOBRun: error locking for OOBRun."); return; } @@ -686,35 +684,25 @@ void Context::objDestroyOOBRun() } mObjDestroy.mDestroyList.clear(); mObjDestroy.mNeedToEmpty = false; - - status = pthread_mutex_unlock(&mObjDestroy.mMutex); - if (status) { - LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status); - } + mObjDestroy.mMutex.unlock(); } } void Context::objDestroyOOBDestroy() { rsAssert(!mObjDestroy.mNeedToEmpty); - pthread_mutex_destroy(&mObjDestroy.mMutex); } void Context::objDestroyAdd(ObjectBase *obj) { - int status = pthread_mutex_lock(&mObjDestroy.mMutex); - if (status) { - LOGE("Context::ObjDestroyOOBRun: error %i locking for OOBRun.", status); + if (!mObjDestroy.mMutex.lock()) { + LOGE("Context::ObjDestroyOOBRun: error locking for OOBRun."); return; } mObjDestroy.mNeedToEmpty = true; mObjDestroy.mDestroyList.add(obj); - - status = pthread_mutex_unlock(&mObjDestroy.mMutex); - if (status) { - LOGE("Context::ObjDestroyOOBRun: error %i unlocking for set condition.", status); - } + mObjDestroy.mMutex.unlock(); } uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait) diff --git a/libs/rs/rsContext.h b/libs/rs/rsContext.h index 82c3687..8e755a9 100644 --- a/libs/rs/rsContext.h +++ b/libs/rs/rsContext.h @@ -18,6 +18,7 @@ #define ANDROID_RS_CONTEXT_H #include "rsUtils.h" +#include "rsMutex.h" #include "rsThreadIO.h" #include "rsType.h" @@ -161,7 +162,7 @@ public: void dumpDebug() const; void checkError(const char *) const; const char * getError(RsError *); - void setError(RsError e, const char *msg); + void setError(RsError e, const char *msg = NULL); mutable const ObjectBase * mObjHead; @@ -227,7 +228,7 @@ protected: struct ObjDestroyOOB { - pthread_mutex_t mMutex; + Mutex mMutex; Vector<ObjectBase *> mDestroyList; bool mNeedToEmpty; }; diff --git a/libs/rs/rsLocklessFifo.cpp b/libs/rs/rsLocklessFifo.cpp index c796520..76ca32e 100644 --- a/libs/rs/rsLocklessFifo.cpp +++ b/libs/rs/rsLocklessFifo.cpp @@ -17,7 +17,7 @@ #include "rsLocklessFifo.h" using namespace android; - +using namespace android::renderscript; LocklessCommandFifo::LocklessCommandFifo() { @@ -128,15 +128,19 @@ void LocklessCommandFifo::flush() //dumpState("flush 2"); } +void LocklessCommandFifo::wait() +{ + while(isEmpty() && !mInShutdown) { + mSignalToControl.set(); + mSignalToWorker.wait(); + } +} + const void * LocklessCommandFifo::get(uint32_t *command, uint32_t *bytesData) { while(1) { //dumpState("get"); - while(isEmpty() && !mInShutdown) { - mSignalToControl.set(); - mSignalToWorker.wait(); - } - + wait(); if (mInShutdown) { *command = 0; *bytesData = 0; @@ -192,79 +196,3 @@ void LocklessCommandFifo::dumpState(const char *s) const LOGV("%s put %p, get %p, buf %p, end %p", s, mPut, mGet, mBuffer, mEnd); } -LocklessCommandFifo::Signal::Signal() -{ - mSet = true; -} - -LocklessCommandFifo::Signal::~Signal() -{ - pthread_mutex_destroy(&mMutex); - pthread_cond_destroy(&mCondition); -} - -bool LocklessCommandFifo::Signal::init() -{ - int status = pthread_mutex_init(&mMutex, NULL); - if (status) { - LOGE("LocklessFifo mutex init failure"); - return false; - } - - status = pthread_cond_init(&mCondition, NULL); - if (status) { - LOGE("LocklessFifo condition init failure"); - pthread_mutex_destroy(&mMutex); - return false; - } - - return true; -} - -void LocklessCommandFifo::Signal::set() -{ - int status; - - status = pthread_mutex_lock(&mMutex); - if (status) { - LOGE("LocklessCommandFifo: error %i locking for set condition.", status); - return; - } - - mSet = true; - - status = pthread_cond_signal(&mCondition); - if (status) { - LOGE("LocklessCommandFifo: error %i on set condition.", status); - } - - status = pthread_mutex_unlock(&mMutex); - if (status) { - LOGE("LocklessCommandFifo: error %i unlocking for set condition.", status); - } -} - -void LocklessCommandFifo::Signal::wait() -{ - int status; - - status = pthread_mutex_lock(&mMutex); - if (status) { - LOGE("LocklessCommandFifo: error %i locking for condition.", status); - return; - } - - if (!mSet) { - status = pthread_cond_wait(&mCondition, &mMutex); - if (status) { - LOGE("LocklessCommandFifo: error %i waiting on condition.", status); - } - } - mSet = false; - - status = pthread_mutex_unlock(&mMutex); - if (status) { - LOGE("LocklessCommandFifo: error %i unlocking for condition.", status); - } -} - diff --git a/libs/rs/rsLocklessFifo.h b/libs/rs/rsLocklessFifo.h index d0a4356..ae906ca 100644 --- a/libs/rs/rsLocklessFifo.h +++ b/libs/rs/rsLocklessFifo.h @@ -19,8 +19,10 @@ #include "rsUtils.h" +#include "rsSignal.h" namespace android { +namespace renderscript { // A simple FIFO to be used as a producer / consumer between two @@ -37,24 +39,7 @@ public: LocklessCommandFifo(); ~LocklessCommandFifo(); - protected: - class Signal { - public: - Signal(); - ~Signal(); - - bool init(); - - void set(); - void wait(); - - protected: - bool mSet; - pthread_mutex_t mMutex; - pthread_cond_t mCondition; - }; - uint8_t * volatile mPut; uint8_t * volatile mGet; uint8_t * mBuffer; @@ -65,14 +50,14 @@ protected: Signal mSignalToWorker; Signal mSignalToControl; - - public: void * reserve(uint32_t bytes); void commit(uint32_t command, uint32_t bytes); void commitSync(uint32_t command, uint32_t bytes); void flush(); + void wait(); + const void * get(uint32_t *command, uint32_t *bytesData); void next(); @@ -88,4 +73,5 @@ private: } +} #endif diff --git a/libs/rs/rsMutex.cpp b/libs/rs/rsMutex.cpp new file mode 100644 index 0000000..37752f2 --- /dev/null +++ b/libs/rs/rsMutex.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2009 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. + */ + +#include "rsMutex.h" + +using namespace android; +using namespace android::renderscript; + + +Mutex::Mutex() +{ +} + +Mutex::~Mutex() +{ + pthread_mutex_destroy(&mMutex); +} + +bool Mutex::init() +{ + int status = pthread_mutex_init(&mMutex, NULL); + if (status) { + LOGE("Mutex::Mutex init failure"); + return false; + } + return true; +} + +bool Mutex::lock() +{ + int status; + status = pthread_mutex_lock(&mMutex); + if (status) { + LOGE("Mutex: error %i locking.", status); + return false; + } + return true; +} + +bool Mutex::unlock() +{ + int status; + status = pthread_mutex_unlock(&mMutex); + if (status) { + LOGE("Mutex error %i unlocking.", status); + return false; + } + return true; +} + + diff --git a/libs/rs/rsMutex.h b/libs/rs/rsMutex.h new file mode 100644 index 0000000..47725d7 --- /dev/null +++ b/libs/rs/rsMutex.h @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2009 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. + */ + +#ifndef ANDROID_RS_MUTEX_H +#define ANDROID_RS_MUTEX_H + + +#include "rsUtils.h" + +namespace android { +namespace renderscript { + +class Mutex { +public: + Mutex(); + ~Mutex(); + + bool init(); + bool lock(); + bool unlock(); + +protected: + pthread_mutex_t mMutex; +}; + +} +} + +#endif + diff --git a/libs/rs/rsSignal.cpp b/libs/rs/rsSignal.cpp new file mode 100644 index 0000000..9239bfd --- /dev/null +++ b/libs/rs/rsSignal.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2009 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. + */ + +#include "rsSignal.h" + +using namespace android; +using namespace android::renderscript; + + +Signal::Signal() +{ + mSet = true; +} + +Signal::~Signal() +{ + pthread_mutex_destroy(&mMutex); + pthread_cond_destroy(&mCondition); +} + +bool Signal::init() +{ + int status = pthread_mutex_init(&mMutex, NULL); + if (status) { + LOGE("LocklessFifo mutex init failure"); + return false; + } + + status = pthread_cond_init(&mCondition, NULL); + if (status) { + LOGE("LocklessFifo condition init failure"); + pthread_mutex_destroy(&mMutex); + return false; + } + + return true; +} + +void Signal::set() +{ + int status; + + status = pthread_mutex_lock(&mMutex); + if (status) { + LOGE("LocklessCommandFifo: error %i locking for set condition.", status); + return; + } + + mSet = true; + + status = pthread_cond_signal(&mCondition); + if (status) { + LOGE("LocklessCommandFifo: error %i on set condition.", status); + } + + status = pthread_mutex_unlock(&mMutex); + if (status) { + LOGE("LocklessCommandFifo: error %i unlocking for set condition.", status); + } +} + +void Signal::wait() +{ + int status; + + status = pthread_mutex_lock(&mMutex); + if (status) { + LOGE("LocklessCommandFifo: error %i locking for condition.", status); + return; + } + + if (!mSet) { + status = pthread_cond_wait(&mCondition, &mMutex); + if (status) { + LOGE("LocklessCommandFifo: error %i waiting on condition.", status); + } + } + mSet = false; + + status = pthread_mutex_unlock(&mMutex); + if (status) { + LOGE("LocklessCommandFifo: error %i unlocking for condition.", status); + } +} + diff --git a/libs/rs/rsSignal.h b/libs/rs/rsSignal.h new file mode 100644 index 0000000..2e760f1 --- /dev/null +++ b/libs/rs/rsSignal.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2009 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. + */ + +#ifndef ANDROID_RS_SIGNAL_H +#define ANDROID_RS_SIGNAL_H + + +#include "rsUtils.h" + +namespace android { +namespace renderscript { + +class Signal { +public: + Signal(); + ~Signal(); + + bool init(); + + void set(); + void wait(); + +protected: + bool mSet; + pthread_mutex_t mMutex; + pthread_cond_t mCondition; +}; + +} +} + +#endif + diff --git a/libs/rs/rsg_ScriptJavaClass.cpp b/libs/rs/rsg_ScriptJavaClass.cpp index cee9f52..0169b98 100644 --- a/libs/rs/rsg_ScriptJavaClass.cpp +++ b/libs/rs/rsg_ScriptJavaClass.cpp @@ -7,8 +7,12 @@ struct Element; struct ElementField { + // An Element Field is a combination of an Element with a name assigned. + const char *name; Element *e; + + ElementField(const char *n, Element *_e) { name = n; e = _e; @@ -20,12 +24,21 @@ struct ElementField { }; struct Element { + // An Element can take one of two forms. + // 1: Basic. It contains a single basic type and vector size. + // 2: Complex. It contains a list of fields with names. Each field + // will in turn be another element. + ElementField *fields; - size_t fieldCount; + size_t fieldCount; // If field count is 0, the element is a Basic type. const char *name; bool generated; + // The basic data type from RenderScript.h RsDataType compType; + + // The vector size of the data type for float2, float3, .... + // Allowed sizes are 2,3,4,8,16 uint32_t compVectorSize; Element() { diff --git a/libs/rs/scriptc/rs_geom.rsh b/libs/rs/scriptc/rs_geom.rsh new file mode 100644 index 0000000..6e9e9fc --- /dev/null +++ b/libs/rs/scriptc/rs_geom.rsh @@ -0,0 +1,26 @@ + +extern float3 __attribute__((overloadable)) cross(float3, float3); +extern float4 __attribute__((overloadable)) cross(float4, float4); + +//extern float __attribute__((overloadable)) dot(float, float); +extern float __attribute__((overloadable)) dot(float2, float2); +extern float __attribute__((overloadable)) dot(float3, float3); +extern float __attribute__((overloadable)) dot(float4, float4); + +//extern float __attribute__((overloadable)) distance(float, float); +extern float __attribute__((overloadable)) distance(float2, float2); +extern float __attribute__((overloadable)) distance(float3, float3); +extern float __attribute__((overloadable)) distance(float4, float4); + +//extern float __attribute__((overloadable)) length(float); +extern float __attribute__((overloadable)) length(float2); +extern float __attribute__((overloadable)) length(float3); +extern float __attribute__((overloadable)) length(float4); + +extern float2 __attribute__((overloadable)) normalize(float2); +extern float3 __attribute__((overloadable)) normalize(float3); +extern float4 __attribute__((overloadable)) normalize(float4); + + + + diff --git a/libs/rs/scriptc/rs_graphics.rsh b/libs/rs/scriptc/rs_graphics.rsh index 70cd562..0f03732 100644 --- a/libs/rs/scriptc/rs_graphics.rsh +++ b/libs/rs/scriptc/rs_graphics.rsh @@ -1,5 +1,7 @@ +extern float rand(float max); + extern float2 vec2Rand(float len); extern float3 float3Norm(float3); diff --git a/libs/rs/scriptc/rs_math.rsh b/libs/rs/scriptc/rs_math.rsh index 613c7ca..fba0f8f 100644 --- a/libs/rs/scriptc/rs_math.rsh +++ b/libs/rs/scriptc/rs_math.rsh @@ -1,264 +1,262 @@ // Float ops extern float __attribute__((overloadable)) abs(float); -extern float2 __attribute__((overloadable)) abs(float2); -extern float3 __attribute__((overloadable)) abs(float3); -extern float4 __attribute__((overloadable)) abs(float4); -extern float8 __attribute__((overloadable)) abs(float8); -extern float16 __attribute__((overloadable)) abs(float16); +//extern float2 __attribute__((overloadable)) abs(float2); +//extern float3 __attribute__((overloadable)) abs(float3); +//extern float4 __attribute__((overloadable)) abs(float4); +//extern float8 __attribute__((overloadable)) abs(float8); +//extern float16 __attribute__((overloadable)) abs(float16); extern float __attribute__((overloadable)) acos(float); -extern float2 __attribute__((overloadable)) acos(float2); -extern float3 __attribute__((overloadable)) acos(float3); -extern float4 __attribute__((overloadable)) acos(float4); -extern float8 __attribute__((overloadable)) acos(float8); -extern float16 __attribute__((overloadable)) acos(float16); +//extern float2 __attribute__((overloadable)) acos(float2); +//extern float3 __attribute__((overloadable)) acos(float3); +//extern float4 __attribute__((overloadable)) acos(float4); +//extern float8 __attribute__((overloadable)) acos(float8); +//extern float16 __attribute__((overloadable)) acos(float16); extern float __attribute__((overloadable)) asin(float); -extern float2 __attribute__((overloadable)) asin(float2); -extern float3 __attribute__((overloadable)) asin(float3); -extern float4 __attribute__((overloadable)) asin(float4); -extern float8 __attribute__((overloadable)) asin(float8); -extern float16 __attribute__((overloadable)) asin(float16); +//extern float2 __attribute__((overloadable)) asin(float2); +//extern float3 __attribute__((overloadable)) asin(float3); +//extern float4 __attribute__((overloadable)) asin(float4); +//extern float8 __attribute__((overloadable)) asin(float8); +//extern float16 __attribute__((overloadable)) asin(float16); extern float __attribute__((overloadable)) atan(float); -extern float2 __attribute__((overloadable)) atan(float2); -extern float3 __attribute__((overloadable)) atan(float3); -extern float4 __attribute__((overloadable)) atan(float4); -extern float8 __attribute__((overloadable)) atan(float8); -extern float16 __attribute__((overloadable)) atan(float16); +//extern float2 __attribute__((overloadable)) atan(float2); +//extern float3 __attribute__((overloadable)) atan(float3); +//extern float4 __attribute__((overloadable)) atan(float4); +//extern float8 __attribute__((overloadable)) atan(float8); +//extern float16 __attribute__((overloadable)) atan(float16); extern float __attribute__((overloadable)) atan2(float, float); -extern float2 __attribute__((overloadable)) atan2(float2, float2); -extern float3 __attribute__((overloadable)) atan2(float3, float3); -extern float4 __attribute__((overloadable)) atan2(float4, float4); -extern float8 __attribute__((overloadable)) atan2(float8, float8); -extern float16 __attribute__((overloadable)) atan2(float16, float16); +//extern float2 __attribute__((overloadable)) atan2(float2, float2); +//extern float3 __attribute__((overloadable)) atan2(float3, float3); +//extern float4 __attribute__((overloadable)) atan2(float4, float4); +//extern float8 __attribute__((overloadable)) atan2(float8, float8); +//extern float16 __attribute__((overloadable)) atan2(float16, float16); extern float __attribute__((overloadable)) ceil(float); -extern float2 __attribute__((overloadable)) ceil(float2); -extern float3 __attribute__((overloadable)) ceil(float3); -extern float4 __attribute__((overloadable)) ceil(float4); -extern float8 __attribute__((overloadable)) ceil(float8); -extern float16 __attribute__((overloadable)) ceil(float16); +//extern float2 __attribute__((overloadable)) ceil(float2); +//extern float3 __attribute__((overloadable)) ceil(float3); +//extern float4 __attribute__((overloadable)) ceil(float4); +//extern float8 __attribute__((overloadable)) ceil(float8); +//extern float16 __attribute__((overloadable)) ceil(float16); extern float __attribute__((overloadable)) clamp(float, float, float); -extern float2 __attribute__((overloadable)) clamp(float2, float2, float2); -extern float3 __attribute__((overloadable)) clamp(float3, float3, float3); -extern float4 __attribute__((overloadable)) clamp(float4, float4, float4); -extern float8 __attribute__((overloadable)) clamp(float8, float8, float8); -extern float16 __attribute__((overloadable)) clamp(float16, float16, float16); -extern float __attribute__((overloadable)) clamp(float, float, float); -extern float2 __attribute__((overloadable)) clamp(float2, float, float); -extern float3 __attribute__((overloadable)) clamp(float3, float, float); -extern float4 __attribute__((overloadable)) clamp(float4, float, float); -extern float8 __attribute__((overloadable)) clamp(float8, float, float); -extern float16 __attribute__((overloadable)) clamp(float16, float, float); +//extern float2 __attribute__((overloadable)) clamp(float2, float2, float2); +//extern float3 __attribute__((overloadable)) clamp(float3, float3, float3); +//extern float4 __attribute__((overloadable)) clamp(float4, float4, float4); +//extern float8 __attribute__((overloadable)) clamp(float8, float8, float8); +//extern float16 __attribute__((overloadable)) clamp(float16, float16, float16); +//extern float2 __attribute__((overloadable)) clamp(float2, float, float); +//extern float3 __attribute__((overloadable)) clamp(float3, float, float); +//extern float4 __attribute__((overloadable)) clamp(float4, float, float); +//extern float8 __attribute__((overloadable)) clamp(float8, float, float); +//extern float16 __attribute__((overloadable)) clamp(float16, float, float); extern float __attribute__((overloadable)) copysign(float, float); -extern float2 __attribute__((overloadable)) copysign(float2, float2); -extern float3 __attribute__((overloadable)) copysign(float3, float3); -extern float4 __attribute__((overloadable)) copysign(float4, float4); -extern float8 __attribute__((overloadable)) copysign(float8, float8); -extern float16 __attribute__((overloadable)) copysign(float16, float16); +//extern float2 __attribute__((overloadable)) copysign(float2, float2); +//extern float3 __attribute__((overloadable)) copysign(float3, float3); +//extern float4 __attribute__((overloadable)) copysign(float4, float4); +//extern float8 __attribute__((overloadable)) copysign(float8, float8); +//extern float16 __attribute__((overloadable)) copysign(float16, float16); extern float __attribute__((overloadable)) cos(float); -extern float2 __attribute__((overloadable)) cos(float2); -extern float3 __attribute__((overloadable)) cos(float3); -extern float4 __attribute__((overloadable)) cos(float4); -extern float8 __attribute__((overloadable)) cos(float8); -extern float16 __attribute__((overloadable)) cos(float16); +//extern float2 __attribute__((overloadable)) cos(float2); +//extern float3 __attribute__((overloadable)) cos(float3); +//extern float4 __attribute__((overloadable)) cos(float4); +//extern float8 __attribute__((overloadable)) cos(float8); +//extern float16 __attribute__((overloadable)) cos(float16); extern float __attribute__((overloadable)) degrees(float); -extern float2 __attribute__((overloadable)) degrees(float2); -extern float3 __attribute__((overloadable)) degrees(float3); -extern float4 __attribute__((overloadable)) degrees(float4); -extern float8 __attribute__((overloadable)) degrees(float8); -extern float16 __attribute__((overloadable)) degrees(float16); +//extern float2 __attribute__((overloadable)) degrees(float2); +//extern float3 __attribute__((overloadable)) degrees(float3); +//extern float4 __attribute__((overloadable)) degrees(float4); +//extern float8 __attribute__((overloadable)) degrees(float8); +//extern float16 __attribute__((overloadable)) degrees(float16); extern float __attribute__((overloadable)) exp(float); -extern float2 __attribute__((overloadable)) exp(float2); -extern float3 __attribute__((overloadable)) exp(float3); -extern float4 __attribute__((overloadable)) exp(float4); -extern float8 __attribute__((overloadable)) exp(float8); -extern float16 __attribute__((overloadable)) exp(float16); +//extern float2 __attribute__((overloadable)) exp(float2); +//extern float3 __attribute__((overloadable)) exp(float3); +//extern float4 __attribute__((overloadable)) exp(float4); +//extern float8 __attribute__((overloadable)) exp(float8); +//extern float16 __attribute__((overloadable)) exp(float16); extern float __attribute__((overloadable)) exp2(float); -extern float2 __attribute__((overloadable)) exp2(float2); -extern float3 __attribute__((overloadable)) exp2(float3); -extern float4 __attribute__((overloadable)) exp2(float4); -extern float8 __attribute__((overloadable)) exp2(float8); -extern float16 __attribute__((overloadable)) exp2(float16); +//extern float2 __attribute__((overloadable)) exp2(float2); +//extern float3 __attribute__((overloadable)) exp2(float3); +//extern float4 __attribute__((overloadable)) exp2(float4); +//extern float8 __attribute__((overloadable)) exp2(float8); +//extern float16 __attribute__((overloadable)) exp2(float16); extern float __attribute__((overloadable)) exp10(float); -extern float2 __attribute__((overloadable)) exp10(float2); -extern float3 __attribute__((overloadable)) exp10(float3); -extern float4 __attribute__((overloadable)) exp10(float4); -extern float8 __attribute__((overloadable)) exp10(float8); -extern float16 __attribute__((overloadable)) exp10(float16); +//extern float2 __attribute__((overloadable)) exp10(float2); +//extern float3 __attribute__((overloadable)) exp10(float3); +//extern float4 __attribute__((overloadable)) exp10(float4); +//extern float8 __attribute__((overloadable)) exp10(float8); +//extern float16 __attribute__((overloadable)) exp10(float16); extern float __attribute__((overloadable)) fabs(float); -extern float2 __attribute__((overloadable)) fabs(float2); -extern float3 __attribute__((overloadable)) fabs(float3); -extern float4 __attribute__((overloadable)) fabs(float4); -extern float8 __attribute__((overloadable)) fabs(float8); -extern float16 __attribute__((overloadable)) fabs(float16); +//extern float2 __attribute__((overloadable)) fabs(float2); +//extern float3 __attribute__((overloadable)) fabs(float3); +//extern float4 __attribute__((overloadable)) fabs(float4); +//extern float8 __attribute__((overloadable)) fabs(float8); +//extern float16 __attribute__((overloadable)) fabs(float16); extern float __attribute__((overloadable)) floor(float); -extern float2 __attribute__((overloadable)) floor(float2); -extern float3 __attribute__((overloadable)) floor(float3); -extern float4 __attribute__((overloadable)) floor(float4); -extern float8 __attribute__((overloadable)) floor(float8); -extern float16 __attribute__((overloadable)) floor(float16); +//extern float2 __attribute__((overloadable)) floor(float2); +//extern float3 __attribute__((overloadable)) floor(float3); +//extern float4 __attribute__((overloadable)) floor(float4); +//extern float8 __attribute__((overloadable)) floor(float8); +//extern float16 __attribute__((overloadable)) floor(float16); extern float __attribute__((overloadable)) fmax(float, float); -extern float2 __attribute__((overloadable)) fmax(float2, float2); -extern float3 __attribute__((overloadable)) fmax(float3, float3); -extern float4 __attribute__((overloadable)) fmax(float4, float4); -extern float8 __attribute__((overloadable)) fmax(float8, float8); -extern float16 __attribute__((overloadable)) fmax(float16, float16); -extern float2 __attribute__((overloadable)) fmax(float2, float); -extern float3 __attribute__((overloadable)) fmax(float3, float); -extern float4 __attribute__((overloadable)) fmax(float4, float); -extern float8 __attribute__((overloadable)) fmax(float8, float); -extern float16 __attribute__((overloadable)) fmax(float16, float); +//extern float2 __attribute__((overloadable)) fmax(float2, float2); +//extern float3 __attribute__((overloadable)) fmax(float3, float3); +//extern float4 __attribute__((overloadable)) fmax(float4, float4); +//extern float8 __attribute__((overloadable)) fmax(float8, float8); +//extern float16 __attribute__((overloadable)) fmax(float16, float16); +//extern float2 __attribute__((overloadable)) fmax(float2, float); +//extern float3 __attribute__((overloadable)) fmax(float3, float); +//extern float4 __attribute__((overloadable)) fmax(float4, float); +//extern float8 __attribute__((overloadable)) fmax(float8, float); +//extern float16 __attribute__((overloadable)) fmax(float16, float); extern float __attribute__((overloadable)) fmin(float, float); -extern float2 __attribute__((overloadable)) fmin(float2, float2); -extern float3 __attribute__((overloadable)) fmin(float3, float3); -extern float4 __attribute__((overloadable)) fmin(float4, float4); -extern float8 __attribute__((overloadable)) fmin(float8, float8); -extern float16 __attribute__((overloadable)) fmin(float16, float16); -extern float2 __attribute__((overloadable)) fmin(float2, float); -extern float3 __attribute__((overloadable)) fmin(float3, float); -extern float4 __attribute__((overloadable)) fmin(float4, float); -extern float8 __attribute__((overloadable)) fmin(float8, float); -extern float16 __attribute__((overloadable)) fmin(float16, float); +//extern float2 __attribute__((overloadable)) fmin(float2, float2); +//extern float3 __attribute__((overloadable)) fmin(float3, float3); +//extern float4 __attribute__((overloadable)) fmin(float4, float4); +//extern float8 __attribute__((overloadable)) fmin(float8, float8); +//extern float16 __attribute__((overloadable)) fmin(float16, float16); +//extern float2 __attribute__((overloadable)) fmin(float2, float); +//extern float3 __attribute__((overloadable)) fmin(float3, float); +//extern float4 __attribute__((overloadable)) fmin(float4, float); +//extern float8 __attribute__((overloadable)) fmin(float8, float); +//extern float16 __attribute__((overloadable)) fmin(float16, float); extern float __attribute__((overloadable)) fmod(float, float); -extern float2 __attribute__((overloadable)) fmod(float2, float2); -extern float3 __attribute__((overloadable)) fmod(float3, float3); -extern float4 __attribute__((overloadable)) fmod(float4, float4); -extern float8 __attribute__((overloadable)) fmod(float8, float8); -extern float16 __attribute__((overloadable)) fmod(float16, float16); +//extern float2 __attribute__((overloadable)) fmod(float2, float2); +//extern float3 __attribute__((overloadable)) fmod(float3, float3); +//extern float4 __attribute__((overloadable)) fmod(float4, float4); +//extern float8 __attribute__((overloadable)) fmod(float8, float8); +//extern float16 __attribute__((overloadable)) fmod(float16, float16); extern float __attribute__((overloadable)) log(float); -extern float2 __attribute__((overloadable)) log(float2); -extern float3 __attribute__((overloadable)) log(float3); -extern float4 __attribute__((overloadable)) log(float4); -extern float8 __attribute__((overloadable)) log(float8); -extern float16 __attribute__((overloadable)) log(float16); +//extern float2 __attribute__((overloadable)) log(float2); +//extern float3 __attribute__((overloadable)) log(float3); +//extern float4 __attribute__((overloadable)) log(float4); +//extern float8 __attribute__((overloadable)) log(float8); +//extern float16 __attribute__((overloadable)) log(float16); extern float __attribute__((overloadable)) log2(float); -extern float2 __attribute__((overloadable)) log2(float2); -extern float3 __attribute__((overloadable)) log2(float3); -extern float4 __attribute__((overloadable)) log2(float4); -extern float8 __attribute__((overloadable)) log2(float8); -extern float16 __attribute__((overloadable)) log2(float16); +//extern float2 __attribute__((overloadable)) log2(float2); +//extern float3 __attribute__((overloadable)) log2(float3); +//extern float4 __attribute__((overloadable)) log2(float4); +//extern float8 __attribute__((overloadable)) log2(float8); +//extern float16 __attribute__((overloadable)) log2(float16); extern float __attribute__((overloadable)) log10(float); -extern float2 __attribute__((overloadable)) log10(float2); -extern float3 __attribute__((overloadable)) log10(float3); -extern float4 __attribute__((overloadable)) log10(float4); -extern float8 __attribute__((overloadable)) log10(float8); -extern float16 __attribute__((overloadable)) log10(float16); +//extern float2 __attribute__((overloadable)) log10(float2); +//extern float3 __attribute__((overloadable)) log10(float3); +//extern float4 __attribute__((overloadable)) log10(float4); +//extern float8 __attribute__((overloadable)) log10(float8); +//extern float16 __attribute__((overloadable)) log10(float16); extern float __attribute__((overloadable)) max(float, float); -extern float2 __attribute__((overloadable)) max(float2, float2); -extern float3 __attribute__((overloadable)) max(float3, float3); -extern float4 __attribute__((overloadable)) max(float4, float4); -extern float8 __attribute__((overloadable)) max(float8, float8); -extern float16 __attribute__((overloadable)) max(float16, float16); +//extern float2 __attribute__((overloadable)) max(float2, float2); +//extern float3 __attribute__((overloadable)) max(float3, float3); +//extern float4 __attribute__((overloadable)) max(float4, float4); +//extern float8 __attribute__((overloadable)) max(float8, float8); +//extern float16 __attribute__((overloadable)) max(float16, float16); extern float __attribute__((overloadable)) min(float, float); -extern float2 __attribute__((overloadable)) min(float2, float2); -extern float3 __attribute__((overloadable)) min(float3, float3); -extern float4 __attribute__((overloadable)) min(float4, float4); -extern float8 __attribute__((overloadable)) min(float8, float8); -extern float16 __attribute__((overloadable)) min(float16, float16); +//extern float2 __attribute__((overloadable)) min(float2, float2); +//extern float3 __attribute__((overloadable)) min(float3, float3); +//extern float4 __attribute__((overloadable)) min(float4, float4); +//extern float8 __attribute__((overloadable)) min(float8, float8); +//extern float16 __attribute__((overloadable)) min(float16, float16); extern float __attribute__((overloadable)) mix(float, float, float); -extern float2 __attribute__((overloadable)) mix(float2, float2, float2); -extern float3 __attribute__((overloadable)) mix(float3, float3, float3); -extern float4 __attribute__((overloadable)) mix(float4, float4, float4); -extern float8 __attribute__((overloadable)) mix(float8, float8, float8); -extern float16 __attribute__((overloadable)) mix(float16, float16, float16); -extern float __attribute__((overloadable)) mix(float, float, float); -extern float2 __attribute__((overloadable)) mix(float2, float2, float); -extern float3 __attribute__((overloadable)) mix(float3, float3, float); -extern float4 __attribute__((overloadable)) mix(float4, float4, float); -extern float8 __attribute__((overloadable)) mix(float8, float8, float); -extern float16 __attribute__((overloadable)) mix(float16, float16, float); +//extern float2 __attribute__((overloadable)) mix(float2, float2, float2); +//extern float3 __attribute__((overloadable)) mix(float3, float3, float3); +//extern float4 __attribute__((overloadable)) mix(float4, float4, float4); +//extern float8 __attribute__((overloadable)) mix(float8, float8, float8); +//extern float16 __attribute__((overloadable)) mix(float16, float16, float16); +//extern float2 __attribute__((overloadable)) mix(float2, float2, float); +//extern float3 __attribute__((overloadable)) mix(float3, float3, float); +//extern float4 __attribute__((overloadable)) mix(float4, float4, float); +//extern float8 __attribute__((overloadable)) mix(float8, float8, float); +//extern float16 __attribute__((overloadable)) mix(float16, float16, float); extern float __attribute__((overloadable)) pow(float, float); -extern float2 __attribute__((overloadable)) pow(float2, float2); -extern float3 __attribute__((overloadable)) pow(float3, float3); -extern float4 __attribute__((overloadable)) pow(float4, float4); -extern float8 __attribute__((overloadable)) pow(float8, float8); -extern float16 __attribute__((overloadable)) pow(float16, float16); +//extern float2 __attribute__((overloadable)) pow(float2, float2); +//extern float3 __attribute__((overloadable)) pow(float3, float3); +//extern float4 __attribute__((overloadable)) pow(float4, float4); +//extern float8 __attribute__((overloadable)) pow(float8, float8); +//extern float16 __attribute__((overloadable)) pow(float16, float16); extern float __attribute__((overloadable)) radians(float); -extern float2 __attribute__((overloadable)) radians(float2); -extern float3 __attribute__((overloadable)) radians(float3); -extern float4 __attribute__((overloadable)) radians(float4); -extern float8 __attribute__((overloadable)) radians(float8); -extern float16 __attribute__((overloadable)) radians(float16); +//extern float2 __attribute__((overloadable)) radians(float2); +//extern float3 __attribute__((overloadable)) radians(float3); +//extern float4 __attribute__((overloadable)) radians(float4); +//extern float8 __attribute__((overloadable)) radians(float8); +//extern float16 __attribute__((overloadable)) radians(float16); extern float __attribute__((overloadable)) rint(float); -extern float2 __attribute__((overloadable)) rint(float2); -extern float3 __attribute__((overloadable)) rint(float3); -extern float4 __attribute__((overloadable)) rint(float4); -extern float8 __attribute__((overloadable)) rint(float8); -extern float16 __attribute__((overloadable)) rint(float16); +//extern float2 __attribute__((overloadable)) rint(float2); +//extern float3 __attribute__((overloadable)) rint(float3); +//extern float4 __attribute__((overloadable)) rint(float4); +//extern float8 __attribute__((overloadable)) rint(float8); +//extern float16 __attribute__((overloadable)) rint(float16); extern float __attribute__((overloadable)) round(float); -extern float2 __attribute__((overloadable)) round(float2); -extern float3 __attribute__((overloadable)) round(float3); -extern float4 __attribute__((overloadable)) round(float4); -extern float8 __attribute__((overloadable)) round(float8); -extern float16 __attribute__((overloadable)) round(float16); +//extern float2 __attribute__((overloadable)) round(float2); +//extern float3 __attribute__((overloadable)) round(float3); +//extern float4 __attribute__((overloadable)) round(float4); +//extern float8 __attribute__((overloadable)) round(float8); +//extern float16 __attribute__((overloadable)) round(float16); extern float __attribute__((overloadable)) rsqrt(float); -extern float2 __attribute__((overloadable)) rsqrt(float2); -extern float3 __attribute__((overloadable)) rsqrt(float3); -extern float4 __attribute__((overloadable)) rsqrt(float4); -extern float8 __attribute__((overloadable)) rsqrt(float8); -extern float16 __attribute__((overloadable)) rsqrt(float16); +//extern float2 __attribute__((overloadable)) rsqrt(float2); +//extern float3 __attribute__((overloadable)) rsqrt(float3); +//extern float4 __attribute__((overloadable)) rsqrt(float4); +//extern float8 __attribute__((overloadable)) rsqrt(float8); +//extern float16 __attribute__((overloadable)) rsqrt(float16); extern float __attribute__((overloadable)) sign(float); -extern float2 __attribute__((overloadable)) sign(float2); -extern float3 __attribute__((overloadable)) sign(float3); -extern float4 __attribute__((overloadable)) sign(float4); -extern float8 __attribute__((overloadable)) sign(float8); -extern float16 __attribute__((overloadable)) sign(float16); +//extern float2 __attribute__((overloadable)) sign(float2); +//extern float3 __attribute__((overloadable)) sign(float3); +//extern float4 __attribute__((overloadable)) sign(float4); +//extern float8 __attribute__((overloadable)) sign(float8); +//extern float16 __attribute__((overloadable)) sign(float16); extern float __attribute__((overloadable)) sin(float); -extern float2 __attribute__((overloadable)) sin(float2); -extern float3 __attribute__((overloadable)) sin(float3); -extern float4 __attribute__((overloadable)) sin(float4); -extern float8 __attribute__((overloadable)) sin(float8); -extern float16 __attribute__((overloadable)) sin(float16); +//extern float2 __attribute__((overloadable)) sin(float2); +//extern float3 __attribute__((overloadable)) sin(float3); +//extern float4 __attribute__((overloadable)) sin(float4); +//extern float8 __attribute__((overloadable)) sin(float8); +//extern float16 __attribute__((overloadable)) sin(float16); extern float __attribute__((overloadable)) sqrt(float); -extern float2 __attribute__((overloadable)) sqrt(float2); -extern float3 __attribute__((overloadable)) sqrt(float3); -extern float4 __attribute__((overloadable)) sqrt(float4); -extern float8 __attribute__((overloadable)) sqrt(float8); -extern float16 __attribute__((overloadable)) sqrt(float16); +//extern float2 __attribute__((overloadable)) sqrt(float2); +//extern float3 __attribute__((overloadable)) sqrt(float3); +//extern float4 __attribute__((overloadable)) sqrt(float4); +//extern float8 __attribute__((overloadable)) sqrt(float8); +//extern float16 __attribute__((overloadable)) sqrt(float16); extern float __attribute__((overloadable)) tan(float); -extern float2 __attribute__((overloadable)) tan(float2); -extern float3 __attribute__((overloadable)) tan(float3); -extern float4 __attribute__((overloadable)) tan(float4); -extern float8 __attribute__((overloadable)) tan(float8); -extern float16 __attribute__((overloadable)) tan(float16); +//extern float2 __attribute__((overloadable)) tan(float2); +//extern float3 __attribute__((overloadable)) tan(float3); +//extern float4 __attribute__((overloadable)) tan(float4); +//extern float8 __attribute__((overloadable)) tan(float8); +//extern float16 __attribute__((overloadable)) tan(float16); extern float __attribute__((overloadable)) trunc(float); -extern float2 __attribute__((overloadable)) trunc(float2); -extern float3 __attribute__((overloadable)) trunc(float3); -extern float4 __attribute__((overloadable)) trunc(float4); -extern float8 __attribute__((overloadable)) trunc(float8); -extern float16 __attribute__((overloadable)) trunc(float16); +//extern float2 __attribute__((overloadable)) trunc(float2); +//extern float3 __attribute__((overloadable)) trunc(float3); +//extern float4 __attribute__((overloadable)) trunc(float4); +//extern float8 __attribute__((overloadable)) trunc(float8); +//extern float16 __attribute__((overloadable)) trunc(float16); @@ -268,11 +266,11 @@ extern float16 __attribute__((overloadable)) trunc(float16); // Int ops extern int __attribute__((overloadable)) abs(int); -extern int2 __attribute__((overloadable)) abs(int2); -extern int3 __attribute__((overloadable)) abs(int3); -extern int4 __attribute__((overloadable)) abs(int4); -extern int8 __attribute__((overloadable)) abs(int8); -extern int16 __attribute__((overloadable)) abs(int16); +//extern int2 __attribute__((overloadable)) abs(int2); +//extern int3 __attribute__((overloadable)) abs(int3); +//extern int4 __attribute__((overloadable)) abs(int4); +//extern int8 __attribute__((overloadable)) abs(int8); +//extern int16 __attribute__((overloadable)) abs(int16); diff --git a/libs/rs/scriptc/rs_types.rsh b/libs/rs/scriptc/rs_types.rsh index 4198a74..b710146 100644 --- a/libs/rs/scriptc/rs_types.rsh +++ b/libs/rs/scriptc/rs_types.rsh @@ -68,4 +68,29 @@ typedef int int8 __attribute__((ext_vector_type(8))); typedef int int16 __attribute__((ext_vector_type(16))); +// RS_KIND_POSITION +typedef float rs_position1; +typedef float2 rs_position2; +typedef float3 rs_position3; +typedef float4 rs_position4; + +// RS_KIND_COLOR +typedef float3 rs_color3f; +typedef float4 rs_color4f; +typedef uchar4 rs_color4u; + +// RS_KIND_NORMAL +typedef float3 rs_normal; + +// RS_KIND_POINT_SIZE +typedef float rs_point_size; + +// RS_KIND_TEXTURE +typedef float rs_texture_coord1; +typedef float2 rs_texture_coord2; +typedef float3 rs_texture_coord3; +typedef float4 rs_texture_coord4; + +// RS_KIND_INDEX +typedef ushort rs_index; diff --git a/media/java/android/media/AudioManager.java b/media/java/android/media/AudioManager.java index 47e2da2..2e3eae0 100644 --- a/media/java/android/media/AudioManager.java +++ b/media/java/android/media/AudioManager.java @@ -1523,4 +1523,22 @@ public class AudioManager { * {@hide} */ private IBinder mICallBack = new Binder(); + + /** + * Checks whether the phone is in silent mode, with or without vibrate. + * + * @return true if phone is in silent mode, with or without vibrate. + * + * @see #getRingerMode() + * + * @hide pending API Council approval + */ + public boolean isSilentMode() { + int ringerMode = getRingerMode(); + boolean silentMode = + (ringerMode == RINGER_MODE_SILENT) || + (ringerMode == RINGER_MODE_VIBRATE); + return silentMode; + } + } diff --git a/opengl/tests/testViewport/Android.mk b/opengl/tests/testViewport/Android.mk new file mode 100644 index 0000000..3da59b5 --- /dev/null +++ b/opengl/tests/testViewport/Android.mk @@ -0,0 +1,22 @@ +######################################################################### +# OpenGL ES JNI sample +# This makefile builds both an activity and a shared library. +######################################################################### +ifneq ($(TARGET_SIMULATOR),true) # not 64 bit clean + +TOP_LOCAL_PATH:= $(call my-dir) + +# Build activity + +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_MODULE_TAGS := optional + +LOCAL_SRC_FILES := $(call all-subdir-java-files) + +LOCAL_PACKAGE_NAME := TestViewport + +include $(BUILD_PACKAGE) + +endif # TARGET_SIMULATOR diff --git a/opengl/tests/testViewport/AndroidManifest.xml b/opengl/tests/testViewport/AndroidManifest.xml new file mode 100644 index 0000000..f4a493e --- /dev/null +++ b/opengl/tests/testViewport/AndroidManifest.xml @@ -0,0 +1,36 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- +/* +** +** Copyright 2009, 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. +*/ +--> + +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.test"> + <uses-permission android:name="android.permission.INTERNET" /> + <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> + <application + android:label="@string/test_activity"> + <activity android:name="TestActivity" + android:theme="@android:style/Theme.NoTitleBar.Fullscreen" + android:configChanges="orientation|keyboardHidden"> + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.LAUNCHER" /> + </intent-filter> + </activity> + </application> +</manifest> diff --git a/opengl/tests/testViewport/README b/opengl/tests/testViewport/README new file mode 100644 index 0000000..c06abc9 --- /dev/null +++ b/opengl/tests/testViewport/README @@ -0,0 +1,28 @@ +Repro steps: + +build, install and run the attached test program TestViewport.apk + +Run on Sapphire with Froyo. + +The program clears the screen to blue, then draws a full screen white quad that +is alligned to the screen. +(Therefore the whole screen should appear to be white.) + + +Note that screen is all white. + +Rotate screen 90 degrees. + +Expected: screen is still all white. + +Actual: screen is blue with offset white rectangle. + +This bug only happens on Sapphire, it works correctly on Passion. + +What happens: + +I think the bug is that the gl.glViewport() call in onSurfaceChanged() is +being ignored by the OpenGL driver. + +NOTE: If a gl.glViewport call is added at the beginning of the onDrawFrame() +call (which means it is called before every draw), the program runs correctly. diff --git a/opengl/tests/testViewport/res/values/strings.xml b/opengl/tests/testViewport/res/values/strings.xml new file mode 100644 index 0000000..f4b8bbb --- /dev/null +++ b/opengl/tests/testViewport/res/values/strings.xml @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- +/* +** +** Copyright 2006, 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. +*/ +--> + +<!-- This file contains resource definitions for displayed strings, allowing + them to be changed based on the locale and options. --> + +<resources> + <!-- Simple strings. --> + <string name="test_activity">Test Viewport</string> + +</resources> + diff --git a/opengl/tests/testViewport/src/com/android/test/TestActivity.java b/opengl/tests/testViewport/src/com/android/test/TestActivity.java new file mode 100644 index 0000000..cc7e450 --- /dev/null +++ b/opengl/tests/testViewport/src/com/android/test/TestActivity.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2007 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.test; + +import android.app.Activity; +import android.os.Bundle; +import android.util.Log; + +public class TestActivity extends Activity { + private final static String TAG = "TestActivity"; + TestView mView; + + @Override + protected void onCreate(Bundle icicle) { + super.onCreate(icicle); + mView = new TestView(getApplication()); + mView.setFocusableInTouchMode(true); + setContentView(mView); + } + + @Override + protected void onPause() { + super.onPause(); + mView.onPause(); + } + + @Override + protected void onResume() { + super.onResume(); + mView.onResume(); + } +} diff --git a/opengl/tests/testViewport/src/com/android/test/TestView.java b/opengl/tests/testViewport/src/com/android/test/TestView.java new file mode 100644 index 0000000..23cc37d --- /dev/null +++ b/opengl/tests/testViewport/src/com/android/test/TestView.java @@ -0,0 +1,262 @@ +/* + * Copyright (C) 2009 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.test; +/* + * Copyright (C) 2008 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. + */ + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.CharBuffer; +import java.nio.FloatBuffer; + +import android.content.Context; +import android.opengl.GLSurfaceView; +import android.util.AttributeSet; +import android.util.Log; +import android.view.KeyEvent; +import android.view.MotionEvent; + +import javax.microedition.khronos.egl.EGL10; +import javax.microedition.khronos.egl.EGLConfig; +import javax.microedition.khronos.opengles.GL; +import javax.microedition.khronos.opengles.GL10; +import javax.microedition.khronos.opengles.GL11; +/** + * An implementation of SurfaceView that uses the dedicated surface for + * displaying an OpenGL animation. This allows the animation to run in a + * separate thread, without requiring that it be driven by the update mechanism + * of the view hierarchy. + * + * The application-specific rendering code is delegated to a GLView.Renderer + * instance. + */ +class TestView extends GLSurfaceView { + TestView(Context context) { + super(context); + init(); + } + + public TestView(Context context, AttributeSet attrs) { + super(context, attrs); + init(); + } + + private void init() { + setRenderer(new Renderer()); + setRenderMode(RENDERMODE_WHEN_DIRTY); + } + + /** A grid is a topologically rectangular array of vertices. + * + * The vertex and index data are held in VBO objects because on most + * GPUs VBO objects are the fastest way of rendering static vertex + * and index data. + * + */ + + private static class Grid { + // Size of vertex data elements in bytes: + final static int FLOAT_SIZE = 4; + final static int CHAR_SIZE = 2; + + // Vertex structure: + // float x, y, z; + + final static int VERTEX_SIZE = 3 * FLOAT_SIZE; + + private int mVertexBufferObjectId; + private int mElementBufferObjectId; + + // These buffers are used to hold the vertex and index data while + // constructing the grid. Once createBufferObjects() is called + // the buffers are nulled out to save memory. + + private ByteBuffer mVertexByteBuffer; + private FloatBuffer mVertexBuffer; + private CharBuffer mIndexBuffer; + + private int mW; + private int mH; + private int mIndexCount; + + public Grid(int w, int h) { + if (w < 0 || w >= 65536) { + throw new IllegalArgumentException("w"); + } + if (h < 0 || h >= 65536) { + throw new IllegalArgumentException("h"); + } + if (w * h >= 65536) { + throw new IllegalArgumentException("w * h >= 65536"); + } + + mW = w; + mH = h; + int size = w * h; + + mVertexByteBuffer = ByteBuffer.allocateDirect(VERTEX_SIZE * size) + .order(ByteOrder.nativeOrder()); + mVertexBuffer = mVertexByteBuffer.asFloatBuffer(); + + int quadW = mW - 1; + int quadH = mH - 1; + int quadCount = quadW * quadH; + int indexCount = quadCount * 6; + mIndexCount = indexCount; + mIndexBuffer = ByteBuffer.allocateDirect(CHAR_SIZE * indexCount) + .order(ByteOrder.nativeOrder()).asCharBuffer(); + + /* + * Initialize triangle list mesh. + * + * [0]-----[ 1] ... + * | / | + * | / | + * | / | + * [w]-----[w+1] ... + * | | + * + */ + + { + int i = 0; + for (int y = 0; y < quadH; y++) { + for (int x = 0; x < quadW; x++) { + char a = (char) (y * mW + x); + char b = (char) (y * mW + x + 1); + char c = (char) ((y + 1) * mW + x); + char d = (char) ((y + 1) * mW + x + 1); + + mIndexBuffer.put(i++, a); + mIndexBuffer.put(i++, c); + mIndexBuffer.put(i++, b); + + mIndexBuffer.put(i++, b); + mIndexBuffer.put(i++, c); + mIndexBuffer.put(i++, d); + } + } + } + + } + + public void set(int i, int j, float x, float y, float z) { + if (i < 0 || i >= mW) { + throw new IllegalArgumentException("i"); + } + if (j < 0 || j >= mH) { + throw new IllegalArgumentException("j"); + } + + int index = mW * j + i; + + mVertexBuffer.position(index * VERTEX_SIZE / FLOAT_SIZE); + mVertexBuffer.put(x); + mVertexBuffer.put(y); + mVertexBuffer.put(z); + } + + public void createBufferObjects(GL gl) { + // Generate a the vertex and element buffer IDs + int[] vboIds = new int[2]; + GL11 gl11 = (GL11) gl; + gl11.glGenBuffers(2, vboIds, 0); + mVertexBufferObjectId = vboIds[0]; + mElementBufferObjectId = vboIds[1]; + + // Upload the vertex data + gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mVertexBufferObjectId); + mVertexByteBuffer.position(0); + gl11.glBufferData(GL11.GL_ARRAY_BUFFER, mVertexByteBuffer.capacity(), mVertexByteBuffer, GL11.GL_STATIC_DRAW); + + gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, mElementBufferObjectId); + mIndexBuffer.position(0); + gl11.glBufferData(GL11.GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer.capacity() * CHAR_SIZE, mIndexBuffer, GL11.GL_STATIC_DRAW); + + // We don't need the in-memory data any more + mVertexBuffer = null; + mVertexByteBuffer = null; + mIndexBuffer = null; + } + + public void draw(GL10 gl) { + GL11 gl11 = (GL11) gl; + + gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); + + gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mVertexBufferObjectId); + gl11.glVertexPointer(3, GL10.GL_FLOAT, VERTEX_SIZE, 0); + + gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, mElementBufferObjectId); + gl11.glDrawElements(GL10.GL_TRIANGLES, mIndexCount, GL10.GL_UNSIGNED_SHORT, 0); + gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); + gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0); + gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0); + } + } + + + private class Renderer implements GLSurfaceView.Renderer { + private static final String TAG = "Renderer"; + private Grid mGrid; + + public void onDrawFrame(GL10 gl) { + gl.glClearColor(0,0,1,1); + gl.glClear(GL10.GL_COLOR_BUFFER_BIT); + mGrid.draw(gl); + } + + public void onSurfaceChanged(GL10 gl, int width, int height) { + gl.glViewport(0, 0, width, height); + gl.glMatrixMode(GL11.GL_PROJECTION); + gl.glLoadIdentity(); + gl.glOrthof(0, width, height, 0, -1, 1); + gl.glMatrixMode(GL11.GL_MODELVIEW); + createGrid(gl, width, height); + } + + public void onSurfaceCreated(GL10 gl, EGLConfig config) { + } + + private void createGrid(GL10 gl, float w, float h) { + mGrid = new Grid(2, 2); + for (int j = 0; j < 2; j++) { + for (int i = 0; i < 2; i++) { + float x = w * i; + float y = h * j; + float z = 0.0f; + mGrid.set(i,j, x, y, z); + } + } + mGrid.createBufferObjects(gl); + } + } +} + diff --git a/policy/com/android/internal/policy/impl/LockScreen.java b/policy/com/android/internal/policy/impl/LockScreen.java index baed9eb..381b913 100644 --- a/policy/com/android/internal/policy/impl/LockScreen.java +++ b/policy/com/android/internal/policy/impl/LockScreen.java @@ -66,7 +66,7 @@ class LockScreen extends LinearLayout implements KeyguardScreen, KeyguardUpdateM private TextView mStatus1; private TextView mStatus2; private TextView mScreenLocked; - private Button mEmergencyCallButton; + private TextView mEmergencyCallText; // current configuration state of keyboard and display private int mKeyboardHidden; @@ -203,18 +203,12 @@ class LockScreen extends LinearLayout implements KeyguardScreen, KeyguardUpdateM mStatus1 = (TextView) findViewById(R.id.status1); mStatus2 = (TextView) findViewById(R.id.status2); - mEmergencyCallButton = (Button) findViewById(R.id.emergencyCallButton); - mEmergencyCallButton.setText(R.string.lockscreen_emergency_call); - mLockPatternUtils.updateEmergencyCallButtonState(mEmergencyCallButton); mScreenLocked = (TextView) findViewById(R.id.screenLocked); mSelector = (SlidingTab) findViewById(R.id.tab_selector); mSelector.setHoldAfterTrigger(true, false); mSelector.setLeftHintText(R.string.lockscreen_unlock_label); - mEmergencyCallButton.setOnClickListener(new View.OnClickListener() { - public void onClick(View v) { - mCallback.takeEmergencyCallAction(); - } - }); + + mEmergencyCallText = (TextView) findViewById(R.id.emergencyCallText); setFocusable(true); setFocusableInTouchMode(true); @@ -269,7 +263,6 @@ class LockScreen extends LinearLayout implements KeyguardScreen, KeyguardUpdateM refreshBatteryStringAndIcon(); refreshAlarmDisplay(); - mLockPatternUtils.updateEmergencyCallButtonState(mEmergencyCallButton); mTimeFormat = DateFormat.getTimeFormat(getContext()); mDateFormatString = getContext().getString(R.string.full_wday_month_day_no_year); @@ -340,8 +333,6 @@ class LockScreen extends LinearLayout implements KeyguardScreen, KeyguardUpdateM * @param iconResourceId The left hand icon. */ private void toastMessage(final TextView textView, final String text, final int color, final int iconResourceId) { - if (DBG) android.util.Log.d("LockScreen", "toastMessage(text=" + text +", color=" + color + ")"); - if (mPendingR1 != null) { textView.removeCallbacks(mPendingR1); mPendingR1 = null; @@ -499,9 +490,8 @@ class LockScreen extends LinearLayout implements KeyguardScreen, KeyguardUpdateM * Update the layout to match the current status. */ private void updateLayout(Status status) { - // The emergency call button appears where the carrier would - // ordinarily be shown, so if one is VISIBLE the other must be - // INVISIBLE. + // The emergency call button no longer appears on this screen. + if (DBG) Log.d(TAG, "updateLayout: status=" + status); switch (status) { case Normal: // text @@ -509,12 +499,13 @@ class LockScreen extends LinearLayout implements KeyguardScreen, KeyguardUpdateM getCarrierString( mUpdateMonitor.getTelephonyPlmn(), mUpdateMonitor.getTelephonySpn())); -// mScreenLocked.setText(R.string.lockscreen_screen_locked); + // unnecessary clutter + //mScreenLocked.setText(R.string.lockscreen_screen_locked); // layout - mScreenLocked.setVisibility(View.VISIBLE); + mScreenLocked.setVisibility(View.INVISIBLE); mSelector.setVisibility(View.VISIBLE); - mEmergencyCallButton.setVisibility(View.GONE); + mEmergencyCallText.setVisibility(View.GONE); break; case NetworkLocked: // The carrier string shows both sim card status (i.e. No Sim Card) and @@ -528,35 +519,30 @@ class LockScreen extends LinearLayout implements KeyguardScreen, KeyguardUpdateM // layout mScreenLocked.setVisibility(View.VISIBLE); mSelector.setVisibility(View.VISIBLE); - mEmergencyCallButton.setVisibility(View.GONE); + mEmergencyCallText.setVisibility(View.GONE); break; case SimMissing: // text - mCarrier.setText(""); - mScreenLocked.setText( - getCarrierString( - mUpdateMonitor.getTelephonyPlmn(), - getContext().getText(R.string.lockscreen_missing_sim_message_short))); - // previously shown here: lockscreen_instructions_when_pattern_disabled + mCarrier.setText(R.string.lockscreen_missing_sim_message_short); + mScreenLocked.setText(R.string.lockscreen_missing_sim_instructions); // layout mScreenLocked.setVisibility(View.VISIBLE); mSelector.setVisibility(View.VISIBLE); - mEmergencyCallButton.setVisibility(View.VISIBLE); + mEmergencyCallText.setVisibility(View.VISIBLE); break; case SimMissingLocked: // text - mCarrier.setText(""); - mScreenLocked.setText( + mCarrier.setText( getCarrierString( mUpdateMonitor.getTelephonyPlmn(), getContext().getText(R.string.lockscreen_missing_sim_message_short))); - // previously shown here: lockscreen_missing_sim_instructions + mScreenLocked.setText(R.string.lockscreen_missing_sim_instructions); // layout mScreenLocked.setVisibility(View.VISIBLE); mSelector.setVisibility(View.GONE); - mEmergencyCallButton.setVisibility(View.VISIBLE); + mEmergencyCallText.setVisibility(View.VISIBLE); break; case SimLocked: // text @@ -568,21 +554,20 @@ class LockScreen extends LinearLayout implements KeyguardScreen, KeyguardUpdateM // layout mScreenLocked.setVisibility(View.INVISIBLE); mSelector.setVisibility(View.VISIBLE); - mEmergencyCallButton.setVisibility(View.GONE); + mEmergencyCallText.setVisibility(View.GONE); break; case SimPukLocked: // text - mCarrier.setText(""); - mScreenLocked.setText( + mCarrier.setText( getCarrierString( mUpdateMonitor.getTelephonyPlmn(), getContext().getText(R.string.lockscreen_sim_puk_locked_message))); - // previously shown here: lockscreen_sim_puk_locked_instructions); + mScreenLocked.setText(R.string.lockscreen_sim_puk_locked_instructions); // layout mScreenLocked.setVisibility(View.VISIBLE); mSelector.setVisibility(View.GONE); - mEmergencyCallButton.setVisibility(View.VISIBLE); + mEmergencyCallText.setVisibility(View.VISIBLE); break; } } @@ -655,7 +640,6 @@ class LockScreen extends LinearLayout implements KeyguardScreen, KeyguardUpdateM /** {@inheritDoc} */ public void onResume() { resetStatusInfo(mUpdateMonitor); - mLockPatternUtils.updateEmergencyCallButtonState(mEmergencyCallButton); } /** {@inheritDoc} */ @@ -673,6 +657,5 @@ class LockScreen extends LinearLayout implements KeyguardScreen, KeyguardUpdateM } public void onPhoneStateChanged(String newState) { - mLockPatternUtils.updateEmergencyCallButtonState(mEmergencyCallButton); } } diff --git a/services/java/com/android/server/LocationManagerService.java b/services/java/com/android/server/LocationManagerService.java index f9c1a93..b92480f 100644 --- a/services/java/com/android/server/LocationManagerService.java +++ b/services/java/com/android/server/LocationManagerService.java @@ -462,10 +462,11 @@ public class LocationManagerService extends ILocationManager.Stub implements Run mEnabledProviders.add(passiveProvider.getName()); // initialize external network location and geocoder services + PackageManager pm = mContext. getPackageManager(); Resources resources = mContext.getResources(); String serviceName = resources.getString( com.android.internal.R.string.config_networkLocationProvider); - if (serviceName != null) { + if (serviceName != null && pm.resolveService(new Intent(serviceName), 0) != null) { mNetworkLocationProvider = new LocationProviderProxy(mContext, LocationManager.NETWORK_PROVIDER, serviceName, mLocationHandler); @@ -473,7 +474,7 @@ public class LocationManagerService extends ILocationManager.Stub implements Run } serviceName = resources.getString(com.android.internal.R.string.config_geocodeProvider); - if (serviceName != null) { + if (serviceName != null && pm.resolveService(new Intent(serviceName), 0) != null) { mGeocodeProvider = new GeocoderProxy(mContext, serviceName); } diff --git a/services/jni/com_android_server_location_GpsLocationProvider.cpp b/services/jni/com_android_server_location_GpsLocationProvider.cpp index 7a74fd4..d6f0d08 100755 --- a/services/jni/com_android_server_location_GpsLocationProvider.cpp +++ b/services/jni/com_android_server_location_GpsLocationProvider.cpp @@ -241,9 +241,9 @@ static jboolean android_location_GpsLocationProvider_init(JNIEnv* env, jobject o sAGpsInterface->init(&sAGpsCallbacks); if (!sGpsNiInterface) - sGpsNiInterface = (const GpsNiInterface*)sGpsInterface->get_extension(GPS_NI_INTERFACE); + sGpsNiInterface = (const GpsNiInterface*)sGpsInterface->get_extension(GPS_NI_INTERFACE); if (sGpsNiInterface) - sGpsNiInterface->init(&sGpsNiCallbacks); + sGpsNiInterface->init(&sGpsNiCallbacks); if (!sGpsDebugInterface) sGpsDebugInterface = (const GpsDebugInterface*)sGpsInterface->get_extension(GPS_DEBUG_INTERFACE); @@ -494,12 +494,10 @@ static void android_location_GpsLocationProvider_set_agps_server(JNIEnv* env, jo static void android_location_GpsLocationProvider_send_ni_response(JNIEnv* env, jobject obj, jint notifId, jint response) { - if (!sGpsNiInterface) { + if (!sGpsNiInterface) sGpsNiInterface = (const GpsNiInterface*)sGpsInterface->get_extension(GPS_NI_INTERFACE); - } - if (sGpsNiInterface) { + if (sGpsNiInterface) sGpsNiInterface->respond(notifId, response); - } } static jstring android_location_GpsLocationProvider_get_internal_state(JNIEnv* env, jobject obj) |