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.category;
18
19import android.os.Bundle;
20import android.support.v4.app.Fragment;
21import android.support.v4.app.FragmentTransaction;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.view.ViewGroup;
25import android.widget.ImageButton;
26import android.widget.LinearLayout;
27
28import com.android.gallery3d.R;
29import com.android.gallery3d.filtershow.FilterShowActivity;
30import com.android.gallery3d.filtershow.imageshow.MasterImage;
31import com.android.gallery3d.filtershow.state.StatePanel;
32
33public class MainPanel extends Fragment {
34
35    private static final String LOGTAG = "MainPanel";
36
37    private LinearLayout mMainView;
38    private ImageButton looksButton;
39    private ImageButton bordersButton;
40    private ImageButton geometryButton;
41    private ImageButton filtersButton;
42
43    public static final String FRAGMENT_TAG = "MainPanel";
44    public static final int LOOKS = 0;
45    public static final int BORDERS = 1;
46    public static final int GEOMETRY = 2;
47    public static final int FILTERS = 3;
48    public static final int VERSIONS = 4;
49
50    private int mCurrentSelected = -1;
51    private int mPreviousToggleVersions = -1;
52
53    private void selection(int position, boolean value) {
54        if (value) {
55            FilterShowActivity activity = (FilterShowActivity) getActivity();
56            activity.setCurrentPanel(position);
57        }
58        switch (position) {
59            case LOOKS: {
60                looksButton.setSelected(value);
61                break;
62            }
63            case BORDERS: {
64                bordersButton.setSelected(value);
65                break;
66            }
67            case GEOMETRY: {
68                geometryButton.setSelected(value);
69                break;
70            }
71            case FILTERS: {
72                filtersButton.setSelected(value);
73                break;
74            }
75        }
76    }
77
78    @Override
79    public void onDestroyView() {
80        super.onDestroyView();
81        if (mMainView != null) {
82            if (mMainView.getParent() != null) {
83                ViewGroup parent = (ViewGroup) mMainView.getParent();
84                parent.removeView(mMainView);
85            }
86        }
87    }
88
89    @Override
90    public View onCreateView(LayoutInflater inflater, ViewGroup container,
91                             Bundle savedInstanceState) {
92
93        mMainView = (LinearLayout) inflater.inflate(
94                R.layout.filtershow_main_panel, null, false);
95
96        looksButton = (ImageButton) mMainView.findViewById(R.id.fxButton);
97        bordersButton = (ImageButton) mMainView.findViewById(R.id.borderButton);
98        geometryButton = (ImageButton) mMainView.findViewById(R.id.geometryButton);
99        filtersButton = (ImageButton) mMainView.findViewById(R.id.colorsButton);
100
101        looksButton.setOnClickListener(new View.OnClickListener() {
102            @Override
103            public void onClick(View v) {
104                showPanel(LOOKS);
105            }
106        });
107        bordersButton.setOnClickListener(new View.OnClickListener() {
108            @Override
109            public void onClick(View v) {
110                showPanel(BORDERS);
111            }
112        });
113        geometryButton.setOnClickListener(new View.OnClickListener() {
114            @Override
115            public void onClick(View v) {
116                showPanel(GEOMETRY);
117            }
118        });
119        filtersButton.setOnClickListener(new View.OnClickListener() {
120            @Override
121            public void onClick(View v) {
122                showPanel(FILTERS);
123            }
124        });
125
126        FilterShowActivity activity = (FilterShowActivity) getActivity();
127        showImageStatePanel(activity.isShowingImageStatePanel());
128        showPanel(activity.getCurrentPanel());
129        return mMainView;
130    }
131
132    private boolean isRightAnimation(int newPos) {
133        if (newPos < mCurrentSelected) {
134            return false;
135        }
136        return true;
137    }
138
139    private void setCategoryFragment(CategoryPanel category, boolean fromRight) {
140        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
141        if (fromRight) {
142            transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_right);
143        } else {
144            transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left);
145        }
146        transaction.replace(R.id.category_panel_container, category, CategoryPanel.FRAGMENT_TAG);
147        transaction.commitAllowingStateLoss();
148    }
149
150    public void loadCategoryLookPanel(boolean force) {
151        if (!force && mCurrentSelected == LOOKS) {
152            return;
153        }
154        boolean fromRight = isRightAnimation(LOOKS);
155        selection(mCurrentSelected, false);
156        CategoryPanel categoryPanel = new CategoryPanel();
157        categoryPanel.setAdapter(LOOKS);
158        setCategoryFragment(categoryPanel, fromRight);
159        mCurrentSelected = LOOKS;
160        selection(mCurrentSelected, true);
161    }
162
163    public void loadCategoryBorderPanel() {
164        if (mCurrentSelected == BORDERS) {
165            return;
166        }
167        boolean fromRight = isRightAnimation(BORDERS);
168        selection(mCurrentSelected, false);
169        CategoryPanel categoryPanel = new CategoryPanel();
170        categoryPanel.setAdapter(BORDERS);
171        setCategoryFragment(categoryPanel, fromRight);
172        mCurrentSelected = BORDERS;
173        selection(mCurrentSelected, true);
174    }
175
176    public void loadCategoryGeometryPanel() {
177        if (mCurrentSelected == GEOMETRY) {
178            return;
179        }
180        if (MasterImage.getImage().hasTinyPlanet()) {
181            return;
182        }
183        boolean fromRight = isRightAnimation(GEOMETRY);
184        selection(mCurrentSelected, false);
185        CategoryPanel categoryPanel = new CategoryPanel();
186        categoryPanel.setAdapter(GEOMETRY);
187        setCategoryFragment(categoryPanel, fromRight);
188        mCurrentSelected = GEOMETRY;
189        selection(mCurrentSelected, true);
190    }
191
192    public void loadCategoryFiltersPanel() {
193        if (mCurrentSelected == FILTERS) {
194            return;
195        }
196        boolean fromRight = isRightAnimation(FILTERS);
197        selection(mCurrentSelected, false);
198        CategoryPanel categoryPanel = new CategoryPanel();
199        categoryPanel.setAdapter(FILTERS);
200        setCategoryFragment(categoryPanel, fromRight);
201        mCurrentSelected = FILTERS;
202        selection(mCurrentSelected, true);
203    }
204
205    public void loadCategoryVersionsPanel() {
206        if (mCurrentSelected == VERSIONS) {
207            return;
208        }
209        FilterShowActivity activity = (FilterShowActivity) getActivity();
210        activity.updateVersions();
211        boolean fromRight = isRightAnimation(VERSIONS);
212        selection(mCurrentSelected, false);
213        CategoryPanel categoryPanel = new CategoryPanel();
214        categoryPanel.setAdapter(VERSIONS);
215        setCategoryFragment(categoryPanel, fromRight);
216        mCurrentSelected = VERSIONS;
217        selection(mCurrentSelected, true);
218    }
219
220    public void showPanel(int currentPanel) {
221        switch (currentPanel) {
222            case LOOKS: {
223                loadCategoryLookPanel(false);
224                break;
225            }
226            case BORDERS: {
227                loadCategoryBorderPanel();
228                break;
229            }
230            case GEOMETRY: {
231                loadCategoryGeometryPanel();
232                break;
233            }
234            case FILTERS: {
235                loadCategoryFiltersPanel();
236                break;
237            }
238            case VERSIONS: {
239                loadCategoryVersionsPanel();
240                break;
241            }
242        }
243    }
244
245    public void setToggleVersionsPanelButton(ImageButton button) {
246        if (button == null) {
247            return;
248        }
249        button.setOnClickListener(new View.OnClickListener() {
250            @Override
251            public void onClick(View v) {
252                if (mCurrentSelected == VERSIONS) {
253                    showPanel(mPreviousToggleVersions);
254                } else {
255                    mPreviousToggleVersions = mCurrentSelected;
256                    showPanel(VERSIONS);
257                }
258            }
259        });
260    }
261
262    public void showImageStatePanel(boolean show) {
263        View container = mMainView.findViewById(R.id.state_panel_container);
264        FragmentTransaction transaction = null;
265        if (container == null) {
266            FilterShowActivity activity = (FilterShowActivity) getActivity();
267            container = activity.getMainStatePanelContainer(R.id.state_panel_container);
268        } else {
269            transaction = getChildFragmentManager().beginTransaction();
270        }
271        if (container == null) {
272            return;
273        } else {
274            transaction = getFragmentManager().beginTransaction();
275        }
276        int currentPanel = mCurrentSelected;
277        if (show) {
278            container.setVisibility(View.VISIBLE);
279            StatePanel statePanel = new StatePanel();
280            statePanel.setMainPanel(this);
281            FilterShowActivity activity = (FilterShowActivity) getActivity();
282            activity.updateVersions();
283            transaction.replace(R.id.state_panel_container, statePanel, StatePanel.FRAGMENT_TAG);
284        } else {
285            container.setVisibility(View.GONE);
286            Fragment statePanel = getChildFragmentManager().findFragmentByTag(StatePanel.FRAGMENT_TAG);
287            if (statePanel != null) {
288                transaction.remove(statePanel);
289            }
290            if (currentPanel == VERSIONS) {
291                currentPanel = LOOKS;
292            }
293        }
294        mCurrentSelected = -1;
295        showPanel(currentPanel);
296        transaction.commit();
297    }
298}
299