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 
17 package com.android.gallery3d.filtershow.category;
18 
19 import android.os.Bundle;
20 import androidx.fragment.app.Fragment;
21 import androidx.fragment.app.FragmentTransaction;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.widget.ImageButton;
26 import android.widget.LinearLayout;
27 
28 import com.android.gallery3d.R;
29 import com.android.gallery3d.filtershow.FilterShowActivity;
30 import com.android.gallery3d.filtershow.imageshow.PrimaryImage;
31 import com.android.gallery3d.filtershow.state.StatePanel;
32 
33 public 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 
selection(int position, boolean value)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
onDestroyView()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
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)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 
isRightAnimation(int newPos)132     private boolean isRightAnimation(int newPos) {
133         if (newPos < mCurrentSelected) {
134             return false;
135         }
136         return true;
137     }
138 
setCategoryFragment(CategoryPanel category, boolean fromRight)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 
loadCategoryLookPanel(boolean force)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 
loadCategoryBorderPanel()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 
loadCategoryGeometryPanel()176     public void loadCategoryGeometryPanel() {
177         if (mCurrentSelected == GEOMETRY) {
178             return;
179         }
180         if (PrimaryImage.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 
loadCategoryFiltersPanel()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 
loadCategoryVersionsPanel()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 
showPanel(int currentPanel)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 
setToggleVersionsPanelButton(ImageButton button)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 
showImageStatePanel(boolean show)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(
287                     StatePanel.FRAGMENT_TAG);
288             if (statePanel != null) {
289                 transaction.remove(statePanel);
290             }
291             if (currentPanel == VERSIONS) {
292                 currentPanel = LOOKS;
293             }
294         }
295         mCurrentSelected = -1;
296         showPanel(currentPanel);
297         transaction.commit();
298     }
299 }
300