diff --git a/app/build.gradle b/app/build.gradle index 62ea5d4733d3db3d5b5fc61a07c7ef108a0e5a17..b525311c3a00a501f956d3b82565f52758d04f47 100755 --- a/app/build.gradle +++ b/app/build.gradle @@ -11,7 +11,7 @@ android { minSdkVersion 9 targetSdkVersion 23 - versionCode 170 + versionCode 172 versionName "6.5" } @@ -35,7 +35,7 @@ android { } dependencies { - compile 'com.android.support:appcompat-v7:23.4.0' + compile 'com.android.support:appcompat-v7:25.4.0' } lintOptions { @@ -56,5 +56,5 @@ android { } } dependencies { - compile 'com.squareup.okhttp3:okhttp:3.9.1' + compile 'com.squareup.okhttp3:okhttp:3.10.0' } \ No newline at end of file diff --git a/app/src/main/java/ohi/andre/consolelauncher/MainManager.java b/app/src/main/java/ohi/andre/consolelauncher/MainManager.java index b51ee1b2c292a771875754b68dba7c3f6b3f360c..cad1ac986277ae559354ddcbc986de1bdb38c7dc 100755 --- a/app/src/main/java/ohi/andre/consolelauncher/MainManager.java +++ b/app/src/main/java/ohi/andre/consolelauncher/MainManager.java @@ -26,6 +26,7 @@ import ohi.andre.consolelauncher.managers.AppsManager; import ohi.andre.consolelauncher.managers.ContactManager; import ohi.andre.consolelauncher.managers.RssManager; import ohi.andre.consolelauncher.managers.TerminalManager; +import ohi.andre.consolelauncher.managers.ThemeManager; import ohi.andre.consolelauncher.managers.TimeManager; import ohi.andre.consolelauncher.managers.music.MusicManager2; import ohi.andre.consolelauncher.managers.music.MusicService; @@ -120,6 +121,7 @@ public class MainManager { private AppsManager appsManager; private ContactManager contactManager; private MusicManager2 musicManager2; + private ThemeManager themeManager; private BroadcastReceiver receiver; @@ -158,6 +160,8 @@ public class MainManager { rssManager = new RssManager(mContext, client); + themeManager = new ThemeManager(client, mContext, c); + musicManager2 = XMLPrefsManager.getBoolean(Behavior.enable_music) ? new MusicManager2(mContext) : null; mainPack = new MainPack(mContext, group, aliasManager, appsManager, musicManager2, contactManager, redirectator, shellHolder, rssManager, client); @@ -290,6 +294,7 @@ public class MainManager { public void destroy() { mainPack.destroy(); + themeManager.dispose(); LocalBroadcastManager.getInstance(mContext.getApplicationContext()).unregisterReceiver(receiver); new StoppableThread() { diff --git a/app/src/main/java/ohi/andre/consolelauncher/commands/CommandTuils.java b/app/src/main/java/ohi/andre/consolelauncher/commands/CommandTuils.java index fef452e3eb41987f70d9e16338a01ac9abb485c1..c19df69eea8bd536ff0e18901b81c606959094da 100755 --- a/app/src/main/java/ohi/andre/consolelauncher/commands/CommandTuils.java +++ b/app/src/main/java/ohi/andre/consolelauncher/commands/CommandTuils.java @@ -441,7 +441,7 @@ public class CommandTuils { xmlPrefsEntrys = new ArrayList<>(); for(XMLPrefsManager.XMLPrefsRoot element : XMLPrefsManager.XMLPrefsRoot.values()) { - xmlPrefsEntrys.addAll(element.copy); + xmlPrefsEntrys.addAll(element.enums); } Collections.addAll(xmlPrefsEntrys, Apps.values()); Collections.addAll(xmlPrefsEntrys, Notifications.values()); diff --git a/app/src/main/java/ohi/andre/consolelauncher/commands/main/raw/config.java b/app/src/main/java/ohi/andre/consolelauncher/commands/main/raw/config.java index 284c4d4e0ecd0ae4bddc8b02ed7bcc1c6ae335c5..bb1c1d71b0bdf5a4ff107c91301baa1dcabb7f92 100755 --- a/app/src/main/java/ohi/andre/consolelauncher/commands/main/raw/config.java +++ b/app/src/main/java/ohi/andre/consolelauncher/commands/main/raw/config.java @@ -166,7 +166,7 @@ public class config extends ParamCommand { for(XMLPrefsManager.XMLPrefsRoot element : XMLPrefsManager.XMLPrefsRoot.values()) { ss.add(element.path); - for(XMLPrefsSave save : element.copy) { + for(XMLPrefsSave save : element.enums) { ss.add(Tuils.DOUBLE_SPACE + save.label()); } } @@ -223,8 +223,8 @@ public class config extends ParamCommand { } } } else { - File old = new File(Tuils.getFolder(), file.getName()); - Tuils.insertOld(old); + File toPutInsideOld = new File(Tuils.getFolder(), file.getName()); + Tuils.insertOld(toPutInsideOld); } File dest = new File(Tuils.getFolder(), file.getName()); diff --git a/app/src/main/java/ohi/andre/consolelauncher/commands/main/raw/theme.java b/app/src/main/java/ohi/andre/consolelauncher/commands/main/raw/theme.java index efb70c1a319b6ada03928c35b9ae30525aee0772..9ca5e95346c2a0a030669b5936af7a03320cc29d 100644 --- a/app/src/main/java/ohi/andre/consolelauncher/commands/main/raw/theme.java +++ b/app/src/main/java/ohi/andre/consolelauncher/commands/main/raw/theme.java @@ -1,10 +1,14 @@ package ohi.andre.consolelauncher.commands.main.raw; +import android.content.Intent; +import android.support.v4.content.LocalBroadcastManager; + import ohi.andre.consolelauncher.R; import ohi.andre.consolelauncher.commands.CommandAbstraction; import ohi.andre.consolelauncher.commands.ExecutePack; import ohi.andre.consolelauncher.commands.main.MainPack; import ohi.andre.consolelauncher.commands.specific.ParamCommand; +import ohi.andre.consolelauncher.managers.ThemeManager; import ohi.andre.consolelauncher.tuils.Tuils; /** @@ -23,47 +27,43 @@ public class theme extends ParamCommand { @Override public String exec(ExecutePack pack) { - return "themes are not working at the moment, the new website will be ready soon"; + Intent intent = new Intent(ThemeManager.ACTION_APPLY); + intent.putExtra(ThemeManager.NAME, pack.getString()); + LocalBroadcastManager.getInstance(pack.context.getApplicationContext()).sendBroadcast(intent); + return null; } }, - view { + standard { + @Override + public int[] args() { + return new int[] {}; + } + @Override public String exec(ExecutePack pack) { - return "themes are not working at the moment, the new website will be ready soon"; + LocalBroadcastManager.getInstance(pack.context.getApplicationContext()).sendBroadcast(new Intent(ThemeManager.ACTION_STANDARD)); + return null; } }, - create { + view { @Override public String exec(ExecutePack pack) { - return "themes are not working at the moment, the new website will be ready soon"; + pack.context.startActivity(Tuils.webPage("https://tui.tarunshankerpandey.com")); + return null; } }, - ls { + create { @Override public String exec(ExecutePack pack) { - return "themes are not working at the moment, the new website will be ready soon"; + pack.context.startActivity(Tuils.webPage("https://tui.tarunshankerpandey.com/create.php")); + return null; } }, old { @Override public String exec(ExecutePack pack) { -// File theme = Tuils.getOld("theme.xml"); -// File suggestions = Tuils.getOld("suggestions.xml"); -// -// if(theme == null || suggestions == null) return pack.context.getString(R.string.theme_old_not_found); -// -// File themeDest = new File(Tuils.getFolder(), "theme.xml"); -// File suggestionsDest = new File(Tuils.getFolder(), "suggestions.xml"); -// -// if(themeDest.exists()) themeDest.delete(); -// if(suggestionsDest.exists()) suggestionsDest.delete(); -// -// theme.renameTo(themeDest); -// suggestions.renameTo(suggestionsDest); -// -// return pack.context.getString(R.string.theme_reverted); - - return "themes are not working at the moment, the new website will be ready soon"; + LocalBroadcastManager.getInstance(pack.context.getApplicationContext()).sendBroadcast(new Intent(ThemeManager.ACTION_REVERT)); + return null; } }, tutorial { diff --git a/app/src/main/java/ohi/andre/consolelauncher/managers/AliasManager.java b/app/src/main/java/ohi/andre/consolelauncher/managers/AliasManager.java index 27dbdcc78107065136b194ec4da30cf8c64bc5c5..c6c1a0d0ddb401c67666c4de05da2f539ff2c406 100755 --- a/app/src/main/java/ohi/andre/consolelauncher/managers/AliasManager.java +++ b/app/src/main/java/ohi/andre/consolelauncher/managers/AliasManager.java @@ -80,17 +80,23 @@ public class AliasManager { } } +// this prevents some erros related to the % sign + private final String SECURITY_REPLACEMENT = "{#@"; + private Pattern securityPattern = Pattern.compile(Pattern.quote(SECURITY_REPLACEMENT)); + public String format(String aliasValue, String params) { params = params.trim(); + aliasValue = parameterPattern.matcher(aliasValue).replaceAll(SECURITY_REPLACEMENT); + if(params.length() == 0) return aliasValue; String[] split = params.split(paramSeparator); for(String s : split) { - aliasValue = parameterPattern.matcher(aliasValue).replaceFirst(s); + aliasValue = securityPattern.matcher(aliasValue).replaceFirst(s); } - if(replaceAllMarkers) aliasValue = parameterPattern.matcher(aliasValue).replaceAll(split[0]); + if(replaceAllMarkers) aliasValue = securityPattern.matcher(aliasValue).replaceAll(split[0]); return aliasValue; } diff --git a/app/src/main/java/ohi/andre/consolelauncher/managers/NotesManager.java b/app/src/main/java/ohi/andre/consolelauncher/managers/NotesManager.java index 844bfb6724baafbbfa50159d85d5908f158184c1..5965e82a5ea7f464c502a2885821041351774cb0 100644 --- a/app/src/main/java/ohi/andre/consolelauncher/managers/NotesManager.java +++ b/app/src/main/java/ohi/andre/consolelauncher/managers/NotesManager.java @@ -133,11 +133,16 @@ public class NotesManager { filter.addAction(ACTION_LOCK); receiver = new BroadcastReceiver() { + + long last = -1; + @Override public void onReceive(Context context, Intent intent) { - if(intent.getAction().equals(ACTION_ADD)) { - Tuils.log("add note"); + long t = System.currentTimeMillis(); + if(t - last < 100) return; + last = t; + if(intent.getAction().equals(ACTION_ADD)) { String text = intent.getStringExtra(TEXT); if(text == null) return; @@ -249,7 +254,7 @@ public class NotesManager { Pattern countPattern = Pattern.compile("%c", Pattern.CASE_INSENSITIVE); Pattern lockPattern = Pattern.compile("%l", Pattern.CASE_INSENSITIVE); Pattern rowPattern = Pattern.compile("%r", Pattern.CASE_INSENSITIVE); - Pattern uriPattern = Pattern.compile("([^#\\s]+:[^\\s]+|www\\.[^\\s]*)"); + Pattern uriPattern = Pattern.compile("(http[s]?:[^\\s]+|www\\.[^\\s]*)\\.[a-z]+"); private void invalidateNotes() { String header = this.header; @@ -356,8 +361,6 @@ public class NotesManager { } private void addNote(Context context, String s, boolean lock) { - Tuils.log("called"); - long t = System.currentTimeMillis(); notes.add(new Note(t, s, lock)); @@ -371,7 +374,7 @@ public class NotesManager { String output = XMLPrefsManager.add(file, NOTE_NODE, new String[] {CREATION_TIME, VALUE_ATTRIBUTE, LOCK}, new String[] {String.valueOf(t), s, String.valueOf(lock)}); if(output != null) { if(output.length() > 0) Tuils.sendOutput(Color.RED, context, output); - else Tuils.sendOutput(Color.RED, context, R.string.note_not_found); + else Tuils.sendOutput(Color.RED, context, R.string.output_error); } invalidateNotes(); diff --git a/app/src/main/java/ohi/andre/consolelauncher/managers/RssManager.java b/app/src/main/java/ohi/andre/consolelauncher/managers/RssManager.java index 9b0b672d4930b3f8325d6b758ad7614cb5e849a2..b552333c8ed5d21de73bdc0011e5c072ce295809 100644 --- a/app/src/main/java/ohi/andre/consolelauncher/managers/RssManager.java +++ b/app/src/main/java/ohi/andre/consolelauncher/managers/RssManager.java @@ -628,7 +628,7 @@ public class RssManager implements XMLPrefsElement { super.run(); if(!Tuils.hasInternetAccess()) { - if(force) Tuils.sendOutput(Color.RED, context, "No internet access"); + if(force) Tuils.sendOutput(Color.RED, context, R.string.no_internet); return; } @@ -794,6 +794,8 @@ public class RssManager implements XMLPrefsElement { CharSequence s = Tuils.span(cp, feed.color); + String dateTag = feed.dateTag == null ? PUBDATE_CHILD : feed.dateTag; + Matcher m = formatPattern.matcher(cp); while(m.find()) { if(m.groupCount() == 3) { @@ -811,7 +813,7 @@ public class RssManager implements XMLPrefsElement { if(value != null) value = value.trim(); else value = Tuils.EMPTYSTRING; - if(feed.dateTag == null ? tag.equals(PUBDATE_CHILD) : (tag.equals(PUBDATE_CHILD) || tag.equals(feed.dateTag))) { + if(tag.equals(dateTag)) { Date d; try { d = feed.timeFormat != null ? feed.timeFormat.parse(value) : defaultRSSDateFormat.parse(value); diff --git a/app/src/main/java/ohi/andre/consolelauncher/managers/ThemeManager.java b/app/src/main/java/ohi/andre/consolelauncher/managers/ThemeManager.java index 091bbbb9bd2e336bf15bfab26ae3cc4579d00e5c..5f1c2e631b61d8f1b4e5809c21ce4d358efdd246 100644 --- a/app/src/main/java/ohi/andre/consolelauncher/managers/ThemeManager.java +++ b/app/src/main/java/ohi/andre/consolelauncher/managers/ThemeManager.java @@ -1,6 +1,26 @@ package ohi.andre.consolelauncher.managers; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.graphics.Color; +import android.support.v4.content.LocalBroadcastManager; + import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import ohi.andre.consolelauncher.BuildConfig; +import ohi.andre.consolelauncher.R; +import ohi.andre.consolelauncher.managers.xml.XMLPrefsManager; +import ohi.andre.consolelauncher.tuils.Tuils; +import ohi.andre.consolelauncher.tuils.interfaces.Reloadable; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; /** * Created by francescoandreuzzi on 17/02/2018. @@ -8,11 +28,206 @@ import java.io.File; public class ThemeManager { - public static void apply(String themeName) { + public static String ACTION_APPLY = BuildConfig.APPLICATION_ID + ".apply", ACTION_REVERT = BuildConfig.APPLICATION_ID + ".revert", ACTION_STANDARD = BuildConfig.APPLICATION_ID + ".standard", NAME = "name"; + + OkHttpClient client; + Context context; + Reloadable reloadable; + + Pattern parser = Pattern.compile("(<SUGGESTIONS>.+<\\/SUGGESTIONS>).*(<THEME>.+<\\/THEME>)", Pattern.DOTALL); + + BroadcastReceiver receiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + if(intent.getAction().equals(ACTION_APPLY)) { + String name = intent.getStringExtra(NAME); + if(name == null) return; + +// name needs to be the absolute path + if(name.endsWith(".zip")) apply(new File(name)); + else apply(name); + } else if(intent.getAction().equals(ACTION_REVERT)) { + revert(); + } else if(intent.getAction().equals(ACTION_STANDARD)) { + standard(); + } + } + }; + public ThemeManager(OkHttpClient client, Context context, Reloadable reloadable) { + this.client = client; + this.context = context; + this.reloadable = reloadable; + + IntentFilter filter = new IntentFilter(); + filter.addAction(ACTION_APPLY); + filter.addAction(ACTION_REVERT); + filter.addAction(ACTION_STANDARD); + + LocalBroadcastManager.getInstance(context.getApplicationContext()).registerReceiver(receiver, filter); + } + + public void dispose() { + LocalBroadcastManager.getInstance(context.getApplicationContext()).unregisterReceiver(receiver); } - public static void apply(File zip) { + public void apply(final String themeName) { + new Thread() { + @Override + public void run() { + super.run(); + + if(!Tuils.hasInternetAccess()) { + Tuils.sendOutput(Color.RED, context, R.string.no_internet); + return; + } + + String url = "https://tui.tarunshankerpandey.com/show_data.php?data_type=xml&theme_id=" + themeName; + + Request.Builder builder = new Request.Builder() + .url(url) + .get(); + + Response response; + try { + response = client.newCall(builder.build()).execute(); + } catch (IOException e) { + Tuils.sendOutput(context, R.string.output_error); + return; + } + + if(response.isSuccessful()) { + String string; + try { + string = response.body().string(); + } catch (IOException e) { + string = Tuils.EMPTYSTRING; + } + + if(string.length() == 0) { + Tuils.sendOutput(context, R.string.theme_not_found); + return; + } + + Matcher m = parser.matcher(string); + if(m.find()) { + String suggestions = m.group(1); + String theme = m.group(2); + + applyTheme(theme, suggestions, true); + } else { + Tuils.sendOutput(context, R.string.theme_not_found); + return; + } + } + } + }.start(); + } + + public void apply(File zip) { } + + private void applyTheme(File theme, File suggestions, boolean keepOld) { + if(theme == null || suggestions == null) { + Tuils.sendOutput(context, R.string.theme_unable); + return; + } + + File oldTheme = new File(Tuils.getFolder(), XMLPrefsManager.XMLPrefsRoot.THEME.path); + File oldSuggestions = new File(Tuils.getFolder(), XMLPrefsManager.XMLPrefsRoot.SUGGESTIONS.path); + if(keepOld) { + Tuils.insertOld(oldTheme); + Tuils.insertOld(oldSuggestions); + } + + theme.renameTo(oldTheme); + suggestions.renameTo(oldSuggestions); + + reloadable.reload(); + } + + private void applyTheme(String theme, String suggestions, boolean keepOld) { + if(theme == null || suggestions == null) { + Tuils.sendOutput(context, R.string.theme_unable); + return; + } + + Matcher colorMatcher = colorParser.matcher(theme); + while(colorMatcher.find()) { + theme = Pattern.compile(Pattern.quote(colorMatcher.group())).matcher(theme).replaceAll(toHexColor(colorMatcher.group())); + } + + colorMatcher = colorParser.matcher(suggestions); + while(colorMatcher.find()) { + suggestions = Pattern.compile(Pattern.quote(colorMatcher.group())).matcher(suggestions).replaceAll(toHexColor(colorMatcher.group())); + } + + File oldTheme = new File(Tuils.getFolder(), XMLPrefsManager.XMLPrefsRoot.THEME.path); + File oldSuggestions = new File(Tuils.getFolder(), XMLPrefsManager.XMLPrefsRoot.SUGGESTIONS.path); + if(keepOld) { + Tuils.insertOld(oldTheme); + Tuils.insertOld(oldSuggestions); + } + oldTheme.delete(); + oldSuggestions.delete(); + + try { + FileOutputStream themeStream = new FileOutputStream(oldTheme); + themeStream.write(theme.getBytes()); + themeStream.flush(); + themeStream.close(); + + FileOutputStream suggestionsStream = new FileOutputStream(oldSuggestions); + suggestionsStream.write(suggestions.getBytes()); + suggestionsStream.flush(); + suggestionsStream.close(); + + reloadable.reload(); + } catch (IOException e) { + Tuils.sendOutput(context, R.string.output_error); + } + } + + private void revert() { + applyTheme(Tuils.getOld(XMLPrefsManager.XMLPrefsRoot.THEME.path), Tuils.getOld(XMLPrefsManager.XMLPrefsRoot.SUGGESTIONS.path), false); + } + + private void standard() { + File oldTheme = new File(Tuils.getFolder(), XMLPrefsManager.XMLPrefsRoot.THEME.path); + File oldSuggestions = new File(Tuils.getFolder(), XMLPrefsManager.XMLPrefsRoot.SUGGESTIONS.path); + Tuils.insertOld(oldTheme); + Tuils.insertOld(oldSuggestions); + + oldTheme.delete(); + oldSuggestions.delete(); + + reloadable.reload(); + } + +// rgba(255,87,34,1) + Pattern colorParser = Pattern.compile("rgba\\([\\s]*(\\d+),[\\s]*(\\d+),[\\s]*(\\d+),[\\s]*(\\d.*\\d*)[\\s]*\\)"); + private String toHexColor(String color) { + Matcher m = colorParser.matcher(color); + if(m.find()) { + int red = Integer.parseInt(m.group(1)); + int green = Integer.parseInt(m.group(2)); + int blue = Integer.parseInt(m.group(3)); + float alpha = Float.parseFloat(m.group(4)); + + String redHex = Integer.toHexString(red); + if(redHex.length() == 1) redHex = "0" + redHex; + + String greenHex = Integer.toHexString(green); + if(greenHex.length() == 1) greenHex = "0" + greenHex; + + String blueHex = Integer.toHexString(blue); + if(blueHex.length() == 1) blueHex = "0" + blueHex; + + String alphaHex = Integer.toHexString((int) alpha); + if(alphaHex.length() == 1) alphaHex = "0" + alphaHex; + + return "#" + (alpha == 1 ? Tuils.EMPTYSTRING : alphaHex) + redHex + greenHex + blueHex; + } else return Tuils.EMPTYSTRING; + } } diff --git a/app/src/main/java/ohi/andre/consolelauncher/managers/suggestions/RemoverRunnable.java b/app/src/main/java/ohi/andre/consolelauncher/managers/suggestions/RemoverRunnable.java index b24bb0e37653e5b770c22f92cf7861a08a87a7f5..f25a45b421232cbe27ef3ba691ff15c9674e5f0a 100644 --- a/app/src/main/java/ohi/andre/consolelauncher/managers/suggestions/RemoverRunnable.java +++ b/app/src/main/java/ohi/andre/consolelauncher/managers/suggestions/RemoverRunnable.java @@ -2,8 +2,6 @@ package ohi.andre.consolelauncher.managers.suggestions; import android.widget.LinearLayout; -import ohi.andre.consolelauncher.tuils.Tuils; - /** * Created by francescoandreuzzi on 11/03/2018. */ @@ -20,14 +18,10 @@ public class RemoverRunnable implements Runnable { @Override public void run() { - Tuils.log("run"); - if(stop) { stop = false; } else suggestionsView.removeAllViews(); isGoingToRun = false; - - Tuils.log("now childs", suggestionsView.getChildCount()); } } diff --git a/app/src/main/java/ohi/andre/consolelauncher/managers/suggestions/SuggestionRunnable.java b/app/src/main/java/ohi/andre/consolelauncher/managers/suggestions/SuggestionRunnable.java index 7e8f50b64fe47771830ece298243cd98a66e1555..8086dff7e260f6a9e8d563b08eb5f05b94dd0e60 100755 --- a/app/src/main/java/ohi/andre/consolelauncher/managers/suggestions/SuggestionRunnable.java +++ b/app/src/main/java/ohi/andre/consolelauncher/managers/suggestions/SuggestionRunnable.java @@ -55,12 +55,35 @@ public class SuggestionRunnable implements Runnable { MainPack pack; + private boolean transparentSuggestions; + private int suggAppBg, suggAliasBg, suggCmdBg, suggContactBg, suggFileBg, suggSongBg, suggDefaultBg; + private int suggAppText, suggAliasText, suggCmdText, suggContactText, suggFileText, suggSongText, suggDefaultText; + public SuggestionRunnable(MainPack pack, ViewGroup suggestionsView, LinearLayout.LayoutParams suggestionViewParams, HorizontalScrollView parent) { this.suggestionsView = suggestionsView; this.suggestionViewParams = suggestionViewParams; this.scrollView = parent; this.pack = pack; + transparentSuggestions = XMLPrefsManager.getBoolean(Suggestions.transparent_suggestions); + if(!transparentSuggestions) { + suggAppBg = XMLPrefsManager.getColor(Suggestions.apps_bg_color); + suggAliasBg = XMLPrefsManager.getColor(Suggestions.alias_bg_color); + suggCmdBg = XMLPrefsManager.getColor(Suggestions.cmd_bg_color); + suggContactBg = XMLPrefsManager.getColor(Suggestions.contact_bg_color); + suggFileBg = XMLPrefsManager.getColor(Suggestions.file_bg_color); + suggSongBg = XMLPrefsManager.getColor(Suggestions.song_bg_color); + suggDefaultBg = XMLPrefsManager.getColor(Suggestions.default_bg_color); + } + + suggAppText = XMLPrefsManager.getColor(Suggestions.apps_text_color); + suggAliasText = XMLPrefsManager.getColor(Suggestions.alias_text_color); + suggCmdText = XMLPrefsManager.getColor(Suggestions.cmd_text_color); + suggContactText = XMLPrefsManager.getColor(Suggestions.contact_text_color); + suggDefaultText = XMLPrefsManager.getColor(Suggestions.default_text_color); + suggFileText = XMLPrefsManager.getColor(Suggestions.file_text_color); + suggSongText = XMLPrefsManager.getColor(Suggestions.song_text_color); + reset(); } @@ -169,26 +192,7 @@ public class SuggestionRunnable implements Runnable { interrupted = false; } - static boolean transparentSuggestions, bgLoad = false, textLoad = false; - static int suggAppBg, suggAliasBg, suggCmdBg, suggContactBg, suggFileBg, suggSongBg, suggDefaultBg; - static int suggAppText, suggAliasText, suggCmdText, suggContactText, suggFileText, suggSongText, suggDefaultText; - - public static Drawable getSuggestionBg(int type) { - if(!bgLoad) { - bgLoad = true; - - transparentSuggestions = XMLPrefsManager.getBoolean(Suggestions.transparent_suggestions); - if(!transparentSuggestions) { - suggAppBg = XMLPrefsManager.getColor(Suggestions.apps_bg_color); - suggAliasBg = XMLPrefsManager.getColor(Suggestions.alias_bg_color); - suggCmdBg = XMLPrefsManager.getColor(Suggestions.cmd_bg_color); - suggContactBg = XMLPrefsManager.getColor(Suggestions.contact_bg_color); - suggFileBg = XMLPrefsManager.getColor(Suggestions.file_bg_color); - suggSongBg = XMLPrefsManager.getColor(Suggestions.song_bg_color); - suggDefaultBg = XMLPrefsManager.getColor(Suggestions.default_bg_color); - } - } - + public Drawable getSuggestionBg(int type) { if(transparentSuggestions) { return new ColorDrawable(Color.TRANSPARENT); } else { @@ -211,19 +215,7 @@ public class SuggestionRunnable implements Runnable { } } - public static int getSuggestionTextColor(int type) { - if(!textLoad) { - textLoad = true; - - suggAppText = XMLPrefsManager.getColor(Suggestions.apps_text_color); - suggAliasText = XMLPrefsManager.getColor(Suggestions.alias_text_color); - suggCmdText = XMLPrefsManager.getColor(Suggestions.cmd_text_color); - suggContactText = XMLPrefsManager.getColor(Suggestions.contact_text_color); - suggDefaultText = XMLPrefsManager.getColor(Suggestions.default_text_color); - suggFileText = XMLPrefsManager.getColor(Suggestions.file_text_color); - suggSongText = XMLPrefsManager.getColor(Suggestions.song_text_color); - } - + public int getSuggestionTextColor(int type) { int chosen; switch (type) { diff --git a/app/src/main/java/ohi/andre/consolelauncher/managers/suggestions/SuggestionsManager.java b/app/src/main/java/ohi/andre/consolelauncher/managers/suggestions/SuggestionsManager.java index d2a6fb82c644dd87caf11b0eb6ea4af230a80225..a0fc9ec6fc3c9309827b6432eda22fc893c0f032 100755 --- a/app/src/main/java/ohi/andre/consolelauncher/managers/suggestions/SuggestionsManager.java +++ b/app/src/main/java/ohi/andre/consolelauncher/managers/suggestions/SuggestionsManager.java @@ -61,9 +61,9 @@ import static ohi.andre.consolelauncher.commands.CommandTuils.xmlPrefsFiles; public class SuggestionsManager { public static final String SINGLE_QUOTE = "'", DOUBLE_QUOTES = "\""; - private final int MAX_RATE = 100, NO_RATE = -1, FIRST_INTERVAL = 6; + private final int NO_RATE = -1, FIRST_INTERVAL = 6; - private boolean showAliasDefault, set = false, clickToLaunch, showAppsGpDefault, enabled; + private boolean showAliasDefault, clickToLaunch, showAppsGpDefault, enabled; private int minCmdPriority; private String multipleCmdSeparator; @@ -91,6 +91,8 @@ public class SuggestionsManager { private RemoverRunnable removeAllSuggestions; + private int minAppRate, minFileRate, minContactRate, minSongRate; + int[] indexes, counts; Comparator<Suggestion> comparator = new Comparator<Suggestion>() { @Override @@ -103,7 +105,9 @@ public class SuggestionsManager { int i = indexes[o1.type] - indexes[o2.type]; - if(i == 0) return o2.rate - o1.rate; + if(i == 0) { + return o2.rate - o1.rate; + } return i; } }; @@ -112,7 +116,7 @@ public class SuggestionsManager { Comparator<Suggestion> noInputComparator = new Comparator<Suggestion>() { @Override public int compare(Suggestion o1, Suggestion o2) { - if(o1.type >= indexes.length || o2.type >= indexes.length) { + if(o1.type >= noInputIndexes.length || o2.type >= noInputIndexes.length) { if(o1.type == o2.type) { return o2.rate - o1.rate; } else return 0; @@ -125,13 +129,6 @@ public class SuggestionsManager { } }; - Comparator<SimpleMutableEntry<Compare.Stringable, Integer>> stringableComparator = new Comparator<SimpleMutableEntry<Compare.Stringable, Integer>>() { - @Override - public int compare(SimpleMutableEntry<Compare.Stringable, Integer> o1, SimpleMutableEntry<Compare.Stringable, Integer> o2) { - return o2.getValue() - o1.getValue(); - } - }; - public SuggestionsManager(LinearLayout suggestionsView, MainPack mainPack, TerminalManager mTerminalAdapter) { this.suggestionsView = suggestionsView; this.pack = mainPack; @@ -172,11 +169,15 @@ public class SuggestionsManager { showAliasDefault = XMLPrefsManager.getBoolean(Suggestions.suggest_alias_default); showAppsGpDefault = XMLPrefsManager.getBoolean(Suggestions.suggest_appgp_default); - set = true; clickToLaunch = XMLPrefsManager.getBoolean(Suggestions.click_to_launch); minCmdPriority = XMLPrefsManager.getInt(Suggestions.noinput_min_command_priority); + minAppRate = XMLPrefsManager.getInt(Suggestions.app_suggestions_minrate); + minSongRate = XMLPrefsManager.getInt(Suggestions.song_suggestions_minrate); + minContactRate = XMLPrefsManager.getInt(Suggestions.contact_suggestions_minrate); + minFileRate = XMLPrefsManager.getInt(Suggestions.file_suggestions_minrate); + String s = XMLPrefsManager.get(Suggestions.suggestions_order); Pattern orderPattern = Pattern.compile("(\\d+)\\((\\d+)\\)"); Matcher m = orderPattern.matcher(s); @@ -305,7 +306,7 @@ public class SuggestionsManager { public void requestSuggestion(final String input) { - Tuils.log("request sugg for", input); + if(!enabled) return; if (suggestionViewParams == null) { suggestionViewParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); @@ -712,12 +713,13 @@ public class SuggestionsManager { private void suggestFile(MainPack info, List<Suggestion> suggestions, String afterLastSpace, String beforeLastSpace) { boolean noAfterLastSpace = afterLastSpace == null || afterLastSpace.length() == 0; boolean afterLastSpaceNotEndsWithSeparator = noAfterLastSpace || !afterLastSpace.endsWith(File.separator); + if(noAfterLastSpace || afterLastSpaceNotEndsWithSeparator) { - suggestions.add(new Suggestion(beforeLastSpace, File.separator, false, MAX_RATE, Suggestion.TYPE_FILE, afterLastSpace)); + suggestions.add(new Suggestion(beforeLastSpace, File.separator, false, Compare.MAX_RATE, Suggestion.TYPE_FILE, afterLastSpace)); } if(Suggestion.appendQuotesBeforeFile && !noAfterLastSpace && !afterLastSpace.endsWith(SINGLE_QUOTE) && !afterLastSpace.endsWith(DOUBLE_QUOTES)) - suggestions.add(new Suggestion(beforeLastSpace, SINGLE_QUOTE, false, MAX_RATE, Suggestion.TYPE_FILE, afterLastSpace)); + suggestions.add(new Suggestion(beforeLastSpace, SINGLE_QUOTE, false, Compare.MAX_RATE - 1, Suggestion.TYPE_FILE, afterLastSpace)); if (noAfterLastSpace) { suggestFilesInDir(null, suggestions, info.currentDirectory, beforeLastSpace); @@ -793,7 +795,8 @@ public class SuggestionsManager { // Tuils.log("als", afterLastSeparator); // Tuils.log("alsals", afterLastSpaceWithoutALS); - for(SimpleMutableEntry<String, Integer> s : Compare.matchesWithRate(files, rmQuotes.matcher(afterLastSeparator).replaceAll(Tuils.EMPTYSTRING), false)) { + List<SimpleMutableEntry<String, Integer>> list = Compare.compareWithRates(minFileRate, files, rmQuotes.matcher(afterLastSeparator).replaceAll(Tuils.EMPTYSTRING), false, Compare.MAX_RATE - 2, false); + for(SimpleMutableEntry<String, Integer> s : list) { suggestions.add(new Suggestion(beforeLastSpace , s.getKey(), false, s.getValue(), Suggestion.TYPE_FILE, afterLastSpaceWithoutALS)); } } @@ -823,13 +826,16 @@ public class SuggestionsManager { for (ContactManager.Contact contact : contacts) suggestions.add(new Suggestion(beforeLastSpace , contact.name, true, NO_RATE, Suggestion.TYPE_CONTACT, contact)); } else { - for(ContactManager.Contact contact : contacts) { - if(Thread.currentThread().isInterrupted()) return; - - int rate = Compare.matches(contact.name, afterLastSpace, true); - if(rate != -1) { - suggestions.add(new Suggestion(beforeLastSpace , contact.name, true, rate, Suggestion.TYPE_CONTACT, contact)); - } +// for(ContactManager.Contact contact : contacts) { +// int rate = Compare.matches(contact.name, afterLastSpace, true); +// if(rate != -1) { +// suggestions.add(new Suggestion(beforeLastSpace , contact.name, true, rate, Suggestion.TYPE_CONTACT, contact)); +// } +// } + + List<SimpleMutableEntry<Compare.Stringable, Integer>> infos = Compare.compareWithRates(minContactRate, contacts, true, afterLastSpace, false); + for(SimpleMutableEntry<Compare.Stringable, Integer> i : infos) { + suggestions.add(new Suggestion(beforeLastSpace , i.getKey().getString(), clickToLaunch, i.getValue(), Suggestion.TYPE_CONTACT, i.getKey())); } } } @@ -846,7 +852,7 @@ public class SuggestionsManager { } } else { - List<SimpleMutableEntry<Compare.Stringable, Integer>> infos = Compare.matchesWithRate(songs, true, afterLastSpace); + List<SimpleMutableEntry<Compare.Stringable, Integer>> infos = Compare.compareWithRates(minSongRate, songs, true, afterLastSpace, false); for(SimpleMutableEntry<Compare.Stringable, Integer> i : infos) { suggestions.add(new Suggestion(beforeLastSpace , i.getKey().getString(), clickToLaunch, i.getValue(), Suggestion.TYPE_SONG)); } @@ -873,7 +879,7 @@ public class SuggestionsManager { CommandAbstraction cmd = info.commandGroup.getCommandByName(s); int[] args = cmd.argType(); boolean exec = args == null || args.length == 0; - suggestions.add(new Suggestion(beforeLastSpace , s, exec && clickToLaunch, MAX_RATE, Suggestion.TYPE_COMMAND)); + suggestions.add(new Suggestion(beforeLastSpace , s, exec && clickToLaunch, Compare.MAX_RATE, Suggestion.TYPE_COMMAND)); canInsert--; } } @@ -899,7 +905,7 @@ public class SuggestionsManager { private void suggestColor(List<Suggestion> suggestions, String afterLastSpace, String beforeLastSpace ) { if(afterLastSpace == null || afterLastSpace.length() == 0 || (afterLastSpace.length() == 1 && afterLastSpace.charAt(0) != '#')) { - suggestions.add(new Suggestion(beforeLastSpace , "#", false, MAX_RATE, Suggestion.TYPE_COLOR)); + suggestions.add(new Suggestion(beforeLastSpace , "#", false, Compare.MAX_RATE, Suggestion.TYPE_COLOR)); } } @@ -918,8 +924,8 @@ public class SuggestionsManager { } private void suggestDefaultApp(MainPack info, List<Suggestion> suggestions, String afterLastSpace, String beforeLastSpace ) { - suggestions.add(new Suggestion(beforeLastSpace , "most_used", false, MAX_RATE, Suggestion.TYPE_PERMANENT)); - suggestions.add(new Suggestion(beforeLastSpace , "null", false, MAX_RATE, Suggestion.TYPE_PERMANENT)); + suggestions.add(new Suggestion(beforeLastSpace , "most_used", false, Compare.MAX_RATE, Suggestion.TYPE_PERMANENT)); + suggestions.add(new Suggestion(beforeLastSpace , "null", false, Compare.MAX_RATE, Suggestion.TYPE_PERMANENT)); suggestApp(info.appsManager.shownApps(), suggestions, afterLastSpace, beforeLastSpace, true); } @@ -937,8 +943,7 @@ public class SuggestionsManager { } } else { - List<SimpleMutableEntry<Compare.Stringable, Integer>> infos = Compare.matchesWithRate(apps, true, afterLastSpace); - Collections.sort(infos, stringableComparator); + List<SimpleMutableEntry<Compare.Stringable, Integer>> infos = Compare.compareWithRates(minAppRate, apps, true, afterLastSpace, true); for(SimpleMutableEntry<Compare.Stringable, Integer> i : infos) { if(canInsert == 0) return; @@ -953,7 +958,7 @@ public class SuggestionsManager { if(xmlPrefsEntrys == null) { xmlPrefsEntrys = new ArrayList<>(); - for(XMLPrefsManager.XMLPrefsRoot element : XMLPrefsManager.XMLPrefsRoot.values()) xmlPrefsEntrys.addAll(element.copy); + for(XMLPrefsManager.XMLPrefsRoot element : XMLPrefsManager.XMLPrefsRoot.values()) xmlPrefsEntrys.addAll(element.enums); Collections.addAll(xmlPrefsEntrys, Apps.values()); Collections.addAll(xmlPrefsEntrys, Notifications.values()); @@ -972,10 +977,11 @@ public class SuggestionsManager { if(Thread.currentThread().isInterrupted()) return; String label = s.label(); - int rate = Compare.matches(label, afterLastSpace, true); - if(rate != -1) { + int rate; + try { + rate = Compare.compare(-1, label, afterLastSpace, true, Compare.MAX_RATE); suggestions.add(new Suggestion(beforeLastSpace , label, false, rate, Suggestion.TYPE_COMMAND)); - } + } catch (Compare.CompareStringLowerThanMinimumException e) {} } } } @@ -1002,7 +1008,7 @@ public class SuggestionsManager { if(Thread.currentThread().isInterrupted()) return; if(s.startsWith(afterLastSpace)) { - suggestions.add(new Suggestion(beforeLastSpace , s, false, MAX_RATE, Suggestion.TYPE_CONFIGFILE, afterLastSpace)); + suggestions.add(new Suggestion(beforeLastSpace , s, false, Compare.MAX_RATE, Suggestion.TYPE_CONFIGFILE, afterLastSpace)); } } } @@ -1026,13 +1032,14 @@ public class SuggestionsManager { canInsert = counts[Suggestion.TYPE_APPGP]; for(AppsManager.Group g : groups) { String label = g.getName(); - int rate = Compare.matches(label, afterLastSpace, true); - if(rate != -1) { + int rate; + try { + rate = Compare.compare(-1, label, afterLastSpace, true, Compare.MAX_RATE); if(canInsert == 0) return; canInsert--; suggestions.add(new Suggestion(beforeLastSpace , label, false, rate, Suggestion.TYPE_APPGP, g)); - } + } catch (Compare.CompareStringLowerThanMinimumException e) {} } } } @@ -1082,7 +1089,7 @@ public class SuggestionsManager { } } else { - List<SimpleMutableEntry<Compare.Stringable, Integer>> infos = Compare.matchesWithRate(apps, true, app); + List<SimpleMutableEntry<Compare.Stringable, Integer>> infos = Compare.compareWithRates(apps, true, app, false); for(SimpleMutableEntry<Compare.Stringable, Integer> i : infos) { suggestions.add(new Suggestion(beforeLastSpace , i.getKey().getString(), clickToLaunch, i.getValue(), Suggestion.TYPE_APP, i.getKey())); } @@ -1102,7 +1109,7 @@ public class SuggestionsManager { } } else { - List<SimpleMutableEntry<Compare.Stringable, Integer>> infos = Compare.matchesWithRate(apps, true, afterLastSpace); + List<SimpleMutableEntry<Compare.Stringable, Integer>> infos = Compare.compareWithRates(apps, true, afterLastSpace, false); for(SimpleMutableEntry<Compare.Stringable, Integer> i : infos) { suggestions.add(new Suggestion(beforeLastSpace , String.valueOf(((BoundApp) i.getKey()).applicationId), i.getKey().getString(), false, i.getValue(), Suggestion.TYPE_APP)); } diff --git a/app/src/main/java/ohi/andre/consolelauncher/managers/xml/XMLPrefsManager.java b/app/src/main/java/ohi/andre/consolelauncher/managers/xml/XMLPrefsManager.java index 0a76a826d3afbccd1d59e9034ab0e6a7970bfa56..14a8da270dd06c39351ef9c653cafef55300102e 100755 --- a/app/src/main/java/ohi/andre/consolelauncher/managers/xml/XMLPrefsManager.java +++ b/app/src/main/java/ohi/andre/consolelauncher/managers/xml/XMLPrefsManager.java @@ -97,16 +97,13 @@ public class XMLPrefsManager { public String path; XMLPrefsList values; - List<XMLPrefsSave> enums; - public List<XMLPrefsSave> copy; + public List<XMLPrefsSave> enums; XMLPrefsRoot(String path, XMLPrefsSave[] en) { this.path = path; this.values = new XMLPrefsList(); - if(en == null) return; this.enums = new ArrayList<>(Arrays.asList(en)); - this.copy = new ArrayList<>(enums); } @Override @@ -164,8 +161,7 @@ public class XMLPrefsManager { Document d = (Document) o[0]; Element root = (Element) o[1]; - List<XMLPrefsSave> enums = element.enums; - if(enums == null) continue; + List<XMLPrefsSave> enums = new ArrayList<>(element.enums); String[] deleted = element.deleted(); boolean needToWrite = false; @@ -678,10 +674,10 @@ public class XMLPrefsManager { return null; } - private static boolean checkAttributes(Element e, String[] thatHasThose, String[] forValues, boolean alsoNotFound) { + private static boolean checkAttributes(Element e, String[] thatHasThose, String[] forValues, boolean alsoIfAttributeNotFound) { if(thatHasThose != null && forValues != null && thatHasThose.length == forValues.length) { for(int a = 0; a < thatHasThose.length; a++) { - if(!e.hasAttribute(thatHasThose[a])) return alsoNotFound; + if(!e.hasAttribute(thatHasThose[a])) return alsoIfAttributeNotFound; if(!forValues[a].equals(e.getAttribute(thatHasThose[a]))) return false; } } diff --git a/app/src/main/java/ohi/andre/consolelauncher/managers/xml/options/Behavior.java b/app/src/main/java/ohi/andre/consolelauncher/managers/xml/options/Behavior.java index 86c781e7e68dfb0a705d7e556272a89746f75514..4119691030761d1c0d2b83ad5dcecc10a9cb7346 100644 --- a/app/src/main/java/ohi/andre/consolelauncher/managers/xml/options/Behavior.java +++ b/app/src/main/java/ohi/andre/consolelauncher/managers/xml/options/Behavior.java @@ -909,7 +909,7 @@ public enum Behavior implements XMLPrefsSave { @Override public String info() { - return "0 = landscape, 1 = portrait, 2 = sensor"; + return "0 = landscape, 1 = portrait, 2 = auto-rotate"; } @Override diff --git a/app/src/main/java/ohi/andre/consolelauncher/managers/xml/options/Suggestions.java b/app/src/main/java/ohi/andre/consolelauncher/managers/xml/options/Suggestions.java index 5f897d5c8d5bd2993407018cc49069db3d3a3c70..c85acfdd8c5b41b5c9aa19367de6569b13e6ad49 100644 --- a/app/src/main/java/ohi/andre/consolelauncher/managers/xml/options/Suggestions.java +++ b/app/src/main/java/ohi/andre/consolelauncher/managers/xml/options/Suggestions.java @@ -263,7 +263,7 @@ public enum Suggestions implements XMLPrefsSave { double_space_click_first_suggestion { @Override public String defaultValue() { - return "false"; + return "true"; } @Override @@ -323,6 +323,70 @@ public enum Suggestions implements XMLPrefsSave { public String info() { return "The minimum priority of a command shown when the input field is empty"; } + }, + file_suggestions_minrate { + @Override + public String defaultValue() { + return "-1"; + } + + @Override + public String type() { + return XMLPrefsSave.INTEGER; + } + + @Override + public String info() { + return "The minimum rate of a file suggestion"; + } + }, + app_suggestions_minrate { + @Override + public String defaultValue() { + return "-1"; + } + + @Override + public String type() { + return XMLPrefsSave.INTEGER; + } + + @Override + public String info() { + return "The minimum rate of an app suggestion"; + } + }, + contact_suggestions_minrate { + @Override + public String defaultValue() { + return "-1"; + } + + @Override + public String type() { + return XMLPrefsSave.INTEGER; + } + + @Override + public String info() { + return "The minimum rate of a contact suggestion"; + } + }, + song_suggestions_minrate { + @Override + public String defaultValue() { + return "-1"; + } + + @Override + public String type() { + return XMLPrefsSave.INTEGER; + } + + @Override + public String info() { + return "The minimum rate of a song suggestion"; + } }; @Override diff --git a/app/src/main/java/ohi/andre/consolelauncher/tuils/Compare.java b/app/src/main/java/ohi/andre/consolelauncher/tuils/Compare.java index b07f86eb41d7c25362233372db14f698479157e8..a0f285ceb7fe8a8b989fa7f9c2bee288d66caf07 100644 --- a/app/src/main/java/ohi/andre/consolelauncher/tuils/Compare.java +++ b/app/src/main/java/ohi/andre/consolelauncher/tuils/Compare.java @@ -17,9 +17,18 @@ public class Compare { static final char[] allowed_separators = {' ', '-', '_'}; private static final String ACCENTS_PATTERN = "\\p{InCombiningDiacriticalMarks}+"; + public static int MAX_RATE = 1000000, MIN_RATE = -1000000; + static Pattern unconsideredSymbols = Pattern.compile("[\\s_-]"); static Pattern accentPattern = Pattern.compile(ACCENTS_PATTERN); + private static Comparator<SimpleMutableEntry<? extends Object, Integer>> entryComparator = new Comparator<SimpleMutableEntry<? extends Object, Integer>>() { + @Override + public int compare(SimpleMutableEntry<? extends Object, Integer> o1, SimpleMutableEntry<? extends Object, Integer> o2) { + return o2.getValue() - o1.getValue(); + } + }; + public static String removeAccents(String s) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) { String decomposed = Normalizer.normalize(s, Normalizer.Form.NFD); @@ -31,9 +40,9 @@ public class Compare { // private static String lastComparator = null; // private static double averageTime; -// private static int n; +// private static int n, averageLength; - public static int matches(String compared, String comparator, boolean allowSkip) { + public static int compare(String compared, String comparator, boolean allowSkip, int maximum) { // Tuils.log("-----------------"); // if(lastComparator != null && lastComparator.equals(comparator)) {} @@ -41,10 +50,12 @@ public class Compare { // Tuils.log("#######################"); // Tuils.log("average for cmp: " + lastComparator); // Tuils.log("average: " + averageTime + ", n: " + n); +// Tuils.log("average compared length", averageLength); // Tuils.log("#######################"); // // averageTime = 0; // n = 0; +// averageLength = 0; // // lastComparator = comparator; // } @@ -71,13 +82,117 @@ public class Compare { } } -// String unconsidered = unconsideredSymbols.matcher(compared).replaceAll(Tuils.EMPTYSTRING); -// if(unconsidered.length() != compared.length()) { -// s.add(new ComparePack(unconsidered, 0, 1)); + s.add(new ComparePack(compared, 0, 1)); + + String unconsidered = unconsideredSymbols.matcher(compared).replaceAll(Tuils.EMPTYSTRING); + if(unconsidered.length() != compared.length()) { + s.add(new ComparePack(unconsidered, 0, 1)); + } + + float maxRate = -1; + int maxRateIndex = -1; + + Main: + for(ComparePack cmp : s) { +// Tuils.log("s: " + cmp.s); + + int stop = Math.min(cmp.s.length(), comparator.length()); +// Tuils.log("stop", stop); + float minus = (float) (0.5 * (comparator.length() / 5)); + + float rate = 0; + for(int i = 0; i < stop; i++) { + char c1 = cmp.s.charAt(i); + char c2 = comparator.charAt(i); + +// Tuils.log("index: " + i); + + if(c1 == c2) { +// Tuils.log("equals"); + rate++; + } else { +// Tuils.log(c1 + " is not " + c2); + rate -= minus; + + if(rate + (stop - 1 - i) < minRate) { +// Tuils.log("continue"); + continue Main; + } + } +// Tuils.log("rate: " + rate); + } + + if(rate >= minRate) { + maxRate = Math.max(maxRate, rate); + maxRateIndex = cmp.index; +// Tuils.log("maxRate changed"); + } + } + +// int delay = (int) (System.currentTimeMillis() - time); +// Tuils.log("return", Math.round(maxRate)); +// Tuils.log("normal time: " + delay); +// Tuils.log("started: " + time, "end: " + System.currentTimeMillis()); +// if(delay > 4) Tuils.log("!!!!!!!!!!!!!"); +// +// averageTime = (averageTime * n + delay) / (n + 1); +// averageLength = (averageLength * n + compared.length()) / (n + 1); +// n++; + + int r = Math.round(maxRate); + if(r == comparator.length() && maxRateIndex == 0) { + return maximum; + } + return r; + } + + public static int compare(int minimum, String compared, String comparator, boolean allowSkip, int maximum) throws CompareStringLowerThanMinimumException { +// Tuils.log("-----------------"); + +// if(lastComparator != null && lastComparator.equals(comparator)) {} +// else { +// Tuils.log("#######################"); +// Tuils.log("average for cmp: " + lastComparator); +// Tuils.log("average: " + averageTime + ", n: " + n); +// Tuils.log("average compared length", averageLength); +// Tuils.log("#######################"); +// +// averageTime = 0; +// n = 0; +// averageLength = 0; +// +// lastComparator = comparator; // } +// long time = System.currentTimeMillis(); + +// Tuils.log("compared: " + compared + ", comparator: " + comparator); + + compared = removeAccents(compared).toLowerCase().trim(); + comparator = removeAccents(comparator).toLowerCase().trim(); + + double minRate = (double) comparator.length() / 2d; +// Tuils.log("min: " + minRate); + + List<ComparePack> s = new ArrayList<>(); + if(allowSkip) { + for(char sep : allowed_separators) { + String[] split = compared.split(String.valueOf(sep)); + if(split.length > 1) { + for(int count = 1; count < split.length; count++) { + s.add(new ComparePack(split[count], count, split.length)); + } + } + } + } + s.add(new ComparePack(compared, 0, 1)); + String unconsidered = unconsideredSymbols.matcher(compared).replaceAll(Tuils.EMPTYSTRING); + if(unconsidered.length() != compared.length()) { + s.add(new ComparePack(unconsidered, 0, 1)); + } + float maxRate = -1; int maxRateIndex = -1; @@ -99,9 +214,7 @@ public class Compare { if(c1 == c2) { // Tuils.log("equals"); rate++; - } - else if(unconsideredSymbols.matcher(String.valueOf(c1)).find()) {} - else { + } else { // Tuils.log(c1 + " is not " + c2); rate -= minus; @@ -122,80 +235,158 @@ public class Compare { // int delay = (int) (System.currentTimeMillis() - time); // Tuils.log("return", Math.round(maxRate)); -// Tuils.log("time: " + delay); +// Tuils.log("normal time: " + delay); +// Tuils.log("started: " + time, "end: " + System.currentTimeMillis()); // if(delay > 4) Tuils.log("!!!!!!!!!!!!!"); // // averageTime = (averageTime * n + delay) / (n + 1); +// averageLength = (averageLength * n + compared.length()) / (n + 1); // n++; int r = Math.round(maxRate); - if(r == comparator.length() && maxRateIndex == 0) return Integer.MAX_VALUE; + if(r < minimum) throw new CompareStringLowerThanMinimumException(); + if(r == comparator.length() && maxRateIndex == 0) { + return maximum; + } return r; } - public static List<String> matches(List<String> compared, String comparator, boolean allowSkip) { - List<SimpleMutableEntry<String, Integer>> ms = matchesWithRate(compared, comparator, allowSkip); + public static int compare(String compared, String comparator, boolean allowSkip) { + return compare(compared, comparator, allowSkip, MAX_RATE); + } + + + + + public static List<SimpleMutableEntry<String, Integer>> compareWithRates(int minimum, List<String> compared, String comparator, boolean allowSkip, int maximum, boolean sort) { + List<SimpleMutableEntry<String, Integer>> ms = new ArrayList<>(); + + for(String s : compared) { + if(Thread.currentThread().isInterrupted()) return ms; - List<String> result = new ArrayList<>(ms.size()); - for(SimpleMutableEntry<String, Integer> e : ms) { - result.add(e.getKey()); + try { + ms.add(new SimpleMutableEntry<>(s, compare(minimum, s, comparator, allowSkip, maximum))); + } catch (CompareStringLowerThanMinimumException e) {} } - return result; - } + if(sort) { + Collections.sort(ms, entryComparator); + } - public static List<String> matches(String[] compared, String comparator, boolean allowSkip) { - return matches(Arrays.asList(compared), comparator, allowSkip); + return ms; } - public static List<SimpleMutableEntry<String, Integer>> matchesWithRate(List<String> compared, String comparator, boolean allowSkip) { + public static List<SimpleMutableEntry<String, Integer>> compareWithRates(List<String> compared, String comparator, boolean allowSkip, int maximum, boolean sort) { List<SimpleMutableEntry<String, Integer>> ms = new ArrayList<>(); for(String s : compared) { if(Thread.currentThread().isInterrupted()) return ms; - int rate = matches(s, comparator, allowSkip); - if(rate != -1) ms.add(new SimpleMutableEntry<>(s, rate)); + ms.add(new SimpleMutableEntry<>(s, compare(s, comparator, allowSkip, maximum))); } - Collections.sort(ms, new Comparator<SimpleMutableEntry<String, Integer>>() { - @Override - public int compare(SimpleMutableEntry<String, Integer> o1, SimpleMutableEntry<String, Integer> o2) { - return o1.getValue() - o2.getValue(); - } - }); + if(sort) { + Collections.sort(ms, entryComparator); + } return ms; } - public static List<SimpleMutableEntry<String, Integer>> matchesWithRate(String[] compared, String comparator, boolean allowSkip) { - return matchesWithRate(Arrays.asList(compared), comparator, allowSkip); + public static List<SimpleMutableEntry<String, Integer>> compareWithRates(int minimum, List<String> compared, String comparator, boolean allowSkip, boolean sort) { + return compareWithRates(minimum, compared, comparator, allowSkip, MAX_RATE, sort); + } + + public static List<SimpleMutableEntry<String, Integer>> compareWithRates(List<String> compared, String comparator, boolean allowSkip, boolean sort) { + return compareWithRates(compared, comparator, allowSkip, MAX_RATE, sort); + } + + public static List<SimpleMutableEntry<String, Integer>> compareWithRates(int minimum, String[] compared, String comparator, boolean allowSkip, int maximum, boolean sort) { + return compareWithRates(minimum, Arrays.asList(compared), comparator, allowSkip, maximum, sort); } - public static List<SimpleMutableEntry<Stringable, Integer>> matchesWithRate(List<? extends Stringable> compared, boolean allowSkip, String comparator) { + public static List<SimpleMutableEntry<String, Integer>> compareWithRates(String[] compared, String comparator, boolean allowSkip, int maximum, boolean sort) { + return compareWithRates(Arrays.asList(compared), comparator, allowSkip, maximum, sort); + } + + public static List<SimpleMutableEntry<String, Integer>> compareWithRates(String[] compared, String comparator, boolean allowSkip, boolean sort) { + return compareWithRates(compared, comparator, allowSkip, MAX_RATE, sort); + } + + public static List<SimpleMutableEntry<Stringable, Integer>> compareWithRates(int minimum, List<? extends Stringable> compared, boolean allowSkip, String comparator, boolean sort) { + return compareWithRates(minimum, compared, allowSkip, comparator, MAX_RATE, sort); + } + + public static List<SimpleMutableEntry<Stringable, Integer>> compareWithRates(int minimum, List<? extends Stringable> compared, boolean allowSkip, String comparator, int maximum, boolean sort) { List<SimpleMutableEntry<Stringable, Integer>> ms = new ArrayList<>(); for(Stringable s : compared) { if(Thread.currentThread().isInterrupted()) return ms; - int rate = matches(s.getString(), comparator, allowSkip); - if(rate != -1) ms.add(new SimpleMutableEntry<>(s, rate)); + try { + ms.add(new SimpleMutableEntry<>(s, compare(minimum, s.getString(), comparator, allowSkip, maximum))); + } catch (CompareStringLowerThanMinimumException e) {} } - Collections.sort(ms, new Comparator<SimpleMutableEntry<Stringable, Integer>>() { - @Override - public int compare(SimpleMutableEntry<Stringable, Integer> o1, SimpleMutableEntry<Stringable, Integer> o2) { - return o1.getValue() - o2.getValue(); - } - }); + if(sort) { + Collections.sort(ms, entryComparator); + } + + return ms; + } + + public static List<SimpleMutableEntry<Stringable, Integer>> compareWithRates(List<? extends Stringable> compared, boolean allowSkip, String comparator, int maximum, boolean sort) { + List<SimpleMutableEntry<Stringable, Integer>> ms = new ArrayList<>(); + + for(Stringable s : compared) { + if(Thread.currentThread().isInterrupted()) return ms; + + ms.add(new SimpleMutableEntry<>(s, compare(s.getString(), comparator, allowSkip, maximum))); + } + + if(sort) { + Collections.sort(ms, entryComparator); + } return ms; } - public static List<SimpleMutableEntry<Stringable, Integer>> matchesWithRate(Stringable[] compared, boolean allowSkip, String comparator) { - return matchesWithRate(Arrays.asList(compared), allowSkip, comparator); + public static List<SimpleMutableEntry<Stringable, Integer>> compareWithRates(List<? extends Stringable> compared, boolean allowSkip, String comparator, boolean sort) { + return compareWithRates(compared, allowSkip, comparator, MAX_RATE, sort); + } + + public static List<SimpleMutableEntry<Stringable, Integer>> compareWithRates(Stringable[] compared, boolean allowSkip, String comparator, int maximum, boolean sort) { + return compareWithRates(Arrays.asList(compared), allowSkip, comparator, maximum, sort); + } + + public static List<SimpleMutableEntry<Stringable, Integer>> compareWithRates(Stringable[] compared, boolean allowSkip, String comparator, boolean sort) { + return compareWithRates(compared, allowSkip, comparator, MAX_RATE, sort); } + + + + public static List<String> compareList(int minimum, List<String> compared, String comparator, boolean allowSkip, int maximum, boolean sort) { + List<String> ms = new ArrayList<>(); + if(!sort) { + for (String s : compared) { + if (Thread.currentThread().isInterrupted()) return ms; + + try { + compare(minimum, s, comparator, allowSkip, maximum); + ms.add(s); + } catch (CompareStringLowerThanMinimumException e) {} + } + } else { + List<SimpleMutableEntry<String, Integer>> list = compareWithRates(minimum, compared, comparator, allowSkip, maximum, sort); + for(SimpleMutableEntry<String, Integer> s : list) ms.add(s.getKey()); + } + + return ms; + } + + + + private static class ComparePack { String s; @@ -212,4 +403,6 @@ public class Compare { public interface Stringable { String getString(); } + + public static class CompareStringLowerThanMinimumException extends Exception {} } diff --git a/app/src/main/java/ohi/andre/consolelauncher/tuils/Tuils.java b/app/src/main/java/ohi/andre/consolelauncher/tuils/Tuils.java index b5c82b9990f11919ead1a0c1042ea5288012b29a..cb7513f668726e482fee280bf108d75d321d5204 100755 --- a/app/src/main/java/ohi/andre/consolelauncher/tuils/Tuils.java +++ b/app/src/main/java/ohi/andre/consolelauncher/tuils/Tuils.java @@ -230,8 +230,31 @@ public class Tuils { return buffer.toString(); } + private static OnBatteryUpdate batteryUpdate; + private static BroadcastReceiver batteryReceiver = null; + public static void registerBatteryReceiver(Context context, OnBatteryUpdate listener) { try { + batteryReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + if(batteryUpdate == null) return; + + switch (intent.getAction()) { + case Intent.ACTION_BATTERY_CHANGED: + int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0); + batteryUpdate.update(level); + break; + case Intent.ACTION_POWER_CONNECTED: + batteryUpdate.onCharging(); + break; + case Intent.ACTION_POWER_DISCONNECTED: + batteryUpdate.onNotCharging(); + break; + } + } + }; + IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); iFilter.addAction(Intent.ACTION_POWER_CONNECTED); iFilter.addAction(Intent.ACTION_POWER_DISCONNECTED); @@ -245,30 +268,9 @@ public class Tuils { } public static void unregisterBatteryReceiver(Context context) { - context.unregisterReceiver(batteryReceiver); + if(batteryReceiver != null) context.unregisterReceiver(batteryReceiver); } - private static OnBatteryUpdate batteryUpdate; - private static BroadcastReceiver batteryReceiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - if(batteryUpdate == null) return; - - switch (intent.getAction()) { - case Intent.ACTION_BATTERY_CHANGED: - int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0); - batteryUpdate.update(level); - break; - case Intent.ACTION_POWER_CONNECTED: - batteryUpdate.onCharging(); - break; - case Intent.ACTION_POWER_DISCONNECTED: - batteryUpdate.onNotCharging(); - break; - } - } - }; - public static boolean containsExtension(String[] array, String value) { try { value = value.toLowerCase().trim(); @@ -306,6 +308,13 @@ public class Tuils { return songs; } + public static String convertStreamToString(java.io.InputStream is) { + if (is == null) return Tuils.EMPTYSTRING; + + java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); + return s.hasNext() ? s.next() : Tuils.EMPTYSTRING; + } + public static long download(InputStream in, File file) throws Exception { OutputStream out = new FileOutputStream(file, false); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 57c1eb48c6bca6aa938379aba52ba7af0bc16f7a..86ea471c58585293f82e222df5664b02a8ac2fe4 100755 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -13,7 +13,7 @@ <string name="anr">t-ui is having some issues. Please send to the developer your file crash.txt in the "t-ui" folder</string> <string name="admin_permission">Lock the screen on double tap</string> <string name="reply_app_not_found">Couldn\'t find bound app:</string> - <string name="error">A fatal error occurred, check the file crash.txt inside your t-ui folder</string> + <string name="no_internet">No internet access</string> <!-- reply --> <string name="reply_id_not_found">No bound app found with ID:</string> @@ -33,6 +33,7 @@ <string name="theme_invalid">Invalid theme</string> <string name="theme_old_not_found">You don\'t have an old theme</string> <string name="theme_reverted">Reverted to the previous theme</string> + <string name="theme_unable">Couldn\'t apply this theme</string> <!-- files --> <string name="output_error">An unknown error occurred</string> @@ -57,21 +58,21 @@ <!-- hints --> <string name="hint_donate">Do you like my work?\nIf yes, rate t-ui on Play Store (cmd: $ rate) or offer me a coffee (cmd: $ donate). Thank you!</string> <string name="hint_twitter">Did you know? t-ui is on Twitter. You can have a look with the cmd \"$ tui -twitter\"</string> - <string name="hint_telegram">Did you know? There\'s a Telegram group with many enthusiastic users, where you can discuss, share and chat with the developer.\nJust use the cmd \"$ tui -telegram\"</string> + <string name="hint_telegram">Did you know? There\'s a Telegram group with many enthusiastic users, where you can discuss, share and chat.\nJust use the cmd \"$ tui -telegram\"</string> <string name="hint_googlep">Did you know? Our official Google+ community is full of tips, experimental apks and discussions.\nYou can have a look with the cmd \"$ tui -googlep\"</string> - <string name="hint_tutorial">Did you read the tutorial? If you don\'t remember how to view it, the cmd is \"$ tutorial\"</string> + <string name="hint_tutorial">Did you read the tutorial? If you don\'t remember how to see it, use \"$ tutorial\"</string> <string name="hint_theme">Did you know that you can easily switch your theme? Have a look at the cmd \"$ theme\"</string> <string name="hint_theme2">Did you know? You can create your custom theme and share it to other users. Have a look at the cmd \"$ theme -create\"</string> <string name="hint_help">Doubts about a command? Just use \"$ help cmdName\"</string> - <string name="hint_music">Did you know? You can listen your music without launching other apps but t-ui. Have a look at the cmd \"$ music\"</string> + <string name="hint_music">Did you know? You can listen your music without launching other apps than t-ui. Have a look at the cmd \"$ music\"</string> <string name="hint_appgroups">Did you know? t-ui 6.4 introduced App Groups, a feature that let\'s you customize your app suggestions, creating different categories.\nYou can get more details with the cmd \"$ apps -tutorial\"</string> - <string name="hint_notifications">Did you know? You can see your notifications directly from t-ui.\nIf you\'re interested, have a look at the tutorial: \"$ notifications -tutorial\"</string> + <string name="hint_notifications">Did you know? You can see your notifications directly inside t-ui.\nIf you\'re interested, have a look at the tutorial: \"$ notifications -tutorial\"</string> <string name="hint_clear">Are you sick of clearing your screen? Have a look at the options \"clear_after_cmds\", \"clear_after_seconds\" and \"maxlines\"</string> <string name="hint_wallpaper">Do you want to use your wallpaper as t-ui background? Use the cmd: \"$ config -set system_wallpaper true\"</string> - <string name="hint_config">Doubts about any config option? Use the cmd \"$ config -info\" and look for that option inside the table</string> - <string name="hint_alias">Did you know that t-ui supports aliases? It also supports parametrized aliases.\nFor more info, use the cmd \"$ alias -tutorial\"</string> - <string name="hint_excludenotification">Did you know? You can exclude an unwanted notification long-clicking on it</string> + <string name="hint_config">Doubts about any config option? Use the cmd \"$ config -info [option]\"table</string> + <string name="hint_alias">Did you know that t-ui supports aliases? \nFor more info, use the cmd \"$ alias -tutorial\"</string> + <string name="hint_excludenotification">Did you know? You can exclude an unwanted notification by long-clicking on it</string> <string name="hint_disable">You can disable this messages with the cmd \"$ config -set show_hints false\"</string> @@ -266,10 +267,11 @@ </string> <string name="help_shellcommands">Print the commands in /system/bin and /system/xbin</string> <string name="help_status">Get info about battery charge, wifi status and mobile data</string> - <string name="help_theme">-apply [theme] -> apply the selected theme (case sensitive). require internet connection. may take some seconds + <string name="help_theme">-apply [name] -> apply the selected theme (case sensitive). require internet connection. may take some seconds + \n-old -> re-apply the theme that was replaced by the current one + \n-standard -> apply the standard theme \n-view -> view the available themes - \n-create -> create a new theme - \n-old -> re-apply the theme that was replaced by the current one</string> + \n-create -> create a new theme</string> <string name="help_time">Print the current time with the given format \n\nUsage: \n$ time [index] (index means the index of the desired time_format</string> diff --git a/build.gradle b/build.gradle index f20d08008a17919f45484cf2ffa376406b16377f..00d9507a9724f4357588503afebfbfda7989eb70 100644 --- a/build.gradle +++ b/build.gradle @@ -3,6 +3,7 @@ buildscript { repositories { jcenter() mavenCentral() + google() } dependencies { @@ -19,5 +20,6 @@ allprojects { repositories { jcenter() maven { url "https://jitpack.io" } + google() } }