From 7a197395999ced8fce1da2a5f86f202647698006 Mon Sep 17 00:00:00 2001 From: Danny Baumann Date: Fri, 20 Sep 2013 10:57:33 +0200 Subject: [PATCH] Some optimizations spotted during profiling. Change-Id: Ice5cc24d2574894798370eaaae144713414eb6cf --- res/layout/navigation_view_details_item.xml | 17 +++--- res/layout/navigation_view_simple_item.xml | 40 +++++-------- .../filemanager/activities/BookmarksActivity.java | 3 - .../filemanager/activities/HistoryActivity.java | 1 - .../filemanager/activities/SearchActivity.java | 1 - .../adapters/FileSystemObjectAdapter.java | 70 ++++++++++------------ .../filemanager/model/FileSystemObject.java | 5 +- .../filemanager/model/GroupPermission.java | 3 +- .../filemanager/model/OthersPermission.java | 3 +- .../cyanogenmod/filemanager/model/Permission.java | 16 ++++- .../cyanogenmod/filemanager/model/Permissions.java | 7 +-- .../filemanager/model/UserPermission.java | 3 +- .../cyanogenmod/filemanager/ui/ThemeManager.java | 29 ++++----- .../filemanager/ui/widgets/NavigationView.java | 6 +- .../cyanogenmod/filemanager/util/FileHelper.java | 14 ++--- 15 files changed, 97 insertions(+), 121 deletions(-) diff --git a/res/layout/navigation_view_details_item.xml b/res/layout/navigation_view_details_item.xml index 79175d6..f3b9b03 100644 --- a/res/layout/navigation_view_details_item.xml +++ b/res/layout/navigation_view_details_item.xml @@ -14,17 +14,17 @@ limitations under the License. --> - + android:background="@drawable/holo_list_selector_deselected" + android:orientation="horizontal" > @@ -32,18 +32,15 @@ android:id="@+id/navigation_view_item_icon" android:layout_width="@dimen/default_row_height" android:layout_height="@dimen/default_row_height" - android:layout_alignParentTop="true" - android:layout_toRightOf="@id/navigation_view_item_check" - android:layout_alignWithParentIfMissing="true" + android:layout_gravity="center_vertical" android:scaleType="centerInside" android:contentDescription="@null" android:src="@null" /> - + diff --git a/res/layout/navigation_view_simple_item.xml b/res/layout/navigation_view_simple_item.xml index 63a8a3f..bd73bf8 100644 --- a/res/layout/navigation_view_simple_item.xml +++ b/res/layout/navigation_view_simple_item.xml @@ -14,17 +14,17 @@ limitations under the License. --> - + android:background="@drawable/holo_list_selector_deselected" + android:orientation="horizontal" > @@ -32,30 +32,20 @@ android:id="@+id/navigation_view_item_icon" android:layout_width="@dimen/default_row_height" android:layout_height="@dimen/default_row_height" - android:layout_alignParentTop="true" - android:layout_toRightOf="@id/navigation_view_item_check" - android:layout_alignWithParentIfMissing="true" + android:layout_gravity="center_vertical" android:scaleType="centerInside" android:contentDescription="@null" android:src="@null" /> - - - - - - + android:layout_weight="1" + android:layout_marginLeft="@dimen/small_margin" + android:layout_marginRight="@dimen/extra_margin" + android:gravity="left|center_vertical" + android:singleLine="true" + android:textAppearance="@style/primary_text_appearance" /> + + diff --git a/src/com/cyanogenmod/filemanager/activities/BookmarksActivity.java b/src/com/cyanogenmod/filemanager/activities/BookmarksActivity.java index 317c8a4..4ac101e 100644 --- a/src/com/cyanogenmod/filemanager/activities/BookmarksActivity.java +++ b/src/com/cyanogenmod/filemanager/activities/BookmarksActivity.java @@ -117,7 +117,6 @@ public class BookmarksActivity extends Activity implements OnItemClickListener, } responder.accept(); adapter.remove(bookmark); - adapter.notifyDataSetChanged(); return; } @@ -304,7 +303,6 @@ public class BookmarksActivity extends Activity implements OnItemClickListener, waiting.setVisibility(View.GONE); if (result.booleanValue()) { adapter.addAll(this.mBookmarks); - adapter.notifyDataSetChanged(); BookmarksActivity.this.mBookmarksListView.setSelection(0); } else { @@ -393,7 +391,6 @@ public class BookmarksActivity extends Activity implements OnItemClickListener, return; } adapter.remove(bookmark); - adapter.notifyDataSetChanged(); return; } } diff --git a/src/com/cyanogenmod/filemanager/activities/HistoryActivity.java b/src/com/cyanogenmod/filemanager/activities/HistoryActivity.java index 964d880..84de995 100644 --- a/src/com/cyanogenmod/filemanager/activities/HistoryActivity.java +++ b/src/com/cyanogenmod/filemanager/activities/HistoryActivity.java @@ -352,7 +352,6 @@ public class HistoryActivity extends Activity implements OnItemClickListener { void clearHistory() { if (this.mAdapter != null) { this.mAdapter.clear(); - this.mAdapter.notifyDataSetChanged(); View msg = findViewById(R.id.history_empty_msg); msg.setVisibility(View.VISIBLE); this.mIsClearHistory = true; diff --git a/src/com/cyanogenmod/filemanager/activities/SearchActivity.java b/src/com/cyanogenmod/filemanager/activities/SearchActivity.java index a50a1a0..c7cc6f1 100644 --- a/src/com/cyanogenmod/filemanager/activities/SearchActivity.java +++ b/src/com/cyanogenmod/filemanager/activities/SearchActivity.java @@ -786,7 +786,6 @@ public class SearchActivity extends Activity void removeAll() { SearchResultAdapter adapter = (SearchResultAdapter)this.mSearchListView.getAdapter(); adapter.clear(); - adapter.notifyDataSetChanged(); this.mSearchListView.setSelection(0); toggleResults(false, true); } diff --git a/src/com/cyanogenmod/filemanager/adapters/FileSystemObjectAdapter.java b/src/com/cyanogenmod/filemanager/adapters/FileSystemObjectAdapter.java index 333ac13..c83da5d 100644 --- a/src/com/cyanogenmod/filemanager/adapters/FileSystemObjectAdapter.java +++ b/src/com/cyanogenmod/filemanager/adapters/FileSystemObjectAdapter.java @@ -19,6 +19,7 @@ package com.cyanogenmod.filemanager.adapters; import android.content.Context; import android.content.res.Resources; import android.graphics.drawable.Drawable; +import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; @@ -76,6 +77,7 @@ public class FileSystemObjectAdapter TextView mTvName; TextView mTvSummary; TextView mTvSize; + Boolean mHasSelectedBg; } /** @@ -256,6 +258,8 @@ public class FileSystemObjectAdapter public View getView(int position, View convertView, ViewGroup parent) { //Check to reuse view View v = convertView; + Theme theme = ThemeManager.getCurrentTheme(getContext()); + if (v == null) { //Create the view holder LayoutInflater li = @@ -273,6 +277,23 @@ public class FileSystemObjectAdapter viewHolder.mBtCheck = (ImageButton)v.findViewById(RESOURCE_ITEM_CHECK); viewHolder.mBtCheck.setVisibility(View.GONE); } + + // Apply the current theme + if (this.mPickable) { + theme.setBackgroundDrawable( + getContext(), v, "background_drawable"); //$NON-NLS-1$ + } + theme.setTextColor( + getContext(), viewHolder.mTvName, "text_color"); //$NON-NLS-1$ + if (viewHolder.mTvSummary != null) { + theme.setTextColor( + getContext(), viewHolder.mTvSummary, "text_color"); //$NON-NLS-1$ + } + if (viewHolder.mTvSize != null) { + theme.setTextColor( + getContext(), viewHolder.mTvSize, "text_color"); //$NON-NLS-1$ + } + v.setTag(viewHolder); } @@ -282,21 +303,6 @@ public class FileSystemObjectAdapter //Retrieve the view holder ViewHolder viewHolder = (ViewHolder)v.getTag(); - // Apply the current theme - Theme theme = ThemeManager.getCurrentTheme(getContext()); - theme.setBackgroundDrawable( - getContext(), v, "background_drawable"); //$NON-NLS-1$ - theme.setTextColor( - getContext(), viewHolder.mTvName, "text_color"); //$NON-NLS-1$ - if (viewHolder.mTvSummary != null) { - theme.setTextColor( - getContext(), viewHolder.mTvSummary, "text_color"); //$NON-NLS-1$ - } - if (viewHolder.mTvSize != null) { - theme.setTextColor( - getContext(), viewHolder.mTvSize, "text_color"); //$NON-NLS-1$ - } - //Set the data if (convertView != null) { @@ -314,18 +320,20 @@ public class FileSystemObjectAdapter } if (!this.mPickable) { viewHolder.mBtCheck.setVisibility( - dataHolder.mName.compareTo( - FileHelper.PARENT_DIRECTORY) == 0 ? View.INVISIBLE : View.VISIBLE); + TextUtils.equals(dataHolder.mName, FileHelper.PARENT_DIRECTORY) ? + View.INVISIBLE : View.VISIBLE); + viewHolder.mBtCheck.setImageDrawable(dataHolder.mDwCheck); viewHolder.mBtCheck.setTag(Integer.valueOf(position)); - // Apply theme - if (dataHolder.mSelected) { - theme.setBackgroundDrawable( - getContext(), v, "selectors_selected_drawable"); //$NON-NLS-1$ - } else { - theme.setBackgroundDrawable( - getContext(), v, "selectors_deselected_drawable"); //$NON-NLS-1$ + if (viewHolder.mHasSelectedBg == null + || viewHolder.mHasSelectedBg != dataHolder.mSelected) { + String drawableId = dataHolder.mSelected + ? "selectors_selected_drawable" //$NON-NLS-1$ + : "selectors_deselected_drawable"; //$NON-NLS-1$ + + theme.setBackgroundDrawable(getContext(), v, drawableId); + viewHolder.mHasSelectedBg = dataHolder.mSelected; } } @@ -380,20 +388,6 @@ public class FileSystemObjectAdapter getContext(), "checkbox_deselected_drawable"); //$NON-NLS-1$ } - if (v != null) { - ((ImageView)v).setImageDrawable(data.mDwCheck); - if (data.mSelected) { - theme.setBackgroundDrawable( - getContext(), - (View)v.getParent(), - "selectors_selected_drawable"); //$NON-NLS-1$ - } else { - theme.setBackgroundDrawable( - getContext(), - (View)v.getParent(), - "selectors_deselected_drawable"); //$NON-NLS-1$ - } - } //Add or remove from the global selected items final List selectedItems = diff --git a/src/com/cyanogenmod/filemanager/model/FileSystemObject.java b/src/com/cyanogenmod/filemanager/model/FileSystemObject.java index 397b0f1..99110a7 100644 --- a/src/com/cyanogenmod/filemanager/model/FileSystemObject.java +++ b/src/com/cyanogenmod/filemanager/model/FileSystemObject.java @@ -348,9 +348,8 @@ public abstract class FileSystemObject implements Serializable, ComparablePermission. * @@ -83,6 +85,7 @@ public abstract class Permission implements Serializable { */ public void setRead(boolean read) { this.mRead = read; + invalidateRawString(); } /** @@ -101,6 +104,7 @@ public abstract class Permission implements Serializable { */ public void setWrite(boolean write) { this.mWrite = write; + invalidateRawString(); } /** @@ -119,6 +123,7 @@ public abstract class Permission implements Serializable { */ public void setExecute(boolean execute) { this.mExecute = execute; + invalidateRawString(); } /** @@ -176,6 +181,15 @@ public abstract class Permission implements Serializable { * * @return String The string representation of the permissions */ - public abstract String toRawString(); + public String toRawString() { + if (mRawString == null) { + mRawString = getRawString(); + } + return mRawString; + } + protected void invalidateRawString() { + mRawString = null; + } + protected abstract String getRawString(); } diff --git a/src/com/cyanogenmod/filemanager/model/Permissions.java b/src/com/cyanogenmod/filemanager/model/Permissions.java index cad6a05..9500086 100644 --- a/src/com/cyanogenmod/filemanager/model/Permissions.java +++ b/src/com/cyanogenmod/filemanager/model/Permissions.java @@ -182,10 +182,9 @@ public class Permissions implements Serializable, Comparable { * @return String The string representation of the permissions */ public String toRawString() { - return String.format("%s%s%s", //$NON-NLS-1$ - this.mUser.toRawString(), - this.mGroup.toRawString(), - this.mOthers.toRawString()); + return this.mUser.toRawString() + + this.mGroup.toRawString() + + this.mOthers.toRawString(); } /** diff --git a/src/com/cyanogenmod/filemanager/model/UserPermission.java b/src/com/cyanogenmod/filemanager/model/UserPermission.java index ced812c..4003259 100644 --- a/src/com/cyanogenmod/filemanager/model/UserPermission.java +++ b/src/com/cyanogenmod/filemanager/model/UserPermission.java @@ -77,6 +77,7 @@ public class UserPermission extends Permission { */ public void setSetUID(boolean setuid) { this.mSetuid = setuid; + invalidateRawString(); } /** @@ -124,7 +125,7 @@ public class UserPermission extends Permission { * {@inheritDoc} */ @Override - public String toRawString() { + protected String getRawString() { StringBuilder p = new StringBuilder(); p.append(isRead() ? READ : UNASIGNED); p.append(isWrite() ? WRITE : UNASIGNED); diff --git a/src/com/cyanogenmod/filemanager/ui/ThemeManager.java b/src/com/cyanogenmod/filemanager/ui/ThemeManager.java index d1fd30d..802ad1e 100644 --- a/src/com/cyanogenmod/filemanager/ui/ThemeManager.java +++ b/src/com/cyanogenmod/filemanager/ui/ThemeManager.java @@ -354,11 +354,7 @@ public final class ThemeManager { public Drawable getPreviewImage(Context ctx) { String resId = "theme_preview_drawable"; //$NON-NLS-1$ if (this.compareTo(ThemeManager.getDefaultTheme(ctx)) != 0) { - resId = - String.format( - "%s_%s", //$NON-NLS-1$ - this.mId, - "theme_preview_drawable"); //$NON-NLS-1$ + resId = mId + "_theme_preview_drawable"; //$NON-NLS-1$ } int id = this.mResources.getIdentifier(resId, "drawable", this.mPackage); //$NON-NLS-1$ if (id != 0) { @@ -374,11 +370,7 @@ public final class ThemeManager { * @return Drawable The drawable */ public Drawable getNoPreviewImage(Context ctx) { - String resId = - String.format( - "%s_%s", //$NON-NLS-1$ - this.mId, - "theme_no_preview_drawable"); //$NON-NLS-1$ + String resId = mId + "_theme_no_preview_drawable"; //$NON-NLS-1$ int id = this.mResources.getIdentifier(resId, "drawable", this.mPackage); //$NON-NLS-1$ if (id != 0) { return this.mResources.getDrawable(id); @@ -399,8 +391,7 @@ public final class ThemeManager { * @param overlay Indicates if the theme should be the overlay one */ public void setBaseTheme(Context ctx, boolean overlay) { - String resId = - String.format("%s_%s", this.mId, "base_theme"); //$NON-NLS-1$ //$NON-NLS-2$ + String resId = mId + "_base_theme"; //$NON-NLS-1$ int id = this.mResources.getIdentifier(resId, "string", this.mPackage); //$NON-NLS-1$ if (id != 0) { String base = this.mResources.getString(id, "holo_light"); //$NON-NLS-1$ @@ -439,7 +430,7 @@ public final class ThemeManager { * @param resource The string resource */ public void setTitlebarDrawable(Context ctx, ActionBar actionBar, String resource) { - String resId = String.format("%s_%s", this.mId, resource); //$NON-NLS-1$ + String resId = this.mId + "_" + resource; //$NON-NLS-1$ int id = this.mResources.getIdentifier(resId, "drawable", this.mPackage); //$NON-NLS-1$ if (id != 0) { actionBar.setBackgroundDrawable(this.mResources.getDrawable(id)); @@ -460,7 +451,7 @@ public final class ThemeManager { * @param resource The string resource */ public void setBackgroundDrawable(Context ctx, View view, String resource) { - String resId = String.format("%s_%s", this.mId, resource); //$NON-NLS-1$ + String resId = mId + "_" + resource; //$NON-NLS-1$ int id = this.mResources.getIdentifier(resId, "drawable", this.mPackage); //$NON-NLS-1$ if (id != 0) { view.setBackground(this.mResources.getDrawable(id)); @@ -481,7 +472,7 @@ public final class ThemeManager { * @param resource The string resource */ public void setImageDrawable(Context ctx, ImageView view, String resource) { - String resId = String.format("%s_%s", this.mId, resource); //$NON-NLS-1$ + String resId = mId + "_" + resource; //$NON-NLS-1$ int id = this.mResources.getIdentifier(resId, "drawable", this.mPackage); //$NON-NLS-1$ if (id != 0) { view.setImageDrawable(this.mResources.getDrawable(id)); @@ -502,7 +493,7 @@ public final class ThemeManager { * @return Drawable The drawable */ public Drawable getDrawable(Context ctx, String resource) { - String resId = String.format("%s_%s", this.mId, resource); //$NON-NLS-1$ + String resId = mId + "_" + resource; //$NON-NLS-1$ int id = this.mResources.getIdentifier(resId, "drawable", this.mPackage); //$NON-NLS-1$ if (id != 0) { return this.mResources.getDrawable(id); @@ -522,7 +513,7 @@ public final class ThemeManager { * @param resource The string resource */ public void setTextColor(Context ctx, TextView view, String resource) { - String resId = String.format("%s_%s", this.mId, resource); //$NON-NLS-1$ + String resId = mId + "_" + resource; //$NON-NLS-1$ int id = this.mResources.getIdentifier(resId, "color", this.mPackage); //$NON-NLS-1$ if (id != 0) { view.setTextColor(this.mResources.getColor(id)); @@ -543,7 +534,7 @@ public final class ThemeManager { * @return int The color reference */ public int getColor(Context ctx, String resource) { - String resId = String.format("%s_%s", this.mId, resource); //$NON-NLS-1$ + String resId = mId + "_" + resource; //$NON-NLS-1$ int id = this.mResources.getIdentifier(resId, "color", this.mPackage); //$NON-NLS-1$ if (id != 0) { return this.mResources.getColor(id); @@ -563,7 +554,7 @@ public final class ThemeManager { * @param resource The string resource */ public void setBackgroundColor(Context ctx, View view, String resource) { - String resId = String.format("%s_%s", this.mId, resource); //$NON-NLS-1$ + String resId = mId + "_" + resource; //$NON-NLS-1$ int id = this.mResources.getIdentifier(resId, "color", this.mPackage); //$NON-NLS-1$ if (id != 0) { view.setBackgroundColor(this.mResources.getColor(id)); diff --git a/src/com/cyanogenmod/filemanager/ui/widgets/NavigationView.java b/src/com/cyanogenmod/filemanager/ui/widgets/NavigationView.java index 58602a8..f17c645 100644 --- a/src/com/cyanogenmod/filemanager/ui/widgets/NavigationView.java +++ b/src/com/cyanogenmod/filemanager/ui/widgets/NavigationView.java @@ -683,7 +683,6 @@ public class NavigationView extends RelativeLayout implements } this.mFiles = files; adapter.addAll(files); - adapter.notifyDataSetChanged(); //Set the adapter this.mAdapter = adapter; @@ -720,7 +719,6 @@ public class NavigationView extends RelativeLayout implements * @param fso The file system object */ public void removeItem(FileSystemObject fso) { - this.mAdapter.remove(fso); // Delete also from internal list if (fso != null) { int cc = this.mFiles.size()-1; @@ -732,7 +730,7 @@ public class NavigationView extends RelativeLayout implements } } } - this.mAdapter.notifyDataSetChanged(); + this.mAdapter.remove(fso); } /** @@ -744,7 +742,6 @@ public class NavigationView extends RelativeLayout implements FileSystemObject fso = this.mAdapter.getItem(path); if (fso != null) { this.mAdapter.remove(fso); - this.mAdapter.notifyDataSetChanged(); } } @@ -1084,6 +1081,7 @@ public class NavigationView extends RelativeLayout implements final AdapterView view = (AdapterView)findViewById(RESOURCE_CURRENT_LAYOUT); FileSystemObjectAdapter adapter = (FileSystemObjectAdapter)view.getAdapter(); + adapter.setNotifyOnChange(false); adapter.clear(); adapter.addAll(files); adapter.notifyDataSetChanged(); diff --git a/src/com/cyanogenmod/filemanager/util/FileHelper.java b/src/com/cyanogenmod/filemanager/util/FileHelper.java index 8687e09..811225a 100644 --- a/src/com/cyanogenmod/filemanager/util/FileHelper.java +++ b/src/com/cyanogenmod/filemanager/util/FileHelper.java @@ -184,7 +184,6 @@ public final class FileHelper { */ public static String getHumanReadableSize(long size) { Resources res = FileManagerApplication.getInstance().getResources(); - final String format = "%d %s"; //$NON-NLS-1$ final int[] magnitude = { R.string.size_bytes, R.string.size_kilobytes, @@ -197,12 +196,11 @@ public final class FileHelper { for (int i = 0; i < cc; i++) { long s = aux / 1024; if (aux < 1024) { - return String.format(format, Long.valueOf(aux), res.getString(magnitude[i])); + return Long.toString(aux) + " " + res.getString(magnitude[i]); //$NON-NLS-1$ } aux = s; } - return String.format( - format, Long.valueOf(aux), res.getString(magnitude[magnitude.length - 1])); + return Long.toString(aux) + " " + res.getString(magnitude[cc - 1]); //$NON-NLS-1$ } /** @@ -828,7 +826,7 @@ public final class FileHelper { if (ext == null) { ext = ""; //$NON-NLS-1$ } else { - ext = String.format(".%s", ext); //$NON-NLS-1$ + ext = "." + ext; //$NON-NLS-1$ } newName = ctx.getString(regexp, name, ext); } while (isNameExists(files, newName)); @@ -1312,14 +1310,12 @@ public final class FileHelper { } // Apply the user settings - String formatted = "-"; //$NON-NLS-1$ if (sFiletimeFormatMode.compareTo(FileTimeFormatMode.SYSTEM) == 0) { String date = sDateFormat.format(filetime); String time = sTimeFormat.format(filetime); - formatted = String.format(sDateTimeFormatOrder, date, time); + return String.format(sDateTimeFormatOrder, date, time); } else { - formatted = sDateFormat.format(filetime); + return sDateFormat.format(filetime); } - return formatted; } } -- 2.11.0