diff --git a/app/src/main/java/fr/neamar/kiss/dataprovider/AppProvider.java b/app/src/main/java/fr/neamar/kiss/dataprovider/AppProvider.java index 7b3c3042a..eea50a827 100644 --- a/app/src/main/java/fr/neamar/kiss/dataprovider/AppProvider.java +++ b/app/src/main/java/fr/neamar/kiss/dataprovider/AppProvider.java @@ -29,7 +29,7 @@ public ArrayList getResults(String query) { int totalWordStarts; ArrayList> matchPositions; - for (Pojo pojo : pojos) { + for (AppPojo pojo : pojos) { pojo.displayName = pojo.name; pojo.displayTags = pojo.tags; relevance = 0; @@ -98,12 +98,17 @@ else if (match) { relevance *= (0.2 + 0.8 * (1.0 / matchPositions.size())); } else { - if (pojo.tags.contains(query)) { + if (pojo.tagsNormalized.startsWith(query)) { relevance = 4 + query.length(); + } + else if (pojo.tagsNormalized.indexOf(query) >= 0) { + relevance = 3 + query.length(); + } + if (relevance > 0) { matchedTags = true; - tagStart = pojo.tags.indexOf(query); - tagEnd = tagStart + query.length(); } + tagStart = pojo.tagsNormalized.indexOf(query); + tagEnd = tagStart + query.length(); } if (relevance > 0) { @@ -134,7 +139,10 @@ public Pojo findById(String id, Boolean allowSideEffect) { // Reset displayName to default value if (allowSideEffect) { pojo.displayName = pojo.name; - pojo.displayTags = pojo.tags; + if (pojo instanceof AppPojo) { + AppPojo appPojo = (AppPojo)pojo; + appPojo.displayTags = appPojo.tags; + } } return pojo; } @@ -160,7 +168,7 @@ public ArrayList getAllApps() { ArrayList records = new ArrayList<>(pojos.size()); records.trimToSize(); - for (Pojo pojo : pojos) { + for (AppPojo pojo : pojos) { pojo.displayName = pojo.name; pojo.displayTags = pojo.tags; records.add(pojo); diff --git a/app/src/main/java/fr/neamar/kiss/loader/LoadAppPojos.java b/app/src/main/java/fr/neamar/kiss/loader/LoadAppPojos.java index 637c5051e..4b512ea21 100644 --- a/app/src/main/java/fr/neamar/kiss/loader/LoadAppPojos.java +++ b/app/src/main/java/fr/neamar/kiss/loader/LoadAppPojos.java @@ -61,7 +61,7 @@ protected ArrayList doInBackground(Void... params) { app.packageName = info.activityInfo.applicationInfo.packageName; app.activityName = info.activityInfo.name; - app.tags = tagsHandler.getTags(app.id); + app.setTags(tagsHandler.getTags(app.id)); apps.add(app); } } diff --git a/app/src/main/java/fr/neamar/kiss/pojo/AppPojo.java b/app/src/main/java/fr/neamar/kiss/pojo/AppPojo.java index 3557c971a..55223192d 100644 --- a/app/src/main/java/fr/neamar/kiss/pojo/AppPojo.java +++ b/app/src/main/java/fr/neamar/kiss/pojo/AppPojo.java @@ -1,6 +1,64 @@ package fr.neamar.kiss.pojo; +import android.util.Pair; + +import fr.neamar.kiss.normalizer.StringNormalizer; + public class AppPojo extends Pojo { public String packageName; public String activityName; + + // Tags assigned to this pojo + public String tags; + // Tags normalized + public String tagsNormalized; + // Array that contains the non-normalized positions for every normalized + // character entry + private int[] tagsPositionMap = null; + // Variable to store the formated (user selection in bold) tag + public String displayTags = ""; + + + public void setTags(String tags) { + // Set the actual user-friendly name + this.tags = tags; + + if (this.tags != null) { + this.tags = this.tags.replaceAll("<", "<"); + // Normalize name for faster searching + Pair normalized = StringNormalizer.normalizeWithMap(this.tags); + this.tagsNormalized = normalized.first; + this.tagsPositionMap = normalized.second; + } + } + + public void setTagHighlight(int positionStart, int positionEnd) { + int posStart = this.mapTagsPosition(positionStart); + int posEnd = this.mapTagsPosition(positionEnd); + + this.displayTags = this.tags.substring(0, posStart) + + '{' + this.tags.substring(posStart, posEnd) + '}' + this.tags.substring(posEnd, this.tags.length()); + } + + /** + * Map a position in the normalized name to a position in the standard name string + * + * @param position Position in normalized name + * @return Position in non-normalized string + */ + public int mapTagsPosition(int position) { + if (this.tagsPositionMap != null) { + if (position < this.tagsPositionMap.length) { + return this.tagsPositionMap[position]; + } + return this.tags.length(); + } else { + // No mapping defined + if (position < this.tags.length()) { + return position; + } + return this.tags.length(); + } + } + } diff --git a/app/src/main/java/fr/neamar/kiss/pojo/Pojo.java b/app/src/main/java/fr/neamar/kiss/pojo/Pojo.java index 6c37ee43b..5b58205ad 100644 --- a/app/src/main/java/fr/neamar/kiss/pojo/Pojo.java +++ b/app/src/main/java/fr/neamar/kiss/pojo/Pojo.java @@ -25,10 +25,6 @@ public abstract class Pojo { // Array that contains the non-normalized positions for every normalized // character entry private int[] namePositionMap = null; - // Tags assigned to this pojo - public String tags; - // Variable to store the formated (user selection in bold) tag - public String displayTags = ""; /** * Map a position in the normalized name to a position in the standard name string @@ -108,9 +104,4 @@ public void setDisplayNameHighlightRegion(List> positions this.displayName += this.name.substring(lastPositionEnd); } - public void setTagHighlight(int positionStart, int positionEnd) { - this.displayTags = this.tags.substring(0, positionStart) - + '{' + this.tags.substring(positionStart, positionEnd) + '}' - + this.tags.substring(positionEnd); - } } diff --git a/app/src/main/java/fr/neamar/kiss/result/AppResult.java b/app/src/main/java/fr/neamar/kiss/result/AppResult.java index 7f3042348..b02192af3 100644 --- a/app/src/main/java/fr/neamar/kiss/result/AppResult.java +++ b/app/src/main/java/fr/neamar/kiss/result/AppResult.java @@ -56,7 +56,9 @@ public View display(final Context context, int position, View v) { appName.setText(enrichText(appPojo.displayName)); TextView tagsView = (TextView) v.findViewById(R.id.item_app_tag); - if (appPojo.displayTags.isEmpty()) { + //Hide tags view if tags are empty or if user has selected to hide them and the query doesnt match tags + if (appPojo.displayTags.isEmpty() || + ((!PreferenceManager.getDefaultSharedPreferences(context).getBoolean("tags-visible", true)) && (appPojo.displayTags.equals(appPojo.tags)))) { tagsView.setVisibility(View.GONE); } else { @@ -132,7 +134,7 @@ protected Boolean popupMenuClickHandler(Context context, RecordAdapter parent, M excludeFromAppList(context, appPojo); return true; case R.id.item_tags_edit: - launchEditTagsDialog(context, pojo); + launchEditTagsDialog(context, appPojo); break; } @@ -149,7 +151,7 @@ private void excludeFromAppList(Context context, AppPojo appPojo) { } - private void launchEditTagsDialog(final Context context, final Pojo app) { + private void launchEditTagsDialog(final Context context, final AppPojo app) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle(context.getResources().getString(R.string.tags_add_title)); @@ -160,7 +162,7 @@ private void launchEditTagsDialog(final Context context, final Pojo app) { ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_dropdown_item_1line, KissApplication.getDataHandler(context).getTagsHandler().getAllTagsAsArray()); tagInput.setTokenizer(new SpaceTokenizer()); - tagInput.setText(app.tags); + tagInput.setText(appPojo.tags); tagInput.setAdapter(adapter); builder.setView(v); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 62e6e3eb6..447e6f760 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -117,6 +117,7 @@ Add / edit tags Add space separated tags Tag was set + Show application tags Excluded apps list erased. App excluded from KISS. Reset through the KISS settings diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 1d72a0215..71f07f43d 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -95,6 +95,10 @@ android:defaultValue="true" android:key="force-portrait" android:title="@string/portrait_title" /> +