OSDN Git Service

merge in jb-mr2-release history after reset to jb-mr2-dev
[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 import com.android.gallery3d.R;
28 import com.android.gallery3d.filtershow.FilterShowActivity;
29 import com.android.gallery3d.filtershow.state.StatePanel;
30
31 public class MainPanel extends Fragment {
32
33     private static final String LOGTAG = "MainPanel";
34
35     private LinearLayout mMainView;
36     private ImageButton looksButton;
37     private ImageButton bordersButton;
38     private ImageButton geometryButton;
39     private ImageButton filtersButton;
40
41     public static final String FRAGMENT_TAG = "MainPanel";
42     public static final int LOOKS = 0;
43     public static final int BORDERS = 1;
44     public static final int GEOMETRY = 2;
45     public static final int FILTERS = 3;
46
47     private int mCurrentSelected = -1;
48
49     private void selection(int position, boolean value) {
50         if (value) {
51             FilterShowActivity activity = (FilterShowActivity) getActivity();
52             activity.setCurrentPanel(position);
53         }
54         switch (position) {
55             case LOOKS: {
56                 looksButton.setSelected(value);
57                 break;
58             }
59             case BORDERS: {
60                 bordersButton.setSelected(value);
61                 break;
62             }
63             case GEOMETRY: {
64                 geometryButton.setSelected(value);
65                 break;
66             }
67             case FILTERS: {
68                 filtersButton.setSelected(value);
69                 break;
70             }
71         }
72     }
73
74     @Override
75     public void onDestroyView() {
76         super.onDestroyView();
77         if (mMainView != null) {
78             if (mMainView.getParent() != null) {
79                 ViewGroup parent = (ViewGroup) mMainView.getParent();
80                 parent.removeView(mMainView);
81             }
82         }
83     }
84
85     @Override
86     public View onCreateView(LayoutInflater inflater, ViewGroup container,
87                              Bundle savedInstanceState) {
88
89         mMainView = (LinearLayout) inflater.inflate(
90                 R.layout.filtershow_main_panel, null, false);
91
92         looksButton = (ImageButton) mMainView.findViewById(R.id.fxButton);
93         bordersButton = (ImageButton) mMainView.findViewById(R.id.borderButton);
94         geometryButton = (ImageButton) mMainView.findViewById(R.id.geometryButton);
95         filtersButton = (ImageButton) mMainView.findViewById(R.id.colorsButton);
96
97         looksButton.setOnClickListener(new View.OnClickListener() {
98             @Override
99             public void onClick(View v) {
100                 showPanel(LOOKS);
101             }
102         });
103         bordersButton.setOnClickListener(new View.OnClickListener() {
104             @Override
105             public void onClick(View v) {
106                 showPanel(BORDERS);
107             }
108         });
109         geometryButton.setOnClickListener(new View.OnClickListener() {
110             @Override
111             public void onClick(View v) {
112                 showPanel(GEOMETRY);
113             }
114         });
115         filtersButton.setOnClickListener(new View.OnClickListener() {
116             @Override
117             public void onClick(View v) {
118                 showPanel(FILTERS);
119             }
120         });
121
122         FilterShowActivity activity = (FilterShowActivity) getActivity();
123         showImageStatePanel(activity.isShowingImageStatePanel());
124         showPanel(activity.getCurrentPanel());
125         return mMainView;
126     }
127
128     private boolean isRightAnimation(int newPos) {
129         if (newPos < mCurrentSelected) {
130             return false;
131         }
132         return true;
133     }
134
135     private void setCategoryFragment(CategoryPanel category, boolean fromRight) {
136         FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
137         if (fromRight) {
138             transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_right);
139         } else {
140             transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left);
141         }
142         transaction.replace(R.id.category_panel_container, category, CategoryPanel.FRAGMENT_TAG);
143         transaction.commit();
144     }
145
146     public void loadCategoryLookPanel() {
147         if (mCurrentSelected == LOOKS) {
148             return;
149         }
150         boolean fromRight = isRightAnimation(LOOKS);
151         selection(mCurrentSelected, false);
152         CategoryPanel categoryPanel = new CategoryPanel();
153         categoryPanel.setAdapter(LOOKS);
154         setCategoryFragment(categoryPanel, fromRight);
155         mCurrentSelected = LOOKS;
156         selection(mCurrentSelected, true);
157     }
158
159     public void loadCategoryBorderPanel() {
160         if (mCurrentSelected == BORDERS) {
161             return;
162         }
163         boolean fromRight = isRightAnimation(BORDERS);
164         selection(mCurrentSelected, false);
165         CategoryPanel categoryPanel = new CategoryPanel();
166         categoryPanel.setAdapter(BORDERS);
167         setCategoryFragment(categoryPanel, fromRight);
168         mCurrentSelected = BORDERS;
169         selection(mCurrentSelected, true);
170     }
171
172     public void loadCategoryGeometryPanel() {
173         if (mCurrentSelected == GEOMETRY) {
174             return;
175         }
176         boolean fromRight = isRightAnimation(GEOMETRY);
177         selection(mCurrentSelected, false);
178         CategoryPanel categoryPanel = new CategoryPanel();
179         categoryPanel.setAdapter(GEOMETRY);
180         setCategoryFragment(categoryPanel, fromRight);
181         mCurrentSelected = GEOMETRY;
182         selection(mCurrentSelected, true);
183     }
184
185     public void loadCategoryFiltersPanel() {
186         if (mCurrentSelected == FILTERS) {
187             return;
188         }
189         boolean fromRight = isRightAnimation(FILTERS);
190         selection(mCurrentSelected, false);
191         CategoryPanel categoryPanel = new CategoryPanel();
192         categoryPanel.setAdapter(FILTERS);
193         setCategoryFragment(categoryPanel, fromRight);
194         mCurrentSelected = FILTERS;
195         selection(mCurrentSelected, true);
196     }
197
198     public void showPanel(int currentPanel) {
199         switch (currentPanel) {
200             case LOOKS: {
201                 loadCategoryLookPanel();
202                 break;
203             }
204             case BORDERS: {
205                 loadCategoryBorderPanel();
206                 break;
207             }
208             case GEOMETRY: {
209                 loadCategoryGeometryPanel();
210                 break;
211             }
212             case FILTERS: {
213                 loadCategoryFiltersPanel();
214                 break;
215             }
216         }
217     }
218
219     public void showImageStatePanel(boolean show) {
220         if (mMainView.findViewById(R.id.state_panel_container) == null) {
221             return;
222         }
223         FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
224         final View container = mMainView.findViewById(R.id.state_panel_container);
225         if (show) {
226             container.setVisibility(View.VISIBLE);
227             StatePanel statePanel = new StatePanel();
228             transaction.replace(R.id.state_panel_container, statePanel, StatePanel.FRAGMENT_TAG);
229         } else {
230             container.setVisibility(View.GONE);
231             Fragment statePanel = getChildFragmentManager().findFragmentByTag(StatePanel.FRAGMENT_TAG);
232             if (statePanel != null) {
233                 transaction.remove(statePanel);
234             }
235         }
236         transaction.commit();
237     }
238 }