1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.gallery3d.filtershow.editors;
18
19import android.app.Activity;
20import android.os.Bundle;
21import android.support.v4.app.Fragment;
22import android.support.v4.app.FragmentTransaction;
23import android.view.LayoutInflater;
24import android.view.View;
25import android.view.ViewGroup;
26import android.widget.Button;
27import android.widget.ImageButton;
28import android.widget.LinearLayout;
29import com.android.gallery3d.R;
30import com.android.gallery3d.filtershow.FilterShowActivity;
31import com.android.gallery3d.filtershow.history.HistoryManager;
32import com.android.gallery3d.filtershow.category.MainPanel;
33import com.android.gallery3d.filtershow.imageshow.MasterImage;
34import com.android.gallery3d.filtershow.state.StatePanel;
35
36public class EditorPanel extends Fragment {
37
38    private static final String LOGTAG = "EditorPanel";
39
40    private LinearLayout mMainView;
41    private Editor mEditor;
42    private int mEditorID;
43
44    public void setEditor(int editor) {
45        mEditorID = editor;
46    }
47
48    @Override
49    public void onAttach(Activity activity) {
50        super.onAttach(activity);
51        FilterShowActivity filterShowActivity = (FilterShowActivity) activity;
52        mEditor = filterShowActivity.getEditor(mEditorID);
53    }
54
55    public void cancelCurrentFilter() {
56        MasterImage masterImage = MasterImage.getImage();
57        HistoryManager adapter = masterImage.getHistory();
58
59        int position = adapter.undo();
60        masterImage.onHistoryItemClick(position);
61        ((FilterShowActivity)getActivity()).invalidateViews();
62    }
63
64    @Override
65    public View onCreateView(LayoutInflater inflater, ViewGroup container,
66                             Bundle savedInstanceState) {
67        FilterShowActivity activity = (FilterShowActivity) getActivity();
68        if (mMainView != null) {
69            if (mMainView.getParent() != null) {
70                ViewGroup parent = (ViewGroup) mMainView.getParent();
71                parent.removeView(mMainView);
72            }
73            showImageStatePanel(activity.isShowingImageStatePanel());
74            return mMainView;
75        }
76        mMainView = (LinearLayout) inflater.inflate(R.layout.filtershow_editor_panel, null);
77
78        View actionControl = mMainView.findViewById(R.id.panelAccessoryViewList);
79        View editControl = mMainView.findViewById(R.id.controlArea);
80        ImageButton cancelButton = (ImageButton) mMainView.findViewById(R.id.cancelFilter);
81        ImageButton applyButton = (ImageButton) mMainView.findViewById(R.id.applyFilter);
82        Button editTitle = (Button) mMainView.findViewById(R.id.applyEffect);
83        cancelButton.setOnClickListener(new View.OnClickListener() {
84            @Override
85            public void onClick(View v) {
86                cancelCurrentFilter();
87                FilterShowActivity activity = (FilterShowActivity) getActivity();
88                activity.backToMain();
89            }
90        });
91
92        Button toggleState = (Button) mMainView.findViewById(R.id.toggle_state);
93        mEditor = activity.getEditor(mEditorID);
94        if (mEditor != null) {
95            mEditor.setUpEditorUI(actionControl, editControl, editTitle, toggleState);
96            mEditor.reflectCurrentFilter();
97            if (mEditor.useUtilityPanel()) {
98                mEditor.openUtilityPanel((LinearLayout) actionControl);
99            }
100        }
101        applyButton.setOnClickListener(new View.OnClickListener() {
102            @Override
103            public void onClick(View v) {
104                FilterShowActivity activity = (FilterShowActivity) getActivity();
105                mEditor.finalApplyCalled();
106                activity.backToMain();
107            }
108        });
109
110        showImageStatePanel(activity.isShowingImageStatePanel());
111        return mMainView;
112    }
113
114    @Override
115    public void onDetach() {
116        if (mEditor != null) {
117            mEditor.detach();
118        }
119        super.onDetach();
120    }
121
122    public void showImageStatePanel(boolean show) {
123        View container = mMainView.findViewById(R.id.state_panel_container);
124        FragmentTransaction transaction = null;
125        boolean child = false;
126        if (container == null) {
127            FilterShowActivity activity = (FilterShowActivity) getActivity();
128            container = activity.getMainStatePanelContainer(R.id.state_panel_container);
129        } else {
130            transaction = getChildFragmentManager().beginTransaction();
131            child = true;
132        }
133        if (container == null) {
134            return;
135        } else {
136            transaction = getFragmentManager().beginTransaction();
137        }
138        Fragment panel = getActivity().getSupportFragmentManager().findFragmentByTag(
139                MainPanel.FRAGMENT_TAG);
140        if (panel == null || panel instanceof MainPanel) {
141            transaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
142        }
143        if (show) {
144            container.setVisibility(View.VISIBLE);
145            StatePanel statePanel = new StatePanel();
146            transaction.replace(R.id.state_panel_container, statePanel, StatePanel.FRAGMENT_TAG);
147        } else {
148            Fragment statePanel = getChildFragmentManager().findFragmentByTag(StatePanel.FRAGMENT_TAG);
149            if (child) {
150                statePanel = getFragmentManager().findFragmentByTag(StatePanel.FRAGMENT_TAG);
151            }
152            if (statePanel != null) {
153                transaction.remove(statePanel);
154            }
155        }
156        transaction.commit();
157    }
158}
159