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.commitAllowingStateLoss(); 147 } 148 149 public void loadCategoryLookPanel(boolean force) { 150 if (!force && mCurrentSelected == LOOKS) { 151 return; 152 } 153 boolean fromRight = isRightAnimation(LOOKS); 154 selection(mCurrentSelected, false); 155 CategoryPanel categoryPanel = new CategoryPanel(); 156 categoryPanel.setAdapter(LOOKS); 157 setCategoryFragment(categoryPanel, fromRight); 158 mCurrentSelected = LOOKS; 159 selection(mCurrentSelected, true); 160 } 161 162 public void loadCategoryBorderPanel() { 163 if (mCurrentSelected == BORDERS) { 164 return; 165 } 166 boolean fromRight = isRightAnimation(BORDERS); 167 selection(mCurrentSelected, false); 168 CategoryPanel categoryPanel = new CategoryPanel(); 169 categoryPanel.setAdapter(BORDERS); 170 setCategoryFragment(categoryPanel, fromRight); 171 mCurrentSelected = BORDERS; 172 selection(mCurrentSelected, true); 173 } 174 175 public void loadCategoryGeometryPanel() { 176 if (mCurrentSelected == GEOMETRY) { 177 return; 178 } 179 boolean fromRight = isRightAnimation(GEOMETRY); 180 selection(mCurrentSelected, false); 181 CategoryPanel categoryPanel = new CategoryPanel(); 182 categoryPanel.setAdapter(GEOMETRY); 183 setCategoryFragment(categoryPanel, fromRight); 184 mCurrentSelected = GEOMETRY; 185 selection(mCurrentSelected, true); 186 } 187 188 public void loadCategoryFiltersPanel() { 189 if (mCurrentSelected == FILTERS) { 190 return; 191 } 192 boolean fromRight = isRightAnimation(FILTERS); 193 selection(mCurrentSelected, false); 194 CategoryPanel categoryPanel = new CategoryPanel(); 195 categoryPanel.setAdapter(FILTERS); 196 setCategoryFragment(categoryPanel, fromRight); 197 mCurrentSelected = FILTERS; 198 selection(mCurrentSelected, true); 199 } 200 201 public void loadCategoryVersionsPanel() { 202 if (mCurrentSelected == VERSIONS) { 203 return; 204 } 205 FilterShowActivity activity = (FilterShowActivity) getActivity(); 206 activity.updateVersions(); 207 boolean fromRight = isRightAnimation(VERSIONS); 208 selection(mCurrentSelected, false); 209 CategoryPanel categoryPanel = new CategoryPanel(); 210 categoryPanel.setAdapter(VERSIONS); 211 setCategoryFragment(categoryPanel, fromRight); 212 mCurrentSelected = VERSIONS; 213 selection(mCurrentSelected, true); 214 } 215 216 public void showPanel(int currentPanel) { 217 switch (currentPanel) { 218 case LOOKS: { 219 loadCategoryLookPanel(false); 220 break; 221 } 222 case BORDERS: { 223 loadCategoryBorderPanel(); 224 break; 225 } 226 case GEOMETRY: { 227 loadCategoryGeometryPanel(); 228 break; 229 } 230 case FILTERS: { 231 loadCategoryFiltersPanel(); 232 break; 233 } 234 case VERSIONS: { 235 loadCategoryVersionsPanel(); 236 break; 237 } 238 } 239 } 240 241 public void setToggleVersionsPanelButton(ImageButton button) { 242 if (button == null) { 243 return; 244 } 245 button.setOnClickListener(new View.OnClickListener() { 246 @Override 247 public void onClick(View v) { 248 if (mCurrentSelected == VERSIONS) { 249 showPanel(mPreviousToggleVersions); 250 } else { 251 mPreviousToggleVersions = mCurrentSelected; 252 showPanel(VERSIONS); 253 } 254 } 255 }); 256 } 257 258 public void showImageStatePanel(boolean show) { 259 View container = mMainView.findViewById(R.id.state_panel_container); 260 FragmentTransaction transaction = null; 261 if (container == null) { 262 FilterShowActivity activity = (FilterShowActivity) getActivity(); 263 container = activity.getMainStatePanelContainer(R.id.state_panel_container); 264 } else { 265 transaction = getChildFragmentManager().beginTransaction(); 266 } 267 if (container == null) { 268 return; 269 } else { 270 transaction = getFragmentManager().beginTransaction(); 271 } 272 int currentPanel = mCurrentSelected; 273 if (show) { 274 container.setVisibility(View.VISIBLE); 275 StatePanel statePanel = new StatePanel(); 276 statePanel.setMainPanel(this); 277 FilterShowActivity activity = (FilterShowActivity) getActivity(); 278 activity.updateVersions(); 279 transaction.replace(R.id.state_panel_container, statePanel, StatePanel.FRAGMENT_TAG); 280 } else { 281 container.setVisibility(View.GONE); 282 Fragment statePanel = getChildFragmentManager().findFragmentByTag(StatePanel.FRAGMENT_TAG); 283 if (statePanel != null) { 284 transaction.remove(statePanel); 285 } 286 if (currentPanel == VERSIONS) { 287 currentPanel = LOOKS; 288 } 289 } 290 mCurrentSelected = -1; 291 showPanel(currentPanel); 292 transaction.commit(); 293 } 294 } 295