From c95a18d672394f49d18fbc66259a6ebf04b49f39 Mon Sep 17 00:00:00 2001 From: HenriDellal Date: Fri, 23 Mar 2018 09:54:59 +0300 Subject: [PATCH] Bug fixes and minor changes --- AndroidManifest.xml | 4 +- res/layout/main_bar.xml | 2 +- res/values/strings.xml | 3 +- src/ru/henridellal/emerald/Apps.java | 54 +++++++++++-------- src/ru/henridellal/emerald/Category.java | 3 ++ .../henridellal/emerald/CategoryAdapter.java | 18 +++++++ .../henridellal/emerald/CategoryManager.java | 46 +++++++--------- .../emerald/CategoryManagerActivity.java | 38 ++++++------- src/ru/henridellal/emerald/GetApps.java | 2 +- src/ru/henridellal/emerald/SwipeListener.java | 37 ++++++------- 10 files changed, 113 insertions(+), 94 deletions(-) create mode 100644 src/ru/henridellal/emerald/CategoryAdapter.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 107ca7a..41a5687 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -1,7 +1,7 @@ diff --git a/res/layout/main_bar.xml b/res/layout/main_bar.xml index e5ab827..942ec9e 100644 --- a/res/layout/main_bar.xml +++ b/res/layout/main_bar.xml @@ -59,7 +59,7 @@ android:layout_width="0dp" android:layout_height="wrap_content" android:visibility="gone" - android:textSize="14sp" + android:textSize="18sp" android:layout_alignParentLeft="true" android:layout_toStartOf="@id/searchBar" android:layout_toLeftOf="@id/webSearchButton" /> diff --git a/res/values/strings.xml b/res/values/strings.xml index f389d0e..6c2fb60 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -49,6 +49,7 @@ Launcher settings Create category + Name is already in use Rename Edit application list Find in app market @@ -87,7 +88,7 @@ Close tutorial -Swipe on the category name to switch to the next/previous category. Click on it to see the full list of categories. +Swipe left/right on the apps menu to switch to the next/previous category. Click on the category name to see the categories list. \n \nOpen Launcher settings/Category manager to edit categories. \n diff --git a/src/ru/henridellal/emerald/Apps.java b/src/ru/henridellal/emerald/Apps.java index 7ef2cec..b80c7c5 100644 --- a/src/ru/henridellal/emerald/Apps.java +++ b/src/ru/henridellal/emerald/Apps.java @@ -20,7 +20,6 @@ import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.content.res.Configuration; -import android.graphics.Color; import android.net.Uri; import android.os.AsyncTask; import android.os.AsyncTask.Status; @@ -31,6 +30,7 @@ import android.text.InputType; import android.text.TextWatcher; //import android.util.Log; +import android.view.GestureDetector; import android.view.inputmethod.InputMethodManager; import android.view.LayoutInflater; import android.view.KeyEvent; @@ -39,7 +39,6 @@ import android.view.MenuItem; import android.view.View; import android.widget.AbsListView; -import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; @@ -47,12 +46,11 @@ import android.widget.GridView; import android.widget.LinearLayout; import android.widget.RelativeLayout; -import android.widget.Spinner; -import android.widget.TextView; import android.widget.Toast; public class Apps extends Activity { + private GestureDetector gestureDetector; private CategoryManager categories; private ArrayList curCatData; private RelativeLayout mainLayout; @@ -62,10 +60,10 @@ public class Apps extends Activity public SharedPreferences options; public static final String PREF_APPS = "apps"; public static final String APP_TAG = "Emerald"; - private CustomAdapter adapter = null; + private CustomAdapter adapter; public static final int GRID = 0; public static final int LIST = 1; - private GetApps scanner = null; + private GetApps scanner; private OnSharedPreferenceChangeListener prefListener; private boolean lock, returnToHome, searchIsOpened, homeButtonPressed, modPressed; private int historySize; @@ -158,15 +156,16 @@ private void addInHistory(AppData a) { //removes app from history if it is already in it // to avoid duplicating //Log.v(APP_TAG, "Add app in history"); - if (categories.getCategoryData(CategoryManager.HISTORY).indexOf(a) != -1) { - categories.removeFromCategory(CategoryManager.HISTORY, a); - } - - categories.addToHistory(a); - //removes old entries if History has maximum size - if (categories.getCategoryData(CategoryManager.HISTORY).size() > historySize) { - categories.removeFromCategory(CategoryManager.HISTORY, categories.getCategoryData(CategoryManager.HISTORY).size()-1); - } + if (!dock.hasApp(a)) { + if (categories.getCategoryData(CategoryManager.HISTORY).indexOf(a) != -1) { + categories.removeFromCategory(CategoryManager.HISTORY, a); + } + categories.addToHistory(a); + //removes old entries if History has maximum size + if (categories.getCategoryData(CategoryManager.HISTORY).size() > historySize) { + categories.removeFromCategory(CategoryManager.HISTORY, categories.getCategoryData(CategoryManager.HISTORY).size()-1); + } + } } //launches app and adds it to history public void launch(AppData a) { @@ -190,10 +189,13 @@ private void openCategoriesList() { builder.setCancelable(true); final ArrayList cats = new ArrayList(categories.getCategories()); cats.remove(CategoryManager.HIDDEN); + ArrayList toRemove = new ArrayList(); for (String category: cats) { if (categories.getCategory(category).isHidden()) - cats.remove(category); + toRemove.add(category); } + cats.removeAll(toRemove); + toRemove = null; final ArrayList categoriesNames = new ArrayList(cats.size()); for (String category: cats) { categoriesNames.add(categories.getCategory(category).getRepresentName(this)); @@ -457,6 +459,12 @@ public void onMyClick(View v) { } } + @Override + public boolean dispatchTouchEvent(MotionEvent event) { + gestureDetector.onTouchEvent(event); + return super.dispatchTouchEvent(event); + } + @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_MENU) { @@ -494,10 +502,10 @@ public boolean onKeyDown(int keyCode, KeyEvent event) { } else if (keyCode == KeyEvent.KEYCODE_0 && !searchIsOpened) { openSearch(); } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) { - categories.setCurCategory(categories.getPrevCategory()); + categories.setCurCategory(categories.getCategory(CategoryManager.PREVIOUS)); loadFilteredApps(); } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) { - categories.setCurCategory(categories.getNextCategory()); + categories.setCurCategory(categories.getCategory(CategoryManager.NEXT)); loadFilteredApps(); } else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) { openCategoriesList(); @@ -657,7 +665,7 @@ protected void onCreate(Bundle savedInstanceState) { notiManager.notify(0, noti); } //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); - setRequestedOrientation(Integer.parseInt(options.getString(Keys.ORIENTATION, "1"))); + setRequestedOrientation(Integer.parseInt(options.getString(Keys.ORIENTATION, "2"))); setContentView(mainLayout); options.edit().putBoolean(Keys.MESSAGE_SHOWN, false).commit(); prefListener = new OnSharedPreferenceChangeListener() { @@ -690,8 +698,12 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Themer.applyTheme(this, options); dock = new Dock(this); changePrefsOnRotate(); - - //grid.setOnTouchListener(new SwipeListener(this)); + gestureDetector = new GestureDetector(this, new SwipeListener(this)); + grid.setOnTouchListener(new View.OnTouchListener() { + public boolean onTouch(View view, MotionEvent event) { + return gestureDetector.onTouchEvent(event); + } + }); } @Override public boolean onOptionsItemSelected(MenuItem item) { diff --git a/src/ru/henridellal/emerald/Category.java b/src/ru/henridellal/emerald/Category.java index 0eb05b3..5e5ffbb 100644 --- a/src/ru/henridellal/emerald/Category.java +++ b/src/ru/henridellal/emerald/Category.java @@ -47,6 +47,9 @@ public void removeAll() { public String getRepresentName(Context context) { return hasCustomName() ? name : context.getResources().getString(stringResourceId); } + public void setName(String name) { + this.name = name; + } public String getName() { return name; } diff --git a/src/ru/henridellal/emerald/CategoryAdapter.java b/src/ru/henridellal/emerald/CategoryAdapter.java new file mode 100644 index 0000000..d5ad598 --- /dev/null +++ b/src/ru/henridellal/emerald/CategoryAdapter.java @@ -0,0 +1,18 @@ +package ru.henridellal.emerald; + +import java.util.ArrayList; +import java.util.List; + +import android.content.Context; +import android.widget.ArrayAdapter; + +public class CategoryAdapter extends ArrayAdapter { + public CategoryAdapter(Context context, int resource, List categoryNames) { + super(context, resource, categoryNames); + } + public void update(ArrayList content) { + clear(); + addAll(content); + notifyDataSetChanged(); + } +} diff --git a/src/ru/henridellal/emerald/CategoryManager.java b/src/ru/henridellal/emerald/CategoryManager.java index 83659f9..38a40ab 100644 --- a/src/ru/henridellal/emerald/CategoryManager.java +++ b/src/ru/henridellal/emerald/CategoryManager.java @@ -26,6 +26,10 @@ public class CategoryManager { public static final String UNCLASSIFIED = "Unclassified"; public static final String HIDDEN = "Hidden"; public static final String HISTORY = "History"; + + public static final byte PREVIOUS = 0; + public static final byte NEXT = 1; + private SoftReference contextRef; private String home; private String curCategory; @@ -63,39 +67,26 @@ public boolean haveCategory(String s) { } /*Functions for spinner. Return names of categories which are neighbouring to the current*/ - public String getPrevCategory() { - int result; - boolean finished = false; + public String getCategory(byte direction) { if (!curCategory.equals(HIDDEN)) { - result = names.indexOf(curCategory); + int result = names.indexOf(curCategory); + boolean finished = false; while (!finished) { - result--; - result = (result > -1) ? result : names.size()-2; - if (!categories.get(names.get(result)).isHidden()) { - finished = true; + if (direction == CategoryManager.PREVIOUS) { + result--; + result = (result > -1) ? result : names.size()-2; + } else { + result++; + result = (result < names.size()-1) ? result : 0; } - } - } else { - result = 0; - } - return names.get(result); - } - public String getNextCategory() { - int result; - boolean finished = false; - if (!curCategory.equals(HIDDEN)) { - result = names.indexOf(curCategory); - while (!finished) { - result++; - result = (result < names.size()-1) ? result : 0; if (!categories.get(names.get(result)).isHidden()) { finished = true; } } + return names.get(result); } else { - result = 0; + return null; } - return names.get(result); } public Category getCategory(String categoryName) { return categories.get(categoryName); @@ -117,8 +108,10 @@ public void loadCategories() { //puts entries from cache to categories if (isEditable(name)) { if (isCustom(name)) { - names.add(name); - categories.put(name, new Category(name, getEntries(f))); + if (!names.contains(name)) { + names.add(name); + categories.put(name, new Category(name, getEntries(f))); + } } else { int stringResourceId = 0; if (HISTORY.equals(name)) { @@ -557,6 +550,7 @@ public boolean renameCategory(String newName, String cat) { categories.put(newName, c); names.remove(cat); names.add(newName); + c.setName(newName); sortNames(); writeCategoriesProps(); setCurCategory(ALL); diff --git a/src/ru/henridellal/emerald/CategoryManagerActivity.java b/src/ru/henridellal/emerald/CategoryManagerActivity.java index 58c6a04..05ecc80 100644 --- a/src/ru/henridellal/emerald/CategoryManagerActivity.java +++ b/src/ru/henridellal/emerald/CategoryManagerActivity.java @@ -19,7 +19,7 @@ public class CategoryManagerActivity extends Activity{ private CategoryManager cm; private ArrayList categories, categoriesNames; - private ArrayAdapter adapter; + private CategoryAdapter adapter; private ListView catListView; private static final int COMMAND_HIDE = 0; @@ -29,8 +29,12 @@ public class CategoryManagerActivity extends Activity{ private static final int COMMAND_RENAME = 4; private static final int COMMAND_EDIT = 5; - public ArrayAdapter getAdapter() { - return adapter; + public void updateCategoriesList() { + categories = cm.getCategories(); + categoriesNames = new ArrayList(categories.size()); + for (String category: categories) { + categoriesNames.add(cm.getCategory(category).getRepresentName(this)); + } } @Override @@ -43,12 +47,8 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.categorymanager); cm = LauncherApp.getInstance().getCategoryManager(); - categories = cm.getCategories(); - categoriesNames = new ArrayList(categories.size()); - for (String category: categories) { - categoriesNames.add(cm.getCategory(category).getRepresentName(this)); - } - adapter = new ArrayAdapter(this, + updateCategoriesList(); + adapter = new CategoryAdapter(this, android.R.layout.simple_list_item_1, categoriesNames); catListView = (ListView)findViewById(R.id.categoryList); catListView.setAdapter(adapter); @@ -111,7 +111,6 @@ public void onClick(DialogInterface di, int which) { break; case COMMAND_RENAME: renameCategory(category); - //Toast.makeText(CategoryManagerActivity.this, "This category name is not editable", Toast.LENGTH_LONG).show(); break; case COMMAND_EDIT: appListEditor(category); @@ -143,7 +142,8 @@ private void deleteCategory(final String category) { new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { cm.removeCategory(category); - getAdapter().notifyDataSetChanged(); + updateCategoriesList(); + adapter.update(categoriesNames); } }).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { @@ -171,13 +171,8 @@ public void onClick(DialogInterface dialog, int which) { builder.create().show(); } private void renameCategory(final String category) { - /*if (! cm.isCustom(catName)) { - Toast.makeText(this, "This category name is not editable", Toast.LENGTH_LONG).show(); - return; - }*/ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(getResources().getText(R.string.rename).toString()); - builder.setMessage("Edit name of category:"); final EditText inputBox = new EditText(this); inputBox.setText(category); builder.setView(inputBox); @@ -185,10 +180,10 @@ private void renameCategory(final String category) { new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if (cm.renameCategory(inputBox.getText().toString(), category)) { - Toast.makeText(CategoryManagerActivity.this, "Successfully renamed", Toast.LENGTH_LONG).show(); - adapter.notifyDataSetChanged(); + updateCategoriesList(); + adapter.update(categoriesNames); } else { - Toast.makeText(CategoryManagerActivity.this, "Name already in use", Toast.LENGTH_LONG).show(); + Toast.makeText(CategoryManagerActivity.this, getResources().getString(R.string.note_rename_error), Toast.LENGTH_LONG).show(); } } }).setNegativeButton(android.R.string.no, @@ -271,9 +266,10 @@ private void newCategory() { new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if (!cm.addCategory(inputBox.getText().toString())) { - Toast.makeText(CategoryManagerActivity.this, "Name already in use", Toast.LENGTH_LONG).show(); + Toast.makeText(CategoryManagerActivity.this, getResources().getString(R.string.note_rename_error), Toast.LENGTH_LONG).show(); } else { - adapter.notifyDataSetChanged(); + updateCategoriesList(); + adapter.update(categoriesNames); } } }).setNegativeButton(android.R.string.cancel, diff --git a/src/ru/henridellal/emerald/GetApps.java b/src/ru/henridellal/emerald/GetApps.java index a049d24..c4889a8 100644 --- a/src/ru/henridellal/emerald/GetApps.java +++ b/src/ru/henridellal/emerald/GetApps.java @@ -66,7 +66,7 @@ protected ArrayList doInBackground(Boolean... slow) { // use intent to get apps that can be launched Intent launchIntent = new Intent(Intent.ACTION_MAIN); launchIntent.addCategory(Intent.CATEGORY_LAUNCHER); - + //TODO access shared preferences the other way int appShortcut = Integer.parseInt(context.options.getString(Keys.APP_SHORTCUT, "3")); boolean icons = appShortcut >= CustomAdapter.ICON; Map cache = new HashMap(); diff --git a/src/ru/henridellal/emerald/SwipeListener.java b/src/ru/henridellal/emerald/SwipeListener.java index 8b8c9ae..af7335a 100644 --- a/src/ru/henridellal/emerald/SwipeListener.java +++ b/src/ru/henridellal/emerald/SwipeListener.java @@ -1,36 +1,31 @@ package ru.henridellal.emerald; +import android.view.GestureDetector; import android.view.MotionEvent; -import android.view.View; import java.lang.ref.SoftReference; -public class SwipeListener implements View.OnTouchListener { - private float x, density; +public class SwipeListener extends GestureDetector.SimpleOnGestureListener { private SoftReference appsRef; + private float density; public SwipeListener(Apps apps) { appsRef = new SoftReference(apps); density = apps.getResources().getDisplayMetrics().density; } @Override - public boolean onTouch(View v, MotionEvent e) { - int action = e.getAction() & 255; - switch (action){ - case MotionEvent.ACTION_DOWN: - x = e.getX(); - return true; - case MotionEvent.ACTION_UP: - if (e.getX()-x > 30.0 * density) { - LauncherApp.getInstance().getCategoryManager().setCurCategory(LauncherApp.getInstance().getCategoryManager().getPrevCategory()); - appsRef.get().loadFilteredApps(); - return true; - } else if (x-e.getX() > 30.0 * density) { - LauncherApp.getInstance().getCategoryManager().setCurCategory(LauncherApp.getInstance().getCategoryManager().getNextCategory()); - appsRef.get().loadFilteredApps(); - return true; - } else v.performClick(); - default: - return false; + public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { + float deltaX = e1.getX() - e2.getX(); + float deltaY = Math.abs(e1.getY() - e2.getY()); + if (Math.abs(deltaX) > 100.f*density && (deltaY < Math.abs(deltaX)*0.3f)) { + CategoryManager cm = LauncherApp.getInstance().getCategoryManager(); + String category = (deltaX > 0.f) ? cm.getCategory(CategoryManager.NEXT) : cm.getCategory(CategoryManager.PREVIOUS); + if (null != category) { + cm.setCurCategory(category); + appsRef.get().loadFilteredApps(); + } + return true; + } else { + return false; } } }