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
17package com.android.gallery3d.filtershow.category;
18
19import android.app.Activity;
20import android.graphics.Rect;
21import android.os.Bundle;
22import android.support.v4.app.Fragment;
23import android.view.LayoutInflater;
24import android.view.View;
25import android.view.ViewGroup;
26import android.widget.ImageButton;
27import android.widget.LinearLayout;
28import android.widget.ListView;
29import android.widget.TextView;
30import com.android.gallery3d.R;
31import com.android.gallery3d.filtershow.FilterShowActivity;
32
33public class CategoryPanel extends Fragment implements View.OnClickListener {
34
35    public static final String FRAGMENT_TAG = "CategoryPanel";
36    private static final String PARAMETER_TAG = "currentPanel";
37
38    private int mCurrentAdapter = MainPanel.LOOKS;
39    private CategoryAdapter mAdapter;
40    private IconView mAddButton;
41
42    public void setAdapter(int value) {
43        mCurrentAdapter = value;
44    }
45
46    @Override
47    public void onAttach(Activity activity) {
48        super.onAttach(activity);
49        loadAdapter(mCurrentAdapter);
50    }
51
52    public void loadAdapter(int adapter) {
53        FilterShowActivity activity = (FilterShowActivity) getActivity();
54        switch (adapter) {
55            case MainPanel.LOOKS: {
56                mAdapter = activity.getCategoryLooksAdapter();
57                if (mAdapter != null) {
58                    mAdapter.initializeSelection(MainPanel.LOOKS);
59                }
60                activity.updateCategories();
61                break;
62            }
63            case MainPanel.BORDERS: {
64                mAdapter = activity.getCategoryBordersAdapter();
65                if (mAdapter != null) {
66                    mAdapter.initializeSelection(MainPanel.BORDERS);
67                }
68                activity.updateCategories();
69                break;
70            }
71            case MainPanel.GEOMETRY: {
72                mAdapter = activity.getCategoryGeometryAdapter();
73                if (mAdapter != null) {
74                    mAdapter.initializeSelection(MainPanel.GEOMETRY);
75                }
76                break;
77            }
78            case MainPanel.FILTERS: {
79                mAdapter = activity.getCategoryFiltersAdapter();
80                if (mAdapter != null) {
81                    mAdapter.initializeSelection(MainPanel.FILTERS);
82                }
83                break;
84            }
85            case MainPanel.VERSIONS: {
86                mAdapter = activity.getCategoryVersionsAdapter();
87                if (mAdapter != null) {
88                    mAdapter.initializeSelection(MainPanel.VERSIONS);
89                }
90                break;
91            }
92        }
93        updateAddButtonVisibility();
94    }
95
96    @Override
97    public void onSaveInstanceState(Bundle state) {
98        super.onSaveInstanceState(state);
99        state.putInt(PARAMETER_TAG, mCurrentAdapter);
100    }
101
102    @Override
103    public View onCreateView(LayoutInflater inflater, ViewGroup container,
104                             Bundle savedInstanceState) {
105        LinearLayout main = (LinearLayout) inflater.inflate(
106                R.layout.filtershow_category_panel_new, container,
107                false);
108
109        if (savedInstanceState != null) {
110            int selectedPanel = savedInstanceState.getInt(PARAMETER_TAG);
111            loadAdapter(selectedPanel);
112        }
113
114        View panelView = main.findViewById(R.id.listItems);
115        if (panelView instanceof CategoryTrack) {
116            CategoryTrack panel = (CategoryTrack) panelView;
117            if (mAdapter != null) {
118                mAdapter.setOrientation(CategoryView.HORIZONTAL);
119                panel.setAdapter(mAdapter);
120                mAdapter.setContainer(panel);
121            }
122        } else if (mAdapter != null) {
123            ListView panel = (ListView) main.findViewById(R.id.listItems);
124            panel.setAdapter(mAdapter);
125            mAdapter.setContainer(panel);
126        }
127
128        mAddButton = (IconView) main.findViewById(R.id.addButton);
129        if (mAddButton != null) {
130            mAddButton.setOnClickListener(this);
131            updateAddButtonVisibility();
132        }
133        return main;
134    }
135
136    @Override
137    public void onClick(View v) {
138        switch (v.getId()) {
139            case R.id.addButton:
140                FilterShowActivity activity = (FilterShowActivity) getActivity();
141                activity.addCurrentVersion();
142                break;
143        }
144    }
145
146    public void updateAddButtonVisibility() {
147        if (mAddButton == null) {
148            return;
149        }
150        FilterShowActivity activity = (FilterShowActivity) getActivity();
151        if (activity.isShowingImageStatePanel() && mAdapter.showAddButton()) {
152            mAddButton.setVisibility(View.VISIBLE);
153            if (mAdapter != null) {
154                mAddButton.setText(mAdapter.getAddButtonText());
155            }
156        } else {
157            mAddButton.setVisibility(View.GONE);
158        }
159    }
160}
161