1/*
2 * Copyright (C) 2010 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.photoeditor;
18
19import android.content.Context;
20import android.content.res.Configuration;
21import android.util.AttributeSet;
22import android.widget.ViewSwitcher;
23
24import com.android.gallery3d.R;
25
26/**
27 * Action bar that contains buttons such as undo, redo, save, etc.
28 */
29public class ActionBar extends RestorableView {
30
31    public ActionBar(Context context, AttributeSet attrs) {
32        super(context, attrs);
33    }
34
35    @Override
36    protected int childLayoutId() {
37        return R.layout.photoeditor_actionbar;
38    }
39
40    @Override
41    protected void onLayout(boolean changed, int l, int t, int r, int b) {
42        super.onLayout(changed, l, t, r, b);
43
44        // Show the action-bar title only when there's still room for it; otherwise, hide it.
45        int width = 0;
46        for (int i = 0; i < getChildCount(); i++) {
47            width += getChildAt(i).getWidth();
48        }
49        findViewById(R.id.action_bar_title).setVisibility(((width > r - l)) ? INVISIBLE: VISIBLE);
50    }
51
52    @Override
53    protected void onFinishInflate() {
54        super.onFinishInflate();
55        updateButtons(false, false);
56    }
57
58    @Override
59    protected void onConfigurationChanged(Configuration newConfig) {
60        super.onConfigurationChanged(newConfig);
61        showSaveOrShare();
62    }
63
64    /**
65     * Save/share button may need being switched when undo/save enabled status is changed/restored.
66     */
67    private void showSaveOrShare() {
68        // Show share-button only after photo is edited and saved; otherwise, show save-button.
69        boolean showShare = findViewById(R.id.undo_button).isEnabled()
70                && !findViewById(R.id.save_button).isEnabled();
71        ViewSwitcher switcher = (ViewSwitcher) findViewById(R.id.save_share_buttons);
72        int next = switcher.getNextView().getId();
73        if ((showShare && (next == R.id.share_button))
74                || (!showShare && (next == R.id.save_button))) {
75            switcher.showNext();
76        }
77    }
78
79    public void updateButtons(boolean canUndo, boolean canRedo) {
80        setViewEnabled(R.id.undo_button, canUndo);
81        setViewEnabled(R.id.redo_button, canRedo);
82        setViewEnabled(R.id.save_button, canUndo);
83        showSaveOrShare();
84    }
85
86    public void updateSave(boolean canSave) {
87        setViewEnabled(R.id.save_button, canSave);
88        showSaveOrShare();
89    }
90
91    public void clickBack() {
92        findViewById(R.id.action_bar_back).performClick();
93    }
94
95    public boolean canSave() {
96        return findViewById(R.id.save_button).isEnabled();
97    }
98}
99