blob: 94379f71adc0f2617adf99e3038678455b768a7f [file] [log] [blame]
Yuli Huang6a12ad72011-09-12 22:25:30 +08001/*
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;
Yuli Huangb5c54b62011-10-18 12:42:50 +080020import android.content.res.Configuration;
Yuli Huang6a12ad72011-09-12 22:25:30 +080021import android.util.AttributeSet;
Yuli Huang915d5832011-10-05 00:01:02 +080022import android.widget.ViewSwitcher;
Yuli Huang6a12ad72011-09-12 22:25:30 +080023
24import com.android.gallery3d.R;
25
26/**
Yuli Huangae70ae42011-10-01 02:05:06 +080027 * Action bar that contains buttons such as undo, redo, save, etc.
Yuli Huang6a12ad72011-09-12 22:25:30 +080028 */
Yuli Huangb5c54b62011-10-18 12:42:50 +080029public class ActionBar extends RestorableView {
Yuli Huang6a12ad72011-09-12 22:25:30 +080030
31 public ActionBar(Context context, AttributeSet attrs) {
32 super(context, attrs);
Yuli Huang6a12ad72011-09-12 22:25:30 +080033 }
34
Yuli Huang8cb3c512011-09-14 15:00:33 +080035 @Override
Yuli Huangb5c54b62011-10-18 12:42:50 +080036 protected int childLayoutId() {
37 return R.layout.photoeditor_actionbar;
38 }
39
40 @Override
Yuli Huang8cb3c512011-09-14 15:00:33 +080041 protected void onLayout(boolean changed, int l, int t, int r, int b) {
42 super.onLayout(changed, l, t, r, b);
43
Yuli Huangae70ae42011-10-01 02:05:06 +080044 // Show the action-bar title only when there's still room for it; otherwise, hide it.
Yuli Huang8cb3c512011-09-14 15:00:33 +080045 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
Yuli Huangae70ae42011-10-01 02:05:06 +080052 @Override
53 protected void onFinishInflate() {
54 super.onFinishInflate();
Yuli Huangb5c54b62011-10-18 12:42:50 +080055 updateButtons(false, false);
56 }
Yuli Huangae70ae42011-10-01 02:05:06 +080057
Yuli Huangb5c54b62011-10-18 12:42:50 +080058 @Override
59 protected void onConfigurationChanged(Configuration newConfig) {
60 super.onConfigurationChanged(newConfig);
61 showSaveOrShare();
Yuli Huangae70ae42011-10-01 02:05:06 +080062 }
63
Yuli Huang6a12ad72011-09-12 22:25:30 +080064 /**
Yuli Huangb5c54b62011-10-18 12:42:50 +080065 * Save/share button may need being switched when undo/save enabled status is changed/restored.
Yuli Huang6a12ad72011-09-12 22:25:30 +080066 */
Yuli Huangb5c54b62011-10-18 12:42:50 +080067 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();
Yuli Huangae70ae42011-10-01 02:05:06 +080076 }
Yuli Huangae70ae42011-10-01 02:05:06 +080077 }
78
Yuli Huangb5c54b62011-10-18 12:42:50 +080079 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();
Yuli Huang6a12ad72011-09-12 22:25:30 +080084 }
85
Yuli Huangb5c54b62011-10-18 12:42:50 +080086 public void updateSave(boolean canSave) {
87 setViewEnabled(R.id.save_button, canSave);
88 showSaveOrShare();
Yuli Huang6a12ad72011-09-12 22:25:30 +080089 }
90
Yuli Huangb5c54b62011-10-18 12:42:50 +080091 public void clickBack() {
92 findViewById(R.id.action_bar_back).performClick();
Yuli Huang6a12ad72011-09-12 22:25:30 +080093 }
94
Yuli Huangb5c54b62011-10-18 12:42:50 +080095 public boolean canSave() {
96 return findViewById(R.id.save_button).isEnabled();
Yuli Huang6a12ad72011-09-12 22:25:30 +080097 }
98}