Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 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 | |
| 17 | package com.android.gallery3d.photoeditor; |
| 18 | |
| 19 | import android.content.Context; |
Yuli Huang | b5c54b6 | 2011-10-18 12:42:50 +0800 | [diff] [blame] | 20 | import android.content.res.Configuration; |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 21 | import android.util.AttributeSet; |
Yuli Huang | 915d583 | 2011-10-05 00:01:02 +0800 | [diff] [blame] | 22 | import android.widget.ViewSwitcher; |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 23 | |
| 24 | import com.android.gallery3d.R; |
| 25 | |
| 26 | /** |
Yuli Huang | ae70ae4 | 2011-10-01 02:05:06 +0800 | [diff] [blame] | 27 | * Action bar that contains buttons such as undo, redo, save, etc. |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 28 | */ |
Yuli Huang | b5c54b6 | 2011-10-18 12:42:50 +0800 | [diff] [blame] | 29 | public class ActionBar extends RestorableView { |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 30 | |
| 31 | public ActionBar(Context context, AttributeSet attrs) { |
| 32 | super(context, attrs); |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 33 | } |
| 34 | |
Yuli Huang | 8cb3c51 | 2011-09-14 15:00:33 +0800 | [diff] [blame] | 35 | @Override |
Yuli Huang | b5c54b6 | 2011-10-18 12:42:50 +0800 | [diff] [blame] | 36 | protected int childLayoutId() { |
| 37 | return R.layout.photoeditor_actionbar; |
| 38 | } |
| 39 | |
| 40 | @Override |
Yuli Huang | 8cb3c51 | 2011-09-14 15:00:33 +0800 | [diff] [blame] | 41 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 42 | super.onLayout(changed, l, t, r, b); |
| 43 | |
Yuli Huang | ae70ae4 | 2011-10-01 02:05:06 +0800 | [diff] [blame] | 44 | // Show the action-bar title only when there's still room for it; otherwise, hide it. |
Yuli Huang | 8cb3c51 | 2011-09-14 15:00:33 +0800 | [diff] [blame] | 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 | |
Yuli Huang | ae70ae4 | 2011-10-01 02:05:06 +0800 | [diff] [blame] | 52 | @Override |
| 53 | protected void onFinishInflate() { |
| 54 | super.onFinishInflate(); |
Yuli Huang | b5c54b6 | 2011-10-18 12:42:50 +0800 | [diff] [blame] | 55 | updateButtons(false, false); |
| 56 | } |
Yuli Huang | ae70ae4 | 2011-10-01 02:05:06 +0800 | [diff] [blame] | 57 | |
Yuli Huang | b5c54b6 | 2011-10-18 12:42:50 +0800 | [diff] [blame] | 58 | @Override |
| 59 | protected void onConfigurationChanged(Configuration newConfig) { |
| 60 | super.onConfigurationChanged(newConfig); |
| 61 | showSaveOrShare(); |
Yuli Huang | ae70ae4 | 2011-10-01 02:05:06 +0800 | [diff] [blame] | 62 | } |
| 63 | |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 64 | /** |
Yuli Huang | b5c54b6 | 2011-10-18 12:42:50 +0800 | [diff] [blame] | 65 | * Save/share button may need being switched when undo/save enabled status is changed/restored. |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 66 | */ |
Yuli Huang | b5c54b6 | 2011-10-18 12:42:50 +0800 | [diff] [blame] | 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(); |
Yuli Huang | ae70ae4 | 2011-10-01 02:05:06 +0800 | [diff] [blame] | 76 | } |
Yuli Huang | ae70ae4 | 2011-10-01 02:05:06 +0800 | [diff] [blame] | 77 | } |
| 78 | |
Yuli Huang | b5c54b6 | 2011-10-18 12:42:50 +0800 | [diff] [blame] | 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(); |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 84 | } |
| 85 | |
Yuli Huang | b5c54b6 | 2011-10-18 12:42:50 +0800 | [diff] [blame] | 86 | public void updateSave(boolean canSave) { |
| 87 | setViewEnabled(R.id.save_button, canSave); |
| 88 | showSaveOrShare(); |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 89 | } |
| 90 | |
Yuli Huang | b5c54b6 | 2011-10-18 12:42:50 +0800 | [diff] [blame] | 91 | public void clickBack() { |
| 92 | findViewById(R.id.action_bar_back).performClick(); |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 93 | } |
| 94 | |
Yuli Huang | b5c54b6 | 2011-10-18 12:42:50 +0800 | [diff] [blame] | 95 | public boolean canSave() { |
| 96 | return findViewById(R.id.save_button).isEnabled(); |
Yuli Huang | 6a12ad7 | 2011-09-12 22:25:30 +0800 | [diff] [blame] | 97 | } |
| 98 | } |