Home | History | Annotate | Download | only in editors
      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.editors;
     18 
     19 import android.app.Activity;
     20 import android.os.Bundle;
     21 import android.support.v4.app.Fragment;
     22 import android.support.v4.app.FragmentTransaction;
     23 import android.view.LayoutInflater;
     24 import android.view.View;
     25 import android.view.ViewGroup;
     26 import android.widget.Button;
     27 import android.widget.ImageButton;
     28 import android.widget.LinearLayout;
     29 import com.android.gallery3d.R;
     30 import com.android.gallery3d.filtershow.FilterShowActivity;
     31 import com.android.gallery3d.filtershow.HistoryAdapter;
     32 import com.android.gallery3d.filtershow.category.MainPanel;
     33 import com.android.gallery3d.filtershow.imageshow.MasterImage;
     34 import com.android.gallery3d.filtershow.state.StatePanel;
     35 
     36 public class EditorPanel extends Fragment {
     37 
     38     private static final String LOGTAG = "EditorPanel";
     39 
     40     private LinearLayout mMainView;
     41     private Editor mEditor;
     42     private int mEditorID;
     43 
     44     public void setEditor(int editor) {
     45         mEditorID = editor;
     46     }
     47 
     48     @Override
     49     public void onAttach(Activity activity) {
     50         super.onAttach(activity);
     51         FilterShowActivity filterShowActivity = (FilterShowActivity) activity;
     52         mEditor = filterShowActivity.getEditor(mEditorID);
     53     }
     54 
     55     public void cancelCurrentFilter() {
     56         MasterImage masterImage = MasterImage.getImage();
     57         HistoryAdapter adapter = masterImage.getHistory();
     58 
     59         int position = adapter.undo();
     60         masterImage.onHistoryItemClick(position);
     61         ((FilterShowActivity)getActivity()).invalidateViews();
     62     }
     63 
     64     @Override
     65     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     66                              Bundle savedInstanceState) {
     67         FilterShowActivity activity = (FilterShowActivity) getActivity();
     68         if (mMainView != null) {
     69             if (mMainView.getParent() != null) {
     70                 ViewGroup parent = (ViewGroup) mMainView.getParent();
     71                 parent.removeView(mMainView);
     72             }
     73             showImageStatePanel(activity.isShowingImageStatePanel());
     74             return mMainView;
     75         }
     76         mMainView = (LinearLayout) inflater.inflate(R.layout.filtershow_editor_panel, null);
     77 
     78         View actionControl = mMainView.findViewById(R.id.panelAccessoryViewList);
     79         View editControl = mMainView.findViewById(R.id.controlArea);
     80         ImageButton cancelButton = (ImageButton) mMainView.findViewById(R.id.cancelFilter);
     81         ImageButton applyButton = (ImageButton) mMainView.findViewById(R.id.applyFilter);
     82         Button editTitle = (Button) mMainView.findViewById(R.id.applyEffect);
     83         cancelButton.setOnClickListener(new View.OnClickListener() {
     84             @Override
     85             public void onClick(View v) {
     86                 cancelCurrentFilter();
     87                 FilterShowActivity activity = (FilterShowActivity) getActivity();
     88                 activity.backToMain();
     89             }
     90         });
     91         applyButton.setOnClickListener(new View.OnClickListener() {
     92             @Override
     93             public void onClick(View v) {
     94                 MasterImage.getImage().invalidateFiltersOnly();
     95                 FilterShowActivity activity = (FilterShowActivity) getActivity();
     96                 activity.backToMain();
     97             }
     98         });
     99 
    100         Button toggleState = (Button) mMainView.findViewById(R.id.toggle_state);
    101 
    102         mEditor = activity.getEditor(mEditorID);
    103         if (mEditor != null) {
    104             mEditor.setUpEditorUI(actionControl, editControl, editTitle, toggleState);
    105             mEditor.getImageShow().select();
    106             mEditor.reflectCurrentFilter();
    107             if (mEditor.useUtilityPanel()) {
    108                 mEditor.openUtilityPanel((LinearLayout) actionControl);
    109             }
    110         }
    111 
    112         showImageStatePanel(activity.isShowingImageStatePanel());
    113         return mMainView;
    114     }
    115 
    116     @Override
    117     public void onDetach() {
    118         if (mEditor != null) {
    119             mEditor.detach();
    120         }
    121         super.onDetach();
    122     }
    123 
    124     public void showImageStatePanel(boolean show) {
    125         if (mMainView.findViewById(R.id.state_panel_container) == null) {
    126             return;
    127         }
    128         FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    129         Fragment panel = getActivity().getSupportFragmentManager().findFragmentByTag(
    130                 MainPanel.FRAGMENT_TAG);
    131         if (panel == null || panel instanceof MainPanel) {
    132             transaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
    133         }
    134         if (show) {
    135             StatePanel statePanel = new StatePanel();
    136             transaction.replace(R.id.state_panel_container, statePanel, StatePanel.FRAGMENT_TAG);
    137         } else {
    138             Fragment statePanel = getChildFragmentManager().findFragmentByTag(StatePanel.FRAGMENT_TAG);
    139             if (statePanel != null) {
    140                 transaction.remove(statePanel);
    141             }
    142         }
    143         transaction.commit();
    144     }
    145 }
    146