diff options
author | nicolasroard <nicolasroard@google.com> | 2013-07-15 11:24:36 -0700 |
---|---|---|
committer | nicolasroard <nicolasroard@google.com> | 2013-07-30 11:28:59 -0700 |
commit | 13ae2608e04998f0d13bc3f58cda5237bdde0b7d (patch) | |
tree | a1bbb351775e1dfe26e8feb60aae123e3b84e51a /src/com/android/gallery3d | |
parent | 7d1433ac9191fa7b7c79a0187d4d0f30c16f84d9 (diff) | |
download | android_packages_apps_Snap-13ae2608e04998f0d13bc3f58cda5237bdde0b7d.tar.gz android_packages_apps_Snap-13ae2608e04998f0d13bc3f58cda5237bdde0b7d.tar.bz2 android_packages_apps_Snap-13ae2608e04998f0d13bc3f58cda5237bdde0b7d.zip |
Add user presets saving/loading
Change-Id: I85c5dd0df1e9264a01c8d7a6d44602e87799890c
Diffstat (limited to 'src/com/android/gallery3d')
12 files changed, 658 insertions, 20 deletions
diff --git a/src/com/android/gallery3d/filtershow/FilterShowActivity.java b/src/com/android/gallery3d/filtershow/FilterShowActivity.java index 8bbdd0c32..b06010f61 100644 --- a/src/com/android/gallery3d/filtershow/FilterShowActivity.java +++ b/src/com/android/gallery3d/filtershow/FilterShowActivity.java @@ -36,6 +36,7 @@ import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; +import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTransaction; @@ -59,6 +60,9 @@ import com.android.gallery3d.R; import com.android.gallery3d.app.PhotoPage; import com.android.gallery3d.data.LocalAlbum; import com.android.gallery3d.filtershow.editors.EditorGrad; +import com.android.gallery3d.filtershow.data.FilterStackSource; +import com.android.gallery3d.filtershow.data.UserPresetsManager; +import com.android.gallery3d.filtershow.filters.FilterUserPresetRepresentation; import com.android.gallery3d.filtershow.pipeline.CachingPipeline; import com.android.gallery3d.filtershow.cache.ImageLoader; import com.android.gallery3d.filtershow.category.Action; @@ -88,6 +92,8 @@ import com.android.gallery3d.filtershow.imageshow.MasterImage; import com.android.gallery3d.filtershow.imageshow.Spline; import com.android.gallery3d.filtershow.pipeline.ImagePreset; import com.android.gallery3d.filtershow.pipeline.ProcessingService; +import com.android.gallery3d.filtershow.presets.PresetManagementDialog; +import com.android.gallery3d.filtershow.presets.UserPresetsAdapter; import com.android.gallery3d.filtershow.provider.SharedImageProvider; import com.android.gallery3d.filtershow.state.StateAdapter; import com.android.gallery3d.filtershow.tools.SaveImage; @@ -143,6 +149,8 @@ public class FilterShowActivity extends FragmentActivity implements OnItemClickL private Uri mSelectedImageUri = null; + private UserPresetsManager mUserPresetsManager = null; + private UserPresetsAdapter mUserPresetsAdapter = null; private CategoryAdapter mCategoryLooksAdapter = null; private CategoryAdapter mCategoryBordersAdapter = null; private CategoryAdapter mCategoryGeometryAdapter = null; @@ -208,6 +216,9 @@ public class FilterShowActivity extends FragmentActivity implements OnItemClickL private void setupPipeline() { doBindService(); ImageFilter.setActivityForMemoryToasts(this); + mUserPresetsManager = new UserPresetsManager(this); + mUserPresetsAdapter = new UserPresetsAdapter(this); + mCategoryLooksAdapter = new CategoryAdapter(this); } public void updateUIAfterServiceStarted() { @@ -321,6 +332,7 @@ public class FilterShowActivity extends FragmentActivity implements OnItemClickL public void fillCategories() { fillLooks(); + loadUserPresets(); fillBorders(); fillTools(); fillEffects(); @@ -439,6 +451,10 @@ public class FilterShowActivity extends FragmentActivity implements OnItemClickL } } + public UserPresetsAdapter getUserPresetsAdapter() { + return mUserPresetsAdapter; + } + public CategoryAdapter getCategoryLooksAdapter() { return mCategoryLooksAdapter; } @@ -662,6 +678,7 @@ public class FilterShowActivity extends FragmentActivity implements OnItemClickL if (mLoadBitmapTask != null) { mLoadBitmapTask.cancel(false); } + mUserPresetsManager.close(); doUnbindService(); super.onDestroy(); } @@ -829,10 +846,70 @@ public class FilterShowActivity extends FragmentActivity implements OnItemClickL saveImage(); return true; } + case R.id.manageUserPresets: { + manageUserPresets(); + return true; + } } return false; } + private void manageUserPresets() { + DialogFragment dialog = new PresetManagementDialog(); + dialog.show(getSupportFragmentManager(), "NoticeDialogFragment"); + } + + public void updateUserPresetsFromAdapter(UserPresetsAdapter adapter) { + ArrayList<FilterUserPresetRepresentation> representations = + adapter.getDeletedRepresentations(); + for (FilterUserPresetRepresentation representation : representations) { + deletePreset(representation.getId()); + } + ArrayList<FilterUserPresetRepresentation> changedRepresentations = + adapter.getChangedRepresentations(); + for (FilterUserPresetRepresentation representation : changedRepresentations) { + updatePreset(representation); + } + adapter.clearDeletedRepresentations(); + adapter.clearChangedRepresentations(); + loadUserPresets(); + } + + public void loadUserPresets() { + mUserPresetsManager.load(); + } + + public void updateUserPresetsFromManager() { + ArrayList<FilterUserPresetRepresentation> presets = mUserPresetsManager.getRepresentations(); + if (presets == null) { + return; + } + if (mCategoryLooksAdapter != null) { + fillLooks(); + } + mUserPresetsAdapter.clear(); + for (int i = 0; i < presets.size(); i++) { + FilterUserPresetRepresentation representation = presets.get(i); + mCategoryLooksAdapter.add( + new Action(this, representation, Action.FULL_VIEW)); + mUserPresetsAdapter.add(new Action(this, representation, Action.FULL_VIEW)); + } + mCategoryLooksAdapter.notifyDataSetInvalidated(); + + } + + public void saveCurrentImagePreset() { + mUserPresetsManager.save(MasterImage.getImage().getPreset()); + } + + private void deletePreset(int id) { + mUserPresetsManager.delete(id); + } + + private void updatePreset(FilterUserPresetRepresentation representation) { + mUserPresetsManager.update(representation); + } + public void enableSave(boolean enable) { if (mSaveButton != null) { mSaveButton.setEnabled(enable); @@ -843,7 +920,7 @@ public class FilterShowActivity extends FragmentActivity implements OnItemClickL FiltersManager filtersManager = FiltersManager.getManager(); ArrayList<FilterRepresentation> filtersRepresentations = filtersManager.getLooks(); - mCategoryLooksAdapter = new CategoryAdapter(this); + mCategoryLooksAdapter.clear(); int verticalItemHeight = (int) getResources().getDimension(R.dimen.action_item_height); mCategoryLooksAdapter.setItemHeight(verticalItemHeight); for (FilterRepresentation representation : filtersRepresentations) { diff --git a/src/com/android/gallery3d/filtershow/category/Action.java b/src/com/android/gallery3d/filtershow/category/Action.java index de282cc28..332ca18b0 100644 --- a/src/com/android/gallery3d/filtershow/category/Action.java +++ b/src/com/android/gallery3d/filtershow/category/Action.java @@ -24,6 +24,9 @@ import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.RectF; +import android.widget.ArrayAdapter; +import android.widget.ListAdapter; +import com.android.gallery3d.filtershow.filters.FilterUserPresetRepresentation; import com.android.gallery3d.filtershow.pipeline.RenderingRequest; import com.android.gallery3d.filtershow.pipeline.RenderingRequestCaller; import com.android.gallery3d.filtershow.filters.FilterRepresentation; @@ -37,7 +40,7 @@ public class Action implements RenderingRequestCaller { private String mName; private Rect mImageFrame; private Bitmap mImage; - private CategoryAdapter mAdapter; + private ArrayAdapter mAdapter; public static final int FULL_VIEW = 0; public static final int CROP_VIEW = 1; private int mType = CROP_VIEW; @@ -100,7 +103,7 @@ public class Action implements RenderingRequestCaller { mImage = image; } - public void setAdapter(CategoryAdapter adapter) { + public void setAdapter(ArrayAdapter adapter) { mAdapter = adapter; } diff --git a/src/com/android/gallery3d/filtershow/category/CategoryAdapter.java b/src/com/android/gallery3d/filtershow/category/CategoryAdapter.java index 445ef5f6a..6451c39df 100644 --- a/src/com/android/gallery3d/filtershow/category/CategoryAdapter.java +++ b/src/com/android/gallery3d/filtershow/category/CategoryAdapter.java @@ -149,6 +149,9 @@ public class CategoryAdapter extends ArrayAdapter<Action> { } public void reflectImagePreset(ImagePreset preset) { + if (preset == null) { + return; + } int selected = 0; // if nothing found, select "none" (first element) FilterRepresentation rep = null; if (mCategory == MainPanel.LOOKS) { diff --git a/src/com/android/gallery3d/filtershow/category/CategoryTrack.java b/src/com/android/gallery3d/filtershow/category/CategoryTrack.java index f55431293..ac8245a3b 100644 --- a/src/com/android/gallery3d/filtershow/category/CategoryTrack.java +++ b/src/com/android/gallery3d/filtershow/category/CategoryTrack.java @@ -35,6 +35,11 @@ public class CategoryTrack extends LinearLayout { super.onChanged(); invalidate(); } + @Override + public void onInvalidated() { + super.onInvalidated(); + fillContent(); + } }; public CategoryTrack(Context context, AttributeSet attrs) { @@ -45,14 +50,14 @@ public class CategoryTrack extends LinearLayout { public void setAdapter(CategoryAdapter adapter) { mAdapter = adapter; - mAdapter.setItemWidth(mElemSize); - mAdapter.setItemHeight(LayoutParams.MATCH_PARENT); mAdapter.registerDataSetObserver(mDataSetObserver); fillContent(); } public void fillContent() { removeAllViews(); + mAdapter.setItemWidth(mElemSize); + mAdapter.setItemHeight(LayoutParams.MATCH_PARENT); int n = mAdapter.getCount(); for (int i = 0; i < n; i++) { View view = mAdapter.getView(i, null, this); diff --git a/src/com/android/gallery3d/filtershow/data/FilterStackSource.java b/src/com/android/gallery3d/filtershow/data/FilterStackSource.java index 4e343777d..d283771b4 100644 --- a/src/com/android/gallery3d/filtershow/data/FilterStackSource.java +++ b/src/com/android/gallery3d/filtershow/data/FilterStackSource.java @@ -24,6 +24,8 @@ import android.util.Log; import android.util.Pair; import com.android.gallery3d.filtershow.data.FilterStackDBHelper.FilterStack; +import com.android.gallery3d.filtershow.filters.FilterUserPresetRepresentation; +import com.android.gallery3d.filtershow.pipeline.ImagePreset; import java.util.ArrayList; import java.util.List; @@ -31,7 +33,7 @@ import java.util.List; public class FilterStackSource { private static final String LOGTAG = "FilterStackSource"; - private SQLiteDatabase database = null;; + private SQLiteDatabase database = null; private final FilterStackDBHelper dbHelper; public FilterStackSource(Context context) { @@ -66,12 +68,25 @@ public class FilterStackSource { return ret; } - public boolean removeStack(String stackName) { + public void updateStackName(int id, String stackName) { + ContentValues val = new ContentValues(); + val.put(FilterStack.STACK_ID, stackName); + database.beginTransaction(); + try { + database.update(FilterStack.TABLE, val, FilterStack._ID + " = ?", + new String[] { "" + id}); + database.setTransactionSuccessful(); + } finally { + database.endTransaction(); + } + } + + public boolean removeStack(int id) { boolean ret = true; database.beginTransaction(); try { - ret = (0 != database.delete(FilterStack.TABLE, FilterStack.STACK_ID + " = ?", - new String[] { stackName})); + ret = (0 != database.delete(FilterStack.TABLE, FilterStack._ID + " = ?", + new String[] { "" + id })); database.setTransactionSuccessful(); } finally { database.endTransaction(); @@ -111,6 +126,45 @@ public class FilterStackSource { return ret; } + public ArrayList<FilterUserPresetRepresentation> getAllUserPresets() { + ArrayList<FilterUserPresetRepresentation> ret = + new ArrayList<FilterUserPresetRepresentation>(); + + Cursor c = null; + database.beginTransaction(); + try { + c = database.query(FilterStack.TABLE, + new String[] { FilterStack._ID, + FilterStack.STACK_ID, + FilterStack.FILTER_STACK }, + null, null, null, null, null, null); + if (c != null) { + boolean loopCheck = c.moveToFirst(); + while (loopCheck) { + int id = c.getInt(0); + String name = (c.isNull(1)) ? null : c.getString(1); + byte[] b = (c.isNull(2)) ? null : c.getBlob(2); + String json = new String(b); + + ImagePreset preset = new ImagePreset(); + preset.readJsonFromString(json); + FilterUserPresetRepresentation representation = + new FilterUserPresetRepresentation(name, preset, id); + ret.add(representation); + loopCheck = c.moveToNext(); + } + } + database.setTransactionSuccessful(); + } finally { + if (c != null) { + c.close(); + } + database.endTransaction(); + } + + return ret; + } + public List<Pair<String, byte[]>> getAllStacks() { List<Pair<String, byte[]>> ret = new ArrayList<Pair<String, byte[]>>(); Cursor c = null; diff --git a/src/com/android/gallery3d/filtershow/data/UserPresetsManager.java b/src/com/android/gallery3d/filtershow/data/UserPresetsManager.java new file mode 100644 index 000000000..114cd3ebc --- /dev/null +++ b/src/com/android/gallery3d/filtershow/data/UserPresetsManager.java @@ -0,0 +1,149 @@ +package com.android.gallery3d.filtershow.data; + +import android.os.Handler; +import android.os.HandlerThread; +import android.os.Message; +import com.android.gallery3d.R; +import com.android.gallery3d.filtershow.FilterShowActivity; +import com.android.gallery3d.filtershow.filters.FilterUserPresetRepresentation; +import com.android.gallery3d.filtershow.pipeline.ImagePreset; + +import java.util.ArrayList; + +public class UserPresetsManager implements Handler.Callback { + + private static final String LOGTAG = "UserPresetsManager"; + + private FilterShowActivity mActivity; + private HandlerThread mHandlerThread = null; + private Handler mProcessingHandler = null; + private FilterStackSource mUserPresets; + + private static final int LOAD = 1; + private static final int LOAD_RESULT = 2; + private static final int SAVE = 3; + private static final int DELETE = 4; + private static final int UPDATE = 5; + + private ArrayList<FilterUserPresetRepresentation> mRepresentations; + + private final Handler mResultHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + switch (msg.what) { + case LOAD_RESULT: + resultLoad(msg); + break; + } + } + }; + + @Override + public boolean handleMessage(Message msg) { + switch (msg.what) { + case LOAD: + processLoad(); + return true; + case SAVE: + processSave(msg); + return true; + case DELETE: + processDelete(msg); + return true; + case UPDATE: + processUpdate(msg); + return true; + } + return false; + } + + public UserPresetsManager(FilterShowActivity context) { + mActivity = context; + mHandlerThread = new HandlerThread(LOGTAG, + android.os.Process.THREAD_PRIORITY_BACKGROUND); + mHandlerThread.start(); + mProcessingHandler = new Handler(mHandlerThread.getLooper(), this); + mUserPresets = new FilterStackSource(mActivity); + mUserPresets.open(); + } + + public ArrayList<FilterUserPresetRepresentation> getRepresentations() { + return mRepresentations; + } + + public void load() { + Message msg = mProcessingHandler.obtainMessage(LOAD); + mProcessingHandler.sendMessage(msg); + } + + public void close() { + mUserPresets.close(); + mHandlerThread.quit(); + } + + static class SaveOperation { + String json; + String name; + } + + public void save(ImagePreset preset) { + Message msg = mProcessingHandler.obtainMessage(SAVE); + SaveOperation op = new SaveOperation(); + op.json = preset.getJsonString(mActivity.getString(R.string.saved)); + op.name= mActivity.getString(R.string.filtershow_new_preset); + msg.obj = op; + mProcessingHandler.sendMessage(msg); + } + + public void delete(int id) { + Message msg = mProcessingHandler.obtainMessage(DELETE); + msg.arg1 = id; + mProcessingHandler.sendMessage(msg); + } + + static class UpdateOperation { + int id; + String name; + } + + public void update(FilterUserPresetRepresentation representation) { + Message msg = mProcessingHandler.obtainMessage(UPDATE); + UpdateOperation op = new UpdateOperation(); + op.id = representation.getId(); + op.name = representation.getName(); + msg.obj = op; + mProcessingHandler.sendMessage(msg); + } + + private void processLoad() { + ArrayList<FilterUserPresetRepresentation> list = mUserPresets.getAllUserPresets(); + Message msg = mResultHandler.obtainMessage(LOAD_RESULT); + msg.obj = list; + mResultHandler.sendMessage(msg); + } + + private void resultLoad(Message msg) { + mRepresentations = + (ArrayList<FilterUserPresetRepresentation>) msg.obj; + mActivity.updateUserPresetsFromManager(); + } + + private void processSave(Message msg) { + SaveOperation op = (SaveOperation) msg.obj; + mUserPresets.insertStack(op.name, op.json.getBytes()); + processLoad(); + } + + private void processDelete(Message msg) { + int id = msg.arg1; + mUserPresets.removeStack(id); + processLoad(); + } + + private void processUpdate(Message msg) { + UpdateOperation op = (UpdateOperation) msg.obj; + mUserPresets.updateStackName(op.id, op.name); + processLoad(); + } + +} diff --git a/src/com/android/gallery3d/filtershow/filters/BaseFiltersManager.java b/src/com/android/gallery3d/filtershow/filters/BaseFiltersManager.java index 550b9d7fc..2205b4dbb 100644 --- a/src/com/android/gallery3d/filtershow/filters/BaseFiltersManager.java +++ b/src/com/android/gallery3d/filtershow/filters/BaseFiltersManager.java @@ -208,7 +208,11 @@ public abstract class BaseFiltersManager implements FiltersManagerInterface { FilterFxRepresentation fx = new FilterFxRepresentation( context.getString(fxNameid[i]), drawid[i], fxNameid[i]); fx.setSerializationName(serializationNames[i]); - mLooks.add(fx); + ImagePreset preset = new ImagePreset(); + preset.addFilter(fx); + FilterUserPresetRepresentation rep = new FilterUserPresetRepresentation( + context.getString(fxNameid[i]), preset, -1); + mLooks.add(rep); addRepresentation(fx); } } diff --git a/src/com/android/gallery3d/filtershow/filters/FilterUserPresetRepresentation.java b/src/com/android/gallery3d/filtershow/filters/FilterUserPresetRepresentation.java new file mode 100644 index 000000000..dfdb6fcf0 --- /dev/null +++ b/src/com/android/gallery3d/filtershow/filters/FilterUserPresetRepresentation.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * 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. + */ + +package com.android.gallery3d.filtershow.filters; + +import com.android.gallery3d.filtershow.editors.ImageOnlyEditor; +import com.android.gallery3d.filtershow.pipeline.ImagePreset; + +public class FilterUserPresetRepresentation extends FilterRepresentation { + + private ImagePreset mPreset; + private int mId; + + public FilterUserPresetRepresentation(String name, ImagePreset preset, int id) { + super(name); + setEditorId(ImageOnlyEditor.ID); + setFilterType(FilterRepresentation.TYPE_FX); + mPreset = preset; + mId = id; + } + + public ImagePreset getImagePreset() { + return mPreset; + } + + public int getId() { + return mId; + } + + public FilterRepresentation copy(){ + FilterRepresentation representation = new FilterUserPresetRepresentation(getName(), + new ImagePreset(mPreset), mId); + return representation; + } + + @Override + public boolean allowsSingleInstanceOnly() { + return true; + } +} diff --git a/src/com/android/gallery3d/filtershow/pipeline/FilterEnvironment.java b/src/com/android/gallery3d/filtershow/pipeline/FilterEnvironment.java index f97dc757a..29608bfbd 100644 --- a/src/com/android/gallery3d/filtershow/pipeline/FilterEnvironment.java +++ b/src/com/android/gallery3d/filtershow/pipeline/FilterEnvironment.java @@ -20,6 +20,7 @@ import android.graphics.Bitmap; import android.support.v8.renderscript.Allocation; import com.android.gallery3d.filtershow.filters.FilterRepresentation; +import com.android.gallery3d.filtershow.filters.FilterUserPresetRepresentation; import com.android.gallery3d.filtershow.filters.FiltersManagerInterface; import com.android.gallery3d.filtershow.filters.ImageFilter; @@ -128,6 +129,12 @@ public class FilterEnvironment { } public Bitmap applyRepresentation(FilterRepresentation representation, Bitmap bitmap) { + if (representation instanceof FilterUserPresetRepresentation) { + // we allow instances of FilterUserPresetRepresentation in a preset only to know if one + // has been applied (so we can show this in the UI). But as all the filters in them are + // applied directly they do not themselves need to do any kind of filtering. + return bitmap; + } ImageFilter filter = mFiltersManager.getFilterForRepresentation(representation); filter.useRepresentation(representation); filter.setEnvironment(this); diff --git a/src/com/android/gallery3d/filtershow/pipeline/ImagePreset.java b/src/com/android/gallery3d/filtershow/pipeline/ImagePreset.java index 5a0857bce..28ae2694e 100644 --- a/src/com/android/gallery3d/filtershow/pipeline/ImagePreset.java +++ b/src/com/android/gallery3d/filtershow/pipeline/ImagePreset.java @@ -33,6 +33,7 @@ import com.android.gallery3d.filtershow.filters.FilterMirrorRepresentation; import com.android.gallery3d.filtershow.filters.FilterRepresentation; import com.android.gallery3d.filtershow.filters.FilterRotateRepresentation; import com.android.gallery3d.filtershow.filters.FilterStraightenRepresentation; +import com.android.gallery3d.filtershow.filters.FilterUserPresetRepresentation; import com.android.gallery3d.filtershow.filters.FiltersManager; import com.android.gallery3d.filtershow.filters.ImageFilter; import com.android.gallery3d.filtershow.imageshow.GeometryMetadata; @@ -52,8 +53,6 @@ public class ImagePreset { private Vector<FilterRepresentation> mFilters = new Vector<FilterRepresentation>(); - protected boolean mIsFxPreset = false; - private boolean mDoApplyGeometry = true; private boolean mDoApplyFilters = true; @@ -65,7 +64,6 @@ public class ImagePreset { } public ImagePreset(ImagePreset source) { - for (int i = 0; i < source.mFilters.size(); i++) { FilterRepresentation representation = null; FilterRepresentation sourceRepresentation = source.mFilters.elementAt(i); @@ -77,9 +75,8 @@ public class ImagePreset { } else { representation = sourceRepresentation.copy(); } - addFilter(representation); + mFilters.add(representation); } - } public FilterRepresentation getFilterRepresentation(int position) { @@ -340,7 +337,21 @@ public class ImagePreset { setGeometry((GeometryMetadata) representation); return; } - + if (representation instanceof FilterUserPresetRepresentation) { + ImagePreset preset = ((FilterUserPresetRepresentation) representation).getImagePreset(); + // user preset replace everything but geometry + GeometryMetadata geometry = getGeometry(); + mFilters.clear(); + mFilters.add(geometry); + for (int i = 0; i < preset.nbFilters(); i++) { + FilterRepresentation rep = preset.getFilterRepresentation(i); + if (!(representation instanceof GeometryMetadata)) { + addFilter(rep); + } + } + mFilters.add(representation); + return; + } if (representation.getFilterType() == FilterRepresentation.TYPE_BORDER) { removeFilter(representation); if (!isNoneBorderFilter(representation)) { @@ -349,7 +360,8 @@ public class ImagePreset { } else if (representation.getFilterType() == FilterRepresentation.TYPE_FX) { boolean found = false; for (int i = 0; i < mFilters.size(); i++) { - int type = mFilters.elementAt(i).getFilterType(); + FilterRepresentation current = mFilters.elementAt(i); + int type = current.getFilterType(); if (found) { if (type != FilterRepresentation.TYPE_VIGNETTE) { mFilters.remove(i); @@ -357,9 +369,33 @@ public class ImagePreset { } } if (type == FilterRepresentation.TYPE_FX) { - mFilters.remove(i); - if (!isNoneFxFilter(representation)) { - mFilters.add(i, representation); + if (current instanceof FilterUserPresetRepresentation) { + ImagePreset preset = ((FilterUserPresetRepresentation) current) + .getImagePreset(); + // If we had an existing user preset, let's remove all the presets that + // were added by it + for (int j = 0; j < preset.nbFilters(); j++) { + FilterRepresentation rep = preset.getFilterRepresentation(j); + int pos = getPositionForRepresentation(rep); + if (pos != -1) { + mFilters.remove(pos); + } + } + int pos = getPositionForRepresentation(current); + if (pos != -1) { + mFilters.remove(pos); + } else { + pos = 0; + } + if (!isNoneFxFilter(representation)) { + mFilters.add(pos, representation); + } + + } else { + mFilters.remove(i); + if (!isNoneFxFilter(representation)) { + mFilters.add(i, representation); + } } found = true; } @@ -535,6 +571,10 @@ public class ImagePreset { // TODO: supports Geometry representations in the state panel. continue; } + if (filter instanceof FilterUserPresetRepresentation) { + // do not show the user preset itself in the state panel + continue; + } State state = new State(filter.getName()); state.setFilterRepresentation(filter); states.add(state); @@ -583,6 +623,9 @@ public class ImagePreset { writer.beginObject(); for (int i = 0; i < numFilters; i++) { FilterRepresentation filter = mFilters.get(i); + if (filter instanceof FilterUserPresetRepresentation) { + continue; + } String sname = filter.getSerializationName(); if (DEBUG) { Log.v(LOGTAG, "Serialization: " + sname); diff --git a/src/com/android/gallery3d/filtershow/presets/PresetManagementDialog.java b/src/com/android/gallery3d/filtershow/presets/PresetManagementDialog.java new file mode 100644 index 000000000..7ab61fcc9 --- /dev/null +++ b/src/com/android/gallery3d/filtershow/presets/PresetManagementDialog.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * 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. + */ + +package com.android.gallery3d.filtershow.presets; + +import android.os.Bundle; +import android.support.v4.app.DialogFragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ListView; +import com.android.gallery3d.R; +import com.android.gallery3d.filtershow.FilterShowActivity; + +public class PresetManagementDialog extends DialogFragment implements View.OnClickListener { + private UserPresetsAdapter mAdapter; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.filtershow_presets_management_dialog, container); + + FilterShowActivity activity = (FilterShowActivity) getActivity(); + mAdapter = activity.getUserPresetsAdapter(); + ListView panel = (ListView) view.findViewById(R.id.listItems); + panel.setAdapter(mAdapter); + + view.findViewById(R.id.cancel).setOnClickListener(this); + view.findViewById(R.id.addpreset).setOnClickListener(this); + view.findViewById(R.id.ok).setOnClickListener(this); + getDialog().setTitle(getString(R.string.filtershow_manage_preset)); + return view; + } + + @Override + public void onClick(View v) { + FilterShowActivity activity = (FilterShowActivity) getActivity(); + switch (v.getId()) { + case R.id.cancel: + mAdapter.clearChangedRepresentations(); + mAdapter.clearDeletedRepresentations(); + activity.updateUserPresetsFromAdapter(mAdapter); + dismiss(); + break; + case R.id.addpreset: + activity.saveCurrentImagePreset(); + dismiss(); + break; + case R.id.ok: + mAdapter.updateCurrent(); + activity.updateUserPresetsFromAdapter(mAdapter); + dismiss(); + break; + } + } +} diff --git a/src/com/android/gallery3d/filtershow/presets/UserPresetsAdapter.java b/src/com/android/gallery3d/filtershow/presets/UserPresetsAdapter.java new file mode 100644 index 000000000..dab9ea454 --- /dev/null +++ b/src/com/android/gallery3d/filtershow/presets/UserPresetsAdapter.java @@ -0,0 +1,171 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * 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. + */ + +package com.android.gallery3d.filtershow.presets; + +import android.content.Context; +import android.graphics.Rect; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.EditText; +import android.widget.ImageButton; +import android.widget.ImageView; +import com.android.gallery3d.R; +import com.android.gallery3d.filtershow.category.Action; +import com.android.gallery3d.filtershow.category.CategoryView; +import com.android.gallery3d.filtershow.filters.FilterRepresentation; +import com.android.gallery3d.filtershow.filters.FilterUserPresetRepresentation; + +import java.util.ArrayList; + +public class UserPresetsAdapter extends ArrayAdapter<Action> + implements View.OnClickListener, View.OnFocusChangeListener { + private static final String LOGTAG = "UserPresetsAdapter"; + private LayoutInflater mInflater; + private int mIconSize = 160; + private ArrayList<FilterUserPresetRepresentation> mDeletedRepresentations = + new ArrayList<FilterUserPresetRepresentation>(); + private ArrayList<FilterUserPresetRepresentation> mChangedRepresentations = + new ArrayList<FilterUserPresetRepresentation>(); + private EditText mCurrentEditText; + + public UserPresetsAdapter(Context context, int textViewResourceId) { + super(context, textViewResourceId); + mInflater = LayoutInflater.from(context); + mIconSize = context.getResources().getDimensionPixelSize(R.dimen.category_panel_icon_size); + } + + public UserPresetsAdapter(Context context) { + this(context, 0); + } + + @Override + public void add(Action action) { + super.add(action); + action.setAdapter(this); + } + + private void deletePreset(Action action) { + FilterRepresentation rep = action.getRepresentation(); + if (rep instanceof FilterUserPresetRepresentation) { + mDeletedRepresentations.add((FilterUserPresetRepresentation) rep); + } + remove(action); + notifyDataSetChanged(); + } + + private void changePreset(Action action) { + FilterRepresentation rep = action.getRepresentation(); + rep.setName(action.getName()); + if (rep instanceof FilterUserPresetRepresentation) { + mChangedRepresentations.add((FilterUserPresetRepresentation) rep); + } + } + + public void updateCurrent() { + if (mCurrentEditText != null) { + updateActionFromEditText(mCurrentEditText); + } + } + + static class UserPresetViewHolder { + ImageView imageView; + EditText editText; + ImageButton deleteButton; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + UserPresetViewHolder viewHolder; + if (convertView == null) { + convertView = mInflater.inflate(R.layout.filtershow_presets_management_row, null); + viewHolder = new UserPresetViewHolder(); + viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageView); + viewHolder.editText = (EditText) convertView.findViewById(R.id.editView); + viewHolder.deleteButton = (ImageButton) convertView.findViewById(R.id.deleteUserPreset); + viewHolder.editText.setOnClickListener(this); + viewHolder.editText.setOnFocusChangeListener(this); + viewHolder.deleteButton.setOnClickListener(this); + convertView.setTag(viewHolder); + } else { + viewHolder = (UserPresetViewHolder) convertView.getTag(); + } + Action action = getItem(position); + viewHolder.imageView.setImageBitmap(action.getImage()); + if (action.getImage() == null) { + // queue image rendering for this action + action.setImageFrame(new Rect(0, 0, mIconSize, mIconSize), CategoryView.VERTICAL); + } + viewHolder.deleteButton.setTag(action); + viewHolder.editText.setTag(action); + viewHolder.editText.setHint(action.getName()); + + return convertView; + } + + public ArrayList<FilterUserPresetRepresentation> getDeletedRepresentations() { + return mDeletedRepresentations; + } + + public void clearDeletedRepresentations() { + mDeletedRepresentations.clear(); + } + + public ArrayList<FilterUserPresetRepresentation> getChangedRepresentations() { + return mChangedRepresentations; + } + + public void clearChangedRepresentations() { + mChangedRepresentations.clear(); + } + + @Override + public void onClick(View v) { + switch (v.getId()) { + case R.id.editView: + v.requestFocus(); + break; + case R.id.deleteUserPreset: + Action action = (Action) v.getTag(); + deletePreset(action); + break; + } + } + + @Override + public void onFocusChange(View v, boolean hasFocus) { + if (v.getId() != R.id.editView) { + return; + } + EditText editText = (EditText) v; + if (!hasFocus) { + updateActionFromEditText(editText); + } else { + mCurrentEditText = editText; + } + } + + private void updateActionFromEditText(EditText editText) { + Action action = (Action) editText.getTag(); + String newName = editText.getText().toString(); + if (newName.length() > 0) { + action.setName(editText.getText().toString()); + changePreset(action); + } + } +} |