diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/Android.mk | 3 | ||||
-rw-r--r-- | common/NOTICE | 190 | ||||
-rw-r--r-- | common/src/com/android/AndroidConstants.java | 50 | ||||
-rw-r--r-- | common/src/com/android/io/FileWrapper.java | 151 | ||||
-rw-r--r-- | common/src/com/android/io/FolderWrapper.java | 154 | ||||
-rw-r--r-- | common/src/com/android/io/IAbstractFile.java | 58 | ||||
-rw-r--r-- | common/src/com/android/io/IAbstractFolder.java | 77 | ||||
-rw-r--r-- | common/src/com/android/io/IAbstractResource.java | 45 | ||||
-rw-r--r-- | common/src/com/android/io/StreamException.java | 28 | ||||
-rw-r--r-- | common/src/com/android/resources/FolderTypeRelationship.java | 164 | ||||
-rw-r--r-- | common/src/com/android/resources/ResourceFolderType.java | 78 | ||||
-rw-r--r-- | common/src/com/android/resources/ResourceType.java | 4 | ||||
-rw-r--r-- | common/tests/.classpath | 8 | ||||
-rw-r--r-- | common/tests/.project | 17 | ||||
-rw-r--r-- | common/tests/Android.mk | 27 | ||||
-rw-r--r-- | common/tests/src/com/android/resources/FolderTypeRelationShipTest.java | 44 |
16 files changed, 1096 insertions, 2 deletions
diff --git a/common/Android.mk b/common/Android.mk index aa2ba8e..ef907f2 100644 --- a/common/Android.mk +++ b/common/Android.mk @@ -25,3 +25,6 @@ LOCAL_MODULE := common LOCAL_MODULE_TAGS := optional include $(BUILD_HOST_JAVA_LIBRARY) + +# Build all sub-directories +include $(call all-makefiles-under,$(LOCAL_PATH)) diff --git a/common/NOTICE b/common/NOTICE new file mode 100644 index 0000000..c5b1efa --- /dev/null +++ b/common/NOTICE @@ -0,0 +1,190 @@ + + Copyright (c) 2005-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. + + 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. + + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + diff --git a/common/src/com/android/AndroidConstants.java b/common/src/com/android/AndroidConstants.java new file mode 100644 index 0000000..dfe137a --- /dev/null +++ b/common/src/com/android/AndroidConstants.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2011 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; + +/** + * Generic Android Constants. + */ +public class AndroidConstants { + + /** Default anim resource folder name, i.e. "anim" */ + public final static String FD_RES_ANIM = "anim"; //$NON-NLS-1$ + /** Default animator resource folder name, i.e. "animator" */ + public final static String FD_RES_ANIMATOR = "animator"; //$NON-NLS-1$ + /** Default color resource folder name, i.e. "color" */ + public final static String FD_RES_COLOR = "color"; //$NON-NLS-1$ + /** Default drawable resource folder name, i.e. "drawable" */ + public final static String FD_RES_DRAWABLE = "drawable"; //$NON-NLS-1$ + /** Default interpolator resource folder name, i.e. "interpolator" */ + public final static String FD_RES_INTERPOLATOR = "interpolator"; //$NON-NLS-1$ + /** Default layout resource folder name, i.e. "layout" */ + public final static String FD_RES_LAYOUT = "layout"; //$NON-NLS-1$ + /** Default menu resource folder name, i.e. "menu" */ + public final static String FD_RES_MENU = "menu"; //$NON-NLS-1$ + /** Default menu resource folder name, i.e. "mipmap" */ + public final static String FD_RES_MIPMAP = "mipmap"; //$NON-NLS-1$ + /** Default values resource folder name, i.e. "values" */ + public final static String FD_RES_VALUES = "values"; //$NON-NLS-1$ + /** Default xml resource folder name, i.e. "xml" */ + public final static String FD_RES_XML = "xml"; //$NON-NLS-1$ + /** Default raw resource folder name, i.e. "raw" */ + public final static String FD_RES_RAW = "raw"; //$NON-NLS-1$ + + /** Separator between the resource folder qualifier. */ + public final static String RES_QUALIFIER_SEP = "-"; //$NON-NLS-1$ + +} diff --git a/common/src/com/android/io/FileWrapper.java b/common/src/com/android/io/FileWrapper.java new file mode 100644 index 0000000..2859c0d --- /dev/null +++ b/common/src/com/android/io/FileWrapper.java @@ -0,0 +1,151 @@ +/* + * 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. + */ + +package com.android.io; + + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URI; + +/** + * An implementation of {@link IAbstractFile} extending {@link File}. + */ +public class FileWrapper extends File implements IAbstractFile { + private static final long serialVersionUID = 1L; + + /** + * Creates a new File instance matching a given {@link File} object. + * @param file the file to match + */ + public FileWrapper(File file) { + super(file.getAbsolutePath()); + } + + /** + * Creates a new File instance from a parent abstract pathname and a child pathname string. + * @param parent the parent pathname + * @param child the child name + * + * @see File#File(File, String) + */ + public FileWrapper(File parent, String child) { + super(parent, child); + } + + /** + * Creates a new File instance by converting the given pathname string into an abstract + * pathname. + * @param osPathname the OS pathname + * + * @see File#File(String) + */ + public FileWrapper(String osPathname) { + super(osPathname); + } + + /** + * Creates a new File instance from a parent abstract pathname and a child pathname string. + * @param parent the parent pathname + * @param child the child name + * + * @see File#File(String, String) + */ + public FileWrapper(String parent, String child) { + super(parent, child); + } + + /** + * Creates a new File instance by converting the given <code>file:</code> URI into an + * abstract pathname. + * @param uri An absolute, hierarchical URI with a scheme equal to "file", a non-empty path + * component, and undefined authority, query, and fragment components + * + * @see File#File(URI) + */ + public FileWrapper(URI uri) { + super(uri); + } + + public InputStream getContents() throws StreamException { + try { + return new FileInputStream(this); + } catch (FileNotFoundException e) { + throw new StreamException(e); + } + } + + public void setContents(InputStream source) throws StreamException { + FileOutputStream fos = null; + try { + fos = new FileOutputStream(this); + + byte[] buffer = new byte[1024]; + int count = 0; + while ((count = source.read(buffer)) != -1) { + fos.write(buffer, 0, count); + } + } catch (IOException e) { + throw new StreamException(e); + } finally { + if (fos != null) { + try { + fos.close(); + } catch (IOException e) { + throw new StreamException(e); + } + } + } + } + + public OutputStream getOutputStream() throws StreamException { + try { + return new FileOutputStream(this); + } catch (FileNotFoundException e) { + throw new StreamException(e); + } + } + + public PreferredWriteMode getPreferredWriteMode() { + return PreferredWriteMode.OUTPUTSTREAM; + } + + public String getOsLocation() { + return getAbsolutePath(); + } + + @Override + public boolean exists() { + return isFile(); + } + + public long getModificationStamp() { + return lastModified(); + } + + public IAbstractFolder getParentFolder() { + String p = this.getParent(); + if (p == null) { + return null; + } + return new FolderWrapper(p); + } +} diff --git a/common/src/com/android/io/FolderWrapper.java b/common/src/com/android/io/FolderWrapper.java new file mode 100644 index 0000000..26ed9cf --- /dev/null +++ b/common/src/com/android/io/FolderWrapper.java @@ -0,0 +1,154 @@ +/* + * 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. + */ + +package com.android.io; + + +import java.io.File; +import java.net.URI; +import java.util.ArrayList; + +/** + * An implementation of {@link IAbstractFolder} extending {@link File}. + */ +public class FolderWrapper extends File implements IAbstractFolder { + + private static final long serialVersionUID = 1L; + + /** + * Creates a new File instance from a parent abstract pathname and a child pathname string. + * @param parent the parent pathname + * @param child the child name + * + * @see File#File(File, String) + */ + public FolderWrapper(File parent, String child) { + super(parent, child); + } + + /** + * Creates a new File instance by converting the given pathname string into an abstract + * pathname. + * @param pathname the pathname + * + * @see File#File(String) + */ + public FolderWrapper(String pathname) { + super(pathname); + } + + /** + * Creates a new File instance from a parent abstract pathname and a child pathname string. + * @param parent the parent pathname + * @param child the child name + * + * @see File#File(String, String) + */ + public FolderWrapper(String parent, String child) { + super(parent, child); + } + + /** + * Creates a new File instance by converting the given <code>file:</code> URI into an + * abstract pathname. + * @param uri An absolute, hierarchical URI with a scheme equal to "file", a non-empty path + * component, and undefined authority, query, and fragment components + * + * @see File#File(URI) + */ + public FolderWrapper(URI uri) { + super(uri); + } + + /** + * Creates a new File instance matching a give {@link File} object. + * @param file the file to match + */ + public FolderWrapper(File file) { + super(file.getAbsolutePath()); + } + + public IAbstractResource[] listMembers() { + File[] files = listFiles(); + final int count = files == null ? 0 : files.length; + IAbstractResource[] afiles = new IAbstractResource[count]; + + if (files != null) { + for (int i = 0 ; i < count ; i++) { + File f = files[i]; + if (f.isFile()) { + afiles[i] = new FileWrapper(f); + } else if (f.isDirectory()) { + afiles[i] = new FolderWrapper(f); + } + } + } + + return afiles; + } + + public boolean hasFile(final String name) { + String[] match = list(new FilenameFilter() { + public boolean accept(IAbstractFolder dir, String filename) { + return name.equals(filename); + } + }); + + return match.length > 0; + } + + public IAbstractFile getFile(String name) { + return new FileWrapper(this, name); + } + + public IAbstractFolder getFolder(String name) { + return new FolderWrapper(this, name); + } + + public IAbstractFolder getParentFolder() { + String p = this.getParent(); + if (p == null) { + return null; + } + return new FolderWrapper(p); + } + + public String getOsLocation() { + return getAbsolutePath(); + } + + @Override + public boolean exists() { + return isDirectory(); + } + + public String[] list(FilenameFilter filter) { + File[] files = listFiles(); + if (files != null && files.length > 0) { + ArrayList<String> list = new ArrayList<String>(); + + for (File file : files) { + if (filter.accept(this, file.getName())) { + list.add(file.getName()); + } + } + + return list.toArray(new String[list.size()]); + } + + return new String[0]; + } +} diff --git a/common/src/com/android/io/IAbstractFile.java b/common/src/com/android/io/IAbstractFile.java new file mode 100644 index 0000000..6dfc8d8 --- /dev/null +++ b/common/src/com/android/io/IAbstractFile.java @@ -0,0 +1,58 @@ +/* + * 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. + */ + +package com.android.io; + +import java.io.InputStream; +import java.io.OutputStream; + +/** + * A file. + */ +public interface IAbstractFile extends IAbstractResource { + public static enum PreferredWriteMode { + INPUTSTREAM, OUTPUTSTREAM; + } + + /** + * Returns an {@link InputStream} object on the file content. + * @throws StreamException + */ + InputStream getContents() throws StreamException; + + /** + * Sets the content of the file. + * @param source the content + * @throws StreamException + */ + void setContents(InputStream source) throws StreamException; + + /** + * Returns an {@link OutputStream} to write into the file. + * @throws StreamException + */ + OutputStream getOutputStream() throws StreamException; + + /** + * Returns the preferred mode to write into the file. + */ + PreferredWriteMode getPreferredWriteMode(); + + /** + * Returns the last modification timestamp + */ + long getModificationStamp(); +} diff --git a/common/src/com/android/io/IAbstractFolder.java b/common/src/com/android/io/IAbstractFolder.java new file mode 100644 index 0000000..8335ef9 --- /dev/null +++ b/common/src/com/android/io/IAbstractFolder.java @@ -0,0 +1,77 @@ +/* + * 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. + */ + +package com.android.io; + +import java.io.File; + +/** + * A folder. + */ +public interface IAbstractFolder extends IAbstractResource { + /** + * Instances of classes that implement this interface are used to + * filter filenames. + */ + public interface FilenameFilter { + /** + * Tests if a specified file should be included in a file list. + * + * @param dir the directory in which the file was found. + * @param name the name of the file. + * @return <code>true</code> if and only if the name should be + * included in the file list; <code>false</code> otherwise. + */ + boolean accept(IAbstractFolder dir, String name); + } + + /** + * Returns true if the receiver contains a file with a given name + * @param name the name of the file. This is the name without the path leading to the + * parent folder. + */ + boolean hasFile(String name); + + /** + * Returns an {@link IAbstractFile} representing a child of the current folder with the + * given name. The file may not actually exist. + * @param name the name of the file. + */ + IAbstractFile getFile(String name); + + /** + * Returns an {@link IAbstractFolder} representing a child of the current folder with the + * given name. The folder may not actually exist. + * @param name the name of the folder. + */ + IAbstractFolder getFolder(String name); + + /** + * Returns a list of all existing file and directory members in this folder. + * The returned array can be empty but is never null. + */ + IAbstractResource[] listMembers(); + + /** + * Returns a list of all existing file and directory members in this folder + * that satisfy the specified filter. + * + * @param filter A filename filter instance. Must not be null. + * @return An array of file names (generated using {@link File#getName()}). + * The array can be empty but is never null. + */ + String[] list(FilenameFilter filter); +} diff --git a/common/src/com/android/io/IAbstractResource.java b/common/src/com/android/io/IAbstractResource.java new file mode 100644 index 0000000..3d762eb --- /dev/null +++ b/common/src/com/android/io/IAbstractResource.java @@ -0,0 +1,45 @@ +/* + * 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. + */ + +package com.android.io; + +/** + * Base representation of a file system resource.<p/> + * This somewhat limited interface is designed to let classes use file-system resources, without + * having the manually handle either the standard Java file or the Eclipse file API.. + */ +public interface IAbstractResource { + + /** + * Returns the name of the resource. + */ + String getName(); + + /** + * Returns the OS path of the folder location. + */ + String getOsLocation(); + + /** + * Returns whether the resource actually exists. + */ + boolean exists(); + + /** + * Returns the parent folder or null if there is no parent. + */ + IAbstractFolder getParentFolder(); +} diff --git a/common/src/com/android/io/StreamException.java b/common/src/com/android/io/StreamException.java new file mode 100644 index 0000000..f67c7a8 --- /dev/null +++ b/common/src/com/android/io/StreamException.java @@ -0,0 +1,28 @@ +/* + * 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 com.android.io; + +/** + * Exception thrown when {@link IAbstractFile#getContents()} fails. + */ +public class StreamException extends Exception { + private static final long serialVersionUID = 1L; + + public StreamException(Exception e) { + super(e); + } +} diff --git a/common/src/com/android/resources/FolderTypeRelationship.java b/common/src/com/android/resources/FolderTypeRelationship.java new file mode 100644 index 0000000..34961a3 --- /dev/null +++ b/common/src/com/android/resources/FolderTypeRelationship.java @@ -0,0 +1,164 @@ +/* + * 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.resources; + + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * This class gives access to the bidirectional relationship between {@link ResourceType} and + * {@link ResourceFolderType}. + */ +public final class FolderTypeRelationship { + + private final static Map<ResourceType, List<ResourceFolderType>> mTypeToFolderMap = + new HashMap<ResourceType, List<ResourceFolderType>>(); + + private final static Map<ResourceFolderType, List<ResourceType>> mFolderToTypeMap = + new HashMap<ResourceFolderType, List<ResourceType>>(); + + static { + // generate the relationships in a temporary map + add(ResourceType.ANIM, ResourceFolderType.ANIM); + add(ResourceType.ANIMATOR, ResourceFolderType.ANIMATOR); + add(ResourceType.ARRAY, ResourceFolderType.VALUES); + add(ResourceType.ATTR, ResourceFolderType.VALUES); + add(ResourceType.BOOL, ResourceFolderType.VALUES); + add(ResourceType.COLOR, ResourceFolderType.VALUES); + add(ResourceType.COLOR, ResourceFolderType.COLOR); + add(ResourceType.DECLARE_STYLEABLE, ResourceFolderType.VALUES); + add(ResourceType.DIMEN, ResourceFolderType.VALUES); + add(ResourceType.DRAWABLE, ResourceFolderType.VALUES); + add(ResourceType.DRAWABLE, ResourceFolderType.DRAWABLE); + add(ResourceType.FRACTION, ResourceFolderType.VALUES); + add(ResourceType.ID, ResourceFolderType.VALUES); + add(ResourceType.INTEGER, ResourceFolderType.VALUES); + add(ResourceType.INTERPOLATOR, ResourceFolderType.INTERPOLATOR); + add(ResourceType.LAYOUT, ResourceFolderType.LAYOUT); + add(ResourceType.MENU, ResourceFolderType.MENU); + add(ResourceType.MIPMAP, ResourceFolderType.MIPMAP); + add(ResourceType.PLURALS, ResourceFolderType.VALUES); + add(ResourceType.PUBLIC, ResourceFolderType.VALUES); + add(ResourceType.RAW, ResourceFolderType.RAW); + add(ResourceType.STRING, ResourceFolderType.VALUES); + add(ResourceType.STYLE, ResourceFolderType.VALUES); + add(ResourceType.STYLEABLE, ResourceFolderType.VALUES); + add(ResourceType.XML, ResourceFolderType.XML); + + makeSafe(); + } + + /** + * Returns a list of {@link ResourceType}s that can be generated from files inside a folder + * of the specified type. + * @param folderType The folder type. + * @return a list of {@link ResourceType}, possibly empty but never null. + */ + public static List<ResourceType> getRelatedResourceTypes(ResourceFolderType folderType) { + List<ResourceType> list = mFolderToTypeMap.get(folderType); + if (list != null) { + return list; + } + + return Collections.emptyList(); + } + + /** + * Returns a list of {@link ResourceFolderType} that can contain files generating resources + * of the specified type. + * @param resType the type of resource. + * @return a list of {@link ResourceFolderType}, possibly empty but never null. + */ + public static List<ResourceFolderType> getRelatedFolders(ResourceType resType) { + List<ResourceFolderType> list = mTypeToFolderMap.get(resType); + if (list != null) { + return list; + } + + return Collections.emptyList(); + } + + /** + * Returns true if the {@link ResourceType} and the {@link ResourceFolderType} values match. + * @param resType the resource type. + * @param folderType the folder type. + * @return true if files inside the folder of the specified {@link ResourceFolderType} + * could generate a resource of the specified {@link ResourceType} + */ + public static boolean match(ResourceType resType, ResourceFolderType folderType) { + List<ResourceFolderType> list = mTypeToFolderMap.get(resType); + + if (list != null) { + return list.contains(folderType); + } + + return false; + } + + /** + * Adds a {@link ResourceType} - {@link ResourceFolderType} relationship. this indicates that + * a file in the folder can generate a resource of the specified type. + * @param type The resourceType + * @param folder The {@link ResourceFolderType} + */ + private static void add(ResourceType type, ResourceFolderType folder) { + // first we add the folder to the list associated with the type. + List<ResourceFolderType> folderList = mTypeToFolderMap.get(type); + if (folderList == null) { + folderList = new ArrayList<ResourceFolderType>(); + mTypeToFolderMap.put(type, folderList); + } + if (folderList.indexOf(folder) == -1) { + folderList.add(folder); + } + + // now we add the type to the list associated with the folder. + List<ResourceType> typeList = mFolderToTypeMap.get(folder); + if (typeList == null) { + typeList = new ArrayList<ResourceType>(); + mFolderToTypeMap.put(folder, typeList); + } + if (typeList.indexOf(type) == -1) { + typeList.add(type); + } + } + + /** + * Makes the maps safe by replacing the current list values with unmodifiable lists. + */ + private static void makeSafe() { + for (ResourceType type : ResourceType.values()) { + List<ResourceFolderType> list = mTypeToFolderMap.get(type); + if (list != null) { + // replace with a unmodifiable list wrapper around the current list. + mTypeToFolderMap.put(type, Collections.unmodifiableList(list)); + } + } + + for (ResourceFolderType folder : ResourceFolderType.values()) { + List<ResourceType> list = mFolderToTypeMap.get(folder); + if (list != null) { + // replace with a unmodifiable list wrapper around the current list. + mFolderToTypeMap.put(folder, Collections.unmodifiableList(list)); + } + } + } +} diff --git a/common/src/com/android/resources/ResourceFolderType.java b/common/src/com/android/resources/ResourceFolderType.java new file mode 100644 index 0000000..3a5b64d --- /dev/null +++ b/common/src/com/android/resources/ResourceFolderType.java @@ -0,0 +1,78 @@ +/* + * 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.resources; + +import com.android.AndroidConstants; + +/** + * Enum representing a type of resource folder. + */ +public enum ResourceFolderType { + ANIM(AndroidConstants.FD_RES_ANIM), + ANIMATOR(AndroidConstants.FD_RES_ANIMATOR), + COLOR(AndroidConstants.FD_RES_COLOR), + DRAWABLE(AndroidConstants.FD_RES_DRAWABLE), + INTERPOLATOR(AndroidConstants.FD_RES_INTERPOLATOR), + LAYOUT(AndroidConstants.FD_RES_LAYOUT), + MENU(AndroidConstants.FD_RES_MENU), + MIPMAP(AndroidConstants.FD_RES_MIPMAP), + RAW(AndroidConstants.FD_RES_RAW), + VALUES(AndroidConstants.FD_RES_VALUES), + XML(AndroidConstants.FD_RES_XML); + + private final String mName; + + ResourceFolderType(String name) { + mName = name; + } + + /** + * Returns the folder name for this resource folder type. + */ + public String getName() { + return mName; + } + + /** + * Returns the enum by name. + * @param name The enum string value. + * @return the enum or null if not found. + */ + public static ResourceFolderType getTypeByName(String name) { + for (ResourceFolderType rType : values()) { + if (rType.mName.equals(name)) { + return rType; + } + } + return null; + } + + /** + * Returns the {@link ResourceFolderType} from the folder name + * @param folderName The name of the folder. This must be a valid folder name in the format + * <code>resType[-resqualifiers[-resqualifiers[...]]</code> + * @return the <code>ResourceFolderType</code> representing the type of the folder, or + * <code>null</code> if no matching type was found. + */ + public static ResourceFolderType getFolderType(String folderName) { + // split the name of the folder in segments. + String[] folderSegments = folderName.split(AndroidConstants.RES_QUALIFIER_SEP); + + // get the enum for the resource type. + return getTypeByName(folderSegments[0]); + } +} diff --git a/common/src/com/android/resources/ResourceType.java b/common/src/com/android/resources/ResourceType.java index a4d3aa2..e9d4d53 100644 --- a/common/src/com/android/resources/ResourceType.java +++ b/common/src/com/android/resources/ResourceType.java @@ -1,11 +1,11 @@ /* * Copyright (C) 2007 The Android Open Source Project * - * Licensed under the Eclipse Public License, Version 1.0 (the "License"); + * 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.eclipse.org/org/documents/epl-v10.php + * 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, diff --git a/common/tests/.classpath b/common/tests/.classpath new file mode 100644 index 0000000..b793adc --- /dev/null +++ b/common/tests/.classpath @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" path="src"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> + <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/3"/> + <classpathentry combineaccessrules="false" kind="src" path="/common"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/common/tests/.project b/common/tests/.project new file mode 100644 index 0000000..9f550a3 --- /dev/null +++ b/common/tests/.project @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>common-tests</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> diff --git a/common/tests/Android.mk b/common/tests/Android.mk new file mode 100644 index 0000000..10e75e8 --- /dev/null +++ b/common/tests/Android.mk @@ -0,0 +1,27 @@ +# Copyright (C) 2011 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. + +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +# Only compile source java files in this lib. +LOCAL_SRC_FILES := $(call all-java-files-under, src) + +LOCAL_MODULE := common-tests +LOCAL_MODULE_TAGS := optional + +LOCAL_JAVA_LIBRARIES := common junit + +include $(BUILD_HOST_JAVA_LIBRARY) diff --git a/common/tests/src/com/android/resources/FolderTypeRelationShipTest.java b/common/tests/src/com/android/resources/FolderTypeRelationShipTest.java new file mode 100644 index 0000000..809eae7 --- /dev/null +++ b/common/tests/src/com/android/resources/FolderTypeRelationShipTest.java @@ -0,0 +1,44 @@ +/* + * 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 com.android.resources; + +import com.android.resources.FolderTypeRelationship; +import com.android.resources.ResourceFolderType; +import com.android.resources.ResourceType; + +import junit.framework.TestCase; + +public class FolderTypeRelationShipTest extends TestCase { + + public void testResourceType() { + // all resource type should be in the FolderTypeRelationShip map. + // loop on all the enum, and make sure there's at least one folder type for it. + for (ResourceType type : ResourceType.values()) { + assertTrue(type.getDisplayName(), + FolderTypeRelationship.getRelatedFolders(type).size() > 0); + } + } + + public void testResourceFolderType() { + // all resource folder type should generate at least one type of resource. + // loop on all the enum, and make sure there's at least one res type for it. + for (ResourceFolderType type : ResourceFolderType.values()) { + assertTrue(type.getName(), + FolderTypeRelationship.getRelatedResourceTypes(type).size() > 0); + } + } +} |