Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mipmap support #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<idea-plugin version="2" url="https://github.com/inmite/android-selector-chapek">
<id>eu.inmite.android.plugin.selectorchapek</id>
<name>SelectorChapek for Android</name>
<version>1.0.2</version>
<version>1.0.5</version>
<vendor email="[email protected]" url="http://www.inmite.eu">Inmite</vendor>

<description><![CDATA[
Expand All @@ -10,6 +10,20 @@

<change-notes><![CDATA[
<html>
<b>1.0.5</b> (03/14/2016)
<ul>
<li>add mipmap support
</ul>
<b>1.0.4</b> (02/08/20135
<ul>
<li>解决生成的文件有两个_selector的问题
</ul>
<b>1.0.3</b> (01/25/2014)
<ul>
<li>生成的文件添加_selector
<li>解决使用xml作为Drawable时生成的文件中包含多余的.xml
</ul>

<b>1.0.2</b> (09/03/2013)
<ul>
<li>Support for all drawable directories
Expand Down
8 changes: 5 additions & 3 deletions src/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Pattern;

Expand All @@ -33,11 +34,12 @@ public class Constants {
public static final String ACTIVATED = "_activated";
public static final String WINDOW_FOCUSED = "_windowfocused";
public static String[] SUFFIXES = new String[]{NORMAL, PRESSED, FOCUSED, SELECTED, CHECKED, DISABLED, HOVERED, CHECKABLE, ACTIVATED, WINDOW_FOCUSED};
public static Pattern VALID_FOLDER_PATTERN = Pattern.compile("^drawable(-[a-zA-Z0-9]+)*$");
public static String EXPORT_FOLDER = "drawable";
public static HashMap<String, State> sMapping;

public static ArrayList<ValidFolder> VALID_FOLDERS=new ArrayList<>();
static {

VALID_FOLDERS.add(new ValidFolder(Pattern.compile("^drawable(-[a-zA-Z0-9]+)*$"),"drawable"));
VALID_FOLDERS.add(new ValidFolder(Pattern.compile("^mipmap(-[a-zA-Z0-9]+)*$"),"mipmap"));
// mapping from file suffixes into android attributes and their default values
sMapping = new HashMap<String, State>();
sMapping.put(FOCUSED, new State("state_focused", false));
Expand Down
7 changes: 4 additions & 3 deletions src/SelectorChapekAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ public void update(AnActionEvent e) {

private boolean isCorrectFolderSelected(VirtualFile selectedFile) {
if (selectedFile != null && selectedFile.isDirectory()) {
Matcher matcher = Constants.VALID_FOLDER_PATTERN.matcher(selectedFile.getName());
if (matcher.matches()) {
return true;
for(ValidFolder validFolder:Constants.VALID_FOLDERS){
if(validFolder.VALID_PATTERN.matcher(selectedFile.getName()).matches()){
return true;
}
}
}
return false;
Expand Down
41 changes: 25 additions & 16 deletions src/SelectorDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public SelectorDetector(Project project) {

public void detectAndCreateSelectors(VirtualFile selectedFolder) {
VirtualFile[] children = selectedFolder.getChildren();
for (VirtualFile child : children) {
for(int i=0;i<children.length;i++){
VirtualFile child = children[i];
Log.d("processing " + child.getName());
List<String> suffixes = detectSuffixes(child);
Log.d("suffixes: " + suffixes);
Expand Down Expand Up @@ -82,27 +83,35 @@ private void createFile(final VirtualFile file, final VirtualFile folder, List<S
return;
}
mCreatedFiles.add(fileName);

RunnableHelper.runWriteCommand(mProject, new Runnable() {
@Override
public void run() {
try {
VirtualFile drawableFolder = folder.getParent().findChild(Constants.EXPORT_FOLDER);
if (drawableFolder == null || !drawableFolder.exists()) {
drawableFolder = folder.getParent().createChildDirectory(null, Constants.EXPORT_FOLDER);
Log.d("drawable folder created");
for(ValidFolder validFolder:Constants.VALID_FOLDERS) {
if(!validFolder.VALID_PATTERN.matcher(file.getParent().getName()).matches()){
continue;
}
Log.d("trying to create: " + drawableFolder + " " + fileName);
VirtualFile newFile = drawableFolder.findChild(fileName);
if (newFile == null || !newFile.exists()) {
newFile = drawableFolder.createChildData(null, fileName);
Log.d("file created: " + fileName);
SelectorGenerator.generate(newFile, detectStates(file, folder));
} else {
Log.d("skipping, file already exists:" + fileName);
String exportFolder = validFolder.EXPORT_FOLDER;
VirtualFile sourcesFolder = folder.getParent().findChild("drawable");
try {
if (sourcesFolder == null || !sourcesFolder.exists()) {
sourcesFolder = folder.getParent().createChildDirectory(null, "drawable");
Log.d("drawable folder created");
}
Log.d("trying to create: " + sourcesFolder + " " + fileName);
VirtualFile newFile = sourcesFolder.findChild(fileName);
if (newFile == null || !newFile.exists()) {
newFile = sourcesFolder.createChildData(null, fileName);
Log.d("file created: " + fileName);
SelectorGenerator.generate(newFile,exportFolder, detectStates(file, folder));
} else {
Log.d("skipping, file already exists:" + fileName);
}
} catch (IOException e) {
Log.e(e);
}
} catch (IOException e) {
Log.e(e);
}

}
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/SelectorGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class SelectorGenerator {
private static final String SCHEMA = "http://schemas.android.com/apk/res/android";
private static final String NS = "android";

public static void generate(VirtualFile newFile, List<SelectorDetector.Result> detectorResults) {
public static void generate(VirtualFile newFile,String sourceTypeName, List<SelectorDetector.Result> detectorResults) {
Log.d("generating XML:");
Element root = new Element("selector");
root.addNamespaceDeclaration(NS, SCHEMA);
Expand All @@ -51,7 +51,7 @@ public static void generate(VirtualFile newFile, List<SelectorDetector.Result> d
for (SelectorDetector.Result result : detectorResults) {
Log.d("fileName=" + result.drawableName + ", states:" + result.states);
Element item = new Element("item");
Attribute attribute = new Attribute("drawable", "@drawable/" + result.drawableName);
Attribute attribute = new Attribute("drawable", "@"+sourceTypeName+"/" + result.drawableName);
attribute.setNamespace(NS, SCHEMA);
item.addAttribute(attribute);
for (String state : allStatesWithoutNormal) {
Expand Down
14 changes: 14 additions & 0 deletions src/ValidFolder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import java.util.regex.Pattern;

/**
* Created by apple on 16/3/14.
*/
public class ValidFolder {
public final Pattern VALID_PATTERN;
public final String EXPORT_FOLDER;

public ValidFolder(Pattern valid_pattern, String export_folder) {
VALID_PATTERN = valid_pattern;
EXPORT_FOLDER = export_folder;
}
}