Skip to content

Commit

Permalink
Merge pull request #590 from nmitsou/fixBugsOnTags
Browse files Browse the repository at this point in the history
fixing search on tags
  • Loading branch information
Neamar authored Feb 16, 2017
2 parents d4ba1d5 + 2a6a5c4 commit ac5fbcd
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 20 deletions.
20 changes: 14 additions & 6 deletions app/src/main/java/fr/neamar/kiss/dataprovider/AppProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public ArrayList<Pojo> getResults(String query) {
int totalWordStarts;
ArrayList<Pair<Integer, Integer>> matchPositions;

for (Pojo pojo : pojos) {
for (AppPojo pojo : pojos) {
pojo.displayName = pojo.name;
pojo.displayTags = pojo.tags;
relevance = 0;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -160,7 +168,7 @@ public ArrayList<Pojo> getAllApps() {
ArrayList<Pojo> 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);
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/fr/neamar/kiss/loader/LoadAppPojos.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected ArrayList<AppPojo> 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);
}
}
Expand Down
58 changes: 58 additions & 0 deletions app/src/main/java/fr/neamar/kiss/pojo/AppPojo.java
Original file line number Diff line number Diff line change
@@ -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("<", "&lt;");
// Normalize name for faster searching
Pair<String, int[]> 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();
}
}

}
9 changes: 0 additions & 9 deletions app/src/main/java/fr/neamar/kiss/pojo/Pojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -108,9 +104,4 @@ public void setDisplayNameHighlightRegion(List<Pair<Integer, Integer>> 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);
}
}
10 changes: 6 additions & 4 deletions app/src/main/java/fr/neamar/kiss/result/AppResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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));

Expand All @@ -160,7 +162,7 @@ private void launchEditTagsDialog(final Context context, final Pojo app) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(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);
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
<string name="menu_tags_edit">Add / edit tags</string>
<string name="tags_add_title">Add space separated tags</string>
<string name="tags_confirmation_added">Tag was set</string>
<string name="tags_visible">Show application tags</string>
<string name="excluded_app_list_erased">Excluded apps list erased.</string>
<string name="excluded_app_list_added">App excluded from KISS. Reset through the KISS settings</string>

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@
android:defaultValue="true"
android:key="force-portrait"
android:title="@string/portrait_title" />
<fr.neamar.kiss.SwitchPreference
android:defaultValue="true"
android:key="tags-visible"
android:title="@string/tags_visible" />
<fr.neamar.kiss.SwitchPreference
android:defaultValue="false"
android:key="icons-hide"
Expand Down

0 comments on commit ac5fbcd

Please sign in to comment.