From 790994964a7aa830c15de90a2adc39ff50e2fb28 Mon Sep 17 00:00:00 2001 From: Francesco Andreuzzi <andreuzzi.francesco@gmail.com> Date: Thu, 16 Jun 2016 15:17:05 +0200 Subject: [PATCH] created --- .../tuils/SuggestionRunnable.java | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 app/src/main/java/ohi/andre/consolelauncher/tuils/SuggestionRunnable.java diff --git a/app/src/main/java/ohi/andre/consolelauncher/tuils/SuggestionRunnable.java b/app/src/main/java/ohi/andre/consolelauncher/tuils/SuggestionRunnable.java new file mode 100644 index 0000000..e624ef5 --- /dev/null +++ b/app/src/main/java/ohi/andre/consolelauncher/tuils/SuggestionRunnable.java @@ -0,0 +1,124 @@ +package ohi.andre.consolelauncher.tuils; + +/*Copyright Francesco Andreuzzi + +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.*/ + +import android.view.View; +import android.view.ViewGroup; +import android.widget.HorizontalScrollView; +import android.widget.TextView; + +import ohi.andre.consolelauncher.managers.SkinManager; +import ohi.andre.consolelauncher.managers.SuggestionsManager; + +public class SuggestionRunnable implements Runnable { + + private ViewGroup.LayoutParams suggestionViewParams; + private ViewGroup suggestionsView; + private HorizontalScrollView scrollView; + private Runnable scrollRunnable = new Runnable() { + @Override + public void run() { + scrollView.fullScroll(View.FOCUS_LEFT); + } + }; + + private int n; + private TextView[] toRecycle; + private TextView[] toAdd; + + private SuggestionsManager.Suggestion[] suggestions; + + private SkinManager skinManager; + + private boolean interrupted; + + public SuggestionRunnable(SkinManager skinManager, ViewGroup suggestionsView, ViewGroup.LayoutParams suggestionViewParams, HorizontalScrollView parent) { + this.skinManager = skinManager; + this.suggestionsView = suggestionsView; + this.suggestionViewParams = suggestionViewParams; + this.scrollView = parent; + + reset(); + } + + public void setN(int n) { + this.n = n; + } + + public void setToRecycle(TextView[] toRecycle) { + this.toRecycle = toRecycle; + } + + public void setToAdd(TextView[] toAdd) { + this.toAdd = toAdd; + } + + public void setSuggestions(SuggestionsManager.Suggestion[] suggestions) { + this.suggestions = suggestions; + } + + @Override + public void run() { + if (n < 0) { + for (int count = toRecycle.length; count < suggestionsView.getChildCount(); count++) { + suggestionsView.removeViewAt(count--); + } + } + + if (interrupted) { + return; + } + + for (int count = 0; count < suggestions.length; count++) { + if (interrupted) { + return; + } + + String s = suggestions[count].text; + if (count < toRecycle.length) { + toRecycle[count].setId(suggestions[count].exec ? SuggestionsManager.Suggestion.EXEC_ON_CLICK : + SuggestionsManager.Suggestion.NONE); + toRecycle[count].setText(s); + toRecycle[count].setBackgroundDrawable(null); + toRecycle[count].setBackgroundDrawable(skinManager.getSuggestionBg(suggestions[count].type)); + } else { + int space = suggestions.length - (count + 1); + if (toAdd != null && space < toAdd.length) { + toAdd[space].setId(suggestions[count].exec ? SuggestionsManager.Suggestion.EXEC_ON_CLICK : + SuggestionsManager.Suggestion.NONE); + toAdd[space].setText(s); + toAdd[space].setBackgroundDrawable(skinManager.getSuggestionBg(suggestions[count].type)); + if(toAdd[space].getParent() == null) { + suggestionsView.addView(toAdd[space], suggestionViewParams); + } else { + throw new UnsupportedOperationException("view has a parent(?)"); + } + } else { + throw new UnsupportedOperationException("no views enough"); + } + } + } + + scrollView.post(scrollRunnable); + } + + public void interrupt() { + interrupted = true; + } + + public void reset() { + interrupted = false; + } +} -- GitLab