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