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.view.LayoutInflater;
23import android.view.View;
24import android.widget.FrameLayout;
25
26import java.util.HashMap;
27import java.util.HashSet;
28import java.util.Map.Entry;
29
30/**
31 * View that holds a single child and could be recreated/restored after orientation changes.
32 */
33public abstract class RestorableView extends FrameLayout {
34
35    private static final float ENABLED_ALPHA = 1;
36    private static final float DISABLED_ALPHA = 0.47f;
37
38    private final HashMap<Integer, Runnable> clickRunnables = new HashMap<Integer, Runnable>();
39    private final HashSet<Integer> changedViews = new HashSet<Integer>();
40    private final LayoutInflater inflater;
41
42    public RestorableView(Context context, AttributeSet attrs) {
43        super(context, attrs);
44        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
45    }
46
47    protected abstract int childLayoutId();
48
49    private void recreateChildView() {
50        if (getChildCount() != 0) {
51            removeAllViews();
52        }
53        inflater.inflate(childLayoutId(), this, true);
54    }
55
56    @Override
57    protected void onFinishInflate() {
58        super.onFinishInflate();
59        recreateChildView();
60    }
61
62    @Override
63    protected void onConfigurationChanged(Configuration newConfig) {
64        super.onConfigurationChanged(newConfig);
65
66        // Remember the removing child before recreating the child.
67        View view = getChildAt(0);
68        recreateChildView();
69
70        // Restore its runnables and status of views that have been changed.
71        for (Entry<Integer, Runnable> entry : clickRunnables.entrySet()) {
72            setClickRunnable(entry.getKey(), entry.getValue());
73        }
74        for (int id : changedViews) {
75            View changed = view.findViewById(id);
76            setViewEnabled(id, changed.isEnabled());
77            setViewSelected(id, changed.isSelected());
78        }
79    }
80
81    public void setClickRunnable(int id, final Runnable r) {
82        findViewById(id).setOnClickListener(new OnClickListener() {
83
84            @Override
85            public void onClick(View v) {
86                if (isEnabled()) {
87                    r.run();
88                }
89            }
90        });
91        clickRunnables.put(id, r);
92    }
93
94    public void setViewEnabled(int id, boolean enabled) {
95        View view = findViewById(id);
96        view.setEnabled(enabled);
97        view.setAlpha(enabled ? ENABLED_ALPHA : DISABLED_ALPHA);
98        // Track views whose enabled status has been updated.
99        changedViews.add(id);
100    }
101
102    public void setViewSelected(int id, boolean selected) {
103        findViewById(id).setSelected(selected);
104        // Track views whose selected status has been updated.
105        changedViews.add(id);
106    }
107}
108