OSDN Git Service

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