From 2f4556e31b5d69e508ab9431eef4fea5f8d89e4f Mon Sep 17 00:00:00 2001 From: Fabrice Di Meglio Date: Mon, 10 Mar 2014 17:30:30 -0700 Subject: [PATCH] Code refactoring Change-Id: I053ab0c691295b81dc7aa0fd858fd609a69ade81 --- src/com/android/settings/indexer/Index.java | 44 +++++++++++---------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/com/android/settings/indexer/Index.java b/src/com/android/settings/indexer/Index.java index 5139e0c54b4..df1a58fe2c8 100644 --- a/src/com/android/settings/indexer/Index.java +++ b/src/com/android/settings/indexer/Index.java @@ -160,28 +160,28 @@ public class Index { return sb.toString(); } - public void addIndexableData(IndexableData data) { - mDataToIndex.add(data); - } - public void addIndexableData(IndexableData[] array) { - final int count = array.length; - for (int n = 0; n < count; n++) { - addIndexableData(array[n]); + synchronized (mDataToIndex) { + final int count = array.length; + for (int n = 0; n < count; n++) { + mDataToIndex.add(array[n]); + } } } public boolean update() { - final IndexTask task = new IndexTask(); - task.execute(); - try { - return task.get(); - } catch (InterruptedException e) { - Log.e(LOG_TAG, "Cannot update index: " + e.getMessage()); - return false; - } catch (ExecutionException e) { - Log.e(LOG_TAG, "Cannot update index: " + e.getMessage()); - return false; + synchronized (mDataToIndex) { + final IndexTask task = new IndexTask(); + task.execute(mDataToIndex); + try { + return task.get(); + } catch (InterruptedException e) { + Log.e(LOG_TAG, "Cannot update index: " + e.getMessage()); + return false; + } catch (ExecutionException e) { + Log.e(LOG_TAG, "Cannot update index: " + e.getMessage()); + return false; + } } } @@ -196,7 +196,7 @@ public class Index { /** * A private class for updating the Index database */ - private class IndexTask extends AsyncTask { + private class IndexTask extends AsyncTask, Integer, Boolean> { @Override protected void onPreExecute() { @@ -211,9 +211,13 @@ public class Index { } @Override - protected Boolean doInBackground(Void... params) { - final SQLiteDatabase database = getWritableDatabase(); + protected Boolean doInBackground(List... params) { boolean result = false; + final List dataToIndex = params[0]; + if (null == dataToIndex || dataToIndex.size() == 0) { + return result; + } + final SQLiteDatabase database = getWritableDatabase(); final Locale locale = Locale.getDefault(); final String localeStr = locale.toString(); if (isLocaleAlreadyIndexed(database, locale)) {