Editor.java revision 6a07dfbe03c8396cc43840a1630ea480a2cff663
15aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford/*
25aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford * Copyright (C) 2012 The Android Open Source Project
35aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford *
45aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford * Licensed under the Apache License, Version 2.0 (the "License");
55aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford * you may not use this file except in compliance with the License.
65aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford * You may obtain a copy of the License at
75aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford *
85aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford *      http://www.apache.org/licenses/LICENSE-2.0
95aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford *
105aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford * Unless required by applicable law or agreed to in writing, software
115aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford * distributed under the License is distributed on an "AS IS" BASIS,
125aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford * See the License for the specific language governing permissions and
145aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford * limitations under the License.
155aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford */
165aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
175aa08941bda0cfbcfba522c53937dd66867f890dJohn Hofordpackage com.android.gallery3d.filtershow.editors;
185aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
195aa08941bda0cfbcfba522c53937dd66867f890dJohn Hofordimport android.content.Context;
206a07dfbe03c8396cc43840a1630ea480a2cff663John Hofordimport android.text.Html;
215aa08941bda0cfbcfba522c53937dd66867f890dJohn Hofordimport android.view.LayoutInflater;
226a07dfbe03c8396cc43840a1630ea480a2cff663John Hofordimport android.view.Menu;
235aa08941bda0cfbcfba522c53937dd66867f890dJohn Hofordimport android.view.View;
245aa08941bda0cfbcfba522c53937dd66867f890dJohn Hofordimport android.view.ViewGroup;
255aa08941bda0cfbcfba522c53937dd66867f890dJohn Hofordimport android.widget.FrameLayout;
265aa08941bda0cfbcfba522c53937dd66867f890dJohn Hofordimport android.widget.LinearLayout;
276a07dfbe03c8396cc43840a1630ea480a2cff663John Hofordimport android.widget.PopupMenu;
286a07dfbe03c8396cc43840a1630ea480a2cff663John Hofordimport android.widget.SeekBar;
296a07dfbe03c8396cc43840a1630ea480a2cff663John Hofordimport android.widget.SeekBar.OnSeekBarChangeListener;
305aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
316a07dfbe03c8396cc43840a1630ea480a2cff663John Hofordimport com.android.gallery3d.R;
325aa08941bda0cfbcfba522c53937dd66867f890dJohn Hofordimport com.android.gallery3d.filtershow.PanelController;
335aa08941bda0cfbcfba522c53937dd66867f890dJohn Hofordimport com.android.gallery3d.filtershow.cache.ImageLoader;
34cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroardimport com.android.gallery3d.filtershow.filters.FilterRepresentation;
355aa08941bda0cfbcfba522c53937dd66867f890dJohn Hofordimport com.android.gallery3d.filtershow.imageshow.ImageShow;
36cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroardimport com.android.gallery3d.filtershow.imageshow.MasterImage;
37cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroardimport com.android.gallery3d.filtershow.presets.ImagePreset;
385aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
395aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford/**
405aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford * Base class for Editors Must contain a mImageShow and a top level view
415aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford */
426a07dfbe03c8396cc43840a1630ea480a2cff663John Hofordpublic class Editor implements OnSeekBarChangeListener {
435aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    protected Context mContext;
445aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    protected View mView;
455aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    protected ImageShow mImageShow;
465aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    protected FrameLayout mFrameLayout;
476a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    protected SeekBar mSeekBar;
485aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    protected PanelController mPanelController;
495aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    protected int mID;
505aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    private final String LOGTAG = "Editor";
51cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard    protected FilterRepresentation mLocalRepresentation = null;
526a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    protected byte mShowParameter = SHOW_VALUE_UNDEFINED;
536a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    public static byte SHOW_VALUE_UNDEFINED = -1;
546a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    public static byte SHOW_VALUE_OFF = 0;
556a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    public static byte SHOW_VALUE_INT = 1;
565aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
575aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    public void setPanelController(PanelController panelController) {
585aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        this.mPanelController = panelController;
595aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    }
605aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
616a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    public String calculateUserMessage(Context context, String effectName, Object parameterValue) {
626a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford        String apply = context.getString(R.string.apply_effect);
636a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford        if (mShowParameter == SHOW_VALUE_INT) {
646a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford            apply += " " + effectName + " " + parameterValue;
656a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford        } else {
666a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford            apply += " " + effectName;
676a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford        }
686a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford        return apply;
696a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    }
706a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
715aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    protected Editor(int id) {
725aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        mID = id;
735aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    }
746a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
755aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    public int getID() {
765aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        return mID;
775aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    }
785aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
796a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
806a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    public byte showParameterValue() {
816a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford        return mShowParameter;
826a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    }
836a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
846a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    /**
856a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford     * @param actionButton the would be the area for menu etc
866a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford     * @param editControl this is the black area for sliders etc
876a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford     */
886a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    public void setUtilityPanelUI(View actionButton, View editControl) {
896a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford        mSeekBar = (SeekBar) editControl.findViewById(R.id.primarySeekBar);
906a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford        mSeekBar.setOnSeekBarChangeListener(this);
916a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    }
926a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
936a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    @Override
946a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    public void onProgressChanged(SeekBar sbar, int progress, boolean arg2) {
956a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
966a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    }
976a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
986a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    public void setPanel() {
996a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
1006a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    }
1016a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
1025aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    public void createEditor(Context context,FrameLayout frameLayout) {
1035aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        mContext = context;
1045aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        mFrameLayout = frameLayout;
105cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard        mLocalRepresentation = null;
1065aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    }
1075aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
1085aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    protected void unpack(int viewid, int layoutid) {
1095aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
1105aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        if (mView == null) {
1115aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford            mView = mFrameLayout.findViewById(viewid);
1125aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford            if (mView == null) {
1135aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService
1145aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford                        (Context.LAYOUT_INFLATER_SERVICE);
1155aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford                mView = inflater.inflate(layoutid, mFrameLayout, false);
1165aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford                mFrameLayout.addView(mView, mView.getLayoutParams());
1175aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford            }
1185aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        }
1195aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        mImageShow = findImageShow(mView);
1205aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    }
1215aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
1225aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    private ImageShow findImageShow(View view) {
1235aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        if (view instanceof ImageShow) {
1245aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford            return (ImageShow) view;
1255aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        }
1265aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        if (!(view instanceof ViewGroup)) {
1275aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford            return null;
1285aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        }
1295aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        ViewGroup vg = (ViewGroup) view;
1305aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        int n = vg.getChildCount();
1315aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        for (int i = 0; i < n; i++) {
1325aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford            View v = vg.getChildAt(i);
1335aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford            if (v instanceof ImageShow) {
1345aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford                return (ImageShow) v;
1355aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford            } else if (v instanceof ViewGroup) {
1365aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford                return findImageShow(v);
1375aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford            }
1385aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        }
1395aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        return null;
1405aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    }
1415aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
1425aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    public View getTopLevelView() {
1435aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        return mView;
1445aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    }
1455aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
1465aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    public ImageShow getImageShow() {
1475aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        return mImageShow;
1485aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    }
1495aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
1505aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    public void setImageLoader(ImageLoader imageLoader) {
1515aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        mImageShow.setImageLoader(imageLoader);
1525aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    }
1535aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
1545aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    public void setVisibility(int visible) {
1555aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        mView.setVisibility(visible);
1565aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    }
1575aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
158cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard    public FilterRepresentation getLocalRepresentation() {
159cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard        if (mLocalRepresentation == null) {
160cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard            ImagePreset preset = MasterImage.getImage().getPreset();
161cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard            FilterRepresentation filterRepresentation = MasterImage.getImage().getCurrentFilterRepresentation();
162cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard            mLocalRepresentation = preset.getFilterRepresentationCopyFrom(filterRepresentation);
1636a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford            if (mShowParameter == SHOW_VALUE_UNDEFINED) {
1646a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford                boolean show = filterRepresentation.showParameterValue();
1656a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford                mShowParameter = show ? SHOW_VALUE_INT : SHOW_VALUE_OFF;
1666a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford            }
167cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard        }
1686a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
169cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard        return mLocalRepresentation;
170cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard    }
171cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard
172cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard    public void commitLocalRepresentation() {
173cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard        ImagePreset preset = MasterImage.getImage().getPreset();
174cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard        preset.updateFilterRepresentation(getLocalRepresentation());
175cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard    }
176cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard
1775aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    /**
1785aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford     * called after the filter is set and the select is called
1795aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford     */
1805aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    public void reflectCurrentFilter() {
181cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard        mLocalRepresentation = null;
1825aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    }
1835aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
1845aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    public boolean useUtilityPanel() {
1855aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        if (mImageShow != null) {
1865aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford            return mImageShow.useUtilityPanel();
1875aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        }
1885aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        return false;
1895aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    }
1905aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
1915aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    public void openUtilityPanel(LinearLayout mAccessoryViewList) {
1925aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        if (mImageShow != null) {
1935aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford            mImageShow.openUtilityPanel(mAccessoryViewList);
1945aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        }
1955aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    }
1965aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
1976a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    protected void createMenu(int[] strId, View button) {
1986a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford        PopupMenu pmenu = new PopupMenu(mContext, button);
1996a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford        Menu menu = pmenu.getMenu();
2006a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford        for (int i = 0; i < strId.length; i++) {
2016a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford            menu.add(Menu.NONE, Menu.FIRST + i, 0, mContext.getString(strId[i]));
2026a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford        }
2036a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    }
2046a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
2056a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    @Override
2066a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    public void onStartTrackingTouch(SeekBar arg0) {
2076a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
2086a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    }
2096a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
2106a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    @Override
2116a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    public void onStopTrackingTouch(SeekBar arg0) {
2126a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
2136a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    }
2146a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
2155aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford}
216